source: src/AST/Decl.cpp@ dd900b5

Last change on this file since dd900b5 was 37273c8, checked in by Andrew Beach <ajbeach@…>, 23 months ago

Removed the old-ast-compatable FunctionDecl constructor. However, enough cases pass nothing polymorphic along some of the uses of the constructor now go to a new monomorphic function constructor.

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