Changeset c0d00b6 for src/SymTab


Ignore:
Timestamp:
Nov 25, 2017, 3:22:18 PM (6 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
c6e2c18
Parents:
9d06142 (diff), 3de176d (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' into cleanup-dtors

Location:
src/SymTab
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Autogen.cc

    r9d06142 rc0d00b6  
    6262                void previsit( FunctionDecl * functionDecl );
    6363
    64                 void previsit( FunctionType * ftype );
    65                 void previsit( PointerType * ptype );
    66 
    6764                void previsit( CompoundStmt * compoundStmt );
    6865
     
    7269                unsigned int functionNesting = 0;     // current level of nested functions
    7370
    74                 InitTweak::ManagedTypes managedTypes;
    7571                std::vector< FuncData > data;
    7672        };
     
    625621        // generate ctor/dtors/assign for typedecls, e.g., otype T = int *;
    626622        void AutogenerateRoutines::previsit( TypeDecl * typeDecl ) {
    627                 visit_children = false;
    628623                if ( ! typeDecl->base ) return;
    629624
     
    631626                TypeFuncGenerator gen( typeDecl, &refType, data, functionNesting, indexer );
    632627                generateFunctions( gen, declsToAddAfter );
    633         }
    634 
    635         void AutogenerateRoutines::previsit( FunctionType *) {
    636                 // ensure that we don't add assignment ops for types defined as part of the function
    637                 visit_children = false;
    638         }
    639 
    640         void AutogenerateRoutines::previsit( PointerType *) {
    641                 // ensure that we don't add assignment ops for types defined as part of the pointer
    642                 visit_children = false;
     628
    643629        }
    644630
     
    648634        }
    649635
    650         void AutogenerateRoutines::previsit( FunctionDecl * functionDecl ) {
    651                 visit_children = false;
    652                 // record the existence of this function as appropriate
    653                 managedTypes.handleDWT( functionDecl );
    654 
    655                 maybeAccept( functionDecl->type, *visitor );
     636        void AutogenerateRoutines::previsit( FunctionDecl * ) {
     637                // Track whether we're currently in a function.
     638                // Can ignore function type idiosyncrasies, because function type can never
     639                // declare a new type.
    656640                functionNesting += 1;
    657                 maybeAccept( functionDecl->statements, *visitor );
    658                 functionNesting -= 1;
     641                GuardAction( [this]()  { functionNesting -= 1; } );
    659642        }
    660643
    661644        void AutogenerateRoutines::previsit( CompoundStmt * ) {
    662                 GuardScope( managedTypes );
    663645                GuardScope( structsDone );
    664646        }
  • src/SymTab/Autogen.h

    r9d06142 rc0d00b6  
    5959        /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls.
    6060        template< typename OutputIterator >
    61         Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false, bool forward = true );
     61        Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, Type * addCast = nullptr, bool forward = true );
    6262
    6363        /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types.
    6464        /// optionally returns a statement which must be inserted prior to the containing loop, if there is one
    6565        template< typename OutputIterator >
    66         Statement * genScalarCall( InitTweak::InitExpander & srcParam, Expression * dstParam, std::string fname, OutputIterator out, Type * type, bool addCast = false ) {
     66        Statement * genScalarCall( InitTweak::InitExpander & srcParam, Expression * dstParam, std::string fname, OutputIterator out, Type * type, Type * addCast = nullptr ) {
    6767                bool isReferenceCtorDtor = false;
    6868                if ( dynamic_cast< ReferenceType * >( type ) && CodeGen::isCtorDtor( fname ) ) {
     
    7171                        fname = "?=?";
    7272                        dstParam = new AddressExpr( dstParam );
    73                         addCast = false;
     73                        addCast = nullptr;
    7474                        isReferenceCtorDtor = true;
    7575                }
     
    8686                        // remove lvalue as a qualifier, this can change to
    8787                        //   type->get_qualifiers() = Type::Qualifiers();
    88                         assert( type );
    89                         Type * castType = type->clone();
     88                        Type * castType = addCast->clone();
    9089                        castType->get_qualifiers() -= Type::Qualifiers( Type::Lvalue | Type::Const | Type::Volatile | Type::Restrict | Type::Atomic );
    9190                        // castType->set_lvalue( true ); // xxx - might not need this
     
    118117        /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
    119118        template< typename OutputIterator >
    120         void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool addCast = false, bool forward = true ) {
     119        void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, Type * addCast = nullptr, bool forward = true ) {
    121120                static UniqueName indexName( "_index" );
    122121
    123122                // for a flexible array member nothing is done -- user must define own assignment
    124                 if ( ! array->get_dimension() ) return ;
     123                if ( ! array->get_dimension() ) return;
     124
     125                if ( addCast ) {
     126                        // peel off array layer from cast
     127                        ArrayType * at = strict_dynamic_cast< ArrayType * >( addCast );
     128                        addCast = at->base;
     129                }
    125130
    126131                Expression * begin, * end, * update, * cmp;
     
    174179
    175180        template< typename OutputIterator >
    176         Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast, bool forward ) {
     181        Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, Type * addCast, bool forward ) {
    177182                if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
    178183                        genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward );
     
    194199                if ( isUnnamedBitfield( obj ) ) return;
    195200
    196                 bool addCast = (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && ! obj->get_bitfieldWidth() ) );
     201                Type * addCast = nullptr;
     202                if ( (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && ! obj->get_bitfieldWidth() ) ) ) {
     203                        assert( dstParam->result );
     204                        addCast = dstParam->result;
     205                }
    197206                std::list< Statement * > stmts;
    198207                genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->type, addCast, forward );
  • src/SymTab/Indexer.cc

    r9d06142 rc0d00b6  
    567567        }
    568568
     569        void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) {
     570                for ( auto d : decls ) {
     571                        addId( d );
     572                }
     573        }
     574
     575        void Indexer::addTypes( const std::list< TypeDecl * > & tds ) {
     576                for ( auto td : tds ) {
     577                        addType( td );
     578                        addIds( td->assertions );
     579                }
     580        }
     581
     582        void Indexer::addFunctionType( FunctionType * ftype ) {
     583                addTypes( ftype->forall );
     584                addIds( ftype->returnVals );
     585                addIds( ftype->parameters );
     586        }
     587
    569588        void Indexer::enterScope() {
    570589                ++scope;
  • src/SymTab/Indexer.h

    r9d06142 rc0d00b6  
    7676                void addTrait( TraitDecl *decl );
    7777
     78                /// convenience function for adding a list of Ids to the indexer
     79                void addIds( const std::list< DeclarationWithType * > & decls );
     80
     81                /// convenience function for adding a list of forall parameters to the indexer
     82                void addTypes( const std::list< TypeDecl * > & tds );
     83
     84                /// convenience function for adding all of the declarations in a function type to the indexer
     85                void addFunctionType( FunctionType * ftype );
     86
    7887                bool doDebug = false; ///< Display debugging trace?
    7988          private:
  • src/SymTab/Validate.cc

    r9d06142 rc0d00b6  
    124124
    125125        /// Associates forward declarations of aggregates with their definitions
    126         struct LinkReferenceToTypes final : public WithIndexer {
     126        struct LinkReferenceToTypes final : public WithIndexer, public WithGuards {
    127127                LinkReferenceToTypes( const Indexer *indexer );
    128128                void postvisit( TypeInstType *typeInst );
     
    137137                void postvisit( UnionDecl *unionDecl );
    138138                void postvisit( TraitDecl * traitDecl );
     139
     140                void previsit( StructDecl *structDecl );
     141                void previsit( UnionDecl *unionDecl );
     142
     143                void renameGenericParams( std::list< TypeDecl * > & params );
    139144
    140145          private:
     
    147152                ForwardStructsType forwardStructs;
    148153                ForwardUnionsType forwardUnions;
     154                /// true if currently in a generic type body, so that type parameter instances can be renamed appropriately
     155                bool inGeneric = false;
    149156        };
    150157
     
    423430        }
    424431
     432        void checkGenericParameters( ReferenceToType * inst ) {
     433                for ( Expression * param : inst->parameters ) {
     434                        if ( ! dynamic_cast< TypeExpr * >( param ) ) {
     435                                throw SemanticError( "Expression parameters for generic types are currently unsupported: ", inst );
     436                        }
     437                }
     438        }
     439
    425440        void LinkReferenceToTypes::postvisit( StructInstType *structInst ) {
    426441                StructDecl *st = local_indexer->lookupStruct( structInst->get_name() );
     
    434449                        forwardStructs[ structInst->get_name() ].push_back( structInst );
    435450                } // if
     451                checkGenericParameters( structInst );
    436452        }
    437453
     
    446462                        forwardUnions[ unionInst->get_name() ].push_back( unionInst );
    447463                } // if
     464                checkGenericParameters( unionInst );
    448465        }
    449466
     
    525542                // need to carry over the 'sized' status of each decl in the instance
    526543                for ( auto p : group_iterate( traitDecl->get_parameters(), traitInst->get_parameters() ) ) {
    527                         TypeExpr * expr = strict_dynamic_cast< TypeExpr * >( std::get<1>(p) );
     544                        TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) );
     545                        if ( ! expr ) {
     546                                throw SemanticError( "Expression parameters for trait instances are currently unsupported: ", std::get<1>(p) );
     547                        }
    528548                        if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) {
    529549                                TypeDecl * formalDecl = std::get<0>(p);
     
    546566                        } // if
    547567                } // if
     568        }
     569
     570        void LinkReferenceToTypes::renameGenericParams( std::list< TypeDecl * > & params ) {
     571                // rename generic type parameters uniquely so that they do not conflict with user-defined function forall parameters, e.g.
     572                //   forall(otype T)
     573                //   struct Box {
     574                //     T x;
     575                //   };
     576                //   forall(otype T)
     577                //   void f(Box(T) b) {
     578                //     ...
     579                //   }
     580                // The T in Box and the T in f are different, so internally the naming must reflect that.
     581                GuardValue( inGeneric );
     582                inGeneric = ! params.empty();
     583                for ( TypeDecl * td : params ) {
     584                        td->name = "__" + td->name + "_generic_";
     585                }
     586        }
     587
     588        void LinkReferenceToTypes::previsit( StructDecl * structDecl ) {
     589                renameGenericParams( structDecl->parameters );
     590        }
     591
     592        void LinkReferenceToTypes::previsit( UnionDecl * unionDecl ) {
     593                renameGenericParams( unionDecl->parameters );
    548594        }
    549595
     
    575621
    576622        void LinkReferenceToTypes::postvisit( TypeInstType *typeInst ) {
     623                // ensure generic parameter instances are renamed like the base type
     624                if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name;
    577625                if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->get_name() ) ) {
    578626                        if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
Note: See TracChangeset for help on using the changeset viewer.