Changeset 59c8dff


Ignore:
Timestamp:
Jan 19, 2024, 2:42:58 AM (20 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
f988834
Parents:
8b4faf6
Message:

Draft Implementation for enum position pesudo function (posE). EnumPosExpr is mostly irrelevant for now. It is used in development/code probing and will be removed later

Location:
src
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Expr.cpp

    r8b4faf6 r59c8dff  
    307307}
    308308
     309//
     310
     311// --- EnumPosExpr
     312EnumPosExpr::EnumPosExpr( const CodeLocation & loc, const EnumInstType * ty)
     313: Expr( loc, new BasicType{ BasicType::UnsignedInt }), type( ty ) {
     314        assert( ty );
     315}
     316
     317EnumPosExpr::EnumPosExpr( const CodeLocation & loc, const Expr * expr )
     318: Expr( loc, new BasicType{ BasicType::UnsignedInt }), expr(expr) {
     319        assert( expr );
     320}
     321
     322
    309323// --- LogicalExpr
    310324
  • src/AST/Expr.hpp

    r8b4faf6 r59c8dff  
    548548};
    549549
     550class EnumPosExpr final : public Expr {
     551public:
     552        ptr<EnumInstType> type;
     553        ptr<Expr> expr;
     554       
     555        EnumPosExpr( const CodeLocation & loc, const EnumInstType * ty );
     556        EnumPosExpr( const CodeLocation & loc, const Expr * expr );
     557        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
     558private:
     559        EnumPosExpr * clone() const override { return new EnumPosExpr{ *this }; }
     560        MUTATE_FRIEND
     561};
     562
    550563/// Variants of short-circuiting logical expression
    551564enum LogicalFlag { OrExpr, AndExpr };
  • src/AST/Fwd.hpp

    r8b4faf6 r59c8dff  
    8989class OffsetofExpr;
    9090class OffsetPackExpr;
     91class EnumPosExpr;
    9192class LogicalExpr;
    9293class ConditionalExpr;
  • src/AST/Node.cpp

    r8b4faf6 r59c8dff  
    232232template class ast::ptr_base< ast::OffsetPackExpr, ast::Node::ref_type::weak >;
    233233template class ast::ptr_base< ast::OffsetPackExpr, ast::Node::ref_type::strong >;
     234template class ast::ptr_base< ast::EnumPosExpr, ast::Node::ref_type::weak >;
     235template class ast::ptr_base< ast::EnumPosExpr, ast::Node::ref_type::strong >;
    234236template class ast::ptr_base< ast::LogicalExpr, ast::Node::ref_type::weak >;
    235237template class ast::ptr_base< ast::LogicalExpr, ast::Node::ref_type::strong >;
  • src/AST/Pass.hpp

    r8b4faf6 r59c8dff  
    191191        const ast::Expr *             visit( const ast::OffsetofExpr         * ) override final;
    192192        const ast::Expr *             visit( const ast::OffsetPackExpr       * ) override final;
     193        const ast::Expr *             visit( const ast::EnumPosExpr          * ) override final;
    193194        const ast::Expr *             visit( const ast::LogicalExpr          * ) override final;
    194195        const ast::Expr *             visit( const ast::ConditionalExpr      * ) override final;
  • src/AST/Pass.impl.hpp

    r8b4faf6 r59c8dff  
    14521452
    14531453//--------------------------------------------------------------------------
     1454// EnumPosExpr
     1455template< typename core_t>
     1456const ast::Expr * ast::Pass< core_t >::visit( const ast::EnumPosExpr * node ) {
     1457        VISIT_START( node );
     1458
     1459        if ( __visit_children() ) {
     1460                guard_symtab guard { *this };
     1461                maybe_accept( node, &EnumPosExpr::type );
     1462                maybe_accept( node, &EnumPosExpr::expr );
     1463        }
     1464
     1465        VISIT_END( Expr, node );
     1466}
     1467
     1468//--------------------------------------------------------------------------
    14541469// LogicalExpr
    14551470template< typename core_t >
  • src/AST/Print.cpp

    r8b4faf6 r59c8dff  
    11831183        }
    11841184
     1185        virtual const ast::Expr * visit( const ast::EnumPosExpr * node ) override final {
     1186                os << "Enum Position Expression on: ";
     1187                ++indent;
     1188                safe_print( node->type );
     1189                --indent;
     1190                postprint( node );
     1191                return node;
     1192        }
     1193
    11851194        virtual const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
    11861195                os << "Short-circuited operation (" << (node->isAnd ? "and" : "or") << ") on: ";
  • src/AST/Visitor.hpp

    r8b4faf6 r59c8dff  
    7979    virtual const ast::Expr *             visit( const ast::OffsetofExpr         * ) = 0;
    8080    virtual const ast::Expr *             visit( const ast::OffsetPackExpr       * ) = 0;
     81    virtual const ast::Expr *             visit( const ast::EnumPosExpr          * ) = 0;
    8182    virtual const ast::Expr *             visit( const ast::LogicalExpr          * ) = 0;
    8283    virtual const ast::Expr *             visit( const ast::ConditionalExpr      * ) = 0;
  • src/CodeGen/CodeGenerator.cpp

    r8b4faf6 r59c8dff  
    778778}
    779779
     780void CodeGenerator::postvisit( ast::EnumPosExpr const * expr ) {
     781        assertf( !options.genC, "EnumPosExpr should not reach code generation." );
     782        output << "__CFA_enumeration_pos(" << genType( expr->type, "", options ) << ")";
     783}
     784
    780785void CodeGenerator::postvisit( ast::LogicalExpr const * expr ) {
    781786        extension( expr );
  • src/CodeGen/CodeGenerator.hpp

    r8b4faf6 r59c8dff  
    7676        void postvisit( ast::OffsetPackExpr const * );
    7777        void postvisit( ast::LogicalExpr const * );
     78        void postvisit( ast::EnumPosExpr const * );
    7879        void postvisit( ast::ConditionalExpr const * );
    7980        void postvisit( ast::CommaExpr const * );
  • src/Common/CodeLocationTools.cpp

    r8b4faf6 r59c8dff  
    158158    macro(OffsetPackExpr, Expr) \
    159159    macro(LogicalExpr, Expr) \
     160        macro(EnumPosExpr, Expr) \
    160161    macro(ConditionalExpr, Expr) \
    161162    macro(CommaExpr, Expr) \
  • src/GenPoly/Box.cpp

    r8b4faf6 r59c8dff  
    15801580        ast::Expr const * postvisit( ast::OffsetPackExpr const * expr );
    15811581
     1582        ast::Expr const * postvisit( ast::EnumPosExpr const * expr );
     1583
    15821584        void beginScope();
    15831585        void endScope();
     
    19501952
    19511953        return new ast::VariableExpr( expr->location, offsetArray );
     1954}
     1955
     1956// TODO
     1957ast::Expr const * PolyGenericCalculator::postvisit( ast::EnumPosExpr const * expr ) {
     1958        return expr;
    19521959}
    19531960
  • src/InitTweak/InitTweak.cc

    r8b4faf6 r59c8dff  
    271271                void previsit( const ast::OffsetofExpr * ) {}
    272272                void previsit( const ast::OffsetPackExpr * ) {}
     273                void previsit( const ast::EnumPosExpr * ) {}
    273274                void previsit( const ast::CommaExpr * ) {}
    274275                void previsit( const ast::LogicalExpr * ) {}
  • src/Parser/ExpressionNode.cc

    r8b4faf6 r59c8dff  
    690690} // build_unary_val
    691691
     692ast::Expr * build_enum_pos_expr( const CodeLocation & location, ast::Expr * expr_node ) {
     693        // return nullptr
     694        return new ast::EnumPosExpr( location, std::move( expr_node ) );
     695}
     696
    692697ast::Expr * build_binary_val( const CodeLocation & location,
    693698                OperKinds op,
  • src/Parser/ExpressionNode.h

    r8b4faf6 r59c8dff  
    8383ast::Expr * build_func( const CodeLocation &, ExpressionNode * function, ExpressionNode * expr_node );
    8484ast::Expr * build_compoundLiteral( const CodeLocation &, DeclarationNode * decl_node, InitializerNode * kids );
     85
     86ast::Expr * build_enum_pos_expr( const CodeLocation &, ast::Expr * expr_node );
  • src/ResolvExpr/CandidateFinder.cpp

    r8b4faf6 r59c8dff  
    5252#include "Common/Stats/Counter.h"
    5353
     54#include "AST/Inspect.hpp"             // for getFunctionName
     55
    5456#define PRINT( text ) if ( resolvep ) { text }
    5557
     
    656658                void postvisit( const ast::OffsetofExpr * offsetofExpr );
    657659                void postvisit( const ast::OffsetPackExpr * offsetPackExpr );
     660                void postvisit( const ast::EnumPosExpr * enumPosExpr );
    658661                void postvisit( const ast::LogicalExpr * logicalExpr );
    659662                void postvisit( const ast::ConditionalExpr * conditionalExpr );
     
    14711474        void Finder::postvisit( const ast::OffsetPackExpr * offsetPackExpr ) {
    14721475                addCandidate( offsetPackExpr, tenv );
     1476        }
     1477
     1478        void Finder::postvisit( const ast::EnumPosExpr * enumPosExpr ) {
     1479                CandidateFinder finder( context, tenv );
     1480                finder.find( enumPosExpr->expr );
     1481                CandidateList winners = findMinCost( finder.candidates );
     1482                if ( winners.size() != 1 ) SemanticError( enumPosExpr->expr.get(), "Ambiguous expression in position. ");
     1483                CandidateRef & choice = winners.front();
     1484                auto refExpr = referenceToRvalueConversion( choice->expr, choice->cost );
     1485                auto refResult = (refExpr->result).as<ast::EnumInstType>();
     1486                if ( !refResult ) {
     1487                        SemanticError( refExpr, "Position for Non enum type is not supported" );
     1488                }
     1489                // determineEnumPosConstant( enumPosExpr, refResult );
     1490
     1491                const ast::NameExpr * const nameExpr = enumPosExpr->expr.strict_as<ast::NameExpr>();
     1492                const ast::EnumDecl * base = refResult->base;
     1493                if ( !base ) {
     1494                        SemanticError( enumPosExpr, "Cannot be reference to a defined enumeration type" );
     1495                }
     1496                auto it = std::find_if( std::begin( base->members ), std::end( base->members ),
     1497                        [nameExpr]( ast::ptr<ast::Decl> decl ) { return decl->name == nameExpr->name; } );
     1498                unsigned position = it - base->members.begin();
     1499                addCandidate( ast::ConstantExpr::from_int( enumPosExpr->location, position ), tenv );
    14731500        }
    14741501
  • src/ResolvExpr/Resolver.cc

    r8b4faf6 r59c8dff  
    420420                const ast::ConstructorInit * previsit( const ast::ConstructorInit * );
    421421
     422                const ast::EnumPosExpr *         previsit( const ast::EnumPosExpr * );
     423
    422424                void resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd);
    423425
     
    12391241        }
    12401242
     1243        const ast::EnumPosExpr * Resolver::previsit( const ast::EnumPosExpr * enumPos ) {
     1244                visitor->maybe_accept( enumPos, &ast::EnumPosExpr::expr );
     1245                return enumPos;
     1246        }
     1247
    12411248        // suppress error on autogen functions and mark invalid autogen as deleted.
    12421249        bool Resolver::on_error(ast::ptr<ast::Decl> & decl) {
  • src/Validate/Autogen.cpp

    r8b4faf6 r59c8dff  
    186186                // could possibly use a new linkage type. For now we just make the
    187187                // basic ones intrinsic to code-gen them as C assignments.
    188                 const auto & real_type = decl->base;
    189                 const auto & basic = real_type.as<ast::BasicType>();
    190                 if(!real_type || (basic && basic->isInteger())) proto_linkage = ast::Linkage::Intrinsic;
     188                // const auto & real_type = decl->base;
     189                // const auto & basic = real_type.as<ast::BasicType>();
     190
     191                // if(!real_type || (basic && basic->isInteger())) proto_linkage = ast::Linkage::Intrinsic;
     192
     193                // Turns other enumeration type into Intrinstic as well to temporarily fix the recursive
     194                // construction bug
     195                proto_linkage = ast::Linkage::Intrinsic;
    191196        }
    192197
     
    742747                        }
    743748                );
     749                // auto fname = ast::getFunctionName( callExpr );
     750                // if (fname == "posE" ) {
     751                //      std::cerr << "Found posE autogen" << std::endl;
     752                // }
    744753                functionDecl->stmts = new ast::CompoundStmt( location,
    745754                        { new ast::ExprStmt( location, callExpr ) }
     
    792801}
    793802
     803struct PseudoFuncGenerateRoutine final :
     804                public ast::WithDeclsToAdd<>,
     805                public ast::WithShortCircuiting {
     806        void previsit( const ast::EnumDecl * enumDecl );
     807};
     808
     809void PseudoFuncGenerateRoutine::previsit( const ast::EnumDecl * enumDecl ) {
     810        const CodeLocation& location = enumDecl->location;
     811        if ( enumDecl->members.size() == 0 || !enumDecl->base || enumDecl->name == "" ) return;
     812
     813        std::vector<ast::ptr<ast::Init>> inits;
     814        std::vector<ast::ptr<ast::Init>> labels;
     815        for ( const ast::Decl * mem: enumDecl->members ) {
     816                auto memAsObjectDecl = dynamic_cast< const ast::ObjectDecl * >( mem );
     817                inits.emplace_back( memAsObjectDecl->init );
     818                labels.emplace_back( new ast::SingleInit(
     819                        location, ast::ConstantExpr::from_string(location, mem->name) ) );
     820        }
     821        auto init = new ast::ListInit( location, std::move( inits ) );
     822        auto label_strings = new ast::ListInit( location, std::move(labels) );
     823        auto values = new ast::ObjectDecl(
     824                location,
     825                "__enum_values_"+enumDecl->name,
     826                new ast::ArrayType(
     827                        // new ast::PointerType( new ast::BasicType{ ast::BasicType::Char} ),
     828                        enumDecl->base,
     829                        ast::ConstantExpr::from_int( location, enumDecl->members.size() ),
     830                        ast::LengthFlag::FixedLen, ast::DimensionFlag::DynamicDim
     831                )
     832                ,init
     833                ,
     834                ast::Storage::Static,
     835                ast::Linkage::AutoGen
     836        );
     837        auto label_arr = new ast::ObjectDecl(
     838                location,
     839                "__enum_labels_"+enumDecl->name,
     840                new ast::ArrayType(
     841                        new ast::PointerType( new ast::BasicType{ ast::BasicType::Char} ),
     842                        ast::ConstantExpr::from_int( location, enumDecl->members.size() ),
     843                        ast::LengthFlag::FixedLen, ast::DimensionFlag::DynamicDim
     844                ),
     845                label_strings,
     846                ast::Storage::Static,
     847                ast::Linkage::AutoGen
     848        );
     849
     850        declsToAddAfter.push_back( values );
     851        declsToAddAfter.push_back( label_arr );
     852}
     853
    794854} // namespace
    795855
    796856void autogenerateRoutines( ast::TranslationUnit & translationUnit ) {
    797857        ast::Pass<AutogenerateRoutines>::run( translationUnit );
     858        // ast::Pass<PseudoFuncGenerateRoutine>::run( translationUnit );
    798859}
    799860
  • src/Validate/module.mk

    r8b4faf6 r59c8dff  
    5252        Validate/ReturnCheck.hpp \
    5353        Validate/VerifyCtorDtorAssign.cpp \
    54         Validate/VerifyCtorDtorAssign.hpp
     54        Validate/VerifyCtorDtorAssign.hpp \
     55        Validate/ReplacePseudoFunc.cpp \
     56        Validate/ReplacePseudoFunc.hpp
    5557
    5658SRCDEMANGLE += $(SRC_VALIDATE)
  • src/main.cc

    r8b4faf6 r59c8dff  
    8282#include "Validate/ReturnCheck.hpp"         // for checkReturnStatements
    8383#include "Validate/VerifyCtorDtorAssign.hpp" // for verifyCtorDtorAssign
     84#include "Validate/ReplacePseudoFunc.hpp"
    8485#include "Virtual/ExpandCasts.h"            // for expandCasts
    8586#include "Virtual/VirtualDtor.hpp"          // for implementVirtDtors
     
    287288                        assertf( extras, "cannot open extras.cf\n" );
    288289                        parse( extras, ast::Linkage::BuiltinC );
    289 
    290290                        if ( ! libcfap ) {
    291291                                // read the prelude in, if not generating the cfa library
     
    322322                PASS( "Forall Pointer Decay", Validate::decayForallPointers, transUnit );
    323323                PASS( "Fix Qualified Types", Validate::fixQualifiedTypes, transUnit );
     324
    324325                PASS( "Eliminate Typedef", Validate::eliminateTypedef, transUnit );
    325326                PASS( "Hoist Struct", Validate::hoistStruct, transUnit );
     
    381382                PASS( "Resolve", ResolvExpr::resolve, transUnit );
    382383                DUMP( exprp, std::move( transUnit ) );
     384                PASS( "Replace Pseudo Func", Validate::replacePseudoFunc, transUnit );
    383385
    384386                PASS( "Fix Init", InitTweak::fix, transUnit, buildingLibrary() );
Note: See TracChangeset for help on using the changeset viewer.