Changeset 90e683b


Ignore:
Timestamp:
Feb 3, 2025, 11:46:55 AM (8 weeks ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
54f70c6
Parents:
bbbff10
Message:

I set out to do a enum rework. It ended up being much the same and I unwound the core rework. But I hope the new names are a bit clearer and other minor fixes are helpful, so I am keeping those.

Location:
src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/AST/Decl.cpp

    rbbbff10 r90e683b  
    169169}
    170170
    171 bool EnumDecl::isTyped() const { return base; }
    172 
    173 bool EnumDecl::isOpaque() const { return isCfa && !isTyped(); }
    174 
    175171}
    176172
  • TabularUnified src/AST/Decl.hpp

    rbbbff10 r90e683b  
    306306enum class EnumAttribute{ Value, Posn, Label };
    307307
    308 /// enum declaration `enum Foo { ... };`
     308/// enum declaration `enum Foo { ... };` or `enum(...) Foo { ... };`
    309309class EnumDecl final : public AggregateDecl {
    310310public:
     
    317317        std::vector< ast::ptr<ast::EnumInstType>> inlinedDecl; // child enums
    318318
     319        bool is_c_enum     () const { return !isCfa; }
     320        bool is_opaque_enum() const { return isCfa && nullptr == base; }
     321        bool is_typed_enum () const { return isCfa && nullptr != base; }
     322
    319323        EnumDecl( const CodeLocation& loc, const std::string& name, bool isCfa = false,
    320324                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall,
     
    331335        const char * typeString() const override { return aggrString( Enum ); }
    332336
    333         bool isTyped() const;
    334         bool isOpaque() const;
    335337private:
    336338        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
  • TabularUnified src/AST/Util.cpp

    rbbbff10 r90e683b  
    8585        // Check that `type->returns` corresponds with `decl->returns`.
    8686        assert( type->returns.size() == decl->returns.size() );
     87}
     88
     89/// Check that an enumeration has not been made with an inconsistent spec.
     90void isEnumerationConsistent( const EnumDecl * node ) {
     91        if ( node->is_c_enum() ) {
     92                assert( nullptr == node->base );
     93        }
    8794}
    8895
     
    135142                previsit( (const ParseNode *)node );
    136143                functionDeclMatchesType( node );
     144        }
     145
     146        void previsit( const EnumDecl * node ) {
     147                previsit( (const ParseNode *)node );
     148                isEnumerationConsistent( node );
    137149        }
    138150
  • TabularUnified src/InitTweak/GenInit.cpp

    rbbbff10 r90e683b  
    164164                                ast::ObjectDecl * arrayDimension = nullptr;
    165165
    166                                 const ast::TypeExpr * ty = dynamic_cast< const ast::TypeExpr * >( arrayType->dimension.get() );
    167                                 if ( ty ) {
     166                                if ( auto ty = dynamic_cast< const ast::TypeExpr * >( arrayType->dimension.get() ) ) {
    168167                                        auto inst = ty->type.as<ast::EnumInstType>();
    169                                         if ( inst ) {
    170                                                 if ( inst->base->isCfa ) {
    171                                                         arrayDimension = new ast::ObjectDecl(
     168                                        if ( inst && !inst->base->is_c_enum() ) {
     169                                                arrayDimension = new ast::ObjectDecl(
     170                                                        arrayType->dimension->location,
     171                                                        dimensionName.newName(),
     172                                                        new ast::BasicType( ast::BasicKind::UnsignedChar ),
     173                                                        new ast::SingleInit(
    172174                                                                arrayType->dimension->location,
    173                                                                 dimensionName.newName(),
    174                                                                 new ast::BasicType( ast::BasicKind::UnsignedChar ),
    175                                                                 new ast::SingleInit(
    176                                                                         arrayType->dimension->location,
    177                                                                         ast::ConstantExpr::from_int( arrayType->dimension->location, inst->base->members.size() )
    178                                                                 )
    179                                                         );
    180                                                         // return arrayType;
    181                                                 }
     175                                                                ast::ConstantExpr::from_int( arrayType->dimension->location, inst->base->members.size() )
     176                                                        )
     177                                                );
    182178                                        }
    183179                                }
  • TabularUnified src/Parser/TypeData.cpp

    rbbbff10 r90e683b  
    14821482                object->isHidden = ast::EnumDecl::EnumHiding::Hide == ret->hide;
    14831483                object->isMember = true;
    1484                 if ( ret->isOpaque() && cur->has_enumeratorValue() ) {
     1484                if ( ret->is_opaque_enum() && cur->has_enumeratorValue() ) {
    14851485                        SemanticError( td->location, "Opague cannot have an explicit initializer value." );
    14861486                } else if ( cur->has_enumeratorValue() ) {
    14871487                        ast::Expr * initValue;
    1488                         if ( ret->isCfa && ret->base ) {
     1488                        if ( ret->is_typed_enum() ) {
    14891489                                CodeLocation location = cur->enumeratorValue->location;
    14901490                                initValue = new ast::CastExpr( location, maybeMoveBuild( cur->consume_enumeratorValue() ), ret->base );
  • TabularUnified src/ResolvExpr/CastCost.cpp

    rbbbff10 r90e683b  
    5353                void postvisit( const ast::EnumInstType * enumInst ) {
    5454                        cost = conversionCost( enumInst, dst, srcIsLvalue, symtab, env );
    55                         if ( enumInst->base->isTyped() ) {
    56                                 auto baseConversionCost = 
     55                        if ( enumInst->base->is_typed_enum() ) {
     56                                auto baseConversionCost =
    5757                                        castCost( enumInst->base->base, dst, srcIsLvalue, symtab, env );
    58                                 cost = baseConversionCost < cost? baseConversionCost: cost;
     58                                cost = baseConversionCost < cost ? baseConversionCost : cost;
    5959                        }
    6060                        static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) };
    6161                        Cost intCost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    6262                        intCost.incSafe();
    63                         cost = intCost < cost? intCost: cost;
     63                        cost = intCost < cost ? intCost : cost;
    6464                }
    6565
  • TabularUnified src/ResolvExpr/CommonType.cpp

    rbbbff10 r90e683b  
    386386                } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
    387387                        const ast::EnumDecl* enumDecl = enumInst->base;
    388                         if ( !enumDecl->isCfa ) {
     388                        if ( enumDecl->is_c_enum() ) {
    389389                                ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ];
    390390                                if (
     
    654654                                result = param;
    655655                        }
    656                 } else if ( param->base && !param->base->isCfa ) {
     656                } else if ( param->base && param->base->is_c_enum() ) {
    657657                        auto basicType = new ast::BasicType( ast::BasicKind::UnsignedInt );
    658658                        result = commonType( basicType, type2, tenv, need, have, open, widen);
  • TabularUnified src/ResolvExpr/ConversionCost.cpp

    rbbbff10 r90e683b  
    246246                }
    247247                if (const ast::EnumInstType * srcAsInst = dynamic_cast< const ast::EnumInstType * >( src )) {
    248                         if (srcAsInst->base && !srcAsInst->base->isCfa) {
     248                        if ( srcAsInst->base && srcAsInst->base->is_c_enum() ) {
    249249                                static const ast::BasicType* integer = new ast::BasicType( ast::BasicKind::UnsignedInt );
    250250                                return ast::Pass<ConversionCost>::read( integer, dst, srcIsLvalue, symtab, env, conversionCost );
     
    324324                conversionCostFromBasicToBasic( basicType, dstAsBasic );
    325325        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    326                 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isCfa ) {
     326                if ( dstAsEnumInst->base && dstAsEnumInst->base->is_c_enum() ) {
    327327                        cost = Cost::safe;
    328328                }
     
    405405        if ( auto dstInst = dynamic_cast<const ast::EnumInstType *>( dst ) ) {
    406406                cost = enumCastCost(inst, dstInst, symtab, env);
    407         } else if ( !inst->base->isCfa ) {
     407        } else if ( inst->base->is_c_enum() ) {
    408408                static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) };
    409409                cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
     
    455455}
    456456
    457 void ConversionCost::postvisit( const ast::VarArgsType * varArgsType ) {
    458         (void)varArgsType;
     457void ConversionCost::postvisit( const ast::VarArgsType * ) {
    459458        if ( dynamic_cast< const ast::VarArgsType * >( dst ) ) {
    460459                cost = Cost::zero;
     
    462461}
    463462
    464 void ConversionCost::postvisit( const ast::ZeroType * zeroType ) {
    465         (void)zeroType;
     463void ConversionCost::postvisit( const ast::ZeroType * ) {
    466464        if ( dynamic_cast< const ast::ZeroType * >( dst ) ) {
    467465                cost = Cost::zero;
     
    487485                // assuming 0p is supposed to be used for pointers?
    488486        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    489                 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isCfa ) {
     487                if ( dstAsEnumInst->base && dstAsEnumInst->base->is_c_enum() ) {
    490488                        cost = Cost::safe;
    491489                }
     
    493491}
    494492
    495 void ConversionCost::postvisit( const ast::OneType * oneType ) {
    496         (void)oneType;
     493void ConversionCost::postvisit( const ast::OneType * ) {
    497494        if ( dynamic_cast< const ast::OneType * >( dst ) ) {
    498495                cost = Cost::zero;
     
    508505                }
    509506        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    510                 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isCfa ) {
     507                if ( dstAsEnumInst->base && dstAsEnumInst->base->is_c_enum() ) {
    511508                        cost = Cost::safe;
    512509                }
  • TabularUnified src/ResolvExpr/ResolveTypeof.cpp

    rbbbff10 r90e683b  
    6262                        // replace basetypeof(<enum>) by int
    6363                        auto enumInst = newType.as< ast::EnumInstType >();
    64                         if ( enumInst && (!enumInst->base || !enumInst->base->isCfa) ) {
     64                        if ( enumInst && (!enumInst->base || enumInst->base->is_c_enum() ) ) {
    6565                                newType = new ast::BasicType(
    6666                                        ast::BasicKind::SignedInt, newType->qualifiers, copy(newType->attributes) );
     
    144144
    145145        auto enumInst = decl->type.as<ast::EnumInstType>();
    146         if ( enumInst && enumInst->base->isCfa ) {
     146        if ( enumInst && !enumInst->base->is_c_enum() ) {
    147147                if ( auto init = decl->init.as<ast::SingleInit>() ) {
    148148                        if ( auto initExpr = init->value.as<ast::ConstantExpr>() ) {
    149149                                if ( initExpr->result.as<ast::ZeroType>() ) {
    150                                         auto newInit = new ast::SingleInit( init->location, 
     150                                        auto newInit = new ast::SingleInit( init->location,
    151151                                                ast::UntypedExpr::createCall( init->location, "lowerBound", {} )
    152152                                        );
  • TabularUnified src/Validate/ImplementEnumFunc.cpp

    rbbbff10 r90e683b  
    6161        ast::FunctionDecl* genLabelProto() const;
    6262        ast::FunctionDecl* genValueProto() const;
    63         ast::FunctionDecl* genQuasiValueProto() const;
    6463        ast::FunctionDecl* genTypeNameProto() const;
    6564
     
    206205
    207206ast::FunctionDecl* EnumAttrFuncGenerator::genValueProto() const {
    208         assert (decl->isTyped());
     207        assert( decl->is_typed_enum() );
    209208        return genProto(
    210209                "value",
     
    414413void EnumAttrFuncGenerator::genTypedEnumFunction(const ast::EnumAttribute attr) {
    415414        if (attr == ast::EnumAttribute::Value) {
    416                 if ( !decl->isTyped() ) return;
     415                if ( !decl->is_typed_enum() ) return;
    417416                std::vector<ast::ptr<ast::Init>> inits = genValueInit();
    418417                ast::ObjectDecl* arrayProto =
     
    483482
    484483void ImplementEnumFunc::previsit(const ast::EnumDecl* enumDecl) {
    485         if (!enumDecl->body || !enumDecl->isCfa) return;
     484        if ( !enumDecl->body || enumDecl->is_c_enum() ) return;
    486485        ast::EnumInstType enumInst(enumDecl->name);
    487486        enumInst.base = enumDecl;
Note: See TracChangeset for help on using the changeset viewer.