source: src/AST/Decl.cpp@ 9802f4c

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 9802f4c was e0e9a0b, checked in by Aaron Moss <a3moss@…>, 6 years ago

Somewhat deeper clone for types with forall qualifiers.

  • Property mode set to 100644
File size: 3.4 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 <iostream>
20#include <string>
21#include <unordered_map>
22
23#include "Common/utility.h"
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 "Parser/ParseNode.h" // for DeclarationNode
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 idMap[ uniqueId ] = this;
43}
44
45readonly<Decl> Decl::fromId( UniqueId id ) {
46 IdMapType::const_iterator i = idMap.find( id );
47 if ( i != idMap.end() ) return i->second;
48 return {};
49}
50
51// --- FunctionDecl
52
53const Type * FunctionDecl::get_type() const { return type.get(); }
54void FunctionDecl::set_type( const Type * t ) {
55 type = strict_dynamic_cast< const FunctionType * >( t );
56}
57
58// --- TypeDecl
59
60std::string TypeDecl::typeString() const {
61 static const std::string kindNames[] = { "object type", "function type", "tuple type" };
62 assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1,
63 "typeString: kindNames is out of sync." );
64 assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." );
65 return (sized ? "sized " : "") + kindNames[ kind ];
66}
67
68std::string TypeDecl::genTypeString() const {
69 static const std::string kindNames[] = { "dtype", "ftype", "ttype" };
70 assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1, "genTypeString: kindNames is out of sync." );
71 assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." );
72 return kindNames[ kind ];
73}
74
75std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) {
76 return out << data.kind << ", " << data.isComplete;
77}
78
79// --- EnumDecl
80
81bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const {
82 if ( enumValues.empty() ) {
83 long long crntVal = 0;
84 for ( const Decl * member : members ) {
85 const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
86 if ( field->init ) {
87 const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() );
88 auto result = eval( init->value );
89 if ( ! result.second ) {
90 SemanticError( init->location, ::toString( "Non-constexpr in initialization of "
91 "enumerator: ", field ) );
92 }
93 crntVal = result.first;
94 }
95 if ( enumValues.count( field->name ) != 0 ) {
96 SemanticError( location, ::toString( "Enum ", name, " has multiple members with the " "name ", field->name ) );
97 }
98 enumValues[ field->name ] = crntVal;
99 ++crntVal;
100 }
101 }
102
103 auto it = enumValues.find( enumerator->name );
104 if ( it != enumValues.end() ) {
105 value = it->second;
106 return true;
107 }
108 return false;
109}
110
111}
112
113// Local Variables: //
114// tab-width: 4 //
115// mode: c++ //
116// compile-command: "make install" //
117// End: //
Note: See TracBrowser for help on using the repository browser.