source: src/SymTab/ValidateType.cc@ 994028dc

ADT ast-experimental
Last change on this file since 994028dc was 5408b59, checked in by JiadaL <j82liang@…>, 3 years ago

Remove var in QualifiedNameExpr

  • Property mode set to 100644
File size: 17.2 KB
Line 
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// ValidateType.cc -- Validate and normalize types.
8//
9// Author : Andrew Beach
10// Created On : Mon May 16 16:21:00 2022
11// Last Modified By : Andrew Beach
12// Last Modified On : Tue May 17 14:06:00 2022
13// Update Count : 0
14//
15
16#include "ValidateType.h"
17
18#include "CodeGen/OperatorTable.h"
19#include "Common/PassVisitor.h"
20#include "SymTab/FixFunction.h"
21#include "SynTree/Declaration.h"
22#include "SynTree/Type.h"
23
24namespace SymTab {
25
26namespace {
27
28/// Replaces enum types by int, and function or array types in function
29/// parameter and return lists by appropriate pointers.
30struct EnumAndPointerDecay_old {
31 void previsit( EnumDecl * aggregateDecl );
32 void previsit( FunctionType * func );
33};
34
35void EnumAndPointerDecay_old::previsit( EnumDecl * enumDecl ) {
36 // Set the type of each member of the enumeration to be EnumConstant
37 for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) {
38 ObjectDecl * obj = dynamic_cast< ObjectDecl * >( * i );
39 assert( obj );
40 obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) );
41 } // for
42}
43
44template< typename DWTList >
45void fixFunctionList( DWTList & dwts, bool isVarArgs, FunctionType * func ) {
46 auto nvals = dwts.size();
47 bool containsVoid = false;
48 for ( auto & dwt : dwts ) {
49 // fix each DWT and record whether a void was found
50 containsVoid |= fixFunction( dwt );
51 }
52
53 // the only case in which "void" is valid is where it is the only one in the list
54 if ( containsVoid && ( nvals > 1 || isVarArgs ) ) {
55 SemanticError( func, "invalid type void in function type " );
56 }
57
58 // one void is the only thing in the list; remove it.
59 if ( containsVoid ) {
60 delete dwts.front();
61 dwts.clear();
62 }
63}
64
65void EnumAndPointerDecay_old::previsit( FunctionType * func ) {
66 // Fix up parameters and return types
67 fixFunctionList( func->parameters, func->isVarArgs, func );
68 fixFunctionList( func->returnVals, false, func );
69}
70
71/// Associates forward declarations of aggregates with their definitions
72struct LinkReferenceToTypes_old final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes_old>, public WithShortCircuiting {
73 LinkReferenceToTypes_old( const Indexer * indexer );
74
75 void postvisit( TypeInstType * typeInst );
76
77 void postvisit( EnumInstType * enumInst );
78 void postvisit( StructInstType * structInst );
79 void postvisit( UnionInstType * unionInst );
80 void postvisit( TraitInstType * traitInst );
81 void previsit( QualifiedType * qualType );
82 void postvisit( QualifiedType * qualType );
83 void postvisit( QualifiedNameExpr * qualExpr );
84
85 void postvisit( EnumDecl * enumDecl );
86 void postvisit( StructDecl * structDecl );
87 void postvisit( UnionDecl * unionDecl );
88 void postvisit( TraitDecl * traitDecl );
89
90 void previsit( StructDecl * structDecl );
91 void previsit( UnionDecl * unionDecl );
92
93 void renameGenericParams( std::list< TypeDecl * > & params );
94
95private:
96 const Indexer * local_indexer;
97
98 typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType;
99 typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
100 typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
101 ForwardEnumsType forwardEnums;
102 ForwardStructsType forwardStructs;
103 ForwardUnionsType forwardUnions;
104 /// true if currently in a generic type body, so that type parameter instances can be renamed appropriately
105 bool inGeneric = false;
106};
107
108
109LinkReferenceToTypes_old::LinkReferenceToTypes_old( const Indexer * other_indexer ) : WithIndexer( false ) {
110 if ( other_indexer ) {
111 local_indexer = other_indexer;
112 } else {
113 local_indexer = &indexer;
114 } // if
115}
116
117void LinkReferenceToTypes_old::postvisit( EnumInstType * enumInst ) {
118 const EnumDecl * st = local_indexer->lookupEnum( enumInst->name );
119 // it's not a semantic error if the enum is not found, just an implicit forward declaration
120 if ( st ) {
121 enumInst->baseEnum = const_cast<EnumDecl *>(st); // Just linking in the node
122 } // if
123 if ( ! st || ! st->body ) {
124 // use of forward declaration
125 forwardEnums[ enumInst->name ].push_back( enumInst );
126 } // if
127}
128
129void LinkReferenceToTypes_old::postvisit( StructInstType * structInst ) {
130 const StructDecl * st = local_indexer->lookupStruct( structInst->name );
131 // it's not a semantic error if the struct is not found, just an implicit forward declaration
132 if ( st ) {
133 structInst->baseStruct = const_cast<StructDecl *>(st); // Just linking in the node
134 } // if
135 if ( ! st || ! st->body ) {
136 // use of forward declaration
137 forwardStructs[ structInst->name ].push_back( structInst );
138 } // if
139}
140
141void LinkReferenceToTypes_old::postvisit( UnionInstType * unionInst ) {
142 const UnionDecl * un = local_indexer->lookupUnion( unionInst->name );
143 // it's not a semantic error if the union is not found, just an implicit forward declaration
144 if ( un ) {
145 unionInst->baseUnion = const_cast<UnionDecl *>(un); // Just linking in the node
146 } // if
147 if ( ! un || ! un->body ) {
148 // use of forward declaration
149 forwardUnions[ unionInst->name ].push_back( unionInst );
150 } // if
151}
152
153void LinkReferenceToTypes_old::previsit( QualifiedType * ) {
154 visit_children = false;
155}
156
157void LinkReferenceToTypes_old::postvisit( QualifiedType * qualType ) {
158 // linking only makes sense for the 'oldest ancestor' of the qualified type
159 qualType->parent->accept( * visitor );
160}
161
162void LinkReferenceToTypes_old::postvisit( QualifiedNameExpr * qualExpr ) {
163 const EnumDecl * st = local_indexer->lookupEnum( qualExpr->type_decl->name );
164 qualExpr->type_decl = const_cast<EnumDecl *>(st);
165}
166
167// expand assertions from trait instance, performing the appropriate type variable substitutions
168template< typename Iterator >
169void expandAssertions( TraitInstType * inst, Iterator out ) {
170 assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) );
171 std::list< DeclarationWithType * > asserts;
172 for ( Declaration * decl : inst->baseTrait->members ) {
173 asserts.push_back( strict_dynamic_cast<DeclarationWithType *>( decl->clone() ) );
174 }
175 // substitute trait decl parameters for instance parameters
176 applySubstitution( inst->baseTrait->parameters.begin(), inst->baseTrait->parameters.end(), inst->parameters.begin(), asserts.begin(), asserts.end(), out );
177}
178
179void LinkReferenceToTypes_old::postvisit( TraitDecl * traitDecl ) {
180 if ( traitDecl->name == "sized" ) {
181 // "sized" is a special trait - flick the sized status on for the type variable
182 assertf( traitDecl->parameters.size() == 1, "Built-in trait 'sized' has incorrect number of parameters: %zd", traitDecl->parameters.size() );
183 TypeDecl * td = traitDecl->parameters.front();
184 td->set_sized( true );
185 }
186
187 // move assertions from type parameters into the body of the trait
188 for ( TypeDecl * td : traitDecl->parameters ) {
189 for ( DeclarationWithType * assert : td->assertions ) {
190 if ( TraitInstType * inst = dynamic_cast< TraitInstType * >( assert->get_type() ) ) {
191 expandAssertions( inst, back_inserter( traitDecl->members ) );
192 } else {
193 traitDecl->members.push_back( assert->clone() );
194 }
195 }
196 deleteAll( td->assertions );
197 td->assertions.clear();
198 } // for
199}
200
201void LinkReferenceToTypes_old::postvisit( TraitInstType * traitInst ) {
202 // handle other traits
203 const TraitDecl * traitDecl = local_indexer->lookupTrait( traitInst->name );
204 if ( ! traitDecl ) {
205 SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name );
206 } // if
207 if ( traitDecl->parameters.size() != traitInst->parameters.size() ) {
208 SemanticError( traitInst, "incorrect number of trait parameters: " );
209 } // if
210 traitInst->baseTrait = const_cast<TraitDecl *>(traitDecl); // Just linking in the node
211
212 // need to carry over the 'sized' status of each decl in the instance
213 for ( auto p : group_iterate( traitDecl->parameters, traitInst->parameters ) ) {
214 TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) );
215 if ( ! expr ) {
216 SemanticError( std::get<1>(p), "Expression parameters for trait instances are currently unsupported: " );
217 }
218 if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) {
219 TypeDecl * formalDecl = std::get<0>(p);
220 TypeDecl * instDecl = inst->baseType;
221 if ( formalDecl->get_sized() ) instDecl->set_sized( true );
222 }
223 }
224 // normalizeAssertions( traitInst->members );
225}
226
227void LinkReferenceToTypes_old::postvisit( EnumDecl * enumDecl ) {
228 // visit enum members first so that the types of self-referencing members are updated properly
229 // Replace the enum base; right now it works only for StructEnum
230 if ( enumDecl->base ) {
231 if ( const TypeInstType * base = dynamic_cast< TypeInstType * >(enumDecl->base) ) {
232 if ( const StructDecl * decl = local_indexer->lookupStruct( base->name ) ) {
233 enumDecl->base = new StructInstType( Type::Qualifiers(), const_cast< StructDecl * >( decl ) ); // Just linking in the node
234 }
235 } else if ( const PointerType * ptr = dynamic_cast< PointerType * >(enumDecl->base) ) {
236 if ( const TypeInstType * ptrBase = dynamic_cast< TypeInstType * >( ptr->base ) ) {
237 if ( const StructDecl * decl = local_indexer->lookupStruct( ptrBase->name ) ) {
238 enumDecl->base = new PointerType( Type::Qualifiers(),
239 new StructInstType( Type::Qualifiers(), const_cast< StructDecl * >( decl ) ) );
240 }
241 }
242 }
243 }
244
245 if ( enumDecl->body ) {
246 ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->name );
247 if ( fwds != forwardEnums.end() ) {
248 for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
249 (* inst)->baseEnum = enumDecl;
250 } // for
251 forwardEnums.erase( fwds );
252 } // if
253 } // if
254}
255
256void LinkReferenceToTypes_old::renameGenericParams( std::list< TypeDecl * > & params ) {
257 // rename generic type parameters uniquely so that they do not conflict with user-defined function forall parameters, e.g.
258 // forall(otype T)
259 // struct Box {
260 // T x;
261 // };
262 // forall(otype T)
263 // void f(Box(T) b) {
264 // ...
265 // }
266 // The T in Box and the T in f are different, so internally the naming must reflect that.
267 GuardValue( inGeneric );
268 inGeneric = ! params.empty();
269 for ( TypeDecl * td : params ) {
270 td->name = "__" + td->name + "_generic_";
271 }
272}
273
274void LinkReferenceToTypes_old::previsit( StructDecl * structDecl ) {
275 renameGenericParams( structDecl->parameters );
276}
277
278void LinkReferenceToTypes_old::previsit( UnionDecl * unionDecl ) {
279 renameGenericParams( unionDecl->parameters );
280}
281
282void LinkReferenceToTypes_old::postvisit( StructDecl * structDecl ) {
283 // visit struct members first so that the types of self-referencing members are updated properly
284 // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their def>
285 if ( structDecl->body ) {
286 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->name );
287 if ( fwds != forwardStructs.end() ) {
288 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
289 (* inst)->baseStruct = structDecl;
290 } // for
291 forwardStructs.erase( fwds );
292 } // if
293 } // if
294}
295
296void LinkReferenceToTypes_old::postvisit( UnionDecl * unionDecl ) {
297 if ( unionDecl->body ) {
298 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name );
299 if ( fwds != forwardUnions.end() ) {
300 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
301 (* inst)->baseUnion = unionDecl;
302 } // for
303 forwardUnions.erase( fwds );
304 } // if
305 } // if
306}
307
308void LinkReferenceToTypes_old::postvisit( TypeInstType * typeInst ) {
309 // ensure generic parameter instances are renamed like the base type
310 if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name;
311 if ( const NamedTypeDecl * namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) {
312 if ( const TypeDecl * typeDecl = dynamic_cast< const TypeDecl * >( namedTypeDecl ) ) {
313 typeInst->set_isFtype( typeDecl->kind == TypeDecl::Ftype );
314 } // if
315 } // if
316}
317
318/* // expand assertions from trait instance, performing the appropriate type variable substitutions
319template< typename Iterator >
320void expandAssertions( TraitInstType * inst, Iterator out ) {
321 assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) );
322 std::list< DeclarationWithType * > asserts;
323 for ( Declaration * decl : inst->baseTrait->members ) {
324 asserts.push_back( strict_dynamic_cast<DeclarationWithType *>( decl->clone() ) );
325 }
326 // substitute trait decl parameters for instance parameters
327 applySubstitution( inst->baseTrait->parameters.begin(), inst->baseTrait->parameters.end(), inst->parameters.begin(), asserts.begin(), asserts.end(), out );
328}*/
329
330/// Replace all traits in assertion lists with their assertions.
331void expandTraits( std::list< TypeDecl * > & forall ) {
332 for ( TypeDecl * type : forall ) {
333 std::list< DeclarationWithType * > asserts;
334 asserts.splice( asserts.end(), type->assertions );
335 // expand trait instances into their members
336 for ( DeclarationWithType * assertion : asserts ) {
337 if ( TraitInstType * traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) {
338 // expand trait instance into all of its members
339 expandAssertions( traitInst, back_inserter( type->assertions ) );
340 delete traitInst;
341 } else {
342 // pass other assertions through
343 type->assertions.push_back( assertion );
344 } // if
345 } // for
346 } // for
347}
348
349struct TraitExpander_old final {
350 void previsit( FunctionType * type ) {
351 expandTraits( type->forall );
352 }
353 void previsit( StructDecl * decl ) {
354 expandTraits( decl->parameters );
355 }
356 void previsit( UnionDecl * decl ) {
357 expandTraits( decl->parameters );
358 }
359};
360
361/*struct TraitExpander_old final {
362 void previsit( FunctionType * );
363 void previsit( StructDecl * );
364 void previsit( UnionDecl * );
365};
366
367void TraitExpander_old::previsit( FunctionType * ftype ) {
368 expandTraits( ftype->forall );
369}
370
371void TraitExpander_old::previsit( StructDecl * aggrDecl ) {
372 expandTraits( aggrDecl->parameters );
373}
374
375void TraitExpander_old::previsit( UnionDecl * aggrDecl ) {
376 expandTraits( aggrDecl->parameters );
377}*/
378
379/// Fix each function in the assertion list and check for invalid void type.
380void fixAssertions(
381 std::list< TypeDecl * > & forall, BaseSyntaxNode * node ) {
382 for ( TypeDecl * type : forall ) {
383 for ( DeclarationWithType *& assertion : type->assertions ) {
384 bool isVoid = fixFunction( assertion );
385 if ( isVoid ) {
386 SemanticError( node, "invalid type void in assertion of function " );
387 } // if
388 } // for
389 }
390}
391
392struct AssertionFixer_old final {
393 void previsit( FunctionType * type ) {
394 fixAssertions( type->forall, type );
395 }
396 void previsit( StructDecl * decl ) {
397 fixAssertions( decl->parameters, decl );
398 }
399 void previsit( UnionDecl * decl ) {
400 fixAssertions( decl->parameters, decl );
401 }
402};
403
404/*
405struct AssertionFixer_old final {
406 void previsit( FunctionType * );
407 void previsit( StructDecl * );
408 void previsit( UnionDecl * );
409};
410
411void AssertionFixer_old::previsit( FunctionType * ftype ) {
412 fixAssertions( ftype->forall, ftype );
413}
414
415void AssertionFixer_old::previsit( StructDecl * aggrDecl ) {
416 fixAssertions( aggrDecl->parameters, aggrDecl );
417}
418
419void AssertionFixer_old::previsit( UnionDecl * aggrDecl ) {
420 fixAssertions( aggrDecl->parameters, aggrDecl );
421}*/
422
423struct CheckOperatorTypes_old final {
424 void previsit( ObjectDecl * );
425};
426
427void CheckOperatorTypes_old::previsit( ObjectDecl * object ) {
428 // ensure that operator names only apply to functions or function pointers
429 if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) {
430 SemanticError( object->location, toCString( "operator ", object->name.c_str(), " is not a function or function pointer." ) );
431 }
432}
433
434struct FixUniqueIds_old final {
435 void previsit( DeclarationWithType * decl ) {
436 decl->fixUniqueId();
437 }
438};
439
440//void FixUniqueIds_old::previsit( DeclarationWithType * decl ) {
441// decl->fixUniqueId();
442//}
443
444
445} // namespace
446
447void validateType( Type *type, const Indexer *indexer ) {
448 PassVisitor<EnumAndPointerDecay_old> epc;
449 PassVisitor<LinkReferenceToTypes_old> lrt( indexer );
450 PassVisitor<TraitExpander_old> te;
451 PassVisitor<AssertionFixer_old> af;
452 PassVisitor<CheckOperatorTypes_old> cot;
453 PassVisitor<FixUniqueIds_old> fui;
454 type->accept( epc );
455 type->accept( lrt );
456 type->accept( te );
457 type->accept( af );
458 type->accept( cot );
459 type->accept( fui );
460}
461
462void decayEnumsAndPointers( std::list< Declaration * > & translationUnit ) {
463 PassVisitor<EnumAndPointerDecay_old> epc;
464 acceptAll( translationUnit, epc );
465}
466
467void linkReferenceToTypes( std::list< Declaration * > & translationUnit ) {
468 PassVisitor<LinkReferenceToTypes_old> lrt( nullptr );
469 acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions
470}
471
472void decayForallPointers( std::list< Declaration * > & translationUnit ) {
473 PassVisitor<TraitExpander_old> te;
474 acceptAll( translationUnit, te );
475 PassVisitor<AssertionFixer_old> af;
476 acceptAll( translationUnit, af );
477 PassVisitor<CheckOperatorTypes_old> cot;
478 acceptAll( translationUnit, cot );
479 PassVisitor<FixUniqueIds_old> fui;
480 acceptAll( translationUnit, fui );
481}
482
483
484} // namespace SymTab
485
486// Local Variables: //
487// tab-width: 4 //
488// mode: c++ //
489// compile-command: "make install" //
490// End: //
Note: See TracBrowser for help on using the repository browser.