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