source: src/SynTree/AggregateDecl.cc @ 9939dc3

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 9939dc3 was 4559b34, checked in by JiadaL <j82liang@…>, 2 years ago

Update the String Enum implementation. The declaration now can handles creating the enum decl. But the compilation fails when trying to create reference to the Enum. Need a way to resolve InstTypes?

  • Property mode set to 100644
File size: 4.9 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// AggregateDecl.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 23:56:39 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Dec 16 15:07:20 2019
13// Update Count     : 31
14//
15
16#include <list>                  // for list
17#include <ostream>               // for operator<<, basic_ostream, ostream
18#include <string>                // for operator<<, string, char_traits
19
20#include "Attribute.h"           // for Attribute
21#include "Common/utility.h"      // for printAll, cloneAll, deleteAll
22#include "Declaration.h"         // for AggregateDecl, TypeDecl, Declaration
23#include "Expression.h"
24#include "Initializer.h"
25#include "LinkageSpec.h"         // for Spec, linkageName, Cforall
26#include "Type.h"                // for Type, Type::StorageClasses
27
28
29// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
30static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
31
32const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
33        return aggregateNames[aggr];
34}
35
36AggregateDecl::AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, Type::StorageClasses(), linkage ), body( false ), attributes( attributes ) {
37}
38
39AggregateDecl::AggregateDecl( const AggregateDecl &other ) : Parent( other ) {
40        cloneAll( other.members, members );
41        cloneAll( other.parameters, parameters );
42        cloneAll( other.attributes, attributes );
43        body = other.body;
44}
45
46AggregateDecl::~AggregateDecl() {
47        deleteAll( attributes );
48        deleteAll( parameters );
49        deleteAll( members );
50}
51
52void AggregateDecl::print( std::ostream &os, Indenter indent ) const {
53        using std::string;
54        using std::endl;
55
56        os << typeString() << " " << name << ":";
57        if ( get_linkage() != LinkageSpec::Cforall ) {
58                os << " " << LinkageSpec::name( linkage );
59        } // if
60        os << " with body " << has_body();
61        if ( ! parameters.empty() ) {
62                os << endl << indent << "... with parameters" << endl;
63                printAll( parameters, os, indent+1 );
64        } // if
65        if ( ! members.empty() ) {
66                os << endl << indent << "... with members" << endl;
67                printAll( members, os, indent+1 );
68        } // if
69        if ( ! attributes.empty() ) {
70                os << endl << indent << "... with attributes" << endl;
71                printAll( attributes, os, indent+1 );
72        } // if
73        os << endl;
74}
75
76void AggregateDecl::printShort( std::ostream &os, Indenter indent ) const {
77        using std::string;
78        using std::endl;
79
80        os << typeString() << " " << name << " with body " << has_body() << endl;
81
82        if ( ! parameters.empty() ) {
83                os << indent << "... with parameters" << endl;
84                printAll( parameters, os, indent+1 );
85        } // if
86}
87
88const char * StructDecl::typeString() const { return aggrString( kind ); }
89
90StructInstType * StructDecl::makeInst( std::list< Expression * > const & new_parameters ) {
91        std::list< Expression * > copy_parameters;
92        cloneAll( new_parameters, copy_parameters );
93        return makeInst( copy( copy_parameters ) );
94}
95
96StructInstType * StructDecl::makeInst( std::list< Expression * > && new_parameters ) {
97        assert( parameters.size() == new_parameters.size() );
98        StructInstType * type = new StructInstType( noQualifiers, this );
99        type->parameters = std::move( new_parameters );
100        return type;
101}
102
103const char * UnionDecl::typeString() const { return aggrString( Union ); }
104
105const char * EnumDecl::typeString() const { return aggrString( Enum ); }
106
107void EnumDecl::print( std::ostream & os, Indenter indent ) const {
108        AggregateDecl::print(os, indent);
109        os << " with base? " << (base? "True" : "False") << std::endl;
110        if ( base ) {
111                os << "Base Type of Enum:" << std::endl;
112                base->print(os, indent);
113        }
114        os <<  std::endl << "End of EnumDecl::print" << std::endl;
115}
116
117const char * TraitDecl::typeString() const { return aggrString( Trait ); }
118
119bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) {
120        if ( enumValues.empty() ) {
121                long long int currentValue = 0;
122                for ( Declaration * member : members ) {
123                        ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member );
124                        if ( field->init ) {
125                                SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init );
126                                auto result = eval( init->value );
127                                if ( ! result.second ) SemanticError( init->location, toString( "Non-constexpr in initialization of enumerator: ", field ) );
128                                currentValue = result.first;
129                        }
130                        assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() );
131                        enumValues[ field->name ] = currentValue;
132                        ++currentValue;
133                }
134        }
135        if ( enumValues.count( enumerator->name ) ) {
136                value = enumValues[ enumerator->name ];
137                return true;
138        }
139        return false;
140}
141
142// Local Variables: //
143// tab-width: 4 //
144// mode: c++ //
145// compile-command: "make install" //
146// End: //
Note: See TracBrowser for help on using the repository browser.