source: src/AST/Decl.cpp@ cdcd53dc

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since cdcd53dc was a300e4a, checked in by Aaron Moss <a3moss@…>, 6 years ago

Add some decls to the new AST

  • Property mode set to 100644
File size: 2.1 KB
Line 
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
16#include <cassert> // for assert, strict_dynamic_cast
17#include <unordered_map>
18
19#include "Decl.hpp"
20
21#include "Fwd.hpp" // for UniqueId
22#include "Init.hpp"
23#include "Node.hpp" // for readonly
24
25namespace ast {
26
27// To canonicalize declarations
28static UniqueId lastUniqueId = 0;
29
30using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >;
31static IdMapType idMap;
32
33void Decl::fixUniqueId() {
34 if ( uniqueId ) return; // ensure only set once
35 uniqueId = ++lastUniqueId;
36 idMap[ uniqueId ] = this;
37}
38
39readonly<Decl> Decl::fromId( UniqueId id ) {
40 IdMapType::const_iterator i = idMap.find( id );
41 if ( i != idMap.end() ) return i->second;
42 return {};
43}
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
77}
78
79// Local Variables: //
80// tab-width: 4 //
81// mode: c++ //
82// compile-command: "make install" //
83// End: //
Note: See TracBrowser for help on using the repository browser.