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