source: src/SynTree/Type.cc@ 8fd1b7c

ADT ast-experimental
Last change on this file since 8fd1b7c was 9feb34b, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Moved toString and toCString to a new header. Updated includes. cassert was somehow getting instances of toString before but that stopped working so I embedded the new smaller include.

  • Property mode set to 100644
File size: 5.7 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// Type.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Jul 14 15:47:00 2021
13// Update Count : 50
14//
15#include "Type.h"
16
17#include "Attribute.h" // for Attribute
18#include "Common/ToString.hpp" // for toCString
19#include "Common/utility.h" // for cloneAll, deleteAll, printAll
20#include "InitTweak/InitTweak.h" // for getPointerBase
21#include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode
22#include "SynTree/Declaration.h" // for TypeDecl
23#include "SynTree/TypeSubstitution.h" // for TypeSubstitution
24
25using namespace std;
26
27// GENERATED START, DO NOT EDIT
28// GENERATED BY BasicTypes-gen.cc
29const char * BasicType::typeNames[] = {
30 "_Bool",
31 "char",
32 "signed char",
33 "unsigned char",
34 "signed short int",
35 "unsigned short int",
36 "signed int",
37 "unsigned int",
38 "signed long int",
39 "unsigned long int",
40 "signed long long int",
41 "unsigned long long int",
42 "__int128",
43 "unsigned __int128",
44 "_Float16",
45 "_Float16 _Complex",
46 "_Float32",
47 "_Float32 _Complex",
48 "float",
49 "float _Complex",
50 "_Float32x",
51 "_Float32x _Complex",
52 "_Float64",
53 "_Float64 _Complex",
54 "double",
55 "double _Complex",
56 "_Float64x",
57 "_Float64x _Complex",
58 "__float80",
59 "_Float128",
60 "_Float128 _Complex",
61 "__float128",
62 "long double",
63 "long double _Complex",
64 "_Float128x",
65 "_Float128x _Complex",
66};
67// GENERATED END
68
69Type::Type( const Qualifiers &tq, const std::list< Attribute * > & attributes ) : tq( tq ), attributes( attributes ) {}
70
71Type::Type( const Type &other ) : BaseSyntaxNode( other ), tq( other.tq ) {
72 cloneAll( other.forall, forall );
73 cloneAll( other.attributes, attributes );
74}
75
76Type::~Type() {
77 deleteAll( forall );
78 deleteAll( attributes );
79}
80
81// These must remain in the same order as the corresponding bit fields.
82const char * Type::FuncSpecifiersNames[] = { "inline", "_Noreturn", "fortran" };
83const char * Type::StorageClassesNames[] = { "extern", "static", "auto", "register", "__thread", "_Thread_local" };
84const char * Type::QualifiersNames[] = { "const", "restrict", "volatile", "mutex", "_Atomic" };
85
86Type * Type::stripDeclarator() {
87 Type * type, * at;
88 for ( type = this; (at = InitTweak::getPointerBase( type )); type = at );
89 return type;
90}
91
92Type * Type::stripReferences() {
93 Type * type;
94 ReferenceType * ref;
95 for ( type = this; (ref = dynamic_cast<ReferenceType *>( type )); type = ref->base );
96 return type;
97}
98
99const Type * Type::stripReferences() const {
100 const Type * type;
101 const ReferenceType * ref;
102 for ( type = this; (ref = dynamic_cast<const ReferenceType *>( type )); type = ref->base );
103 return type;
104}
105
106int Type::referenceDepth() const { return 0; }
107
108AggregateDecl * Type::getAggr() const {
109 assertf( false, "Non-aggregate type: %s", toCString( this ) );
110}
111
112TypeSubstitution Type::genericSubstitution() const { assertf( false, "Non-aggregate type: %s", toCString( this ) ); }
113
114void Type::print( std::ostream & os, Indenter indent ) const {
115 if ( ! forall.empty() ) {
116 os << "forall" << std::endl;
117 printAll( forall, os, indent+1 );
118 os << ++indent;
119 } // if
120
121 if ( ! attributes.empty() ) {
122 os << "with attributes" << endl;
123 printAll( attributes, os, indent+1 );
124 } // if
125
126 tq.print( os );
127}
128
129
130QualifiedType::QualifiedType( const Type::Qualifiers & tq, Type * parent, Type * child ) : Type( tq, {} ), parent( parent ), child( child ) {
131}
132
133QualifiedType::QualifiedType( const QualifiedType & other ) : Type( other ), parent( maybeClone( other.parent ) ), child( maybeClone( other.child ) ) {
134}
135
136QualifiedType::~QualifiedType() {
137 delete parent;
138 delete child;
139}
140
141void QualifiedType::print( std::ostream & os, Indenter indent ) const {
142 os << "Qualified Type:" << endl;
143 os << indent+1;
144 parent->print( os, indent+1 );
145 os << endl << indent+1;
146 child->print( os, indent+1 );
147 os << endl;
148 Type::print( os, indent+1 );
149}
150
151GlobalScopeType::GlobalScopeType() : Type( Type::Qualifiers(), {} ) {}
152
153void GlobalScopeType::print( std::ostream & os, Indenter ) const {
154 os << "Global Scope Type" << endl;
155}
156
157
158// Empty Variable declarations:
159const Type::FuncSpecifiers noFuncSpecifiers;
160const Type::StorageClasses noStorageClasses;
161const Type::Qualifiers noQualifiers;
162
163bool isUnboundType(const Type * type) {
164 if (auto typeInst = dynamic_cast<const TypeInstType *>(type)) {
165 // xxx - look for a type name produced by renameTyVars.
166
167 // TODO: once TypeInstType representation is updated, it should properly check
168 // if the context id is filled. this is a temporary hack for now
169 return isUnboundType(typeInst->name);
170 }
171 return false;
172}
173
174bool isUnboundType(const std::string & tname) {
175 // xxx - look for a type name produced by renameTyVars.
176
177 // TODO: once TypeInstType representation is updated, it should properly check
178 // if the context id is filled. this is a temporary hack for now
179 if (std::count(tname.begin(), tname.end(), '_') >= 3) {
180 return true;
181 }
182 return false;
183}
184
185VTableType::VTableType( const Type::Qualifiers &tq, Type *base, const std::list< Attribute * > & attributes )
186 : Type( tq, attributes ), base( base ) {
187 assertf( base, "VTableType with a null base created." );
188}
189
190VTableType::VTableType( const VTableType &other )
191 : Type( other ), base( other.base->clone() ) {
192}
193
194VTableType::~VTableType() {
195 delete base;
196}
197
198void VTableType::print( std::ostream &os, Indenter indent ) const {
199 Type::print( os, indent );
200 os << "get virtual-table type of ";
201 if ( base ) {
202 base->print( os, indent );
203 } // if
204}
205
206// Local Variables: //
207// tab-width: 4 //
208// mode: c++ //
209// compile-command: "make install" //
210// End: //
Note: See TracBrowser for help on using the repository browser.