source: src/AST/Type.cpp @ 7030dab

arm-ehenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 7030dab was 7030dab, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Merge branch 'master' into new-ast

  • Property mode set to 100644
File size: 6.4 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.cpp --
8//
9// Author           : Aaron B. Moss
10// Created On       : Mon May 13 15:00:00 2019
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Dec 15 16:56:28 2019
13// Update Count     : 4
14//
15
16#include "Type.hpp"
17
18#include <cassert>
19#include <utility>               // for move
20#include <vector>
21
22#include "Decl.hpp"
23#include "ForallSubstitutor.hpp" // for substituteForall
24#include "Init.hpp"
25#include "Common/utility.h"      // for copy, move
26#include "InitTweak/InitTweak.h" // for getPointerBase
27#include "Tuples/Tuples.h"       // for isTtype
28
29namespace ast {
30
31const Type * Type::getComponent( unsigned i ) const {
32        assertf( size() == 1 && i == 0, "Type::getComponent was called with size %d and index %d\n", size(), i );
33        return this;
34}
35
36const Type * Type::stripDeclarator() const {
37        const Type * t;
38        const Type * a;
39        for ( t = this; (a = InitTweak::getPointerBase( t )); t = a );
40        return t;
41}
42
43const Type * Type::stripReferences() const {
44        const Type * t;
45        const ReferenceType * r;
46        for ( t = this; (r = dynamic_cast<const ReferenceType *>(t) ); t = r->base );
47        return t;
48}
49
50// --- BasicType
51
52// GENERATED START, DO NOT EDIT
53// GENERATED BY BasicTypes-gen.cc
54const char * BasicType::typeNames[] = {
55        "_Bool",
56        "char",
57        "signed char",
58        "unsigned char",
59        "signed short int",
60        "unsigned short int",
61        "signed int",
62        "unsigned int",
63        "signed long int",
64        "unsigned long int",
65        "signed long long int",
66        "unsigned long long int",
67        "__int128",
68        "unsigned __int128",
69        "_Float16",
70        "_Float16 _Complex",
71        "_Float32",
72        "_Float32 _Complex",
73        "float",
74        "float _Complex",
75        "_Float32x",
76        "_Float32x _Complex",
77        "_Float64",
78        "_Float64 _Complex",
79        "double",
80        "double _Complex",
81        "_Float64x",
82        "_Float64x _Complex",
83        "__float80",
84        "_Float128",
85        "_Float128 _Complex",
86        "__float128",
87        "long double",
88        "long double _Complex",
89        "_Float128x",
90        "_Float128x _Complex",
91};
92// GENERATED END
93
94// --- ParameterizedType
95
96void ParameterizedType::initWithSub( 
97        const ParameterizedType & o, Pass< ForallSubstitutor > & sub
98) {
99        forall = sub.pass( o.forall );
100}
101
102// --- FunctionType
103
104FunctionType::FunctionType( const FunctionType & o )
105: ParameterizedType( o.qualifiers, copy( o.attributes ) ), returns(), params(), 
106  isVarArgs( o.isVarArgs ) {
107        Pass< ForallSubstitutor > sub;
108        initWithSub( o, sub );           // initialize substitution map
109        returns = sub.pass( o.returns ); // apply to return and parameter types
110        params = sub.pass( o.params );
111}
112
113namespace {
114        bool containsTtype( const std::vector<ptr<DeclWithType>> & l ) {
115                if ( ! l.empty() ) {
116                        return Tuples::isTtype( l.back()->get_type() );
117                }
118                return false;
119        }
120}
121
122bool FunctionType::isTtype() const {
123        return containsTtype( returns ) || containsTtype( params );
124}
125
126// --- ReferenceToType
127
128void ReferenceToType::initWithSub( const ReferenceToType & o, Pass< ForallSubstitutor > & sub ) {
129        ParameterizedType::initWithSub( o, sub ); // initialize substitution
130        params = sub.pass( o.params );            // apply to parameters
131}
132
133ReferenceToType::ReferenceToType( const ReferenceToType & o )
134: ParameterizedType( o.qualifiers, copy( o.attributes ) ), params(), name( o.name ), 
135  hoistType( o.hoistType ) {
136        Pass< ForallSubstitutor > sub;
137        initWithSub( o, sub );
138}
139
140std::vector<readonly<Decl>> ReferenceToType::lookup( const std::string& name ) const {
141        assertf( aggr(), "Must have aggregate to perform lookup" );
142
143        std::vector<readonly<Decl>> found;
144        for ( const Decl * decl : aggr()->members ) {
145                if ( decl->name == name ) { found.emplace_back( decl ); }
146        }
147        return found;
148}
149
150// --- StructInstType
151
152StructInstType::StructInstType( 
153        const StructDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
154: ReferenceToType( b->name, q, move(as) ), base( b ) {}
155
156bool StructInstType::isComplete() const { return base ? base->body : false; }
157
158// --- UnionInstType
159
160UnionInstType::UnionInstType( 
161        const UnionDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
162: ReferenceToType( b->name, q, move(as) ), base( b ) {}
163
164bool UnionInstType::isComplete() const { return base ? base->body : false; }
165
166// --- EnumInstType
167
168EnumInstType::EnumInstType( 
169        const EnumDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
170: ReferenceToType( b->name, q, move(as) ), base( b ) {}
171
172bool EnumInstType::isComplete() const { return base ? base->body : false; }
173
174// --- TraitInstType
175
176TraitInstType::TraitInstType( 
177        const TraitDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
178: ReferenceToType( b->name, q, move(as) ), base( b ) {}
179
180// --- TypeInstType
181
182TypeInstType::TypeInstType( const TypeInstType & o ) 
183: ReferenceToType( o.name, o.qualifiers, copy( o.attributes ) ), base(), kind( o.kind ) {
184        Pass< ForallSubstitutor > sub;
185        initWithSub( o, sub );      // initialize substitution
186        base = sub.pass( o.base );  // apply to base type
187}
188
189void TypeInstType::set_base( const TypeDecl * b ) {
190        base = b;
191        kind = b->kind;
192}
193
194bool TypeInstType::isComplete() const { return base->sized; }
195
196// --- TupleType
197
198TupleType::TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q )
199: Type( q ), types( move(ts) ), members() {
200        // This constructor is awkward. `TupleType` needs to contain objects so that members can be
201        // named, but members without initializer nodes end up getting constructors, which breaks
202        // things. This happens because the object decls have to be visited so that their types are
203        // kept in sync with the types listed here. Ultimately, the types listed here should perhaps
204        // be eliminated and replaced with a list-view over members. The temporary solution is to
205        // make a `ListInit` with `maybeConstructed = false`, so when the object is visited it is not
206        // constructed. Potential better solutions include:
207        //   a) Separate `TupleType` from its declarations, into `TupleDecl` and `Tuple{Inst?}Type`,
208        //      similar to the aggregate types.
209        //   b) Separate initializer nodes better, e.g. add a `MaybeConstructed` node that is replaced
210        //      by `genInit`, rather than the current boolean flag.
211        members.reserve( types.size() );
212        for ( const Type * ty : types ) {
213                members.emplace_back( new ObjectDecl{
214                        CodeLocation{}, "", ty, new ListInit( CodeLocation{}, {}, {}, MaybeConstruct ),
215                        Storage::Classes{}, Linkage::Cforall } );
216        }
217}
218
219}
220
221// Local Variables: //
222// tab-width: 4 //
223// mode: c++ //
224// compile-command: "make install" //
225// End: //
Note: See TracBrowser for help on using the repository browser.