ir_import_prototypes.cpp revision 8273bd46877e2ea2b8a02b87a11c68102d07e1f2
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 <cstdio>
31#include "ir.h"
32#include "glsl_symbol_table.h"
33
34/**
35 * Visitor used to import function prototypes
36 *
37 * Normally the \c clone method of either \c ir_function or
38 * \c ir_function_signature could be used.  However, we don't want a complete
39 * clone of the \c ir_function_signature.  We want everything \b except the
40 * body of the function.
41 */
42class import_prototype_visitor : public ir_hierarchical_visitor {
43public:
44   /**
45    */
46   import_prototype_visitor(exec_list *list, glsl_symbol_table *symbols,
47			    void *mem_ctx)
48   {
49      this->mem_ctx = mem_ctx;
50      this->list = list;
51      this->symbols = symbols;
52      this->function = NULL;
53   }
54
55   virtual ir_visitor_status visit_enter(ir_function *ir)
56   {
57      assert(this->function == NULL);
58
59      this->function = this->symbols->get_function(ir->name);
60      if (!this->function) {
61	 this->function = new(this->mem_ctx) ir_function(ir->name);
62
63	 list->push_tail(this->function);
64
65	 /* Add the new function to the symbol table.
66	  */
67	 this->symbols->add_function(this->function->name, this->function);
68      }
69      return visit_continue;
70   }
71
72   virtual ir_visitor_status visit_leave(ir_function *ir)
73   {
74      (void) ir;
75      assert(this->function != NULL);
76
77      this->function = NULL;
78      return visit_continue;
79   }
80
81   ir_visitor_status visit_enter(ir_function_signature *ir)
82   {
83      assert(this->function != NULL);
84
85      ir_function_signature *copy =
86	 new(mem_ctx) ir_function_signature(ir->return_type);
87
88      copy->is_defined = false;
89      copy->is_built_in = ir->is_built_in;
90
91      /* Clone the parameter list, but NOT the body.
92       */
93      foreach_list_const(node, &ir->parameters) {
94	 const ir_variable *const param = (const ir_variable *) node;
95
96	 assert(const_cast<ir_variable *>(param)->as_variable() != NULL);
97
98	 ir_variable *const param_copy = param->clone(mem_ctx, NULL);
99	 copy->parameters.push_tail(param_copy);
100      }
101
102      this->function->add_signature(copy);
103
104      /* Do not process child nodes of the ir_function_signature.  There can
105       * never be any nodes inside the ir_function_signature that we care
106       * about.  Instead continue with the next sibling.
107       */
108      return visit_continue_with_parent;
109   }
110
111private:
112   exec_list *list;
113   ir_function *function;
114   glsl_symbol_table *symbols;
115   void *mem_ctx;
116};
117
118
119/**
120 * Import function prototypes from one IR tree into another
121 *
122 * \param source   Source instruction stream containing functions whose
123 *                 prototypes are to be imported
124 * \param dest     Destination instruction stream where new \c ir_function and
125 *                 \c ir_function_signature nodes will be stored
126 * \param symbols  Symbol table where new functions will be stored
127 * \param mem_ctx  talloc memory context used for new allocations
128 */
129void
130import_prototypes(const exec_list *source, exec_list *dest,
131		  glsl_symbol_table *symbols, void *mem_ctx)
132{
133   import_prototype_visitor v(dest, symbols, mem_ctx);
134
135   /* Making source be const is just extra documentation.
136    */
137   v.run(const_cast<exec_list *>(source));
138}
139