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 | // Validate.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 21:50:04 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Wed Apr 13 16:39:30 2016 |
---|
13 | // Update Count : 251 |
---|
14 | // |
---|
15 | |
---|
16 | // The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be |
---|
17 | // as regular in structure as possible. Some assumptions can be made regarding the state of the tree after this pass is |
---|
18 | // complete, including: |
---|
19 | // |
---|
20 | // - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or |
---|
21 | // union. |
---|
22 | // |
---|
23 | // - All enumeration constants have type EnumInstType. |
---|
24 | // |
---|
25 | // - The type "void" never occurs in lists of function parameter or return types; neither do tuple types. A function |
---|
26 | // taking no arguments has no argument types, and tuples are flattened. |
---|
27 | // |
---|
28 | // - No context instances exist; they are all replaced by the set of declarations signified by the context, instantiated |
---|
29 | // by the particular set of type arguments. |
---|
30 | // |
---|
31 | // - Every declaration is assigned a unique id. |
---|
32 | // |
---|
33 | // - No typedef declarations or instances exist; the actual type is substituted for each instance. |
---|
34 | // |
---|
35 | // - Each type, struct, and union definition is followed by an appropriate assignment operator. |
---|
36 | // |
---|
37 | // - Each use of a struct or union is connected to a complete definition of that struct or union, even if that |
---|
38 | // definition occurs later in the input. |
---|
39 | |
---|
40 | #include <list> |
---|
41 | #include <iterator> |
---|
42 | #include "Common/utility.h" |
---|
43 | #include "Common/UniqueName.h" |
---|
44 | #include "Validate.h" |
---|
45 | #include "SynTree/Visitor.h" |
---|
46 | #include "SynTree/Mutator.h" |
---|
47 | #include "SynTree/Type.h" |
---|
48 | #include "SynTree/Expression.h" |
---|
49 | #include "SynTree/Statement.h" |
---|
50 | #include "SynTree/TypeSubstitution.h" |
---|
51 | #include "Indexer.h" |
---|
52 | #include "FixFunction.h" |
---|
53 | // #include "ImplementationType.h" |
---|
54 | #include "GenPoly/DeclMutator.h" |
---|
55 | #include "AddVisit.h" |
---|
56 | #include "MakeLibCfa.h" |
---|
57 | #include "TypeEquality.h" |
---|
58 | #include "ResolvExpr/typeops.h" |
---|
59 | |
---|
60 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; } |
---|
61 | |
---|
62 | namespace SymTab { |
---|
63 | class HoistStruct : public Visitor { |
---|
64 | public: |
---|
65 | /// Flattens nested struct types |
---|
66 | static void hoistStruct( std::list< Declaration * > &translationUnit ); |
---|
67 | |
---|
68 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } |
---|
69 | |
---|
70 | virtual void visit( StructDecl *aggregateDecl ); |
---|
71 | virtual void visit( UnionDecl *aggregateDecl ); |
---|
72 | |
---|
73 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
74 | virtual void visit( SwitchStmt *switchStmt ); |
---|
75 | virtual void visit( ChooseStmt *chooseStmt ); |
---|
76 | // virtual void visit( CaseStmt *caseStmt ); |
---|
77 | private: |
---|
78 | HoistStruct(); |
---|
79 | |
---|
80 | template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl ); |
---|
81 | |
---|
82 | std::list< Declaration * > declsToAdd; |
---|
83 | bool inStruct; |
---|
84 | }; |
---|
85 | |
---|
86 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers. |
---|
87 | class Pass1 : public Visitor { |
---|
88 | typedef Visitor Parent; |
---|
89 | virtual void visit( EnumDecl *aggregateDecl ); |
---|
90 | virtual void visit( FunctionType *func ); |
---|
91 | }; |
---|
92 | |
---|
93 | /// Associates forward declarations of aggregates with their definitions |
---|
94 | class Pass2 : public Indexer { |
---|
95 | typedef Indexer Parent; |
---|
96 | public: |
---|
97 | Pass2( bool doDebug, const Indexer *indexer ); |
---|
98 | private: |
---|
99 | virtual void visit( StructInstType *structInst ); |
---|
100 | virtual void visit( UnionInstType *unionInst ); |
---|
101 | virtual void visit( TraitInstType *contextInst ); |
---|
102 | virtual void visit( StructDecl *structDecl ); |
---|
103 | virtual void visit( UnionDecl *unionDecl ); |
---|
104 | virtual void visit( TypeInstType *typeInst ); |
---|
105 | |
---|
106 | const Indexer *indexer; |
---|
107 | |
---|
108 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType; |
---|
109 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType; |
---|
110 | ForwardStructsType forwardStructs; |
---|
111 | ForwardUnionsType forwardUnions; |
---|
112 | }; |
---|
113 | |
---|
114 | /// Replaces array and function types in forall lists by appropriate pointer type |
---|
115 | class Pass3 : public Indexer { |
---|
116 | typedef Indexer Parent; |
---|
117 | public: |
---|
118 | Pass3( const Indexer *indexer ); |
---|
119 | private: |
---|
120 | virtual void visit( ObjectDecl *object ); |
---|
121 | virtual void visit( FunctionDecl *func ); |
---|
122 | |
---|
123 | const Indexer *indexer; |
---|
124 | }; |
---|
125 | |
---|
126 | class AutogenerateRoutines : public Visitor { |
---|
127 | public: |
---|
128 | /// Generates assignment operators for aggregate types as required |
---|
129 | static void autogenerateRoutines( std::list< Declaration * > &translationUnit ); |
---|
130 | |
---|
131 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } |
---|
132 | |
---|
133 | virtual void visit( EnumDecl *enumDecl ); |
---|
134 | virtual void visit( StructDecl *structDecl ); |
---|
135 | virtual void visit( UnionDecl *structDecl ); |
---|
136 | virtual void visit( TypeDecl *typeDecl ); |
---|
137 | virtual void visit( TraitDecl *ctxDecl ); |
---|
138 | virtual void visit( FunctionDecl *functionDecl ); |
---|
139 | |
---|
140 | virtual void visit( FunctionType *ftype ); |
---|
141 | virtual void visit( PointerType *ftype ); |
---|
142 | |
---|
143 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
144 | virtual void visit( SwitchStmt *switchStmt ); |
---|
145 | virtual void visit( ChooseStmt *chooseStmt ); |
---|
146 | // virtual void visit( CaseStmt *caseStmt ); |
---|
147 | |
---|
148 | AutogenerateRoutines() : functionNesting( 0 ) {} |
---|
149 | private: |
---|
150 | template< typename StmtClass > void visitStatement( StmtClass *stmt ); |
---|
151 | |
---|
152 | std::list< Declaration * > declsToAdd; |
---|
153 | std::set< std::string > structsDone; |
---|
154 | unsigned int functionNesting; // current level of nested functions |
---|
155 | }; |
---|
156 | |
---|
157 | class ReturnChecker : public Visitor { |
---|
158 | public: |
---|
159 | /// Checks that return statements return nothing if their return type is void |
---|
160 | /// and return something if the return type is non-void. |
---|
161 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit ); |
---|
162 | private: |
---|
163 | virtual void visit( FunctionDecl * functionDecl ); |
---|
164 | |
---|
165 | virtual void visit( ReturnStmt * returnStmt ); |
---|
166 | |
---|
167 | std::list< DeclarationWithType * > returnVals; |
---|
168 | }; |
---|
169 | |
---|
170 | class EliminateTypedef : public Mutator { |
---|
171 | public: |
---|
172 | EliminateTypedef() : scopeLevel( 0 ) {} |
---|
173 | /// Replaces typedefs by forward declarations |
---|
174 | static void eliminateTypedef( std::list< Declaration * > &translationUnit ); |
---|
175 | private: |
---|
176 | virtual Declaration *mutate( TypedefDecl *typeDecl ); |
---|
177 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); |
---|
178 | virtual DeclarationWithType *mutate( FunctionDecl *funcDecl ); |
---|
179 | virtual DeclarationWithType *mutate( ObjectDecl *objDecl ); |
---|
180 | virtual CompoundStmt *mutate( CompoundStmt *compoundStmt ); |
---|
181 | virtual Type *mutate( TypeInstType *aggregateUseType ); |
---|
182 | virtual Expression *mutate( CastExpr *castExpr ); |
---|
183 | |
---|
184 | virtual Declaration *mutate( StructDecl * structDecl ); |
---|
185 | virtual Declaration *mutate( UnionDecl * unionDecl ); |
---|
186 | virtual Declaration *mutate( EnumDecl * enumDecl ); |
---|
187 | virtual Declaration *mutate( TraitDecl * contextDecl ); |
---|
188 | |
---|
189 | template<typename AggDecl> |
---|
190 | AggDecl *handleAggregate( AggDecl * aggDecl ); |
---|
191 | |
---|
192 | template<typename AggDecl> |
---|
193 | void addImplicitTypedef( AggDecl * aggDecl ); |
---|
194 | |
---|
195 | typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap; |
---|
196 | TypedefMap typedefNames; |
---|
197 | int scopeLevel; |
---|
198 | }; |
---|
199 | |
---|
200 | class CompoundLiteral : public GenPoly::DeclMutator { |
---|
201 | DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass; |
---|
202 | |
---|
203 | virtual DeclarationWithType * mutate( ObjectDecl *objectDecl ); |
---|
204 | virtual Expression *mutate( CompoundLiteralExpr *compLitExpr ); |
---|
205 | }; |
---|
206 | |
---|
207 | void validate( std::list< Declaration * > &translationUnit, bool doDebug ) { |
---|
208 | Pass1 pass1; |
---|
209 | Pass2 pass2( doDebug, 0 ); |
---|
210 | Pass3 pass3( 0 ); |
---|
211 | CompoundLiteral compoundliteral; |
---|
212 | |
---|
213 | EliminateTypedef::eliminateTypedef( translationUnit ); |
---|
214 | HoistStruct::hoistStruct( translationUnit ); |
---|
215 | acceptAll( translationUnit, pass1 ); |
---|
216 | acceptAll( translationUnit, pass2 ); |
---|
217 | ReturnChecker::checkFunctionReturns( translationUnit ); |
---|
218 | mutateAll( translationUnit, compoundliteral ); |
---|
219 | AutogenerateRoutines::autogenerateRoutines( translationUnit ); |
---|
220 | acceptAll( translationUnit, pass3 ); |
---|
221 | } |
---|
222 | |
---|
223 | void validateType( Type *type, const Indexer *indexer ) { |
---|
224 | Pass1 pass1; |
---|
225 | Pass2 pass2( false, indexer ); |
---|
226 | Pass3 pass3( indexer ); |
---|
227 | type->accept( pass1 ); |
---|
228 | type->accept( pass2 ); |
---|
229 | type->accept( pass3 ); |
---|
230 | } |
---|
231 | |
---|
232 | template< typename Visitor > |
---|
233 | void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor, bool addBefore ) { |
---|
234 | std::list< Declaration * >::iterator i = translationUnit.begin(); |
---|
235 | while ( i != translationUnit.end() ) { |
---|
236 | (*i)->accept( visitor ); |
---|
237 | std::list< Declaration * >::iterator next = i; |
---|
238 | next++; |
---|
239 | if ( ! visitor.get_declsToAdd().empty() ) { |
---|
240 | translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() ); |
---|
241 | } // if |
---|
242 | i = next; |
---|
243 | } // while |
---|
244 | } |
---|
245 | |
---|
246 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { |
---|
247 | HoistStruct hoister; |
---|
248 | acceptAndAdd( translationUnit, hoister, true ); |
---|
249 | } |
---|
250 | |
---|
251 | HoistStruct::HoistStruct() : inStruct( false ) { |
---|
252 | } |
---|
253 | |
---|
254 | void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) { |
---|
255 | std::list< Declaration * >::iterator i = declList.begin(); |
---|
256 | while ( i != declList.end() ) { |
---|
257 | std::list< Declaration * >::iterator next = i; |
---|
258 | ++next; |
---|
259 | if ( pred( *i ) ) { |
---|
260 | if ( doDelete ) { |
---|
261 | delete *i; |
---|
262 | } // if |
---|
263 | declList.erase( i ); |
---|
264 | } // if |
---|
265 | i = next; |
---|
266 | } // while |
---|
267 | } |
---|
268 | |
---|
269 | bool isStructOrUnion( Declaration *decl ) { |
---|
270 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ); |
---|
271 | } |
---|
272 | |
---|
273 | template< typename AggDecl > |
---|
274 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) { |
---|
275 | if ( inStruct ) { |
---|
276 | // Add elements in stack order corresponding to nesting structure. |
---|
277 | declsToAdd.push_front( aggregateDecl ); |
---|
278 | Visitor::visit( aggregateDecl ); |
---|
279 | } else { |
---|
280 | inStruct = true; |
---|
281 | Visitor::visit( aggregateDecl ); |
---|
282 | inStruct = false; |
---|
283 | } // if |
---|
284 | // Always remove the hoisted aggregate from the inner structure. |
---|
285 | filter( aggregateDecl->get_members(), isStructOrUnion, false ); |
---|
286 | } |
---|
287 | |
---|
288 | void HoistStruct::visit( StructDecl *aggregateDecl ) { |
---|
289 | handleAggregate( aggregateDecl ); |
---|
290 | } |
---|
291 | |
---|
292 | void HoistStruct::visit( UnionDecl *aggregateDecl ) { |
---|
293 | handleAggregate( aggregateDecl ); |
---|
294 | } |
---|
295 | |
---|
296 | void HoistStruct::visit( CompoundStmt *compoundStmt ) { |
---|
297 | addVisit( compoundStmt, *this ); |
---|
298 | } |
---|
299 | |
---|
300 | void HoistStruct::visit( SwitchStmt *switchStmt ) { |
---|
301 | addVisit( switchStmt, *this ); |
---|
302 | } |
---|
303 | |
---|
304 | void HoistStruct::visit( ChooseStmt *switchStmt ) { |
---|
305 | addVisit( switchStmt, *this ); |
---|
306 | } |
---|
307 | |
---|
308 | // void HoistStruct::visit( CaseStmt *caseStmt ) { |
---|
309 | // addVisit( caseStmt, *this ); |
---|
310 | // } |
---|
311 | |
---|
312 | void Pass1::visit( EnumDecl *enumDecl ) { |
---|
313 | // Set the type of each member of the enumeration to be EnumConstant |
---|
314 | |
---|
315 | for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) { |
---|
316 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i ); |
---|
317 | assert( obj ); |
---|
318 | // obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) ); |
---|
319 | BasicType * enumType = new BasicType( Type::Qualifiers(), BasicType::SignedInt ); |
---|
320 | obj->set_type( enumType ) ; |
---|
321 | } // for |
---|
322 | Parent::visit( enumDecl ); |
---|
323 | } |
---|
324 | |
---|
325 | namespace { |
---|
326 | template< typename DWTIterator > |
---|
327 | void fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) { |
---|
328 | // the only case in which "void" is valid is where it is the only one in the list; then it should be removed |
---|
329 | // entirely other fix ups are handled by the FixFunction class |
---|
330 | if ( begin == end ) return; |
---|
331 | FixFunction fixer; |
---|
332 | DWTIterator i = begin; |
---|
333 | *i = (*i )->acceptMutator( fixer ); |
---|
334 | if ( fixer.get_isVoid() ) { |
---|
335 | DWTIterator j = i; |
---|
336 | ++i; |
---|
337 | func->get_parameters().erase( j ); |
---|
338 | if ( i != end ) { |
---|
339 | throw SemanticError( "invalid type void in function type ", func ); |
---|
340 | } // if |
---|
341 | } else { |
---|
342 | ++i; |
---|
343 | for ( ; i != end; ++i ) { |
---|
344 | FixFunction fixer; |
---|
345 | *i = (*i )->acceptMutator( fixer ); |
---|
346 | if ( fixer.get_isVoid() ) { |
---|
347 | throw SemanticError( "invalid type void in function type ", func ); |
---|
348 | } // if |
---|
349 | } // for |
---|
350 | } // if |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | void Pass1::visit( FunctionType *func ) { |
---|
355 | // Fix up parameters and return types |
---|
356 | fixFunctionList( func->get_parameters().begin(), func->get_parameters().end(), func ); |
---|
357 | fixFunctionList( func->get_returnVals().begin(), func->get_returnVals().end(), func ); |
---|
358 | Visitor::visit( func ); |
---|
359 | } |
---|
360 | |
---|
361 | Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) { |
---|
362 | if ( other_indexer ) { |
---|
363 | indexer = other_indexer; |
---|
364 | } else { |
---|
365 | indexer = this; |
---|
366 | } // if |
---|
367 | } |
---|
368 | |
---|
369 | void Pass2::visit( StructInstType *structInst ) { |
---|
370 | Parent::visit( structInst ); |
---|
371 | StructDecl *st = indexer->lookupStruct( structInst->get_name() ); |
---|
372 | // it's not a semantic error if the struct is not found, just an implicit forward declaration |
---|
373 | if ( st ) { |
---|
374 | //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); |
---|
375 | structInst->set_baseStruct( st ); |
---|
376 | } // if |
---|
377 | if ( ! st || st->get_members().empty() ) { |
---|
378 | // use of forward declaration |
---|
379 | forwardStructs[ structInst->get_name() ].push_back( structInst ); |
---|
380 | } // if |
---|
381 | } |
---|
382 | |
---|
383 | void Pass2::visit( UnionInstType *unionInst ) { |
---|
384 | Parent::visit( unionInst ); |
---|
385 | UnionDecl *un = indexer->lookupUnion( unionInst->get_name() ); |
---|
386 | // it's not a semantic error if the union is not found, just an implicit forward declaration |
---|
387 | if ( un ) { |
---|
388 | unionInst->set_baseUnion( un ); |
---|
389 | } // if |
---|
390 | if ( ! un || un->get_members().empty() ) { |
---|
391 | // use of forward declaration |
---|
392 | forwardUnions[ unionInst->get_name() ].push_back( unionInst ); |
---|
393 | } // if |
---|
394 | } |
---|
395 | |
---|
396 | void Pass2::visit( TraitInstType *contextInst ) { |
---|
397 | Parent::visit( contextInst ); |
---|
398 | TraitDecl *ctx = indexer->lookupTrait( contextInst->get_name() ); |
---|
399 | if ( ! ctx ) { |
---|
400 | throw SemanticError( "use of undeclared context " + contextInst->get_name() ); |
---|
401 | } // if |
---|
402 | for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) { |
---|
403 | for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { |
---|
404 | if ( TraitInstType *otherCtx = dynamic_cast< TraitInstType * >(*assert ) ) { |
---|
405 | cloneAll( otherCtx->get_members(), contextInst->get_members() ); |
---|
406 | } else { |
---|
407 | contextInst->get_members().push_back( (*assert )->clone() ); |
---|
408 | } // if |
---|
409 | } // for |
---|
410 | } // for |
---|
411 | |
---|
412 | if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) { |
---|
413 | throw SemanticError( "incorrect number of context parameters: ", contextInst ); |
---|
414 | } // if |
---|
415 | |
---|
416 | applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), ctx->get_members().begin(), ctx->get_members().end(), back_inserter( contextInst->get_members() ) ); |
---|
417 | } |
---|
418 | |
---|
419 | void Pass2::visit( StructDecl *structDecl ) { |
---|
420 | if ( ! structDecl->get_members().empty() ) { |
---|
421 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); |
---|
422 | if ( fwds != forwardStructs.end() ) { |
---|
423 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
424 | (*inst )->set_baseStruct( structDecl ); |
---|
425 | } // for |
---|
426 | forwardStructs.erase( fwds ); |
---|
427 | } // if |
---|
428 | } // if |
---|
429 | Indexer::visit( structDecl ); |
---|
430 | } |
---|
431 | |
---|
432 | void Pass2::visit( UnionDecl *unionDecl ) { |
---|
433 | if ( ! unionDecl->get_members().empty() ) { |
---|
434 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); |
---|
435 | if ( fwds != forwardUnions.end() ) { |
---|
436 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
437 | (*inst )->set_baseUnion( unionDecl ); |
---|
438 | } // for |
---|
439 | forwardUnions.erase( fwds ); |
---|
440 | } // if |
---|
441 | } // if |
---|
442 | Indexer::visit( unionDecl ); |
---|
443 | } |
---|
444 | |
---|
445 | void Pass2::visit( TypeInstType *typeInst ) { |
---|
446 | if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) { |
---|
447 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { |
---|
448 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); |
---|
449 | } // if |
---|
450 | } // if |
---|
451 | } |
---|
452 | |
---|
453 | Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) { |
---|
454 | if ( other_indexer ) { |
---|
455 | indexer = other_indexer; |
---|
456 | } else { |
---|
457 | indexer = this; |
---|
458 | } // if |
---|
459 | } |
---|
460 | |
---|
461 | /// Fix up assertions |
---|
462 | void forallFixer( Type *func ) { |
---|
463 | for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) { |
---|
464 | std::list< DeclarationWithType * > toBeDone, nextRound; |
---|
465 | toBeDone.splice( toBeDone.end(), (*type )->get_assertions() ); |
---|
466 | while ( ! toBeDone.empty() ) { |
---|
467 | for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) { |
---|
468 | if ( TraitInstType *ctx = dynamic_cast< TraitInstType * >( (*assertion )->get_type() ) ) { |
---|
469 | for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) { |
---|
470 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i ); |
---|
471 | assert( dwt ); |
---|
472 | nextRound.push_back( dwt->clone() ); |
---|
473 | } |
---|
474 | delete ctx; |
---|
475 | } else { |
---|
476 | FixFunction fixer; |
---|
477 | *assertion = (*assertion )->acceptMutator( fixer ); |
---|
478 | if ( fixer.get_isVoid() ) { |
---|
479 | throw SemanticError( "invalid type void in assertion of function ", func ); |
---|
480 | } |
---|
481 | (*type )->get_assertions().push_back( *assertion ); |
---|
482 | } // if |
---|
483 | } // for |
---|
484 | toBeDone.clear(); |
---|
485 | toBeDone.splice( toBeDone.end(), nextRound ); |
---|
486 | } // while |
---|
487 | } // for |
---|
488 | } |
---|
489 | |
---|
490 | void Pass3::visit( ObjectDecl *object ) { |
---|
491 | forallFixer( object->get_type() ); |
---|
492 | if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) { |
---|
493 | forallFixer( pointer->get_base() ); |
---|
494 | } // if |
---|
495 | Parent::visit( object ); |
---|
496 | object->fixUniqueId(); |
---|
497 | } |
---|
498 | |
---|
499 | void Pass3::visit( FunctionDecl *func ) { |
---|
500 | forallFixer( func->get_type() ); |
---|
501 | Parent::visit( func ); |
---|
502 | func->fixUniqueId(); |
---|
503 | } |
---|
504 | |
---|
505 | static const std::list< std::string > noLabels; |
---|
506 | |
---|
507 | void AutogenerateRoutines::autogenerateRoutines( std::list< Declaration * > &translationUnit ) { |
---|
508 | AutogenerateRoutines visitor; |
---|
509 | acceptAndAdd( translationUnit, visitor, false ); |
---|
510 | } |
---|
511 | |
---|
512 | template< typename OutputIterator > |
---|
513 | void makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out ) { |
---|
514 | ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member ); |
---|
515 | // unnamed bit fields are not copied as they cannot be accessed |
---|
516 | if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return; |
---|
517 | |
---|
518 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
519 | |
---|
520 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
521 | derefExpr->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
522 | |
---|
523 | // do something special for unnamed members |
---|
524 | Expression *dstselect = new AddressExpr( new MemberExpr( member, derefExpr ) ); |
---|
525 | assignExpr->get_args().push_back( dstselect ); |
---|
526 | |
---|
527 | Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) ); |
---|
528 | assignExpr->get_args().push_back( srcselect ); |
---|
529 | |
---|
530 | *out++ = new ExprStmt( noLabels, assignExpr ); |
---|
531 | } |
---|
532 | |
---|
533 | template< typename OutputIterator > |
---|
534 | void makeArrayAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, ArrayType *array, OutputIterator out ) { |
---|
535 | static UniqueName indexName( "_index" ); |
---|
536 | |
---|
537 | // for a flexible array member nothing is done -- user must define own assignment |
---|
538 | if ( ! array->get_dimension() ) return; |
---|
539 | |
---|
540 | ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 ); |
---|
541 | *out++ = new DeclStmt( noLabels, index ); |
---|
542 | |
---|
543 | UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
544 | init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) ); |
---|
545 | init->get_args().push_back( new NameExpr( "0" ) ); |
---|
546 | Statement *initStmt = new ExprStmt( noLabels, init ); |
---|
547 | std::list<Statement *> initList; |
---|
548 | initList.push_back( initStmt ); |
---|
549 | |
---|
550 | UntypedExpr *cond = new UntypedExpr( new NameExpr( "?<?" ) ); |
---|
551 | cond->get_args().push_back( new VariableExpr( index ) ); |
---|
552 | cond->get_args().push_back( array->get_dimension()->clone() ); |
---|
553 | |
---|
554 | UntypedExpr *inc = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
555 | inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) ); |
---|
556 | |
---|
557 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
558 | |
---|
559 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
560 | derefExpr->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
561 | |
---|
562 | Expression *dstselect = new MemberExpr( member, derefExpr ); |
---|
563 | UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) ); |
---|
564 | dstIndex->get_args().push_back( dstselect ); |
---|
565 | dstIndex->get_args().push_back( new VariableExpr( index ) ); |
---|
566 | assignExpr->get_args().push_back( dstIndex ); |
---|
567 | |
---|
568 | Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) ); |
---|
569 | UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) ); |
---|
570 | srcIndex->get_args().push_back( srcselect ); |
---|
571 | srcIndex->get_args().push_back( new VariableExpr( index ) ); |
---|
572 | assignExpr->get_args().push_back( srcIndex ); |
---|
573 | |
---|
574 | *out++ = new ForStmt( noLabels, initList, cond, inc, new ExprStmt( noLabels, assignExpr ) ); |
---|
575 | } |
---|
576 | |
---|
577 | template< typename OutputIterator > |
---|
578 | void makeUnionFieldsAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, UnionInstType *unionType, OutputIterator out ) { |
---|
579 | UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) ); |
---|
580 | copy->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
581 | copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) ); |
---|
582 | copy->get_args().push_back( new SizeofExpr( unionType ) ); |
---|
583 | |
---|
584 | *out++ = new ExprStmt( noLabels, copy ); |
---|
585 | } |
---|
586 | |
---|
587 | //E ?=?(E volatile*, int), |
---|
588 | // ?=?(E _Atomic volatile*, int); |
---|
589 | void makeEnumAssignment( EnumDecl *enumDecl, EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) { |
---|
590 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
591 | |
---|
592 | ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 ); |
---|
593 | assignType->get_returnVals().push_back( returnVal ); |
---|
594 | |
---|
595 | // need two assignment operators with different types |
---|
596 | FunctionType * assignType2 = assignType->clone(); |
---|
597 | |
---|
598 | // E ?=?(E volatile *, E) |
---|
599 | Type *etype = refType->clone(); |
---|
600 | // etype->get_qualifiers() += Type::Qualifiers(false, true, false, false, false, false); |
---|
601 | |
---|
602 | ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), etype ), 0 ); |
---|
603 | assignType->get_parameters().push_back( dstParam ); |
---|
604 | |
---|
605 | ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, etype->clone(), 0 ); |
---|
606 | assignType->get_parameters().push_back( srcParam ); |
---|
607 | |
---|
608 | // E ?=?(E volatile *, int) |
---|
609 | assignType2->get_parameters().push_back( dstParam->clone() ); |
---|
610 | BasicType * paramType = new BasicType(Type::Qualifiers(), BasicType::SignedInt); |
---|
611 | ObjectDecl *srcParam2 = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, paramType, 0 ); |
---|
612 | assignType2->get_parameters().push_back( srcParam2 ); |
---|
613 | |
---|
614 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
615 | // because each unit generates copies of the default routines for each aggregate. |
---|
616 | |
---|
617 | // since there is no definition, these should not be inline |
---|
618 | // make these intrinsic so that the code generator does not make use of them |
---|
619 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType, 0, false, false ); |
---|
620 | assignDecl->fixUniqueId(); |
---|
621 | FunctionDecl *assignDecl2 = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType2, 0, false, false ); |
---|
622 | assignDecl2->fixUniqueId(); |
---|
623 | |
---|
624 | // these should be built in the same way that the prelude |
---|
625 | // functions are, so build a list containing the prototypes |
---|
626 | // and allow MakeLibCfa to autogenerate the bodies. |
---|
627 | std::list< Declaration * > assigns; |
---|
628 | assigns.push_back( assignDecl ); |
---|
629 | assigns.push_back( assignDecl2 ); |
---|
630 | |
---|
631 | LibCfa::makeLibCfa( assigns ); |
---|
632 | |
---|
633 | // need to remove the prototypes, since this may be nested in a routine |
---|
634 | for (int start = 0, end = assigns.size()/2; start < end; start++) { |
---|
635 | delete assigns.front(); |
---|
636 | assigns.pop_front(); |
---|
637 | } // for |
---|
638 | |
---|
639 | declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() ); |
---|
640 | } |
---|
641 | |
---|
642 | /// Clones a reference type, replacing any parameters it may have with a clone of the provided list |
---|
643 | template< typename GenericInstType > |
---|
644 | GenericInstType *cloneWithParams( GenericInstType *refType, const std::list< Expression* >& params ) { |
---|
645 | GenericInstType *clone = refType->clone(); |
---|
646 | clone->get_parameters().clear(); |
---|
647 | cloneAll( params, clone->get_parameters() ); |
---|
648 | return clone; |
---|
649 | } |
---|
650 | |
---|
651 | /// Creates a new type decl that's the same as src, but renamed and with only the ?=? assertion (for complete types only) |
---|
652 | TypeDecl *cloneAndRename( TypeDecl *src, const std::string &name ) { |
---|
653 | TypeDecl *dst = new TypeDecl( name, src->get_storageClass(), 0, src->get_kind() ); |
---|
654 | |
---|
655 | if ( src->get_kind() == TypeDecl::Any ) { |
---|
656 | // just include assignment operator assertion |
---|
657 | TypeInstType *assignParamType = new TypeInstType( Type::Qualifiers(), name, dst ); |
---|
658 | FunctionType *assignFunctionType = new FunctionType( Type::Qualifiers(), false ); |
---|
659 | assignFunctionType->get_returnVals().push_back( |
---|
660 | new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType->clone(), 0 ) ); |
---|
661 | assignFunctionType->get_parameters().push_back( |
---|
662 | new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), assignParamType->clone() ), 0 ) ); |
---|
663 | assignFunctionType->get_parameters().push_back( |
---|
664 | new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType, 0 ) ); |
---|
665 | FunctionDecl *assignAssert = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignFunctionType, 0, false, false ); |
---|
666 | dst->get_assertions().push_back( assignAssert ); |
---|
667 | } |
---|
668 | |
---|
669 | return dst; |
---|
670 | } |
---|
671 | |
---|
672 | Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) { |
---|
673 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
674 | |
---|
675 | // Make function polymorphic in same parameters as generic struct, if applicable |
---|
676 | bool isGeneric = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for union) |
---|
677 | std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters(); |
---|
678 | std::list< Expression* > structParams; // List of matching parameters to put on types |
---|
679 | TypeSubstitution genericSubs; // Substitutions to make to member types of struct |
---|
680 | for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) { |
---|
681 | isGeneric = true; |
---|
682 | TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() ); |
---|
683 | assignType->get_forall().push_back( typeParam ); |
---|
684 | TypeInstType *newParamType = new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ); |
---|
685 | genericSubs.add( (*param)->get_name(), newParamType ); |
---|
686 | structParams.push_back( new TypeExpr( newParamType ) ); |
---|
687 | } |
---|
688 | |
---|
689 | ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 ); |
---|
690 | assignType->get_returnVals().push_back( returnVal ); |
---|
691 | |
---|
692 | ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, structParams ) ), 0 ); |
---|
693 | assignType->get_parameters().push_back( dstParam ); |
---|
694 | |
---|
695 | ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 ); |
---|
696 | assignType->get_parameters().push_back( srcParam ); |
---|
697 | |
---|
698 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
699 | // because each unit generates copies of the default routines for each aggregate. |
---|
700 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false ); |
---|
701 | assignDecl->fixUniqueId(); |
---|
702 | |
---|
703 | for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) { |
---|
704 | if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) { |
---|
705 | // query the type qualifiers of this field and skip assigning it if it is marked const. |
---|
706 | // If it is an array type, we need to strip off the array layers to find its qualifiers. |
---|
707 | Type * type = dwt->get_type(); |
---|
708 | while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { |
---|
709 | type = at->get_base(); |
---|
710 | } |
---|
711 | |
---|
712 | if ( type->get_qualifiers().isConst ) { |
---|
713 | // don't assign const members |
---|
714 | continue; |
---|
715 | } |
---|
716 | |
---|
717 | if ( isGeneric ) { |
---|
718 | // rewrite member type in terms of the type variables on this operator |
---|
719 | DeclarationWithType *fixedMember = dwt->clone(); |
---|
720 | genericSubs.apply( fixedMember ); |
---|
721 | |
---|
722 | // assign to both destination and return value |
---|
723 | if ( ArrayType *array = dynamic_cast< ArrayType * >( fixedMember->get_type() ) ) { |
---|
724 | makeArrayAssignment( srcParam, dstParam, fixedMember, array, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
725 | makeArrayAssignment( srcParam, returnVal, fixedMember, array, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
726 | } else { |
---|
727 | makeScalarAssignment( srcParam, dstParam, fixedMember, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
728 | makeScalarAssignment( srcParam, returnVal, fixedMember, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
729 | } // if |
---|
730 | } else { |
---|
731 | // assign to destination |
---|
732 | if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) { |
---|
733 | makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
734 | } else { |
---|
735 | makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
736 | } // if |
---|
737 | } // if |
---|
738 | } // if |
---|
739 | } // for |
---|
740 | if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) ); |
---|
741 | |
---|
742 | return assignDecl; |
---|
743 | } |
---|
744 | |
---|
745 | Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting ) { |
---|
746 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
747 | |
---|
748 | // Make function polymorphic in same parameters as generic union, if applicable |
---|
749 | bool isGeneric = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for struct) |
---|
750 | std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters(); |
---|
751 | std::list< Expression* > unionParams; // List of matching parameters to put on types |
---|
752 | for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) { |
---|
753 | isGeneric = true; |
---|
754 | TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() ); |
---|
755 | assignType->get_forall().push_back( typeParam ); |
---|
756 | unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) ); |
---|
757 | } |
---|
758 | |
---|
759 | ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 ); |
---|
760 | assignType->get_returnVals().push_back( returnVal ); |
---|
761 | |
---|
762 | ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, unionParams ) ), 0 ); |
---|
763 | assignType->get_parameters().push_back( dstParam ); |
---|
764 | |
---|
765 | ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 ); |
---|
766 | assignType->get_parameters().push_back( srcParam ); |
---|
767 | |
---|
768 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
769 | // because each unit generates copies of the default routines for each aggregate. |
---|
770 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false ); |
---|
771 | assignDecl->fixUniqueId(); |
---|
772 | |
---|
773 | makeUnionFieldsAssignment( srcParam, dstParam, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
774 | if ( isGeneric ) makeUnionFieldsAssignment( srcParam, returnVal, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
775 | |
---|
776 | if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) ); |
---|
777 | |
---|
778 | return assignDecl; |
---|
779 | } |
---|
780 | |
---|
781 | void AutogenerateRoutines::visit( EnumDecl *enumDecl ) { |
---|
782 | if ( ! enumDecl->get_members().empty() ) { |
---|
783 | EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() ); |
---|
784 | // enumInst->set_baseEnum( enumDecl ); |
---|
785 | // declsToAdd.push_back( |
---|
786 | makeEnumAssignment( enumDecl, enumInst, functionNesting, declsToAdd ); |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | void AutogenerateRoutines::visit( StructDecl *structDecl ) { |
---|
791 | if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) { |
---|
792 | StructInstType structInst( Type::Qualifiers(), structDecl->get_name() ); |
---|
793 | structInst.set_baseStruct( structDecl ); |
---|
794 | declsToAdd.push_back( makeStructAssignment( structDecl, &structInst, functionNesting ) ); |
---|
795 | structsDone.insert( structDecl->get_name() ); |
---|
796 | } // if |
---|
797 | } |
---|
798 | |
---|
799 | void AutogenerateRoutines::visit( UnionDecl *unionDecl ) { |
---|
800 | if ( ! unionDecl->get_members().empty() ) { |
---|
801 | UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() ); |
---|
802 | unionInst.set_baseUnion( unionDecl ); |
---|
803 | declsToAdd.push_back( makeUnionAssignment( unionDecl, &unionInst, functionNesting ) ); |
---|
804 | } // if |
---|
805 | } |
---|
806 | |
---|
807 | void AutogenerateRoutines::visit( TypeDecl *typeDecl ) { |
---|
808 | CompoundStmt *stmts = 0; |
---|
809 | TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false ); |
---|
810 | typeInst->set_baseType( typeDecl ); |
---|
811 | ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 ); |
---|
812 | ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 ); |
---|
813 | if ( typeDecl->get_base() ) { |
---|
814 | stmts = new CompoundStmt( std::list< Label >() ); |
---|
815 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
816 | assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) ); |
---|
817 | assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) ); |
---|
818 | stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) ); |
---|
819 | } // if |
---|
820 | FunctionType *type = new FunctionType( Type::Qualifiers(), false ); |
---|
821 | type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) ); |
---|
822 | type->get_parameters().push_back( dst ); |
---|
823 | type->get_parameters().push_back( src ); |
---|
824 | FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false, false ); |
---|
825 | declsToAdd.push_back( func ); |
---|
826 | } |
---|
827 | |
---|
828 | void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) { |
---|
829 | for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) { |
---|
830 | statements.insert( i, new DeclStmt( noLabels, *decl ) ); |
---|
831 | } // for |
---|
832 | declsToAdd.clear(); |
---|
833 | } |
---|
834 | |
---|
835 | void AutogenerateRoutines::visit( FunctionType *) { |
---|
836 | // ensure that we don't add assignment ops for types defined as part of the function |
---|
837 | } |
---|
838 | |
---|
839 | void AutogenerateRoutines::visit( PointerType *) { |
---|
840 | // ensure that we don't add assignment ops for types defined as part of the pointer |
---|
841 | } |
---|
842 | |
---|
843 | void AutogenerateRoutines::visit( TraitDecl *) { |
---|
844 | // ensure that we don't add assignment ops for types defined as part of the context |
---|
845 | } |
---|
846 | |
---|
847 | template< typename StmtClass > |
---|
848 | inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) { |
---|
849 | std::set< std::string > oldStructs = structsDone; |
---|
850 | addVisit( stmt, *this ); |
---|
851 | structsDone = oldStructs; |
---|
852 | } |
---|
853 | |
---|
854 | void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) { |
---|
855 | maybeAccept( functionDecl->get_functionType(), *this ); |
---|
856 | acceptAll( functionDecl->get_oldDecls(), *this ); |
---|
857 | functionNesting += 1; |
---|
858 | maybeAccept( functionDecl->get_statements(), *this ); |
---|
859 | functionNesting -= 1; |
---|
860 | } |
---|
861 | |
---|
862 | void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) { |
---|
863 | visitStatement( compoundStmt ); |
---|
864 | } |
---|
865 | |
---|
866 | void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) { |
---|
867 | visitStatement( switchStmt ); |
---|
868 | } |
---|
869 | |
---|
870 | void AutogenerateRoutines::visit( ChooseStmt *switchStmt ) { |
---|
871 | visitStatement( switchStmt ); |
---|
872 | } |
---|
873 | |
---|
874 | // void AutogenerateRoutines::visit( CaseStmt *caseStmt ) { |
---|
875 | // visitStatement( caseStmt ); |
---|
876 | // } |
---|
877 | |
---|
878 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) { |
---|
879 | ReturnChecker checker; |
---|
880 | acceptAll( translationUnit, checker ); |
---|
881 | } |
---|
882 | |
---|
883 | void ReturnChecker::visit( FunctionDecl * functionDecl ) { |
---|
884 | std::list< DeclarationWithType * > oldReturnVals = returnVals; |
---|
885 | returnVals = functionDecl->get_functionType()->get_returnVals(); |
---|
886 | Visitor::visit( functionDecl ); |
---|
887 | returnVals = oldReturnVals; |
---|
888 | } |
---|
889 | |
---|
890 | void ReturnChecker::visit( ReturnStmt * returnStmt ) { |
---|
891 | if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) { |
---|
892 | throw SemanticError( "Non-void function returns no values: " , returnStmt ); |
---|
893 | } else if ( returnStmt->get_expr() != NULL && returnVals.size() == 0 ) { |
---|
894 | throw SemanticError( "void function returns values: " , returnStmt ); |
---|
895 | } |
---|
896 | } |
---|
897 | |
---|
898 | |
---|
899 | bool isTypedef( Declaration *decl ) { |
---|
900 | return dynamic_cast< TypedefDecl * >( decl ); |
---|
901 | } |
---|
902 | |
---|
903 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { |
---|
904 | EliminateTypedef eliminator; |
---|
905 | mutateAll( translationUnit, eliminator ); |
---|
906 | filter( translationUnit, isTypedef, true ); |
---|
907 | } |
---|
908 | |
---|
909 | Type *EliminateTypedef::mutate( TypeInstType * typeInst ) { |
---|
910 | // instances of typedef types will come here. If it is an instance |
---|
911 | // of a typdef type, link the instance to its actual type. |
---|
912 | TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() ); |
---|
913 | if ( def != typedefNames.end() ) { |
---|
914 | Type *ret = def->second.first->get_base()->clone(); |
---|
915 | ret->get_qualifiers() += typeInst->get_qualifiers(); |
---|
916 | // place instance parameters on the typedef'd type |
---|
917 | if ( ! typeInst->get_parameters().empty() ) { |
---|
918 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret); |
---|
919 | if ( ! rtt ) { |
---|
920 | throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name()); |
---|
921 | } |
---|
922 | rtt->get_parameters().clear(); |
---|
923 | cloneAll( typeInst->get_parameters(), rtt->get_parameters() ); |
---|
924 | mutateAll( rtt->get_parameters(), *this ); // recursively fix typedefs on parameters |
---|
925 | } // if |
---|
926 | delete typeInst; |
---|
927 | return ret; |
---|
928 | } // if |
---|
929 | return typeInst; |
---|
930 | } |
---|
931 | |
---|
932 | Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { |
---|
933 | Declaration *ret = Mutator::mutate( tyDecl ); |
---|
934 | if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { |
---|
935 | // typedef to the same name from the same scope |
---|
936 | // must be from the same type |
---|
937 | |
---|
938 | Type * t1 = tyDecl->get_base(); |
---|
939 | Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base(); |
---|
940 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { |
---|
941 | throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() ); |
---|
942 | } |
---|
943 | } else { |
---|
944 | typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel ); |
---|
945 | } // if |
---|
946 | |
---|
947 | // When a typedef is a forward declaration: |
---|
948 | // typedef struct screen SCREEN; |
---|
949 | // the declaration portion must be retained: |
---|
950 | // struct screen; |
---|
951 | // because the expansion of the typedef is: |
---|
952 | // void rtn( SCREEN *p ) => void rtn( struct screen *p ) |
---|
953 | // hence the type-name "screen" must be defined. |
---|
954 | // Note, qualifiers on the typedef are superfluous for the forward declaration. |
---|
955 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) { |
---|
956 | return new StructDecl( aggDecl->get_name() ); |
---|
957 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) { |
---|
958 | return new UnionDecl( aggDecl->get_name() ); |
---|
959 | } else { |
---|
960 | return ret; |
---|
961 | } // if |
---|
962 | } |
---|
963 | |
---|
964 | TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) { |
---|
965 | TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); |
---|
966 | if ( i != typedefNames.end() ) { |
---|
967 | typedefNames.erase( i ) ; |
---|
968 | } // if |
---|
969 | return typeDecl; |
---|
970 | } |
---|
971 | |
---|
972 | DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) { |
---|
973 | TypedefMap oldNames = typedefNames; |
---|
974 | DeclarationWithType *ret = Mutator::mutate( funcDecl ); |
---|
975 | typedefNames = oldNames; |
---|
976 | return ret; |
---|
977 | } |
---|
978 | |
---|
979 | DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) { |
---|
980 | TypedefMap oldNames = typedefNames; |
---|
981 | DeclarationWithType *ret = Mutator::mutate( objDecl ); |
---|
982 | typedefNames = oldNames; |
---|
983 | // is the type a function? |
---|
984 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) { |
---|
985 | // replace the current object declaration with a function declaration |
---|
986 | return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() ); |
---|
987 | } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) { |
---|
988 | throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl ); |
---|
989 | } // if |
---|
990 | return ret; |
---|
991 | } |
---|
992 | |
---|
993 | Expression *EliminateTypedef::mutate( CastExpr * castExpr ) { |
---|
994 | TypedefMap oldNames = typedefNames; |
---|
995 | Expression *ret = Mutator::mutate( castExpr ); |
---|
996 | typedefNames = oldNames; |
---|
997 | return ret; |
---|
998 | } |
---|
999 | |
---|
1000 | CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) { |
---|
1001 | TypedefMap oldNames = typedefNames; |
---|
1002 | scopeLevel += 1; |
---|
1003 | CompoundStmt *ret = Mutator::mutate( compoundStmt ); |
---|
1004 | scopeLevel -= 1; |
---|
1005 | std::list< Statement * >::iterator i = compoundStmt->get_kids().begin(); |
---|
1006 | while ( i != compoundStmt->get_kids().end() ) { |
---|
1007 | std::list< Statement * >::iterator next = i+1; |
---|
1008 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) { |
---|
1009 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) { |
---|
1010 | delete *i; |
---|
1011 | compoundStmt->get_kids().erase( i ); |
---|
1012 | } // if |
---|
1013 | } // if |
---|
1014 | i = next; |
---|
1015 | } // while |
---|
1016 | typedefNames = oldNames; |
---|
1017 | return ret; |
---|
1018 | } |
---|
1019 | |
---|
1020 | // there may be typedefs nested within aggregates in order for everything to work properly, these should be removed |
---|
1021 | // as well |
---|
1022 | template<typename AggDecl> |
---|
1023 | AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) { |
---|
1024 | std::list<Declaration *>::iterator it = aggDecl->get_members().begin(); |
---|
1025 | for ( ; it != aggDecl->get_members().end(); ) { |
---|
1026 | std::list< Declaration * >::iterator next = it+1; |
---|
1027 | if ( dynamic_cast< TypedefDecl * >( *it ) ) { |
---|
1028 | delete *it; |
---|
1029 | aggDecl->get_members().erase( it ); |
---|
1030 | } // if |
---|
1031 | it = next; |
---|
1032 | } |
---|
1033 | return aggDecl; |
---|
1034 | } |
---|
1035 | |
---|
1036 | template<typename AggDecl> |
---|
1037 | void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { |
---|
1038 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { |
---|
1039 | Type *type; |
---|
1040 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) { |
---|
1041 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ); |
---|
1042 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) { |
---|
1043 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ); |
---|
1044 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) { |
---|
1045 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ); |
---|
1046 | } // if |
---|
1047 | TypedefDecl * tyDecl = new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type ); |
---|
1048 | typedefNames[ aggDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel ); |
---|
1049 | } // if |
---|
1050 | } |
---|
1051 | Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) { |
---|
1052 | addImplicitTypedef( structDecl ); |
---|
1053 | Mutator::mutate( structDecl ); |
---|
1054 | return handleAggregate( structDecl ); |
---|
1055 | } |
---|
1056 | |
---|
1057 | Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) { |
---|
1058 | addImplicitTypedef( unionDecl ); |
---|
1059 | Mutator::mutate( unionDecl ); |
---|
1060 | return handleAggregate( unionDecl ); |
---|
1061 | } |
---|
1062 | |
---|
1063 | Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) { |
---|
1064 | addImplicitTypedef( enumDecl ); |
---|
1065 | Mutator::mutate( enumDecl ); |
---|
1066 | return handleAggregate( enumDecl ); |
---|
1067 | } |
---|
1068 | |
---|
1069 | Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) { |
---|
1070 | Mutator::mutate( contextDecl ); |
---|
1071 | return handleAggregate( contextDecl ); |
---|
1072 | } |
---|
1073 | |
---|
1074 | DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) { |
---|
1075 | storageclass = objectDecl->get_storageClass(); |
---|
1076 | DeclarationWithType * temp = Mutator::mutate( objectDecl ); |
---|
1077 | storageclass = DeclarationNode::NoStorageClass; |
---|
1078 | return temp; |
---|
1079 | } |
---|
1080 | |
---|
1081 | Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) { |
---|
1082 | // transform [storage_class] ... (struct S){ 3, ... }; |
---|
1083 | // into [storage_class] struct S temp = { 3, ... }; |
---|
1084 | static UniqueName indexName( "_compLit" ); |
---|
1085 | |
---|
1086 | ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageclass, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() ); |
---|
1087 | compLitExpr->set_type( 0 ); |
---|
1088 | compLitExpr->set_initializer( 0 ); |
---|
1089 | delete compLitExpr; |
---|
1090 | DeclarationWithType * newtempvar = mutate( tempvar ); |
---|
1091 | addDeclaration( newtempvar ); // add modified temporary to current block |
---|
1092 | return new VariableExpr( newtempvar ); |
---|
1093 | } |
---|
1094 | } // namespace SymTab |
---|
1095 | |
---|
1096 | // Local Variables: // |
---|
1097 | // tab-width: 4 // |
---|
1098 | // mode: c++ // |
---|
1099 | // compile-command: "make install" // |
---|
1100 | // End: // |
---|