source: src/AST/Decl.cpp @ 2a5e8a6

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 2a5e8a6 was 360b2e13, checked in by Aaron Moss <a3moss@…>, 5 years ago

Add TypeDecl? to new AST

  • Property mode set to 100644
File size: 3.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 : Aaron B. Moss
12// Last Modified On : Thu May 9 10:00:00 2019
13// Update Count     : 1
14//
15
16#include "Decl.hpp"
17
18#include <cassert>             // for assert, strict_dynamic_cast
19#include <string>
20#include <unordered_map>
21
22#include "Fwd.hpp"             // for UniqueId
23#include "Init.hpp"
24#include "Node.hpp"            // for readonly
25#include "Parser/ParseNode.h"  // for DeclarationNode
26
27namespace ast {
28
29// To canonicalize declarations
30static UniqueId lastUniqueId = 0;
31
32using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >;
33static IdMapType idMap;
34
35void Decl::fixUniqueId() {
36        if ( uniqueId ) return;  // ensure only set once
37        uniqueId = ++lastUniqueId;
38        idMap[ uniqueId ] = this;
39}
40
41readonly<Decl> Decl::fromId( UniqueId id ) {
42        IdMapType::const_iterator i = idMap.find( id );
43        if ( i != idMap.end() ) return i->second;
44        return {};
45}
46
47// --- TypeDecl
48
49std::string TypeDecl::typeString() const {
50        static const std::string kindNames[] = { "object type", "function type", "tuple type" };
51        assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1, 
52                "typeString: kindNames is out of sync." );
53        assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." );
54        return (sized ? "sized " : "") + kindNames[ kind ];
55}
56
57std::string TypeDecl::genTypeString() const {
58        static const std::string kindNames[] = { "dtype", "ftype", "ttype" };
59        assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1, "genTypeString: kindNames is out of sync." );
60        assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." );
61        return kindNames[ kind ];
62}
63
64// --- EnumDecl
65
66bool EnumDecl::valueOf( Decl* enumerator, long long& value ) const {
67        if ( enumValues.empty() ) {
68                long long crntVal = 0;
69                for ( const Decl* member : members ) {
70                        const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
71                        if ( field->init ) {
72                                const SingleInit* init = strict_dynamic_cast< const SingleInit* >( field->init );
73                                auto result = eval( init->value );
74                                if ( ! result.second ) {
75                                        SemanticError( init->location, toString( "Non-constexpr in initialization of "
76                                                "enumerator: ", field ) );
77                                }
78                                crntVal = result.first;
79                        }
80                        if ( enumValues.count( field->name ) != 0 ) {
81                                SemanticError( location, toString( "Enum ", name, " has multiple members with the "     "name ", field->name ) );
82                        }
83                        enumValues[ field->name ] = crntVal;
84                        ++crntVal;
85                }
86        }
87
88        auto it = enumValues.find( enumerator->name );
89        if ( it != enumValues.end() ) {
90                value = it->second;
91                return true;
92        }
93        return false;
94}
95
96}
97
98// Local Variables: //
99// tab-width: 4 //
100// mode: c++ //
101// compile-command: "make install" //
102// End: //
Note: See TracBrowser for help on using the repository browser.