[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 | // |
---|
[1e8b02f5] | 7 | // ReferenceToType.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[43c89a7] | 12 | // Last Modified On : Thu Feb 23 16:38:54 2017 |
---|
| 13 | // Update Count : 24 |
---|
[0dd3a2f] | 14 | // |
---|
[51b7345] | 15 | |
---|
[9bfc9da] | 16 | #include <cassert> // for assert |
---|
| 17 | #include <list> // for list, _List_const_iterator, list<>::cons... |
---|
| 18 | #include <ostream> // for operator<<, basic_ostream, ostream, endl |
---|
| 19 | #include <string> // for string, operator<<, char_traits, operator== |
---|
[51b7345] | 20 | |
---|
[9bfc9da] | 21 | #include "Common/utility.h" // for printAll, cloneAll, deleteAll |
---|
| 22 | #include "Declaration.h" // for StructDecl, UnionDecl, EnumDecl, Declara... |
---|
| 23 | #include "Expression.h" // for Expression |
---|
| 24 | #include "Type.h" // for TypeInstType, StructInstType, UnionInstType |
---|
| 25 | #include "TypeSubstitution.h" // for TypeSubstitution |
---|
[cd6a6ff] | 26 | #include "CompilationState.h" |
---|
[ea6332d] | 27 | |
---|
| 28 | class Attribute; |
---|
[51b7345] | 29 | |
---|
[43c89a7] | 30 | ReferenceToType::ReferenceToType( const Type::Qualifiers &tq, const std::string &name, const std::list< Attribute * > & attributes ) : Type( tq, attributes ), name( name ), hoistType( false ) { |
---|
[51b7345] | 31 | } |
---|
| 32 | |
---|
[43c89a7] | 33 | ReferenceToType::ReferenceToType( const ReferenceToType &other ) : Type( other ), name( other.name ), hoistType( other.hoistType ) { |
---|
[a08ba92] | 34 | cloneAll( other.parameters, parameters ); |
---|
[51b7345] | 35 | } |
---|
| 36 | |
---|
[0dd3a2f] | 37 | ReferenceToType::~ReferenceToType() { |
---|
[a08ba92] | 38 | deleteAll( parameters ); |
---|
[51b7345] | 39 | } |
---|
| 40 | |
---|
[50377a4] | 41 | void ReferenceToType::print( std::ostream &os, Indenter indent ) const { |
---|
[a08ba92] | 42 | using std::endl; |
---|
[1e8b02f5] | 43 | |
---|
[a08ba92] | 44 | Type::print( os, indent ); |
---|
| 45 | os << "instance of " << typeString() << " " << name << " "; |
---|
| 46 | if ( ! parameters.empty() ) { |
---|
[50377a4] | 47 | os << endl << indent << "... with parameters" << endl; |
---|
| 48 | printAll( parameters, os, indent+1 ); |
---|
[a08ba92] | 49 | } // if |
---|
[51b7345] | 50 | } |
---|
| 51 | |
---|
| 52 | namespace { |
---|
[4e7cc5ce] | 53 | void doLookup( const std::list< Declaration * > & members, const std::string & name, std::list< Declaration* > & foundDecls ) { |
---|
| 54 | for ( Declaration * decl : members ) { |
---|
| 55 | if ( decl->name == name ) { |
---|
| 56 | foundDecls.push_back( decl ); |
---|
[0dd3a2f] | 57 | } // if |
---|
| 58 | } // for |
---|
[51b7345] | 59 | } |
---|
| 60 | } // namespace |
---|
| 61 | |
---|
[c0aa336] | 62 | StructInstType::StructInstType( const Type::Qualifiers & tq, StructDecl * baseStruct, const std::list< Attribute * > & attributes ) : |
---|
[4e7cc5ce] | 63 | Parent( tq, baseStruct->name, attributes ), baseStruct( baseStruct ) {} |
---|
[f006f01] | 64 | |
---|
[51b7345] | 65 | std::string StructInstType::typeString() const { return "struct"; } |
---|
| 66 | |
---|
[9bfc9da] | 67 | const std::list<TypeDecl*>* StructInstType::get_baseParameters() const { |
---|
| 68 | if ( ! baseStruct ) return nullptr; |
---|
| 69 | return &baseStruct->get_parameters(); |
---|
| 70 | } |
---|
| 71 | |
---|
[ed94eac] | 72 | std::list<TypeDecl*>* StructInstType::get_baseParameters() { |
---|
[50377a4] | 73 | if ( ! baseStruct ) return nullptr; |
---|
[ed94eac] | 74 | return &baseStruct->get_parameters(); |
---|
| 75 | } |
---|
[37a3b8f9] | 76 | |
---|
[c0aa336] | 77 | bool StructInstType::isComplete() const { return baseStruct ? baseStruct->has_body() : false; } |
---|
[4a9ccc3] | 78 | |
---|
[0b3b2ae] | 79 | AggregateDecl * StructInstType::getAggr() const { return baseStruct; } |
---|
[373d0b5] | 80 | |
---|
[9bfc9da] | 81 | TypeSubstitution StructInstType::genericSubstitution() const { |
---|
| 82 | return TypeSubstitution( get_baseParameters()->begin(), get_baseParameters()->end(), parameters.begin() ); |
---|
| 83 | } |
---|
| 84 | |
---|
[0dd3a2f] | 85 | void StructInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { |
---|
[a08ba92] | 86 | assert( baseStruct ); |
---|
[4e7cc5ce] | 87 | doLookup( baseStruct->members, name, foundDecls ); |
---|
[51b7345] | 88 | } |
---|
| 89 | |
---|
[50377a4] | 90 | void StructInstType::print( std::ostream &os, Indenter indent ) const { |
---|
[5d125e4] | 91 | using std::endl; |
---|
| 92 | |
---|
[50377a4] | 93 | if ( baseStruct == nullptr ) ReferenceToType::print( os, indent ); |
---|
[5d125e4] | 94 | else { |
---|
| 95 | Type::print( os, indent ); |
---|
[08222c7] | 96 | os << "instance of " << typeString() << " " << name << " with body " << baseStruct->has_body(); |
---|
[5d125e4] | 97 | if ( ! parameters.empty() ) { |
---|
[50377a4] | 98 | os << endl << indent << "... with parameters" << endl; |
---|
| 99 | printAll( parameters, os, indent+1 ); |
---|
[5d125e4] | 100 | } // if |
---|
| 101 | } // if |
---|
| 102 | } |
---|
| 103 | |
---|
[c0aa336] | 104 | |
---|
| 105 | UnionInstType::UnionInstType( const Type::Qualifiers & tq, UnionDecl * baseUnion, const std::list< Attribute * > & attributes ) : |
---|
[4e7cc5ce] | 106 | Parent( tq, baseUnion->name, attributes ), baseUnion( baseUnion ) {} |
---|
[c0aa336] | 107 | |
---|
[51b7345] | 108 | std::string UnionInstType::typeString() const { return "union"; } |
---|
| 109 | |
---|
[c0aa336] | 110 | std::list< TypeDecl * > * UnionInstType::get_baseParameters() { |
---|
[50377a4] | 111 | if ( ! baseUnion ) return nullptr; |
---|
[ed94eac] | 112 | return &baseUnion->get_parameters(); |
---|
| 113 | } |
---|
[37a3b8f9] | 114 | |
---|
[9bfc9da] | 115 | const std::list< TypeDecl * > * UnionInstType::get_baseParameters() const { |
---|
| 116 | if ( ! baseUnion ) return nullptr; |
---|
| 117 | return &baseUnion->get_parameters(); |
---|
| 118 | } |
---|
| 119 | |
---|
[c0aa336] | 120 | bool UnionInstType::isComplete() const { return baseUnion ? baseUnion->has_body() : false; } |
---|
[4a9ccc3] | 121 | |
---|
[0b3b2ae] | 122 | AggregateDecl * UnionInstType::getAggr() const { return baseUnion; } |
---|
[373d0b5] | 123 | |
---|
[9bfc9da] | 124 | TypeSubstitution UnionInstType::genericSubstitution() const { |
---|
| 125 | return TypeSubstitution( get_baseParameters()->begin(), get_baseParameters()->end(), parameters.begin() ); |
---|
| 126 | } |
---|
| 127 | |
---|
[0dd3a2f] | 128 | void UnionInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { |
---|
[a08ba92] | 129 | assert( baseUnion ); |
---|
[4e7cc5ce] | 130 | doLookup( baseUnion->members, name, foundDecls ); |
---|
[51b7345] | 131 | } |
---|
| 132 | |
---|
[50377a4] | 133 | void UnionInstType::print( std::ostream &os, Indenter indent ) const { |
---|
[5d125e4] | 134 | using std::endl; |
---|
| 135 | |
---|
[50377a4] | 136 | if ( baseUnion == nullptr ) ReferenceToType::print( os, indent ); |
---|
[5d125e4] | 137 | else { |
---|
| 138 | Type::print( os, indent ); |
---|
[08222c7] | 139 | os << "instance of " << typeString() << " " << name << " with body " << baseUnion->has_body(); |
---|
[5d125e4] | 140 | if ( ! parameters.empty() ) { |
---|
[50377a4] | 141 | os << endl << indent << "... with parameters" << endl; |
---|
| 142 | printAll( parameters, os, indent+1 ); |
---|
[5d125e4] | 143 | } // if |
---|
| 144 | } // if |
---|
| 145 | } |
---|
| 146 | |
---|
[c0aa336] | 147 | |
---|
| 148 | EnumInstType::EnumInstType( const Type::Qualifiers & tq, EnumDecl * baseEnum, const std::list< Attribute * > & attributes ) : |
---|
| 149 | Parent( tq, baseEnum->get_name(), attributes ), baseEnum( baseEnum ) {} |
---|
| 150 | |
---|
[51b7345] | 151 | std::string EnumInstType::typeString() const { return "enum"; } |
---|
| 152 | |
---|
[c0aa336] | 153 | bool EnumInstType::isComplete() const { return baseEnum ? baseEnum->has_body() : false; } |
---|
| 154 | |
---|
[0b3b2ae] | 155 | AggregateDecl * EnumInstType::getAggr() const { return baseEnum; } |
---|
| 156 | |
---|
[6137fbb] | 157 | void EnumInstType::print( std::ostream &os, Indenter indent ) const { |
---|
| 158 | using std::endl; |
---|
| 159 | |
---|
| 160 | if ( baseEnum == nullptr ) ReferenceToType::print( os, indent ); |
---|
| 161 | else { |
---|
| 162 | Type::print( os, indent ); |
---|
[08222c7] | 163 | os << "instance of " << typeString() << " " << name << " with body " << baseEnum->has_body(); |
---|
[6137fbb] | 164 | } // if |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
[4a9ccc3] | 168 | std::string TraitInstType::typeString() const { return "trait"; } |
---|
[51b7345] | 169 | |
---|
[be9036d] | 170 | TraitInstType::TraitInstType( const Type::Qualifiers & tq, TraitDecl * baseTrait, const std::list< Attribute * > & attributes ) : Parent( tq, baseTrait->name, attributes ), baseTrait( baseTrait ) {} |
---|
| 171 | |
---|
| 172 | TraitInstType::TraitInstType( const TraitInstType &other ) : Parent( other ), baseTrait( other.baseTrait ) { |
---|
[51b7345] | 173 | } |
---|
| 174 | |
---|
[4040425] | 175 | TraitInstType::~TraitInstType() { |
---|
[51b7345] | 176 | } |
---|
| 177 | |
---|
[4a9ccc3] | 178 | bool TraitInstType::isComplete() const { assert( false ); } |
---|
| 179 | |
---|
[c0aa336] | 180 | TypeInstType::TypeInstType( const Type::Qualifiers &tq, const std::string &name, TypeDecl *baseType, const std::list< Attribute * > & attributes ) : Parent( tq, name, attributes ) { |
---|
[a08ba92] | 181 | set_baseType( baseType ); |
---|
[51b7345] | 182 | } |
---|
| 183 | |
---|
[c0aa336] | 184 | TypeInstType::TypeInstType( const Type::Qualifiers &tq, const std::string &name, bool isFtype, const std::list< Attribute * > & attributes ) : Parent( tq, name, attributes ), baseType( 0 ), isFtype( isFtype ) { |
---|
[51b7345] | 185 | } |
---|
| 186 | |
---|
[2c57025] | 187 | TypeInstType::TypeInstType( const TypeInstType &other ) : Parent( other ), baseType( other.baseType ), isFtype( other.isFtype ) { |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | |
---|
[1e8b02f5] | 191 | TypeInstType::~TypeInstType() { |
---|
| 192 | // delete baseType; //This is shared and should not be deleted |
---|
| 193 | } |
---|
| 194 | |
---|
[0dd3a2f] | 195 | void TypeInstType::set_baseType( TypeDecl *newValue ) { |
---|
[a08ba92] | 196 | baseType = newValue; |
---|
| 197 | isFtype = newValue->get_kind() == TypeDecl::Ftype; |
---|
[51b7345] | 198 | } |
---|
| 199 | |
---|
| 200 | std::string TypeInstType::typeString() const { return "type"; } |
---|
| 201 | |
---|
[4a9ccc3] | 202 | bool TypeInstType::isComplete() const { return baseType->isComplete(); } |
---|
| 203 | |
---|
[50377a4] | 204 | void TypeInstType::print( std::ostream &os, Indenter indent ) const { |
---|
[a08ba92] | 205 | using std::endl; |
---|
[1e8b02f5] | 206 | |
---|
[a08ba92] | 207 | Type::print( os, indent ); |
---|
[cd6a6ff] | 208 | os << "instance of " << typeString() << " "; |
---|
| 209 | if( deterministic_output && isUnboundType(name) ) os << "[unbound]"; |
---|
| 210 | else os << name; |
---|
| 211 | os << " (" << ( isFtype ? "" : "not" ) << " function type)"; |
---|
[a08ba92] | 212 | if ( ! parameters.empty() ) { |
---|
[50377a4] | 213 | os << endl << indent << "... with parameters" << endl; |
---|
| 214 | printAll( parameters, os, indent+1 ); |
---|
[a08ba92] | 215 | } // if |
---|
[51b7345] | 216 | } |
---|
| 217 | |
---|
[0dd3a2f] | 218 | // Local Variables: // |
---|
| 219 | // tab-width: 4 // |
---|
| 220 | // mode: c++ // |
---|
| 221 | // compile-command: "make install" // |
---|
| 222 | // End: // |
---|