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 | // Box.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 : Fri Feb 5 16:45:07 2016 |
---|
13 | // Update Count : 286 |
---|
14 | // |
---|
15 | |
---|
16 | #include <algorithm> |
---|
17 | #include <iterator> |
---|
18 | #include <list> |
---|
19 | #include <map> |
---|
20 | #include <set> |
---|
21 | #include <stack> |
---|
22 | #include <string> |
---|
23 | #include <utility> |
---|
24 | #include <vector> |
---|
25 | #include <cassert> |
---|
26 | |
---|
27 | #include "Box.h" |
---|
28 | #include "DeclMutator.h" |
---|
29 | #include "PolyMutator.h" |
---|
30 | #include "FindFunction.h" |
---|
31 | #include "ScopedMap.h" |
---|
32 | #include "ScopedSet.h" |
---|
33 | #include "ScrubTyVars.h" |
---|
34 | |
---|
35 | #include "Parser/ParseNode.h" |
---|
36 | |
---|
37 | #include "SynTree/Constant.h" |
---|
38 | #include "SynTree/Declaration.h" |
---|
39 | #include "SynTree/Expression.h" |
---|
40 | #include "SynTree/Initializer.h" |
---|
41 | #include "SynTree/Mutator.h" |
---|
42 | #include "SynTree/Statement.h" |
---|
43 | #include "SynTree/Type.h" |
---|
44 | #include "SynTree/TypeSubstitution.h" |
---|
45 | |
---|
46 | #include "ResolvExpr/TypeEnvironment.h" |
---|
47 | #include "ResolvExpr/TypeMap.h" |
---|
48 | #include "ResolvExpr/typeops.h" |
---|
49 | |
---|
50 | #include "SymTab/Indexer.h" |
---|
51 | #include "SymTab/Mangler.h" |
---|
52 | |
---|
53 | #include "Common/SemanticError.h" |
---|
54 | #include "Common/UniqueName.h" |
---|
55 | #include "Common/utility.h" |
---|
56 | |
---|
57 | #include <ext/functional> // temporary |
---|
58 | |
---|
59 | namespace GenPoly { |
---|
60 | namespace { |
---|
61 | const std::list<Label> noLabels; |
---|
62 | |
---|
63 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ); |
---|
64 | |
---|
65 | /// Abstracts type equality for a list of parameter types |
---|
66 | struct TypeList { |
---|
67 | TypeList() : params() {} |
---|
68 | TypeList( const std::list< Type* > &_params ) : params() { cloneAll(_params, params); } |
---|
69 | TypeList( std::list< Type* > &&_params ) : params( _params ) {} |
---|
70 | |
---|
71 | TypeList( const TypeList &that ) : params() { cloneAll(that.params, params); } |
---|
72 | TypeList( TypeList &&that ) : params( std::move( that.params ) ) {} |
---|
73 | |
---|
74 | /// Extracts types from a list of TypeExpr* |
---|
75 | TypeList( const std::list< TypeExpr* >& _params ) : params() { |
---|
76 | for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) { |
---|
77 | params.push_back( (*param)->get_type()->clone() ); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | TypeList& operator= ( const TypeList &that ) { |
---|
82 | deleteAll( params ); |
---|
83 | |
---|
84 | params.clear(); |
---|
85 | cloneAll( that.params, params ); |
---|
86 | |
---|
87 | return *this; |
---|
88 | } |
---|
89 | |
---|
90 | TypeList& operator= ( TypeList &&that ) { |
---|
91 | deleteAll( params ); |
---|
92 | |
---|
93 | params = std::move( that.params ); |
---|
94 | |
---|
95 | return *this; |
---|
96 | } |
---|
97 | |
---|
98 | ~TypeList() { deleteAll( params ); } |
---|
99 | |
---|
100 | bool operator== ( const TypeList& that ) const { |
---|
101 | if ( params.size() != that.params.size() ) return false; |
---|
102 | |
---|
103 | SymTab::Indexer dummy; |
---|
104 | for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) { |
---|
105 | if ( ! ResolvExpr::typesCompatible( *it, *jt, dummy ) ) return false; |
---|
106 | } |
---|
107 | return true; |
---|
108 | } |
---|
109 | |
---|
110 | std::list< Type* > params; ///< Instantiation parameters |
---|
111 | }; |
---|
112 | |
---|
113 | /// Maps a key and a TypeList to the some value, accounting for scope |
---|
114 | template< typename Key, typename Value > |
---|
115 | class InstantiationMap { |
---|
116 | /// Wraps value for a specific (Key, TypeList) combination |
---|
117 | typedef std::pair< TypeList, Value* > Instantiation; |
---|
118 | /// List of TypeLists paired with their appropriate values |
---|
119 | typedef std::vector< Instantiation > ValueList; |
---|
120 | /// Underlying map type; maps keys to a linear list of corresponding TypeLists and values |
---|
121 | typedef ScopedMap< Key*, ValueList > InnerMap; |
---|
122 | |
---|
123 | InnerMap instantiations; ///< instantiations |
---|
124 | |
---|
125 | public: |
---|
126 | /// Starts a new scope |
---|
127 | void beginScope() { instantiations.beginScope(); } |
---|
128 | |
---|
129 | /// Ends a scope |
---|
130 | void endScope() { instantiations.endScope(); } |
---|
131 | |
---|
132 | /// Gets the value for the (key, typeList) pair, returns NULL on none such. |
---|
133 | Value *lookup( Key *key, const std::list< TypeExpr* >& params ) const { |
---|
134 | TypeList typeList( params ); |
---|
135 | |
---|
136 | // scan scopes for matches to the key |
---|
137 | for ( typename InnerMap::const_iterator insts = instantiations.find( key ); insts != instantiations.end(); insts = instantiations.findNext( insts, key ) ) { |
---|
138 | for ( typename ValueList::const_reverse_iterator inst = insts->second.rbegin(); inst != insts->second.rend(); ++inst ) { |
---|
139 | if ( inst->first == typeList ) return inst->second; |
---|
140 | } |
---|
141 | } |
---|
142 | // no matching instantiations found |
---|
143 | return 0; |
---|
144 | } |
---|
145 | |
---|
146 | /// Adds a value for a (key, typeList) pair to the current scope |
---|
147 | void insert( Key *key, const std::list< TypeExpr* > ¶ms, Value *value ) { |
---|
148 | instantiations[ key ].push_back( Instantiation( TypeList( params ), value ) ); |
---|
149 | } |
---|
150 | }; |
---|
151 | |
---|
152 | /// Adds layout-generation functions to polymorphic types |
---|
153 | class LayoutFunctionBuilder : public DeclMutator { |
---|
154 | unsigned int functionNesting; // current level of nested functions |
---|
155 | public: |
---|
156 | LayoutFunctionBuilder() : functionNesting( 0 ) {} |
---|
157 | |
---|
158 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); |
---|
159 | virtual Declaration *mutate( StructDecl *structDecl ); |
---|
160 | virtual Declaration *mutate( UnionDecl *unionDecl ); |
---|
161 | }; |
---|
162 | |
---|
163 | /// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call |
---|
164 | class Pass1 : public PolyMutator { |
---|
165 | public: |
---|
166 | Pass1(); |
---|
167 | virtual Expression *mutate( ApplicationExpr *appExpr ); |
---|
168 | virtual Expression *mutate( AddressExpr *addrExpr ); |
---|
169 | virtual Expression *mutate( UntypedExpr *expr ); |
---|
170 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ); |
---|
171 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); |
---|
172 | virtual Expression *mutate( CommaExpr *commaExpr ); |
---|
173 | virtual Expression *mutate( ConditionalExpr *condExpr ); |
---|
174 | virtual Statement * mutate( ReturnStmt *returnStmt ); |
---|
175 | virtual Type *mutate( PointerType *pointerType ); |
---|
176 | virtual Type * mutate( FunctionType *functionType ); |
---|
177 | |
---|
178 | virtual void doBeginScope(); |
---|
179 | virtual void doEndScope(); |
---|
180 | private: |
---|
181 | /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application |
---|
182 | void passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ); |
---|
183 | /// passes extra type parameters into a polymorphic function application |
---|
184 | void passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); |
---|
185 | /// wraps a function application with a new temporary for the out-parameter return value |
---|
186 | Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ); |
---|
187 | /// Replaces all the type parameters of a generic type with their concrete equivalents under the current environment |
---|
188 | void replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ); |
---|
189 | /// Replaces a polymorphic type with its concrete equivalant under the current environment (returns itself if concrete). |
---|
190 | /// If `doClone` is set to false, will not clone interior types |
---|
191 | Type *replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone = true ); |
---|
192 | /// wraps a function application returning a polymorphic type with a new temporary for the out-parameter return value |
---|
193 | Expression *addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg ); |
---|
194 | Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); |
---|
195 | void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars ); |
---|
196 | void boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); |
---|
197 | void addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ); |
---|
198 | /// Stores assignment operators from assertion list in local map of assignment operations |
---|
199 | void findAssignOps( const std::list< TypeDecl *> &forall ); |
---|
200 | void passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars ); |
---|
201 | FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ); |
---|
202 | /// Replaces intrinsic operator functions with their arithmetic desugaring |
---|
203 | Expression *handleIntrinsics( ApplicationExpr *appExpr ); |
---|
204 | /// Inserts a new temporary variable into the current scope with an auto-generated name |
---|
205 | ObjectDecl *makeTemporary( Type *type ); |
---|
206 | |
---|
207 | ScopedMap< std::string, DeclarationWithType *> assignOps; ///< Currently known type variable assignment operators |
---|
208 | ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps; ///< Currently known assignment operators |
---|
209 | ScopedMap< std::string, DeclarationWithType* > adapters; ///< Set of adapter functions in the current scope |
---|
210 | |
---|
211 | DeclarationWithType *retval; |
---|
212 | bool useRetval; |
---|
213 | UniqueName tempNamer; |
---|
214 | }; |
---|
215 | |
---|
216 | /// * Moves polymorphic returns in function types to pointer-type parameters |
---|
217 | /// * adds type size and assertion parameters to parameter lists |
---|
218 | class Pass2 : public PolyMutator { |
---|
219 | public: |
---|
220 | template< typename DeclClass > |
---|
221 | DeclClass *handleDecl( DeclClass *decl, Type *type ); |
---|
222 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); |
---|
223 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ); |
---|
224 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); |
---|
225 | virtual TypedefDecl *mutate( TypedefDecl *typedefDecl ); |
---|
226 | virtual Type *mutate( PointerType *pointerType ); |
---|
227 | virtual Type *mutate( FunctionType *funcType ); |
---|
228 | |
---|
229 | private: |
---|
230 | void addAdapters( FunctionType *functionType ); |
---|
231 | |
---|
232 | std::map< UniqueId, std::string > adapterName; |
---|
233 | }; |
---|
234 | |
---|
235 | /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately |
---|
236 | class GenericInstantiator : public DeclMutator { |
---|
237 | /// Map of (generic type, parameter list) pairs to concrete type instantiations |
---|
238 | InstantiationMap< AggregateDecl, AggregateDecl > instantiations; |
---|
239 | /// Namer for concrete types |
---|
240 | UniqueName typeNamer; |
---|
241 | |
---|
242 | public: |
---|
243 | GenericInstantiator() : DeclMutator(), instantiations(), typeNamer("_conc_") {} |
---|
244 | |
---|
245 | virtual Type* mutate( StructInstType *inst ); |
---|
246 | virtual Type* mutate( UnionInstType *inst ); |
---|
247 | |
---|
248 | // virtual Expression* mutate( MemberExpr *memberExpr ); |
---|
249 | |
---|
250 | virtual void doBeginScope(); |
---|
251 | virtual void doEndScope(); |
---|
252 | private: |
---|
253 | /// Wrap instantiation lookup for structs |
---|
254 | StructDecl* lookup( StructInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (StructDecl*)instantiations.lookup( inst->get_baseStruct(), typeSubs ); } |
---|
255 | /// Wrap instantiation lookup for unions |
---|
256 | UnionDecl* lookup( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (UnionDecl*)instantiations.lookup( inst->get_baseUnion(), typeSubs ); } |
---|
257 | /// Wrap instantiation insertion for structs |
---|
258 | void insert( StructInstType *inst, const std::list< TypeExpr* > &typeSubs, StructDecl *decl ) { instantiations.insert( inst->get_baseStruct(), typeSubs, decl ); } |
---|
259 | /// Wrap instantiation insertion for unions |
---|
260 | void insert( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs, UnionDecl *decl ) { instantiations.insert( inst->get_baseUnion(), typeSubs, decl ); } |
---|
261 | }; |
---|
262 | |
---|
263 | /// Replaces member and size/align/offsetof expressions on polymorphic generic types with calculated expressions. |
---|
264 | /// * Replaces member expressions for polymorphic types with calculated add-field-offset-and-dereference |
---|
265 | /// * Calculates polymorphic offsetof expressions from offset array |
---|
266 | /// * Inserts dynamic calculation of polymorphic type layouts where needed |
---|
267 | class PolyGenericCalculator : public PolyMutator { |
---|
268 | public: |
---|
269 | template< typename DeclClass > |
---|
270 | DeclClass *handleDecl( DeclClass *decl, Type *type ); |
---|
271 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); |
---|
272 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ); |
---|
273 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl ); |
---|
274 | virtual TypeDecl *mutate( TypeDecl *objectDecl ); |
---|
275 | virtual Statement *mutate( DeclStmt *declStmt ); |
---|
276 | virtual Type *mutate( PointerType *pointerType ); |
---|
277 | virtual Type *mutate( FunctionType *funcType ); |
---|
278 | virtual Expression *mutate( MemberExpr *memberExpr ); |
---|
279 | virtual Expression *mutate( SizeofExpr *sizeofExpr ); |
---|
280 | virtual Expression *mutate( AlignofExpr *alignofExpr ); |
---|
281 | virtual Expression *mutate( OffsetofExpr *offsetofExpr ); |
---|
282 | virtual Expression *mutate( OffsetPackExpr *offsetPackExpr ); |
---|
283 | |
---|
284 | virtual void doBeginScope(); |
---|
285 | virtual void doEndScope(); |
---|
286 | |
---|
287 | private: |
---|
288 | /// Makes a new variable in the current scope with the given name, type & optional initializer |
---|
289 | ObjectDecl *makeVar( const std::string &name, Type *type, Initializer *init = 0 ); |
---|
290 | /// returns true if the type has a dynamic layout; such a layout will be stored in appropriately-named local variables when the function returns |
---|
291 | bool findGeneric( Type *ty ); |
---|
292 | /// adds type parameters to the layout call; will generate the appropriate parameters if needed |
---|
293 | void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ); |
---|
294 | |
---|
295 | /// Enters a new scope for type-variables, adding the type variables from ty |
---|
296 | void beginTypeScope( Type *ty ); |
---|
297 | /// Exits the type-variable scope |
---|
298 | void endTypeScope(); |
---|
299 | |
---|
300 | ScopedSet< std::string > knownLayouts; ///< Set of generic type layouts known in the current scope, indexed by sizeofName |
---|
301 | ScopedSet< std::string > knownOffsets; ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName |
---|
302 | }; |
---|
303 | |
---|
304 | /// Replaces initialization of polymorphic values with alloca, declaration of dtype/ftype with appropriate void expression, and sizeof expressions of polymorphic types with the proper variable |
---|
305 | class Pass3 : public PolyMutator { |
---|
306 | public: |
---|
307 | template< typename DeclClass > |
---|
308 | DeclClass *handleDecl( DeclClass *decl, Type *type ); |
---|
309 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); |
---|
310 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ); |
---|
311 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl ); |
---|
312 | virtual TypeDecl *mutate( TypeDecl *objectDecl ); |
---|
313 | virtual Type *mutate( PointerType *pointerType ); |
---|
314 | virtual Type *mutate( FunctionType *funcType ); |
---|
315 | private: |
---|
316 | }; |
---|
317 | |
---|
318 | } // anonymous namespace |
---|
319 | |
---|
320 | /// version of mutateAll with special handling for translation unit so you can check the end of the prelude when debugging |
---|
321 | template< typename MutatorType > |
---|
322 | inline void mutateTranslationUnit( std::list< Declaration* > &translationUnit, MutatorType &mutator ) { |
---|
323 | bool seenIntrinsic = false; |
---|
324 | SemanticError errors; |
---|
325 | for ( typename std::list< Declaration* >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
326 | try { |
---|
327 | if ( *i ) { |
---|
328 | if ( (*i)->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
329 | seenIntrinsic = true; |
---|
330 | } else if ( seenIntrinsic ) { |
---|
331 | seenIntrinsic = false; // break on this line when debugging for end of prelude |
---|
332 | } |
---|
333 | |
---|
334 | *i = dynamic_cast< Declaration* >( (*i)->acceptMutator( mutator ) ); |
---|
335 | assert( *i ); |
---|
336 | } // if |
---|
337 | } catch( SemanticError &e ) { |
---|
338 | errors.append( e ); |
---|
339 | } // try |
---|
340 | } // for |
---|
341 | if ( ! errors.isEmpty() ) { |
---|
342 | throw errors; |
---|
343 | } // if |
---|
344 | } |
---|
345 | |
---|
346 | void box( std::list< Declaration *>& translationUnit ) { |
---|
347 | LayoutFunctionBuilder layoutBuilder; |
---|
348 | Pass1 pass1; |
---|
349 | Pass2 pass2; |
---|
350 | GenericInstantiator instantiator; |
---|
351 | PolyGenericCalculator polyCalculator; |
---|
352 | Pass3 pass3; |
---|
353 | |
---|
354 | layoutBuilder.mutateDeclarationList( translationUnit ); |
---|
355 | mutateTranslationUnit/*All*/( translationUnit, pass1 ); |
---|
356 | mutateTranslationUnit/*All*/( translationUnit, pass2 ); |
---|
357 | instantiator.mutateDeclarationList( translationUnit ); |
---|
358 | mutateTranslationUnit/*All*/( translationUnit, polyCalculator ); |
---|
359 | mutateTranslationUnit/*All*/( translationUnit, pass3 ); |
---|
360 | } |
---|
361 | |
---|
362 | ////////////////////////////////// LayoutFunctionBuilder //////////////////////////////////////////// |
---|
363 | |
---|
364 | DeclarationWithType *LayoutFunctionBuilder::mutate( FunctionDecl *functionDecl ) { |
---|
365 | functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) ); |
---|
366 | mutateAll( functionDecl->get_oldDecls(), *this ); |
---|
367 | ++functionNesting; |
---|
368 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); |
---|
369 | --functionNesting; |
---|
370 | return functionDecl; |
---|
371 | } |
---|
372 | |
---|
373 | /// Get a list of type declarations that will affect a layout function |
---|
374 | std::list< TypeDecl* > takeOtypeOnly( std::list< TypeDecl* > &decls ) { |
---|
375 | std::list< TypeDecl * > otypeDecls; |
---|
376 | |
---|
377 | for ( std::list< TypeDecl* >::const_iterator decl = decls.begin(); decl != decls.end(); ++decl ) { |
---|
378 | if ( (*decl)->get_kind() == TypeDecl::Any ) { |
---|
379 | otypeDecls.push_back( *decl ); |
---|
380 | } |
---|
381 | } |
---|
382 | |
---|
383 | return otypeDecls; |
---|
384 | } |
---|
385 | |
---|
386 | /// Adds parameters for otype layout to a function type |
---|
387 | void addOtypeParams( FunctionType *layoutFnType, std::list< TypeDecl* > &otypeParams ) { |
---|
388 | BasicType sizeAlignType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
389 | |
---|
390 | for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) { |
---|
391 | TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param ); |
---|
392 | std::string paramName = mangleType( ¶mType ); |
---|
393 | layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); |
---|
394 | layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | /// Builds a layout function declaration |
---|
399 | FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) { |
---|
400 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
401 | // because each unit generates copies of the default routines for each aggregate. |
---|
402 | FunctionDecl *layoutDecl = new FunctionDecl( |
---|
403 | layoutofName( typeDecl ), functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false ); |
---|
404 | layoutDecl->fixUniqueId(); |
---|
405 | return layoutDecl; |
---|
406 | } |
---|
407 | |
---|
408 | /// Makes a unary operation |
---|
409 | Expression *makeOp( const std::string &name, Expression *arg ) { |
---|
410 | UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) ); |
---|
411 | expr->get_args().push_back( arg ); |
---|
412 | return expr; |
---|
413 | } |
---|
414 | |
---|
415 | /// Makes a binary operation |
---|
416 | Expression *makeOp( const std::string &name, Expression *lhs, Expression *rhs ) { |
---|
417 | UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) ); |
---|
418 | expr->get_args().push_back( lhs ); |
---|
419 | expr->get_args().push_back( rhs ); |
---|
420 | return expr; |
---|
421 | } |
---|
422 | |
---|
423 | /// Returns the dereference of a local pointer variable |
---|
424 | Expression *derefVar( ObjectDecl *var ) { |
---|
425 | return makeOp( "*?", new VariableExpr( var ) ); |
---|
426 | } |
---|
427 | |
---|
428 | /// makes an if-statement with a single-expression if-block and no then block |
---|
429 | Statement *makeCond( Expression *cond, Expression *ifPart ) { |
---|
430 | return new IfStmt( noLabels, cond, new ExprStmt( noLabels, ifPart ), 0 ); |
---|
431 | } |
---|
432 | |
---|
433 | /// makes a statement that assigns rhs to lhs if lhs < rhs |
---|
434 | Statement *makeAssignMax( Expression *lhs, Expression *rhs ) { |
---|
435 | return makeCond( makeOp( "?<?", lhs, rhs ), makeOp( "?=?", lhs->clone(), rhs->clone() ) ); |
---|
436 | } |
---|
437 | |
---|
438 | /// makes a statement that aligns lhs to rhs (rhs should be an integer power of two) |
---|
439 | Statement *makeAlignTo( Expression *lhs, Expression *rhs ) { |
---|
440 | // check that the lhs is zeroed out to the level of rhs |
---|
441 | Expression *ifCond = makeOp( "?&?", lhs, makeOp( "?-?", rhs, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), "1" ) ) ) ); |
---|
442 | // if not aligned, increment to alignment |
---|
443 | Expression *ifExpr = makeOp( "?+=?", lhs->clone(), makeOp( "?-?", rhs->clone(), ifCond->clone() ) ); |
---|
444 | return makeCond( ifCond, ifExpr ); |
---|
445 | } |
---|
446 | |
---|
447 | /// adds an expression to a compound statement |
---|
448 | void addExpr( CompoundStmt *stmts, Expression *expr ) { |
---|
449 | stmts->get_kids().push_back( new ExprStmt( noLabels, expr ) ); |
---|
450 | } |
---|
451 | |
---|
452 | /// adds a statement to a compound statement |
---|
453 | void addStmt( CompoundStmt *stmts, Statement *stmt ) { |
---|
454 | stmts->get_kids().push_back( stmt ); |
---|
455 | } |
---|
456 | |
---|
457 | Declaration *LayoutFunctionBuilder::mutate( StructDecl *structDecl ) { |
---|
458 | // do not generate layout function for "empty" tag structs |
---|
459 | if ( structDecl->get_members().empty() ) return structDecl; |
---|
460 | |
---|
461 | // get parameters that can change layout, exiting early if none |
---|
462 | std::list< TypeDecl* > otypeParams = takeOtypeOnly( structDecl->get_parameters() ); |
---|
463 | if ( otypeParams.empty() ) return structDecl; |
---|
464 | |
---|
465 | // build layout function signature |
---|
466 | FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false ); |
---|
467 | BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
468 | PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); |
---|
469 | |
---|
470 | ObjectDecl *sizeParam = new ObjectDecl( sizeofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); |
---|
471 | layoutFnType->get_parameters().push_back( sizeParam ); |
---|
472 | ObjectDecl *alignParam = new ObjectDecl( alignofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); |
---|
473 | layoutFnType->get_parameters().push_back( alignParam ); |
---|
474 | ObjectDecl *offsetParam = new ObjectDecl( offsetofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); |
---|
475 | layoutFnType->get_parameters().push_back( offsetParam ); |
---|
476 | addOtypeParams( layoutFnType, otypeParams ); |
---|
477 | |
---|
478 | // build function decl |
---|
479 | FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType ); |
---|
480 | |
---|
481 | // calculate struct layout in function body |
---|
482 | |
---|
483 | // initialize size and alignment to 0 and 1 (will have at least one member to re-edit size |
---|
484 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "0" ) ) ) ); |
---|
485 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) ); |
---|
486 | unsigned long n_members = 0; |
---|
487 | bool firstMember = true; |
---|
488 | for ( std::list< Declaration* >::const_iterator member = structDecl->get_members().begin(); member != structDecl->get_members().end(); ++member ) { |
---|
489 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ); |
---|
490 | assert( dwt ); |
---|
491 | Type *memberType = dwt->get_type(); |
---|
492 | |
---|
493 | if ( firstMember ) { |
---|
494 | firstMember = false; |
---|
495 | } else { |
---|
496 | // make sure all members after the first (automatically aligned at 0) are properly padded for alignment |
---|
497 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), new AlignofExpr( memberType->clone() ) ) ); |
---|
498 | } |
---|
499 | |
---|
500 | // place current size in the current offset index |
---|
501 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", makeOp( "?[?]", new VariableExpr( offsetParam ), new ConstantExpr( Constant::from( n_members ) ) ), |
---|
502 | derefVar( sizeParam ) ) ); |
---|
503 | ++n_members; |
---|
504 | |
---|
505 | // add member size to current size |
---|
506 | addExpr( layoutDecl->get_statements(), makeOp( "?+=?", derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) ); |
---|
507 | |
---|
508 | // take max of member alignment and global alignment |
---|
509 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) ); |
---|
510 | } |
---|
511 | // make sure the type is end-padded to a multiple of its alignment |
---|
512 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) ); |
---|
513 | |
---|
514 | addDeclarationAfter( layoutDecl ); |
---|
515 | return structDecl; |
---|
516 | } |
---|
517 | |
---|
518 | Declaration *LayoutFunctionBuilder::mutate( UnionDecl *unionDecl ) { |
---|
519 | // do not generate layout function for "empty" tag unions |
---|
520 | if ( unionDecl->get_members().empty() ) return unionDecl; |
---|
521 | |
---|
522 | // get parameters that can change layout, exiting early if none |
---|
523 | std::list< TypeDecl* > otypeParams = takeOtypeOnly( unionDecl->get_parameters() ); |
---|
524 | if ( otypeParams.empty() ) return unionDecl; |
---|
525 | |
---|
526 | // build layout function signature |
---|
527 | FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false ); |
---|
528 | BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
529 | PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); |
---|
530 | |
---|
531 | ObjectDecl *sizeParam = new ObjectDecl( sizeofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); |
---|
532 | layoutFnType->get_parameters().push_back( sizeParam ); |
---|
533 | ObjectDecl *alignParam = new ObjectDecl( alignofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); |
---|
534 | layoutFnType->get_parameters().push_back( alignParam ); |
---|
535 | addOtypeParams( layoutFnType, otypeParams ); |
---|
536 | |
---|
537 | // build function decl |
---|
538 | FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType ); |
---|
539 | |
---|
540 | // calculate union layout in function body |
---|
541 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) ); |
---|
542 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) ); |
---|
543 | for ( std::list< Declaration* >::const_iterator member = unionDecl->get_members().begin(); member != unionDecl->get_members().end(); ++member ) { |
---|
544 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ); |
---|
545 | assert( dwt ); |
---|
546 | Type *memberType = dwt->get_type(); |
---|
547 | |
---|
548 | // take max member size and global size |
---|
549 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) ); |
---|
550 | |
---|
551 | // take max of member alignment and global alignment |
---|
552 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) ); |
---|
553 | } |
---|
554 | // make sure the type is end-padded to a multiple of its alignment |
---|
555 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) ); |
---|
556 | |
---|
557 | addDeclarationAfter( layoutDecl ); |
---|
558 | return unionDecl; |
---|
559 | } |
---|
560 | |
---|
561 | ////////////////////////////////////////// Pass1 //////////////////////////////////////////////////// |
---|
562 | |
---|
563 | namespace { |
---|
564 | std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) { |
---|
565 | std::stringstream name; |
---|
566 | |
---|
567 | // NOTE: this function previously used isPolyObj, which failed to produce |
---|
568 | // the correct thing in some situations. It's not clear to me why this wasn't working. |
---|
569 | |
---|
570 | // if the return type or a parameter type involved polymorphic types, then the adapter will need |
---|
571 | // to take those polymorphic types as pointers. Therefore, there can be two different functions |
---|
572 | // with the same mangled name, so we need to further mangle the names. |
---|
573 | for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) { |
---|
574 | if ( isPolyType( (*retval)->get_type(), tyVars ) ) { |
---|
575 | name << "P"; |
---|
576 | } else { |
---|
577 | name << "M"; |
---|
578 | } |
---|
579 | } |
---|
580 | name << "_"; |
---|
581 | std::list< DeclarationWithType *> ¶mList = function->get_parameters(); |
---|
582 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
583 | if ( isPolyType( (*arg)->get_type(), tyVars ) ) { |
---|
584 | name << "P"; |
---|
585 | } else { |
---|
586 | name << "M"; |
---|
587 | } |
---|
588 | } // for |
---|
589 | return name.str(); |
---|
590 | } |
---|
591 | |
---|
592 | std::string mangleAdapterName( FunctionType * function, const TyVarMap &tyVars ) { |
---|
593 | return SymTab::Mangler::mangle( function ) + makePolyMonoSuffix( function, tyVars ); |
---|
594 | } |
---|
595 | |
---|
596 | std::string makeAdapterName( const std::string &mangleName ) { |
---|
597 | return "_adapter" + mangleName; |
---|
598 | } |
---|
599 | |
---|
600 | Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {} |
---|
601 | |
---|
602 | /// Returns T if the given declaration is (*?=?)(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise |
---|
603 | TypeInstType *isTypeInstAssignment( DeclarationWithType *decl ) { |
---|
604 | if ( decl->get_name() == "?=?" ) { |
---|
605 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { |
---|
606 | if ( funType->get_parameters().size() == 2 ) { |
---|
607 | if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) { |
---|
608 | if ( TypeInstType *refType = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) { |
---|
609 | if ( TypeInstType *refType2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) { |
---|
610 | if ( refType->get_name() == refType2->get_name() ) { |
---|
611 | return refType; |
---|
612 | } // if |
---|
613 | } // if |
---|
614 | } // if |
---|
615 | } // if |
---|
616 | } // if |
---|
617 | } // if |
---|
618 | } // if |
---|
619 | return 0; |
---|
620 | } |
---|
621 | |
---|
622 | /// returns T if the given declaration is: (*?=?)(T *, T) for some type T (return not checked, but maybe should be), NULL otherwise |
---|
623 | /// Only picks assignments where neither parameter is cv-qualified |
---|
624 | Type *isAssignment( DeclarationWithType *decl ) { |
---|
625 | if ( decl->get_name() == "?=?" ) { |
---|
626 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { |
---|
627 | if ( funType->get_parameters().size() == 2 ) { |
---|
628 | Type::Qualifiers defaultQualifiers; |
---|
629 | Type *paramType1 = funType->get_parameters().front()->get_type(); |
---|
630 | if ( paramType1->get_qualifiers() != defaultQualifiers ) return 0; |
---|
631 | Type *paramType2 = funType->get_parameters().back()->get_type(); |
---|
632 | if ( paramType2->get_qualifiers() != defaultQualifiers ) return 0; |
---|
633 | |
---|
634 | if ( PointerType *pointerType = dynamic_cast< PointerType* >( paramType1 ) ) { |
---|
635 | Type *baseType1 = pointerType->get_base(); |
---|
636 | if ( baseType1->get_qualifiers() != defaultQualifiers ) return 0; |
---|
637 | SymTab::Indexer dummy; |
---|
638 | if ( ResolvExpr::typesCompatible( baseType1, paramType2, dummy ) ) { |
---|
639 | return baseType1; |
---|
640 | } // if |
---|
641 | } // if |
---|
642 | } // if |
---|
643 | } // if |
---|
644 | } // if |
---|
645 | return 0; |
---|
646 | } |
---|
647 | |
---|
648 | void Pass1::findAssignOps( const std::list< TypeDecl *> &forall ) { |
---|
649 | // what if a nested function uses an assignment operator? |
---|
650 | // assignOps.clear(); |
---|
651 | for ( std::list< TypeDecl *>::const_iterator i = forall.begin(); i != forall.end(); ++i ) { |
---|
652 | for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) { |
---|
653 | std::string typeName; |
---|
654 | if ( TypeInstType *typeInst = isTypeInstAssignment( *assert ) ) { |
---|
655 | assignOps[ typeInst->get_name() ] = *assert; |
---|
656 | } // if |
---|
657 | } // for |
---|
658 | } // for |
---|
659 | } |
---|
660 | |
---|
661 | DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) { |
---|
662 | // if this is a assignment function, put it in the map for this scope |
---|
663 | if ( Type *assignedType = isAssignment( functionDecl ) ) { |
---|
664 | if ( ! dynamic_cast< TypeInstType* >( assignedType ) ) { |
---|
665 | scopedAssignOps.insert( assignedType, functionDecl ); |
---|
666 | } |
---|
667 | } |
---|
668 | |
---|
669 | if ( functionDecl->get_statements() ) { // empty routine body ? |
---|
670 | doBeginScope(); |
---|
671 | scopeTyVars.beginScope(); |
---|
672 | assignOps.beginScope(); |
---|
673 | DeclarationWithType *oldRetval = retval; |
---|
674 | bool oldUseRetval = useRetval; |
---|
675 | |
---|
676 | // process polymorphic return value |
---|
677 | retval = 0; |
---|
678 | if ( isPolyRet( functionDecl->get_functionType() ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) { |
---|
679 | retval = functionDecl->get_functionType()->get_returnVals().front(); |
---|
680 | |
---|
681 | // give names to unnamed return values |
---|
682 | if ( retval->get_name() == "" ) { |
---|
683 | retval->set_name( "_retparm" ); |
---|
684 | retval->set_linkage( LinkageSpec::C ); |
---|
685 | } // if |
---|
686 | } // if |
---|
687 | |
---|
688 | FunctionType *functionType = functionDecl->get_functionType(); |
---|
689 | makeTyVarMap( functionDecl->get_functionType(), scopeTyVars ); |
---|
690 | findAssignOps( functionDecl->get_functionType()->get_forall() ); |
---|
691 | |
---|
692 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); |
---|
693 | std::list< FunctionType *> functions; |
---|
694 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { |
---|
695 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { |
---|
696 | findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter ); |
---|
697 | } // for |
---|
698 | } // for |
---|
699 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
700 | findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter ); |
---|
701 | } // for |
---|
702 | |
---|
703 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { |
---|
704 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars ); |
---|
705 | if ( adapters.find( mangleName ) == adapters.end() ) { |
---|
706 | std::string adapterName = makeAdapterName( mangleName ); |
---|
707 | adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) ); |
---|
708 | } // if |
---|
709 | } // for |
---|
710 | |
---|
711 | functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) ); |
---|
712 | |
---|
713 | scopeTyVars.endScope(); |
---|
714 | assignOps.endScope(); |
---|
715 | retval = oldRetval; |
---|
716 | useRetval = oldUseRetval; |
---|
717 | doEndScope(); |
---|
718 | } // if |
---|
719 | return functionDecl; |
---|
720 | } |
---|
721 | |
---|
722 | TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) { |
---|
723 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
724 | return Mutator::mutate( typeDecl ); |
---|
725 | } |
---|
726 | |
---|
727 | Expression *Pass1::mutate( CommaExpr *commaExpr ) { |
---|
728 | bool oldUseRetval = useRetval; |
---|
729 | useRetval = false; |
---|
730 | commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) ); |
---|
731 | useRetval = oldUseRetval; |
---|
732 | commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) ); |
---|
733 | return commaExpr; |
---|
734 | } |
---|
735 | |
---|
736 | Expression *Pass1::mutate( ConditionalExpr *condExpr ) { |
---|
737 | bool oldUseRetval = useRetval; |
---|
738 | useRetval = false; |
---|
739 | condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) ); |
---|
740 | useRetval = oldUseRetval; |
---|
741 | condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) ); |
---|
742 | condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) ); |
---|
743 | return condExpr; |
---|
744 | |
---|
745 | } |
---|
746 | |
---|
747 | void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) { |
---|
748 | Type *polyType = isPolyType( parmType, exprTyVars ); |
---|
749 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { |
---|
750 | std::string typeName = mangleType( polyType ); |
---|
751 | if ( seenTypes.count( typeName ) ) return; |
---|
752 | |
---|
753 | arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) ); |
---|
754 | arg++; |
---|
755 | arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) ); |
---|
756 | arg++; |
---|
757 | if ( dynamic_cast< StructInstType* >( polyType ) ) { |
---|
758 | if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) { |
---|
759 | // zero-length arrays are forbidden by C, so don't pass offset for empty struct |
---|
760 | if ( ! argBaseStructType->get_baseStruct()->get_members().empty() ) { |
---|
761 | arg = appExpr->get_args().insert( arg, new OffsetPackExpr( argBaseStructType->clone() ) ); |
---|
762 | arg++; |
---|
763 | } |
---|
764 | } else { |
---|
765 | throw SemanticError( "Cannot pass non-struct type for generic struct" ); |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | seenTypes.insert( typeName ); |
---|
770 | } |
---|
771 | } |
---|
772 | |
---|
773 | void Pass1::passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { |
---|
774 | // pass size/align for type variables |
---|
775 | for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) { |
---|
776 | ResolvExpr::EqvClass eqvClass; |
---|
777 | assert( env ); |
---|
778 | if ( tyParm->second == TypeDecl::Any ) { |
---|
779 | Type *concrete = env->lookup( tyParm->first ); |
---|
780 | if ( concrete ) { |
---|
781 | arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) ); |
---|
782 | arg++; |
---|
783 | arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) ); |
---|
784 | arg++; |
---|
785 | } else { |
---|
786 | throw SemanticError( "unbound type variable in application ", appExpr ); |
---|
787 | } // if |
---|
788 | } // if |
---|
789 | } // for |
---|
790 | |
---|
791 | // add size/align for generic types to parameter list |
---|
792 | if ( appExpr->get_function()->get_results().empty() ) return; |
---|
793 | FunctionType *funcType = getFunctionType( appExpr->get_function()->get_results().front() ); |
---|
794 | assert( funcType ); |
---|
795 | |
---|
796 | std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); |
---|
797 | std::list< Expression* >::const_iterator fnArg = arg; |
---|
798 | std::set< std::string > seenTypes; //< names for generic types we've seen |
---|
799 | |
---|
800 | // a polymorphic return type may need to be added to the argument list |
---|
801 | if ( polyRetType ) { |
---|
802 | Type *concRetType = replaceWithConcrete( appExpr, polyRetType ); |
---|
803 | passArgTypeVars( appExpr, polyRetType, concRetType, arg, exprTyVars, seenTypes ); |
---|
804 | } |
---|
805 | |
---|
806 | // add type information args for presently unseen types in parameter list |
---|
807 | for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) { |
---|
808 | VariableExpr *fnArgBase = getBaseVar( *fnArg ); |
---|
809 | if ( ! fnArgBase || fnArgBase->get_results().empty() ) continue; |
---|
810 | passArgTypeVars( appExpr, (*fnParm)->get_type(), fnArgBase->get_results().front(), arg, exprTyVars, seenTypes ); |
---|
811 | } |
---|
812 | } |
---|
813 | |
---|
814 | ObjectDecl *Pass1::makeTemporary( Type *type ) { |
---|
815 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 ); |
---|
816 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
817 | return newObj; |
---|
818 | } |
---|
819 | |
---|
820 | Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) { |
---|
821 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. |
---|
822 | // if ( useRetval ) { |
---|
823 | // assert( retval ); |
---|
824 | // arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) ); |
---|
825 | // arg++; |
---|
826 | // } else { |
---|
827 | |
---|
828 | // Create temporary to hold return value of polymorphic function and produce that temporary as a result |
---|
829 | // using a comma expression. Possibly change comma expression into statement expression "{}" for multiple |
---|
830 | // return values. |
---|
831 | ObjectDecl *newObj = makeTemporary( retType->clone() ); |
---|
832 | Expression *paramExpr = new VariableExpr( newObj ); |
---|
833 | |
---|
834 | // If the type of the temporary is not polymorphic, box temporary by taking its address; |
---|
835 | // otherwise the temporary is already boxed and can be used directly. |
---|
836 | if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) { |
---|
837 | paramExpr = new AddressExpr( paramExpr ); |
---|
838 | } // if |
---|
839 | arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call |
---|
840 | arg++; |
---|
841 | // Build a comma expression to call the function and emulate a normal return. |
---|
842 | CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) ); |
---|
843 | commaExpr->set_env( appExpr->get_env() ); |
---|
844 | appExpr->set_env( 0 ); |
---|
845 | return commaExpr; |
---|
846 | // } // if |
---|
847 | // return appExpr; |
---|
848 | } |
---|
849 | |
---|
850 | void Pass1::replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ) { |
---|
851 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
852 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
853 | assert(paramType && "Aggregate parameters should be type expressions"); |
---|
854 | paramType->set_type( replaceWithConcrete( appExpr, paramType->get_type(), false ) ); |
---|
855 | } |
---|
856 | } |
---|
857 | |
---|
858 | Type *Pass1::replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone ) { |
---|
859 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { |
---|
860 | Type *concrete = env->lookup( typeInst->get_name() ); |
---|
861 | if ( concrete == 0 ) { |
---|
862 | throw SemanticError( "Unbound type variable " + typeInst->get_name() + " in ", appExpr ); |
---|
863 | } // if |
---|
864 | return concrete; |
---|
865 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
866 | if ( doClone ) { |
---|
867 | structType = structType->clone(); |
---|
868 | } |
---|
869 | replaceParametersWithConcrete( appExpr, structType->get_parameters() ); |
---|
870 | return structType; |
---|
871 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
872 | if ( doClone ) { |
---|
873 | unionType = unionType->clone(); |
---|
874 | } |
---|
875 | replaceParametersWithConcrete( appExpr, unionType->get_parameters() ); |
---|
876 | return unionType; |
---|
877 | } |
---|
878 | return type; |
---|
879 | } |
---|
880 | |
---|
881 | Expression *Pass1::addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg ) { |
---|
882 | assert( env ); |
---|
883 | Type *concrete = replaceWithConcrete( appExpr, polyType ); |
---|
884 | // add out-parameter for return value |
---|
885 | return addRetParam( appExpr, function, concrete, arg ); |
---|
886 | } |
---|
887 | |
---|
888 | Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { |
---|
889 | Expression *ret = appExpr; |
---|
890 | if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
891 | ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg ); |
---|
892 | } // if |
---|
893 | std::string mangleName = mangleAdapterName( function, tyVars ); |
---|
894 | std::string adapterName = makeAdapterName( mangleName ); |
---|
895 | |
---|
896 | // cast adaptee to void (*)(), since it may have any type inside a polymorphic function |
---|
897 | Type * adapteeType = new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ); |
---|
898 | appExpr->get_args().push_front( new CastExpr( appExpr->get_function(), adapteeType ) ); |
---|
899 | appExpr->set_function( new NameExpr( adapterName ) ); |
---|
900 | |
---|
901 | return ret; |
---|
902 | } |
---|
903 | |
---|
904 | void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) { |
---|
905 | assert( ! arg->get_results().empty() ); |
---|
906 | if ( isPolyType( param, exprTyVars ) ) { |
---|
907 | if ( isPolyType( arg->get_results().front() ) ) { |
---|
908 | // if the argument's type is polymorphic, we don't need to box again! |
---|
909 | return; |
---|
910 | } else if ( arg->get_results().front()->get_isLvalue() ) { |
---|
911 | // VariableExpr and MemberExpr are lvalues; need to check this isn't coming from the second arg of a comma expression though (not an lvalue) |
---|
912 | if ( CommaExpr *commaArg = dynamic_cast< CommaExpr* >( arg ) ) { |
---|
913 | commaArg->set_arg2( new AddressExpr( commaArg->get_arg2() ) ); |
---|
914 | } else { |
---|
915 | arg = new AddressExpr( arg ); |
---|
916 | } |
---|
917 | } else { |
---|
918 | // use type computed in unification to declare boxed variables |
---|
919 | Type * newType = param->clone(); |
---|
920 | if ( env ) env->apply( newType ); |
---|
921 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, newType, 0 ); |
---|
922 | newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right??? |
---|
923 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
924 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
925 | assign->get_args().push_back( new VariableExpr( newObj ) ); |
---|
926 | assign->get_args().push_back( arg ); |
---|
927 | stmtsToAdd.push_back( new ExprStmt( noLabels, assign ) ); |
---|
928 | arg = new AddressExpr( new VariableExpr( newObj ) ); |
---|
929 | } // if |
---|
930 | } // if |
---|
931 | } |
---|
932 | |
---|
933 | /// cast parameters to polymorphic functions so that types are replaced with |
---|
934 | /// void * if they are type parameters in the formal type. |
---|
935 | /// this gets rid of warnings from gcc. |
---|
936 | void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) { |
---|
937 | Type * newType = formal->clone(); |
---|
938 | if ( getFunctionType( newType ) ) { |
---|
939 | newType = ScrubTyVars::scrub( newType, tyVars ); |
---|
940 | actual = new CastExpr( actual, newType ); |
---|
941 | } // if |
---|
942 | } |
---|
943 | |
---|
944 | void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { |
---|
945 | for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) { |
---|
946 | assert( arg != appExpr->get_args().end() ); |
---|
947 | addCast( *arg, (*param)->get_type(), exprTyVars ); |
---|
948 | boxParam( (*param)->get_type(), *arg, exprTyVars ); |
---|
949 | } // for |
---|
950 | } |
---|
951 | |
---|
952 | void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { |
---|
953 | std::list< Expression *>::iterator cur = arg; |
---|
954 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { |
---|
955 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { |
---|
956 | InferredParams::const_iterator inferParam = appExpr->get_inferParams().find( (*assert)->get_uniqueId() ); |
---|
957 | assert( inferParam != appExpr->get_inferParams().end() && "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" ); |
---|
958 | Expression *newExpr = inferParam->second.expr->clone(); |
---|
959 | addCast( newExpr, (*assert)->get_type(), tyVars ); |
---|
960 | boxParam( (*assert)->get_type(), newExpr, tyVars ); |
---|
961 | appExpr->get_args().insert( cur, newExpr ); |
---|
962 | } // for |
---|
963 | } // for |
---|
964 | } |
---|
965 | |
---|
966 | void makeRetParm( FunctionType *funcType ) { |
---|
967 | DeclarationWithType *retParm = funcType->get_returnVals().front(); |
---|
968 | |
---|
969 | // make a new parameter that is a pointer to the type of the old return value |
---|
970 | retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) ); |
---|
971 | funcType->get_parameters().push_front( retParm ); |
---|
972 | |
---|
973 | // we don't need the return value any more |
---|
974 | funcType->get_returnVals().clear(); |
---|
975 | } |
---|
976 | |
---|
977 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) { |
---|
978 | // actually make the adapter type |
---|
979 | FunctionType *adapter = adaptee->clone(); |
---|
980 | if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
981 | makeRetParm( adapter ); |
---|
982 | } // if |
---|
983 | adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) ); |
---|
984 | return adapter; |
---|
985 | } |
---|
986 | |
---|
987 | Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) { |
---|
988 | assert( param ); |
---|
989 | assert( arg ); |
---|
990 | if ( isPolyType( realParam->get_type(), tyVars ) ) { |
---|
991 | if ( ! isPolyType( arg->get_type() ) ) { |
---|
992 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
993 | deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) ); |
---|
994 | deref->get_results().push_back( arg->get_type()->clone() ); |
---|
995 | return deref; |
---|
996 | } // if |
---|
997 | } // if |
---|
998 | return new VariableExpr( param ); |
---|
999 | } |
---|
1000 | |
---|
1001 | void addAdapterParams( ApplicationExpr *adapteeApp, std::list< DeclarationWithType *>::iterator arg, std::list< DeclarationWithType *>::iterator param, std::list< DeclarationWithType *>::iterator paramEnd, std::list< DeclarationWithType *>::iterator realParam, const TyVarMap &tyVars ) { |
---|
1002 | UniqueName paramNamer( "_p" ); |
---|
1003 | for ( ; param != paramEnd; ++param, ++arg, ++realParam ) { |
---|
1004 | if ( (*param)->get_name() == "" ) { |
---|
1005 | (*param)->set_name( paramNamer.newName() ); |
---|
1006 | (*param)->set_linkage( LinkageSpec::C ); |
---|
1007 | } // if |
---|
1008 | adapteeApp->get_args().push_back( makeAdapterArg( *param, *arg, *realParam, tyVars ) ); |
---|
1009 | } // for |
---|
1010 | } |
---|
1011 | |
---|
1012 | FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) { |
---|
1013 | FunctionType *adapterType = makeAdapterType( adaptee, tyVars ); |
---|
1014 | adapterType = ScrubTyVars::scrub( adapterType, tyVars ); |
---|
1015 | DeclarationWithType *adapteeDecl = adapterType->get_parameters().front(); |
---|
1016 | adapteeDecl->set_name( "_adaptee" ); |
---|
1017 | ApplicationExpr *adapteeApp = new ApplicationExpr( new CastExpr( new VariableExpr( adapteeDecl ), new PointerType( Type::Qualifiers(), realType ) ) ); |
---|
1018 | Statement *bodyStmt; |
---|
1019 | |
---|
1020 | std::list< TypeDecl *>::iterator tyArg = realType->get_forall().begin(); |
---|
1021 | std::list< TypeDecl *>::iterator tyParam = adapterType->get_forall().begin(); |
---|
1022 | std::list< TypeDecl *>::iterator realTyParam = adaptee->get_forall().begin(); |
---|
1023 | for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) { |
---|
1024 | assert( tyArg != realType->get_forall().end() ); |
---|
1025 | std::list< DeclarationWithType *>::iterator assertArg = (*tyArg)->get_assertions().begin(); |
---|
1026 | std::list< DeclarationWithType *>::iterator assertParam = (*tyParam)->get_assertions().begin(); |
---|
1027 | std::list< DeclarationWithType *>::iterator realAssertParam = (*realTyParam)->get_assertions().begin(); |
---|
1028 | for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) { |
---|
1029 | assert( assertArg != (*tyArg)->get_assertions().end() ); |
---|
1030 | adapteeApp->get_args().push_back( makeAdapterArg( *assertParam, *assertArg, *realAssertParam, tyVars ) ); |
---|
1031 | } // for |
---|
1032 | } // for |
---|
1033 | |
---|
1034 | std::list< DeclarationWithType *>::iterator arg = realType->get_parameters().begin(); |
---|
1035 | std::list< DeclarationWithType *>::iterator param = adapterType->get_parameters().begin(); |
---|
1036 | std::list< DeclarationWithType *>::iterator realParam = adaptee->get_parameters().begin(); |
---|
1037 | param++; // skip adaptee parameter |
---|
1038 | if ( realType->get_returnVals().empty() ) { |
---|
1039 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); |
---|
1040 | bodyStmt = new ExprStmt( noLabels, adapteeApp ); |
---|
1041 | } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
1042 | if ( (*param)->get_name() == "" ) { |
---|
1043 | (*param)->set_name( "_ret" ); |
---|
1044 | (*param)->set_linkage( LinkageSpec::C ); |
---|
1045 | } // if |
---|
1046 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
1047 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
1048 | deref->get_args().push_back( new CastExpr( new VariableExpr( *param++ ), new PointerType( Type::Qualifiers(), realType->get_returnVals().front()->get_type()->clone() ) ) ); |
---|
1049 | assign->get_args().push_back( deref ); |
---|
1050 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); |
---|
1051 | assign->get_args().push_back( adapteeApp ); |
---|
1052 | bodyStmt = new ExprStmt( noLabels, assign ); |
---|
1053 | } else { |
---|
1054 | // adapter for a function that returns a monomorphic value |
---|
1055 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); |
---|
1056 | bodyStmt = new ReturnStmt( noLabels, adapteeApp ); |
---|
1057 | } // if |
---|
1058 | CompoundStmt *adapterBody = new CompoundStmt( noLabels ); |
---|
1059 | adapterBody->get_kids().push_back( bodyStmt ); |
---|
1060 | std::string adapterName = makeAdapterName( mangleName ); |
---|
1061 | return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false ); |
---|
1062 | } |
---|
1063 | |
---|
1064 | void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) { |
---|
1065 | // collect a list of function types passed as parameters or implicit parameters (assertions) |
---|
1066 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); |
---|
1067 | std::list< FunctionType *> functions; |
---|
1068 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { |
---|
1069 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { |
---|
1070 | findFunction( (*assert)->get_type(), functions, exprTyVars, needsAdapter ); |
---|
1071 | } // for |
---|
1072 | } // for |
---|
1073 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
1074 | findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter ); |
---|
1075 | } // for |
---|
1076 | |
---|
1077 | // parameter function types for which an appropriate adapter has been generated. we cannot use the types |
---|
1078 | // after applying substitutions, since two different parameter types may be unified to the same type |
---|
1079 | std::set< std::string > adaptersDone; |
---|
1080 | |
---|
1081 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { |
---|
1082 | FunctionType *originalFunction = (*funType)->clone(); |
---|
1083 | FunctionType *realFunction = (*funType)->clone(); |
---|
1084 | std::string mangleName = SymTab::Mangler::mangle( realFunction ); |
---|
1085 | |
---|
1086 | // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this |
---|
1087 | // pre-substitution parameter function type. |
---|
1088 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { |
---|
1089 | adaptersDone.insert( adaptersDone.begin(), mangleName ); |
---|
1090 | |
---|
1091 | // apply substitution to type variables to figure out what the adapter's type should look like |
---|
1092 | assert( env ); |
---|
1093 | env->apply( realFunction ); |
---|
1094 | mangleName = SymTab::Mangler::mangle( realFunction ); |
---|
1095 | mangleName += makePolyMonoSuffix( originalFunction, exprTyVars ); |
---|
1096 | |
---|
1097 | typedef ScopedMap< std::string, DeclarationWithType* >::iterator AdapterIter; |
---|
1098 | AdapterIter adapter = adapters.find( mangleName ); |
---|
1099 | if ( adapter == adapters.end() ) { |
---|
1100 | // adapter has not been created yet in the current scope, so define it |
---|
1101 | FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars ); |
---|
1102 | std::pair< AdapterIter, bool > answer = adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) ); |
---|
1103 | adapter = answer.first; |
---|
1104 | stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) ); |
---|
1105 | } // if |
---|
1106 | assert( adapter != adapters.end() ); |
---|
1107 | |
---|
1108 | // add the appropriate adapter as a parameter |
---|
1109 | appExpr->get_args().push_front( new VariableExpr( adapter->second ) ); |
---|
1110 | } // if |
---|
1111 | } // for |
---|
1112 | } // passAdapters |
---|
1113 | |
---|
1114 | Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) { |
---|
1115 | NameExpr *opExpr; |
---|
1116 | if ( isIncr ) { |
---|
1117 | opExpr = new NameExpr( "?+=?" ); |
---|
1118 | } else { |
---|
1119 | opExpr = new NameExpr( "?-=?" ); |
---|
1120 | } // if |
---|
1121 | UntypedExpr *addAssign = new UntypedExpr( opExpr ); |
---|
1122 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { |
---|
1123 | addAssign->get_args().push_back( address->get_arg() ); |
---|
1124 | } else { |
---|
1125 | addAssign->get_args().push_back( appExpr->get_args().front() ); |
---|
1126 | } // if |
---|
1127 | addAssign->get_args().push_back( new NameExpr( sizeofName( mangleType( polyType ) ) ) ); |
---|
1128 | addAssign->get_results().front() = appExpr->get_results().front()->clone(); |
---|
1129 | if ( appExpr->get_env() ) { |
---|
1130 | addAssign->set_env( appExpr->get_env() ); |
---|
1131 | appExpr->set_env( 0 ); |
---|
1132 | } // if |
---|
1133 | appExpr->get_args().clear(); |
---|
1134 | delete appExpr; |
---|
1135 | return addAssign; |
---|
1136 | } |
---|
1137 | |
---|
1138 | Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) { |
---|
1139 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->get_function() ) ) { |
---|
1140 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
1141 | if ( varExpr->get_var()->get_name() == "?[?]" ) { |
---|
1142 | assert( ! appExpr->get_results().empty() ); |
---|
1143 | assert( appExpr->get_args().size() == 2 ); |
---|
1144 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env ); |
---|
1145 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env ); |
---|
1146 | assert( ! baseType1 || ! baseType2 ); // the arguments cannot both be polymorphic pointers |
---|
1147 | UntypedExpr *ret = 0; |
---|
1148 | if ( baseType1 || baseType2 ) { // one of the arguments is a polymorphic pointer |
---|
1149 | ret = new UntypedExpr( new NameExpr( "?+?" ) ); |
---|
1150 | } // if |
---|
1151 | if ( baseType1 ) { |
---|
1152 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1153 | multiply->get_args().push_back( appExpr->get_args().back() ); |
---|
1154 | multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); |
---|
1155 | ret->get_args().push_back( appExpr->get_args().front() ); |
---|
1156 | ret->get_args().push_back( multiply ); |
---|
1157 | } else if ( baseType2 ) { |
---|
1158 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1159 | multiply->get_args().push_back( appExpr->get_args().front() ); |
---|
1160 | multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) ); |
---|
1161 | ret->get_args().push_back( multiply ); |
---|
1162 | ret->get_args().push_back( appExpr->get_args().back() ); |
---|
1163 | } // if |
---|
1164 | if ( baseType1 || baseType2 ) { |
---|
1165 | ret->get_results().push_front( appExpr->get_results().front()->clone() ); |
---|
1166 | if ( appExpr->get_env() ) { |
---|
1167 | ret->set_env( appExpr->get_env() ); |
---|
1168 | appExpr->set_env( 0 ); |
---|
1169 | } // if |
---|
1170 | appExpr->get_args().clear(); |
---|
1171 | delete appExpr; |
---|
1172 | return ret; |
---|
1173 | } // if |
---|
1174 | } else if ( varExpr->get_var()->get_name() == "*?" ) { |
---|
1175 | assert( ! appExpr->get_results().empty() ); |
---|
1176 | assert( ! appExpr->get_args().empty() ); |
---|
1177 | if ( isPolyType( appExpr->get_results().front(), scopeTyVars, env ) ) { |
---|
1178 | Expression *ret = appExpr->get_args().front(); |
---|
1179 | delete ret->get_results().front(); |
---|
1180 | ret->get_results().front() = appExpr->get_results().front()->clone(); |
---|
1181 | if ( appExpr->get_env() ) { |
---|
1182 | ret->set_env( appExpr->get_env() ); |
---|
1183 | appExpr->set_env( 0 ); |
---|
1184 | } // if |
---|
1185 | appExpr->get_args().clear(); |
---|
1186 | delete appExpr; |
---|
1187 | return ret; |
---|
1188 | } // if |
---|
1189 | } else if ( varExpr->get_var()->get_name() == "?++" || varExpr->get_var()->get_name() == "?--" ) { |
---|
1190 | assert( ! appExpr->get_results().empty() ); |
---|
1191 | assert( appExpr->get_args().size() == 1 ); |
---|
1192 | if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) { |
---|
1193 | Type *tempType = appExpr->get_results().front()->clone(); |
---|
1194 | if ( env ) { |
---|
1195 | env->apply( tempType ); |
---|
1196 | } // if |
---|
1197 | ObjectDecl *newObj = makeTemporary( tempType ); |
---|
1198 | VariableExpr *tempExpr = new VariableExpr( newObj ); |
---|
1199 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
1200 | assignExpr->get_args().push_back( tempExpr->clone() ); |
---|
1201 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { |
---|
1202 | assignExpr->get_args().push_back( address->get_arg()->clone() ); |
---|
1203 | } else { |
---|
1204 | assignExpr->get_args().push_back( appExpr->get_args().front()->clone() ); |
---|
1205 | } // if |
---|
1206 | CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) ); |
---|
1207 | return new CommaExpr( firstComma, tempExpr ); |
---|
1208 | } // if |
---|
1209 | } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) { |
---|
1210 | assert( ! appExpr->get_results().empty() ); |
---|
1211 | assert( appExpr->get_args().size() == 1 ); |
---|
1212 | if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) { |
---|
1213 | return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" ); |
---|
1214 | } // if |
---|
1215 | } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) { |
---|
1216 | assert( ! appExpr->get_results().empty() ); |
---|
1217 | assert( appExpr->get_args().size() == 2 ); |
---|
1218 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env ); |
---|
1219 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env ); |
---|
1220 | if ( baseType1 && baseType2 ) { |
---|
1221 | UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) ); |
---|
1222 | divide->get_args().push_back( appExpr ); |
---|
1223 | divide->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); |
---|
1224 | divide->get_results().push_front( appExpr->get_results().front()->clone() ); |
---|
1225 | if ( appExpr->get_env() ) { |
---|
1226 | divide->set_env( appExpr->get_env() ); |
---|
1227 | appExpr->set_env( 0 ); |
---|
1228 | } // if |
---|
1229 | return divide; |
---|
1230 | } else if ( baseType1 ) { |
---|
1231 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1232 | multiply->get_args().push_back( appExpr->get_args().back() ); |
---|
1233 | multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); |
---|
1234 | appExpr->get_args().back() = multiply; |
---|
1235 | } else if ( baseType2 ) { |
---|
1236 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1237 | multiply->get_args().push_back( appExpr->get_args().front() ); |
---|
1238 | multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) ); |
---|
1239 | appExpr->get_args().front() = multiply; |
---|
1240 | } // if |
---|
1241 | } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) { |
---|
1242 | assert( ! appExpr->get_results().empty() ); |
---|
1243 | assert( appExpr->get_args().size() == 2 ); |
---|
1244 | Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ); |
---|
1245 | if ( baseType ) { |
---|
1246 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1247 | multiply->get_args().push_back( appExpr->get_args().back() ); |
---|
1248 | multiply->get_args().push_back( new SizeofExpr( baseType->clone() ) ); |
---|
1249 | appExpr->get_args().back() = multiply; |
---|
1250 | } // if |
---|
1251 | } // if |
---|
1252 | return appExpr; |
---|
1253 | } // if |
---|
1254 | } // if |
---|
1255 | return 0; |
---|
1256 | } |
---|
1257 | |
---|
1258 | Expression *Pass1::mutate( ApplicationExpr *appExpr ) { |
---|
1259 | // std::cerr << "mutate appExpr: "; |
---|
1260 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { |
---|
1261 | // std::cerr << i->first << " "; |
---|
1262 | // } |
---|
1263 | // std::cerr << "\n"; |
---|
1264 | bool oldUseRetval = useRetval; |
---|
1265 | useRetval = false; |
---|
1266 | appExpr->get_function()->acceptMutator( *this ); |
---|
1267 | mutateAll( appExpr->get_args(), *this ); |
---|
1268 | useRetval = oldUseRetval; |
---|
1269 | |
---|
1270 | assert( ! appExpr->get_function()->get_results().empty() ); |
---|
1271 | PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() ); |
---|
1272 | assert( pointer ); |
---|
1273 | FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() ); |
---|
1274 | assert( function ); |
---|
1275 | |
---|
1276 | if ( Expression *newExpr = handleIntrinsics( appExpr ) ) { |
---|
1277 | return newExpr; |
---|
1278 | } // if |
---|
1279 | |
---|
1280 | Expression *ret = appExpr; |
---|
1281 | |
---|
1282 | std::list< Expression *>::iterator arg = appExpr->get_args().begin(); |
---|
1283 | std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin(); |
---|
1284 | |
---|
1285 | TyVarMap exprTyVars( (TypeDecl::Kind)-1 ); |
---|
1286 | makeTyVarMap( function, exprTyVars ); |
---|
1287 | ReferenceToType *polyRetType = isPolyRet( function ); |
---|
1288 | |
---|
1289 | if ( polyRetType ) { |
---|
1290 | ret = addPolyRetParam( appExpr, function, polyRetType, arg ); |
---|
1291 | } else if ( needsAdapter( function, scopeTyVars ) ) { |
---|
1292 | // std::cerr << "needs adapter: "; |
---|
1293 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { |
---|
1294 | // std::cerr << i->first << " "; |
---|
1295 | // } |
---|
1296 | // std::cerr << "\n"; |
---|
1297 | // change the application so it calls the adapter rather than the passed function |
---|
1298 | ret = applyAdapter( appExpr, function, arg, scopeTyVars ); |
---|
1299 | } // if |
---|
1300 | arg = appExpr->get_args().begin(); |
---|
1301 | |
---|
1302 | passTypeVars( appExpr, polyRetType, arg, exprTyVars ); |
---|
1303 | addInferredParams( appExpr, function, arg, exprTyVars ); |
---|
1304 | |
---|
1305 | arg = paramBegin; |
---|
1306 | |
---|
1307 | boxParams( appExpr, function, arg, exprTyVars ); |
---|
1308 | passAdapters( appExpr, function, exprTyVars ); |
---|
1309 | |
---|
1310 | return ret; |
---|
1311 | } |
---|
1312 | |
---|
1313 | Expression *Pass1::mutate( UntypedExpr *expr ) { |
---|
1314 | if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) { |
---|
1315 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { |
---|
1316 | if ( name->get_name() == "*?" ) { |
---|
1317 | Expression *ret = expr->get_args().front(); |
---|
1318 | expr->get_args().clear(); |
---|
1319 | delete expr; |
---|
1320 | return ret->acceptMutator( *this ); |
---|
1321 | } // if |
---|
1322 | } // if |
---|
1323 | } // if |
---|
1324 | return PolyMutator::mutate( expr ); |
---|
1325 | } |
---|
1326 | |
---|
1327 | Expression *Pass1::mutate( AddressExpr *addrExpr ) { |
---|
1328 | assert( ! addrExpr->get_arg()->get_results().empty() ); |
---|
1329 | |
---|
1330 | bool needs = false; |
---|
1331 | if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) { |
---|
1332 | if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) { |
---|
1333 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { |
---|
1334 | if ( name->get_name() == "*?" ) { |
---|
1335 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) { |
---|
1336 | assert( ! appExpr->get_function()->get_results().empty() ); |
---|
1337 | PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() ); |
---|
1338 | assert( pointer ); |
---|
1339 | FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() ); |
---|
1340 | assert( function ); |
---|
1341 | needs = needsAdapter( function, scopeTyVars ); |
---|
1342 | } // if |
---|
1343 | } // if |
---|
1344 | } // if |
---|
1345 | } // if |
---|
1346 | } // if |
---|
1347 | addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) ); |
---|
1348 | if ( isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env ) || needs ) { |
---|
1349 | Expression *ret = addrExpr->get_arg(); |
---|
1350 | delete ret->get_results().front(); |
---|
1351 | ret->get_results().front() = addrExpr->get_results().front()->clone(); |
---|
1352 | addrExpr->set_arg( 0 ); |
---|
1353 | delete addrExpr; |
---|
1354 | return ret; |
---|
1355 | } else { |
---|
1356 | return addrExpr; |
---|
1357 | } // if |
---|
1358 | } |
---|
1359 | |
---|
1360 | /// Wraps a function declaration in a new pointer-to-function variable expression |
---|
1361 | VariableExpr *wrapFunctionDecl( DeclarationWithType *functionDecl ) { |
---|
1362 | // line below cloned from FixFunction.cc |
---|
1363 | ObjectDecl *functionObj = new ObjectDecl( functionDecl->get_name(), functionDecl->get_storageClass(), functionDecl->get_linkage(), 0, |
---|
1364 | new PointerType( Type::Qualifiers(), functionDecl->get_type()->clone() ), 0 ); |
---|
1365 | functionObj->set_mangleName( functionDecl->get_mangleName() ); |
---|
1366 | return new VariableExpr( functionObj ); |
---|
1367 | } |
---|
1368 | |
---|
1369 | Statement * Pass1::mutate( ReturnStmt *returnStmt ) { |
---|
1370 | if ( retval && returnStmt->get_expr() ) { |
---|
1371 | assert( ! returnStmt->get_expr()->get_results().empty() ); |
---|
1372 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. |
---|
1373 | // if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) { |
---|
1374 | // by this point, a cast expr on a polymorphic return value is redundant |
---|
1375 | while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) { |
---|
1376 | returnStmt->set_expr( castExpr->get_arg() ); |
---|
1377 | returnStmt->get_expr()->set_env( castExpr->get_env() ); |
---|
1378 | castExpr->set_env( 0 ); |
---|
1379 | castExpr->set_arg( 0 ); |
---|
1380 | delete castExpr; |
---|
1381 | } //while |
---|
1382 | |
---|
1383 | // find assignment operator for (polymorphic) return type |
---|
1384 | ApplicationExpr *assignExpr = 0; |
---|
1385 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) { |
---|
1386 | // find assignment operator for type variable |
---|
1387 | ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); |
---|
1388 | if ( assignIter == assignOps.end() ) { |
---|
1389 | throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() ); |
---|
1390 | } // if |
---|
1391 | assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) ); |
---|
1392 | } else if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( retval->get_type() ) ) { |
---|
1393 | // find assignment operator for generic type |
---|
1394 | DeclarationWithType *functionDecl = scopedAssignOps.find( refType ); |
---|
1395 | if ( ! functionDecl ) { |
---|
1396 | throw SemanticError( "Attempt to return dtype or ftype generic object in ", returnStmt->get_expr() ); |
---|
1397 | } |
---|
1398 | |
---|
1399 | // wrap it up in an application expression |
---|
1400 | assignExpr = new ApplicationExpr( wrapFunctionDecl( functionDecl ) ); |
---|
1401 | assignExpr->set_env( env->clone() ); |
---|
1402 | |
---|
1403 | // find each of its needed secondary assignment operators |
---|
1404 | std::list< Expression* > &tyParams = refType->get_parameters(); |
---|
1405 | std::list< TypeDecl* > &forallParams = functionDecl->get_type()->get_forall(); |
---|
1406 | std::list< Expression* >::const_iterator tyIt = tyParams.begin(); |
---|
1407 | std::list< TypeDecl* >::const_iterator forallIt = forallParams.begin(); |
---|
1408 | for ( ; tyIt != tyParams.end() && forallIt != forallParams.end(); ++tyIt, ++forallIt ) { |
---|
1409 | // Add appropriate mapping to assignment expression environment |
---|
1410 | TypeExpr *formalTypeExpr = dynamic_cast< TypeExpr* >( *tyIt ); |
---|
1411 | assert( formalTypeExpr && "type parameters must be type expressions" ); |
---|
1412 | Type *formalType = formalTypeExpr->get_type(); |
---|
1413 | assignExpr->get_env()->add( (*forallIt)->get_name(), formalType ); |
---|
1414 | |
---|
1415 | // skip types with no assign op (ftype/dtype) |
---|
1416 | if ( (*forallIt)->get_kind() != TypeDecl::Any ) continue; |
---|
1417 | |
---|
1418 | // find assignment operator for formal type |
---|
1419 | DeclarationWithType *assertAssign = 0; |
---|
1420 | if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) { |
---|
1421 | ScopedMap< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() ); |
---|
1422 | if ( assertAssignIt == assignOps.end() ) { |
---|
1423 | throw SemanticError( "No assignment operation found for ", formalTypeInstType ); |
---|
1424 | } |
---|
1425 | assertAssign = assertAssignIt->second; |
---|
1426 | } else { |
---|
1427 | assertAssign = scopedAssignOps.find( formalType ); |
---|
1428 | if ( ! assertAssign ) { |
---|
1429 | throw SemanticError( "No assignment operation found for ", formalType ); |
---|
1430 | } |
---|
1431 | } |
---|
1432 | |
---|
1433 | // add inferred parameter for field assignment operator to assignment expression |
---|
1434 | std::list< DeclarationWithType* > &asserts = (*forallIt)->get_assertions(); |
---|
1435 | assert( ! asserts.empty() && "Type param needs assignment operator assertion" ); |
---|
1436 | DeclarationWithType *actualDecl = asserts.front(); |
---|
1437 | assignExpr->get_inferParams()[ actualDecl->get_uniqueId() ] |
---|
1438 | = ParamEntry( assertAssign->get_uniqueId(), assertAssign->get_type()->clone(), actualDecl->get_type()->clone(), wrapFunctionDecl( assertAssign ) ); |
---|
1439 | } |
---|
1440 | } |
---|
1441 | assert( assignExpr ); |
---|
1442 | |
---|
1443 | // replace return statement with appropriate assignment to out parameter |
---|
1444 | Expression *retParm = new NameExpr( retval->get_name() ); |
---|
1445 | retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) ); |
---|
1446 | assignExpr->get_args().push_back( retParm ); |
---|
1447 | assignExpr->get_args().push_back( returnStmt->get_expr() ); |
---|
1448 | stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) ); |
---|
1449 | // } else { |
---|
1450 | // useRetval = true; |
---|
1451 | // stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) ); |
---|
1452 | // useRetval = false; |
---|
1453 | // } // if |
---|
1454 | returnStmt->set_expr( 0 ); |
---|
1455 | } else { |
---|
1456 | returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) ); |
---|
1457 | } // if |
---|
1458 | return returnStmt; |
---|
1459 | } |
---|
1460 | |
---|
1461 | Type * Pass1::mutate( PointerType *pointerType ) { |
---|
1462 | scopeTyVars.beginScope(); |
---|
1463 | makeTyVarMap( pointerType, scopeTyVars ); |
---|
1464 | |
---|
1465 | Type *ret = Mutator::mutate( pointerType ); |
---|
1466 | |
---|
1467 | scopeTyVars.endScope(); |
---|
1468 | return ret; |
---|
1469 | } |
---|
1470 | |
---|
1471 | Type * Pass1::mutate( FunctionType *functionType ) { |
---|
1472 | scopeTyVars.beginScope(); |
---|
1473 | makeTyVarMap( functionType, scopeTyVars ); |
---|
1474 | |
---|
1475 | Type *ret = Mutator::mutate( functionType ); |
---|
1476 | |
---|
1477 | scopeTyVars.endScope(); |
---|
1478 | return ret; |
---|
1479 | } |
---|
1480 | |
---|
1481 | void Pass1::doBeginScope() { |
---|
1482 | adapters.beginScope(); |
---|
1483 | scopedAssignOps.beginScope(); |
---|
1484 | } |
---|
1485 | |
---|
1486 | void Pass1::doEndScope() { |
---|
1487 | adapters.endScope(); |
---|
1488 | scopedAssignOps.endScope(); |
---|
1489 | } |
---|
1490 | |
---|
1491 | ////////////////////////////////////////// Pass2 //////////////////////////////////////////////////// |
---|
1492 | |
---|
1493 | void Pass2::addAdapters( FunctionType *functionType ) { |
---|
1494 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); |
---|
1495 | std::list< FunctionType *> functions; |
---|
1496 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
1497 | Type *orig = (*arg)->get_type(); |
---|
1498 | findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter ); |
---|
1499 | (*arg)->set_type( orig ); |
---|
1500 | } |
---|
1501 | std::set< std::string > adaptersDone; |
---|
1502 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { |
---|
1503 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars ); |
---|
1504 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { |
---|
1505 | std::string adapterName = makeAdapterName( mangleName ); |
---|
1506 | paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ); |
---|
1507 | adaptersDone.insert( adaptersDone.begin(), mangleName ); |
---|
1508 | } |
---|
1509 | } |
---|
1510 | // deleteAll( functions ); |
---|
1511 | } |
---|
1512 | |
---|
1513 | template< typename DeclClass > |
---|
1514 | DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) { |
---|
1515 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); |
---|
1516 | |
---|
1517 | return ret; |
---|
1518 | } |
---|
1519 | |
---|
1520 | DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) { |
---|
1521 | return handleDecl( functionDecl, functionDecl->get_functionType() ); |
---|
1522 | } |
---|
1523 | |
---|
1524 | ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) { |
---|
1525 | return handleDecl( objectDecl, objectDecl->get_type() ); |
---|
1526 | } |
---|
1527 | |
---|
1528 | TypeDecl * Pass2::mutate( TypeDecl *typeDecl ) { |
---|
1529 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
1530 | if ( typeDecl->get_base() ) { |
---|
1531 | return handleDecl( typeDecl, typeDecl->get_base() ); |
---|
1532 | } else { |
---|
1533 | return Mutator::mutate( typeDecl ); |
---|
1534 | } |
---|
1535 | } |
---|
1536 | |
---|
1537 | TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) { |
---|
1538 | return handleDecl( typedefDecl, typedefDecl->get_base() ); |
---|
1539 | } |
---|
1540 | |
---|
1541 | Type * Pass2::mutate( PointerType *pointerType ) { |
---|
1542 | scopeTyVars.beginScope(); |
---|
1543 | makeTyVarMap( pointerType, scopeTyVars ); |
---|
1544 | |
---|
1545 | Type *ret = Mutator::mutate( pointerType ); |
---|
1546 | |
---|
1547 | scopeTyVars.endScope(); |
---|
1548 | return ret; |
---|
1549 | } |
---|
1550 | |
---|
1551 | Type *Pass2::mutate( FunctionType *funcType ) { |
---|
1552 | scopeTyVars.beginScope(); |
---|
1553 | makeTyVarMap( funcType, scopeTyVars ); |
---|
1554 | |
---|
1555 | // move polymorphic return type to parameter list |
---|
1556 | if ( isPolyRet( funcType ) ) { |
---|
1557 | DeclarationWithType *ret = funcType->get_returnVals().front(); |
---|
1558 | ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) ); |
---|
1559 | funcType->get_parameters().push_front( ret ); |
---|
1560 | funcType->get_returnVals().pop_front(); |
---|
1561 | } |
---|
1562 | |
---|
1563 | // add size/align and assertions for type parameters to parameter list |
---|
1564 | std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin(); |
---|
1565 | std::list< DeclarationWithType *> inferredParams; |
---|
1566 | ObjectDecl newObj( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ); |
---|
1567 | ObjectDecl newPtr( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, |
---|
1568 | new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 ); |
---|
1569 | for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) { |
---|
1570 | ObjectDecl *sizeParm, *alignParm; |
---|
1571 | // add all size and alignment parameters to parameter list |
---|
1572 | if ( (*tyParm)->get_kind() == TypeDecl::Any ) { |
---|
1573 | TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm ); |
---|
1574 | std::string parmName = mangleType( &parmType ); |
---|
1575 | |
---|
1576 | sizeParm = newObj.clone(); |
---|
1577 | sizeParm->set_name( sizeofName( parmName ) ); |
---|
1578 | last = funcType->get_parameters().insert( last, sizeParm ); |
---|
1579 | ++last; |
---|
1580 | |
---|
1581 | alignParm = newObj.clone(); |
---|
1582 | alignParm->set_name( alignofName( parmName ) ); |
---|
1583 | last = funcType->get_parameters().insert( last, alignParm ); |
---|
1584 | ++last; |
---|
1585 | } |
---|
1586 | // move all assertions into parameter list |
---|
1587 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) { |
---|
1588 | // *assert = (*assert)->acceptMutator( *this ); |
---|
1589 | inferredParams.push_back( *assert ); |
---|
1590 | } |
---|
1591 | (*tyParm)->get_assertions().clear(); |
---|
1592 | } |
---|
1593 | |
---|
1594 | // add size/align for generic parameter types to parameter list |
---|
1595 | std::set< std::string > seenTypes; // sizeofName for generic types we've seen |
---|
1596 | for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) { |
---|
1597 | Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars ); |
---|
1598 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { |
---|
1599 | std::string typeName = mangleType( polyType ); |
---|
1600 | if ( seenTypes.count( typeName ) ) continue; |
---|
1601 | |
---|
1602 | ObjectDecl *sizeParm, *alignParm, *offsetParm; |
---|
1603 | sizeParm = newObj.clone(); |
---|
1604 | sizeParm->set_name( sizeofName( typeName ) ); |
---|
1605 | last = funcType->get_parameters().insert( last, sizeParm ); |
---|
1606 | ++last; |
---|
1607 | |
---|
1608 | alignParm = newObj.clone(); |
---|
1609 | alignParm->set_name( alignofName( typeName ) ); |
---|
1610 | last = funcType->get_parameters().insert( last, alignParm ); |
---|
1611 | ++last; |
---|
1612 | |
---|
1613 | if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( polyType ) ) { |
---|
1614 | // NOTE zero-length arrays are illegal in C, so empty structs have no offset array |
---|
1615 | if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) { |
---|
1616 | offsetParm = newPtr.clone(); |
---|
1617 | offsetParm->set_name( offsetofName( typeName ) ); |
---|
1618 | last = funcType->get_parameters().insert( last, offsetParm ); |
---|
1619 | ++last; |
---|
1620 | } |
---|
1621 | } |
---|
1622 | |
---|
1623 | seenTypes.insert( typeName ); |
---|
1624 | } |
---|
1625 | } |
---|
1626 | |
---|
1627 | // splice assertion parameters into parameter list |
---|
1628 | funcType->get_parameters().splice( last, inferredParams ); |
---|
1629 | addAdapters( funcType ); |
---|
1630 | mutateAll( funcType->get_returnVals(), *this ); |
---|
1631 | mutateAll( funcType->get_parameters(), *this ); |
---|
1632 | |
---|
1633 | scopeTyVars.endScope(); |
---|
1634 | return funcType; |
---|
1635 | } |
---|
1636 | |
---|
1637 | //////////////////////////////////////// GenericInstantiator ////////////////////////////////////////////////// |
---|
1638 | |
---|
1639 | /// Makes substitutions of params into baseParams; returns true if all parameters substituted for a concrete type |
---|
1640 | bool makeSubstitutions( const std::list< TypeDecl* >& baseParams, const std::list< Expression* >& params, std::list< TypeExpr* >& out ) { |
---|
1641 | bool allConcrete = true; // will finish the substitution list even if they're not all concrete |
---|
1642 | |
---|
1643 | // substitute concrete types for given parameters, and incomplete types for placeholders |
---|
1644 | std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin(); |
---|
1645 | std::list< Expression* >::const_iterator param = params.begin(); |
---|
1646 | for ( ; baseParam != baseParams.end() && param != params.end(); ++baseParam, ++param ) { |
---|
1647 | // switch ( (*baseParam)->get_kind() ) { |
---|
1648 | // case TypeDecl::Any: { // any type is a valid substitution here; complete types can be used to instantiate generics |
---|
1649 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
1650 | assert(paramType && "Aggregate parameters should be type expressions"); |
---|
1651 | out.push_back( paramType->clone() ); |
---|
1652 | // check that the substituted type isn't a type variable itself |
---|
1653 | if ( dynamic_cast< TypeInstType* >( paramType->get_type() ) ) { |
---|
1654 | allConcrete = false; |
---|
1655 | } |
---|
1656 | // break; |
---|
1657 | // } |
---|
1658 | // case TypeDecl::Dtype: // dtype can be consistently replaced with void [only pointers, which become void*] |
---|
1659 | // out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) ); |
---|
1660 | // break; |
---|
1661 | // case TypeDecl::Ftype: // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype] |
---|
1662 | // out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) ); |
---|
1663 | // break; |
---|
1664 | // } |
---|
1665 | } |
---|
1666 | |
---|
1667 | // if any parameters left over, not done |
---|
1668 | if ( baseParam != baseParams.end() ) return false; |
---|
1669 | // // if not enough parameters given, substitute remaining incomplete types for placeholders |
---|
1670 | // for ( ; baseParam != baseParams.end(); ++baseParam ) { |
---|
1671 | // switch ( (*baseParam)->get_kind() ) { |
---|
1672 | // case TypeDecl::Any: // no more substitutions here, fail early |
---|
1673 | // return false; |
---|
1674 | // case TypeDecl::Dtype: // dtype can be consistently replaced with void [only pointers, which become void*] |
---|
1675 | // out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) ); |
---|
1676 | // break; |
---|
1677 | // case TypeDecl::Ftype: // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype] |
---|
1678 | // out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) ); |
---|
1679 | // break; |
---|
1680 | // } |
---|
1681 | // } |
---|
1682 | |
---|
1683 | return allConcrete; |
---|
1684 | } |
---|
1685 | |
---|
1686 | /// Substitutes types of members of in according to baseParams => typeSubs, appending the result to out |
---|
1687 | void substituteMembers( const std::list< Declaration* >& in, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs, |
---|
1688 | std::list< Declaration* >& out ) { |
---|
1689 | // substitute types into new members |
---|
1690 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() ); |
---|
1691 | for ( std::list< Declaration* >::const_iterator member = in.begin(); member != in.end(); ++member ) { |
---|
1692 | Declaration *newMember = (*member)->clone(); |
---|
1693 | subs.apply(newMember); |
---|
1694 | out.push_back( newMember ); |
---|
1695 | } |
---|
1696 | } |
---|
1697 | |
---|
1698 | Type* GenericInstantiator::mutate( StructInstType *inst ) { |
---|
1699 | // mutate subtypes |
---|
1700 | Type *mutated = Mutator::mutate( inst ); |
---|
1701 | inst = dynamic_cast< StructInstType* >( mutated ); |
---|
1702 | if ( ! inst ) return mutated; |
---|
1703 | |
---|
1704 | // exit early if no need for further mutation |
---|
1705 | if ( inst->get_parameters().empty() ) return inst; |
---|
1706 | assert( inst->get_baseParameters() && "Base struct has parameters" ); |
---|
1707 | |
---|
1708 | // check if type can be concretely instantiated; put substitutions into typeSubs |
---|
1709 | std::list< TypeExpr* > typeSubs; |
---|
1710 | if ( ! makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ) ) { |
---|
1711 | deleteAll( typeSubs ); |
---|
1712 | return inst; |
---|
1713 | } |
---|
1714 | |
---|
1715 | // make concrete instantiation of generic type |
---|
1716 | StructDecl *concDecl = lookup( inst, typeSubs ); |
---|
1717 | if ( ! concDecl ) { |
---|
1718 | // set concDecl to new type, insert type declaration into statements to add |
---|
1719 | concDecl = new StructDecl( typeNamer.newName( inst->get_name() ) ); |
---|
1720 | substituteMembers( inst->get_baseStruct()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() ); |
---|
1721 | DeclMutator::addDeclaration( concDecl ); |
---|
1722 | insert( inst, typeSubs, concDecl ); |
---|
1723 | } |
---|
1724 | StructInstType *newInst = new StructInstType( inst->get_qualifiers(), concDecl->get_name() ); |
---|
1725 | newInst->set_baseStruct( concDecl ); |
---|
1726 | |
---|
1727 | deleteAll( typeSubs ); |
---|
1728 | delete inst; |
---|
1729 | return newInst; |
---|
1730 | } |
---|
1731 | |
---|
1732 | Type* GenericInstantiator::mutate( UnionInstType *inst ) { |
---|
1733 | // mutate subtypes |
---|
1734 | Type *mutated = Mutator::mutate( inst ); |
---|
1735 | inst = dynamic_cast< UnionInstType* >( mutated ); |
---|
1736 | if ( ! inst ) return mutated; |
---|
1737 | |
---|
1738 | // exit early if no need for further mutation |
---|
1739 | if ( inst->get_parameters().empty() ) return inst; |
---|
1740 | assert( inst->get_baseParameters() && "Base union has parameters" ); |
---|
1741 | |
---|
1742 | // check if type can be concretely instantiated; put substitutions into typeSubs |
---|
1743 | std::list< TypeExpr* > typeSubs; |
---|
1744 | if ( ! makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ) ) { |
---|
1745 | deleteAll( typeSubs ); |
---|
1746 | return inst; |
---|
1747 | } |
---|
1748 | |
---|
1749 | // make concrete instantiation of generic type |
---|
1750 | UnionDecl *concDecl = lookup( inst, typeSubs ); |
---|
1751 | if ( ! concDecl ) { |
---|
1752 | // set concDecl to new type, insert type declaration into statements to add |
---|
1753 | concDecl = new UnionDecl( typeNamer.newName( inst->get_name() ) ); |
---|
1754 | substituteMembers( inst->get_baseUnion()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() ); |
---|
1755 | DeclMutator::addDeclaration( concDecl ); |
---|
1756 | insert( inst, typeSubs, concDecl ); |
---|
1757 | } |
---|
1758 | UnionInstType *newInst = new UnionInstType( inst->get_qualifiers(), concDecl->get_name() ); |
---|
1759 | newInst->set_baseUnion( concDecl ); |
---|
1760 | |
---|
1761 | deleteAll( typeSubs ); |
---|
1762 | delete inst; |
---|
1763 | return newInst; |
---|
1764 | } |
---|
1765 | |
---|
1766 | // /// Gets the base struct or union declaration for a member expression; NULL if not applicable |
---|
1767 | // AggregateDecl* getMemberBaseDecl( MemberExpr *memberExpr ) { |
---|
1768 | // // get variable for member aggregate |
---|
1769 | // VariableExpr *varExpr = dynamic_cast< VariableExpr* >( memberExpr->get_aggregate() ); |
---|
1770 | // if ( ! varExpr ) return NULL; |
---|
1771 | // |
---|
1772 | // // get object for variable |
---|
1773 | // ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() ); |
---|
1774 | // if ( ! objectDecl ) return NULL; |
---|
1775 | // |
---|
1776 | // // get base declaration from object type |
---|
1777 | // Type *objectType = objectDecl->get_type(); |
---|
1778 | // StructInstType *structType = dynamic_cast< StructInstType* >( objectType ); |
---|
1779 | // if ( structType ) return structType->get_baseStruct(); |
---|
1780 | // UnionInstType *unionType = dynamic_cast< UnionInstType* >( objectType ); |
---|
1781 | // if ( unionType ) return unionType->get_baseUnion(); |
---|
1782 | // |
---|
1783 | // return NULL; |
---|
1784 | // } |
---|
1785 | // |
---|
1786 | // /// Finds the declaration with the given name, returning decls.end() if none such |
---|
1787 | // std::list< Declaration* >::const_iterator findDeclNamed( const std::list< Declaration* > &decls, const std::string &name ) { |
---|
1788 | // for( std::list< Declaration* >::const_iterator decl = decls.begin(); decl != decls.end(); ++decl ) { |
---|
1789 | // if ( (*decl)->get_name() == name ) return decl; |
---|
1790 | // } |
---|
1791 | // return decls.end(); |
---|
1792 | // } |
---|
1793 | // |
---|
1794 | // Expression* Instantiate::mutate( MemberExpr *memberExpr ) { |
---|
1795 | // // mutate, exiting early if no longer MemberExpr |
---|
1796 | // Expression *expr = Mutator::mutate( memberExpr ); |
---|
1797 | // memberExpr = dynamic_cast< MemberExpr* >( expr ); |
---|
1798 | // if ( ! memberExpr ) return expr; |
---|
1799 | // |
---|
1800 | // // get declaration of member and base declaration of member, exiting early if not found |
---|
1801 | // AggregateDecl *memberBase = getMemberBaseDecl( memberExpr ); |
---|
1802 | // if ( ! memberBase ) return memberExpr; |
---|
1803 | // DeclarationWithType *memberDecl = memberExpr->get_member(); |
---|
1804 | // std::list< Declaration* >::const_iterator baseIt = findDeclNamed( memberBase->get_members(), memberDecl->get_name() ); |
---|
1805 | // if ( baseIt == memberBase->get_members().end() ) return memberExpr; |
---|
1806 | // DeclarationWithType *baseDecl = dynamic_cast< DeclarationWithType* >( *baseIt ); |
---|
1807 | // if ( ! baseDecl ) return memberExpr; |
---|
1808 | // |
---|
1809 | // // check if stated type of the member is not the type of the member's declaration; if so, need a cast |
---|
1810 | // // this *SHOULD* be safe, I don't think anything but the void-replacements I put in for dtypes would make it past the typechecker |
---|
1811 | // SymTab::Indexer dummy; |
---|
1812 | // if ( ResolvExpr::typesCompatible( memberDecl->get_type(), baseDecl->get_type(), dummy ) ) return memberExpr; |
---|
1813 | // else return new CastExpr( memberExpr, memberDecl->get_type() ); |
---|
1814 | // } |
---|
1815 | |
---|
1816 | void GenericInstantiator::doBeginScope() { |
---|
1817 | DeclMutator::doBeginScope(); |
---|
1818 | instantiations.beginScope(); |
---|
1819 | } |
---|
1820 | |
---|
1821 | void GenericInstantiator::doEndScope() { |
---|
1822 | DeclMutator::doEndScope(); |
---|
1823 | instantiations.endScope(); |
---|
1824 | } |
---|
1825 | |
---|
1826 | ////////////////////////////////////////// PolyGenericCalculator //////////////////////////////////////////////////// |
---|
1827 | |
---|
1828 | void PolyGenericCalculator::beginTypeScope( Type *ty ) { |
---|
1829 | scopeTyVars.beginScope(); |
---|
1830 | makeTyVarMap( ty, scopeTyVars ); |
---|
1831 | } |
---|
1832 | |
---|
1833 | void PolyGenericCalculator::endTypeScope() { |
---|
1834 | scopeTyVars.endScope(); |
---|
1835 | } |
---|
1836 | |
---|
1837 | template< typename DeclClass > |
---|
1838 | DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) { |
---|
1839 | beginTypeScope( type ); |
---|
1840 | knownLayouts.beginScope(); |
---|
1841 | knownOffsets.beginScope(); |
---|
1842 | |
---|
1843 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); |
---|
1844 | |
---|
1845 | knownOffsets.endScope(); |
---|
1846 | knownLayouts.endScope(); |
---|
1847 | endTypeScope(); |
---|
1848 | return ret; |
---|
1849 | } |
---|
1850 | |
---|
1851 | ObjectDecl * PolyGenericCalculator::mutate( ObjectDecl *objectDecl ) { |
---|
1852 | return handleDecl( objectDecl, objectDecl->get_type() ); |
---|
1853 | } |
---|
1854 | |
---|
1855 | DeclarationWithType * PolyGenericCalculator::mutate( FunctionDecl *functionDecl ) { |
---|
1856 | return handleDecl( functionDecl, functionDecl->get_functionType() ); |
---|
1857 | } |
---|
1858 | |
---|
1859 | TypedefDecl * PolyGenericCalculator::mutate( TypedefDecl *typedefDecl ) { |
---|
1860 | return handleDecl( typedefDecl, typedefDecl->get_base() ); |
---|
1861 | } |
---|
1862 | |
---|
1863 | TypeDecl * PolyGenericCalculator::mutate( TypeDecl *typeDecl ) { |
---|
1864 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
1865 | return Mutator::mutate( typeDecl ); |
---|
1866 | } |
---|
1867 | |
---|
1868 | Type * PolyGenericCalculator::mutate( PointerType *pointerType ) { |
---|
1869 | beginTypeScope( pointerType ); |
---|
1870 | |
---|
1871 | Type *ret = Mutator::mutate( pointerType ); |
---|
1872 | |
---|
1873 | endTypeScope(); |
---|
1874 | return ret; |
---|
1875 | } |
---|
1876 | |
---|
1877 | Type * PolyGenericCalculator::mutate( FunctionType *funcType ) { |
---|
1878 | beginTypeScope( funcType ); |
---|
1879 | |
---|
1880 | // make sure that any type information passed into the function is accounted for |
---|
1881 | for ( std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); fnParm != funcType->get_parameters().end(); ++fnParm ) { |
---|
1882 | // condition here duplicates that in Pass2::mutate( FunctionType* ) |
---|
1883 | Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars ); |
---|
1884 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { |
---|
1885 | knownLayouts.insert( mangleType( polyType ) ); |
---|
1886 | } |
---|
1887 | } |
---|
1888 | |
---|
1889 | Type *ret = Mutator::mutate( funcType ); |
---|
1890 | |
---|
1891 | endTypeScope(); |
---|
1892 | return ret; |
---|
1893 | } |
---|
1894 | |
---|
1895 | Statement *PolyGenericCalculator::mutate( DeclStmt *declStmt ) { |
---|
1896 | if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) { |
---|
1897 | if ( findGeneric( objectDecl->get_type() ) ) { |
---|
1898 | // change initialization of a polymorphic value object |
---|
1899 | // to allocate storage with alloca |
---|
1900 | Type *declType = objectDecl->get_type(); |
---|
1901 | UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) ); |
---|
1902 | alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType ) ) ) ); |
---|
1903 | |
---|
1904 | delete objectDecl->get_init(); |
---|
1905 | |
---|
1906 | std::list<Expression*> designators; |
---|
1907 | objectDecl->set_init( new SingleInit( alloc, designators ) ); |
---|
1908 | } |
---|
1909 | } |
---|
1910 | return Mutator::mutate( declStmt ); |
---|
1911 | } |
---|
1912 | |
---|
1913 | /// Finds the member in the base list that matches the given declaration; returns its index, or -1 if not present |
---|
1914 | long findMember( DeclarationWithType *memberDecl, std::list< Declaration* > &baseDecls ) { |
---|
1915 | long i = 0; |
---|
1916 | for(std::list< Declaration* >::const_iterator decl = baseDecls.begin(); decl != baseDecls.end(); ++decl, ++i ) { |
---|
1917 | if ( memberDecl->get_name() != (*decl)->get_name() ) continue; |
---|
1918 | |
---|
1919 | if ( DeclarationWithType *declWithType = dynamic_cast< DeclarationWithType* >( *decl ) ) { |
---|
1920 | if ( memberDecl->get_mangleName().empty() || declWithType->get_mangleName().empty() |
---|
1921 | || memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i; |
---|
1922 | else continue; |
---|
1923 | } else return i; |
---|
1924 | } |
---|
1925 | return -1; |
---|
1926 | } |
---|
1927 | |
---|
1928 | /// Returns an index expression into the offset array for a type |
---|
1929 | Expression *makeOffsetIndex( Type *objectType, long i ) { |
---|
1930 | std::stringstream offset_namer; |
---|
1931 | offset_namer << i; |
---|
1932 | ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) ); |
---|
1933 | UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) ); |
---|
1934 | fieldOffset->get_args().push_back( new NameExpr( offsetofName( mangleType( objectType ) ) ) ); |
---|
1935 | fieldOffset->get_args().push_back( fieldIndex ); |
---|
1936 | return fieldOffset; |
---|
1937 | } |
---|
1938 | |
---|
1939 | /// Returns an expression dereferenced n times |
---|
1940 | Expression *makeDerefdVar( Expression *derefdVar, long n ) { |
---|
1941 | for ( int i = 1; i < n; ++i ) { |
---|
1942 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
1943 | derefExpr->get_args().push_back( derefdVar ); |
---|
1944 | derefdVar = derefExpr; |
---|
1945 | } |
---|
1946 | return derefdVar; |
---|
1947 | } |
---|
1948 | |
---|
1949 | Expression *PolyGenericCalculator::mutate( MemberExpr *memberExpr ) { |
---|
1950 | // mutate, exiting early if no longer MemberExpr |
---|
1951 | Expression *expr = Mutator::mutate( memberExpr ); |
---|
1952 | memberExpr = dynamic_cast< MemberExpr* >( expr ); |
---|
1953 | if ( ! memberExpr ) return expr; |
---|
1954 | |
---|
1955 | // get declaration for base struct, exiting early if not found |
---|
1956 | int varDepth; |
---|
1957 | VariableExpr *varExpr = getBaseVar( memberExpr->get_aggregate(), &varDepth ); |
---|
1958 | if ( ! varExpr ) return memberExpr; |
---|
1959 | ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() ); |
---|
1960 | if ( ! objectDecl ) return memberExpr; |
---|
1961 | |
---|
1962 | // only mutate member expressions for polymorphic types |
---|
1963 | int tyDepth; |
---|
1964 | Type *objectType = hasPolyBase( objectDecl->get_type(), scopeTyVars, &tyDepth ); |
---|
1965 | if ( ! objectType ) return memberExpr; |
---|
1966 | findGeneric( objectType ); // ensure layout for this type is available |
---|
1967 | |
---|
1968 | Expression *newMemberExpr = 0; |
---|
1969 | if ( StructInstType *structType = dynamic_cast< StructInstType* >( objectType ) ) { |
---|
1970 | // look up offset index |
---|
1971 | long i = findMember( memberExpr->get_member(), structType->get_baseStruct()->get_members() ); |
---|
1972 | if ( i == -1 ) return memberExpr; |
---|
1973 | |
---|
1974 | // replace member expression with pointer to base plus offset |
---|
1975 | UntypedExpr *fieldLoc = new UntypedExpr( new NameExpr( "?+?" ) ); |
---|
1976 | fieldLoc->get_args().push_back( makeDerefdVar( varExpr->clone(), varDepth ) ); |
---|
1977 | fieldLoc->get_args().push_back( makeOffsetIndex( objectType, i ) ); |
---|
1978 | newMemberExpr = fieldLoc; |
---|
1979 | } else if ( dynamic_cast< UnionInstType* >( objectType ) ) { |
---|
1980 | // union members are all at offset zero, so build appropriately-dereferenced variable |
---|
1981 | newMemberExpr = makeDerefdVar( varExpr->clone(), varDepth ); |
---|
1982 | } else return memberExpr; |
---|
1983 | assert( newMemberExpr ); |
---|
1984 | |
---|
1985 | Type *memberType = memberExpr->get_member()->get_type(); |
---|
1986 | if ( ! isPolyType( memberType, scopeTyVars ) ) { |
---|
1987 | // Not all members of a polymorphic type are themselves of polymorphic type; in this case the member expression should be wrapped and dereferenced to form an lvalue |
---|
1988 | CastExpr *ptrCastExpr = new CastExpr( newMemberExpr, new PointerType( Type::Qualifiers(), memberType->clone() ) ); |
---|
1989 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
1990 | derefExpr->get_args().push_back( ptrCastExpr ); |
---|
1991 | newMemberExpr = derefExpr; |
---|
1992 | } |
---|
1993 | |
---|
1994 | delete memberExpr; |
---|
1995 | return newMemberExpr; |
---|
1996 | } |
---|
1997 | |
---|
1998 | ObjectDecl *PolyGenericCalculator::makeVar( const std::string &name, Type *type, Initializer *init ) { |
---|
1999 | ObjectDecl *newObj = new ObjectDecl( name, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, init ); |
---|
2000 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
2001 | return newObj; |
---|
2002 | } |
---|
2003 | |
---|
2004 | void PolyGenericCalculator::addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ) { |
---|
2005 | for ( std::list< Type* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) { |
---|
2006 | if ( findGeneric( *param ) ) { |
---|
2007 | // push size/align vars for a generic parameter back |
---|
2008 | std::string paramName = mangleType( *param ); |
---|
2009 | layoutCall->get_args().push_back( new NameExpr( sizeofName( paramName ) ) ); |
---|
2010 | layoutCall->get_args().push_back( new NameExpr( alignofName( paramName ) ) ); |
---|
2011 | } else { |
---|
2012 | layoutCall->get_args().push_back( new SizeofExpr( (*param)->clone() ) ); |
---|
2013 | layoutCall->get_args().push_back( new AlignofExpr( (*param)->clone() ) ); |
---|
2014 | } |
---|
2015 | } |
---|
2016 | } |
---|
2017 | |
---|
2018 | /// returns true if any of the otype parameters have a dynamic layout and puts all otype parameters in the output list |
---|
2019 | bool findGenericParams( std::list< TypeDecl* > &baseParams, std::list< Expression* > &typeParams, std::list< Type* > &out ) { |
---|
2020 | bool hasDynamicLayout = false; |
---|
2021 | |
---|
2022 | std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin(); |
---|
2023 | std::list< Expression* >::const_iterator typeParam = typeParams.begin(); |
---|
2024 | for ( ; baseParam != baseParams.end() && typeParam != typeParams.end(); ++baseParam, ++typeParam ) { |
---|
2025 | // skip non-otype parameters |
---|
2026 | if ( (*baseParam)->get_kind() != TypeDecl::Any ) continue; |
---|
2027 | TypeExpr *typeExpr = dynamic_cast< TypeExpr* >( *typeParam ); |
---|
2028 | assert( typeExpr && "all otype parameters should be type expressions" ); |
---|
2029 | |
---|
2030 | Type *type = typeExpr->get_type(); |
---|
2031 | out.push_back( type ); |
---|
2032 | if ( isPolyType( type ) ) hasDynamicLayout = true; |
---|
2033 | } |
---|
2034 | assert( baseParam == baseParams.end() && typeParam == typeParams.end() ); |
---|
2035 | |
---|
2036 | return hasDynamicLayout; |
---|
2037 | } |
---|
2038 | |
---|
2039 | bool PolyGenericCalculator::findGeneric( Type *ty ) { |
---|
2040 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( ty ) ) { |
---|
2041 | // duplicate logic from isPolyType() |
---|
2042 | if ( env ) { |
---|
2043 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) { |
---|
2044 | return findGeneric( newType ); |
---|
2045 | } // if |
---|
2046 | } // if |
---|
2047 | if ( scopeTyVars.find( typeInst->get_name() ) != scopeTyVars.end() ) { |
---|
2048 | // NOTE assumes here that getting put in the scopeTyVars included having the layout variables set |
---|
2049 | return true; |
---|
2050 | } |
---|
2051 | return false; |
---|
2052 | } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) { |
---|
2053 | // check if this type already has a layout generated for it |
---|
2054 | std::string typeName = mangleType( ty ); |
---|
2055 | if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true; |
---|
2056 | |
---|
2057 | // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized |
---|
2058 | std::list< Type* > otypeParams; |
---|
2059 | if ( ! findGenericParams( *structTy->get_baseParameters(), structTy->get_parameters(), otypeParams ) ) return false; |
---|
2060 | |
---|
2061 | // insert local variables for layout and generate call to layout function |
---|
2062 | knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call |
---|
2063 | Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
2064 | |
---|
2065 | int n_members = structTy->get_baseStruct()->get_members().size(); |
---|
2066 | if ( n_members == 0 ) { |
---|
2067 | // all empty structs have the same layout - size 1, align 1 |
---|
2068 | makeVar( sizeofName( typeName ), layoutType, new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) ); |
---|
2069 | makeVar( alignofName( typeName ), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) ); |
---|
2070 | // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array |
---|
2071 | } else { |
---|
2072 | ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType ); |
---|
2073 | ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() ); |
---|
2074 | ObjectDecl *offsetVar = makeVar( offsetofName( typeName ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from( n_members ) ), false, false ) ); |
---|
2075 | |
---|
2076 | // generate call to layout function |
---|
2077 | UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( structTy->get_baseStruct() ) ) ); |
---|
2078 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); |
---|
2079 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); |
---|
2080 | layoutCall->get_args().push_back( new VariableExpr( offsetVar ) ); |
---|
2081 | addOtypeParamsToLayoutCall( layoutCall, otypeParams ); |
---|
2082 | |
---|
2083 | stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) ); |
---|
2084 | } |
---|
2085 | |
---|
2086 | return true; |
---|
2087 | } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) { |
---|
2088 | // check if this type already has a layout generated for it |
---|
2089 | std::string typeName = mangleType( ty ); |
---|
2090 | if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true; |
---|
2091 | |
---|
2092 | // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized |
---|
2093 | std::list< Type* > otypeParams; |
---|
2094 | if ( ! findGenericParams( *unionTy->get_baseParameters(), unionTy->get_parameters(), otypeParams ) ) return false; |
---|
2095 | |
---|
2096 | // insert local variables for layout and generate call to layout function |
---|
2097 | knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call |
---|
2098 | Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
2099 | |
---|
2100 | ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType ); |
---|
2101 | ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() ); |
---|
2102 | |
---|
2103 | // generate call to layout function |
---|
2104 | UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( unionTy->get_baseUnion() ) ) ); |
---|
2105 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); |
---|
2106 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); |
---|
2107 | addOtypeParamsToLayoutCall( layoutCall, otypeParams ); |
---|
2108 | |
---|
2109 | stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) ); |
---|
2110 | |
---|
2111 | return true; |
---|
2112 | } |
---|
2113 | |
---|
2114 | return false; |
---|
2115 | } |
---|
2116 | |
---|
2117 | Expression *PolyGenericCalculator::mutate( SizeofExpr *sizeofExpr ) { |
---|
2118 | Type *ty = sizeofExpr->get_type(); |
---|
2119 | if ( findGeneric( ty ) ) { |
---|
2120 | Expression *ret = new NameExpr( sizeofName( mangleType( ty ) ) ); |
---|
2121 | delete sizeofExpr; |
---|
2122 | return ret; |
---|
2123 | } |
---|
2124 | return sizeofExpr; |
---|
2125 | } |
---|
2126 | |
---|
2127 | Expression *PolyGenericCalculator::mutate( AlignofExpr *alignofExpr ) { |
---|
2128 | Type *ty = alignofExpr->get_type(); |
---|
2129 | if ( findGeneric( ty ) ) { |
---|
2130 | Expression *ret = new NameExpr( alignofName( mangleType( ty ) ) ); |
---|
2131 | delete alignofExpr; |
---|
2132 | return ret; |
---|
2133 | } |
---|
2134 | return alignofExpr; |
---|
2135 | } |
---|
2136 | |
---|
2137 | Expression *PolyGenericCalculator::mutate( OffsetofExpr *offsetofExpr ) { |
---|
2138 | // mutate, exiting early if no longer OffsetofExpr |
---|
2139 | Expression *expr = Mutator::mutate( offsetofExpr ); |
---|
2140 | offsetofExpr = dynamic_cast< OffsetofExpr* >( expr ); |
---|
2141 | if ( ! offsetofExpr ) return expr; |
---|
2142 | |
---|
2143 | // only mutate expressions for polymorphic structs/unions |
---|
2144 | Type *ty = offsetofExpr->get_type(); |
---|
2145 | if ( ! findGeneric( ty ) ) return offsetofExpr; |
---|
2146 | |
---|
2147 | if ( StructInstType *structType = dynamic_cast< StructInstType* >( ty ) ) { |
---|
2148 | // replace offsetof expression by index into offset array |
---|
2149 | long i = findMember( offsetofExpr->get_member(), structType->get_baseStruct()->get_members() ); |
---|
2150 | if ( i == -1 ) return offsetofExpr; |
---|
2151 | |
---|
2152 | Expression *offsetInd = makeOffsetIndex( ty, i ); |
---|
2153 | delete offsetofExpr; |
---|
2154 | return offsetInd; |
---|
2155 | } else if ( dynamic_cast< UnionInstType* >( ty ) ) { |
---|
2156 | // all union members are at offset zero |
---|
2157 | delete offsetofExpr; |
---|
2158 | return new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::string("0") ) ); |
---|
2159 | } else return offsetofExpr; |
---|
2160 | } |
---|
2161 | |
---|
2162 | Expression *PolyGenericCalculator::mutate( OffsetPackExpr *offsetPackExpr ) { |
---|
2163 | StructInstType *ty = offsetPackExpr->get_type(); |
---|
2164 | |
---|
2165 | Expression *ret = 0; |
---|
2166 | if ( findGeneric( ty ) ) { |
---|
2167 | // pull offset back from generated type information |
---|
2168 | ret = new NameExpr( offsetofName( mangleType( ty ) ) ); |
---|
2169 | } else { |
---|
2170 | std::string offsetName = offsetofName( mangleType( ty ) ); |
---|
2171 | if ( knownOffsets.find( offsetName ) != knownOffsets.end() ) { |
---|
2172 | // use the already-generated offsets for this type |
---|
2173 | ret = new NameExpr( offsetName ); |
---|
2174 | } else { |
---|
2175 | knownOffsets.insert( offsetName ); |
---|
2176 | |
---|
2177 | std::list< Declaration* > &baseMembers = ty->get_baseStruct()->get_members(); |
---|
2178 | Type *offsetType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
2179 | |
---|
2180 | // build initializer list for offset array |
---|
2181 | std::list< Initializer* > inits; |
---|
2182 | for ( std::list< Declaration* >::const_iterator member = baseMembers.begin(); member != baseMembers.end(); ++member ) { |
---|
2183 | DeclarationWithType *memberDecl; |
---|
2184 | if ( DeclarationWithType *origMember = dynamic_cast< DeclarationWithType* >( *member ) ) { |
---|
2185 | memberDecl = origMember->clone(); |
---|
2186 | } else { |
---|
2187 | memberDecl = new ObjectDecl( (*member)->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, offsetType->clone(), 0 ); |
---|
2188 | } |
---|
2189 | inits.push_back( new SingleInit( new OffsetofExpr( ty->clone(), memberDecl ) ) ); |
---|
2190 | } |
---|
2191 | |
---|
2192 | // build the offset array and replace the pack with a reference to it |
---|
2193 | ObjectDecl *offsetArray = makeVar( offsetName, new ArrayType( Type::Qualifiers(), offsetType, new ConstantExpr( Constant::from( baseMembers.size() ) ), false, false ), |
---|
2194 | new ListInit( inits ) ); |
---|
2195 | ret = new VariableExpr( offsetArray ); |
---|
2196 | } |
---|
2197 | } |
---|
2198 | |
---|
2199 | delete offsetPackExpr; |
---|
2200 | return ret; |
---|
2201 | } |
---|
2202 | |
---|
2203 | void PolyGenericCalculator::doBeginScope() { |
---|
2204 | knownLayouts.beginScope(); |
---|
2205 | knownOffsets.beginScope(); |
---|
2206 | } |
---|
2207 | |
---|
2208 | void PolyGenericCalculator::doEndScope() { |
---|
2209 | knownLayouts.endScope(); |
---|
2210 | knownOffsets.endScope(); |
---|
2211 | } |
---|
2212 | |
---|
2213 | ////////////////////////////////////////// Pass3 //////////////////////////////////////////////////// |
---|
2214 | |
---|
2215 | template< typename DeclClass > |
---|
2216 | DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) { |
---|
2217 | scopeTyVars.beginScope(); |
---|
2218 | makeTyVarMap( type, scopeTyVars ); |
---|
2219 | |
---|
2220 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); |
---|
2221 | ScrubTyVars::scrub( decl, scopeTyVars ); |
---|
2222 | |
---|
2223 | scopeTyVars.endScope(); |
---|
2224 | return ret; |
---|
2225 | } |
---|
2226 | |
---|
2227 | ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) { |
---|
2228 | return handleDecl( objectDecl, objectDecl->get_type() ); |
---|
2229 | } |
---|
2230 | |
---|
2231 | DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) { |
---|
2232 | return handleDecl( functionDecl, functionDecl->get_functionType() ); |
---|
2233 | } |
---|
2234 | |
---|
2235 | TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) { |
---|
2236 | return handleDecl( typedefDecl, typedefDecl->get_base() ); |
---|
2237 | } |
---|
2238 | |
---|
2239 | TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) { |
---|
2240 | // Initializer *init = 0; |
---|
2241 | // std::list< Expression *> designators; |
---|
2242 | // scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
2243 | // if ( typeDecl->get_base() ) { |
---|
2244 | // init = new SimpleInit( new SizeofExpr( handleDecl( typeDecl, typeDecl->get_base() ) ), designators ); |
---|
2245 | // } |
---|
2246 | // return new ObjectDecl( typeDecl->get_name(), Declaration::Extern, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ), init ); |
---|
2247 | |
---|
2248 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
2249 | return Mutator::mutate( typeDecl ); |
---|
2250 | } |
---|
2251 | |
---|
2252 | Type * Pass3::mutate( PointerType *pointerType ) { |
---|
2253 | scopeTyVars.beginScope(); |
---|
2254 | makeTyVarMap( pointerType, scopeTyVars ); |
---|
2255 | |
---|
2256 | Type *ret = Mutator::mutate( pointerType ); |
---|
2257 | |
---|
2258 | scopeTyVars.endScope(); |
---|
2259 | return ret; |
---|
2260 | } |
---|
2261 | |
---|
2262 | Type * Pass3::mutate( FunctionType *functionType ) { |
---|
2263 | scopeTyVars.beginScope(); |
---|
2264 | makeTyVarMap( functionType, scopeTyVars ); |
---|
2265 | |
---|
2266 | Type *ret = Mutator::mutate( functionType ); |
---|
2267 | |
---|
2268 | scopeTyVars.endScope(); |
---|
2269 | return ret; |
---|
2270 | } |
---|
2271 | } // anonymous namespace |
---|
2272 | } // namespace GenPoly |
---|
2273 | |
---|
2274 | // Local Variables: // |
---|
2275 | // tab-width: 4 // |
---|
2276 | // mode: c++ // |
---|
2277 | // compile-command: "make install" // |
---|
2278 | // End: // |
---|