Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r033ff37 ra1c9ddd  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jul 25 22:17:10 2019
    13 // Update Count     : 1116
     12// Last Modified On : Thu Jun  7 12:08:55 2018
     13// Update Count     : 1079
    1414//
    1515
     
    4141
    4242// These must harmonize with the corresponding DeclarationNode enumerations.
    43 const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "int128",
    44                                                                                                    "float", "double", "long double", "float80", "float128",
    45                                                                                                    "_float16", "_float32", "_float32x", "_float64", "_float64x", "_float128", "_float128x", "NoBasicTypeNames" };
    46 const char * DeclarationNode::complexTypeNames[] = { "_Complex", "NoComplexTypeNames", "_Imaginary" }; // Imaginary unsupported => parse, but make invisible and print error message
     43const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "int128", "float80", "float128", "NoBasicTypeNames" };
     44const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
    4745const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
    4846const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
    4947const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" };
    5048const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
    51 const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "__auto_type", "zero_t", "one_t", "NoBuiltinTypeNames" };
     49const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "zero_t", "one_t", "NoBuiltinTypeNames" };
    5250
    5351UniqueName DeclarationNode::anonymous( "__anonymous" );
     
    5654
    5755DeclarationNode::DeclarationNode() :
    58         linkage( ::linkage ) {
     56                builtin( NoBuiltinType ),
     57                type( nullptr ),
     58                bitfieldWidth( nullptr ),
     59                hasEllipsis( false ),
     60                linkage( ::linkage ),
     61                asmName( nullptr ),
     62                initializer( nullptr ),
     63                extension( false ),
     64                asmStmt( nullptr ) {
    5965
    6066//      variable.name = nullptr;
     
    98104        newnode->builtin = NoBuiltinType;
    99105        newnode->type = maybeClone( type );
    100         newnode->inLine = inLine;
    101106        newnode->storageClasses = storageClasses;
    102107        newnode->funcSpecs = funcSpecs;
     
    126131} // DeclarationNode::clone
    127132
    128 void DeclarationNode::print( std::ostream & os, int indent ) const {
     133void DeclarationNode::print( std::ostream &os, int indent ) const {
    129134        os << string( indent, ' ' );
    130135        if ( name ) {
     
    162167}
    163168
    164 void DeclarationNode::printList( std::ostream & os, int indent ) const {
     169void DeclarationNode::printList( std::ostream &os, int indent ) const {
    165170        ParseNode::printList( os, indent );
    166171        if ( hasEllipsis ) {
     
    249254} // DeclarationNode::newFromTypedef
    250255
    251 DeclarationNode * DeclarationNode::newFromGlobalScope() {
    252         DeclarationNode * newnode = new DeclarationNode;
    253         newnode->type = new TypeData( TypeData::GlobalScope );
    254         return newnode;
    255 }
    256 
    257 DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
    258         DeclarationNode * newnode = new DeclarationNode;
    259         newnode->type = new TypeData( TypeData::Qualified );
    260         newnode->type->qualified.parent = parent->type;
    261         newnode->type->qualified.child = child->type;
    262         parent->type = nullptr;
    263         child->type = nullptr;
    264         delete parent;
    265         delete child;
    266         return newnode;
    267 }
    268 
    269256DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
     257        assert( name );
    270258        DeclarationNode * newnode = new DeclarationNode;
    271259        newnode->type = new TypeData( TypeData::Aggregate );
    272260        newnode->type->aggregate.kind = kind;
    273         newnode->type->aggregate.name =  name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
     261        newnode->type->aggregate.name = name;
    274262        newnode->type->aggregate.actuals = actuals;
    275263        newnode->type->aggregate.fields = fields;
     
    277265        newnode->type->aggregate.tagged = false;
    278266        newnode->type->aggregate.parent = nullptr;
    279         newnode->type->aggregate.anon = name == nullptr;
    280267        return newnode;
    281268} // DeclarationNode::newAggregate
    282269
    283270DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) {
     271        assert( name );
    284272        DeclarationNode * newnode = new DeclarationNode;
    285273        newnode->type = new TypeData( TypeData::Enum );
    286         newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
     274        newnode->type->enumeration.name = name;
    287275        newnode->type->enumeration.constants = constants;
    288276        newnode->type->enumeration.body = body;
    289         newnode->type->enumeration.anon = name == nullptr;
    290277        return newnode;
    291278} // DeclarationNode::newEnum
     
    404391}
    405392
    406 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) {
    407         DeclarationNode * newnode = new DeclarationNode;
    408         newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof );
     393DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
     394        DeclarationNode * newnode = new DeclarationNode;
     395        newnode->type = new TypeData( TypeData::Typeof );
    409396        newnode->type->typeexpr = expr;
    410397        return newnode;
     
    418405        return newnode;
    419406} // DeclarationNode::newBuiltinType
     407
     408DeclarationNode * DeclarationNode::newAttr( const string * name, ExpressionNode * expr ) {
     409        DeclarationNode * newnode = new DeclarationNode;
     410        newnode->type = nullptr;
     411//      newnode->attr.name = name;
     412        newnode->name = name;
     413        newnode->attr.expr = expr;
     414        return newnode;
     415}
     416
     417DeclarationNode * DeclarationNode::newAttr( const string * name, DeclarationNode * type ) {
     418        DeclarationNode * newnode = new DeclarationNode;
     419        newnode->type = nullptr;
     420//      newnode->attr.name = name;
     421        newnode->name = name;
     422        newnode->attr.type = type;
     423        return newnode;
     424}
    420425
    421426DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
     
    498503} // DeclarationNode::copySpecifiers
    499504
    500 static void addQualifiersToType( TypeData *& src, TypeData * dst ) {
     505static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
     506        if ( src->forall && dst->kind == TypeData::Function ) {
     507                if ( dst->forall ) {
     508                        dst->forall->appendList( src->forall );
     509                } else {
     510                        dst->forall = src->forall;
     511                } // if
     512                src->forall = nullptr;
     513        } // if
    501514        if ( dst->base ) {
    502515                addQualifiersToType( src, dst->base );
     
    551564} // addQualifiers
    552565
    553 static void addTypeToType( TypeData *& src, TypeData *& dst ) {
     566static void addTypeToType( TypeData *&src, TypeData *&dst ) {
    554567        if ( src->forall && dst->kind == TypeData::Function ) {
    555568                if ( dst->forall ) {
     
    942955}
    943956
    944 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > & outputList ) {
     957void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
    945958        SemanticErrorException errors;
    946959        std::back_insert_iterator< std::list< Declaration * > > out( outputList );
     
    948961        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
    949962                try {
    950                         bool extracted = false;
    951                         bool anon = false;
    952963                        if ( DeclarationNode * extr = cur->extractAggregate() ) {
    953964                                // handle the case where a structure declaration is contained within an object or type declaration
    954965                                Declaration * decl = extr->build();
    955966                                if ( decl ) {
    956                                         // hoist the structure declaration
    957967                                        decl->location = cur->location;
    958968                                        * out++ = decl;
    959 
    960                                         // need to remember the cases where a declaration contains an anonymous aggregate definition
    961                                         extracted = true;
    962                                         assert( extr->type );
    963                                         if ( extr->type->kind == TypeData::Aggregate ) {
    964                                                 anon = extr->type->aggregate.anon;
    965                                         } else if ( extr->type->kind == TypeData::Enum ) {
    966                                                 // xxx - is it useful to have an implicit anonymous enum member?
    967                                                 anon = extr->type->enumeration.anon;
    968                                         }
    969969                                } // if
    970970                                delete extr;
     
    973973                        Declaration * decl = cur->build();
    974974                        if ( decl ) {
    975                                 // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.:
    976                                 // struct S {
    977                                 //   struct T { int x; }; // no anonymous member
    978                                 //   struct { int y; };   // anonymous member
    979                                 //   struct T;            // anonymous member
    980                                 // };
    981                                 if ( ! (extracted && decl->name == "" && ! anon && ! cur->get_inLine()) ) {
    982                                         if ( decl->name == "" ) {
    983                                                 if ( DeclarationWithType * dwt = dynamic_cast<DeclarationWithType *>( decl ) ) {
    984                                                         if ( ReferenceToType * aggr = dynamic_cast<ReferenceToType *>( dwt->get_type() ) ) {
    985                                                                 if ( aggr->name.find("anonymous") == std::string::npos ) {
    986                                                                         if ( ! cur->get_inLine() ) {
    987                                                                                 // temporary: warn about anonymous member declarations of named types, since
    988                                                                                 // this conflicts with the syntax for the forward declaration of an anonymous type
    989                                                                                 SemanticWarning( cur->location, Warning::AggrForwardDecl, aggr->name.c_str() );
    990                                                                         } // if
    991                                                                 } // if
    992                                                         } // if
    993                                                 } // if
    994                                         } // if
    995                                         decl->location = cur->location;
    996                                         *out++ = decl;
     975                                decl->location = cur->location;
     976                                * out++ = decl;
     977                        } // if
     978                } catch( SemanticErrorException &e ) {
     979                        errors.append( e );
     980                } // try
     981        } // while
     982
     983        if ( ! errors.isEmpty() ) {
     984                throw errors;
     985        } // if
     986} // buildList
     987
     988void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
     989        SemanticErrorException errors;
     990        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
     991
     992        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
     993                try {
     994                        Declaration * decl = cur->build();
     995                        if ( decl ) {
     996                                if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
     997                                        dwt->location = cur->location;
     998                                        * out++ = dwt;
     999                                } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
     1000                                        StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
     1001                                        auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
     1002                                        obj->location = cur->location;
     1003                                        * out++ = obj;
     1004                                        delete agg;
     1005                                } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
     1006                                        UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
     1007                                        auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
     1008                                        obj->location = cur->location;
     1009                                        * out++ = obj;
    9971010                                } // if
    9981011                        } // if
    999                 } catch( SemanticErrorException & e ) {
     1012                } catch( SemanticErrorException &e ) {
    10001013                        errors.append( e );
    10011014                } // try
     
    10071020} // buildList
    10081021
    1009 // currently only builds assertions, function parameters, and return values
    1010 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > & outputList ) {
    1011         SemanticErrorException errors;
    1012         std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
    1013 
    1014         for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
    1015                 try {
    1016                         Declaration * decl = cur->build();
    1017                         assert( decl );
    1018                         if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
    1019                                 dwt->location = cur->location;
    1020                                 * out++ = dwt;
    1021                         } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
    1022                                 // e.g., int foo(struct S) {}
    1023                                 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name );
    1024                                 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
    1025                                 obj->location = cur->location;
    1026                                 * out++ = obj;
    1027                                 delete agg;
    1028                         } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
    1029                                 // e.g., int foo(union U) {}
    1030                                 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name );
    1031                                 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
    1032                                 obj->location = cur->location;
    1033                                 * out++ = obj;
    1034                         } else if ( EnumDecl * agg = dynamic_cast< EnumDecl * >( decl ) ) {
    1035                                 // e.g., int foo(enum E) {}
    1036                                 EnumInstType * inst = new EnumInstType( Type::Qualifiers(), agg->name );
    1037                                 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
    1038                                 obj->location = cur->location;
    1039                                 * out++ = obj;
    1040                         } // if
    1041                 } catch( SemanticErrorException & e ) {
    1042                         errors.append( e );
    1043                 } // try
    1044         } // for
    1045 
    1046         if ( ! errors.isEmpty() ) {
    1047                 throw errors;
    1048         } // if
    1049 } // buildList
    1050 
    1051 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > & outputList ) {
     1022void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
    10521023        SemanticErrorException errors;
    10531024        std::back_insert_iterator< std::list< Type * > > out( outputList );
     
    10571028                try {
    10581029                        * out++ = cur->buildType();
    1059                 } catch( SemanticErrorException & e ) {
     1030                } catch( SemanticErrorException &e ) {
    10601031                        errors.append( e );
    10611032                } // try
     
    10931064                if ( type->kind != TypeData::Function && funcSpecs.any() ) {
    10941065                        SemanticError( this, "invalid function specifier for " );
    1095                 } // if
    1096                 // Forall qualifier can only appear on a function/aggregate definition/declaration.
    1097                 //
    1098                 //    forall int f();                                   // allowed
    1099                 //    forall int g( int i );                    // allowed
    1100                 //    forall int i;                                             // disallowed
    1101                 if ( type->kind != TypeData::Function && type->forall ) {
    1102                         SemanticError( this, "invalid type qualifier for " );
    11031066                } // if
    11041067                bool isDelete = initializer && initializer->get_isDelete();
Note: See TracChangeset for help on using the changeset viewer.