| 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 | // ReferenceToType.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Thu Feb 23 16:38:54 2017
 | 
|---|
| 13 | // Update Count     : 24
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 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==
 | 
|---|
| 20 | 
 | 
|---|
| 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 | 
 | 
|---|
| 26 | class Attribute;
 | 
|---|
| 27 | 
 | 
|---|
| 28 | ReferenceToType::ReferenceToType( const Type::Qualifiers &tq, const std::string &name, const std::list< Attribute * > & attributes ) : Type( tq, attributes ), name( name ), hoistType( false ) {
 | 
|---|
| 29 | }
 | 
|---|
| 30 | 
 | 
|---|
| 31 | ReferenceToType::ReferenceToType( const ReferenceToType &other ) : Type( other ), name( other.name ), hoistType( other.hoistType ) {
 | 
|---|
| 32 |         cloneAll( other.parameters, parameters );
 | 
|---|
| 33 | }
 | 
|---|
| 34 | 
 | 
|---|
| 35 | ReferenceToType::~ReferenceToType() {
 | 
|---|
| 36 |         deleteAll( parameters );
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | void ReferenceToType::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 40 |         using std::endl;
 | 
|---|
| 41 | 
 | 
|---|
| 42 |         Type::print( os, indent );
 | 
|---|
| 43 |         os << "instance of " << typeString() << " " << name << " ";
 | 
|---|
| 44 |         if ( ! parameters.empty() ) {
 | 
|---|
| 45 |                 os << endl << indent << "... with parameters" << endl;
 | 
|---|
| 46 |                 printAll( parameters, os, indent+1 );
 | 
|---|
| 47 |         } // if
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | namespace {
 | 
|---|
| 51 |         void doLookup( const std::list< Declaration* > &members, const std::string &name, std::list< Declaration* > &foundDecls ) {
 | 
|---|
| 52 |                 for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
 | 
|---|
| 53 |                         if ( (*i)->get_name() == name ) {
 | 
|---|
| 54 |                                 foundDecls.push_back( *i );
 | 
|---|
| 55 |                         } // if
 | 
|---|
| 56 |                 } // for
 | 
|---|
| 57 |         }
 | 
|---|
| 58 | } // namespace
 | 
|---|
| 59 | 
 | 
|---|
| 60 | StructInstType::StructInstType( const Type::Qualifiers & tq, StructDecl * baseStruct, const std::list< Attribute * > & attributes ) :
 | 
|---|
| 61 |                 Parent( tq, baseStruct->get_name(), attributes ), baseStruct( baseStruct ) {}
 | 
|---|
| 62 | 
 | 
|---|
| 63 | std::string StructInstType::typeString() const { return "struct"; }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | std::list<TypeDecl*>* StructInstType::get_baseParameters() {
 | 
|---|
| 66 |         if ( ! baseStruct ) return nullptr;
 | 
|---|
| 67 |         return &baseStruct->get_parameters();
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | bool StructInstType::isComplete() const { return baseStruct ? baseStruct->has_body() : false; }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | void StructInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const {
 | 
|---|
| 73 |         assert( baseStruct );
 | 
|---|
| 74 |         doLookup( baseStruct->get_members(), name, foundDecls );
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | void StructInstType::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 78 |         using std::endl;
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         if ( baseStruct == nullptr ) ReferenceToType::print( os, indent );
 | 
|---|
| 81 |         else {
 | 
|---|
| 82 |                 Type::print( os, indent );
 | 
|---|
| 83 |                 os << "instance of " << typeString() << " " << name << " with body " << baseStruct->has_body() << " ";
 | 
|---|
| 84 |                 if ( ! parameters.empty() ) {
 | 
|---|
| 85 |                         os << endl << indent << "... with parameters" << endl;
 | 
|---|
| 86 |                         printAll( parameters, os, indent+1 );
 | 
|---|
| 87 |                 } // if
 | 
|---|
| 88 |         } // if
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | 
 | 
|---|
| 92 | UnionInstType::UnionInstType( const Type::Qualifiers & tq, UnionDecl * baseUnion, const std::list< Attribute * > & attributes ) :
 | 
|---|
| 93 |                 Parent( tq, baseUnion->get_name(), attributes ), baseUnion( baseUnion ) {}
 | 
|---|
| 94 | 
 | 
|---|
| 95 | std::string UnionInstType::typeString() const { return "union"; }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | std::list< TypeDecl * > * UnionInstType::get_baseParameters() {
 | 
|---|
| 98 |         if ( ! baseUnion ) return nullptr;
 | 
|---|
| 99 |         return &baseUnion->get_parameters();
 | 
|---|
| 100 | }
 | 
|---|
| 101 | 
 | 
|---|
| 102 | bool UnionInstType::isComplete() const { return baseUnion ? baseUnion->has_body() : false; }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | void UnionInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const {
 | 
|---|
| 105 |         assert( baseUnion );
 | 
|---|
| 106 |         doLookup( baseUnion->get_members(), name, foundDecls );
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | void UnionInstType::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 110 |         using std::endl;
 | 
|---|
| 111 | 
 | 
|---|
| 112 |         if ( baseUnion == nullptr ) ReferenceToType::print( os, indent );
 | 
|---|
| 113 |         else {
 | 
|---|
| 114 |                 Type::print( os, indent );
 | 
|---|
| 115 |                 os << "instance of " << typeString() << " " << name << " with body " << baseUnion->has_body() << " ";
 | 
|---|
| 116 |                 if ( ! parameters.empty() ) {
 | 
|---|
| 117 |                         os << endl << indent << "... with parameters" << endl;
 | 
|---|
| 118 |                         printAll( parameters, os, indent+1 );
 | 
|---|
| 119 |                 } // if
 | 
|---|
| 120 |         } // if
 | 
|---|
| 121 | }
 | 
|---|
| 122 | 
 | 
|---|
| 123 | 
 | 
|---|
| 124 | EnumInstType::EnumInstType( const Type::Qualifiers & tq, EnumDecl * baseEnum, const std::list< Attribute * > & attributes ) :
 | 
|---|
| 125 |                 Parent( tq, baseEnum->get_name(), attributes ), baseEnum( baseEnum ) {}
 | 
|---|
| 126 | 
 | 
|---|
| 127 | std::string EnumInstType::typeString() const { return "enum"; }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | bool EnumInstType::isComplete() const { return baseEnum ? baseEnum->has_body() : false; }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | void EnumInstType::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 132 |         using std::endl;
 | 
|---|
| 133 | 
 | 
|---|
| 134 |         if ( baseEnum == nullptr ) ReferenceToType::print( os, indent );
 | 
|---|
| 135 |         else {
 | 
|---|
| 136 |                 Type::print( os, indent );
 | 
|---|
| 137 |                 os << "instance of " << typeString() << " " << name << " with body " << baseEnum->has_body() << " ";
 | 
|---|
| 138 |         } // if
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | 
 | 
|---|
| 142 | std::string TraitInstType::typeString() const { return "trait"; }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | TraitInstType::TraitInstType( const Type::Qualifiers & tq, TraitDecl * baseTrait, const std::list< Attribute * > & attributes ) : Parent( tq, baseTrait->name, attributes ), baseTrait( baseTrait ) {}
 | 
|---|
| 145 | 
 | 
|---|
| 146 | TraitInstType::TraitInstType( const TraitInstType &other ) : Parent( other ), baseTrait( other.baseTrait ) {
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | TraitInstType::~TraitInstType() {
 | 
|---|
| 150 | }
 | 
|---|
| 151 | 
 | 
|---|
| 152 | bool TraitInstType::isComplete() const { assert( false ); }
 | 
|---|
| 153 | 
 | 
|---|
| 154 | TypeInstType::TypeInstType( const Type::Qualifiers &tq, const std::string &name, TypeDecl *baseType, const std::list< Attribute * > & attributes ) : Parent( tq, name, attributes ) {
 | 
|---|
| 155 |         set_baseType( baseType );
 | 
|---|
| 156 | }
 | 
|---|
| 157 | 
 | 
|---|
| 158 | 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 ) {
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | TypeInstType::TypeInstType( const TypeInstType &other ) : Parent( other ), baseType( other.baseType ), isFtype( other.isFtype ) {
 | 
|---|
| 162 | }
 | 
|---|
| 163 | 
 | 
|---|
| 164 | 
 | 
|---|
| 165 | TypeInstType::~TypeInstType() {
 | 
|---|
| 166 |         // delete baseType; //This is shared and should not be deleted
 | 
|---|
| 167 | }
 | 
|---|
| 168 | 
 | 
|---|
| 169 | void TypeInstType::set_baseType( TypeDecl *newValue ) {
 | 
|---|
| 170 |         baseType = newValue;
 | 
|---|
| 171 |         isFtype = newValue->get_kind() == TypeDecl::Ftype;
 | 
|---|
| 172 | }
 | 
|---|
| 173 | 
 | 
|---|
| 174 | std::string TypeInstType::typeString() const { return "type"; }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | bool TypeInstType::isComplete() const { return baseType->isComplete(); }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | void TypeInstType::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 179 |         using std::endl;
 | 
|---|
| 180 | 
 | 
|---|
| 181 |         Type::print( os, indent );
 | 
|---|
| 182 |         os << "instance of " << typeString() << " " << get_name() << " (" << ( isFtype ? "" : "not" ) << " function type) ";
 | 
|---|
| 183 |         if ( ! parameters.empty() ) {
 | 
|---|
| 184 |                 os << endl << indent << "... with parameters" << endl;
 | 
|---|
| 185 |                 printAll( parameters, os, indent+1 );
 | 
|---|
| 186 |         } // if
 | 
|---|
| 187 | }
 | 
|---|
| 188 | 
 | 
|---|
| 189 | // Local Variables: //
 | 
|---|
| 190 | // tab-width: 4 //
 | 
|---|
| 191 | // mode: c++ //
 | 
|---|
| 192 | // compile-command: "make install" //
 | 
|---|
| 193 | // End: //
 | 
|---|