| 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 : Fri Jul  1 09:12:33 2022 | 
|---|
| 13 | // Update Count     : 32 | 
|---|
| 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/Eval.h"         // for eval | 
|---|
| 22 | #include "Common/utility.h"      // for printAll, cloneAll, deleteAll | 
|---|
| 23 | #include "Declaration.h"         // for AggregateDecl, TypeDecl, Declaration | 
|---|
| 24 | #include "Expression.h" | 
|---|
| 25 | #include "Initializer.h" | 
|---|
| 26 | #include "LinkageSpec.h"         // for Spec, linkageName, Cforall | 
|---|
| 27 | #include "Type.h"                // for Type, Type::StorageClasses | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | // These must harmonize with the corresponding AggregateDecl::Aggregate enumerations. | 
|---|
| 31 | static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" }; | 
|---|
| 32 |  | 
|---|
| 33 | const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) { | 
|---|
| 34 | return aggregateNames[aggr]; | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | AggregateDecl::AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, Type::StorageClasses(), linkage ), body( false ), attributes( attributes ) { | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | AggregateDecl::AggregateDecl( const AggregateDecl &other ) : Parent( other ) { | 
|---|
| 41 | cloneAll( other.members, members ); | 
|---|
| 42 | cloneAll( other.parameters, parameters ); | 
|---|
| 43 | cloneAll( other.attributes, attributes ); | 
|---|
| 44 | body = other.body; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | AggregateDecl::~AggregateDecl() { | 
|---|
| 48 | deleteAll( attributes ); | 
|---|
| 49 | deleteAll( parameters ); | 
|---|
| 50 | deleteAll( members ); | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | void AggregateDecl::print( std::ostream &os, Indenter indent ) const { | 
|---|
| 54 | using std::string; | 
|---|
| 55 | using std::endl; | 
|---|
| 56 |  | 
|---|
| 57 | os << typeString() << " " << name << ":"; | 
|---|
| 58 | if ( get_linkage() != LinkageSpec::Cforall ) { | 
|---|
| 59 | os << " " << LinkageSpec::name( linkage ); | 
|---|
| 60 | } // if | 
|---|
| 61 | os << " with body " << has_body(); | 
|---|
| 62 | if ( ! parameters.empty() ) { | 
|---|
| 63 | os << endl << indent << "... with parameters" << endl; | 
|---|
| 64 | printAll( parameters, os, indent+1 ); | 
|---|
| 65 | } // if | 
|---|
| 66 | if ( ! members.empty() ) { | 
|---|
| 67 | os << endl << indent << "... with members" << endl; | 
|---|
| 68 | printAll( members, os, indent+1 ); | 
|---|
| 69 | } // if | 
|---|
| 70 | if ( ! attributes.empty() ) { | 
|---|
| 71 | os << endl << indent << "... with attributes" << endl; | 
|---|
| 72 | printAll( attributes, os, indent+1 ); | 
|---|
| 73 | } // if | 
|---|
| 74 | os << endl; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | void AggregateDecl::printShort( std::ostream &os, Indenter indent ) const { | 
|---|
| 78 | using std::string; | 
|---|
| 79 | using std::endl; | 
|---|
| 80 |  | 
|---|
| 81 | os << typeString() << " " << name << " with body " << has_body() << endl; | 
|---|
| 82 |  | 
|---|
| 83 | if ( ! parameters.empty() ) { | 
|---|
| 84 | os << indent << "... with parameters" << endl; | 
|---|
| 85 | printAll( parameters, os, indent+1 ); | 
|---|
| 86 | } // if | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | const char * StructDecl::typeString() const { return aggrString( kind ); } | 
|---|
| 90 |  | 
|---|
| 91 | StructInstType * StructDecl::makeInst( std::list< Expression * > const & new_parameters ) { | 
|---|
| 92 | std::list< Expression * > copy_parameters; | 
|---|
| 93 | cloneAll( new_parameters, copy_parameters ); | 
|---|
| 94 | return makeInst( copy( copy_parameters ) ); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | StructInstType * 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 |  | 
|---|
| 104 | const char * UnionDecl::typeString() const { return aggrString( Union ); } | 
|---|
| 105 |  | 
|---|
| 106 | const char * EnumDecl::typeString() const { return aggrString( Enum ); } | 
|---|
| 107 |  | 
|---|
| 108 | void 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 |  | 
|---|
| 118 | const char * TraitDecl::typeString() const { return aggrString( Trait ); } | 
|---|
| 119 |  | 
|---|
| 120 | bool 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 ); | 
|---|
| 127 | auto result = eval( init->value ); | 
|---|
| 128 | if ( ! result.second ) SemanticError( init->location, toString( "Enumerator value for '", field, "' is not an integer constant" ) ); | 
|---|
| 129 | currentValue = result.first; | 
|---|
| 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 |  | 
|---|
| 143 | // Local Variables: // | 
|---|
| 144 | // tab-width: 4 // | 
|---|
| 145 | // mode: c++ // | 
|---|
| 146 | // compile-command: "make install" // | 
|---|
| 147 | // End: // | 
|---|