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