source: src/AST/Decl.cpp@ 5ad24a2c

Last change on this file since 5ad24a2c was 8941b6b, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Direct translation of code generation.

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