source: src/GenPoly/GenPoly.cc@ 6d160d7

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 6d160d7 was 32805db, checked in by Aaron Moss <a3moss@…>, 10 years ago

Switched parameter adder over to add for pointer*-to-polymorphic-generic as well

  • Property mode set to 100644
File size: 7.6 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// GenPoly.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Dec 15 16:11:18 2015
13// Update Count : 13
14//
15
16#include "GenPoly.h"
17
18#include "SymTab/Mangler.h"
19#include "SynTree/Expression.h"
20#include "SynTree/Type.h"
21
22#include <iostream>
23using namespace std;
24
25namespace GenPoly {
26 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
27 if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
28 return true;
29 } // if
30 for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
31 if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
32 return true;
33 } // if
34 } // for
35 return false;
36 }
37
38 ReferenceToType *isPolyRet( FunctionType *function ) {
39 if ( ! function->get_returnVals().empty() ) {
40 TyVarMap forallTypes;
41 makeTyVarMap( function, forallTypes );
42 return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes );
43 } // if
44 return 0;
45 }
46
47 namespace {
48 /// Checks a parameter list for polymorphic parameters; will substitute according to env if present
49 bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
50 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
51 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
52 assert(paramType && "Aggregate parameters should be type expressions");
53 if ( isPolyType( paramType->get_type(), env ) ) return true;
54 }
55 return false;
56 }
57
58 /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
59 bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
60 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
61 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
62 assert(paramType && "Aggregate parameters should be type expressions");
63 if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
64 }
65 return false;
66 }
67 }
68
69 Type *isPolyType( Type *type, const TypeSubstitution *env ) {
70 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
71 if ( env ) {
72 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
73 return isPolyType( newType, env );
74 } // if
75 } // if
76 return type;
77 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
78 if ( hasPolyParams( structType->get_parameters(), env ) ) return type;
79 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
80 if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;
81 }
82 return 0;
83 }
84
85 Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
86 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
87 if ( env ) {
88 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
89 return isPolyType( newType, tyVars, env );
90 } // if
91 } // if
92 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
93 return type;
94 }
95 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
96 if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
97 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
98 if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
99 }
100 return 0;
101 }
102
103 Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {
104 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
105 return isPolyType( ptr->get_base(), env );
106 } else if ( env ) {
107 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
108 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
109 return isPolyPtr( newType, env );
110 } // if
111 } // if
112 } // if
113 return 0;
114 }
115
116 Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
117 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
118 return isPolyType( ptr->get_base(), tyVars, env );
119 } else if ( env ) {
120 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
121 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
122 return isPolyPtr( newType, tyVars, env );
123 } // if
124 } // if
125 } // if
126 return 0;
127 }
128
129 Type *hasPolyBase( Type *type, const TypeSubstitution *env ) {
130 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
131 return hasPolyBase( ptr->get_base(), env );
132 } else if ( env ) {
133 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
134 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
135 return hasPolyBase( newType, env );
136 } // if
137 } // if
138 }
139
140 return isPolyType( type, env );
141 }
142
143 Type *hasPolyBase( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
144 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
145 return hasPolyBase( ptr->get_base(), tyVars, env );
146 } else if ( env ) {
147 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
148 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
149 return hasPolyBase( newType, tyVars, env );
150 } // if
151 } // if
152 }
153
154 return isPolyType( type, tyVars, env );
155 }
156
157 FunctionType * getFunctionType( Type *ty ) {
158 PointerType *ptrType;
159 if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
160 return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
161 } else {
162 return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
163 }
164 }
165
166 VariableExpr *getBaseVar( Expression *expr ) {
167 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) {
168 // found the variable directly
169 return varExpr;
170 } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) {
171 return getBaseVar( addressExpr->get_arg() );
172 } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) {
173 // look for compiler-inserted dereference operator
174 NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() );
175 if ( ! fn || fn->get_name() != std::string("*?") ) return 0;
176 return getBaseVar( *untypedExpr->begin_args() );
177 } else return 0;
178 }
179
180 void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
181 for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
182 assert( *tyVar );
183 tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind();
184 }
185 if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
186 makeTyVarMap( pointer->get_base(), tyVarMap );
187 }
188 }
189
190 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
191 for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
192 os << i->first << " (" << i->second << ") ";
193 } // for
194 os << std::endl;
195 }
196
197 std::string sizeofName( Type *ty ) {
198 return std::string( "_sizeof_" ) + SymTab::Mangler::mangleType( ty );
199 }
200
201 std::string alignofName( Type *ty ) {
202 return std::string( "_alignof_" ) + SymTab::Mangler::mangleType( ty );
203 }
204
205 std::string offsetofName( Type* ty ) {
206 return std::string( "_offsetof_" ) + SymTab::Mangler::mangleType( ty );
207 }
208
209} // namespace GenPoly
210
211// Local Variables: //
212// tab-width: 4 //
213// mode: c++ //
214// compile-command: "make install" //
215// End: //
Note: See TracBrowser for help on using the repository browser.