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. A function |
---|
26 | // taking no arguments has no argument types. |
---|
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 | // xxx - shouldn't this be declsToAddBefore? |
---|
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 | if ( contextInst->get_name() == "sized" ) { |
---|
364 | // "sized" is a special trait with no members - just flick the sized status on for the type variable |
---|
365 | if ( contextInst->get_parameters().size() != 1 ) { |
---|
366 | throw SemanticError( "incorrect number of context parameters: ", contextInst ); |
---|
367 | } |
---|
368 | TypeExpr * param = safe_dynamic_cast< TypeExpr * > ( contextInst->get_parameters().front() ); |
---|
369 | TypeInstType * inst = safe_dynamic_cast< TypeInstType * > ( param->get_type() ); |
---|
370 | TypeDecl * decl = inst->get_baseType(); |
---|
371 | decl->set_sized( true ); |
---|
372 | // since "sized" is special, the next few steps don't apply |
---|
373 | return; |
---|
374 | } |
---|
375 | TraitDecl *ctx = indexer->lookupTrait( contextInst->get_name() ); |
---|
376 | if ( ! ctx ) { |
---|
377 | throw SemanticError( "use of undeclared context " + contextInst->get_name() ); |
---|
378 | } // if |
---|
379 | for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) { |
---|
380 | for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { |
---|
381 | if ( TraitInstType *otherCtx = dynamic_cast< TraitInstType * >(*assert ) ) { |
---|
382 | cloneAll( otherCtx->get_members(), contextInst->get_members() ); |
---|
383 | } else { |
---|
384 | contextInst->get_members().push_back( (*assert )->clone() ); |
---|
385 | } // if |
---|
386 | } // for |
---|
387 | } // for |
---|
388 | |
---|
389 | if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) { |
---|
390 | throw SemanticError( "incorrect number of context parameters: ", contextInst ); |
---|
391 | } // if |
---|
392 | |
---|
393 | // need to clone members of the context for ownership purposes |
---|
394 | std::list< Declaration * > members; |
---|
395 | std::transform( ctx->get_members().begin(), ctx->get_members().end(), back_inserter( members ), [](Declaration * dwt) { return dwt->clone(); } ); |
---|
396 | |
---|
397 | applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), members.begin(), members.end(), back_inserter( contextInst->get_members() ) ); |
---|
398 | } |
---|
399 | |
---|
400 | void Pass2::visit( StructDecl *structDecl ) { |
---|
401 | // visit struct members first so that the types of self-referencing members are updated properly |
---|
402 | Parent::visit( structDecl ); |
---|
403 | if ( ! structDecl->get_members().empty() ) { |
---|
404 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); |
---|
405 | if ( fwds != forwardStructs.end() ) { |
---|
406 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
407 | (*inst )->set_baseStruct( structDecl ); |
---|
408 | } // for |
---|
409 | forwardStructs.erase( fwds ); |
---|
410 | } // if |
---|
411 | } // if |
---|
412 | } |
---|
413 | |
---|
414 | void Pass2::visit( UnionDecl *unionDecl ) { |
---|
415 | Parent::visit( unionDecl ); |
---|
416 | if ( ! unionDecl->get_members().empty() ) { |
---|
417 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); |
---|
418 | if ( fwds != forwardUnions.end() ) { |
---|
419 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
420 | (*inst )->set_baseUnion( unionDecl ); |
---|
421 | } // for |
---|
422 | forwardUnions.erase( fwds ); |
---|
423 | } // if |
---|
424 | } // if |
---|
425 | } |
---|
426 | |
---|
427 | void Pass2::visit( TypeInstType *typeInst ) { |
---|
428 | if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) { |
---|
429 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { |
---|
430 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); |
---|
431 | } // if |
---|
432 | } // if |
---|
433 | } |
---|
434 | |
---|
435 | Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) { |
---|
436 | if ( other_indexer ) { |
---|
437 | indexer = other_indexer; |
---|
438 | } else { |
---|
439 | indexer = this; |
---|
440 | } // if |
---|
441 | } |
---|
442 | |
---|
443 | /// Fix up assertions |
---|
444 | void forallFixer( Type *func ) { |
---|
445 | for ( Type::ForallList::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) { |
---|
446 | std::list< DeclarationWithType * > toBeDone, nextRound; |
---|
447 | toBeDone.splice( toBeDone.end(), (*type )->get_assertions() ); |
---|
448 | while ( ! toBeDone.empty() ) { |
---|
449 | for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) { |
---|
450 | if ( TraitInstType *ctx = dynamic_cast< TraitInstType * >( (*assertion )->get_type() ) ) { |
---|
451 | for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) { |
---|
452 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i ); |
---|
453 | assert( dwt ); |
---|
454 | nextRound.push_back( dwt->clone() ); |
---|
455 | } |
---|
456 | delete ctx; |
---|
457 | } else { |
---|
458 | FixFunction fixer; |
---|
459 | *assertion = (*assertion )->acceptMutator( fixer ); |
---|
460 | if ( fixer.get_isVoid() ) { |
---|
461 | throw SemanticError( "invalid type void in assertion of function ", func ); |
---|
462 | } |
---|
463 | (*type )->get_assertions().push_back( *assertion ); |
---|
464 | } // if |
---|
465 | } // for |
---|
466 | toBeDone.clear(); |
---|
467 | toBeDone.splice( toBeDone.end(), nextRound ); |
---|
468 | } // while |
---|
469 | } // for |
---|
470 | } |
---|
471 | |
---|
472 | void Pass3::visit( ObjectDecl *object ) { |
---|
473 | forallFixer( object->get_type() ); |
---|
474 | if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) { |
---|
475 | forallFixer( pointer->get_base() ); |
---|
476 | } // if |
---|
477 | Parent::visit( object ); |
---|
478 | object->fixUniqueId(); |
---|
479 | } |
---|
480 | |
---|
481 | void Pass3::visit( FunctionDecl *func ) { |
---|
482 | forallFixer( func->get_type() ); |
---|
483 | Parent::visit( func ); |
---|
484 | func->fixUniqueId(); |
---|
485 | } |
---|
486 | |
---|
487 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) { |
---|
488 | ReturnChecker checker; |
---|
489 | acceptAll( translationUnit, checker ); |
---|
490 | } |
---|
491 | |
---|
492 | void ReturnChecker::visit( FunctionDecl * functionDecl ) { |
---|
493 | std::list< DeclarationWithType * > oldReturnVals = returnVals; |
---|
494 | returnVals = functionDecl->get_functionType()->get_returnVals(); |
---|
495 | Visitor::visit( functionDecl ); |
---|
496 | returnVals = oldReturnVals; |
---|
497 | } |
---|
498 | |
---|
499 | void ReturnChecker::visit( ReturnStmt * returnStmt ) { |
---|
500 | // Previously this also checked for the existence of an expr paired with no return values on |
---|
501 | // the function return type. This is incorrect, since you can have an expression attached to |
---|
502 | // a return statement in a void-returning function in C. The expression is treated as if it |
---|
503 | // were cast to void. |
---|
504 | if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) { |
---|
505 | throw SemanticError( "Non-void function returns no values: " , returnStmt ); |
---|
506 | } |
---|
507 | } |
---|
508 | |
---|
509 | |
---|
510 | bool isTypedef( Declaration *decl ) { |
---|
511 | return dynamic_cast< TypedefDecl * >( decl ); |
---|
512 | } |
---|
513 | |
---|
514 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { |
---|
515 | EliminateTypedef eliminator; |
---|
516 | mutateAll( translationUnit, eliminator ); |
---|
517 | if ( eliminator.typedefNames.count( "size_t" ) ) { |
---|
518 | // grab and remember declaration of size_t |
---|
519 | SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone(); |
---|
520 | } else { |
---|
521 | // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong |
---|
522 | // eventually should have a warning for this case. |
---|
523 | SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
524 | } |
---|
525 | filter( translationUnit, isTypedef, true ); |
---|
526 | |
---|
527 | } |
---|
528 | |
---|
529 | Type *EliminateTypedef::mutate( TypeInstType * typeInst ) { |
---|
530 | // instances of typedef types will come here. If it is an instance |
---|
531 | // of a typdef type, link the instance to its actual type. |
---|
532 | TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() ); |
---|
533 | if ( def != typedefNames.end() ) { |
---|
534 | Type *ret = def->second.first->get_base()->clone(); |
---|
535 | ret->get_qualifiers() += typeInst->get_qualifiers(); |
---|
536 | // place instance parameters on the typedef'd type |
---|
537 | if ( ! typeInst->get_parameters().empty() ) { |
---|
538 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret); |
---|
539 | if ( ! rtt ) { |
---|
540 | throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name()); |
---|
541 | } |
---|
542 | rtt->get_parameters().clear(); |
---|
543 | cloneAll( typeInst->get_parameters(), rtt->get_parameters() ); |
---|
544 | mutateAll( rtt->get_parameters(), *this ); // recursively fix typedefs on parameters |
---|
545 | } // if |
---|
546 | delete typeInst; |
---|
547 | return ret; |
---|
548 | } else { |
---|
549 | TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() ); |
---|
550 | assert( base != typedeclNames.end() ); |
---|
551 | typeInst->set_baseType( base->second ); |
---|
552 | } // if |
---|
553 | return typeInst; |
---|
554 | } |
---|
555 | |
---|
556 | Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { |
---|
557 | Declaration *ret = Mutator::mutate( tyDecl ); |
---|
558 | |
---|
559 | if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { |
---|
560 | // typedef to the same name from the same scope |
---|
561 | // must be from the same type |
---|
562 | |
---|
563 | Type * t1 = tyDecl->get_base(); |
---|
564 | Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base(); |
---|
565 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { |
---|
566 | throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() ); |
---|
567 | } |
---|
568 | } else { |
---|
569 | typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel ); |
---|
570 | } // if |
---|
571 | |
---|
572 | // When a typedef is a forward declaration: |
---|
573 | // typedef struct screen SCREEN; |
---|
574 | // the declaration portion must be retained: |
---|
575 | // struct screen; |
---|
576 | // because the expansion of the typedef is: |
---|
577 | // void rtn( SCREEN *p ) => void rtn( struct screen *p ) |
---|
578 | // hence the type-name "screen" must be defined. |
---|
579 | // Note, qualifiers on the typedef are superfluous for the forward declaration. |
---|
580 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) { |
---|
581 | return new StructDecl( aggDecl->get_name() ); |
---|
582 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) { |
---|
583 | return new UnionDecl( aggDecl->get_name() ); |
---|
584 | } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( tyDecl->get_base() ) ) { |
---|
585 | return new EnumDecl( enumDecl->get_name() ); |
---|
586 | } else { |
---|
587 | return ret->clone(); |
---|
588 | } // if |
---|
589 | } |
---|
590 | |
---|
591 | TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) { |
---|
592 | TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); |
---|
593 | if ( i != typedefNames.end() ) { |
---|
594 | typedefNames.erase( i ) ; |
---|
595 | } // if |
---|
596 | |
---|
597 | typedeclNames[ typeDecl->get_name() ] = typeDecl; |
---|
598 | return Mutator::mutate( typeDecl ); |
---|
599 | } |
---|
600 | |
---|
601 | DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) { |
---|
602 | typedefNames.beginScope(); |
---|
603 | DeclarationWithType *ret = Mutator::mutate( funcDecl ); |
---|
604 | typedefNames.endScope(); |
---|
605 | return ret; |
---|
606 | } |
---|
607 | |
---|
608 | DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) { |
---|
609 | typedefNames.beginScope(); |
---|
610 | DeclarationWithType *ret = Mutator::mutate( objDecl ); |
---|
611 | typedefNames.endScope(); |
---|
612 | // is the type a function? |
---|
613 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) { |
---|
614 | // replace the current object declaration with a function declaration |
---|
615 | return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() ); |
---|
616 | } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) { |
---|
617 | throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl ); |
---|
618 | } // if |
---|
619 | return ret; |
---|
620 | } |
---|
621 | |
---|
622 | Expression *EliminateTypedef::mutate( CastExpr * castExpr ) { |
---|
623 | typedefNames.beginScope(); |
---|
624 | Expression *ret = Mutator::mutate( castExpr ); |
---|
625 | typedefNames.endScope(); |
---|
626 | return ret; |
---|
627 | } |
---|
628 | |
---|
629 | CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) { |
---|
630 | typedefNames.beginScope(); |
---|
631 | scopeLevel += 1; |
---|
632 | CompoundStmt *ret = Mutator::mutate( compoundStmt ); |
---|
633 | scopeLevel -= 1; |
---|
634 | std::list< Statement * >::iterator i = compoundStmt->get_kids().begin(); |
---|
635 | while ( i != compoundStmt->get_kids().end() ) { |
---|
636 | std::list< Statement * >::iterator next = i+1; |
---|
637 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) { |
---|
638 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) { |
---|
639 | delete *i; |
---|
640 | compoundStmt->get_kids().erase( i ); |
---|
641 | } // if |
---|
642 | } // if |
---|
643 | i = next; |
---|
644 | } // while |
---|
645 | typedefNames.endScope(); |
---|
646 | return ret; |
---|
647 | } |
---|
648 | |
---|
649 | // there may be typedefs nested within aggregates in order for everything to work properly, these should be removed |
---|
650 | // as well |
---|
651 | template<typename AggDecl> |
---|
652 | AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) { |
---|
653 | std::list<Declaration *>::iterator it = aggDecl->get_members().begin(); |
---|
654 | for ( ; it != aggDecl->get_members().end(); ) { |
---|
655 | std::list< Declaration * >::iterator next = it+1; |
---|
656 | if ( dynamic_cast< TypedefDecl * >( *it ) ) { |
---|
657 | delete *it; |
---|
658 | aggDecl->get_members().erase( it ); |
---|
659 | } // if |
---|
660 | it = next; |
---|
661 | } |
---|
662 | return aggDecl; |
---|
663 | } |
---|
664 | |
---|
665 | template<typename AggDecl> |
---|
666 | void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { |
---|
667 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { |
---|
668 | Type *type = nullptr; |
---|
669 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) { |
---|
670 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ); |
---|
671 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) { |
---|
672 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ); |
---|
673 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) { |
---|
674 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ); |
---|
675 | } // if |
---|
676 | TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type ) ); |
---|
677 | typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel ); |
---|
678 | } // if |
---|
679 | } |
---|
680 | |
---|
681 | Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) { |
---|
682 | addImplicitTypedef( structDecl ); |
---|
683 | Mutator::mutate( structDecl ); |
---|
684 | return handleAggregate( structDecl ); |
---|
685 | } |
---|
686 | |
---|
687 | Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) { |
---|
688 | addImplicitTypedef( unionDecl ); |
---|
689 | Mutator::mutate( unionDecl ); |
---|
690 | return handleAggregate( unionDecl ); |
---|
691 | } |
---|
692 | |
---|
693 | Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) { |
---|
694 | addImplicitTypedef( enumDecl ); |
---|
695 | Mutator::mutate( enumDecl ); |
---|
696 | return handleAggregate( enumDecl ); |
---|
697 | } |
---|
698 | |
---|
699 | Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) { |
---|
700 | Mutator::mutate( contextDecl ); |
---|
701 | return handleAggregate( contextDecl ); |
---|
702 | } |
---|
703 | |
---|
704 | void VerifyCtorDtorAssign::verify( std::list< Declaration * > & translationUnit ) { |
---|
705 | VerifyCtorDtorAssign verifier; |
---|
706 | acceptAll( translationUnit, verifier ); |
---|
707 | } |
---|
708 | |
---|
709 | void VerifyCtorDtorAssign::visit( FunctionDecl * funcDecl ) { |
---|
710 | FunctionType * funcType = funcDecl->get_functionType(); |
---|
711 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals(); |
---|
712 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters(); |
---|
713 | |
---|
714 | if ( InitTweak::isCtorDtorAssign( funcDecl->get_name() ) ) { |
---|
715 | if ( params.size() == 0 ) { |
---|
716 | throw SemanticError( "Constructors, destructors, and assignment functions require at least one parameter ", funcDecl ); |
---|
717 | } |
---|
718 | if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) { |
---|
719 | throw SemanticError( "First parameter of a constructor, destructor, or assignment function must be a pointer ", funcDecl ); |
---|
720 | } |
---|
721 | if ( InitTweak::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) { |
---|
722 | throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl ); |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | Visitor::visit( funcDecl ); |
---|
727 | } |
---|
728 | |
---|
729 | DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) { |
---|
730 | storageclass = objectDecl->get_storageClass(); |
---|
731 | DeclarationWithType * temp = Mutator::mutate( objectDecl ); |
---|
732 | storageclass = DeclarationNode::NoStorageClass; |
---|
733 | return temp; |
---|
734 | } |
---|
735 | |
---|
736 | Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) { |
---|
737 | // transform [storage_class] ... (struct S){ 3, ... }; |
---|
738 | // into [storage_class] struct S temp = { 3, ... }; |
---|
739 | static UniqueName indexName( "_compLit" ); |
---|
740 | |
---|
741 | ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageclass, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() ); |
---|
742 | compLitExpr->set_type( 0 ); |
---|
743 | compLitExpr->set_initializer( 0 ); |
---|
744 | delete compLitExpr; |
---|
745 | DeclarationWithType * newtempvar = mutate( tempvar ); |
---|
746 | addDeclaration( newtempvar ); // add modified temporary to current block |
---|
747 | return new VariableExpr( newtempvar ); |
---|
748 | } |
---|
749 | } // namespace SymTab |
---|
750 | |
---|
751 | // Local Variables: // |
---|
752 | // tab-width: 4 // |
---|
753 | // mode: c++ // |
---|
754 | // compile-command: "make install" // |
---|
755 | // End: // |
---|