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