| 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 | // ResolveTypeof.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Richard C. Bilson
|
|---|
| 10 | // Created On : Sun May 17 12:12:20 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Tue May 19 16:49:04 2015
|
|---|
| 13 | // Update Count : 3
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "ResolveTypeof.h"
|
|---|
| 17 |
|
|---|
| 18 | #include <cassert> // for assert
|
|---|
| 19 |
|
|---|
| 20 | #include "AST/CVQualifiers.hpp"
|
|---|
| 21 | #include "AST/Node.hpp"
|
|---|
| 22 | #include "AST/Pass.hpp"
|
|---|
| 23 | #include "AST/Type.hpp"
|
|---|
| 24 | #include "AST/TypeEnvironment.hpp"
|
|---|
| 25 | #include "Common/PassVisitor.h" // for PassVisitor
|
|---|
| 26 | #include "Common/utility.h" // for copy
|
|---|
| 27 | #include "Resolver.h" // for resolveInVoidContext
|
|---|
| 28 | #include "SynTree/Expression.h" // for Expression
|
|---|
| 29 | #include "SynTree/Mutator.h" // for Mutator
|
|---|
| 30 | #include "SynTree/Type.h" // for TypeofType, Type
|
|---|
| 31 | #include "SymTab/Mangler.h"
|
|---|
| 32 | #include "InitTweak/InitTweak.h" // for isConstExpr
|
|---|
| 33 |
|
|---|
| 34 | namespace SymTab {
|
|---|
| 35 | class Indexer;
|
|---|
| 36 | } // namespace SymTab
|
|---|
| 37 |
|
|---|
| 38 | namespace ResolvExpr {
|
|---|
| 39 | namespace {
|
|---|
| 40 | #if 0
|
|---|
| 41 | void
|
|---|
| 42 | printAlts( const AltList &list, std::ostream &os, int indent = 0 )
|
|---|
| 43 | {
|
|---|
| 44 | for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) {
|
|---|
| 45 | i->print( os, indent );
|
|---|
| 46 | os << std::endl;
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 | #endif
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | class ResolveTypeof_old : public WithShortCircuiting {
|
|---|
| 53 | public:
|
|---|
| 54 | ResolveTypeof_old( const SymTab::Indexer &indexer ) : indexer( indexer ) {}
|
|---|
| 55 | void premutate( TypeofType *typeofType );
|
|---|
| 56 | Type * postmutate( TypeofType *typeofType );
|
|---|
| 57 |
|
|---|
| 58 | private:
|
|---|
| 59 | const SymTab::Indexer &indexer;
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | Type * resolveTypeof( Type *type, const SymTab::Indexer &indexer ) {
|
|---|
| 63 | PassVisitor<ResolveTypeof_old> mutator( indexer );
|
|---|
| 64 | return type->acceptMutator( mutator );
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void ResolveTypeof_old::premutate( TypeofType * ) {
|
|---|
| 68 | visit_children = false;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | Type * ResolveTypeof_old::postmutate( TypeofType *typeofType ) {
|
|---|
| 72 | #if 0
|
|---|
| 73 | std::cerr << "resolving typeof: ";
|
|---|
| 74 | typeofType->print( std::cerr );
|
|---|
| 75 | std::cerr << std::endl;
|
|---|
| 76 | #endif
|
|---|
| 77 | // pass on null expression
|
|---|
| 78 | if ( ! typeofType->expr ) return typeofType;
|
|---|
| 79 |
|
|---|
| 80 | bool isBasetypeof = typeofType->is_basetypeof;
|
|---|
| 81 | auto oldQuals = typeofType->get_qualifiers().val;
|
|---|
| 82 |
|
|---|
| 83 | Type* newType;
|
|---|
| 84 | if ( TypeExpr* tyExpr = dynamic_cast<TypeExpr*>(typeofType->expr) ) {
|
|---|
| 85 | // typeof wrapping type
|
|---|
| 86 | newType = tyExpr->type;
|
|---|
| 87 | tyExpr->type = nullptr;
|
|---|
| 88 | delete tyExpr;
|
|---|
| 89 | } else {
|
|---|
| 90 | // typeof wrapping expression
|
|---|
| 91 | Expression * newExpr = resolveInVoidContext( typeofType->expr, indexer );
|
|---|
| 92 | assert( newExpr->result && ! newExpr->result->isVoid() );
|
|---|
| 93 | newType = newExpr->result;
|
|---|
| 94 | newExpr->result = nullptr;
|
|---|
| 95 | delete typeofType;
|
|---|
| 96 | delete newExpr;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | // clear qualifiers for base, combine with typeoftype quals in any case
|
|---|
| 100 | if ( isBasetypeof ) {
|
|---|
| 101 | // replace basetypeof(<enum>) by int
|
|---|
| 102 | if ( dynamic_cast<EnumInstType*>(newType) ) {
|
|---|
| 103 | Type* newerType =
|
|---|
| 104 | new BasicType{ newType->get_qualifiers(), BasicType::SignedInt,
|
|---|
| 105 | newType->attributes };
|
|---|
| 106 | delete newType;
|
|---|
| 107 | newType = newerType;
|
|---|
| 108 | }
|
|---|
| 109 | newType->get_qualifiers().val
|
|---|
| 110 | = ( newType->get_qualifiers().val & ~Type::Qualifiers::Mask ) | oldQuals;
|
|---|
| 111 | } else {
|
|---|
| 112 | newType->get_qualifiers().val |= oldQuals;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | return newType;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | namespace {
|
|---|
| 119 | struct ResolveTypeof_new : public ast::WithShortCircuiting {
|
|---|
| 120 | const ast::SymbolTable & localSymtab;
|
|---|
| 121 |
|
|---|
| 122 | ResolveTypeof_new( const ast::SymbolTable & syms ) : localSymtab( syms ) {}
|
|---|
| 123 |
|
|---|
| 124 | void previsit( const ast::TypeofType * ) { visit_children = false; }
|
|---|
| 125 |
|
|---|
| 126 | const ast::Type * postvisit( const ast::TypeofType * typeofType ) {
|
|---|
| 127 | // pass on null expression
|
|---|
| 128 | if ( ! typeofType->expr ) return typeofType;
|
|---|
| 129 |
|
|---|
| 130 | ast::ptr< ast::Type > newType;
|
|---|
| 131 | if ( auto tyExpr = typeofType->expr.as< ast::TypeExpr >() ) {
|
|---|
| 132 | // typeof wrapping type
|
|---|
| 133 | newType = tyExpr->type;
|
|---|
| 134 | } else {
|
|---|
| 135 | // typeof wrapping expression
|
|---|
| 136 | ast::TypeEnvironment dummy;
|
|---|
| 137 | ast::ptr< ast::Expr > newExpr =
|
|---|
| 138 | resolveInVoidContext( typeofType->expr, localSymtab, dummy );
|
|---|
| 139 | assert( newExpr->result && ! newExpr->result->isVoid() );
|
|---|
| 140 | newType = newExpr->result;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | // clear qualifiers for base, combine with typeoftype quals regardless
|
|---|
| 144 | if ( typeofType->kind == ast::TypeofType::Basetypeof ) {
|
|---|
| 145 | // replace basetypeof(<enum>) by int
|
|---|
| 146 | if ( newType.as< ast::EnumInstType >() ) {
|
|---|
| 147 | newType = new ast::BasicType{
|
|---|
| 148 | ast::BasicType::SignedInt, newType->qualifiers, copy(newType->attributes) };
|
|---|
| 149 | }
|
|---|
| 150 | reset_qualifiers(
|
|---|
| 151 | newType,
|
|---|
| 152 | ( newType->qualifiers & ~ast::CV::EquivQualifiers ) | typeofType->qualifiers );
|
|---|
| 153 | } else {
|
|---|
| 154 | add_qualifiers( newType, typeofType->qualifiers );
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | return newType.release();
|
|---|
| 158 | }
|
|---|
| 159 | };
|
|---|
| 160 | } // anonymous namespace
|
|---|
| 161 |
|
|---|
| 162 | const ast::Type * resolveTypeof( const ast::Type * type , const ast::SymbolTable & symtab ) {
|
|---|
| 163 | ast::Pass< ResolveTypeof_new > mutator{ symtab };
|
|---|
| 164 | return type->accept( mutator );
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | struct FixArrayDimension {
|
|---|
| 168 | // should not require a mutable symbol table - prevent pass template instantiation
|
|---|
| 169 | const ast::SymbolTable & _symtab;
|
|---|
| 170 | FixArrayDimension(const ast::SymbolTable & symtab): _symtab(symtab) {}
|
|---|
| 171 |
|
|---|
| 172 | const ast::ArrayType * previsit (const ast::ArrayType * arrayType) {
|
|---|
| 173 | if (!arrayType->dimension) return arrayType;
|
|---|
| 174 | auto mutType = mutate(arrayType);
|
|---|
| 175 | ast::ptr<ast::Type> sizetype = ast::sizeType ? ast::sizeType : new ast::BasicType(ast::BasicType::LongUnsignedInt);
|
|---|
| 176 | mutType->dimension = findSingleExpression(arrayType->dimension, sizetype, _symtab);
|
|---|
| 177 |
|
|---|
| 178 | if (InitTweak::isConstExpr(mutType->dimension)) {
|
|---|
| 179 | mutType->isVarLen = ast::LengthFlag::FixedLen;
|
|---|
| 180 | }
|
|---|
| 181 | else {
|
|---|
| 182 | mutType->isVarLen = ast::LengthFlag::VariableLen;
|
|---|
| 183 | }
|
|---|
| 184 | return mutType;
|
|---|
| 185 | }
|
|---|
| 186 | };
|
|---|
| 187 |
|
|---|
| 188 | const ast::Type * fixArrayType( const ast::Type * type, const ast::SymbolTable & symtab) {
|
|---|
| 189 | ast::Pass<FixArrayDimension> visitor {symtab};
|
|---|
| 190 | return type->accept(visitor);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ast::SymbolTable & symtab ) {
|
|---|
| 194 | if (!decl->isTypeFixed) {
|
|---|
| 195 | auto mutDecl = mutate(decl);
|
|---|
| 196 | auto resolvedType = resolveTypeof(decl->type, symtab);
|
|---|
| 197 | resolvedType = fixArrayType(resolvedType, symtab);
|
|---|
| 198 | mutDecl->type = resolvedType;
|
|---|
| 199 |
|
|---|
| 200 | // check variable length if object is an array.
|
|---|
| 201 | // xxx - should this be part of fixObjectType?
|
|---|
| 202 |
|
|---|
| 203 | /*
|
|---|
| 204 | if (auto arrayType = dynamic_cast<const ast::ArrayType *>(resolvedType)) {
|
|---|
| 205 | auto dimExpr = findSingleExpression(arrayType->dimension, ast::sizeType, symtab);
|
|---|
| 206 | if (auto varexpr = arrayType->dimension.as<ast::VariableExpr>()) {// hoisted previously
|
|---|
| 207 | if (InitTweak::isConstExpr(varexpr->var.strict_as<ast::ObjectDecl>()->init)) {
|
|---|
| 208 | auto mutType = mutate(arrayType);
|
|---|
| 209 | mutType->isVarLen = ast::LengthFlag::VariableLen;
|
|---|
| 210 | mutDecl->type = mutType;
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | */
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 | if (!mutDecl->name.empty())
|
|---|
| 218 | mutDecl->mangleName = Mangle::mangle(mutDecl); // do not mangle unnamed variables
|
|---|
| 219 |
|
|---|
| 220 | mutDecl->isTypeFixed = true;
|
|---|
| 221 | return mutDecl;
|
|---|
| 222 | }
|
|---|
| 223 | return decl;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | } // namespace ResolvExpr
|
|---|
| 227 |
|
|---|
| 228 | // Local Variables: //
|
|---|
| 229 | // tab-width: 4 //
|
|---|
| 230 | // mode: c++ //
|
|---|
| 231 | // compile-command: "make install" //
|
|---|
| 232 | // End: //
|
|---|