Changeset a300e4a for src/AST/Decl.cpp


Ignore:
Timestamp:
May 9, 2019, 5:17:51 PM (5 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
3e46cc8
Parents:
2bb4a01
Message:

Add some decls to the new AST

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Decl.cpp

    r2bb4a01 ra300e4a  
    1414//
    1515
     16#include <cassert>        // for assert, strict_dynamic_cast
    1617#include <unordered_map>
    1718
    1819#include "Decl.hpp"
    1920
    20 #include "Fwd.hpp"   // for UniqueId
    21 #include "Node.hpp"  // for readonly
     21#include "Fwd.hpp"        // for UniqueId
     22#include "Init.hpp"
     23#include "Node.hpp"       // for readonly
    2224
    2325namespace ast {
     26
    2427// To canonicalize declarations
    2528static UniqueId lastUniqueId = 0;
     
    3942        return {};
    4043}
     44
     45// --- EnumDecl
     46
     47bool EnumDecl::valueOf( Decl* enumerator, long long& value ) const {
     48        if ( enumValues.empty() ) {
     49                long long crntVal = 0;
     50                for ( const Decl* member : members ) {
     51                        const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
     52                        if ( field->init ) {
     53                                const SingleInit* init = strict_dynamic_cast< const SingleInit* >( field->init );
     54                                auto result = eval( init->value );
     55                                if ( ! result.second ) {
     56                                        SemanticError( init->location, toString( "Non-constexpr in initialization of "
     57                                                "enumerator: ", field ) );
     58                                }
     59                                crntVal = result.first;
     60                        }
     61                        if ( enumValues.count( field->name ) != 0 ) {
     62                                SemanticError( location, toString( "Enum ", name, " has multiple members with the "     "name ", field->name ) );
     63                        }
     64                        enumValues[ field->name ] = crntVal;
     65                        ++crntVal;
     66                }
     67        }
     68
     69        auto it = enumValues.find( enumerator->name );
     70        if ( it != enumValues.end() ) {
     71                value = it->second;
     72                return true;
     73        }
     74        return false;
     75}
     76
    4177}
    4278
Note: See TracChangeset for help on using the changeset viewer.