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