source: src/SymTab/ValidateType.cc @ 9939dc3

pthread-emulationqualifiedEnum
Last change on this file since 9939dc3 was 9939dc3, checked in by Andrew Beach <ajbeach@…>, 9 months ago

Reduced the number of object files linked into the demangler. Some of the divisions are rather odd, Lvalue2 and FixMain2, but they should be a better base to work from. Also improved the calling of the impurity detector visitors slightly.

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