source: src/AST/Decl.cpp@ 4bc4b4c

Last change on this file since 4bc4b4c was c36a419, checked in by Andrew Beach <ajbeach@…>, 23 months ago

Removed Decl::fromId as it was unused. There are a few places that use uniqueId directly.

  • Property mode set to 100644
File size: 7.1 KB
Line 
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 : Andrew Beach
12// Last Modified On : Thu May 5 12:10:00 2022
13// Update Count : 24
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/Eval.h" // for eval
23
24#include "Fwd.hpp" // for UniqueId
25#include "Init.hpp"
26#include "Node.hpp" // for readonly
27#include "Type.hpp" // for readonly
28#include "Expr.hpp"
29
30namespace ast {
31
32// To canonicalize declarations
33static UniqueId lastUniqueId = 0;
34
35void Decl::fixUniqueId() {
36 if ( uniqueId ) return; // ensure only set once
37 uniqueId = ++lastUniqueId;
38}
39
40// --- FunctionDecl
41
42FunctionDecl::FunctionDecl( const CodeLocation & loc, const std::string & name,
43 std::vector<ptr<TypeDecl>>&& forall,
44 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
45 CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage,
46 std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, ArgumentFlag isVarArgs )
47: DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ),
48 type_params(std::move(forall)), assertions(),
49 params(std::move(params)), returns(std::move(returns)), stmts( stmts ) {
50 FunctionType * ftype = new FunctionType( isVarArgs );
51 for (auto & param : this->params) {
52 ftype->params.emplace_back(param->get_type());
53 }
54 for (auto & ret : this->returns) {
55 ftype->returns.emplace_back(ret->get_type());
56 }
57 for (auto & tp : this->type_params) {
58 ftype->forall.emplace_back(new TypeInstType(tp));
59 for (auto & ap: tp->assertions) {
60 ftype->assertions.emplace_back(new VariableExpr(loc, ap));
61 }
62 }
63 this->type = ftype;
64}
65
66FunctionDecl::FunctionDecl( const CodeLocation & location, const std::string & name,
67 std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions,
68 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
69 CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage,
70 std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, ArgumentFlag isVarArgs )
71: DeclWithType( location, name, storage, linkage, std::move(attrs), fs ),
72 type_params( std::move( forall) ), assertions( std::move( assertions ) ),
73 params( std::move(params) ), returns( std::move(returns) ),
74 type( nullptr ), stmts( stmts ) {
75 FunctionType * type = new FunctionType( isVarArgs );
76 for ( auto & param : this->params ) {
77 type->params.emplace_back( param->get_type() );
78 }
79 for ( auto & ret : this->returns ) {
80 type->returns.emplace_back( ret->get_type() );
81 }
82 for ( auto & param : this->type_params ) {
83 type->forall.emplace_back( new TypeInstType( param ) );
84 }
85 for ( auto & assertion : this->assertions ) {
86 type->assertions.emplace_back(
87 new VariableExpr( assertion->location, assertion ) );
88 }
89 this->type = type;
90}
91
92
93const Type * FunctionDecl::get_type() const { return type.get(); }
94void FunctionDecl::set_type( const Type * t ) {
95 type = strict_dynamic_cast< const FunctionType * >( t );
96}
97
98// --- TypeDecl
99
100const char * TypeDecl::typeString() const {
101 static const char * kindNames[] = { "sized data type", "sized data type", "sized object type", "sized function type", "sized tuple type", "sized length value" };
102 static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "typeString: kindNames is out of sync." );
103 assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
104 // sizeof("sized") includes '\0' and gives the offset to remove "sized ".
105 return sized ? kindNames[ kind ] : &kindNames[ kind ][ sizeof("sized") ];
106}
107
108const char * TypeDecl::genTypeString() const {
109 static const char * kindNames[] = { "T &", "T *", "T", "(*)", "T ...", "[T]" };
110 static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "genTypeString: kindNames is out of sync." );
111 assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
112 return kindNames[ kind ];
113}
114
115std::ostream & operator<< ( std::ostream & out, const TypeData & data ) {
116 return out << data.kind << ", " << data.isComplete;
117}
118
119// --- AggregateDecl
120
121// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
122static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
123
124const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
125 return aggregateNames[aggr];
126}
127
128// --- EnumDecl
129
130bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const {
131 if ( enumValues.empty() ) {
132 Evaluation crntVal = {0, true, true}; // until expression is given, we know to start counting from 0
133 for ( const Decl * member : members ) {
134 const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
135 if ( field->init ) {
136 const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() );
137 crntVal = eval( init->value );
138 if ( ! crntVal.isEvaluableInGCC ) {
139 SemanticError( init->location, ::toString( "Non-constexpr in initialization of "
140 "enumerator: ", field ) );
141 }
142 }
143 if ( enumValues.count( field->name ) != 0 ) {
144 SemanticError( location, ::toString( "Enum ", name, " has multiple members with the " "name ", field->name ) );
145 }
146 if (crntVal.hasKnownValue) {
147 enumValues[ field->name ] = crntVal.knownValue;
148 }
149 ++crntVal.knownValue;
150 }
151 }
152
153 auto it = enumValues.find( enumerator->name );
154
155 if ( it != enumValues.end() ) {
156
157 // Handle typed enum by casting the value in (C++) compiler
158 // if ( base ) { // A typed enum
159 // if ( const BasicType * bt = dynamic_cast<const BasicType *>(base) ) {
160 // switch( bt->kind ) {
161 // case BasicType::Kind::Bool: value = (bool) it->second; break;
162 // case BasicType::Kind::Char: value = (char) it->second; break;
163 // case BasicType::Kind::SignedChar: value = (signed char) it->second; break;
164 // case BasicType::Kind::UnsignedChar: value = (unsigned char) it->second; break;
165 // case BasicType::Kind::ShortSignedInt: value = (short signed int) it->second; break;
166 // case BasicType::Kind::SignedInt: value = (signed int) it->second; break;
167 // case BasicType::Kind::UnsignedInt: value = (unsigned int) it->second; break;
168 // case BasicType::Kind::LongSignedInt: value = (long signed int) it->second; break;
169 // case BasicType::Kind::LongUnsignedInt: value = (long unsigned int) it->second; break;
170 // case BasicType::Kind::LongLongSignedInt: value = (long long signed int) it->second; break;
171 // case BasicType::Kind::LongLongUnsignedInt: value = (long long unsigned int) it->second; break;
172 // // TODO: value should be able to handle long long unsigned int
173
174 // default:
175 // value = it->second;
176 // }
177 // }
178 // } else {
179 value = it->second;
180 //}
181
182 return true;
183 }
184 return false;
185}
186
187}
188
189// Local Variables: //
190// tab-width: 4 //
191// mode: c++ //
192// compile-command: "make install" //
193// End: //
Note: See TracBrowser for help on using the repository browser.