[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
|
---|
[7edd5c1] | 11 | // Last Modified By : Andrew Beach
|
---|
| 12 | // Last Modified On : Thu May 5 12:10:00 2022
|
---|
| 13 | // Update Count : 24
|
---|
[2bb4a01] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "Decl.hpp"
|
---|
| 17 |
|
---|
[360b2e13] | 18 | #include <cassert> // for assert, strict_dynamic_cast
|
---|
[d76c588] | 19 | #include <iostream>
|
---|
[360b2e13] | 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
|
---|
[a4a6802] | 28 | #include "Expr.hpp"
|
---|
[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;
|
---|
[7d7ef6f] | 41 | // The extra readonly pointer is causing some reference counting issues.
|
---|
| 42 | // idMap[ uniqueId ] = this;
|
---|
[2bb4a01] | 43 | }
|
---|
| 44 |
|
---|
| 45 | readonly<Decl> Decl::fromId( UniqueId id ) {
|
---|
[7d7ef6f] | 46 | // Right now this map is always empty, so don't use it.
|
---|
[9490621] | 47 | assert( false );
|
---|
[2bb4a01] | 48 | IdMapType::const_iterator i = idMap.find( id );
|
---|
| 49 | if ( i != idMap.end() ) return i->second;
|
---|
| 50 | return {};
|
---|
| 51 | }
|
---|
[a300e4a] | 52 |
|
---|
[8a5530c] | 53 | // --- FunctionDecl
|
---|
| 54 |
|
---|
[7edd5c1] | 55 | FunctionDecl::FunctionDecl( const CodeLocation & loc, const std::string & name,
|
---|
[3e5dd913] | 56 | std::vector<ptr<TypeDecl>>&& forall,
|
---|
| 57 | std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
|
---|
| 58 | CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage,
|
---|
| 59 | std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, bool isVarArgs)
|
---|
| 60 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
|
---|
| 61 | type_params(std::move(forall)), stmts( stmts ) {
|
---|
| 62 | FunctionType * ftype = new FunctionType(static_cast<ArgumentFlag>(isVarArgs));
|
---|
| 63 | for (auto & param : this->params) {
|
---|
| 64 | ftype->params.emplace_back(param->get_type());
|
---|
| 65 | }
|
---|
| 66 | for (auto & ret : this->returns) {
|
---|
| 67 | ftype->returns.emplace_back(ret->get_type());
|
---|
| 68 | }
|
---|
| 69 | for (auto & tp : this->type_params) {
|
---|
[b230091] | 70 | ftype->forall.emplace_back(new TypeInstType(tp));
|
---|
[a4a6802] | 71 | for (auto & ap: tp->assertions) {
|
---|
| 72 | ftype->assertions.emplace_back(new VariableExpr(loc, ap));
|
---|
| 73 | }
|
---|
[3e5dd913] | 74 | }
|
---|
| 75 | this->type = ftype;
|
---|
| 76 | }
|
---|
[490fb92e] | 77 |
|
---|
[7edd5c1] | 78 | FunctionDecl::FunctionDecl( const CodeLocation & location, const std::string & name,
|
---|
| 79 | std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions,
|
---|
| 80 | std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
|
---|
| 81 | CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage,
|
---|
| 82 | std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, bool isVarArgs)
|
---|
| 83 | : DeclWithType( location, name, storage, linkage, std::move(attrs), fs ),
|
---|
| 84 | params( std::move(params) ), returns( std::move(returns) ),
|
---|
| 85 | type_params( std::move( forall) ), assertions( std::move( assertions ) ),
|
---|
| 86 | type( nullptr ), stmts( stmts ) {
|
---|
| 87 | FunctionType * type = new FunctionType( (isVarArgs) ? VariableArgs : FixedArgs );
|
---|
| 88 | for ( auto & param : this->params ) {
|
---|
| 89 | type->params.emplace_back( param->get_type() );
|
---|
| 90 | }
|
---|
| 91 | for ( auto & ret : this->returns ) {
|
---|
| 92 | type->returns.emplace_back( ret->get_type() );
|
---|
| 93 | }
|
---|
| 94 | for ( auto & param : this->type_params ) {
|
---|
| 95 | type->forall.emplace_back( new TypeInstType( param ) );
|
---|
| 96 | }
|
---|
| 97 | for ( auto & assertion : this->assertions ) {
|
---|
| 98 | type->assertions.emplace_back(
|
---|
| 99 | new VariableExpr( assertion->location, assertion ) );
|
---|
| 100 | }
|
---|
| 101 | this->type = type;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[490fb92e] | 104 |
|
---|
[87701b6] | 105 | const Type * FunctionDecl::get_type() const { return type.get(); }
|
---|
[e0e9a0b] | 106 | void FunctionDecl::set_type( const Type * t ) {
|
---|
| 107 | type = strict_dynamic_cast< const FunctionType * >( t );
|
---|
| 108 | }
|
---|
[8a5530c] | 109 |
|
---|
[360b2e13] | 110 | // --- TypeDecl
|
---|
| 111 |
|
---|
[312029a] | 112 | const char * TypeDecl::typeString() const {
|
---|
[6e50a6b] | 113 | static const char * kindNames[] = { "sized data type", "sized data type", "sized object type", "sized function type", "sized tuple type", "sized length value" };
|
---|
[b66d14a] | 114 | static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "typeString: kindNames is out of sync." );
|
---|
[07de76b] | 115 | assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
|
---|
[312029a] | 116 | return sized ? kindNames[ kind ] : &kindNames[ kind ][ sizeof("sized") ]; // sizeof includes '\0'
|
---|
[360b2e13] | 117 | }
|
---|
| 118 |
|
---|
[312029a] | 119 | const char * TypeDecl::genTypeString() const {
|
---|
[b66d14a] | 120 | static const char * kindNames[] = { "T &", "T *", "T", "(*)", "T ...", "[T]" };
|
---|
| 121 | static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "genTypeString: kindNames is out of sync." );
|
---|
[07de76b] | 122 | assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
|
---|
[360b2e13] | 123 | return kindNames[ kind ];
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[d76c588] | 126 | std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) {
|
---|
| 127 | return out << data.kind << ", " << data.isComplete;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[312029a] | 130 | // --- AggregateDecl
|
---|
| 131 |
|
---|
| 132 | // These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
|
---|
| 133 | static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
|
---|
| 134 |
|
---|
| 135 | const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
|
---|
| 136 | return aggregateNames[aggr];
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[a300e4a] | 139 | // --- EnumDecl
|
---|
| 140 |
|
---|
[9d6e7fa9] | 141 | bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const {
|
---|
[a300e4a] | 142 | if ( enumValues.empty() ) {
|
---|
| 143 | long long crntVal = 0;
|
---|
[9d6e7fa9] | 144 | for ( const Decl * member : members ) {
|
---|
[a300e4a] | 145 | const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
|
---|
| 146 | if ( field->init ) {
|
---|
[87701b6] | 147 | const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() );
|
---|
[a300e4a] | 148 | auto result = eval( init->value );
|
---|
| 149 | if ( ! result.second ) {
|
---|
[733074e] | 150 | SemanticError( init->location, ::toString( "Non-constexpr in initialization of "
|
---|
[a300e4a] | 151 | "enumerator: ", field ) );
|
---|
| 152 | }
|
---|
| 153 | crntVal = result.first;
|
---|
| 154 | }
|
---|
| 155 | if ( enumValues.count( field->name ) != 0 ) {
|
---|
[733074e] | 156 | SemanticError( location, ::toString( "Enum ", name, " has multiple members with the " "name ", field->name ) );
|
---|
[a300e4a] | 157 | }
|
---|
| 158 | enumValues[ field->name ] = crntVal;
|
---|
| 159 | ++crntVal;
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | auto it = enumValues.find( enumerator->name );
|
---|
[f135b50] | 164 |
|
---|
[a300e4a] | 165 | if ( it != enumValues.end() ) {
|
---|
[f135b50] | 166 |
|
---|
| 167 | // Handle typed enum by casting the value in (C++) compiler
|
---|
[3e54399] | 168 | // if ( base ) { // A typed enum
|
---|
| 169 | // if ( const BasicType * bt = dynamic_cast<const BasicType *>(base) ) {
|
---|
| 170 | // switch( bt->kind ) {
|
---|
| 171 | // case BasicType::Kind::Bool: value = (bool) it->second; break;
|
---|
| 172 | // case BasicType::Kind::Char: value = (char) it->second; break;
|
---|
| 173 | // case BasicType::Kind::SignedChar: value = (signed char) it->second; break;
|
---|
| 174 | // case BasicType::Kind::UnsignedChar: value = (unsigned char) it->second; break;
|
---|
| 175 | // case BasicType::Kind::ShortSignedInt: value = (short signed int) it->second; break;
|
---|
| 176 | // case BasicType::Kind::SignedInt: value = (signed int) it->second; break;
|
---|
| 177 | // case BasicType::Kind::UnsignedInt: value = (unsigned int) it->second; break;
|
---|
| 178 | // case BasicType::Kind::LongSignedInt: value = (long signed int) it->second; break;
|
---|
| 179 | // case BasicType::Kind::LongUnsignedInt: value = (long unsigned int) it->second; break;
|
---|
| 180 | // case BasicType::Kind::LongLongSignedInt: value = (long long signed int) it->second; break;
|
---|
| 181 | // case BasicType::Kind::LongLongUnsignedInt: value = (long long unsigned int) it->second; break;
|
---|
| 182 | // // TODO: value should be able to handle long long unsigned int
|
---|
| 183 |
|
---|
| 184 | // default:
|
---|
| 185 | // value = it->second;
|
---|
| 186 | // }
|
---|
| 187 | // }
|
---|
| 188 | // } else {
|
---|
[f135b50] | 189 | value = it->second;
|
---|
[3e54399] | 190 | //}
|
---|
[f135b50] | 191 |
|
---|
[a300e4a] | 192 | return true;
|
---|
| 193 | }
|
---|
| 194 | return false;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[2bb4a01] | 197 | }
|
---|
| 198 |
|
---|
| 199 | // Local Variables: //
|
---|
| 200 | // tab-width: 4 //
|
---|
| 201 | // mode: c++ //
|
---|
| 202 | // compile-command: "make install" //
|
---|
| 203 | // End: //
|
---|