Changeset 85855b0 for src/AST


Ignore:
Timestamp:
Jun 10, 2024, 2:43:13 AM (19 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
42cdd07d
Parents:
d68de59
Message:
  1. Implement enum cast; 2. Change valueE so that opague enum returns quasi_void; 3. change enum hiding interpretation and pass visiting scheme
Location:
src/AST
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Decl.cpp

    rd68de59 r85855b0  
    170170
    171171const std::string EnumDecl::getUnmangeldArrayName( const ast::EnumAttribute attr ) const {
    172                 switch( attr ) {
    173                         case ast::EnumAttribute::Value: return "values_" + name ;
    174                         case ast::EnumAttribute::Label: return "labels_" + name;
    175                         default: /* Posn does not generate array */
    176                                 return "";
     172        switch( attr ) {
     173                case ast::EnumAttribute::Value: return "values_" + name ;
     174                case ast::EnumAttribute::Label: return "labels_" + name;
     175                default: /* Posn does not generate array */
     176                        return "";
     177        }
     178}
     179
     180unsigned EnumDecl::calChildOffset(const std::string & target) const{
     181        unsigned offset = 0;
     182        for (auto childEnum: inlinedDecl) {
     183                auto childDecl = childEnum->base;
     184                if (childDecl->name == target) {
     185                        return offset;
    177186                }
    178         }
     187                offset += childDecl->members.size();
     188        }
     189    std::cerr << "Cannot find the target enum" << std::endl;
     190        return 0;
     191}
     192
     193unsigned EnumDecl::calChildOffset(const ast::EnumInstType * target) const{
     194        return calChildOffset(target->base->name);
     195}
     196
     197bool EnumDecl::isSubTypeOf(const ast::EnumDecl * other) const {
     198        if (name == other->name) return true;
     199        for (auto inlined: other->inlinedDecl) {
     200                if (isSubTypeOf(inlined->base)) return true;
     201        }
     202        return false;
     203}
    179204
    180205}
  • src/AST/Decl.hpp

    rd68de59 r85855b0  
    7575        bool isDeleted = false;
    7676        bool isTypeFixed = false;
     77        bool isHidden = false;
    7778
    7879        DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
     
    313314        ptr<Type> base;
    314315        enum class EnumHiding { Visible, Hide } hide;
     316        std::vector< ast::ptr<ast::EnumInstType>> inlinedDecl; // child enums
     317
    315318        EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false,
    316319                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall,
     
    328331
    329332        const std::string getUnmangeldArrayName( const EnumAttribute attr ) const;
     333
     334        unsigned calChildOffset(const std::string & childEnum) const;
     335        unsigned calChildOffset(const ast::EnumInstType * childEnum) const;
     336
     337        bool isSubTypeOf(const ast::EnumDecl *) const;
    330338private:
    331339        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
  • src/AST/Expr.hpp

    rd68de59 r85855b0  
    256256public:
    257257        ptr<Decl> type_decl;
    258         std::string name;
     258        const std::string type_name;
     259        const std::string name;
    259260
    260261        QualifiedNameExpr( const CodeLocation & loc, const Decl * d, const std::string & n )
    261         : Expr( loc ), type_decl( d ), name( n ) {}
     262        : Expr( loc ), type_decl( d ), type_name(""), name( n ) {}
     263
     264        QualifiedNameExpr( const CodeLocation & loc, const std::string & type_name, const std::string & name)
     265        : Expr( loc ), type_name( type_name ), name( name ) {}
    262266
    263267        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
  • src/AST/Pass.impl.hpp

    rd68de59 r85855b0  
    560560
    561561        if ( __visit_children() ) {
    562                 if ( node->hide == ast::EnumDecl::EnumHiding::Hide ) {
    563                         guard_symtab guard { *this };
    564                         maybe_accept( node, &EnumDecl::base );
    565                         maybe_accept( node, &EnumDecl::params     );
    566                         maybe_accept( node, &EnumDecl::members    );
    567                         maybe_accept( node, &EnumDecl::attributes );
    568                 } else {
    569                         maybe_accept( node, &EnumDecl::base );
    570                         maybe_accept( node, &EnumDecl::params     );
    571                         maybe_accept( node, &EnumDecl::members    );
    572                         maybe_accept( node, &EnumDecl::attributes );
    573                 }
     562                maybe_accept( node, &EnumDecl::base        );
     563                maybe_accept( node, &EnumDecl::params      );
     564                maybe_accept( node, &EnumDecl::members     );
     565                maybe_accept( node, &EnumDecl::attributes  );
     566                maybe_accept( node, &EnumDecl::inlinedDecl );
    574567        }
    575568
  • src/AST/SymbolTable.cpp

    rd68de59 r85855b0  
    159159}
    160160
     161std::vector<SymbolTable::IdData> SymbolTable::lookupIdIgnoreHidden( const std::string &id ) const {
     162        std::vector<IdData> out;
     163        std::vector<IdData> lookupResult = lookupId(id);
     164        for ( auto candidate: lookupResult) {
     165                if ( candidate.id ) {
     166                        if (candidate.id->isHidden) continue;
     167                }
     168                out.push_back(candidate);
     169        }
     170        return out;
     171}
     172
    161173std::vector<SymbolTable::IdData> SymbolTable::specialLookupId( SymbolTable::SpecialFunctionKind kind, const std::string & otypeKey ) const {
    162174        static Stats::Counters::CounterGroup * special_stats = Stats::Counters::build<Stats::Counters::CounterGroup>("Special Lookups");
  • src/AST/SymbolTable.hpp

    rd68de59 r85855b0  
    121121        /// Gets all declarations with the given ID
    122122        std::vector<IdData> lookupId( const std::string &id ) const;
     123        /// Gets all declarations with the given ID, ignoring hidden members from enumeration
     124        std::vector<IdData> lookupIdIgnoreHidden( const std::string &id ) const;
    123125        /// Gets special functions associated with a type; if no key is given, returns everything
    124126        std::vector<IdData> specialLookupId( SpecialFunctionKind kind, const std::string & otypeKey = "" ) const;
  • src/AST/Util.cpp

    rd68de59 r85855b0  
    352352        void previsit( EnumDecl const * decl ) {
    353353                enumDecls.insert( decl );
    354                 if ( ast::EnumDecl::EnumHiding::Visible == decl->hide ) {
    355                         for ( auto & member : decl->members ) {
    356                                 typedDecls.insert( member.strict_as<ast::DeclWithType>() );
    357                         }
     354                for ( auto & member : decl->members ) {
     355                        typedDecls.insert( member.strict_as<ast::DeclWithType>() );
    358356                }
    359357                beginScope();
Note: See TracChangeset for help on using the changeset viewer.