Changeset 777ed2b for src/Parser


Ignore:
Timestamp:
Jul 11, 2018, 11:55:59 AM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
Children:
0fc52b6
Parents:
fc20514 (diff), 7de22b28 (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:

fix conflicts

Location:
src/Parser
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rfc20514 r777ed2b  
    254254} // DeclarationNode::newFromTypedef
    255255
     256DeclarationNode * DeclarationNode::newFromGlobalScope() {
     257        DeclarationNode * newnode = new DeclarationNode;
     258        newnode->type = new TypeData( TypeData::GlobalScope );
     259        return newnode;
     260}
     261
     262DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
     263        DeclarationNode * newnode = new DeclarationNode;
     264        newnode->type = new TypeData( TypeData::Qualified );
     265        newnode->type->qualified.parent = parent->type;
     266        newnode->type->qualified.child = child->type;
     267        parent->type = nullptr;
     268        child->type = nullptr;
     269        delete parent;
     270        delete child;
     271        return newnode;
     272}
     273
    256274DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
    257         assert( name );
    258275        DeclarationNode * newnode = new DeclarationNode;
    259276        newnode->type = new TypeData( TypeData::Aggregate );
    260277        newnode->type->aggregate.kind = kind;
    261         newnode->type->aggregate.name = name;
     278        newnode->type->aggregate.name =  name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
    262279        newnode->type->aggregate.actuals = actuals;
    263280        newnode->type->aggregate.fields = fields;
     
    265282        newnode->type->aggregate.tagged = false;
    266283        newnode->type->aggregate.parent = nullptr;
     284        newnode->type->aggregate.anon = name == nullptr;
    267285        return newnode;
    268286} // DeclarationNode::newAggregate
    269287
    270288DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) {
    271         assert( name );
    272289        DeclarationNode * newnode = new DeclarationNode;
    273290        newnode->type = new TypeData( TypeData::Enum );
    274         newnode->type->enumeration.name = name;
     291        newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
    275292        newnode->type->enumeration.constants = constants;
    276293        newnode->type->enumeration.body = body;
     294        newnode->type->enumeration.anon = name == nullptr;
    277295        return newnode;
    278296} // DeclarationNode::newEnum
     
    953971        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
    954972                try {
     973                        bool extracted = false;
     974                        bool anon = false;
    955975                        if ( DeclarationNode * extr = cur->extractAggregate() ) {
    956976                                // handle the case where a structure declaration is contained within an object or type declaration
    957977                                Declaration * decl = extr->build();
    958978                                if ( decl ) {
     979                                        // hoist the structure declaration
    959980                                        decl->location = cur->location;
    960981                                        * out++ = decl;
     982
     983                                        // need to remember the cases where a declaration contains an anonymous aggregate definition
     984                                        extracted = true;
     985                                        assert( extr->type );
     986                                        if ( extr->type->kind == TypeData::Aggregate ) {
     987                                                anon = extr->type->aggregate.anon;
     988                                        } else if ( extr->type->kind == TypeData::Enum ) {
     989                                                // xxx - is it useful to have an implicit anonymous enum member?
     990                                                anon = extr->type->enumeration.anon;
     991                                        }
    961992                                } // if
    962993                                delete extr;
     
    965996                        Declaration * decl = cur->build();
    966997                        if ( decl ) {
    967                                 decl->location = cur->location;
    968                                 * out++ = decl;
     998                                // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.:
     999                                // struct S {
     1000                                //   struct T { int x; }; // no anonymous member
     1001                                //   struct { int y; };   // anonymous member
     1002                                //   struct T;            // anonymous member
     1003                                // };
     1004                                if ( ! (extracted && decl->name == "" && ! anon) ) {
     1005                                        decl->location = cur->location;
     1006                                        * out++ = decl;
     1007                                }
    9691008                        } // if
    9701009                } catch( SemanticErrorException &e ) {
     
    9781017} // buildList
    9791018
     1019// currently only builds assertions, function parameters, and return values
    9801020void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
    9811021        SemanticErrorException errors;
     
    9851025                try {
    9861026                        Declaration * decl = cur->build();
    987                         if ( decl ) {
    988                                 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
    989                                         dwt->location = cur->location;
    990                                         * out++ = dwt;
    991                                 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
    992                                         StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    993                                         auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
    994                                         obj->location = cur->location;
    995                                         * out++ = obj;
    996                                         delete agg;
    997                                 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
    998                                         UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    999                                         auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
    1000                                         obj->location = cur->location;
    1001                                         * out++ = obj;
    1002                                 } // if
     1027                        assert( decl );
     1028                        if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
     1029                                dwt->location = cur->location;
     1030                                * out++ = dwt;
     1031                        } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
     1032                                // e.g., int foo(struct S) {}
     1033                                StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name );
     1034                                auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
     1035                                obj->location = cur->location;
     1036                                * out++ = obj;
     1037                                delete agg;
     1038                        } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
     1039                                // e.g., int foo(union U) {}
     1040                                UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name );
     1041                                auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
     1042                                obj->location = cur->location;
     1043                                * out++ = obj;
     1044                        } else if ( EnumDecl * agg = dynamic_cast< EnumDecl * >( decl ) ) {
     1045                                // e.g., int foo(enum E) {}
     1046                                EnumInstType * inst = new EnumInstType( Type::Qualifiers(), agg->name );
     1047                                auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
     1048                                obj->location = cur->location;
     1049                                * out++ = obj;
    10031050                        } // if
    10041051                } catch( SemanticErrorException &e ) {
  • src/Parser/ParseNode.h

    rfc20514 r777ed2b  
    231231        static DeclarationNode * newForall( DeclarationNode * );
    232232        static DeclarationNode * newFromTypedef( const std::string * );
     233        static DeclarationNode * newFromGlobalScope();
     234        static DeclarationNode * newQualifiedType( DeclarationNode *, DeclarationNode * );
    233235        static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body );
    234236        static DeclarationNode * newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
  • src/Parser/TypeData.cc

    rfc20514 r777ed2b  
    3737          case Reference:
    3838          case EnumConstant:
     39          case GlobalScope:
    3940                // nothing else to initialize
    4041                break;
     
    6263                enumeration.constants = nullptr;
    6364                enumeration.body = false;
     65                enumeration.anon = false;
    6466                break;
    6567          case Aggregate:
     
    7375                aggregate.tagged = false;
    7476                aggregate.parent = nullptr;
     77                aggregate.anon = false;
    7578                break;
    7679          case AggregateInst:
     
    98101          case Builtin:
    99102                // builtin = new Builtin_t;
     103                case Qualified:
     104                qualified.parent = nullptr;
     105                qualified.child = nullptr;
    100106                break;
    101107        } // switch
     
    112118          case Reference:
    113119          case EnumConstant:
     120          case GlobalScope:
    114121                // nothing to destroy
    115122                break;
     
    165172                // delete builtin;
    166173                break;
     174          case Qualified:
     175                delete qualified.parent;
     176                delete qualified.child;
    167177        } // switch
    168178} // TypeData::~TypeData
     
    180190          case Pointer:
    181191          case Reference:
     192          case GlobalScope:
    182193                // nothing else to copy
    183194                break;
     
    207218                newtype->aggregate.fields = maybeClone( aggregate.fields );
    208219                newtype->aggregate.body = aggregate.body;
     220                newtype->aggregate.anon = aggregate.anon;
    209221                newtype->aggregate.tagged = aggregate.tagged;
    210222                newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
     
    219231                newtype->enumeration.constants = maybeClone( enumeration.constants );
    220232                newtype->enumeration.body = enumeration.body;
     233                newtype->enumeration.anon = enumeration.anon;
    221234                break;
    222235          case Symbolic:
     
    238251                newtype->builtintype = builtintype;
    239252                break;
     253                case Qualified:
     254                newtype->qualified.parent = maybeClone( qualified.parent );
     255                newtype->qualified.child = maybeClone( qualified.child );
     256                break;
    240257        } // switch
    241258        return newtype;
     
    406423        } // switch
    407424} // TypeData::print
     425
     426const std::string * TypeData::leafName() const {
     427        switch ( kind ) {
     428          case Unknown:
     429          case Pointer:
     430          case Reference:
     431          case EnumConstant:
     432          case GlobalScope:
     433          case Array:
     434          case Basic:
     435          case Function:
     436          case AggregateInst:
     437          case Tuple:
     438          case Typeof:
     439          case Builtin:
     440                assertf(false, "Tried to get leaf name from kind without a name: %d", kind);
     441                break;
     442          case Aggregate:
     443                return aggregate.name;
     444          case Enum:
     445                return enumeration.name;
     446          case Symbolic:
     447          case SymbolicInst:
     448                return symbolic.name;
     449          case Qualified:
     450                return qualified.child->leafName();
     451        } // switch
     452        assert(false);
     453}
    408454
    409455
     
    465511                return new EnumInstType( buildQualifiers( td ), "" );
    466512          case TypeData::SymbolicInst:
    467                 return buildSymbolicInst( td );;
     513                return buildSymbolicInst( td );
    468514          case TypeData::Tuple:
    469515                return buildTuple( td );
     
    480526                        return new VarArgsType( buildQualifiers( td ) );
    481527                }
     528          case TypeData::GlobalScope:
     529                return new GlobalScopeType();
     530                case TypeData::Qualified:
     531                return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) );
    482532          case TypeData::Symbolic:
    483533          case TypeData::Enum:
     
    485535                assert( false );
    486536        } // switch
     537
    487538        return nullptr;
    488539} // typebuild
     
    893944        assert( td->kind == TypeData::Function );
    894945        FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
    895         buildList( td->function.params, ft->get_parameters() );
    896         buildForall( td->forall, ft->get_forall() );
     946        buildList( td->function.params, ft->parameters );
     947        buildForall( td->forall, ft->forall );
    897948        if ( td->base ) {
    898949                switch ( td->base->kind ) {
    899950                  case TypeData::Tuple:
    900                         buildList( td->base->tuple, ft->get_returnVals() );
     951                        buildList( td->base->tuple, ft->returnVals );
    901952                        break;
    902953                  default:
  • src/Parser/TypeData.h

    rfc20514 r777ed2b  
    2727struct TypeData {
    2828        enum Kind { Basic, Pointer, Array, Reference, Function, Aggregate, AggregateInst, Enum, EnumConstant, Symbolic,
    29                                 SymbolicInst, Tuple, Typeof, Builtin, Unknown };
     29                                SymbolicInst, Tuple, Typeof, Builtin, GlobalScope, Qualified, Unknown };
    3030
    3131        struct Aggregate_t {
     
    3636                DeclarationNode * fields;
    3737                bool body;
     38                bool anon;
    3839
    3940                bool tagged;
     
    5758                DeclarationNode * constants;
    5859                bool body;
     60                bool anon;
    5961        };
    6062
     
    7577        };
    7678
     79        struct Qualified_t { // qualified type S.T
     80                TypeData * parent;
     81                TypeData * child;
     82        };
     83
    7784        CodeLocation location;
    7885
     
    8895        DeclarationNode * forall;
    8996
    90         // Basic_t basic;
    9197        Aggregate_t aggregate;
    9298        AggInst_t aggInst;
    9399        Array_t array;
    94100        Enumeration_t enumeration;
    95         // Variable_t variable;
    96101        Function_t function;
    97102        Symbolic_t symbolic;
     103        Qualified_t qualified;
    98104        DeclarationNode * tuple;
    99105        ExpressionNode * typeexpr;
     
    103109        void print( std::ostream &, int indent = 0 ) const;
    104110        TypeData * clone() const;
     111
     112        const std::string * leafName() const;
    105113};
    106114
  • src/Parser/parser.yy

    rfc20514 r777ed2b  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Jul 11 11:20:51 2018
    13 // Update Count     : 3738
     12// Last Modified On : Wed Jul 11 11:55:24 2018
     13// Update Count     : 3739
    1414//
    1515
     
    18251825                { $$ = DeclarationNode::newFromTypedef( $1 ); }
    18261826        | '.' TYPEDEFname
    1827                 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
     1827                { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), DeclarationNode::newFromTypedef( $2 ) ); }
    18281828        | type_name '.' TYPEDEFname
    1829                 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
     1829                { $$ = DeclarationNode::newQualifiedType( $1, DeclarationNode::newFromTypedef( $3 ) ); }
    18301830        | typegen_name
    18311831        | '.' typegen_name
    1832                 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
     1832                { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), $2 ); }
    18331833        | type_name '.' typegen_name
    1834                 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
     1834                { $$ = DeclarationNode::newQualifiedType( $1, $3 ); }
    18351835        ;
    18361836
     
    18631863                { forall = false; }                                                             // reset
    18641864          '{' field_declaration_list_opt '}' type_parameters_opt
    1865                 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), $7, $5, true )->addQualifiers( $2 ); }
     1865                { $$ = DeclarationNode::newAggregate( $1, nullptr, $7, $5, true )->addQualifiers( $2 ); }
    18661866        | aggregate_key attribute_list_opt no_attr_identifier fred
    18671867                {
     
    18731873        | aggregate_key attribute_list_opt type_name fred
    18741874                {
    1875                         typedefTable.makeTypedef( *$3->type->symbolic.name, forall | typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef
     1875                        // for type_name can be a qualified type name S.T, in which case only the last name in the chain needs a typedef (other names in the chain should already have one)
     1876                        typedefTable.makeTypedef( *$3->type->leafName(), forall | typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef
    18761877                        forall = false;                                                         // reset
    18771878                }
     
    19361937                { distExt( $3 ); $$ = distAttr( $2, $3 ); }             // mark all fields in list
    19371938        | typedef_declaration ';'                                                       // CFA
    1938                 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }
    19391939        | cfa_field_declaring_list ';'                                          // CFA, new style field declaration
    19401940        | EXTENSION cfa_field_declaring_list ';'                        // GCC
    19411941                { distExt( $2 ); $$ = $2; }                                             // mark all fields in list
    19421942        | cfa_typedef_declaration ';'                                           // CFA
    1943                 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }
    19441943        | static_assert                                                                         // C11
    19451944        ;
     
    19901989enum_type:                                                                                              // enum
    19911990        ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
    1992                 { $$ = DeclarationNode::newEnum( new string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }
     1991                { $$ = DeclarationNode::newEnum( nullptr, $4, true )->addQualifiers( $2 ); }
    19931992        | ENUM attribute_list_opt no_attr_identifier
    19941993                { typedefTable.makeTypedef( *$3 ); }
Note: See TracChangeset for help on using the changeset viewer.