[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 |
---|
[312029a] | 11 | // Last Modified By : Peter A. Buhr |
---|
[20207c0] | 12 | // Last Modified On : Tue Jan 12 16:54:55 2021 |
---|
| 13 | // Update Count : 23 |
---|
[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; |
---|
| 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 | |
---|
[490fb92e] | 52 | FunctionDecl::FunctionDecl( const CodeLocation & loc, const std::string & name, |
---|
[3e5dd913] | 53 | std::vector<ptr<TypeDecl>>&& forall, |
---|
| 54 | std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, |
---|
| 55 | CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage, |
---|
| 56 | std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, bool isVarArgs) |
---|
| 57 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)), |
---|
| 58 | type_params(std::move(forall)), stmts( stmts ) { |
---|
| 59 | FunctionType * ftype = new FunctionType(static_cast<ArgumentFlag>(isVarArgs)); |
---|
| 60 | for (auto & param : this->params) { |
---|
| 61 | ftype->params.emplace_back(param->get_type()); |
---|
| 62 | } |
---|
| 63 | for (auto & ret : this->returns) { |
---|
| 64 | ftype->returns.emplace_back(ret->get_type()); |
---|
| 65 | } |
---|
| 66 | for (auto & tp : this->type_params) { |
---|
| 67 | ftype->forall.emplace_back(new TypeInstType(tp->name, tp)); |
---|
[a4a6802] | 68 | for (auto & ap: tp->assertions) { |
---|
| 69 | ftype->assertions.emplace_back(new VariableExpr(loc, ap)); |
---|
| 70 | } |
---|
[3e5dd913] | 71 | } |
---|
| 72 | this->type = ftype; |
---|
| 73 | } |
---|
[490fb92e] | 74 | |
---|
| 75 | |
---|
[87701b6] | 76 | const Type * FunctionDecl::get_type() const { return type.get(); } |
---|
[e0e9a0b] | 77 | void FunctionDecl::set_type( const Type * t ) { |
---|
| 78 | type = strict_dynamic_cast< const FunctionType * >( t ); |
---|
| 79 | } |
---|
[8a5530c] | 80 | |
---|
[360b2e13] | 81 | // --- TypeDecl |
---|
| 82 | |
---|
[312029a] | 83 | const char * TypeDecl::typeString() const { |
---|
[6e50a6b] | 84 | static const char * kindNames[] = { "sized data type", "sized data type", "sized object type", "sized function type", "sized tuple type", "sized length value" }; |
---|
[b66d14a] | 85 | static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "typeString: kindNames is out of sync." ); |
---|
[07de76b] | 86 | assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." ); |
---|
[312029a] | 87 | return sized ? kindNames[ kind ] : &kindNames[ kind ][ sizeof("sized") ]; // sizeof includes '\0' |
---|
[360b2e13] | 88 | } |
---|
| 89 | |
---|
[312029a] | 90 | const char * TypeDecl::genTypeString() const { |
---|
[b66d14a] | 91 | static const char * kindNames[] = { "T &", "T *", "T", "(*)", "T ...", "[T]" }; |
---|
| 92 | static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "genTypeString: kindNames is out of sync." ); |
---|
[07de76b] | 93 | assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." ); |
---|
[360b2e13] | 94 | return kindNames[ kind ]; |
---|
| 95 | } |
---|
| 96 | |
---|
[d76c588] | 97 | std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) { |
---|
| 98 | return out << data.kind << ", " << data.isComplete; |
---|
| 99 | } |
---|
| 100 | |
---|
[312029a] | 101 | // --- AggregateDecl |
---|
| 102 | |
---|
| 103 | // These must harmonize with the corresponding AggregateDecl::Aggregate enumerations. |
---|
| 104 | static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" }; |
---|
| 105 | |
---|
| 106 | const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) { |
---|
| 107 | return aggregateNames[aggr]; |
---|
| 108 | } |
---|
| 109 | |
---|
[a300e4a] | 110 | // --- EnumDecl |
---|
| 111 | |
---|
[9d6e7fa9] | 112 | bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const { |
---|
[a300e4a] | 113 | if ( enumValues.empty() ) { |
---|
| 114 | long long crntVal = 0; |
---|
[9d6e7fa9] | 115 | for ( const Decl * member : members ) { |
---|
[a300e4a] | 116 | const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member ); |
---|
| 117 | if ( field->init ) { |
---|
[87701b6] | 118 | const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() ); |
---|
[a300e4a] | 119 | auto result = eval( init->value ); |
---|
| 120 | if ( ! result.second ) { |
---|
[733074e] | 121 | SemanticError( init->location, ::toString( "Non-constexpr in initialization of " |
---|
[a300e4a] | 122 | "enumerator: ", field ) ); |
---|
| 123 | } |
---|
| 124 | crntVal = result.first; |
---|
| 125 | } |
---|
| 126 | if ( enumValues.count( field->name ) != 0 ) { |
---|
[733074e] | 127 | SemanticError( location, ::toString( "Enum ", name, " has multiple members with the " "name ", field->name ) ); |
---|
[a300e4a] | 128 | } |
---|
| 129 | enumValues[ field->name ] = crntVal; |
---|
| 130 | ++crntVal; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | auto it = enumValues.find( enumerator->name ); |
---|
| 135 | if ( it != enumValues.end() ) { |
---|
| 136 | value = it->second; |
---|
| 137 | return true; |
---|
| 138 | } |
---|
| 139 | return false; |
---|
| 140 | } |
---|
| 141 | |
---|
[2bb4a01] | 142 | } |
---|
| 143 | |
---|
| 144 | // Local Variables: // |
---|
| 145 | // tab-width: 4 // |
---|
| 146 | // mode: c++ // |
---|
| 147 | // compile-command: "make install" // |
---|
| 148 | // End: // |
---|