Changeset f238fcc2 for src


Ignore:
Timestamp:
Mar 21, 2022, 3:17:37 PM (2 years ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
4390fb6
Parents:
3e54399
Message:

Enable numeric type for typed enum

Location:
src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r3e54399 rf238fcc2  
    14961496                getAccept1< ast::type, decltype( old->child ) >( old->child )
    14971497
    1498 #       define GET_ACCEPT_E(child, type) \
    1499                 getAccept1< ast::type, decltype( old->base ) >( old->base )
    15001498
    15011499        template<typename NewT, typename OldC>
     
    15131511#       define GET_ACCEPT_V(child, type) \
    15141512                getAcceptV< ast::type, decltype( old->child ) >( old->child )
     1513
     1514#       define GET_ACCEPT_E(child, type) \
     1515                getAccept1< ast::type, decltype( old->base ) >( old->base )
    15151516
    15161517        template<typename NewT, typename OldC>
     
    17141715        }
    17151716
    1716         // Marker
    17171717        // Convert SynTree::EnumDecl to AST::EnumDecl
    17181718        virtual void visit( const EnumDecl * old ) override final {
     
    17231723                        GET_ACCEPT_V(attributes, Attribute),
    17241724                        { old->linkage.val },
    1725                         old->base? GET_ACCEPT_E(base, Type) : nullptr,
     1725                        GET_ACCEPT_1(base, Type),
    17261726                        old->enumValues
    17271727                );
  • src/CodeGen/GenType.cc

    r3e54399 rf238fcc2  
    253253
    254254        void GenType::postvisit( EnumInstType * enumInst ) {
    255                 typeString = enumInst->name + " " + typeString;
    256                 if ( options.genC ) typeString = "enum " + typeString;
     255                if ( enumInst->baseEnum->base
     256                && dynamic_cast<BasicType *>(enumInst->baseEnum->base)
     257                && dynamic_cast<BasicType *>(enumInst->baseEnum->base)->kind != BasicType::Kind::SignedInt) {
     258                        typeString = genType(enumInst->baseEnum->base, "", options) + typeString;
     259                } else {
     260                        typeString = enumInst->name + " " + typeString;
     261                        if ( options.genC ) {
     262                                typeString = "enum " + typeString;
     263                        }
     264                }
    257265                handleQualifiers( enumInst );
    258266        }
  • src/Parser/TypeData.cc

    r3e54399 rf238fcc2  
    918918EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
    919919        assert( td->kind == TypeData::Enum );
    920         Type* baseType = td->base ? typebuild(td->base) : nullptr;
     920        Type * baseType = td->base ? typebuild(td->base) : nullptr;
    921921        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
     922        buildList( td->enumeration.constants, ret->get_members() );
    923923        list< Declaration * >::iterator members = ret->get_members().begin();
    924924        for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
     
    926926                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
    927927                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
     928                } else {
     929                        if ( baseType && (!dynamic_cast<BasicType *>(baseType) || !dynamic_cast<BasicType *>(baseType)->isWholeNumber())) {
     930                                SemanticError( td->location, "A non whole number enum value decl must be explicitly initialized." );
     931                        }
    928932                } // if
    929933        } // for
  • src/ResolvExpr/ConversionCost.cc

    r3e54399 rf238fcc2  
    333333                } else if ( dynamic_cast< const EnumInstType * >( dest ) ) {
    334334                        // xxx - not positive this is correct, but appears to allow casting int => enum
    335                         cost = Cost::unsafe;
     335                        // TODO
     336                        EnumDecl * decl = dynamic_cast< const EnumInstType * >( dest )->baseEnum;
     337                        if ( decl->base ) {
     338                                cost = Cost::infinity;
     339                        } else {
     340                                cost = Cost::unsafe;
     341                        } // if
    336342                } // if
    337343                // no cases for zero_t/one_t because it should not be possible to convert int, etc. to zero_t/one_t.
     
    610616        } else if ( dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    611617                // xxx - not positive this is correct, but appears to allow casting int => enum
    612                 cost = Cost::unsafe;
     618                const ast::EnumDecl * decl = (dynamic_cast< const ast::EnumInstType * >( dst ))->base.get();
     619                if ( decl->base ) {
     620                        cost = Cost::infinity;
     621                } else {
     622                        cost = Cost::unsafe;
     623                } // if
    613624        }
    614625}
  • src/SynTree/BasicType.cc

    r3e54399 rf238fcc2  
    2929}
    3030
     31bool BasicType::isWholeNumber() const {
     32        return kind == Bool ||
     33                kind ==Char ||
     34                kind == SignedChar ||
     35                kind == UnsignedChar ||
     36                kind == ShortSignedInt ||
     37                kind == ShortUnsignedInt ||
     38                kind == SignedInt ||
     39                kind == UnsignedInt ||
     40                kind == LongSignedInt ||
     41                kind == LongUnsignedInt ||
     42                kind == LongLongSignedInt ||
     43                kind ==LongLongUnsignedInt ||
     44                kind == SignedInt128 ||
     45                kind == UnsignedInt128;
     46}
     47
    3148bool BasicType::isInteger() const {
    3249        return kind <= UnsignedInt128;
  • src/SynTree/Type.h

    r3e54399 rf238fcc2  
    268268        virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); }
    269269        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
    270 
     270        bool isWholeNumber() const;
    271271        bool isInteger() const;
    272272};
Note: See TracChangeset for help on using the changeset viewer.