source: src/AST/Decl.cpp @ bc4a433

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since bc4a433 was 6e50a6b, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Implementing language-provided syntax for (array) dimensions.

Former z(i) and Z(N) macros are eliminated.

  • Property mode set to 100644
File size: 4.7 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 : Peter A. Buhr
12// Last Modified On : Tue Jan 12 16:54:55 2021
13// Update Count     : 23
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/utility.h"
23
24#include "Fwd.hpp"             // for UniqueId
25#include "Init.hpp"
26#include "Node.hpp"            // for readonly
27#include "Type.hpp"            // for readonly
28
29namespace ast {
30
31// To canonicalize declarations
32static UniqueId lastUniqueId = 0;
33
34using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >;
35static IdMapType idMap;
36
37void Decl::fixUniqueId() {
38        if ( uniqueId ) return;  // ensure only set once
39        uniqueId = ++lastUniqueId;
40        idMap[ uniqueId ] = this;
41}
42
43readonly<Decl> Decl::fromId( UniqueId id ) {
44        IdMapType::const_iterator i = idMap.find( id );
45        if ( i != idMap.end() ) return i->second;
46        return {};
47}
48
49// --- FunctionDecl
50
51FunctionDecl::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        type_params(std::move(forall)), 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        for (auto & tp : this->type_params) {
66                ftype->forall.emplace_back(new TypeInstType(tp->name, tp));
67        }
68        this->type = ftype;
69}
70
71
72const Type * FunctionDecl::get_type() const { return type.get(); }
73void FunctionDecl::set_type( const Type * t ) {
74        type = strict_dynamic_cast< const FunctionType * >( t );
75}
76
77// --- TypeDecl
78
79const char * TypeDecl::typeString() const {
80        static const char * kindNames[] = { "sized data type", "sized data type", "sized object type", "sized function type", "sized tuple type", "sized length value" };
81        static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "typeString: kindNames is out of sync." );
82        assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
83        return sized ? kindNames[ kind ] : &kindNames[ kind ][ sizeof("sized") ]; // sizeof includes '\0'
84}
85
86const char * TypeDecl::genTypeString() const {
87        static const char * kindNames[] = { "T &", "T *", "T", "(*)", "T ...", "[T]" };
88        static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "genTypeString: kindNames is out of sync." );
89        assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
90        return kindNames[ kind ];
91}
92
93std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) {
94        return out << data.kind << ", " << data.isComplete;
95}
96
97// --- AggregateDecl
98
99// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
100static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
101
102const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
103        return aggregateNames[aggr];
104}
105
106// --- EnumDecl
107
108bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const {
109        if ( enumValues.empty() ) {
110                long long crntVal = 0;
111                for ( const Decl * member : members ) {
112                        const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
113                        if ( field->init ) {
114                                const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() );
115                                auto result = eval( init->value );
116                                if ( ! result.second ) {
117                                        SemanticError( init->location, ::toString( "Non-constexpr in initialization of "
118                                                "enumerator: ", field ) );
119                                }
120                                crntVal = result.first;
121                        }
122                        if ( enumValues.count( field->name ) != 0 ) {
123                                SemanticError( location, ::toString( "Enum ", name, " has multiple members with the "   "name ", field->name ) );
124                        }
125                        enumValues[ field->name ] = crntVal;
126                        ++crntVal;
127                }
128        }
129
130        auto it = enumValues.find( enumerator->name );
131        if ( it != enumValues.end() ) {
132                value = it->second;
133                return true;
134        }
135        return false;
136}
137
138}
139
140// Local Variables: //
141// tab-width: 4 //
142// mode: c++ //
143// compile-command: "make install" //
144// End: //
Note: See TracBrowser for help on using the repository browser.