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