source: src/AST/Type.cpp @ 3e5dd913

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since 3e5dd913 was 3e5dd913, checked in by Fangren Yu <f37yu@…>, 10 months ago

reimplement function type and eliminate deep copy

  • Property mode set to 100644
File size: 5.3 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 : Andrew Beach
12// Last Modified On : Thu Jul 23 14:16:00 2020
13// Update Count     : 5
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 "Init.hpp"
24#include "Common/utility.h"      // for copy, move
25#include "InitTweak/InitTweak.h" // for getPointerBase
26#include "Tuples/Tuples.h"       // for isTtype
27
28namespace ast {
29
30const Type * Type::getComponent( unsigned i ) const {
31        assertf( size() == 1 && i == 0, "Type::getComponent was called with size %d and index %d\n", size(), i );
32        return this;
33}
34
35const Type * Type::stripDeclarator() const {
36        const Type * t;
37        const Type * a;
38        for ( t = this; (a = InitTweak::getPointerBase( t )); t = a );
39        return t;
40}
41
42const Type * Type::stripReferences() const {
43        const Type * t;
44        const ReferenceType * r;
45        for ( t = this; (r = dynamic_cast<const ReferenceType *>(t) ); t = r->base );
46        return t;
47}
48
49// --- BasicType
50
51// GENERATED START, DO NOT EDIT
52// GENERATED BY BasicTypes-gen.cc
53const char * BasicType::typeNames[] = {
54        "_Bool",
55        "char",
56        "signed char",
57        "unsigned char",
58        "signed short int",
59        "unsigned short int",
60        "signed int",
61        "unsigned int",
62        "signed long int",
63        "unsigned long int",
64        "signed long long int",
65        "unsigned long long int",
66        "__int128",
67        "unsigned __int128",
68        "_Float16",
69        "_Float16 _Complex",
70        "_Float32",
71        "_Float32 _Complex",
72        "float",
73        "float _Complex",
74        "_Float32x",
75        "_Float32x _Complex",
76        "_Float64",
77        "_Float64 _Complex",
78        "double",
79        "double _Complex",
80        "_Float64x",
81        "_Float64x _Complex",
82        "__float80",
83        "_Float128",
84        "_Float128 _Complex",
85        "__float128",
86        "long double",
87        "long double _Complex",
88        "_Float128x",
89        "_Float128x _Complex",
90};
91// GENERATED END
92
93// --- FunctionType
94namespace {
95        bool containsTtype( const std::vector<ptr<Type>> & l ) {
96                if ( ! l.empty() ) {
97                        return Tuples::isTtype( l.back() );
98                }
99                return false;
100        }
101}
102
103bool FunctionType::isTtype() const {
104        return containsTtype( returns ) || containsTtype( params );
105}
106
107std::vector<readonly<Decl>> BaseInstType::lookup( const std::string& name ) const {
108        assertf( aggr(), "Must have aggregate to perform lookup" );
109
110        std::vector<readonly<Decl>> found;
111        for ( const Decl * decl : aggr()->members ) {
112                if ( decl->name == name ) { found.emplace_back( decl ); }
113        }
114        return found;
115}
116
117// --- SueInstType (StructInstType, UnionInstType, EnumInstType)
118
119template<typename decl_t>
120SueInstType<decl_t>::SueInstType(
121        const decl_t * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
122: BaseInstType( b->name, q, move(as) ), base( b ) {}
123
124template<typename decl_t>
125SueInstType<decl_t>::SueInstType(
126        const base_type * b, std::vector<ptr<Expr>> && params,
127        CV::Qualifiers q, std::vector<ptr<Attribute>> && as )
128: BaseInstType( b->name, std::move(params), q, std::move(as) ), base( b ) {}
129
130template<typename decl_t>
131bool SueInstType<decl_t>::isComplete() const {
132        return base ? base->body : false;
133}
134
135template class SueInstType<StructDecl>;
136template class SueInstType<UnionDecl>;
137template class SueInstType<EnumDecl>;
138
139// --- TraitInstType
140
141TraitInstType::TraitInstType(
142        const TraitDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
143: BaseInstType( b->name, q, move(as) ), base( b ) {}
144
145void TypeInstType::set_base( const TypeDecl * b ) {
146        base = b;
147        kind = b->kind;
148}
149
150bool TypeInstType::isComplete() const { return base->sized; }
151
152// --- TupleType
153
154TupleType::TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q )
155: Type( q ), types( move(ts) ), members() {
156        // This constructor is awkward. `TupleType` needs to contain objects so that members can be
157        // named, but members without initializer nodes end up getting constructors, which breaks
158        // things. This happens because the object decls have to be visited so that their types are
159        // kept in sync with the types listed here. Ultimately, the types listed here should perhaps
160        // be eliminated and replaced with a list-view over members. The temporary solution is to
161        // make a `ListInit` with `maybeConstructed = false`, so when the object is visited it is not
162        // constructed. Potential better solutions include:
163        //   a) Separate `TupleType` from its declarations, into `TupleDecl` and `Tuple{Inst?}Type`,
164        //      similar to the aggregate types.
165        //   b) Separate initializer nodes better, e.g. add a `MaybeConstructed` node that is replaced
166        //      by `genInit`, rather than the current boolean flag.
167        members.reserve( types.size() );
168        for ( const Type * ty : types ) {
169                members.emplace_back( new ObjectDecl{
170                        CodeLocation{}, "", ty, new ListInit( CodeLocation{}, {}, {}, NoConstruct ),
171                        Storage::Classes{}, Linkage::Cforall } );
172        }
173}
174
175bool isUnboundType(const Type * type) {
176        if (auto typeInst = dynamic_cast<const TypeInstType *>(type)) {
177                // xxx - look for a type name produced by renameTyVars.
178
179                // TODO: once TypeInstType representation is updated, it should properly check
180                // if the context id is filled. this is a temporary hack for now
181                return typeInst->formal_usage > 0;
182        }
183        return false;
184}
185
186}
187
188// Local Variables: //
189// tab-width: 4 //
190// mode: c++ //
191// compile-command: "make install" //
192// End: //
Note: See TracBrowser for help on using the repository browser.