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