Changeset f135b50 for src/AST


Ignore:
Timestamp:
Feb 28, 2022, 3:41:44 AM (4 years ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT, ast-experimental, 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/AST
Files:
3 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
Note: See TracChangeset for help on using the changeset viewer.