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