Home | History | Annotate | Download | only in glsl
      1 /*
      2  * Copyright  2010 Intel Corporation
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     21  * DEALINGS IN THE SOFTWARE.
     22  */
     23 
     24 /**
     25  * \file ir_import_prototypes.cpp
     26  * Import function prototypes from one IR tree into another.
     27  *
     28  * \author Ian Romanick
     29  */
     30 #include "ir.h"
     31 #include "glsl_symbol_table.h"
     32 
     33 /**
     34  * Visitor used to import function prototypes
     35  *
     36  * Normally the \c clone method of either \c ir_function or
     37  * \c ir_function_signature could be used.  However, we don't want a complete
     38  * clone of the \c ir_function_signature.  We want everything \b except the
     39  * body of the function.
     40  */
     41 class import_prototype_visitor : public ir_hierarchical_visitor {
     42 public:
     43    /**
     44     */
     45    import_prototype_visitor(exec_list *list, glsl_symbol_table *symbols,
     46 			    void *mem_ctx)
     47    {
     48       this->mem_ctx = mem_ctx;
     49       this->list = list;
     50       this->symbols = symbols;
     51       this->function = NULL;
     52    }
     53 
     54    virtual ir_visitor_status visit_enter(ir_function *ir)
     55    {
     56       assert(this->function == NULL);
     57 
     58       this->function = this->symbols->get_function(ir->name);
     59       if (!this->function) {
     60 	 this->function = new(this->mem_ctx) ir_function(ir->name);
     61 
     62 	 list->push_tail(this->function);
     63 
     64 	 /* Add the new function to the symbol table.
     65 	  */
     66 	 this->symbols->add_function(this->function);
     67       }
     68       return visit_continue;
     69    }
     70 
     71    virtual ir_visitor_status visit_leave(ir_function *ir)
     72    {
     73       (void) ir;
     74       assert(this->function != NULL);
     75 
     76       this->function = NULL;
     77       return visit_continue;
     78    }
     79 
     80    ir_visitor_status visit_enter(ir_function_signature *ir)
     81    {
     82       assert(this->function != NULL);
     83 
     84       ir_function_signature *copy = ir->clone_prototype(mem_ctx, NULL);
     85 
     86       this->function->add_signature(copy);
     87 
     88       /* Do not process child nodes of the ir_function_signature.  There can
     89        * never be any nodes inside the ir_function_signature that we care
     90        * about.  Instead continue with the next sibling.
     91        */
     92       return visit_continue_with_parent;
     93    }
     94 
     95 private:
     96    exec_list *list;
     97    ir_function *function;
     98    glsl_symbol_table *symbols;
     99    void *mem_ctx;
    100 };
    101 
    102 
    103 /**
    104  * Import function prototypes from one IR tree into another
    105  *
    106  * \param source   Source instruction stream containing functions whose
    107  *                 prototypes are to be imported
    108  * \param dest     Destination instruction stream where new \c ir_function and
    109  *                 \c ir_function_signature nodes will be stored
    110  * \param symbols  Symbol table where new functions will be stored
    111  * \param mem_ctx  ralloc memory context used for new allocations
    112  */
    113 void
    114 import_prototypes(const exec_list *source, exec_list *dest,
    115 		  glsl_symbol_table *symbols, void *mem_ctx)
    116 {
    117    import_prototype_visitor v(dest, symbols, mem_ctx);
    118 
    119    /* Making source be const is just extra documentation.
    120     */
    121    v.run(const_cast<exec_list *>(source));
    122 }
    123