source: src/SynTree/ReferenceToType.cc @ f980549

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f980549 was be9036d, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Reorganize TraitInstType? and TraitDecl?, add sized trait definition to prelude

Previously, TraitInstType? cloned all of the members of TraitDecl?. This commit changes
TraitInstType? to instead contain a pointer to the base TraitDecl?, analogous to StructInstType?
and StructDecl?, etc. In particular, this makes the code simpler and makes it easier to
fully expand the members of a trait declaration.

  • Property mode set to 100644
File size: 6.6 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// 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 <stddef.h>          // for NULL
17#include <cassert>           // for assert
18#include <list>              // for list, _List_const_iterator, list<>::cons...
19#include <ostream>           // for operator<<, basic_ostream, ostream, endl
20#include <string>            // for string, operator<<, char_traits, operator==
21
22#include "Common/utility.h"  // for printAll, cloneAll, deleteAll
23#include "Declaration.h"     // for StructDecl, UnionDecl, EnumDecl, Declara...
24#include "Expression.h"      // for Expression
25#include "Type.h"            // for TypeInstType, StructInstType, UnionInstType
26
27class Attribute;
28
29ReferenceToType::ReferenceToType( const Type::Qualifiers &tq, const std::string &name, const std::list< Attribute * > & attributes ) : Type( tq, attributes ), name( name ), hoistType( false ) {
30}
31
32ReferenceToType::ReferenceToType( const ReferenceToType &other ) : Type( other ), name( other.name ), hoistType( other.hoistType ) {
33        cloneAll( other.parameters, parameters );
34}
35
36ReferenceToType::~ReferenceToType() {
37        deleteAll( parameters );
38}
39
40void ReferenceToType::print( std::ostream &os, int indent ) const {
41        using std::endl;
42
43        Type::print( os, indent );
44        os << "instance of " << typeString() << " " << name << " ";
45        if ( ! parameters.empty() ) {
46                os << endl << std::string( indent, ' ' ) << "with parameters" << endl;
47                printAll( parameters, os, indent+2 );
48        } // if
49}
50
51namespace {
52        void doLookup( const std::list< Declaration* > &members, const std::string &name, std::list< Declaration* > &foundDecls ) {
53                for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
54                        if ( (*i)->get_name() == name ) {
55                                foundDecls.push_back( *i );
56                        } // if
57                } // for
58        }
59} // namespace
60
61StructInstType::StructInstType( const Type::Qualifiers & tq, StructDecl * baseStruct, const std::list< Attribute * > & attributes ) :
62                Parent( tq, baseStruct->get_name(), attributes ), baseStruct( baseStruct ) {}
63
64std::string StructInstType::typeString() const { return "struct"; }
65
66std::list<TypeDecl*>* StructInstType::get_baseParameters() {
67        if ( ! baseStruct ) return NULL;
68        return &baseStruct->get_parameters();
69}
70
71bool StructInstType::isComplete() const { return baseStruct ? baseStruct->has_body() : false; }
72
73void StructInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const {
74        assert( baseStruct );
75        doLookup( baseStruct->get_members(), name, foundDecls );
76}
77
78void StructInstType::print( std::ostream &os, int indent ) const {
79        using std::endl;
80
81        if ( baseStruct == NULL ) ReferenceToType::print( os, indent );
82        else {
83                Type::print( os, indent );
84                os << "instance of " << typeString() << " " << name << " with body " << baseStruct->has_body() << " ";
85                if ( ! parameters.empty() ) {
86                        os << endl << std::string( indent, ' ' ) << "with parameters" << endl;
87                        printAll( parameters, os, indent+2 );
88                } // if
89        } // if
90}
91
92
93UnionInstType::UnionInstType( const Type::Qualifiers & tq, UnionDecl * baseUnion, const std::list< Attribute * > & attributes ) :
94                Parent( tq, baseUnion->get_name(), attributes ), baseUnion( baseUnion ) {}
95
96std::string UnionInstType::typeString() const { return "union"; }
97
98std::list< TypeDecl * > * UnionInstType::get_baseParameters() {
99        if ( ! baseUnion ) return NULL;
100        return &baseUnion->get_parameters();
101}
102
103bool UnionInstType::isComplete() const { return baseUnion ? baseUnion->has_body() : false; }
104
105void UnionInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const {
106        assert( baseUnion );
107        doLookup( baseUnion->get_members(), name, foundDecls );
108}
109
110void UnionInstType::print( std::ostream &os, int indent ) const {
111        using std::endl;
112
113        if ( baseUnion == NULL ) ReferenceToType::print( os, indent );
114        else {
115                Type::print( os, indent );
116                os << "instance of " << typeString() << " " << name << " with body " << baseUnion->has_body() << " ";
117                if ( ! parameters.empty() ) {
118                        os << endl << std::string( indent, ' ' ) << "with parameters" << endl;
119                        printAll( parameters, os, indent+2 );
120                } // if
121        } // if
122}
123
124
125EnumInstType::EnumInstType( const Type::Qualifiers & tq, EnumDecl * baseEnum, const std::list< Attribute * > & attributes ) :
126                Parent( tq, baseEnum->get_name(), attributes ), baseEnum( baseEnum ) {}
127
128std::string EnumInstType::typeString() const { return "enum"; }
129
130bool EnumInstType::isComplete() const { return baseEnum ? baseEnum->has_body() : false; }
131
132std::string TraitInstType::typeString() const { return "trait"; }
133
134TraitInstType::TraitInstType( const Type::Qualifiers & tq, TraitDecl * baseTrait, const std::list< Attribute * > & attributes ) : Parent( tq, baseTrait->name, attributes ), baseTrait( baseTrait ) {}
135
136TraitInstType::TraitInstType( const TraitInstType &other ) : Parent( other ), baseTrait( other.baseTrait ) {
137}
138
139TraitInstType::~TraitInstType() {
140}
141
142bool TraitInstType::isComplete() const { assert( false ); }
143
144TypeInstType::TypeInstType( const Type::Qualifiers &tq, const std::string &name, TypeDecl *baseType, const std::list< Attribute * > & attributes ) : Parent( tq, name, attributes ) {
145        set_baseType( baseType );
146}
147
148TypeInstType::TypeInstType( const Type::Qualifiers &tq, const std::string &name, bool isFtype, const std::list< Attribute * > & attributes ) : Parent( tq, name, attributes ), baseType( 0 ), isFtype( isFtype ) {
149}
150
151TypeInstType::TypeInstType( const TypeInstType &other ) : Parent( other ), baseType( other.baseType ), isFtype( other.isFtype ) {
152}
153
154
155TypeInstType::~TypeInstType() {
156        // delete baseType; //This is shared and should not be deleted
157}
158
159void TypeInstType::set_baseType( TypeDecl *newValue ) {
160        baseType = newValue;
161        isFtype = newValue->get_kind() == TypeDecl::Ftype;
162}
163
164std::string TypeInstType::typeString() const { return "type"; }
165
166bool TypeInstType::isComplete() const { return baseType->isComplete(); }
167
168void TypeInstType::print( std::ostream &os, int indent ) const {
169        using std::endl;
170
171        Type::print( os, indent );
172        os << "instance of " << typeString() << " " << get_name() << " (" << ( isFtype ? "" : "not" ) << " function type) ";
173        if ( ! parameters.empty() ) {
174                os << endl << std::string( indent, ' ' ) << "with parameters" << endl;
175                printAll( parameters, os, indent+2 );
176        } // if
177}
178
179// Local Variables: //
180// tab-width: 4 //
181// mode: c++ //
182// compile-command: "make install" //
183// End: //
Note: See TracBrowser for help on using the repository browser.