source: src/SynTree/AggregateDecl.cc @ 8f31be6

Last change on this file since 8f31be6 was 8f06277, checked in by Andrew Beach <ajbeach@…>, 17 months ago

Some clean-up in Common/utility.h. Deleted some unused declarations and moved others to one of two new headers.

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