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 "Decl.hpp" |
---|
17 | |
---|
18 | #include <cassert> // for assert, strict_dynamic_cast |
---|
19 | #include <iostream> |
---|
20 | #include <string> |
---|
21 | #include <unordered_map> |
---|
22 | |
---|
23 | #include "Common/utility.h" |
---|
24 | |
---|
25 | #include "Fwd.hpp" // for UniqueId |
---|
26 | #include "Init.hpp" |
---|
27 | #include "Node.hpp" // for readonly |
---|
28 | #include "Type.hpp" // for readonly |
---|
29 | #include "Parser/ParseNode.h" // for DeclarationNode |
---|
30 | |
---|
31 | namespace ast { |
---|
32 | |
---|
33 | // To canonicalize declarations |
---|
34 | static UniqueId lastUniqueId = 0; |
---|
35 | |
---|
36 | using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >; |
---|
37 | static IdMapType idMap; |
---|
38 | |
---|
39 | void Decl::fixUniqueId() { |
---|
40 | if ( uniqueId ) return; // ensure only set once |
---|
41 | uniqueId = ++lastUniqueId; |
---|
42 | idMap[ uniqueId ] = this; |
---|
43 | } |
---|
44 | |
---|
45 | readonly<Decl> Decl::fromId( UniqueId id ) { |
---|
46 | IdMapType::const_iterator i = idMap.find( id ); |
---|
47 | if ( i != idMap.end() ) return i->second; |
---|
48 | return {}; |
---|
49 | } |
---|
50 | |
---|
51 | // --- FunctionDecl |
---|
52 | |
---|
53 | const Type * FunctionDecl::get_type() const { return type.get(); } |
---|
54 | void FunctionDecl::set_type(Type * t) { type = strict_dynamic_cast< FunctionType* >( t ); } |
---|
55 | |
---|
56 | // --- TypeDecl |
---|
57 | |
---|
58 | std::string TypeDecl::typeString() const { |
---|
59 | static const std::string kindNames[] = { "object type", "function type", "tuple type" }; |
---|
60 | assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1, |
---|
61 | "typeString: kindNames is out of sync." ); |
---|
62 | assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." ); |
---|
63 | return (sized ? "sized " : "") + kindNames[ kind ]; |
---|
64 | } |
---|
65 | |
---|
66 | std::string TypeDecl::genTypeString() const { |
---|
67 | static const std::string kindNames[] = { "dtype", "ftype", "ttype" }; |
---|
68 | assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1, "genTypeString: kindNames is out of sync." ); |
---|
69 | assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." ); |
---|
70 | return kindNames[ kind ]; |
---|
71 | } |
---|
72 | |
---|
73 | std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) { |
---|
74 | return out << data.kind << ", " << data.isComplete; |
---|
75 | } |
---|
76 | |
---|
77 | // --- EnumDecl |
---|
78 | |
---|
79 | bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const { |
---|
80 | if ( enumValues.empty() ) { |
---|
81 | long long crntVal = 0; |
---|
82 | for ( const Decl * member : members ) { |
---|
83 | const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member ); |
---|
84 | if ( field->init ) { |
---|
85 | const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() ); |
---|
86 | auto result = eval( init->value ); |
---|
87 | if ( ! result.second ) { |
---|
88 | SemanticError( init->location, ::toString( "Non-constexpr in initialization of " |
---|
89 | "enumerator: ", field ) ); |
---|
90 | } |
---|
91 | crntVal = result.first; |
---|
92 | } |
---|
93 | if ( enumValues.count( field->name ) != 0 ) { |
---|
94 | SemanticError( location, ::toString( "Enum ", name, " has multiple members with the " "name ", field->name ) ); |
---|
95 | } |
---|
96 | enumValues[ field->name ] = crntVal; |
---|
97 | ++crntVal; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | auto it = enumValues.find( enumerator->name ); |
---|
102 | if ( it != enumValues.end() ) { |
---|
103 | value = it->second; |
---|
104 | return true; |
---|
105 | } |
---|
106 | return false; |
---|
107 | } |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | // Local Variables: // |
---|
112 | // tab-width: 4 // |
---|
113 | // mode: c++ // |
---|
114 | // compile-command: "make install" // |
---|
115 | // End: // |
---|