Changeset fc9153d


Ignore:
Timestamp:
Dec 22, 2017, 4:17:52 PM (6 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
caab997
Parents:
ddb80bd
Message:

Add helper function for determining the value of an enumerator

Location:
src/SynTree
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/AggregateDecl.cc

    rddb80bd rfc9153d  
    8686std::string TraitDecl::typeString() const { return "trait"; }
    8787
     88namespace {
     89        long long int getConstValue( Expression * expr ) {
     90                if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( expr ) ) {
     91                        return getConstValue( castExpr->arg );
     92                } else if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
     93                        return constExpr->intValue();
     94                } else {
     95                        assertf( false, "Unhandled expression type in getConstValue for enumerators: %s", toString( expr ).c_str() );
     96                }
     97        }
     98}
     99
     100bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) {
     101        if ( enumValues.empty() ) {
     102                long long int currentValue = 0;
     103                for ( Declaration * member : members ) {
     104                        ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member );
     105                        if ( field->init ) {
     106                                SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init );
     107                                currentValue = getConstValue( init->value );
     108                        }
     109                        assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() );
     110                        enumValues[ field->name ] = currentValue;
     111                        ++currentValue;
     112                }
     113        }
     114        if ( enumValues.count( enumerator->name ) ) {
     115                value = enumValues[ enumerator->name ];
     116                return true;
     117        }
     118        return false;
     119}
     120
    88121// Local Variables: //
    89122// tab-width: 4 //
  • src/SynTree/Declaration.h

    rddb80bd rfc9153d  
    319319        EnumDecl( const EnumDecl &other ) : Parent( other ) {}
    320320
     321        bool valueOf( Declaration * enumerator, long long int & value );
     322
    321323        virtual EnumDecl *clone() const override { return new EnumDecl( *this ); }
    322324        virtual void accept( Visitor &v ) override { v.visit( this ); }
    323325        virtual Declaration *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
    324326  private:
     327        std::map< std::string, long long int > enumValues;
    325328        virtual std::string typeString() const override;
    326329};
Note: See TracChangeset for help on using the changeset viewer.