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 : Tue Jul 12 17:49:21 2016 |
---|
13 | // Update Count : 298 |
---|
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/ScopedMap.h" |
---|
43 | #include "Common/utility.h" |
---|
44 | #include "Common/UniqueName.h" |
---|
45 | #include "Validate.h" |
---|
46 | #include "SynTree/Visitor.h" |
---|
47 | #include "SynTree/Mutator.h" |
---|
48 | #include "SynTree/Type.h" |
---|
49 | #include "SynTree/Expression.h" |
---|
50 | #include "SynTree/Statement.h" |
---|
51 | #include "SynTree/TypeSubstitution.h" |
---|
52 | #include "Indexer.h" |
---|
53 | #include "FixFunction.h" |
---|
54 | // #include "ImplementationType.h" |
---|
55 | #include "GenPoly/DeclMutator.h" |
---|
56 | #include "AddVisit.h" |
---|
57 | #include "MakeLibCfa.h" |
---|
58 | #include "TypeEquality.h" |
---|
59 | #include "Autogen.h" |
---|
60 | #include "ResolvExpr/typeops.h" |
---|
61 | #include <algorithm> |
---|
62 | #include "InitTweak/InitTweak.h" |
---|
63 | |
---|
64 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; } |
---|
65 | |
---|
66 | namespace SymTab { |
---|
67 | class HoistStruct : public Visitor { |
---|
68 | public: |
---|
69 | /// Flattens nested struct types |
---|
70 | static void hoistStruct( std::list< Declaration * > &translationUnit ); |
---|
71 | |
---|
72 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } |
---|
73 | |
---|
74 | virtual void visit( StructDecl *aggregateDecl ); |
---|
75 | virtual void visit( UnionDecl *aggregateDecl ); |
---|
76 | |
---|
77 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
78 | virtual void visit( SwitchStmt *switchStmt ); |
---|
79 | private: |
---|
80 | HoistStruct(); |
---|
81 | |
---|
82 | template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl ); |
---|
83 | |
---|
84 | std::list< Declaration * > declsToAdd; |
---|
85 | bool inStruct; |
---|
86 | }; |
---|
87 | |
---|
88 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers. |
---|
89 | class EnumAndPointerDecayPass : public Visitor { |
---|
90 | typedef Visitor Parent; |
---|
91 | virtual void visit( EnumDecl *aggregateDecl ); |
---|
92 | virtual void visit( FunctionType *func ); |
---|
93 | }; |
---|
94 | |
---|
95 | /// Associates forward declarations of aggregates with their definitions |
---|
96 | class Pass2 final : public Indexer { |
---|
97 | typedef Indexer Parent; |
---|
98 | public: |
---|
99 | Pass2( bool doDebug, const Indexer *indexer ); |
---|
100 | private: |
---|
101 | using Indexer::visit; |
---|
102 | void visit( StructInstType *structInst ) final; |
---|
103 | void visit( UnionInstType *unionInst ) final; |
---|
104 | void visit( TraitInstType *contextInst ) final; |
---|
105 | void visit( StructDecl *structDecl ) final; |
---|
106 | void visit( UnionDecl *unionDecl ) final; |
---|
107 | void visit( TypeInstType *typeInst ) final; |
---|
108 | |
---|
109 | const Indexer *indexer; |
---|
110 | |
---|
111 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType; |
---|
112 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType; |
---|
113 | ForwardStructsType forwardStructs; |
---|
114 | ForwardUnionsType forwardUnions; |
---|
115 | }; |
---|
116 | |
---|
117 | /// Replaces array and function types in forall lists by appropriate pointer type |
---|
118 | class Pass3 : public Indexer { |
---|
119 | typedef Indexer Parent; |
---|
120 | public: |
---|
121 | Pass3( const Indexer *indexer ); |
---|
122 | private: |
---|
123 | virtual void visit( ObjectDecl *object ); |
---|
124 | virtual void visit( FunctionDecl *func ); |
---|
125 | |
---|
126 | const Indexer *indexer; |
---|
127 | }; |
---|
128 | |
---|
129 | class ReturnChecker : public Visitor { |
---|
130 | public: |
---|
131 | /// Checks that return statements return nothing if their return type is void |
---|
132 | /// and return something if the return type is non-void. |
---|
133 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit ); |
---|
134 | private: |
---|
135 | virtual void visit( FunctionDecl * functionDecl ); |
---|
136 | |
---|
137 | virtual void visit( ReturnStmt * returnStmt ); |
---|
138 | |
---|
139 | std::list< DeclarationWithType * > returnVals; |
---|
140 | }; |
---|
141 | |
---|
142 | class EliminateTypedef : public Mutator { |
---|
143 | public: |
---|
144 | EliminateTypedef() : scopeLevel( 0 ) {} |
---|
145 | /// Replaces typedefs by forward declarations |
---|
146 | static void eliminateTypedef( std::list< Declaration * > &translationUnit ); |
---|
147 | private: |
---|
148 | virtual Declaration *mutate( TypedefDecl *typeDecl ); |
---|
149 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); |
---|
150 | virtual DeclarationWithType *mutate( FunctionDecl *funcDecl ); |
---|
151 | virtual DeclarationWithType *mutate( ObjectDecl *objDecl ); |
---|
152 | virtual CompoundStmt *mutate( CompoundStmt *compoundStmt ); |
---|
153 | virtual Type *mutate( TypeInstType *aggregateUseType ); |
---|
154 | virtual Expression *mutate( CastExpr *castExpr ); |
---|
155 | |
---|
156 | virtual Declaration *mutate( StructDecl * structDecl ); |
---|
157 | virtual Declaration *mutate( UnionDecl * unionDecl ); |
---|
158 | virtual Declaration *mutate( EnumDecl * enumDecl ); |
---|
159 | virtual Declaration *mutate( TraitDecl * contextDecl ); |
---|
160 | |
---|
161 | template<typename AggDecl> |
---|
162 | AggDecl *handleAggregate( AggDecl * aggDecl ); |
---|
163 | |
---|
164 | template<typename AggDecl> |
---|
165 | void addImplicitTypedef( AggDecl * aggDecl ); |
---|
166 | |
---|
167 | typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr; |
---|
168 | typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap; |
---|
169 | typedef std::map< std::string, TypeDecl * > TypeDeclMap; |
---|
170 | TypedefMap typedefNames; |
---|
171 | TypeDeclMap typedeclNames; |
---|
172 | int scopeLevel; |
---|
173 | }; |
---|
174 | |
---|
175 | class VerifyCtorDtorAssign : public Visitor { |
---|
176 | public: |
---|
177 | /// ensure that constructors, destructors, and assignment have at least one |
---|
178 | /// parameter, the first of which must be a pointer, and that ctor/dtors have no |
---|
179 | /// return values. |
---|
180 | static void verify( std::list< Declaration * > &translationUnit ); |
---|
181 | |
---|
182 | virtual void visit( FunctionDecl *funcDecl ); |
---|
183 | }; |
---|
184 | |
---|
185 | class CompoundLiteral final : public GenPoly::DeclMutator { |
---|
186 | DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass; |
---|
187 | |
---|
188 | using GenPoly::DeclMutator::mutate; |
---|
189 | DeclarationWithType * mutate( ObjectDecl *objectDecl ) final; |
---|
190 | Expression *mutate( CompoundLiteralExpr *compLitExpr ) final; |
---|
191 | }; |
---|
192 | |
---|
193 | void validate( std::list< Declaration * > &translationUnit, bool doDebug ) { |
---|
194 | EnumAndPointerDecayPass epc; |
---|
195 | Pass2 pass2( doDebug, 0 ); |
---|
196 | Pass3 pass3( 0 ); |
---|
197 | CompoundLiteral compoundliteral; |
---|
198 | |
---|
199 | EliminateTypedef::eliminateTypedef( translationUnit ); |
---|
200 | HoistStruct::hoistStruct( translationUnit ); |
---|
201 | autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecayPass |
---|
202 | acceptAll( translationUnit, epc ); |
---|
203 | acceptAll( translationUnit, pass2 ); |
---|
204 | ReturnChecker::checkFunctionReturns( translationUnit ); |
---|
205 | compoundliteral.mutateDeclarationList( translationUnit ); |
---|
206 | acceptAll( translationUnit, pass3 ); |
---|
207 | VerifyCtorDtorAssign::verify( translationUnit ); |
---|
208 | } |
---|
209 | |
---|
210 | void validateType( Type *type, const Indexer *indexer ) { |
---|
211 | EnumAndPointerDecayPass epc; |
---|
212 | Pass2 pass2( false, indexer ); |
---|
213 | Pass3 pass3( indexer ); |
---|
214 | type->accept( epc ); |
---|
215 | type->accept( pass2 ); |
---|
216 | type->accept( pass3 ); |
---|
217 | } |
---|
218 | |
---|
219 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { |
---|
220 | HoistStruct hoister; |
---|
221 | acceptAndAdd( translationUnit, hoister, true ); |
---|
222 | } |
---|
223 | |
---|
224 | HoistStruct::HoistStruct() : inStruct( false ) { |
---|
225 | } |
---|
226 | |
---|
227 | void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) { |
---|
228 | std::list< Declaration * >::iterator i = declList.begin(); |
---|
229 | while ( i != declList.end() ) { |
---|
230 | std::list< Declaration * >::iterator next = i; |
---|
231 | ++next; |
---|
232 | if ( pred( *i ) ) { |
---|
233 | if ( doDelete ) { |
---|
234 | delete *i; |
---|
235 | } // if |
---|
236 | declList.erase( i ); |
---|
237 | } // if |
---|
238 | i = next; |
---|
239 | } // while |
---|
240 | } |
---|
241 | |
---|
242 | bool isStructOrUnion( Declaration *decl ) { |
---|
243 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ); |
---|
244 | } |
---|
245 | |
---|
246 | template< typename AggDecl > |
---|
247 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) { |
---|
248 | if ( inStruct ) { |
---|
249 | // Add elements in stack order corresponding to nesting structure. |
---|
250 | declsToAdd.push_front( aggregateDecl ); |
---|
251 | Visitor::visit( aggregateDecl ); |
---|
252 | } else { |
---|
253 | inStruct = true; |
---|
254 | Visitor::visit( aggregateDecl ); |
---|
255 | inStruct = false; |
---|
256 | } // if |
---|
257 | // Always remove the hoisted aggregate from the inner structure. |
---|
258 | filter( aggregateDecl->get_members(), isStructOrUnion, false ); |
---|
259 | } |
---|
260 | |
---|
261 | void HoistStruct::visit( StructDecl *aggregateDecl ) { |
---|
262 | handleAggregate( aggregateDecl ); |
---|
263 | } |
---|
264 | |
---|
265 | void HoistStruct::visit( UnionDecl *aggregateDecl ) { |
---|
266 | handleAggregate( aggregateDecl ); |
---|
267 | } |
---|
268 | |
---|
269 | void HoistStruct::visit( CompoundStmt *compoundStmt ) { |
---|
270 | addVisit( compoundStmt, *this ); |
---|
271 | } |
---|
272 | |
---|
273 | void HoistStruct::visit( SwitchStmt *switchStmt ) { |
---|
274 | addVisit( switchStmt, *this ); |
---|
275 | } |
---|
276 | |
---|
277 | void EnumAndPointerDecayPass::visit( EnumDecl *enumDecl ) { |
---|
278 | // Set the type of each member of the enumeration to be EnumConstant |
---|
279 | for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) { |
---|
280 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i ); |
---|
281 | assert( obj ); |
---|
282 | obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) ); |
---|
283 | } // for |
---|
284 | Parent::visit( enumDecl ); |
---|
285 | } |
---|
286 | |
---|
287 | namespace { |
---|
288 | template< typename DWTList > |
---|
289 | void fixFunctionList( DWTList & dwts, FunctionType * func ) { |
---|
290 | // the only case in which "void" is valid is where it is the only one in the list; then it should be removed |
---|
291 | // entirely other fix ups are handled by the FixFunction class |
---|
292 | typedef typename DWTList::iterator DWTIterator; |
---|
293 | DWTIterator begin( dwts.begin() ), end( dwts.end() ); |
---|
294 | if ( begin == end ) return; |
---|
295 | FixFunction fixer; |
---|
296 | DWTIterator i = begin; |
---|
297 | *i = (*i)->acceptMutator( fixer ); |
---|
298 | if ( fixer.get_isVoid() ) { |
---|
299 | DWTIterator j = i; |
---|
300 | ++i; |
---|
301 | delete *j; |
---|
302 | dwts.erase( j ); |
---|
303 | if ( i != end ) { |
---|
304 | throw SemanticError( "invalid type void in function type ", func ); |
---|
305 | } // if |
---|
306 | } else { |
---|
307 | ++i; |
---|
308 | for ( ; i != end; ++i ) { |
---|
309 | FixFunction fixer; |
---|
310 | *i = (*i )->acceptMutator( fixer ); |
---|
311 | if ( fixer.get_isVoid() ) { |
---|
312 | throw SemanticError( "invalid type void in function type ", func ); |
---|
313 | } // if |
---|
314 | } // for |
---|
315 | } // if |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | void EnumAndPointerDecayPass::visit( FunctionType *func ) { |
---|
320 | // Fix up parameters and return types |
---|
321 | fixFunctionList( func->get_parameters(), func ); |
---|
322 | fixFunctionList( func->get_returnVals(), func ); |
---|
323 | Visitor::visit( func ); |
---|
324 | } |
---|
325 | |
---|
326 | Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) { |
---|
327 | if ( other_indexer ) { |
---|
328 | indexer = other_indexer; |
---|
329 | } else { |
---|
330 | indexer = this; |
---|
331 | } // if |
---|
332 | } |
---|
333 | |
---|
334 | void Pass2::visit( StructInstType *structInst ) { |
---|
335 | Parent::visit( structInst ); |
---|
336 | StructDecl *st = indexer->lookupStruct( structInst->get_name() ); |
---|
337 | // it's not a semantic error if the struct is not found, just an implicit forward declaration |
---|
338 | if ( st ) { |
---|
339 | //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); |
---|
340 | structInst->set_baseStruct( st ); |
---|
341 | } // if |
---|
342 | if ( ! st || st->get_members().empty() ) { |
---|
343 | // use of forward declaration |
---|
344 | forwardStructs[ structInst->get_name() ].push_back( structInst ); |
---|
345 | } // if |
---|
346 | } |
---|
347 | |
---|
348 | void Pass2::visit( UnionInstType *unionInst ) { |
---|
349 | Parent::visit( unionInst ); |
---|
350 | UnionDecl *un = indexer->lookupUnion( unionInst->get_name() ); |
---|
351 | // it's not a semantic error if the union is not found, just an implicit forward declaration |
---|
352 | if ( un ) { |
---|
353 | unionInst->set_baseUnion( un ); |
---|
354 | } // if |
---|
355 | if ( ! un || un->get_members().empty() ) { |
---|
356 | // use of forward declaration |
---|
357 | forwardUnions[ unionInst->get_name() ].push_back( unionInst ); |
---|
358 | } // if |
---|
359 | } |
---|
360 | |
---|
361 | void Pass2::visit( TraitInstType *contextInst ) { |
---|
362 | Parent::visit( contextInst ); |
---|
363 | TraitDecl *ctx = indexer->lookupTrait( contextInst->get_name() ); |
---|
364 | if ( ! ctx ) { |
---|
365 | throw SemanticError( "use of undeclared context " + contextInst->get_name() ); |
---|
366 | } // if |
---|
367 | for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) { |
---|
368 | for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { |
---|
369 | if ( TraitInstType *otherCtx = dynamic_cast< TraitInstType * >(*assert ) ) { |
---|
370 | cloneAll( otherCtx->get_members(), contextInst->get_members() ); |
---|
371 | } else { |
---|
372 | contextInst->get_members().push_back( (*assert )->clone() ); |
---|
373 | } // if |
---|
374 | } // for |
---|
375 | } // for |
---|
376 | |
---|
377 | if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) { |
---|
378 | throw SemanticError( "incorrect number of context parameters: ", contextInst ); |
---|
379 | } // if |
---|
380 | |
---|
381 | // need to clone members of the context for ownership purposes |
---|
382 | std::list< Declaration * > members; |
---|
383 | std::transform( ctx->get_members().begin(), ctx->get_members().end(), back_inserter( members ), [](Declaration * dwt) { return dwt->clone(); } ); |
---|
384 | |
---|
385 | applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), members.begin(), members.end(), back_inserter( contextInst->get_members() ) ); |
---|
386 | } |
---|
387 | |
---|
388 | void Pass2::visit( StructDecl *structDecl ) { |
---|
389 | // visit struct members first so that the types of self-referencing members are updated properly |
---|
390 | Parent::visit( structDecl ); |
---|
391 | if ( ! structDecl->get_members().empty() ) { |
---|
392 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); |
---|
393 | if ( fwds != forwardStructs.end() ) { |
---|
394 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
395 | (*inst )->set_baseStruct( structDecl ); |
---|
396 | } // for |
---|
397 | forwardStructs.erase( fwds ); |
---|
398 | } // if |
---|
399 | } // if |
---|
400 | } |
---|
401 | |
---|
402 | void Pass2::visit( UnionDecl *unionDecl ) { |
---|
403 | Parent::visit( unionDecl ); |
---|
404 | if ( ! unionDecl->get_members().empty() ) { |
---|
405 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); |
---|
406 | if ( fwds != forwardUnions.end() ) { |
---|
407 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
408 | (*inst )->set_baseUnion( unionDecl ); |
---|
409 | } // for |
---|
410 | forwardUnions.erase( fwds ); |
---|
411 | } // if |
---|
412 | } // if |
---|
413 | } |
---|
414 | |
---|
415 | void Pass2::visit( TypeInstType *typeInst ) { |
---|
416 | if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) { |
---|
417 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { |
---|
418 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); |
---|
419 | } // if |
---|
420 | } // if |
---|
421 | } |
---|
422 | |
---|
423 | Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) { |
---|
424 | if ( other_indexer ) { |
---|
425 | indexer = other_indexer; |
---|
426 | } else { |
---|
427 | indexer = this; |
---|
428 | } // if |
---|
429 | } |
---|
430 | |
---|
431 | /// Fix up assertions |
---|
432 | void forallFixer( Type *func ) { |
---|
433 | for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) { |
---|
434 | std::list< DeclarationWithType * > toBeDone, nextRound; |
---|
435 | toBeDone.splice( toBeDone.end(), (*type )->get_assertions() ); |
---|
436 | while ( ! toBeDone.empty() ) { |
---|
437 | for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) { |
---|
438 | if ( TraitInstType *ctx = dynamic_cast< TraitInstType * >( (*assertion )->get_type() ) ) { |
---|
439 | for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) { |
---|
440 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i ); |
---|
441 | assert( dwt ); |
---|
442 | nextRound.push_back( dwt->clone() ); |
---|
443 | } |
---|
444 | delete ctx; |
---|
445 | } else { |
---|
446 | FixFunction fixer; |
---|
447 | *assertion = (*assertion )->acceptMutator( fixer ); |
---|
448 | if ( fixer.get_isVoid() ) { |
---|
449 | throw SemanticError( "invalid type void in assertion of function ", func ); |
---|
450 | } |
---|
451 | (*type )->get_assertions().push_back( *assertion ); |
---|
452 | } // if |
---|
453 | } // for |
---|
454 | toBeDone.clear(); |
---|
455 | toBeDone.splice( toBeDone.end(), nextRound ); |
---|
456 | } // while |
---|
457 | } // for |
---|
458 | } |
---|
459 | |
---|
460 | void Pass3::visit( ObjectDecl *object ) { |
---|
461 | forallFixer( object->get_type() ); |
---|
462 | if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) { |
---|
463 | forallFixer( pointer->get_base() ); |
---|
464 | } // if |
---|
465 | Parent::visit( object ); |
---|
466 | object->fixUniqueId(); |
---|
467 | } |
---|
468 | |
---|
469 | void Pass3::visit( FunctionDecl *func ) { |
---|
470 | forallFixer( func->get_type() ); |
---|
471 | Parent::visit( func ); |
---|
472 | func->fixUniqueId(); |
---|
473 | } |
---|
474 | |
---|
475 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) { |
---|
476 | ReturnChecker checker; |
---|
477 | acceptAll( translationUnit, checker ); |
---|
478 | } |
---|
479 | |
---|
480 | void ReturnChecker::visit( FunctionDecl * functionDecl ) { |
---|
481 | std::list< DeclarationWithType * > oldReturnVals = returnVals; |
---|
482 | returnVals = functionDecl->get_functionType()->get_returnVals(); |
---|
483 | Visitor::visit( functionDecl ); |
---|
484 | returnVals = oldReturnVals; |
---|
485 | } |
---|
486 | |
---|
487 | void ReturnChecker::visit( ReturnStmt * returnStmt ) { |
---|
488 | // Previously this also checked for the existence of an expr paired with no return values on |
---|
489 | // the function return type. This is incorrect, since you can have an expression attached to |
---|
490 | // a return statement in a void-returning function in C. The expression is treated as if it |
---|
491 | // were cast to void. |
---|
492 | if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) { |
---|
493 | throw SemanticError( "Non-void function returns no values: " , returnStmt ); |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | |
---|
498 | bool isTypedef( Declaration *decl ) { |
---|
499 | return dynamic_cast< TypedefDecl * >( decl ); |
---|
500 | } |
---|
501 | |
---|
502 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { |
---|
503 | EliminateTypedef eliminator; |
---|
504 | mutateAll( translationUnit, eliminator ); |
---|
505 | if ( eliminator.typedefNames.count( "size_t" ) ) { |
---|
506 | // grab and remember declaration of size_t |
---|
507 | SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone(); |
---|
508 | } else { |
---|
509 | // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong |
---|
510 | // eventually should have a warning for this case. |
---|
511 | SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
512 | } |
---|
513 | filter( translationUnit, isTypedef, true ); |
---|
514 | |
---|
515 | } |
---|
516 | |
---|
517 | Type *EliminateTypedef::mutate( TypeInstType * typeInst ) { |
---|
518 | // instances of typedef types will come here. If it is an instance |
---|
519 | // of a typdef type, link the instance to its actual type. |
---|
520 | TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() ); |
---|
521 | if ( def != typedefNames.end() ) { |
---|
522 | Type *ret = def->second.first->get_base()->clone(); |
---|
523 | ret->get_qualifiers() += typeInst->get_qualifiers(); |
---|
524 | // place instance parameters on the typedef'd type |
---|
525 | if ( ! typeInst->get_parameters().empty() ) { |
---|
526 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret); |
---|
527 | if ( ! rtt ) { |
---|
528 | throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name()); |
---|
529 | } |
---|
530 | rtt->get_parameters().clear(); |
---|
531 | cloneAll( typeInst->get_parameters(), rtt->get_parameters() ); |
---|
532 | mutateAll( rtt->get_parameters(), *this ); // recursively fix typedefs on parameters |
---|
533 | } // if |
---|
534 | delete typeInst; |
---|
535 | return ret; |
---|
536 | } else { |
---|
537 | TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() ); |
---|
538 | assert( base != typedeclNames.end() ); |
---|
539 | typeInst->set_baseType( base->second ); |
---|
540 | } // if |
---|
541 | return typeInst; |
---|
542 | } |
---|
543 | |
---|
544 | Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { |
---|
545 | Declaration *ret = Mutator::mutate( tyDecl ); |
---|
546 | |
---|
547 | if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { |
---|
548 | // typedef to the same name from the same scope |
---|
549 | // must be from the same type |
---|
550 | |
---|
551 | Type * t1 = tyDecl->get_base(); |
---|
552 | Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base(); |
---|
553 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { |
---|
554 | throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() ); |
---|
555 | } |
---|
556 | } else { |
---|
557 | typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel ); |
---|
558 | } // if |
---|
559 | |
---|
560 | // When a typedef is a forward declaration: |
---|
561 | // typedef struct screen SCREEN; |
---|
562 | // the declaration portion must be retained: |
---|
563 | // struct screen; |
---|
564 | // because the expansion of the typedef is: |
---|
565 | // void rtn( SCREEN *p ) => void rtn( struct screen *p ) |
---|
566 | // hence the type-name "screen" must be defined. |
---|
567 | // Note, qualifiers on the typedef are superfluous for the forward declaration. |
---|
568 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) { |
---|
569 | return new StructDecl( aggDecl->get_name() ); |
---|
570 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) { |
---|
571 | return new UnionDecl( aggDecl->get_name() ); |
---|
572 | } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( tyDecl->get_base() ) ) { |
---|
573 | return new EnumDecl( enumDecl->get_name() ); |
---|
574 | } else { |
---|
575 | return ret->clone(); |
---|
576 | } // if |
---|
577 | } |
---|
578 | |
---|
579 | TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) { |
---|
580 | TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); |
---|
581 | if ( i != typedefNames.end() ) { |
---|
582 | typedefNames.erase( i ) ; |
---|
583 | } // if |
---|
584 | |
---|
585 | typedeclNames[ typeDecl->get_name() ] = typeDecl; |
---|
586 | return typeDecl; |
---|
587 | } |
---|
588 | |
---|
589 | DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) { |
---|
590 | typedefNames.beginScope(); |
---|
591 | DeclarationWithType *ret = Mutator::mutate( funcDecl ); |
---|
592 | typedefNames.endScope(); |
---|
593 | return ret; |
---|
594 | } |
---|
595 | |
---|
596 | DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) { |
---|
597 | typedefNames.beginScope(); |
---|
598 | DeclarationWithType *ret = Mutator::mutate( objDecl ); |
---|
599 | typedefNames.endScope(); |
---|
600 | // is the type a function? |
---|
601 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) { |
---|
602 | // replace the current object declaration with a function declaration |
---|
603 | return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() ); |
---|
604 | } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) { |
---|
605 | throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl ); |
---|
606 | } // if |
---|
607 | return ret; |
---|
608 | } |
---|
609 | |
---|
610 | Expression *EliminateTypedef::mutate( CastExpr * castExpr ) { |
---|
611 | typedefNames.beginScope(); |
---|
612 | Expression *ret = Mutator::mutate( castExpr ); |
---|
613 | typedefNames.endScope(); |
---|
614 | return ret; |
---|
615 | } |
---|
616 | |
---|
617 | CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) { |
---|
618 | typedefNames.beginScope(); |
---|
619 | scopeLevel += 1; |
---|
620 | CompoundStmt *ret = Mutator::mutate( compoundStmt ); |
---|
621 | scopeLevel -= 1; |
---|
622 | std::list< Statement * >::iterator i = compoundStmt->get_kids().begin(); |
---|
623 | while ( i != compoundStmt->get_kids().end() ) { |
---|
624 | std::list< Statement * >::iterator next = i+1; |
---|
625 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) { |
---|
626 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) { |
---|
627 | delete *i; |
---|
628 | compoundStmt->get_kids().erase( i ); |
---|
629 | } // if |
---|
630 | } // if |
---|
631 | i = next; |
---|
632 | } // while |
---|
633 | typedefNames.endScope(); |
---|
634 | return ret; |
---|
635 | } |
---|
636 | |
---|
637 | // there may be typedefs nested within aggregates in order for everything to work properly, these should be removed |
---|
638 | // as well |
---|
639 | template<typename AggDecl> |
---|
640 | AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) { |
---|
641 | std::list<Declaration *>::iterator it = aggDecl->get_members().begin(); |
---|
642 | for ( ; it != aggDecl->get_members().end(); ) { |
---|
643 | std::list< Declaration * >::iterator next = it+1; |
---|
644 | if ( dynamic_cast< TypedefDecl * >( *it ) ) { |
---|
645 | delete *it; |
---|
646 | aggDecl->get_members().erase( it ); |
---|
647 | } // if |
---|
648 | it = next; |
---|
649 | } |
---|
650 | return aggDecl; |
---|
651 | } |
---|
652 | |
---|
653 | template<typename AggDecl> |
---|
654 | void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { |
---|
655 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { |
---|
656 | Type *type = nullptr; |
---|
657 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) { |
---|
658 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ); |
---|
659 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) { |
---|
660 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ); |
---|
661 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) { |
---|
662 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ); |
---|
663 | } // if |
---|
664 | TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type ) ); |
---|
665 | typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel ); |
---|
666 | } // if |
---|
667 | } |
---|
668 | |
---|
669 | Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) { |
---|
670 | addImplicitTypedef( structDecl ); |
---|
671 | Mutator::mutate( structDecl ); |
---|
672 | return handleAggregate( structDecl ); |
---|
673 | } |
---|
674 | |
---|
675 | Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) { |
---|
676 | addImplicitTypedef( unionDecl ); |
---|
677 | Mutator::mutate( unionDecl ); |
---|
678 | return handleAggregate( unionDecl ); |
---|
679 | } |
---|
680 | |
---|
681 | Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) { |
---|
682 | addImplicitTypedef( enumDecl ); |
---|
683 | Mutator::mutate( enumDecl ); |
---|
684 | return handleAggregate( enumDecl ); |
---|
685 | } |
---|
686 | |
---|
687 | Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) { |
---|
688 | Mutator::mutate( contextDecl ); |
---|
689 | return handleAggregate( contextDecl ); |
---|
690 | } |
---|
691 | |
---|
692 | void VerifyCtorDtorAssign::verify( std::list< Declaration * > & translationUnit ) { |
---|
693 | VerifyCtorDtorAssign verifier; |
---|
694 | acceptAll( translationUnit, verifier ); |
---|
695 | } |
---|
696 | |
---|
697 | void VerifyCtorDtorAssign::visit( FunctionDecl * funcDecl ) { |
---|
698 | FunctionType * funcType = funcDecl->get_functionType(); |
---|
699 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals(); |
---|
700 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters(); |
---|
701 | |
---|
702 | if ( InitTweak::isCtorDtorAssign( funcDecl->get_name() ) ) { |
---|
703 | if ( params.size() == 0 ) { |
---|
704 | throw SemanticError( "Constructors, destructors, and assignment functions require at least one parameter ", funcDecl ); |
---|
705 | } |
---|
706 | if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) { |
---|
707 | throw SemanticError( "First parameter of a constructor, destructor, or assignment function must be a pointer ", funcDecl ); |
---|
708 | } |
---|
709 | if ( InitTweak::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) { |
---|
710 | throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl ); |
---|
711 | } |
---|
712 | } |
---|
713 | |
---|
714 | Visitor::visit( funcDecl ); |
---|
715 | } |
---|
716 | |
---|
717 | DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) { |
---|
718 | storageclass = objectDecl->get_storageClass(); |
---|
719 | DeclarationWithType * temp = Mutator::mutate( objectDecl ); |
---|
720 | storageclass = DeclarationNode::NoStorageClass; |
---|
721 | return temp; |
---|
722 | } |
---|
723 | |
---|
724 | Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) { |
---|
725 | // transform [storage_class] ... (struct S){ 3, ... }; |
---|
726 | // into [storage_class] struct S temp = { 3, ... }; |
---|
727 | static UniqueName indexName( "_compLit" ); |
---|
728 | |
---|
729 | ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageclass, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() ); |
---|
730 | compLitExpr->set_type( 0 ); |
---|
731 | compLitExpr->set_initializer( 0 ); |
---|
732 | delete compLitExpr; |
---|
733 | DeclarationWithType * newtempvar = mutate( tempvar ); |
---|
734 | addDeclaration( newtempvar ); // add modified temporary to current block |
---|
735 | return new VariableExpr( newtempvar ); |
---|
736 | } |
---|
737 | } // namespace SymTab |
---|
738 | |
---|
739 | // Local Variables: // |
---|
740 | // tab-width: 4 // |
---|
741 | // mode: c++ // |
---|
742 | // compile-command: "make install" // |
---|
743 | // End: // |
---|