| 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 Apr 6 15:59:00 2023
|
|---|
| 13 | // Update Count : 7
|
|---|
| 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 "Inspect.hpp"
|
|---|
| 25 | #include "Common/Utility.hpp" // for copy, move
|
|---|
| 26 | #include "Tuples/Tuples.hpp" // for isTtype
|
|---|
| 27 |
|
|---|
| 28 | namespace ast {
|
|---|
| 29 |
|
|---|
| 30 | const 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 |
|
|---|
| 35 | const Type * Type::stripDeclarator() const {
|
|---|
| 36 | const Type * t;
|
|---|
| 37 | const Type * a;
|
|---|
| 38 | for ( t = this; (a = ast::getPointerBase( t )); t = a );
|
|---|
| 39 | return t;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | const 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.cpp
|
|---|
| 53 | const 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 | "long double",
|
|---|
| 84 | "long double _Complex",
|
|---|
| 85 | "__float128",
|
|---|
| 86 | "_Float128",
|
|---|
| 87 | "_Float128 _Complex",
|
|---|
| 88 | "_Float128x",
|
|---|
| 89 | "_Float128x _Complex",
|
|---|
| 90 | };
|
|---|
| 91 | // GENERATED END
|
|---|
| 92 |
|
|---|
| 93 | // --- FunctionType
|
|---|
| 94 | namespace {
|
|---|
| 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 |
|
|---|
| 103 | bool FunctionType::isTtype() const {
|
|---|
| 104 | return containsTtype( returns ) || containsTtype( params );
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | // --- BaseInstType
|
|---|
| 108 |
|
|---|
| 109 | std::vector<readonly<Decl>> BaseInstType::lookup( const std::string& name ) const {
|
|---|
| 110 | assertf( aggr(), "Must have aggregate to perform lookup" );
|
|---|
| 111 |
|
|---|
| 112 | std::vector<readonly<Decl>> found;
|
|---|
| 113 | for ( const Decl * decl : aggr()->members ) {
|
|---|
| 114 | if ( decl->name == name ) { found.emplace_back( decl ); }
|
|---|
| 115 | }
|
|---|
| 116 | return found;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | // --- SueInstType (StructInstType, UnionInstType, EnumInstType)
|
|---|
| 120 |
|
|---|
| 121 | template<typename decl_t>
|
|---|
| 122 | SueInstType<decl_t>::SueInstType(
|
|---|
| 123 | const base_type * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
|
|---|
| 124 | : BaseInstType( b->name, q, std::move(as) ), base( b ) {}
|
|---|
| 125 |
|
|---|
| 126 | template<typename decl_t>
|
|---|
| 127 | SueInstType<decl_t>::SueInstType(
|
|---|
| 128 | const base_type * b, std::vector<ptr<Expr>> && params,
|
|---|
| 129 | CV::Qualifiers q, std::vector<ptr<Attribute>> && as )
|
|---|
| 130 | : BaseInstType( b->name, std::move(params), q, std::move(as) ), base( b ) {}
|
|---|
| 131 |
|
|---|
| 132 | template<typename decl_t>
|
|---|
| 133 | bool SueInstType<decl_t>::isComplete() const {
|
|---|
| 134 | return base ? base->body : false;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | template class SueInstType<StructDecl>;
|
|---|
| 138 | template class SueInstType<UnionDecl>;
|
|---|
| 139 | template class SueInstType<EnumDecl>;
|
|---|
| 140 |
|
|---|
| 141 | // --- TraitInstType
|
|---|
| 142 |
|
|---|
| 143 | TraitInstType::TraitInstType(
|
|---|
| 144 | const TraitDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
|
|---|
| 145 | : BaseInstType( b->name, q, std::move(as) ), base( b ) {}
|
|---|
| 146 |
|
|---|
| 147 | // --- TypeInstType
|
|---|
| 148 |
|
|---|
| 149 | TypeInstType::TypeInstType( const TypeEnvKey & key )
|
|---|
| 150 | : BaseInstType(key.base->name), base(key.base), kind(key.base->kind), formal_usage(key.formal_usage), expr_id(key.expr_id) {}
|
|---|
| 151 |
|
|---|
| 152 | bool TypeInstType::operator==( const TypeInstType & other ) const {
|
|---|
| 153 | return base == other.base
|
|---|
| 154 | && formal_usage == other.formal_usage
|
|---|
| 155 | && expr_id == other.expr_id;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | TypeInstType::TypeInstType( const TypeDecl * b,
|
|---|
| 159 | CV::Qualifiers q, std::vector<ptr<Attribute>> && as )
|
|---|
| 160 | : BaseInstType( b->name, q, std::move(as) ), base( b ), kind( b->kind ) {}
|
|---|
| 161 |
|
|---|
| 162 | void TypeInstType::set_base( const TypeDecl * b ) {
|
|---|
| 163 | base = b;
|
|---|
| 164 | kind = b->kind;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | bool TypeInstType::isComplete() const { return base->sized; }
|
|---|
| 168 |
|
|---|
| 169 | static std::string toTypeString( int usage, int id, std::string const & name ) {
|
|---|
| 170 | return "_" + std::to_string( usage ) + "_" + std::to_string( id ) + "_" + name;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | std::string TypeInstType::typeString() const {
|
|---|
| 174 | return ( 0 < formal_usage ) ? toTypeString( formal_usage, expr_id, name ) : name;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | std::string TypeEnvKey::typeString() const {
|
|---|
| 178 | return toTypeString( formal_usage, expr_id, base->name );
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | bool TypeEnvKey::operator==(
|
|---|
| 182 | const TypeEnvKey & other ) const {
|
|---|
| 183 | return base == other.base
|
|---|
| 184 | && formal_usage == other.formal_usage
|
|---|
| 185 | && expr_id == other.expr_id;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | bool TypeEnvKey::operator<(
|
|---|
| 189 | const TypeEnvKey & other ) const {
|
|---|
| 190 | // TypeEnvKey ordering is an arbitrary total ordering.
|
|---|
| 191 | // It doesn't mean anything but allows for a sorting.
|
|---|
| 192 | if ( base < other.base ) {
|
|---|
| 193 | return true;
|
|---|
| 194 | } else if ( other.base < base ) {
|
|---|
| 195 | return false;
|
|---|
| 196 | } else if ( formal_usage < other.formal_usage ) {
|
|---|
| 197 | return true;
|
|---|
| 198 | } else if ( other.formal_usage < formal_usage ) {
|
|---|
| 199 | return false;
|
|---|
| 200 | } else {
|
|---|
| 201 | return expr_id < other.expr_id;
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | // --- TupleType
|
|---|
| 206 |
|
|---|
| 207 | TupleType::TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q )
|
|---|
| 208 | : Type( q ), types( std::move(ts) ) {}
|
|---|
| 209 |
|
|---|
| 210 | bool isUnboundType(const Type * type) {
|
|---|
| 211 | if (auto typeInst = dynamic_cast<const TypeInstType *>(type)) {
|
|---|
| 212 | // xxx - look for a type name produced by renameTyVars.
|
|---|
| 213 |
|
|---|
| 214 | // TODO: once TypeInstType representation is updated, it should properly check
|
|---|
| 215 | // if the context id is filled. this is a temporary hack for now
|
|---|
| 216 | return typeInst->formal_usage > 0;
|
|---|
| 217 | }
|
|---|
| 218 | return false;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | // Local Variables: //
|
|---|
| 224 | // tab-width: 4 //
|
|---|
| 225 | // mode: c++ //
|
|---|
| 226 | // compile-command: "make install" //
|
|---|
| 227 | // End: //
|
|---|