Changeset f135b50


Ignore:
Timestamp:
Feb 28, 2022, 3:41:44 AM (7 months ago)
Author:
JiadaL <j82liang@…>
Branches:
enum, master, pthread-emulation, qualifiedEnum
Children:
786c438
Parents:
a8ef59e
Message:

The compiler is now trying to pass the value of enum const to code gen; but it won't work cause we must be able to evaluate the const_expr in compiler time. It is not currently passed as a Expression but won't be able to evaluate at compile time

Location:
src
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    ra8ef59e rf135b50  
    276276                decl->parent = get<AggregateDecl>().accept1( node->parent );
    277277                declPostamble( decl, node );
    278                 return nullptr;
     278                return nullptr; // ??
    279279        }
    280280
     
    305305        }
    306306
    307         const ast::Decl * visit( const ast::EnumDecl * node ) override final {
     307        const ast::Decl * visit( const ast::EnumDecl * node ) override final { // Marker: what is this for?
     308        // Called in ConverterNewToOld
    308309                if ( inCache( node ) ) return nullptr;
    309310                auto decl = new EnumDecl(
     
    312313                        LinkageSpec::Spec( node->linkage.val )
    313314                );
    314                 return aggregatePostamble( decl, node );
     315                return aggregatePostamble( decl, node ); // Node info, including members, processed in aggregatePostamble
    315316        }
    316317
     
    14951496                getAccept1< ast::type, decltype( old->child ) >( old->child )
    14961497
     1498#       define GET_ACCEPT_E(child, type) \
     1499                getAccept1< ast::type, decltype( old->base ) >( old->base )
     1500
    14971501        template<typename NewT, typename OldC>
    14981502        std::vector< ast::ptr<NewT> > getAcceptV( const OldC& old ) {
     
    17101714        }
    17111715
     1716        // Marker
     1717        // Convert SynTree::EnumDecl to AST::EnumDecl
    17121718        virtual void visit( const EnumDecl * old ) override final {
    17131719                if ( inCache( old ) ) return;
     
    17161722                        old->name,
    17171723                        GET_ACCEPT_V(attributes, Attribute),
    1718                         { old->linkage.val }
     1724                        { old->linkage.val },
     1725                        old->base? GET_ACCEPT_E(base, Type) : nullptr,
     1726                        old->enumValues
    17191727                );
    17201728                cache.emplace( old, decl );
     
    17261734                decl->uniqueId   = old->uniqueId;
    17271735                decl->storage    = { old->storageClasses.val };
    1728 
    17291736                this->node = decl;
    17301737        }
  • src/AST/Decl.cpp

    ra8ef59e rf135b50  
    133133
    134134        auto it = enumValues.find( enumerator->name );
     135       
    135136        if ( it != enumValues.end() ) {
    136                 value = it->second;
     137                       
     138                // Handle typed enum by casting the value in (C++) compiler
     139                if ( base ) { // A typed enum
     140                        if ( const BasicType * bt = dynamic_cast<const BasicType *>(base) ) {
     141                                switch( bt->kind ) {
     142                                        case BasicType::Kind::Bool:     value = (bool) it->second; break;
     143                                        case BasicType::Kind::Char: value = (char) it->second; break;
     144                                        case BasicType::Kind::SignedChar: value = (signed char) it->second; break;
     145                                        case BasicType::Kind::UnsignedChar: value = (unsigned char) it->second; break;
     146                                        case BasicType::Kind::ShortSignedInt: value = (short signed int) it->second; break;
     147                                        case BasicType::Kind::SignedInt: value = (signed int) it->second; break;
     148                                        case BasicType::Kind::UnsignedInt: value = (unsigned int) it->second; break;
     149                                        case BasicType::Kind::LongSignedInt: value = (long signed int) it->second; break;
     150                                        case BasicType::Kind::LongUnsignedInt: value = (long unsigned int) it->second; break;
     151                                        case BasicType::Kind::LongLongSignedInt: value = (long long signed int) it->second; break;
     152                                        case BasicType::Kind::LongLongUnsignedInt: value = (long long unsigned int) it->second; break;
     153                                        // TODO: value should be able to handle long long unsigned int
     154
     155                                        default:
     156                                        value = it->second;
     157                                }
     158                        }
     159                } else {
     160                        value = it->second;
     161                }
     162
    137163                return true;
    138164        }
  • src/AST/Decl.hpp

    ra8ef59e rf135b50  
    302302class EnumDecl final : public AggregateDecl {
    303303public:
     304        Type * base;
     305
    304306        EnumDecl( const CodeLocation& loc, const std::string& name,
    305                 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
    306         : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
     307                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall, Type * base = nullptr,
     308                 std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() )
     309        : AggregateDecl( loc, name, std::move(attrs), linkage ), base(base), enumValues(enumValues) {}
    307310
    308311        /// gets the integer value for this enumerator, returning true iff value found
     312        // Maybe it is not used in producing the enum value
    309313        bool valueOf( const Decl * enumerator, long long& value ) const;
    310314
  • src/CodeGen/CodeGenerator.cc

    ra8ef59e rf135b50  
    274274        void CodeGenerator::postvisit( EnumDecl * enumDecl ) {
    275275                extension( enumDecl );
    276                 output << "enum ";
     276                output << "enum /* Marker CodeGenerator::EnumDecl */ ";
    277277                genAttributes( enumDecl->get_attributes() );
    278278
     
    291291                                if ( obj->get_init() ) {
    292292                                        output << " = ";
     293                                        // In progress
    293294                                        obj->get_init()->accept( *visitor );
    294295                                } // if
  • src/CodeGen/GenType.cc

    ra8ef59e rf135b50  
    254254        void GenType::postvisit( EnumInstType * enumInst ) {
    255255                typeString = enumInst->name + " " + typeString;
    256                 if ( options.genC ) typeString = "enum " + typeString;
     256                if ( options.genC ) typeString = "enum /* Marker GenType::EnumInstType */ " + typeString;
    257257                handleQualifiers( enumInst );
    258258        }
  • src/Common/Eval.cc

    ra8ef59e rf135b50  
    112112        }
    113113
    114         void postvisit( const ast::VariableExpr * expr ) {
     114        void postvisit( const ast::VariableExpr * expr ) { // No hit
    115115                if ( const ast::EnumInstType * inst = dynamic_cast<const ast::EnumInstType *>(expr->result.get()) ) {
    116116                        if ( const ast::EnumDecl * decl = inst->base ) {
  • src/Common/PassVisitor.impl.h

    ra8ef59e rf135b50  
    754754
    755755        // unlike structs, traits, and unions, enums inject their members into the global scope
     756        if ( node->base ) maybeAccept_impl( node->base, *this ); // Need this? Maybe not?
    756757        maybeAccept_impl( node->parameters, *this );
    757758        maybeAccept_impl( node->members   , *this );
  • src/Parser/DeclarationNode.cc

    ra8ef59e rf135b50  
    7878        delete variable.initializer;
    7979
    80         delete type;
     80//      delete type;
    8181        delete bitfieldWidth;
    8282
     
    253253} // DeclarationNode::newAggregate
    254254
    255 DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool ) {
     255DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed) {
    256256        DeclarationNode * newnode = new DeclarationNode;
    257257        newnode->type = new TypeData( TypeData::Enum );
     
    272272} // DeclarationNode::newName
    273273
    274 DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
     274DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) { // Marker
    275275        DeclarationNode * newnode = newName( name );
    276276        newnode->enumeratorValue.reset( constant );
     
    667667}
    668668
     669DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) {
     670        if ( o && o -> type)  {
     671                type->base= o->type;
     672        }
     673        delete o;
     674        return this;
     675}
     676
    669677DeclarationNode * DeclarationNode::addTypedef() {
    670678        TypeData * newtype = new TypeData( TypeData::Symbolic );
  • src/Parser/ParseNode.h

    ra8ef59e rf135b50  
    265265        DeclarationNode * addType( DeclarationNode * );
    266266        DeclarationNode * addTypedef();
     267        DeclarationNode * addEnumBase( DeclarationNode * );
    267268        DeclarationNode * addAssertions( DeclarationNode * );
    268269        DeclarationNode * addName( std::string * );
  • src/Parser/StatementNode.cc

    ra8ef59e rf135b50  
    366366} // maybe_build_compound
    367367
     368// Question
    368369Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
    369370        list< Expression * > out, in;
  • src/Parser/TypeData.cc

    ra8ef59e rf135b50  
    918918EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
    919919        assert( td->kind == TypeData::Enum );
    920         EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
    921         buildList( td->enumeration.constants, ret->get_members() );
     920        Type* baseType = td->base ? typebuild(td->base) : nullptr;
     921        EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage, baseType );
     922        buildList( td->enumeration.constants, ret->get_members() ); // enumConstant is both a node and a list
    922923        list< Declaration * >::iterator members = ret->get_members().begin();
    923         for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
     924        for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
    924925                if ( cur->has_enumeratorValue() ) {
    925926                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
     
    927928                } // if
    928929        } // for
    929         ret->set_body( td->enumeration.body );
     930        ret->set_body( td->enumeration.body ); // Boolean; if it has body
    930931        return ret;
    931932} // buildEnum
  • src/Parser/TypeData.h

    ra8ef59e rf135b50  
    132132                                                 Initializer * init = nullptr, std::list< class Attribute * > attributes = std::list< class Attribute * >() );
    133133FunctionType * buildFunction( const TypeData * );
     134Declaration * addEnumBase( Declaration *, const TypeData * );
    134135void buildKRFunction( const TypeData::Function_t & function );
    135136
  • src/Parser/parser.yy

    ra8ef59e rf135b50  
    22922292                        { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
    22932293                        // SemanticError( yylloc, "Typed enumeration is currently unimplemented." ); $$ = nullptr;
    2294                         $$ = DeclarationNode::newEnum( nullptr, $7, true, true ) ->addQualifiers( $5 );
     2294
     2295                        $$ = DeclarationNode::newEnum( nullptr, $7, true, true ) ->addQualifiers( $5 )  -> addEnumBase( $3 );
     2296                        // $$ = DeclarationNode::newEnum( nullptr, $7, true, true ) ->addQualifiers( $5 );
    22952297                }
    22962298        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt // Question: why attributes/qualifier after identifier
     
    23012303          '{' enumerator_list comma_opt '}'
    23022304                {
    2303                         $$ = DeclarationNode::newEnum( $6, $10, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 );
     2305                        $$ = DeclarationNode::newEnum( $6, $10, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 ) -> addEnumBase( $3 );
     2306                        // $$ = DeclarationNode::newEnum( $6, $10, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 );
    23042307                }
    23052308        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
     
    23072310                        if ( $3->storageClasses.val != 0 || $3->type->qualifiers.val != 0 ) { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
    23082311                        typedefTable.makeTypedef( *$6->name );
    2309                         $$ = DeclarationNode::newEnum( $6->name, $9, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 );
     2312                        $$ = DeclarationNode::newEnum( $6->name, $9, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 ) -> addEnumBase( $3 );
     2313                        // $$ = DeclarationNode::newEnum( $6->name, $9, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 );
    23102314                }
    23112315        | enum_type_nobody
  • src/SymTab/Demangle.cc

    ra8ef59e rf135b50  
    252252
    253253        void GenType::postvisit( EnumInstType * enumInst ) {
    254                 typeString = "enum " + enumInst->name + " " + typeString;
     254                typeString = "enum /* Marker demangle::EnumInstType */ " + enumInst->name + " " + typeString;
    255255                handleQualifiers( enumInst );
    256256        }
  • src/SynTree/Declaration.h

    ra8ef59e rf135b50  
    119119  public:
    120120        Type * type;
    121         Initializer * init;
     121        Initializer * init; // For Enum, the init is a pointer that contain the enum value; see Parser::TypeData::buildEnum
    122122        Expression * bitfieldWidth;
    123123
     
    335335        typedef AggregateDecl Parent;
    336336  public:
    337         EnumDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
     337        EnumDecl( const std::string & name,
     338         const std::list< Attribute * > & attributes = std::list< class Attribute * >(),
     339          LinkageSpec::Spec linkage = LinkageSpec::Cforall,
     340          Type * baseType = nullptr ) : Parent( name, attributes, linkage ) , base( baseType ){}
    338341        EnumDecl( const EnumDecl & other ) : Parent( other ) {}
    339342
     
    344347        virtual void accept( Visitor & v ) const override { v.visit( this ); }
    345348        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
    346   private:
     349        Type * base;
    347350        std::unordered_map< std::string, long long int > enumValues;
     351  private:
     352        // std::unordered_map< std::string, long long int > enumValues;
    348353        virtual const char * typeString() const override;
    349354};
  • src/SynTree/Visitor.h

    ra8ef59e rf135b50  
    3535        virtual void visit( UnionDecl * node ) { visit( const_cast<const UnionDecl *>(node) ); }
    3636        virtual void visit( const UnionDecl * aggregateDecl ) = 0;
    37         virtual void visit( EnumDecl * node ) { visit( const_cast<const EnumDecl *>(node) ); }
     37        virtual void visit( EnumDecl * node ) { visit( const_cast<const EnumDecl *>(node) ); } // Marker 1
    3838        virtual void visit( const EnumDecl * aggregateDecl ) = 0;
    3939        virtual void visit( TraitDecl * node ) { visit( const_cast<const TraitDecl *>(node) ); }
     
    190190        virtual void visit( UnionInstType * node ) { visit( const_cast<const UnionInstType *>(node) ); }
    191191        virtual void visit( const UnionInstType * aggregateUseType ) = 0;
    192         virtual void visit( EnumInstType * node ) { visit( const_cast<const EnumInstType *>(node) ); }
     192        virtual void visit( EnumInstType * node ) { visit( const_cast<const EnumInstType *>(node) ); } // Marker 2
    193193        virtual void visit( const EnumInstType * aggregateUseType ) = 0;
    194194        virtual void visit( TraitInstType * node ) { visit( const_cast<const TraitInstType *>(node) ); }
Note: See TracChangeset for help on using the changeset viewer.