Changeset 1e30df7 for src


Ignore:
Timestamp:
Oct 18, 2022, 5:48:13 PM (19 months ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT, ast-experimental, master
Children:
5408b59
Parents:
a55472cc
Message:

Supports inline enums

Location:
src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    ra55472cc r1e30df7  
    16151615                        { old->get_funcSpec().val }
    16161616                );
     1617                decl->enumInLine = old->enumInLine;
    16171618                cache.emplace(old, decl);
    16181619                assert(cache.find( old ) != cache.end());
  • src/AST/Decl.hpp

    ra55472cc r1e30df7  
    105105        ptr<Init> init;
    106106        ptr<Expr> bitfieldWidth;
     107        bool enumInLine = false; // A flag vairable to tell the compile:
     108        // this is not a real object declaration. It is a place holder for
     109        // a set of enum value (ObjectDecl).
    107110
    108111        ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
  • src/Parser/DeclarationNode.cc

    ra55472cc r1e30df7  
    297297        } // if
    298298} // DeclarationNode::newEnumValueGeneric
     299
     300DeclarationNode * DeclarationNode::newEnumInLine( const string name ) {
     301        DeclarationNode * newnode = newName( new std::string(name) );
     302        newnode->enumInLine = true;
     303        return newnode;
     304}
    299305
    300306DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
  • src/Parser/ParseNode.h

    ra55472cc r1e30df7  
    240240        static DeclarationNode * newEnumConstant( const std::string * name, ExpressionNode * constant );
    241241        static DeclarationNode * newEnumValueGeneric( const std::string * name, InitializerNode * init );
     242        static DeclarationNode * newEnumInLine( const std::string name );
    242243        static DeclarationNode * newName( const std::string * );
    243244        static DeclarationNode * newFromTypeGen( const std::string *, ExpressionNode * params );
     
    339340
    340341        bool inLine = false;
     342        bool enumInLine = false;
    341343        Type::FuncSpecifiers funcSpecs;
    342344        Type::StorageClasses storageClasses;
  • src/Parser/TypeData.cc

    ra55472cc r1e30df7  
    924924        list< Declaration * >::iterator members = ret->get_members().begin();
    925925        for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
     926                if ( cur->enumInLine ) {
     927                        // Tell the compiler this is a inline value placeholder
     928                        ObjectDecl * member = dynamic_cast< ObjectDecl* >(* members);
     929                        member->enumInLine = true;
     930                }
    926931                if ( ret->isTyped && !ret->base && cur->has_enumeratorValue() ) {
    927932                        SemanticError( td->location, "Enumerator of enum(void) cannot have an explicit initializer value." );
  • src/Parser/parser.yy

    ra55472cc r1e30df7  
    26012601                { $$ = DeclarationNode::newEnumValueGeneric( $1, $2 ); }
    26022602        | INLINE type_name
    2603                 { $$ = DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ); }
     2603                { $$ = DeclarationNode::newEnumInLine( *$2->type->symbolic.name ); }
    26042604        | enumerator_list ',' identifier_or_type_name enumerator_value_opt
    26052605                { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( $3, $4 ) ); }
  • src/SynTree/Declaration.h

    ra55472cc r1e30df7  
    121121        Initializer * init;
    122122        Expression * bitfieldWidth;
     123        bool enumInLine = false;
    123124
    124125        ObjectDecl( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression * bitfieldWidth, Type * type, Initializer * init,
  • src/Validate/LinkReferenceToTypes.cpp

    ra55472cc r1e30df7  
    203203        }
    204204
     205        // The following section
     206        auto mut = ast::mutate( decl );
     207        std::vector<ast::ptr<ast::Decl>> buffer;
     208        for ( auto it = decl->members.begin(); it != decl->members.end(); ++it) {
     209                auto member = (*it).as<ast::ObjectDecl>();
     210                if ( member->enumInLine ) {
     211                        auto targetEnum = symtab.lookupEnum( member->name );
     212                        if (targetEnum) {                       
     213                                for (auto singleMamber : targetEnum->members) {
     214                                        auto tm = singleMamber.as<ast::ObjectDecl>();
     215                                        auto t = new ast::ObjectDecl(
     216                                                member->location, // use the "inline" location
     217                                                tm->name,
     218                                                new ast::EnumInstType( decl, ast::CV::Const ),
     219                                                // Construct a new EnumInstType as the type
     220                                                tm->init,
     221                                                tm->storage,
     222                                                tm->linkage,
     223                                                tm->bitfieldWidth,
     224                                                {}, // enum member doesn't have attribute
     225                                                tm->funcSpec
     226                                        );
     227                                        buffer.push_back(t);
     228                                }
     229                        }
     230                } else {
     231                        buffer.push_back( *it );
     232                }
     233        }
     234        mut->members = buffer;
     235        decl = mut;
     236
    205237        ForwardEnumsType::iterator fwds = forwardEnums.find( decl->name );
    206238        if ( fwds != forwardEnums.end() ) {
Note: See TracChangeset for help on using the changeset viewer.