[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(); } |
---|
| 54 | void FunctionDecl::set_type(Type * t) { type = strict_dynamic_cast< FunctionType* >( t ); } |
---|
[8a5530c] | 55 | |
---|
[360b2e13] | 56 | // --- TypeDecl |
---|
| 57 | |
---|
| 58 | std::string TypeDecl::typeString() const { |
---|
| 59 | static const std::string kindNames[] = { "object type", "function type", "tuple type" }; |
---|
[8a5530c] | 60 | assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1, |
---|
[360b2e13] | 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 | |
---|
[d76c588] | 73 | std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) { |
---|
| 74 | return out << data.kind << ", " << data.isComplete; |
---|
| 75 | } |
---|
| 76 | |
---|
[a300e4a] | 77 | // --- EnumDecl |
---|
| 78 | |
---|
[9d6e7fa9] | 79 | bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const { |
---|
[a300e4a] | 80 | if ( enumValues.empty() ) { |
---|
| 81 | long long crntVal = 0; |
---|
[9d6e7fa9] | 82 | for ( const Decl * member : members ) { |
---|
[a300e4a] | 83 | const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member ); |
---|
| 84 | if ( field->init ) { |
---|
[87701b6] | 85 | const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() ); |
---|
[a300e4a] | 86 | auto result = eval( init->value ); |
---|
| 87 | if ( ! result.second ) { |
---|
[733074e] | 88 | SemanticError( init->location, ::toString( "Non-constexpr in initialization of " |
---|
[a300e4a] | 89 | "enumerator: ", field ) ); |
---|
| 90 | } |
---|
| 91 | crntVal = result.first; |
---|
| 92 | } |
---|
| 93 | if ( enumValues.count( field->name ) != 0 ) { |
---|
[733074e] | 94 | SemanticError( location, ::toString( "Enum ", name, " has multiple members with the " "name ", field->name ) ); |
---|
[a300e4a] | 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 | |
---|
[2bb4a01] | 109 | } |
---|
| 110 | |
---|
| 111 | // Local Variables: // |
---|
| 112 | // tab-width: 4 // |
---|
| 113 | // mode: c++ // |
---|
| 114 | // compile-command: "make install" // |
---|
| 115 | // End: // |
---|