source: src/AST/Decl.cpp @ 312029a

arm-ehjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since 312029a was 312029a, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

move enum Aggregate from DeclarationNode? to AggregateDecl?, add control-keyword field-dereference to replace control-keyword cast

  • Property mode set to 100644
File size: 3.8 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 : Wed Dec 11 16:41:39 2019
13// Update Count     : 18
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#include "Parser/ParseNode.h"  // for DeclarationNode
29
30namespace ast {
31
32// To canonicalize declarations
33static UniqueId lastUniqueId = 0;
34
35using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >;
36static IdMapType idMap;
37
38void Decl::fixUniqueId() {
39        if ( uniqueId ) return;  // ensure only set once
40        uniqueId = ++lastUniqueId;
41        idMap[ uniqueId ] = this;
42}
43
44readonly<Decl> Decl::fromId( UniqueId id ) {
45        IdMapType::const_iterator i = idMap.find( id );
46        if ( i != idMap.end() ) return i->second;
47        return {};
48}
49
50// --- FunctionDecl
51
52const Type * FunctionDecl::get_type() const { return type.get(); }
53void FunctionDecl::set_type(Type * t) { type = strict_dynamic_cast< FunctionType* >( t ); }
54
55// --- TypeDecl
56
57const char * TypeDecl::typeString() const {
58        static const char * kindNames[] = { "sized object type", "sized function type", "sized tuple type" };
59        assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1,
60                "typeString: kindNames is out of sync." );
61        assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." );
62        return sized ? kindNames[ kind ] : &kindNames[ kind ][ sizeof("sized") ]; // sizeof includes '\0'
63}
64
65const char * TypeDecl::genTypeString() const {
66        static const char * kindNames[] = { "dtype", "ftype", "ttype" };
67        assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1, "genTypeString: kindNames is out of sync." );
68        assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." );
69        return kindNames[ kind ];
70}
71
72std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) {
73        return out << data.kind << ", " << data.isComplete;
74}
75
76// --- AggregateDecl
77
78// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
79static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
80
81const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
82        return aggregateNames[aggr];
83}
84
85// --- EnumDecl
86
87bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const {
88        if ( enumValues.empty() ) {
89                long long crntVal = 0;
90                for ( const Decl * member : members ) {
91                        const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
92                        if ( field->init ) {
93                                const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() );
94                                auto result = eval( init->value );
95                                if ( ! result.second ) {
96                                        SemanticError( init->location, ::toString( "Non-constexpr in initialization of "
97                                                "enumerator: ", field ) );
98                                }
99                                crntVal = result.first;
100                        }
101                        if ( enumValues.count( field->name ) != 0 ) {
102                                SemanticError( location, ::toString( "Enum ", name, " has multiple members with the "   "name ", field->name ) );
103                        }
104                        enumValues[ field->name ] = crntVal;
105                        ++crntVal;
106                }
107        }
108
109        auto it = enumValues.find( enumerator->name );
110        if ( it != enumValues.end() ) {
111                value = it->second;
112                return true;
113        }
114        return false;
115}
116
117}
118
119// Local Variables: //
120// tab-width: 4 //
121// mode: c++ //
122// compile-command: "make install" //
123// End: //
Note: See TracBrowser for help on using the repository browser.