Ignore:
Timestamp:
May 18, 2022, 3:59:28 PM (2 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
Children:
76f5e9f
Parents:
622a358 (diff), e6cf857f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    r622a358 r288927f  
    1010// Created On       : Sun May 17 21:50:04 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Apr 29  9:45:00 2022
    13 // Update Count     : 365
     12// Last Modified On : Tue May 17 14:36:00 2022
     13// Update Count     : 366
    1414//
    1515
     
    7474#include "ResolvExpr/ResolveTypeof.h"  // for resolveTypeof
    7575#include "SymTab/Autogen.h"            // for SizeType
     76#include "SymTab/ValidateType.h"       // for decayEnumsAndPointers, decayFo...
    7677#include "SynTree/LinkageSpec.h"       // for C
    7778#include "SynTree/Attribute.h"         // for noAttributes, Attribute
     
    134135        };
    135136
    136         /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
    137         struct EnumAndPointerDecay_old {
    138                 void previsit( EnumDecl * aggregateDecl );
    139                 void previsit( FunctionType * func );
    140         };
    141 
    142         /// Associates forward declarations of aggregates with their definitions
    143         struct LinkReferenceToTypes_old final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes_old>, public WithShortCircuiting {
    144                 LinkReferenceToTypes_old( const Indexer * indexer );
    145 
    146                 void postvisit( TypeInstType * typeInst );
    147 
    148                 void postvisit( EnumInstType * enumInst );
    149                 void postvisit( StructInstType * structInst );
    150                 void postvisit( UnionInstType * unionInst );
    151                 void postvisit( TraitInstType * traitInst );
    152                 void previsit( QualifiedType * qualType );
    153                 void postvisit( QualifiedType * qualType );
    154 
    155                 void postvisit( EnumDecl * enumDecl );
    156                 void postvisit( StructDecl * structDecl );
    157                 void postvisit( UnionDecl * unionDecl );
    158                 void postvisit( TraitDecl * traitDecl );
    159 
    160                 void previsit( StructDecl * structDecl );
    161                 void previsit( UnionDecl * unionDecl );
    162 
    163                 void renameGenericParams( std::list< TypeDecl * > & params );
    164 
    165           private:
    166                 const Indexer * local_indexer;
    167 
    168                 typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType;
    169                 typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
    170                 typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
    171                 ForwardEnumsType forwardEnums;
    172                 ForwardStructsType forwardStructs;
    173                 ForwardUnionsType forwardUnions;
    174                 /// true if currently in a generic type body, so that type parameter instances can be renamed appropriately
    175                 bool inGeneric = false;
    176         };
    177 
    178137        /// Does early resolution on the expressions that give enumeration constants their values
    179138        struct ResolveEnumInitializers final : public WithIndexer, public WithGuards, public WithVisitorRef<ResolveEnumInitializers>, public WithShortCircuiting {
     
    193152                void previsit( StructDecl * aggrDecl );
    194153                void previsit( UnionDecl * aggrDecl );
    195         };
    196 
    197         // These structs are the sub-sub-passes of ForallPointerDecay_old.
    198 
    199         struct TraitExpander_old final {
    200                 void previsit( FunctionType * );
    201                 void previsit( StructDecl * );
    202                 void previsit( UnionDecl * );
    203         };
    204 
    205         struct AssertionFixer_old final {
    206                 void previsit( FunctionType * );
    207                 void previsit( StructDecl * );
    208                 void previsit( UnionDecl * );
    209         };
    210 
    211         struct CheckOperatorTypes_old final {
    212                 void previsit( ObjectDecl * );
    213         };
    214 
    215         struct FixUniqueIds_old final {
    216                 void previsit( DeclarationWithType * );
    217154        };
    218155
     
    358295
    359296        void validate_A( std::list< Declaration * > & translationUnit ) {
    360                 PassVisitor<EnumAndPointerDecay_old> epc;
    361297                PassVisitor<HoistTypeDecls> hoistDecls;
    362298                {
     
    367303                        ReplaceTypedef::replaceTypedef( translationUnit );
    368304                        ReturnTypeFixer::fix( translationUnit ); // must happen before autogen
    369                         acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes_old because it is an indexer and needs correct types for mangling
    370                 }
    371         }
    372 
    373         void linkReferenceToTypes( std::list< Declaration * > & translationUnit ) {
    374                 PassVisitor<LinkReferenceToTypes_old> lrt( nullptr );
    375                 acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions
     305                        decayEnumsAndPointers( translationUnit ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes_old because it is an indexer and needs correct types for mangling
     306                }
    376307        }
    377308
     
    412343                        });
    413344                }
    414         }
    415 
    416         static void decayForallPointers( std::list< Declaration * > & translationUnit ) {
    417                 PassVisitor<TraitExpander_old> te;
    418                 acceptAll( translationUnit, te );
    419                 PassVisitor<AssertionFixer_old> af;
    420                 acceptAll( translationUnit, af );
    421                 PassVisitor<CheckOperatorTypes_old> cot;
    422                 acceptAll( translationUnit, cot );
    423                 PassVisitor<FixUniqueIds_old> fui;
    424                 acceptAll( translationUnit, fui );
    425345        }
    426346
     
    501421        }
    502422
    503         void validateType( Type * type, const Indexer * indexer ) {
    504                 PassVisitor<EnumAndPointerDecay_old> epc;
    505                 PassVisitor<LinkReferenceToTypes_old> lrt( indexer );
    506                 PassVisitor<TraitExpander_old> te;
    507                 PassVisitor<AssertionFixer_old> af;
    508                 PassVisitor<CheckOperatorTypes_old> cot;
    509                 PassVisitor<FixUniqueIds_old> fui;
    510                 type->accept( epc );
    511                 type->accept( lrt );
    512                 type->accept( te );
    513                 type->accept( af );
    514                 type->accept( cot );
    515                 type->accept( fui );
    516         }
    517 
    518423        void HoistTypeDecls::handleType( Type * type ) {
    519424                // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here
     
    708613        }
    709614
    710         void EnumAndPointerDecay_old::previsit( EnumDecl * enumDecl ) {
    711                 // Set the type of each member of the enumeration to be EnumConstant
    712                 for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) {
    713                         ObjectDecl * obj = dynamic_cast< ObjectDecl * >( * i );
    714                         assert( obj );
    715                         obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) );
    716                 } // for
    717         }
    718 
    719         namespace {
    720                 template< typename DWTList >
    721                 void fixFunctionList( DWTList & dwts, bool isVarArgs, FunctionType * func ) {
    722                         auto nvals = dwts.size();
    723                         bool containsVoid = false;
    724                         for ( auto & dwt : dwts ) {
    725                                 // fix each DWT and record whether a void was found
    726                                 containsVoid |= fixFunction( dwt );
    727                         }
    728 
    729                         // the only case in which "void" is valid is where it is the only one in the list
    730                         if ( containsVoid && ( nvals > 1 || isVarArgs ) ) {
    731                                 SemanticError( func, "invalid type void in function type " );
    732                         }
    733 
    734                         // one void is the only thing in the list; remove it.
    735                         if ( containsVoid ) {
    736                                 delete dwts.front();
    737                                 dwts.clear();
    738                         }
    739                 }
    740         }
    741 
    742         void EnumAndPointerDecay_old::previsit( FunctionType * func ) {
    743                 // Fix up parameters and return types
    744                 fixFunctionList( func->parameters, func->isVarArgs, func );
    745                 fixFunctionList( func->returnVals, false, func );
    746         }
    747 
    748         LinkReferenceToTypes_old::LinkReferenceToTypes_old( const Indexer * other_indexer ) : WithIndexer( false ) {
    749                 if ( other_indexer ) {
    750                         local_indexer = other_indexer;
    751                 } else {
    752                         local_indexer = &indexer;
    753                 } // if
    754         }
    755 
    756         void LinkReferenceToTypes_old::postvisit( EnumInstType * enumInst ) {
    757                 const EnumDecl * st = local_indexer->lookupEnum( enumInst->name );
    758                 // it's not a semantic error if the enum is not found, just an implicit forward declaration
    759                 if ( st ) {
    760                         enumInst->baseEnum = const_cast<EnumDecl *>(st); // Just linking in the node
    761                 } // if
    762                 if ( ! st || ! st->body ) {
    763                         // use of forward declaration
    764                         forwardEnums[ enumInst->name ].push_back( enumInst );
    765                 } // if
    766         }
    767         void LinkReferenceToTypes_old::postvisit( StructInstType * structInst ) {
    768                 const StructDecl * st = local_indexer->lookupStruct( structInst->name );
    769                 // it's not a semantic error if the struct is not found, just an implicit forward declaration
    770                 if ( st ) {
    771                         structInst->baseStruct = const_cast<StructDecl *>(st); // Just linking in the node
    772                 } // if
    773                 if ( ! st || ! st->body ) {
    774                         // use of forward declaration
    775                         forwardStructs[ structInst->name ].push_back( structInst );
    776                 } // if
    777         }
    778 
    779         void LinkReferenceToTypes_old::postvisit( UnionInstType * unionInst ) {
    780                 const UnionDecl * un = local_indexer->lookupUnion( unionInst->name );
    781                 // it's not a semantic error if the union is not found, just an implicit forward declaration
    782                 if ( un ) {
    783                         unionInst->baseUnion = const_cast<UnionDecl *>(un); // Just linking in the node
    784                 } // if
    785                 if ( ! un || ! un->body ) {
    786                         // use of forward declaration
    787                         forwardUnions[ unionInst->name ].push_back( unionInst );
    788                 } // if
    789         }
    790 
    791         void LinkReferenceToTypes_old::previsit( QualifiedType * ) {
    792                 visit_children = false;
    793         }
    794 
    795         void LinkReferenceToTypes_old::postvisit( QualifiedType * qualType ) {
    796                 // linking only makes sense for the 'oldest ancestor' of the qualified type
    797                 qualType->parent->accept( * visitor );
    798         }
    799 
    800         template< typename Decl >
    801         void normalizeAssertions( std::list< Decl * > & assertions ) {
    802                 // ensure no duplicate trait members after the clone
    803                 auto pred = [](Decl * d1, Decl * d2) {
    804                         // only care if they're equal
    805                         DeclarationWithType * dwt1 = dynamic_cast<DeclarationWithType *>( d1 );
    806                         DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 );
    807                         if ( dwt1 && dwt2 ) {
    808                                 if ( dwt1->name == dwt2->name && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) {
    809                                         // std::cerr << "=========== equal:" << std::endl;
    810                                         // std::cerr << "d1: " << d1 << std::endl;
    811                                         // std::cerr << "d2: " << d2 << std::endl;
    812                                         return false;
    813                                 }
    814                         }
    815                         return d1 < d2;
    816                 };
    817                 std::set<Decl *, decltype(pred)> unique_members( assertions.begin(), assertions.end(), pred );
    818                 // if ( unique_members.size() != assertions.size() ) {
    819                 //      std::cerr << "============different" << std::endl;
    820                 //      std::cerr << unique_members.size() << " " << assertions.size() << std::endl;
    821                 // }
    822 
    823                 std::list< Decl * > order;
    824                 order.splice( order.end(), assertions );
    825                 std::copy_if( order.begin(), order.end(), back_inserter( assertions ), [&]( Decl * decl ) {
    826                         return unique_members.count( decl );
    827                 });
    828         }
    829 
    830615        // expand assertions from trait instance, performing the appropriate type variable substitutions
    831616        template< typename Iterator >
     
    838623                // substitute trait decl parameters for instance parameters
    839624                applySubstitution( inst->baseTrait->parameters.begin(), inst->baseTrait->parameters.end(), inst->parameters.begin(), asserts.begin(), asserts.end(), out );
    840         }
    841 
    842         void LinkReferenceToTypes_old::postvisit( TraitDecl * traitDecl ) {
    843                 if ( traitDecl->name == "sized" ) {
    844                         // "sized" is a special trait - flick the sized status on for the type variable
    845                         assertf( traitDecl->parameters.size() == 1, "Built-in trait 'sized' has incorrect number of parameters: %zd", traitDecl->parameters.size() );
    846                         TypeDecl * td = traitDecl->parameters.front();
    847                         td->set_sized( true );
    848                 }
    849 
    850                 // move assertions from type parameters into the body of the trait
    851                 for ( TypeDecl * td : traitDecl->parameters ) {
    852                         for ( DeclarationWithType * assert : td->assertions ) {
    853                                 if ( TraitInstType * inst = dynamic_cast< TraitInstType * >( assert->get_type() ) ) {
    854                                         expandAssertions( inst, back_inserter( traitDecl->members ) );
    855                                 } else {
    856                                         traitDecl->members.push_back( assert->clone() );
    857                                 }
    858                         }
    859                         deleteAll( td->assertions );
    860                         td->assertions.clear();
    861                 } // for
    862         }
    863 
    864         void LinkReferenceToTypes_old::postvisit( TraitInstType * traitInst ) {
    865                 // handle other traits
    866                 const TraitDecl * traitDecl = local_indexer->lookupTrait( traitInst->name );
    867                 if ( ! traitDecl ) {
    868                         SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name );
    869                 } // if
    870                 if ( traitDecl->parameters.size() != traitInst->parameters.size() ) {
    871                         SemanticError( traitInst, "incorrect number of trait parameters: " );
    872                 } // if
    873                 traitInst->baseTrait = const_cast<TraitDecl *>(traitDecl); // Just linking in the node
    874 
    875                 // need to carry over the 'sized' status of each decl in the instance
    876                 for ( auto p : group_iterate( traitDecl->parameters, traitInst->parameters ) ) {
    877                         TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) );
    878                         if ( ! expr ) {
    879                                 SemanticError( std::get<1>(p), "Expression parameters for trait instances are currently unsupported: " );
    880                         }
    881                         if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) {
    882                                 TypeDecl * formalDecl = std::get<0>(p);
    883                                 TypeDecl * instDecl = inst->baseType;
    884                                 if ( formalDecl->get_sized() ) instDecl->set_sized( true );
    885                         }
    886                 }
    887                 // normalizeAssertions( traitInst->members );
    888         }
    889 
    890         void LinkReferenceToTypes_old::postvisit( EnumDecl * enumDecl ) {
    891                 // visit enum members first so that the types of self-referencing members are updated properly
    892                 // Replace the enum base; right now it works only for StructEnum
    893                 if ( enumDecl->base && dynamic_cast<TypeInstType*>(enumDecl->base) ) {
    894                         std::string baseName = static_cast<TypeInstType*>(enumDecl->base)->name;
    895                         const StructDecl * st = local_indexer->lookupStruct( baseName );
    896                         if ( st ) {
    897                                 enumDecl->base = new StructInstType(Type::Qualifiers(),const_cast<StructDecl *>(st)); // Just linking in the node
    898                         }
    899                 }
    900                 if ( enumDecl->body ) {
    901                         ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->name );
    902                         if ( fwds != forwardEnums.end() ) {
    903                                 for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    904                                         (* inst)->baseEnum = enumDecl;
    905                                 } // for
    906                                 forwardEnums.erase( fwds );
    907                         } // if
    908                 } // if
    909         }
    910 
    911         void LinkReferenceToTypes_old::renameGenericParams( std::list< TypeDecl * > & params ) {
    912                 // rename generic type parameters uniquely so that they do not conflict with user-defined function forall parameters, e.g.
    913                 //   forall(otype T)
    914                 //   struct Box {
    915                 //     T x;
    916                 //   };
    917                 //   forall(otype T)
    918                 //   void f(Box(T) b) {
    919                 //     ...
    920                 //   }
    921                 // The T in Box and the T in f are different, so internally the naming must reflect that.
    922                 GuardValue( inGeneric );
    923                 inGeneric = ! params.empty();
    924                 for ( TypeDecl * td : params ) {
    925                         td->name = "__" + td->name + "_generic_";
    926                 }
    927         }
    928 
    929         void LinkReferenceToTypes_old::previsit( StructDecl * structDecl ) {
    930                 renameGenericParams( structDecl->parameters );
    931         }
    932 
    933         void LinkReferenceToTypes_old::previsit( UnionDecl * unionDecl ) {
    934                 renameGenericParams( unionDecl->parameters );
    935         }
    936 
    937         void LinkReferenceToTypes_old::postvisit( StructDecl * structDecl ) {
    938                 // visit struct members first so that the types of self-referencing members are updated properly
    939                 // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults)
    940                 if ( structDecl->body ) {
    941                         ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->name );
    942                         if ( fwds != forwardStructs.end() ) {
    943                                 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    944                                         (* inst)->baseStruct = structDecl;
    945                                 } // for
    946                                 forwardStructs.erase( fwds );
    947                         } // if
    948                 } // if
    949         }
    950 
    951         void LinkReferenceToTypes_old::postvisit( UnionDecl * unionDecl ) {
    952                 if ( unionDecl->body ) {
    953                         ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name );
    954                         if ( fwds != forwardUnions.end() ) {
    955                                 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    956                                         (* inst)->baseUnion = unionDecl;
    957                                 } // for
    958                                 forwardUnions.erase( fwds );
    959                         } // if
    960                 } // if
    961         }
    962 
    963         void LinkReferenceToTypes_old::postvisit( TypeInstType * typeInst ) {
    964                 // ensure generic parameter instances are renamed like the base type
    965                 if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name;
    966                 if ( const NamedTypeDecl * namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) {
    967                         if ( const TypeDecl * typeDecl = dynamic_cast< const TypeDecl * >( namedTypeDecl ) ) {
    968                                 typeInst->set_isFtype( typeDecl->kind == TypeDecl::Ftype );
    969                         } // if
    970                 } // if
    971625        }
    972626
     
    997651                                                }
    998652                                        }
    999                                        
    1000653                                }
    1001654                        }
     
    1085738        void ForallPointerDecay_old::previsit( UnionDecl * aggrDecl ) {
    1086739                forallFixer( aggrDecl->parameters, aggrDecl );
    1087         }
    1088 
    1089         void TraitExpander_old::previsit( FunctionType * ftype ) {
    1090                 expandTraits( ftype->forall );
    1091         }
    1092 
    1093         void TraitExpander_old::previsit( StructDecl * aggrDecl ) {
    1094                 expandTraits( aggrDecl->parameters );
    1095         }
    1096 
    1097         void TraitExpander_old::previsit( UnionDecl * aggrDecl ) {
    1098                 expandTraits( aggrDecl->parameters );
    1099         }
    1100 
    1101         void AssertionFixer_old::previsit( FunctionType * ftype ) {
    1102                 fixAssertions( ftype->forall, ftype );
    1103         }
    1104 
    1105         void AssertionFixer_old::previsit( StructDecl * aggrDecl ) {
    1106                 fixAssertions( aggrDecl->parameters, aggrDecl );
    1107         }
    1108 
    1109         void AssertionFixer_old::previsit( UnionDecl * aggrDecl ) {
    1110                 fixAssertions( aggrDecl->parameters, aggrDecl );
    1111         }
    1112 
    1113         void CheckOperatorTypes_old::previsit( ObjectDecl * object ) {
    1114                 // ensure that operator names only apply to functions or function pointers
    1115                 if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) {
    1116                         SemanticError( object->location, toCString( "operator ", object->name.c_str(), " is not a function or function pointer." )  );
    1117                 }
    1118         }
    1119 
    1120         void FixUniqueIds_old::previsit( DeclarationWithType * decl ) {
    1121                 decl->fixUniqueId();
    1122740        }
    1123741
Note: See TracChangeset for help on using the changeset viewer.