Ignore:
Timestamp:
Oct 29, 2019, 4:01:24 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
773db65, 9421f3d8
Parents:
7951100 (diff), 8364209 (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/Parser/DeclarationNode.cc

    r7951100 rb067d9b  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jun  7 12:08:55 2018
    13 // Update Count     : 1079
     12// Last Modified On : Thu Jul 25 22:17:10 2019
     13// Update Count     : 1116
    1414//
    1515
     
    4141
    4242// These must harmonize with the corresponding DeclarationNode enumerations.
    43 const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "int128", "float80", "float128", "NoBasicTypeNames" };
    44 const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
     43const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "int128",
     44                                                                                                   "float", "double", "long double", "float80", "float128",
     45                                                                                                   "_float16", "_float32", "_float32x", "_float64", "_float64x", "_float128", "_float128x", "NoBasicTypeNames" };
     46const char * DeclarationNode::complexTypeNames[] = { "_Complex", "NoComplexTypeNames", "_Imaginary" }; // Imaginary unsupported => parse, but make invisible and print error message
    4547const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
    4648const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
    4749const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" };
    4850const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
    49 const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "zero_t", "one_t", "NoBuiltinTypeNames" };
     51const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "__auto_type", "zero_t", "one_t", "NoBuiltinTypeNames" };
    5052
    5153UniqueName DeclarationNode::anonymous( "__anonymous" );
     
    5456
    5557DeclarationNode::DeclarationNode() :
    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 ) {
     58        linkage( ::linkage ) {
    6559
    6660//      variable.name = nullptr;
     
    10498        newnode->builtin = NoBuiltinType;
    10599        newnode->type = maybeClone( type );
     100        newnode->inLine = inLine;
    106101        newnode->storageClasses = storageClasses;
    107102        newnode->funcSpecs = funcSpecs;
     
    131126} // DeclarationNode::clone
    132127
    133 void DeclarationNode::print( std::ostream &os, int indent ) const {
     128void DeclarationNode::print( std::ostream & os, int indent ) const {
    134129        os << string( indent, ' ' );
    135130        if ( name ) {
     
    167162}
    168163
    169 void DeclarationNode::printList( std::ostream &os, int indent ) const {
     164void DeclarationNode::printList( std::ostream & os, int indent ) const {
    170165        ParseNode::printList( os, indent );
    171166        if ( hasEllipsis ) {
     
    254249} // DeclarationNode::newFromTypedef
    255250
     251DeclarationNode * DeclarationNode::newFromGlobalScope() {
     252        DeclarationNode * newnode = new DeclarationNode;
     253        newnode->type = new TypeData( TypeData::GlobalScope );
     254        return newnode;
     255}
     256
     257DeclarationNode * 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
    256269DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
    257         assert( name );
    258270        DeclarationNode * newnode = new DeclarationNode;
    259271        newnode->type = new TypeData( TypeData::Aggregate );
    260272        newnode->type->aggregate.kind = kind;
    261         newnode->type->aggregate.name = name;
     273        newnode->type->aggregate.name =  name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
    262274        newnode->type->aggregate.actuals = actuals;
    263275        newnode->type->aggregate.fields = fields;
     
    265277        newnode->type->aggregate.tagged = false;
    266278        newnode->type->aggregate.parent = nullptr;
     279        newnode->type->aggregate.anon = name == nullptr;
    267280        return newnode;
    268281} // DeclarationNode::newAggregate
    269282
    270283DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) {
    271         assert( name );
    272284        DeclarationNode * newnode = new DeclarationNode;
    273285        newnode->type = new TypeData( TypeData::Enum );
    274         newnode->type->enumeration.name = name;
     286        newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
    275287        newnode->type->enumeration.constants = constants;
    276288        newnode->type->enumeration.body = body;
     289        newnode->type->enumeration.anon = name == nullptr;
    277290        return newnode;
    278291} // DeclarationNode::newEnum
     
    391404}
    392405
    393 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
    394         DeclarationNode * newnode = new DeclarationNode;
    395         newnode->type = new TypeData( TypeData::Typeof );
     406DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) {
     407        DeclarationNode * newnode = new DeclarationNode;
     408        newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof );
    396409        newnode->type->typeexpr = expr;
    397410        return newnode;
     
    405418        return newnode;
    406419} // DeclarationNode::newBuiltinType
    407 
    408 DeclarationNode * 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 
    417 DeclarationNode * 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 }
    425420
    426421DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
     
    503498} // DeclarationNode::copySpecifiers
    504499
    505 static 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
     500static void addQualifiersToType( TypeData *& src, TypeData * dst ) {
    514501        if ( dst->base ) {
    515502                addQualifiersToType( src, dst->base );
     
    564551} // addQualifiers
    565552
    566 static void addTypeToType( TypeData *&src, TypeData *&dst ) {
     553static void addTypeToType( TypeData *& src, TypeData *& dst ) {
    567554        if ( src->forall && dst->kind == TypeData::Function ) {
    568555                if ( dst->forall ) {
     
    955942}
    956943
    957 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
     944void buildList( const DeclarationNode * firstNode, std::list< Declaration * > & outputList ) {
    958945        SemanticErrorException errors;
    959946        std::back_insert_iterator< std::list< Declaration * > > out( outputList );
     
    961948        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
    962949                try {
     950                        bool extracted = false;
     951                        bool anon = false;
    963952                        if ( DeclarationNode * extr = cur->extractAggregate() ) {
    964953                                // handle the case where a structure declaration is contained within an object or type declaration
    965954                                Declaration * decl = extr->build();
    966955                                if ( decl ) {
     956                                        // hoist the structure declaration
    967957                                        decl->location = cur->location;
    968958                                        * 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                                 decl->location = cur->location;
    976                                 * out++ = 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;
     997                                } // if
    977998                        } // if
    978                 } catch( SemanticErrorException &e ) {
     999                } catch( SemanticErrorException & e ) {
    9791000                        errors.append( e );
    9801001                } // try
    981         } // while
     1002        } // for
    9821003
    9831004        if ( ! errors.isEmpty() ) {
     
    9861007} // buildList
    9871008
    988 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
     1009// currently only builds assertions, function parameters, and return values
     1010void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > & outputList ) {
    9891011        SemanticErrorException errors;
    9901012        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
     
    9931015                try {
    9941016                        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;
    1010                                 } // if
     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;
    10111040                        } // if
    1012                 } catch( SemanticErrorException &e ) {
     1041                } catch( SemanticErrorException & e ) {
    10131042                        errors.append( e );
    10141043                } // try
     
    10201049} // buildList
    10211050
    1022 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
     1051void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > & outputList ) {
    10231052        SemanticErrorException errors;
    10241053        std::back_insert_iterator< std::list< Type * > > out( outputList );
     
    10281057                try {
    10291058                        * out++ = cur->buildType();
    1030                 } catch( SemanticErrorException &e ) {
     1059                } catch( SemanticErrorException & e ) {
    10311060                        errors.append( e );
    10321061                } // try
     
    10641093                if ( type->kind != TypeData::Function && funcSpecs.any() ) {
    10651094                        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 " );
    10661103                } // if
    10671104                bool isDelete = initializer && initializer->get_isDelete();
Note: See TracChangeset for help on using the changeset viewer.