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