[2bb4a01] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // Decl.cpp -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
| 10 | // Created On : Thu May 9 10:00:00 2019 |
---|
| 11 | // Last Modified By : Aaron B. Moss |
---|
| 12 | // Last Modified On : Thu May 9 10:00:00 2019 |
---|
| 13 | // Update Count : 1 |
---|
| 14 | // |
---|
| 15 | |
---|
[a300e4a] | 16 | #include <cassert> // for assert, strict_dynamic_cast |
---|
[2bb4a01] | 17 | #include <unordered_map> |
---|
| 18 | |
---|
| 19 | #include "Decl.hpp" |
---|
| 20 | |
---|
[a300e4a] | 21 | #include "Fwd.hpp" // for UniqueId |
---|
| 22 | #include "Init.hpp" |
---|
| 23 | #include "Node.hpp" // for readonly |
---|
[2bb4a01] | 24 | |
---|
| 25 | namespace ast { |
---|
[a300e4a] | 26 | |
---|
[2bb4a01] | 27 | // To canonicalize declarations |
---|
| 28 | static UniqueId lastUniqueId = 0; |
---|
| 29 | |
---|
| 30 | using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >; |
---|
| 31 | static IdMapType idMap; |
---|
| 32 | |
---|
| 33 | void Decl::fixUniqueId() { |
---|
| 34 | if ( uniqueId ) return; // ensure only set once |
---|
| 35 | uniqueId = ++lastUniqueId; |
---|
| 36 | idMap[ uniqueId ] = this; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | readonly<Decl> Decl::fromId( UniqueId id ) { |
---|
| 40 | IdMapType::const_iterator i = idMap.find( id ); |
---|
| 41 | if ( i != idMap.end() ) return i->second; |
---|
| 42 | return {}; |
---|
| 43 | } |
---|
[a300e4a] | 44 | |
---|
| 45 | // --- EnumDecl |
---|
| 46 | |
---|
| 47 | bool 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 | |
---|
[2bb4a01] | 77 | } |
---|
| 78 | |
---|
| 79 | // Local Variables: // |
---|
| 80 | // tab-width: 4 // |
---|
| 81 | // mode: c++ // |
---|
| 82 | // compile-command: "make install" // |
---|
| 83 | // End: // |
---|