[a32b204] | 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 | //
|
---|
[71f4e4f] | 7 | // Resolver.cc --
|
---|
[a32b204] | 8 | //
|
---|
[d76c588] | 9 | // Author : Aaron B. Moss
|
---|
[a32b204] | 10 | // Created On : Sun May 17 12:17:01 2015
|
---|
[4c2fe47] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[ca9d65e] | 12 | // Last Modified On : Thu Dec 14 18:44:43 2023
|
---|
| 13 | // Update Count : 251
|
---|
[a32b204] | 14 | //
|
---|
| 15 |
|
---|
[e3e16bc] | 16 | #include <cassert> // for strict_dynamic_cast, assert
|
---|
[ea6332d] | 17 | #include <memory> // for allocator, allocator_traits<...
|
---|
| 18 | #include <tuple> // for get
|
---|
[6d6e829] | 19 | #include <vector> // for vector
|
---|
[ea6332d] | 20 |
|
---|
[99d4584] | 21 | #include "Candidate.hpp"
|
---|
| 22 | #include "CandidateFinder.hpp"
|
---|
[d76c588] | 23 | #include "CurrentObject.h" // for CurrentObject
|
---|
| 24 | #include "RenameVars.h" // for RenameVars, global_renamer
|
---|
| 25 | #include "Resolver.h"
|
---|
[16ba4a6f] | 26 | #include "ResolveTypeof.h"
|
---|
[4a89b52] | 27 | #include "ResolveMode.hpp" // for ResolveMode
|
---|
[d76c588] | 28 | #include "typeops.h" // for extractResultType
|
---|
| 29 | #include "Unify.h" // for unify
|
---|
[16ba4a6f] | 30 | #include "CompilationState.h"
|
---|
[2a8f0c1] | 31 | #include "AST/Decl.hpp"
|
---|
| 32 | #include "AST/Init.hpp"
|
---|
[d76c588] | 33 | #include "AST/Pass.hpp"
|
---|
[99d4584] | 34 | #include "AST/Print.hpp"
|
---|
[d76c588] | 35 | #include "AST/SymbolTable.hpp"
|
---|
[2773ab8] | 36 | #include "AST/Type.hpp"
|
---|
[8f06277] | 37 | #include "Common/Eval.h" // for eval
|
---|
[9feb34b] | 38 | #include "Common/Iterate.hpp" // for group_iterate
|
---|
[ea6332d] | 39 | #include "Common/SemanticError.h" // for SemanticError
|
---|
[57e0289] | 40 | #include "Common/Stats/ResolveTime.h" // for ResolveTime::start(), ResolveTime::stop()
|
---|
[9feb34b] | 41 | #include "Common/ToString.hpp" // for toCString
|
---|
[c6b4432] | 42 | #include "Common/UniqueName.h" // for UniqueName
|
---|
[0a60c04] | 43 | #include "InitTweak/GenInit.h"
|
---|
[ea6332d] | 44 | #include "InitTweak/InitTweak.h" // for isIntrinsicSingleArgCallStmt
|
---|
[16ba4a6f] | 45 | #include "SymTab/Mangler.h" // for Mangler
|
---|
[0a60c04] | 46 | #include "Tuples/Tuples.h"
|
---|
[2bfc6b2] | 47 | #include "Validate/FindSpecialDecls.h" // for SizeType
|
---|
[51b73452] | 48 |
|
---|
[d9a0e76] | 49 | using namespace std;
|
---|
[51b73452] | 50 |
|
---|
[d9a0e76] | 51 | namespace ResolvExpr {
|
---|
[99d4584] | 52 | namespace {
|
---|
| 53 | /// Finds deleted expressions in an expression tree
|
---|
[0bd3faf] | 54 | struct DeleteFinder final : public ast::WithShortCircuiting, public ast::WithVisitorRef<DeleteFinder> {
|
---|
[e6b42e7] | 55 | const ast::DeletedExpr * result = nullptr;
|
---|
[99d4584] | 56 |
|
---|
| 57 | void previsit( const ast::DeletedExpr * expr ) {
|
---|
[e6b42e7] | 58 | if ( result ) { visit_children = false; }
|
---|
| 59 | else { result = expr; }
|
---|
[99d4584] | 60 | }
|
---|
| 61 |
|
---|
[361bf01] | 62 | void previsit( const ast::Expr * expr ) {
|
---|
[e6b42e7] | 63 | if ( result ) { visit_children = false; }
|
---|
[361bf01] | 64 | if (expr->inferred.hasParams()) {
|
---|
| 65 | for (auto & imp : expr->inferred.inferParams() ) {
|
---|
[2dda05d] | 66 | imp.second.expr->accept(*visitor);
|
---|
[361bf01] | 67 | }
|
---|
| 68 | }
|
---|
[99d4584] | 69 | }
|
---|
| 70 | };
|
---|
[4894239] | 71 |
|
---|
[0bd3faf] | 72 | struct ResolveDesignators final : public ast::WithShortCircuiting {
|
---|
[4894239] | 73 | ResolveContext& context;
|
---|
| 74 | bool result = false;
|
---|
| 75 |
|
---|
[0bd3faf] | 76 | ResolveDesignators( ResolveContext& _context ): context{_context} {};
|
---|
[4894239] | 77 |
|
---|
| 78 | void previsit( const ast::Node * ) {
|
---|
| 79 | // short circuit if we already know there are designations
|
---|
| 80 | if ( result ) visit_children = false;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | void previsit( const ast::Designation * des ) {
|
---|
| 84 | if ( result ) visit_children = false;
|
---|
| 85 | else if ( ! des->designators.empty() ) {
|
---|
| 86 | if ( (des->designators.size() == 1) ) {
|
---|
| 87 | const ast::Expr * designator = des->designators.at(0);
|
---|
| 88 | if ( const ast::NameExpr * designatorName = dynamic_cast<const ast::NameExpr *>(designator) ) {
|
---|
| 89 | auto candidates = context.symtab.lookupId(designatorName->name);
|
---|
| 90 | for ( auto candidate : candidates ) {
|
---|
| 91 | if ( dynamic_cast<const ast::EnumInstType *>(candidate.id->get_type()) ) {
|
---|
| 92 | result = true;
|
---|
| 93 | break;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
[2345ab3] | 96 | }
|
---|
| 97 | }
|
---|
[4894239] | 98 | visit_children = false;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | };
|
---|
[d57e349] | 102 | } // anonymous namespace
|
---|
| 103 | /// Check if this expression is or includes a deleted expression
|
---|
| 104 | const ast::DeletedExpr * findDeletedExpr( const ast::Expr * expr ) {
|
---|
[0bd3faf] | 105 | return ast::Pass<DeleteFinder>::read( expr );
|
---|
[d57e349] | 106 | }
|
---|
[99d4584] | 107 |
|
---|
[d57e349] | 108 | namespace {
|
---|
[b7d92b96] | 109 | /// always-accept candidate filter
|
---|
| 110 | bool anyCandidate( const Candidate & ) { return true; }
|
---|
| 111 |
|
---|
[99d4584] | 112 | /// Calls the CandidateFinder and finds the single best candidate
|
---|
| 113 | CandidateRef findUnfinishedKindExpression(
|
---|
[39d8950] | 114 | const ast::Expr * untyped, const ResolveContext & context, const std::string & kind,
|
---|
[4a89b52] | 115 | std::function<bool(const Candidate &)> pred = anyCandidate, ResolveMode mode = {}
|
---|
[99d4584] | 116 | ) {
|
---|
| 117 | if ( ! untyped ) return nullptr;
|
---|
| 118 |
|
---|
| 119 | // xxx - this isn't thread-safe, but should work until we parallelize the resolver
|
---|
| 120 | static unsigned recursion_level = 0;
|
---|
| 121 |
|
---|
| 122 | ++recursion_level;
|
---|
| 123 | ast::TypeEnvironment env;
|
---|
[39d8950] | 124 | CandidateFinder finder( context, env );
|
---|
[46da46b] | 125 | finder.allowVoid = true;
|
---|
[99d4584] | 126 | finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode );
|
---|
| 127 | --recursion_level;
|
---|
| 128 |
|
---|
| 129 | // produce a filtered list of candidates
|
---|
| 130 | CandidateList candidates;
|
---|
| 131 | for ( auto & cand : finder.candidates ) {
|
---|
| 132 | if ( pred( *cand ) ) { candidates.emplace_back( cand ); }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // produce invalid error if no candidates
|
---|
| 136 | if ( candidates.empty() ) {
|
---|
[ef5b828] | 137 | SemanticError( untyped,
|
---|
| 138 | toString( "No reasonable alternatives for ", kind, (kind != "" ? " " : ""),
|
---|
[99d4584] | 139 | "expression: ") );
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | // search for cheapest candidate
|
---|
| 143 | CandidateList winners;
|
---|
| 144 | bool seen_undeleted = false;
|
---|
| 145 | for ( CandidateRef & cand : candidates ) {
|
---|
| 146 | int c = winners.empty() ? -1 : cand->cost.compare( winners.front()->cost );
|
---|
| 147 |
|
---|
| 148 | if ( c > 0 ) continue; // skip more expensive than winner
|
---|
| 149 |
|
---|
| 150 | if ( c < 0 ) {
|
---|
| 151 | // reset on new cheapest
|
---|
| 152 | seen_undeleted = ! findDeletedExpr( cand->expr );
|
---|
| 153 | winners.clear();
|
---|
| 154 | } else /* if ( c == 0 ) */ {
|
---|
| 155 | if ( findDeletedExpr( cand->expr ) ) {
|
---|
| 156 | // skip deleted expression if already seen one equivalent-cost not
|
---|
| 157 | if ( seen_undeleted ) continue;
|
---|
| 158 | } else if ( ! seen_undeleted ) {
|
---|
| 159 | // replace list of equivalent-cost deleted expressions with one non-deleted
|
---|
| 160 | winners.clear();
|
---|
| 161 | seen_undeleted = true;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | winners.emplace_back( std::move( cand ) );
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | // promote candidate.cvtCost to .cost
|
---|
[46da46b] | 169 | // promoteCvtCost( winners );
|
---|
[99d4584] | 170 |
|
---|
| 171 | // produce ambiguous errors, if applicable
|
---|
| 172 | if ( winners.size() != 1 ) {
|
---|
| 173 | std::ostringstream stream;
|
---|
[ef5b828] | 174 | stream << "Cannot choose between " << winners.size() << " alternatives for "
|
---|
[99d4584] | 175 | << kind << (kind != "" ? " " : "") << "expression\n";
|
---|
| 176 | ast::print( stream, untyped );
|
---|
| 177 | stream << " Alternatives are:\n";
|
---|
| 178 | print( stream, winners, 1 );
|
---|
| 179 | SemanticError( untyped->location, stream.str() );
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | // single selected choice
|
---|
| 183 | CandidateRef & choice = winners.front();
|
---|
| 184 |
|
---|
| 185 | // fail on only expression deleted
|
---|
| 186 | if ( ! seen_undeleted ) {
|
---|
| 187 | SemanticError( untyped->location, choice->expr.get(), "Unique best alternative "
|
---|
| 188 | "includes deleted identifier in " );
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | return std::move( choice );
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /// Strips extraneous casts out of an expression
|
---|
[0bd3faf] | 195 | struct StripCasts final {
|
---|
[c408483] | 196 | const ast::Expr * postvisit( const ast::CastExpr * castExpr ) {
|
---|
[ef5b828] | 197 | if (
|
---|
[2890212] | 198 | castExpr->isGenerated == ast::GeneratedCast
|
---|
[ef5b828] | 199 | && typesCompatible( castExpr->arg->result, castExpr->result )
|
---|
[99d4584] | 200 | ) {
|
---|
| 201 | // generated cast is the same type as its argument, remove it after keeping env
|
---|
[ef5b828] | 202 | return ast::mutate_field(
|
---|
[b7d92b96] | 203 | castExpr->arg.get(), &ast::Expr::env, castExpr->env );
|
---|
[99d4584] | 204 | }
|
---|
| 205 | return castExpr;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | static void strip( ast::ptr< ast::Expr > & expr ) {
|
---|
[0bd3faf] | 209 | ast::Pass< StripCasts > stripper;
|
---|
[99d4584] | 210 | expr = expr->accept( stripper );
|
---|
| 211 | }
|
---|
| 212 | };
|
---|
| 213 |
|
---|
[60aaa51d] | 214 | /// Swaps argument into expression pointer, saving original environment
|
---|
| 215 | void swap_and_save_env( ast::ptr< ast::Expr > & expr, const ast::Expr * newExpr ) {
|
---|
| 216 | ast::ptr< ast::TypeSubstitution > env = expr->env;
|
---|
| 217 | expr.set_and_mutate( newExpr )->env = env;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[b7d92b96] | 220 | /// Removes cast to type of argument (unlike StripCasts, also handles non-generated casts)
|
---|
[918e4165] | 221 | void removeExtraneousCast( ast::ptr<ast::Expr> & expr ) {
|
---|
[b7d92b96] | 222 | if ( const ast::CastExpr * castExpr = expr.as< ast::CastExpr >() ) {
|
---|
[251ce80] | 223 | if ( typesCompatible( castExpr->arg->result, castExpr->result ) ) {
|
---|
[b7d92b96] | 224 | // cast is to the same type as its argument, remove it
|
---|
[60aaa51d] | 225 | swap_and_save_env( expr, castExpr->arg );
|
---|
[b7d92b96] | 226 | }
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[6668a3e] | 230 |
|
---|
[490fb92e] | 231 | } // anonymous namespace
|
---|
| 232 | /// Establish post-resolver invariants for expressions
|
---|
[ef5b828] | 233 | void finishExpr(
|
---|
| 234 | ast::ptr< ast::Expr > & expr, const ast::TypeEnvironment & env,
|
---|
[99d4584] | 235 | const ast::TypeSubstitution * oldenv = nullptr
|
---|
| 236 | ) {
|
---|
| 237 | // set up new type substitution for expression
|
---|
[ef5b828] | 238 | ast::ptr< ast::TypeSubstitution > newenv =
|
---|
[99d4584] | 239 | oldenv ? oldenv : new ast::TypeSubstitution{};
|
---|
| 240 | env.writeToSubstitution( *newenv.get_and_mutate() );
|
---|
| 241 | expr.get_and_mutate()->env = std::move( newenv );
|
---|
| 242 | // remove unncecessary casts
|
---|
[0bd3faf] | 243 | StripCasts::strip( expr );
|
---|
[99d4584] | 244 | }
|
---|
[ef5b828] | 245 |
|
---|
[4b7cce6] | 246 | ast::ptr< ast::Expr > resolveInVoidContext(
|
---|
[39d8950] | 247 | const ast::Expr * expr, const ResolveContext & context,
|
---|
| 248 | ast::TypeEnvironment & env
|
---|
[4b7cce6] | 249 | ) {
|
---|
| 250 | assertf( expr, "expected a non-null expression" );
|
---|
[ef5b828] | 251 |
|
---|
[4b7cce6] | 252 | // set up and resolve expression cast to void
|
---|
[417117e] | 253 | ast::ptr< ast::CastExpr > untyped = new ast::CastExpr{ expr };
|
---|
[ef5b828] | 254 | CandidateRef choice = findUnfinishedKindExpression(
|
---|
[4a89b52] | 255 | untyped, context, "", anyCandidate, ResolveMode::withAdjustment() );
|
---|
[ef5b828] | 256 |
|
---|
[4b7cce6] | 257 | // a cast expression has either 0 or 1 interpretations (by language rules);
|
---|
| 258 | // if 0, an exception has already been thrown, and this code will not run
|
---|
| 259 | const ast::CastExpr * castExpr = choice->expr.strict_as< ast::CastExpr >();
|
---|
| 260 | env = std::move( choice->env );
|
---|
| 261 |
|
---|
| 262 | return castExpr->arg;
|
---|
| 263 | }
|
---|
[b7d92b96] | 264 |
|
---|
[490fb92e] | 265 | /// Resolve `untyped` to the expression whose candidate is the best match for a `void`
|
---|
[b7d92b96] | 266 | /// context.
|
---|
[ef5b828] | 267 | ast::ptr< ast::Expr > findVoidExpression(
|
---|
[39d8950] | 268 | const ast::Expr * untyped, const ResolveContext & context
|
---|
[b7d92b96] | 269 | ) {
|
---|
| 270 | ast::TypeEnvironment env;
|
---|
[39d8950] | 271 | ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, context, env );
|
---|
[b7d92b96] | 272 | finishExpr( newExpr, env, untyped->env );
|
---|
| 273 | return newExpr;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[490fb92e] | 276 | namespace {
|
---|
[6668a3e] | 277 |
|
---|
[490fb92e] | 278 |
|
---|
[ef5b828] | 279 | /// resolve `untyped` to the expression whose candidate satisfies `pred` with the
|
---|
[99d4584] | 280 | /// lowest cost, returning the resolved version
|
---|
| 281 | ast::ptr< ast::Expr > findKindExpression(
|
---|
[39d8950] | 282 | const ast::Expr * untyped, const ResolveContext & context,
|
---|
[ef5b828] | 283 | std::function<bool(const Candidate &)> pred = anyCandidate,
|
---|
[4a89b52] | 284 | const std::string & kind = "", ResolveMode mode = {}
|
---|
[99d4584] | 285 | ) {
|
---|
| 286 | if ( ! untyped ) return {};
|
---|
[ef5b828] | 287 | CandidateRef choice =
|
---|
[39d8950] | 288 | findUnfinishedKindExpression( untyped, context, kind, pred, mode );
|
---|
[490fb92e] | 289 | ResolvExpr::finishExpr( choice->expr, choice->env, untyped->env );
|
---|
[99d4584] | 290 | return std::move( choice->expr );
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[2773ab8] | 293 | /// Resolve `untyped` to the single expression whose candidate is the best match
|
---|
[ef5b828] | 294 | ast::ptr< ast::Expr > findSingleExpression(
|
---|
[39d8950] | 295 | const ast::Expr * untyped, const ResolveContext & context
|
---|
[2773ab8] | 296 | ) {
|
---|
[57e0289] | 297 | Stats::ResolveTime::start( untyped );
|
---|
[39d8950] | 298 | auto res = findKindExpression( untyped, context );
|
---|
[57e0289] | 299 | Stats::ResolveTime::stop();
|
---|
| 300 | return res;
|
---|
[2773ab8] | 301 | }
|
---|
[18e683b] | 302 | } // anonymous namespace
|
---|
[2773ab8] | 303 |
|
---|
[16ba4a6f] | 304 | ast::ptr< ast::Expr > findSingleExpression(
|
---|
[39d8950] | 305 | const ast::Expr * untyped, const ast::Type * type,
|
---|
| 306 | const ResolveContext & context
|
---|
[16ba4a6f] | 307 | ) {
|
---|
| 308 | assert( untyped && type );
|
---|
| 309 | ast::ptr< ast::Expr > castExpr = new ast::CastExpr{ untyped, type };
|
---|
[39d8950] | 310 | ast::ptr< ast::Expr > newExpr = findSingleExpression( castExpr, context );
|
---|
[918e4165] | 311 | removeExtraneousCast( newExpr );
|
---|
[16ba4a6f] | 312 | return newExpr;
|
---|
| 313 | }
|
---|
[b7d92b96] | 314 |
|
---|
[18e683b] | 315 | namespace {
|
---|
[16ba4a6f] | 316 | bool structOrUnion( const Candidate & i ) {
|
---|
| 317 | const ast::Type * t = i.expr->result->stripReferences();
|
---|
| 318 | return dynamic_cast< const ast::StructInstType * >( t ) || dynamic_cast< const ast::UnionInstType * >( t );
|
---|
| 319 | }
|
---|
[99d4584] | 320 | /// Predicate for "Candidate has integral type"
|
---|
| 321 | bool hasIntegralType( const Candidate & i ) {
|
---|
| 322 | const ast::Type * type = i.expr->result;
|
---|
[ef5b828] | 323 |
|
---|
[99d4584] | 324 | if ( auto bt = dynamic_cast< const ast::BasicType * >( type ) ) {
|
---|
| 325 | return bt->isInteger();
|
---|
[ef5b828] | 326 | } else if (
|
---|
| 327 | dynamic_cast< const ast::EnumInstType * >( type )
|
---|
[99d4584] | 328 | || dynamic_cast< const ast::ZeroType * >( type )
|
---|
| 329 | || dynamic_cast< const ast::OneType * >( type )
|
---|
| 330 | ) {
|
---|
| 331 | return true;
|
---|
| 332 | } else return false;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | /// Resolve `untyped` as an integral expression, returning the resolved version
|
---|
[ef5b828] | 336 | ast::ptr< ast::Expr > findIntegralExpression(
|
---|
[39d8950] | 337 | const ast::Expr * untyped, const ResolveContext & context
|
---|
[99d4584] | 338 | ) {
|
---|
[39d8950] | 339 | return findKindExpression( untyped, context, hasIntegralType, "condition" );
|
---|
[99d4584] | 340 | }
|
---|
[60aaa51d] | 341 |
|
---|
| 342 | /// check if a type is a character type
|
---|
| 343 | bool isCharType( const ast::Type * t ) {
|
---|
| 344 | if ( auto bt = dynamic_cast< const ast::BasicType * >( t ) ) {
|
---|
[ef5b828] | 345 | return bt->kind == ast::BasicType::Char
|
---|
| 346 | || bt->kind == ast::BasicType::SignedChar
|
---|
[60aaa51d] | 347 | || bt->kind == ast::BasicType::UnsignedChar;
|
---|
| 348 | }
|
---|
| 349 | return false;
|
---|
| 350 | }
|
---|
[2773ab8] | 351 |
|
---|
| 352 | /// Advance a type itertor to the next mutex parameter
|
---|
| 353 | template<typename Iter>
|
---|
| 354 | inline bool nextMutex( Iter & it, const Iter & end ) {
|
---|
[954c954] | 355 | while ( it != end && ! (*it)->is_mutex() ) { ++it; }
|
---|
[2773ab8] | 356 | return it != end;
|
---|
| 357 | }
|
---|
[99d4584] | 358 | }
|
---|
| 359 |
|
---|
[0bd3faf] | 360 | class Resolver final
|
---|
[4864a73] | 361 | : public ast::WithSymbolTable, public ast::WithGuards,
|
---|
[0bd3faf] | 362 | public ast::WithVisitorRef<Resolver>, public ast::WithShortCircuiting,
|
---|
[0e42794] | 363 | public ast::WithStmtsToAdd<> {
|
---|
[4864a73] | 364 |
|
---|
[2a8f0c1] | 365 | ast::ptr< ast::Type > functionReturn = nullptr;
|
---|
[2b59f55] | 366 | ast::CurrentObject currentObject;
|
---|
[16ba4a6f] | 367 | // for work previously in GenInit
|
---|
[0bd3faf] | 368 | static InitTweak::ManagedTypes managedTypes;
|
---|
[3bc69f2] | 369 | ResolveContext context;
|
---|
[16ba4a6f] | 370 |
|
---|
[99d4584] | 371 | bool inEnumDecl = false;
|
---|
[2a8f0c1] | 372 |
|
---|
[4864a73] | 373 | public:
|
---|
[c15085d] | 374 | static size_t traceId;
|
---|
[0bd3faf] | 375 | Resolver( const ast::TranslationGlobal & global ) :
|
---|
[b9fe89b] | 376 | ast::WithSymbolTable(ast::SymbolTable::ErrorDetection::ValidateOnAdd),
|
---|
[3bc69f2] | 377 | context{ symtab, global } {}
|
---|
[0bd3faf] | 378 | Resolver( const ResolveContext & context ) :
|
---|
[3bc69f2] | 379 | ast::WithSymbolTable{ context.symtab },
|
---|
| 380 | context{ symtab, context.global } {}
|
---|
[d76c588] | 381 |
|
---|
[16ba4a6f] | 382 | const ast::FunctionDecl * previsit( const ast::FunctionDecl * );
|
---|
[99d4584] | 383 | const ast::FunctionDecl * postvisit( const ast::FunctionDecl * );
|
---|
[16ba4a6f] | 384 | const ast::ObjectDecl * previsit( const ast::ObjectDecl * );
|
---|
| 385 | void previsit( const ast::AggregateDecl * );
|
---|
| 386 | void previsit( const ast::StructDecl * );
|
---|
[99d4584] | 387 | void previsit( const ast::EnumDecl * );
|
---|
| 388 | const ast::StaticAssertDecl * previsit( const ast::StaticAssertDecl * );
|
---|
| 389 |
|
---|
[0f6a7752] | 390 | const ast::ArrayType * previsit( const ast::ArrayType * );
|
---|
| 391 | const ast::PointerType * previsit( const ast::PointerType * );
|
---|
[99d4584] | 392 |
|
---|
[2773ab8] | 393 | const ast::ExprStmt * previsit( const ast::ExprStmt * );
|
---|
| 394 | const ast::AsmExpr * previsit( const ast::AsmExpr * );
|
---|
| 395 | const ast::AsmStmt * previsit( const ast::AsmStmt * );
|
---|
| 396 | const ast::IfStmt * previsit( const ast::IfStmt * );
|
---|
[3bc69f2] | 397 | const ast::WhileDoStmt * previsit( const ast::WhileDoStmt * );
|
---|
[2773ab8] | 398 | const ast::ForStmt * previsit( const ast::ForStmt * );
|
---|
| 399 | const ast::SwitchStmt * previsit( const ast::SwitchStmt * );
|
---|
[400b8be] | 400 | const ast::CaseClause * previsit( const ast::CaseClause * );
|
---|
[2773ab8] | 401 | const ast::BranchStmt * previsit( const ast::BranchStmt * );
|
---|
| 402 | const ast::ReturnStmt * previsit( const ast::ReturnStmt * );
|
---|
| 403 | const ast::ThrowStmt * previsit( const ast::ThrowStmt * );
|
---|
[400b8be] | 404 | const ast::CatchClause * previsit( const ast::CatchClause * );
|
---|
| 405 | const ast::CatchClause * postvisit( const ast::CatchClause * );
|
---|
[2773ab8] | 406 | const ast::WaitForStmt * previsit( const ast::WaitForStmt * );
|
---|
[16ba4a6f] | 407 | const ast::WithStmt * previsit( const ast::WithStmt * );
|
---|
[99d4584] | 408 |
|
---|
[2d11663] | 409 | const ast::SingleInit * previsit( const ast::SingleInit * );
|
---|
| 410 | const ast::ListInit * previsit( const ast::ListInit * );
|
---|
| 411 | const ast::ConstructorInit * previsit( const ast::ConstructorInit * );
|
---|
[16ba4a6f] | 412 |
|
---|
| 413 | void resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd);
|
---|
| 414 |
|
---|
| 415 | void beginScope() { managedTypes.beginScope(); }
|
---|
| 416 | void endScope() { managedTypes.endScope(); }
|
---|
[e00c22f] | 417 | bool on_error(ast::ptr<ast::Decl> & decl);
|
---|
[d76c588] | 418 | };
|
---|
[0bd3faf] | 419 | // size_t Resolver::traceId = Stats::Heap::new_stacktrace_id("Resolver");
|
---|
[d76c588] | 420 |
|
---|
[0bd3faf] | 421 | InitTweak::ManagedTypes Resolver::managedTypes;
|
---|
[16ba4a6f] | 422 |
|
---|
[293dc1c] | 423 | void resolve( ast::TranslationUnit& translationUnit ) {
|
---|
[0bd3faf] | 424 | ast::Pass< Resolver >::run( translationUnit, translationUnit.global );
|
---|
[d76c588] | 425 | }
|
---|
| 426 |
|
---|
[ef5b828] | 427 | ast::ptr< ast::Init > resolveCtorInit(
|
---|
[39d8950] | 428 | const ast::ConstructorInit * ctorInit, const ResolveContext & context
|
---|
[234b1cb] | 429 | ) {
|
---|
| 430 | assert( ctorInit );
|
---|
[0bd3faf] | 431 | ast::Pass< Resolver > resolver( context );
|
---|
[234b1cb] | 432 | return ctorInit->accept( resolver );
|
---|
| 433 | }
|
---|
| 434 |
|
---|
[302ef2a] | 435 | const ast::Expr * resolveStmtExpr(
|
---|
[39d8950] | 436 | const ast::StmtExpr * stmtExpr, const ResolveContext & context
|
---|
[17a0ede2] | 437 | ) {
|
---|
| 438 | assert( stmtExpr );
|
---|
[0bd3faf] | 439 | ast::Pass< Resolver > resolver( context );
|
---|
[302ef2a] | 440 | auto ret = mutate(stmtExpr->accept(resolver));
|
---|
| 441 | strict_dynamic_cast< ast::StmtExpr * >( ret )->computeResult();
|
---|
[17a0ede2] | 442 | return ret;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[16ba4a6f] | 445 | namespace {
|
---|
[39d8950] | 446 | const ast::Attribute * handleAttribute(const CodeLocation & loc, const ast::Attribute * attr, const ResolveContext & context) {
|
---|
[16ba4a6f] | 447 | std::string name = attr->normalizedName();
|
---|
| 448 | if (name == "constructor" || name == "destructor") {
|
---|
| 449 | if (attr->params.size() == 1) {
|
---|
| 450 | auto arg = attr->params.front();
|
---|
[39d8950] | 451 | auto resolved = ResolvExpr::findSingleExpression( arg, new ast::BasicType( ast::BasicType::LongLongSignedInt ), context );
|
---|
[16ba4a6f] | 452 | auto result = eval(arg);
|
---|
| 453 |
|
---|
| 454 | auto mutAttr = mutate(attr);
|
---|
| 455 | mutAttr->params.front() = resolved;
|
---|
[8f557161] | 456 | if (! result.hasKnownValue) {
|
---|
[16ba4a6f] | 457 | SemanticWarning(loc, Warning::GccAttributes,
|
---|
| 458 | toCString( name, " priorities must be integers from 0 to 65535 inclusive: ", arg ) );
|
---|
| 459 | }
|
---|
| 460 | else {
|
---|
[8f557161] | 461 | auto priority = result.knownValue;
|
---|
[16ba4a6f] | 462 | if (priority < 101) {
|
---|
| 463 | SemanticWarning(loc, Warning::GccAttributes,
|
---|
| 464 | toCString( name, " priorities from 0 to 100 are reserved for the implementation" ) );
|
---|
| 465 | } else if (priority < 201 && ! buildingLibrary()) {
|
---|
| 466 | SemanticWarning(loc, Warning::GccAttributes,
|
---|
| 467 | toCString( name, " priorities from 101 to 200 are reserved for the implementation" ) );
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
| 470 | return mutAttr;
|
---|
| 471 | } else if (attr->params.size() > 1) {
|
---|
| 472 | SemanticWarning(loc, Warning::GccAttributes, toCString( "too many arguments to ", name, " attribute" ) );
|
---|
| 473 | } else {
|
---|
| 474 | SemanticWarning(loc, Warning::GccAttributes, toCString( "too few arguments to ", name, " attribute" ) );
|
---|
| 475 | }
|
---|
| 476 | }
|
---|
| 477 | return attr;
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[0bd3faf] | 481 | const ast::FunctionDecl * Resolver::previsit( const ast::FunctionDecl * functionDecl ) {
|
---|
[2a8f0c1] | 482 | GuardValue( functionReturn );
|
---|
[16ba4a6f] | 483 |
|
---|
| 484 | assert (functionDecl->unique());
|
---|
| 485 | if (!functionDecl->has_body() && !functionDecl->withExprs.empty()) {
|
---|
| 486 | SemanticError(functionDecl->location, functionDecl, "Function without body has with declarations");
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | if (!functionDecl->isTypeFixed) {
|
---|
| 490 | auto mutDecl = mutate(functionDecl);
|
---|
| 491 | auto mutType = mutDecl->type.get_and_mutate();
|
---|
| 492 |
|
---|
| 493 | for (auto & attr: mutDecl->attributes) {
|
---|
[3bc69f2] | 494 | attr = handleAttribute(mutDecl->location, attr, context );
|
---|
[16ba4a6f] | 495 | }
|
---|
| 496 |
|
---|
[3e5dd913] | 497 | // handle assertions
|
---|
[16ba4a6f] | 498 |
|
---|
| 499 | symtab.enterScope();
|
---|
[3e5dd913] | 500 | mutType->forall.clear();
|
---|
| 501 | mutType->assertions.clear();
|
---|
| 502 | for (auto & typeParam : mutDecl->type_params) {
|
---|
| 503 | symtab.addType(typeParam);
|
---|
[b230091] | 504 | mutType->forall.emplace_back(new ast::TypeInstType(typeParam));
|
---|
[3e5dd913] | 505 | }
|
---|
| 506 | for (auto & asst : mutDecl->assertions) {
|
---|
[3bc69f2] | 507 | asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), context);
|
---|
[3e5dd913] | 508 | symtab.addId(asst);
|
---|
| 509 | mutType->assertions.emplace_back(new ast::VariableExpr(functionDecl->location, asst));
|
---|
[16ba4a6f] | 510 | }
|
---|
| 511 |
|
---|
| 512 | // temporarily adds params to symbol table.
|
---|
| 513 | // actual scoping rules for params and withexprs differ - see Pass::visit(FunctionDecl)
|
---|
| 514 |
|
---|
| 515 | std::vector<ast::ptr<ast::Type>> paramTypes;
|
---|
| 516 | std::vector<ast::ptr<ast::Type>> returnTypes;
|
---|
| 517 |
|
---|
| 518 | for (auto & param : mutDecl->params) {
|
---|
[3bc69f2] | 519 | param = fixObjectType(param.strict_as<ast::ObjectDecl>(), context);
|
---|
[16ba4a6f] | 520 | symtab.addId(param);
|
---|
| 521 | paramTypes.emplace_back(param->get_type());
|
---|
| 522 | }
|
---|
| 523 | for (auto & ret : mutDecl->returns) {
|
---|
[3bc69f2] | 524 | ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), context);
|
---|
[16ba4a6f] | 525 | returnTypes.emplace_back(ret->get_type());
|
---|
| 526 | }
|
---|
| 527 | // since function type in decl is just a view of param types, need to update that as well
|
---|
| 528 | mutType->params = std::move(paramTypes);
|
---|
| 529 | mutType->returns = std::move(returnTypes);
|
---|
| 530 |
|
---|
[3e5dd913] | 531 | auto renamedType = strict_dynamic_cast<const ast::FunctionType *>(renameTyVars(mutType, RenameMode::GEN_EXPR_ID));
|
---|
| 532 |
|
---|
[16ba4a6f] | 533 | std::list<ast::ptr<ast::Stmt>> newStmts;
|
---|
| 534 | resolveWithExprs (mutDecl->withExprs, newStmts);
|
---|
| 535 |
|
---|
| 536 | if (mutDecl->stmts) {
|
---|
| 537 | auto mutStmt = mutDecl->stmts.get_and_mutate();
|
---|
| 538 | mutStmt->kids.splice(mutStmt->kids.begin(), std::move(newStmts));
|
---|
| 539 | mutDecl->stmts = mutStmt;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | symtab.leaveScope();
|
---|
| 543 |
|
---|
[3e5dd913] | 544 | mutDecl->type = renamedType;
|
---|
[16ba4a6f] | 545 | mutDecl->mangleName = Mangle::mangle(mutDecl);
|
---|
| 546 | mutDecl->isTypeFixed = true;
|
---|
| 547 | functionDecl = mutDecl;
|
---|
| 548 | }
|
---|
| 549 | managedTypes.handleDWT(functionDecl);
|
---|
| 550 |
|
---|
[2a8f0c1] | 551 | functionReturn = extractResultType( functionDecl->type );
|
---|
[16ba4a6f] | 552 | return functionDecl;
|
---|
[d76c588] | 553 | }
|
---|
| 554 |
|
---|
[0bd3faf] | 555 | const ast::FunctionDecl * Resolver::postvisit( const ast::FunctionDecl * functionDecl ) {
|
---|
[4864a73] | 556 | // default value expressions have an environment which shouldn't be there and trips up
|
---|
[2a8f0c1] | 557 | // later passes.
|
---|
[e068c8a] | 558 | assert( functionDecl->unique() );
|
---|
| 559 | ast::FunctionType * mutType = mutate( functionDecl->type.get() );
|
---|
| 560 |
|
---|
| 561 | for ( unsigned i = 0 ; i < mutType->params.size() ; ++i ) {
|
---|
| 562 | if ( const ast::ObjectDecl * obj = mutType->params[i].as< ast::ObjectDecl >() ) {
|
---|
| 563 | if ( const ast::SingleInit * init = obj->init.as< ast::SingleInit >() ) {
|
---|
| 564 | if ( init->value->env == nullptr ) continue;
|
---|
| 565 | // clone initializer minus the initializer environment
|
---|
| 566 | auto mutParam = mutate( mutType->params[i].strict_as< ast::ObjectDecl >() );
|
---|
| 567 | auto mutInit = mutate( mutParam->init.strict_as< ast::SingleInit >() );
|
---|
| 568 | auto mutValue = mutate( mutInit->value.get() );
|
---|
| 569 |
|
---|
| 570 | mutValue->env = nullptr;
|
---|
| 571 | mutInit->value = mutValue;
|
---|
| 572 | mutParam->init = mutInit;
|
---|
| 573 | mutType->params[i] = mutParam;
|
---|
| 574 |
|
---|
| 575 | assert( ! mutType->params[i].strict_as< ast::ObjectDecl >()->init.strict_as< ast::SingleInit >()->value->env);
|
---|
| 576 | }
|
---|
| 577 | }
|
---|
[2a8f0c1] | 578 | }
|
---|
[73973b6] | 579 | mutate_field(functionDecl, &ast::FunctionDecl::type, mutType);
|
---|
| 580 | return functionDecl;
|
---|
[d76c588] | 581 | }
|
---|
| 582 |
|
---|
[0bd3faf] | 583 | const ast::ObjectDecl * Resolver::previsit( const ast::ObjectDecl * objectDecl ) {
|
---|
[ef5b828] | 584 | // To handle initialization of routine pointers [e.g. int (*fp)(int) = foo()],
|
---|
| 585 | // class-variable `initContext` is changed multiple times because the LHS is analyzed
|
---|
| 586 | // twice. The second analysis changes `initContext` because a function type can contain
|
---|
| 587 | // object declarations in the return and parameter types. Therefore each value of
|
---|
| 588 | // `initContext` is retained so the type on the first analysis is preserved and used for
|
---|
[b7d92b96] | 589 | // selecting the RHS.
|
---|
| 590 | GuardValue( currentObject );
|
---|
[16ba4a6f] | 591 |
|
---|
[b7d92b96] | 592 | if ( inEnumDecl && dynamic_cast< const ast::EnumInstType * >( objectDecl->get_type() ) ) {
|
---|
[ef5b828] | 593 | // enumerator initializers should not use the enum type to initialize, since the
|
---|
[b7d92b96] | 594 | // enum type is still incomplete at this point. Use `int` instead.
|
---|
[4559b34] | 595 |
|
---|
[12df6fe] | 596 | if ( auto enumBase = dynamic_cast< const ast::EnumInstType * >
|
---|
| 597 | ( objectDecl->get_type() )->base->base ) {
|
---|
[5bb1ac1] | 598 | objectDecl = fixObjectType( objectDecl, context );
|
---|
[2345ab3] | 599 | currentObject = ast::CurrentObject{
|
---|
| 600 | objectDecl->location,
|
---|
[5bb1ac1] | 601 | enumBase
|
---|
| 602 | };
|
---|
[4559b34] | 603 | } else {
|
---|
[5bb1ac1] | 604 | objectDecl = fixObjectType( objectDecl, context );
|
---|
[4559b34] | 605 | currentObject = ast::CurrentObject{
|
---|
| 606 | objectDecl->location, new ast::BasicType{ ast::BasicType::SignedInt } };
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[b7d92b96] | 609 | }
|
---|
[16ba4a6f] | 610 | else {
|
---|
[12df6fe] | 611 | if ( !objectDecl->isTypeFixed ) {
|
---|
[3bc69f2] | 612 | auto newDecl = fixObjectType(objectDecl, context);
|
---|
[16ba4a6f] | 613 | auto mutDecl = mutate(newDecl);
|
---|
[4a8f150] | 614 |
|
---|
[16ba4a6f] | 615 | // generate CtorInit wrapper when necessary.
|
---|
| 616 | // in certain cases, fixObjectType is called before reaching
|
---|
| 617 | // this object in visitor pass, thus disabling CtorInit codegen.
|
---|
| 618 | // this happens on aggregate members and function parameters.
|
---|
| 619 | if ( InitTweak::tryConstruct( mutDecl ) && ( managedTypes.isManaged( mutDecl ) || ((! isInFunction() || mutDecl->storage.is_static ) && ! InitTweak::isConstExpr( mutDecl->init ) ) ) ) {
|
---|
| 620 | // constructed objects cannot be designated
|
---|
[92355883] | 621 | if ( InitTweak::isDesignated( mutDecl->init ) ) {
|
---|
[0bd3faf] | 622 | ast::Pass<ResolveDesignators> res( context );
|
---|
[4894239] | 623 | maybe_accept( mutDecl->init.get(), res );
|
---|
| 624 | if ( !res.core.result ) {
|
---|
[ca9d65e] | 625 | SemanticError( mutDecl, "Cannot include designations in the initializer for a managed Object.\n"
|
---|
| 626 | "If this is really what you want, initialize with @=." );
|
---|
[4894239] | 627 | }
|
---|
[92355883] | 628 | }
|
---|
[16ba4a6f] | 629 | // constructed objects should not have initializers nested too deeply
|
---|
| 630 | if ( ! InitTweak::checkInitDepth( mutDecl ) ) SemanticError( mutDecl, "Managed object's initializer is too deep " );
|
---|
| 631 |
|
---|
| 632 | mutDecl->init = InitTweak::genCtorInit( mutDecl->location, mutDecl );
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | objectDecl = mutDecl;
|
---|
| 636 | }
|
---|
| 637 | currentObject = ast::CurrentObject{ objectDecl->location, objectDecl->get_type() };
|
---|
| 638 | }
|
---|
[4a8f150] | 639 |
|
---|
[16ba4a6f] | 640 | return objectDecl;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
[0bd3faf] | 643 | void Resolver::previsit( const ast::AggregateDecl * _aggDecl ) {
|
---|
[16ba4a6f] | 644 | auto aggDecl = mutate(_aggDecl);
|
---|
| 645 | assertf(aggDecl == _aggDecl, "type declarations must be unique");
|
---|
| 646 |
|
---|
| 647 | for (auto & member: aggDecl->members) {
|
---|
| 648 | // nested type decls are hoisted already. no need to do anything
|
---|
| 649 | if (auto obj = member.as<ast::ObjectDecl>()) {
|
---|
[3bc69f2] | 650 | member = fixObjectType(obj, context);
|
---|
[16ba4a6f] | 651 | }
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 |
|
---|
[0bd3faf] | 655 | void Resolver::previsit( const ast::StructDecl * structDecl ) {
|
---|
[16ba4a6f] | 656 | previsit(static_cast<const ast::AggregateDecl *>(structDecl));
|
---|
| 657 | managedTypes.handleStruct(structDecl);
|
---|
[d76c588] | 658 | }
|
---|
| 659 |
|
---|
[0bd3faf] | 660 | void Resolver::previsit( const ast::EnumDecl * ) {
|
---|
[99d4584] | 661 | // in case we decide to allow nested enums
|
---|
| 662 | GuardValue( inEnumDecl );
|
---|
[2890212] | 663 | inEnumDecl = true;
|
---|
[16ba4a6f] | 664 | // don't need to fix types for enum fields
|
---|
[d76c588] | 665 | }
|
---|
| 666 |
|
---|
[0bd3faf] | 667 | const ast::StaticAssertDecl * Resolver::previsit(
|
---|
[ef5b828] | 668 | const ast::StaticAssertDecl * assertDecl
|
---|
[99d4584] | 669 | ) {
|
---|
[ef5b828] | 670 | return ast::mutate_field(
|
---|
| 671 | assertDecl, &ast::StaticAssertDecl::cond,
|
---|
[3bc69f2] | 672 | findIntegralExpression( assertDecl->cond, context ) );
|
---|
[b7d92b96] | 673 | }
|
---|
| 674 |
|
---|
| 675 | template< typename PtrType >
|
---|
[39d8950] | 676 | const PtrType * handlePtrType( const PtrType * type, const ResolveContext & context ) {
|
---|
[0f6a7752] | 677 | if ( type->dimension ) {
|
---|
[5cf1228] | 678 | const ast::Type * sizeType = context.global.sizeType.get();
|
---|
[9e23b446] | 679 | ast::ptr< ast::Expr > dimension = findSingleExpression( type->dimension, sizeType, context );
|
---|
| 680 | assertf(dimension->env->empty(), "array dimension expr has nonempty env");
|
---|
| 681 | dimension.get_and_mutate()->env = nullptr;
|
---|
[5cf1228] | 682 | ast::mutate_field( type, &PtrType::dimension, dimension );
|
---|
[0f6a7752] | 683 | }
|
---|
| 684 | return type;
|
---|
[d76c588] | 685 | }
|
---|
| 686 |
|
---|
[0bd3faf] | 687 | const ast::ArrayType * Resolver::previsit( const ast::ArrayType * at ) {
|
---|
[3bc69f2] | 688 | return handlePtrType( at, context );
|
---|
[d76c588] | 689 | }
|
---|
| 690 |
|
---|
[0bd3faf] | 691 | const ast::PointerType * Resolver::previsit( const ast::PointerType * pt ) {
|
---|
[3bc69f2] | 692 | return handlePtrType( pt, context );
|
---|
[d76c588] | 693 | }
|
---|
| 694 |
|
---|
[0bd3faf] | 695 | const ast::ExprStmt * Resolver::previsit( const ast::ExprStmt * exprStmt ) {
|
---|
[b7d92b96] | 696 | visit_children = false;
|
---|
| 697 | assertf( exprStmt->expr, "ExprStmt has null expression in resolver" );
|
---|
[ef5b828] | 698 |
|
---|
| 699 | return ast::mutate_field(
|
---|
[3bc69f2] | 700 | exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, context ) );
|
---|
[d76c588] | 701 | }
|
---|
| 702 |
|
---|
[0bd3faf] | 703 | const ast::AsmExpr * Resolver::previsit( const ast::AsmExpr * asmExpr ) {
|
---|
[b7d92b96] | 704 | visit_children = false;
|
---|
| 705 |
|
---|
[ef5b828] | 706 | asmExpr = ast::mutate_field(
|
---|
[3bc69f2] | 707 | asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, context ) );
|
---|
[ef5b828] | 708 |
|
---|
[b7d92b96] | 709 | return asmExpr;
|
---|
[d76c588] | 710 | }
|
---|
| 711 |
|
---|
[0bd3faf] | 712 | const ast::AsmStmt * Resolver::previsit( const ast::AsmStmt * asmStmt ) {
|
---|
[2b59f55] | 713 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::input );
|
---|
| 714 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::output );
|
---|
| 715 | visit_children = false;
|
---|
| 716 | return asmStmt;
|
---|
[d76c588] | 717 | }
|
---|
| 718 |
|
---|
[0bd3faf] | 719 | const ast::IfStmt * Resolver::previsit( const ast::IfStmt * ifStmt ) {
|
---|
[b7d92b96] | 720 | return ast::mutate_field(
|
---|
[3bc69f2] | 721 | ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, context ) );
|
---|
[d76c588] | 722 | }
|
---|
| 723 |
|
---|
[0bd3faf] | 724 | const ast::WhileDoStmt * Resolver::previsit( const ast::WhileDoStmt * whileDoStmt ) {
|
---|
[ef5b828] | 725 | return ast::mutate_field(
|
---|
[3bc69f2] | 726 | whileDoStmt, &ast::WhileDoStmt::cond, findIntegralExpression( whileDoStmt->cond, context ) );
|
---|
[d76c588] | 727 | }
|
---|
| 728 |
|
---|
[0bd3faf] | 729 | const ast::ForStmt * Resolver::previsit( const ast::ForStmt * forStmt ) {
|
---|
[b7d92b96] | 730 | if ( forStmt->cond ) {
|
---|
| 731 | forStmt = ast::mutate_field(
|
---|
[3bc69f2] | 732 | forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, context ) );
|
---|
[b7d92b96] | 733 | }
|
---|
| 734 |
|
---|
| 735 | if ( forStmt->inc ) {
|
---|
| 736 | forStmt = ast::mutate_field(
|
---|
[3bc69f2] | 737 | forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, context ) );
|
---|
[b7d92b96] | 738 | }
|
---|
| 739 |
|
---|
| 740 | return forStmt;
|
---|
[d76c588] | 741 | }
|
---|
| 742 |
|
---|
[0bd3faf] | 743 | const ast::SwitchStmt * Resolver::previsit( const ast::SwitchStmt * switchStmt ) {
|
---|
[b7d92b96] | 744 | GuardValue( currentObject );
|
---|
| 745 | switchStmt = ast::mutate_field(
|
---|
[ef5b828] | 746 | switchStmt, &ast::SwitchStmt::cond,
|
---|
[3bc69f2] | 747 | findIntegralExpression( switchStmt->cond, context ) );
|
---|
[2b59f55] | 748 | currentObject = ast::CurrentObject{ switchStmt->location, switchStmt->cond->result };
|
---|
[b7d92b96] | 749 | return switchStmt;
|
---|
[d76c588] | 750 | }
|
---|
| 751 |
|
---|
[0bd3faf] | 752 | const ast::CaseClause * Resolver::previsit( const ast::CaseClause * caseStmt ) {
|
---|
[b7d92b96] | 753 | if ( caseStmt->cond ) {
|
---|
[60aaa51d] | 754 | std::deque< ast::InitAlternative > initAlts = currentObject.getOptions();
|
---|
[2b59f55] | 755 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral "
|
---|
| 756 | "expression." );
|
---|
[ef5b828] | 757 |
|
---|
| 758 | ast::ptr< ast::Expr > untyped =
|
---|
[2b59f55] | 759 | new ast::CastExpr{ caseStmt->location, caseStmt->cond, initAlts.front().type };
|
---|
[3bc69f2] | 760 | ast::ptr< ast::Expr > newExpr = findSingleExpression( untyped, context );
|
---|
[ef5b828] | 761 |
|
---|
| 762 | // case condition cannot have a cast in C, so it must be removed here, regardless of
|
---|
[2b59f55] | 763 | // whether it would perform a conversion.
|
---|
| 764 | if ( const ast::CastExpr * castExpr = newExpr.as< ast::CastExpr >() ) {
|
---|
[60aaa51d] | 765 | swap_and_save_env( newExpr, castExpr->arg );
|
---|
[2b59f55] | 766 | }
|
---|
[ef5b828] | 767 |
|
---|
[400b8be] | 768 | caseStmt = ast::mutate_field( caseStmt, &ast::CaseClause::cond, newExpr );
|
---|
[b7d92b96] | 769 | }
|
---|
| 770 | return caseStmt;
|
---|
[d76c588] | 771 | }
|
---|
| 772 |
|
---|
[0bd3faf] | 773 | const ast::BranchStmt * Resolver::previsit( const ast::BranchStmt * branchStmt ) {
|
---|
[b7d92b96] | 774 | visit_children = false;
|
---|
| 775 | // must resolve the argument of a computed goto
|
---|
| 776 | if ( branchStmt->kind == ast::BranchStmt::Goto && branchStmt->computedTarget ) {
|
---|
| 777 | // computed goto argument is void*
|
---|
[2773ab8] | 778 | ast::ptr< ast::Type > target = new ast::PointerType{ new ast::VoidType{} };
|
---|
[b7d92b96] | 779 | branchStmt = ast::mutate_field(
|
---|
[ef5b828] | 780 | branchStmt, &ast::BranchStmt::computedTarget,
|
---|
[3bc69f2] | 781 | findSingleExpression( branchStmt->computedTarget, target, context ) );
|
---|
[b7d92b96] | 782 | }
|
---|
| 783 | return branchStmt;
|
---|
[d76c588] | 784 | }
|
---|
| 785 |
|
---|
[0bd3faf] | 786 | const ast::ReturnStmt * Resolver::previsit( const ast::ReturnStmt * returnStmt ) {
|
---|
[2b59f55] | 787 | visit_children = false;
|
---|
| 788 | if ( returnStmt->expr ) {
|
---|
| 789 | returnStmt = ast::mutate_field(
|
---|
[ef5b828] | 790 | returnStmt, &ast::ReturnStmt::expr,
|
---|
[3bc69f2] | 791 | findSingleExpression( returnStmt->expr, functionReturn, context ) );
|
---|
[2b59f55] | 792 | }
|
---|
| 793 | return returnStmt;
|
---|
[d76c588] | 794 | }
|
---|
| 795 |
|
---|
[0bd3faf] | 796 | const ast::ThrowStmt * Resolver::previsit( const ast::ThrowStmt * throwStmt ) {
|
---|
[2b59f55] | 797 | visit_children = false;
|
---|
| 798 | if ( throwStmt->expr ) {
|
---|
[ef5b828] | 799 | const ast::StructDecl * exceptionDecl =
|
---|
[3090127] | 800 | symtab.lookupStruct( "__cfaehm_base_exception_t" );
|
---|
[2b59f55] | 801 | assert( exceptionDecl );
|
---|
[ef5b828] | 802 | ast::ptr< ast::Type > exceptType =
|
---|
[2b59f55] | 803 | new ast::PointerType{ new ast::StructInstType{ exceptionDecl } };
|
---|
| 804 | throwStmt = ast::mutate_field(
|
---|
[ef5b828] | 805 | throwStmt, &ast::ThrowStmt::expr,
|
---|
[3bc69f2] | 806 | findSingleExpression( throwStmt->expr, exceptType, context ) );
|
---|
[2b59f55] | 807 | }
|
---|
| 808 | return throwStmt;
|
---|
[d76c588] | 809 | }
|
---|
| 810 |
|
---|
[0bd3faf] | 811 | const ast::CatchClause * Resolver::previsit( const ast::CatchClause * catchClause ) {
|
---|
[3b0bc16] | 812 | // Until we are very sure this invarent (ifs that move between passes have then)
|
---|
[b9fa85b] | 813 | // holds, check it. This allows a check for when to decode the mangling.
|
---|
[400b8be] | 814 | if ( auto ifStmt = catchClause->body.as<ast::IfStmt>() ) {
|
---|
[3b0bc16] | 815 | assert( ifStmt->then );
|
---|
[b9fa85b] | 816 | }
|
---|
| 817 | // Encode the catchStmt so the condition can see the declaration.
|
---|
[400b8be] | 818 | if ( catchClause->cond ) {
|
---|
| 819 | ast::CatchClause * clause = mutate( catchClause );
|
---|
| 820 | clause->body = new ast::IfStmt( clause->location, clause->cond, nullptr, clause->body );
|
---|
| 821 | clause->cond = nullptr;
|
---|
| 822 | return clause;
|
---|
[b9fa85b] | 823 | }
|
---|
[400b8be] | 824 | return catchClause;
|
---|
[b9fa85b] | 825 | }
|
---|
| 826 |
|
---|
[0bd3faf] | 827 | const ast::CatchClause * Resolver::postvisit( const ast::CatchClause * catchClause ) {
|
---|
[b9fa85b] | 828 | // Decode the catchStmt so everything is stored properly.
|
---|
[400b8be] | 829 | const ast::IfStmt * ifStmt = catchClause->body.as<ast::IfStmt>();
|
---|
[3b0bc16] | 830 | if ( nullptr != ifStmt && nullptr == ifStmt->then ) {
|
---|
[b9fa85b] | 831 | assert( ifStmt->cond );
|
---|
[3b0bc16] | 832 | assert( ifStmt->else_ );
|
---|
[400b8be] | 833 | ast::CatchClause * clause = ast::mutate( catchClause );
|
---|
| 834 | clause->cond = ifStmt->cond;
|
---|
| 835 | clause->body = ifStmt->else_;
|
---|
[b9fa85b] | 836 | // ifStmt should be implicately deleted here.
|
---|
[400b8be] | 837 | return clause;
|
---|
[2b59f55] | 838 | }
|
---|
[400b8be] | 839 | return catchClause;
|
---|
[d76c588] | 840 | }
|
---|
| 841 |
|
---|
[0bd3faf] | 842 | const ast::WaitForStmt * Resolver::previsit( const ast::WaitForStmt * stmt ) {
|
---|
[2773ab8] | 843 | visit_children = false;
|
---|
| 844 |
|
---|
| 845 | // Resolve all clauses first
|
---|
| 846 | for ( unsigned i = 0; i < stmt->clauses.size(); ++i ) {
|
---|
[f6e6a55] | 847 | const ast::WaitForClause & clause = *stmt->clauses[i];
|
---|
[2773ab8] | 848 |
|
---|
| 849 | ast::TypeEnvironment env;
|
---|
[3bc69f2] | 850 | CandidateFinder funcFinder( context, env );
|
---|
[2773ab8] | 851 |
|
---|
| 852 | // Find all candidates for a function in canonical form
|
---|
[4a89b52] | 853 | funcFinder.find( clause.target, ResolveMode::withAdjustment() );
|
---|
[2773ab8] | 854 |
|
---|
| 855 | if ( funcFinder.candidates.empty() ) {
|
---|
| 856 | stringstream ss;
|
---|
| 857 | ss << "Use of undeclared indentifier '";
|
---|
[c86b08d] | 858 | ss << clause.target.strict_as< ast::NameExpr >()->name;
|
---|
[2773ab8] | 859 | ss << "' in call to waitfor";
|
---|
| 860 | SemanticError( stmt->location, ss.str() );
|
---|
| 861 | }
|
---|
| 862 |
|
---|
[f6e6a55] | 863 | if ( clause.target_args.empty() ) {
|
---|
[ef5b828] | 864 | SemanticError( stmt->location,
|
---|
[2773ab8] | 865 | "Waitfor clause must have at least one mutex parameter");
|
---|
| 866 | }
|
---|
| 867 |
|
---|
| 868 | // Find all alternatives for all arguments in canonical form
|
---|
[ef5b828] | 869 | std::vector< CandidateFinder > argFinders =
|
---|
[f6e6a55] | 870 | funcFinder.findSubExprs( clause.target_args );
|
---|
[ef5b828] | 871 |
|
---|
[2773ab8] | 872 | // List all combinations of arguments
|
---|
| 873 | std::vector< CandidateList > possibilities;
|
---|
| 874 | combos( argFinders.begin(), argFinders.end(), back_inserter( possibilities ) );
|
---|
| 875 |
|
---|
| 876 | // For every possible function:
|
---|
[ef5b828] | 877 | // * try matching the arguments to the parameters, not the other way around because
|
---|
[2773ab8] | 878 | // more arguments than parameters
|
---|
| 879 | CandidateList funcCandidates;
|
---|
| 880 | std::vector< CandidateList > argsCandidates;
|
---|
| 881 | SemanticErrorException errors;
|
---|
| 882 | for ( CandidateRef & func : funcFinder.candidates ) {
|
---|
| 883 | try {
|
---|
[ef5b828] | 884 | auto pointerType = dynamic_cast< const ast::PointerType * >(
|
---|
[2773ab8] | 885 | func->expr->result->stripReferences() );
|
---|
| 886 | if ( ! pointerType ) {
|
---|
[ef5b828] | 887 | SemanticError( stmt->location, func->expr->result.get(),
|
---|
[2773ab8] | 888 | "candidate not viable: not a pointer type\n" );
|
---|
| 889 | }
|
---|
| 890 |
|
---|
| 891 | auto funcType = pointerType->base.as< ast::FunctionType >();
|
---|
| 892 | if ( ! funcType ) {
|
---|
[ef5b828] | 893 | SemanticError( stmt->location, func->expr->result.get(),
|
---|
[2773ab8] | 894 | "candidate not viable: not a function type\n" );
|
---|
| 895 | }
|
---|
| 896 |
|
---|
| 897 | {
|
---|
| 898 | auto param = funcType->params.begin();
|
---|
| 899 | auto paramEnd = funcType->params.end();
|
---|
| 900 |
|
---|
| 901 | if( ! nextMutex( param, paramEnd ) ) {
|
---|
[ef5b828] | 902 | SemanticError( stmt->location, funcType,
|
---|
[2773ab8] | 903 | "candidate function not viable: no mutex parameters\n");
|
---|
| 904 | }
|
---|
| 905 | }
|
---|
| 906 |
|
---|
| 907 | CandidateRef func2{ new Candidate{ *func } };
|
---|
| 908 | // strip reference from function
|
---|
| 909 | func2->expr = referenceToRvalueConversion( func->expr, func2->cost );
|
---|
| 910 |
|
---|
| 911 | // Each argument must be matched with a parameter of the current candidate
|
---|
| 912 | for ( auto & argsList : possibilities ) {
|
---|
| 913 | try {
|
---|
| 914 | // Declare data structures needed for resolution
|
---|
| 915 | ast::OpenVarSet open;
|
---|
| 916 | ast::AssertionSet need, have;
|
---|
| 917 | ast::TypeEnvironment resultEnv{ func->env };
|
---|
[ef5b828] | 918 | // Add all type variables as open so that those not used in the
|
---|
[2773ab8] | 919 | // parameter list are still considered open
|
---|
| 920 | resultEnv.add( funcType->forall );
|
---|
| 921 |
|
---|
| 922 | // load type variables from arguments into one shared space
|
---|
| 923 | for ( auto & arg : argsList ) {
|
---|
| 924 | resultEnv.simpleCombine( arg->env );
|
---|
| 925 | }
|
---|
| 926 |
|
---|
| 927 | // Make sure we don't widen any existing bindings
|
---|
| 928 | resultEnv.forbidWidening();
|
---|
| 929 |
|
---|
| 930 | // Find any unbound type variables
|
---|
| 931 | resultEnv.extractOpenVars( open );
|
---|
| 932 |
|
---|
| 933 | auto param = funcType->params.begin();
|
---|
| 934 | auto paramEnd = funcType->params.end();
|
---|
| 935 |
|
---|
| 936 | unsigned n_mutex_param = 0;
|
---|
| 937 |
|
---|
[ef5b828] | 938 | // For every argument of its set, check if it matches one of the
|
---|
[2773ab8] | 939 | // parameters. The order is important
|
---|
| 940 | for ( auto & arg : argsList ) {
|
---|
| 941 | // Ignore non-mutex arguments
|
---|
| 942 | if ( ! nextMutex( param, paramEnd ) ) {
|
---|
| 943 | // We ran out of parameters but still have arguments.
|
---|
| 944 | // This function doesn't match
|
---|
[ef5b828] | 945 | SemanticError( stmt->location, funcType,
|
---|
[2773ab8] | 946 | toString("candidate function not viable: too many mutex "
|
---|
| 947 | "arguments, expected ", n_mutex_param, "\n" ) );
|
---|
| 948 | }
|
---|
| 949 |
|
---|
| 950 | ++n_mutex_param;
|
---|
| 951 |
|
---|
[4c2fe47] | 952 | // Check if the argument matches the parameter type in the current scope.
|
---|
[954c954] | 953 | // ast::ptr< ast::Type > paramType = (*param)->get_type();
|
---|
[4c2fe47] | 954 |
|
---|
[ef5b828] | 955 | if (
|
---|
| 956 | ! unify(
|
---|
[251ce80] | 957 | arg->expr->result, *param, resultEnv, need, have, open )
|
---|
[2773ab8] | 958 | ) {
|
---|
| 959 | // Type doesn't match
|
---|
| 960 | stringstream ss;
|
---|
| 961 | ss << "candidate function not viable: no known conversion "
|
---|
| 962 | "from '";
|
---|
[954c954] | 963 | ast::print( ss, *param );
|
---|
[2773ab8] | 964 | ss << "' to '";
|
---|
| 965 | ast::print( ss, arg->expr->result );
|
---|
| 966 | ss << "' with env '";
|
---|
| 967 | ast::print( ss, resultEnv );
|
---|
| 968 | ss << "'\n";
|
---|
| 969 | SemanticError( stmt->location, funcType, ss.str() );
|
---|
| 970 | }
|
---|
| 971 |
|
---|
| 972 | ++param;
|
---|
| 973 | }
|
---|
| 974 |
|
---|
| 975 | // All arguments match!
|
---|
| 976 |
|
---|
| 977 | // Check if parameters are missing
|
---|
| 978 | if ( nextMutex( param, paramEnd ) ) {
|
---|
| 979 | do {
|
---|
| 980 | ++n_mutex_param;
|
---|
| 981 | ++param;
|
---|
| 982 | } while ( nextMutex( param, paramEnd ) );
|
---|
| 983 |
|
---|
[ef5b828] | 984 | // We ran out of arguments but still have parameters left; this
|
---|
[2773ab8] | 985 | // function doesn't match
|
---|
[ef5b828] | 986 | SemanticError( stmt->location, funcType,
|
---|
[2773ab8] | 987 | toString( "candidate function not viable: too few mutex "
|
---|
| 988 | "arguments, expected ", n_mutex_param, "\n" ) );
|
---|
| 989 | }
|
---|
| 990 |
|
---|
| 991 | // All parameters match!
|
---|
| 992 |
|
---|
| 993 | // Finish the expressions to tie in proper environments
|
---|
| 994 | finishExpr( func2->expr, resultEnv );
|
---|
| 995 | for ( CandidateRef & arg : argsList ) {
|
---|
| 996 | finishExpr( arg->expr, resultEnv );
|
---|
| 997 | }
|
---|
| 998 |
|
---|
| 999 | // This is a match, store it and save it for later
|
---|
| 1000 | funcCandidates.emplace_back( std::move( func2 ) );
|
---|
| 1001 | argsCandidates.emplace_back( std::move( argsList ) );
|
---|
| 1002 |
|
---|
| 1003 | } catch ( SemanticErrorException & e ) {
|
---|
| 1004 | errors.append( e );
|
---|
| 1005 | }
|
---|
| 1006 | }
|
---|
| 1007 | } catch ( SemanticErrorException & e ) {
|
---|
| 1008 | errors.append( e );
|
---|
| 1009 | }
|
---|
| 1010 | }
|
---|
| 1011 |
|
---|
| 1012 | // Make sure correct number of arguments
|
---|
| 1013 | if( funcCandidates.empty() ) {
|
---|
[ef5b828] | 1014 | SemanticErrorException top( stmt->location,
|
---|
[2773ab8] | 1015 | "No alternatives for function in call to waitfor" );
|
---|
| 1016 | top.append( errors );
|
---|
| 1017 | throw top;
|
---|
| 1018 | }
|
---|
| 1019 |
|
---|
| 1020 | if( argsCandidates.empty() ) {
|
---|
[ef5b828] | 1021 | SemanticErrorException top( stmt->location,
|
---|
| 1022 | "No alternatives for arguments in call to waitfor" );
|
---|
[2773ab8] | 1023 | top.append( errors );
|
---|
| 1024 | throw top;
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
| 1027 | if( funcCandidates.size() > 1 ) {
|
---|
[ef5b828] | 1028 | SemanticErrorException top( stmt->location,
|
---|
[2773ab8] | 1029 | "Ambiguous function in call to waitfor" );
|
---|
| 1030 | top.append( errors );
|
---|
| 1031 | throw top;
|
---|
| 1032 | }
|
---|
| 1033 | if( argsCandidates.size() > 1 ) {
|
---|
| 1034 | SemanticErrorException top( stmt->location,
|
---|
| 1035 | "Ambiguous arguments in call to waitfor" );
|
---|
| 1036 | top.append( errors );
|
---|
| 1037 | throw top;
|
---|
| 1038 | }
|
---|
| 1039 | // TODO: need to use findDeletedExpr to ensure no deleted identifiers are used.
|
---|
| 1040 |
|
---|
| 1041 | // build new clause
|
---|
[f6e6a55] | 1042 | auto clause2 = new ast::WaitForClause( clause.location );
|
---|
[ef5b828] | 1043 |
|
---|
[c86b08d] | 1044 | clause2->target = funcCandidates.front()->expr;
|
---|
[ef5b828] | 1045 |
|
---|
[f6e6a55] | 1046 | clause2->target_args.reserve( clause.target_args.size() );
|
---|
[6668a3e] | 1047 | const ast::StructDecl * decl_monitor = symtab.lookupStruct( "monitor$" );
|
---|
[2773ab8] | 1048 | for ( auto arg : argsCandidates.front() ) {
|
---|
[6668a3e] | 1049 | const auto & loc = stmt->location;
|
---|
| 1050 |
|
---|
| 1051 | ast::Expr * init = new ast::CastExpr( loc,
|
---|
| 1052 | new ast::UntypedExpr( loc,
|
---|
| 1053 | new ast::NameExpr( loc, "get_monitor" ),
|
---|
| 1054 | { arg->expr }
|
---|
| 1055 | ),
|
---|
| 1056 | new ast::PointerType(
|
---|
| 1057 | new ast::StructInstType(
|
---|
| 1058 | decl_monitor
|
---|
| 1059 | )
|
---|
| 1060 | )
|
---|
| 1061 | );
|
---|
| 1062 |
|
---|
[f6e6a55] | 1063 | clause2->target_args.emplace_back( findSingleExpression( init, context ) );
|
---|
[2773ab8] | 1064 | }
|
---|
| 1065 |
|
---|
| 1066 | // Resolve the conditions as if it were an IfStmt, statements normally
|
---|
[c86b08d] | 1067 | clause2->when_cond = findSingleExpression( clause.when_cond, context );
|
---|
[f6e6a55] | 1068 | clause2->stmt = clause.stmt->accept( *visitor );
|
---|
[2773ab8] | 1069 |
|
---|
| 1070 | // set results into stmt
|
---|
| 1071 | auto n = mutate( stmt );
|
---|
[f6e6a55] | 1072 | n->clauses[i] = clause2;
|
---|
[2773ab8] | 1073 | stmt = n;
|
---|
| 1074 | }
|
---|
| 1075 |
|
---|
[f6e6a55] | 1076 | if ( stmt->timeout_stmt ) {
|
---|
[2773ab8] | 1077 | // resolve the timeout as a size_t, the conditions like IfStmt, and stmts normally
|
---|
[ef5b828] | 1078 | ast::ptr< ast::Type > target =
|
---|
[2773ab8] | 1079 | new ast::BasicType{ ast::BasicType::LongLongUnsignedInt };
|
---|
[f6e6a55] | 1080 | auto timeout_time = findSingleExpression( stmt->timeout_time, target, context );
|
---|
| 1081 | auto timeout_cond = findSingleExpression( stmt->timeout_cond, context );
|
---|
| 1082 | auto timeout_stmt = stmt->timeout_stmt->accept( *visitor );
|
---|
[2773ab8] | 1083 |
|
---|
| 1084 | // set results into stmt
|
---|
| 1085 | auto n = mutate( stmt );
|
---|
[f6e6a55] | 1086 | n->timeout_time = std::move( timeout_time );
|
---|
| 1087 | n->timeout_cond = std::move( timeout_cond );
|
---|
| 1088 | n->timeout_stmt = std::move( timeout_stmt );
|
---|
[2773ab8] | 1089 | stmt = n;
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
[f6e6a55] | 1092 | if ( stmt->else_stmt ) {
|
---|
[2773ab8] | 1093 | // resolve the condition like IfStmt, stmts normally
|
---|
[f6e6a55] | 1094 | auto else_cond = findSingleExpression( stmt->else_cond, context );
|
---|
| 1095 | auto else_stmt = stmt->else_stmt->accept( *visitor );
|
---|
[2773ab8] | 1096 |
|
---|
| 1097 | // set results into stmt
|
---|
| 1098 | auto n = mutate( stmt );
|
---|
[f6e6a55] | 1099 | n->else_cond = std::move( else_cond );
|
---|
| 1100 | n->else_stmt = std::move( else_stmt );
|
---|
[2773ab8] | 1101 | stmt = n;
|
---|
| 1102 | }
|
---|
| 1103 |
|
---|
| 1104 | return stmt;
|
---|
[d76c588] | 1105 | }
|
---|
| 1106 |
|
---|
[0bd3faf] | 1107 | const ast::WithStmt * Resolver::previsit( const ast::WithStmt * withStmt ) {
|
---|
[16ba4a6f] | 1108 | auto mutStmt = mutate(withStmt);
|
---|
| 1109 | resolveWithExprs(mutStmt->exprs, stmtsToAddBefore);
|
---|
| 1110 | return mutStmt;
|
---|
| 1111 | }
|
---|
| 1112 |
|
---|
[0bd3faf] | 1113 | void Resolver::resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd) {
|
---|
[16ba4a6f] | 1114 | for (auto & expr : exprs) {
|
---|
| 1115 | // only struct- and union-typed expressions are viable candidates
|
---|
[3bc69f2] | 1116 | expr = findKindExpression( expr, context, structOrUnion, "with expression" );
|
---|
[16ba4a6f] | 1117 |
|
---|
| 1118 | // if with expression might be impure, create a temporary so that it is evaluated once
|
---|
| 1119 | if ( Tuples::maybeImpure( expr ) ) {
|
---|
| 1120 | static UniqueName tmpNamer( "_with_tmp_" );
|
---|
| 1121 | const CodeLocation loc = expr->location;
|
---|
| 1122 | auto tmp = new ast::ObjectDecl(loc, tmpNamer.newName(), expr->result, new ast::SingleInit(loc, expr ) );
|
---|
| 1123 | expr = new ast::VariableExpr( loc, tmp );
|
---|
| 1124 | stmtsToAdd.push_back( new ast::DeclStmt(loc, tmp ) );
|
---|
| 1125 | if ( InitTweak::isConstructable( tmp->type ) ) {
|
---|
| 1126 | // generate ctor/dtor and resolve them
|
---|
| 1127 | tmp->init = InitTweak::genCtorInit( loc, tmp );
|
---|
| 1128 | }
|
---|
| 1129 | // since tmp is freshly created, this should modify tmp in-place
|
---|
| 1130 | tmp->accept( *visitor );
|
---|
| 1131 | }
|
---|
[9e23b446] | 1132 | else if (expr->env && expr->env->empty()) {
|
---|
| 1133 | expr = ast::mutate_field(expr.get(), &ast::Expr::env, nullptr);
|
---|
| 1134 | }
|
---|
[16ba4a6f] | 1135 | }
|
---|
| 1136 | }
|
---|
[60aaa51d] | 1137 |
|
---|
| 1138 |
|
---|
[0bd3faf] | 1139 | const ast::SingleInit * Resolver::previsit( const ast::SingleInit * singleInit ) {
|
---|
[60aaa51d] | 1140 | visit_children = false;
|
---|
[ef5b828] | 1141 | // resolve initialization using the possibilities as determined by the `currentObject`
|
---|
[60aaa51d] | 1142 | // cursor.
|
---|
[ef5b828] | 1143 | ast::ptr< ast::Expr > untyped = new ast::UntypedInitExpr{
|
---|
[60aaa51d] | 1144 | singleInit->location, singleInit->value, currentObject.getOptions() };
|
---|
[3bc69f2] | 1145 | ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, context );
|
---|
[60aaa51d] | 1146 | const ast::InitExpr * initExpr = newExpr.strict_as< ast::InitExpr >();
|
---|
| 1147 |
|
---|
| 1148 | // move cursor to the object that is actually initialized
|
---|
| 1149 | currentObject.setNext( initExpr->designation );
|
---|
| 1150 |
|
---|
| 1151 | // discard InitExpr wrapper and retain relevant pieces.
|
---|
[ef5b828] | 1152 | // `initExpr` may have inferred params in the case where the expression specialized a
|
---|
| 1153 | // function pointer, and newExpr may already have inferParams of its own, so a simple
|
---|
[60aaa51d] | 1154 | // swap is not sufficient
|
---|
| 1155 | ast::Expr::InferUnion inferred = initExpr->inferred;
|
---|
| 1156 | swap_and_save_env( newExpr, initExpr->expr );
|
---|
| 1157 | newExpr.get_and_mutate()->inferred.splice( std::move(inferred) );
|
---|
| 1158 |
|
---|
[ef5b828] | 1159 | // get the actual object's type (may not exactly match what comes back from the resolver
|
---|
[60aaa51d] | 1160 | // due to conversions)
|
---|
| 1161 | const ast::Type * initContext = currentObject.getCurrentType();
|
---|
| 1162 |
|
---|
[918e4165] | 1163 | removeExtraneousCast( newExpr );
|
---|
[60aaa51d] | 1164 |
|
---|
| 1165 | // check if actual object's type is char[]
|
---|
| 1166 | if ( auto at = dynamic_cast< const ast::ArrayType * >( initContext ) ) {
|
---|
| 1167 | if ( isCharType( at->base ) ) {
|
---|
| 1168 | // check if the resolved type is char*
|
---|
| 1169 | if ( auto pt = newExpr->result.as< ast::PointerType >() ) {
|
---|
| 1170 | if ( isCharType( pt->base ) ) {
|
---|
[ef5b828] | 1171 | // strip cast if we're initializing a char[] with a char*
|
---|
[60aaa51d] | 1172 | // e.g. char x[] = "hello"
|
---|
| 1173 | if ( auto ce = newExpr.as< ast::CastExpr >() ) {
|
---|
| 1174 | swap_and_save_env( newExpr, ce->arg );
|
---|
| 1175 | }
|
---|
| 1176 | }
|
---|
| 1177 | }
|
---|
| 1178 | }
|
---|
| 1179 | }
|
---|
| 1180 |
|
---|
| 1181 | // move cursor to next object in preparation for next initializer
|
---|
| 1182 | currentObject.increment();
|
---|
| 1183 |
|
---|
| 1184 | // set initializer expression to resolved expression
|
---|
| 1185 | return ast::mutate_field( singleInit, &ast::SingleInit::value, std::move(newExpr) );
|
---|
[d76c588] | 1186 | }
|
---|
| 1187 |
|
---|
[0bd3faf] | 1188 | const ast::ListInit * Resolver::previsit( const ast::ListInit * listInit ) {
|
---|
[60aaa51d] | 1189 | // move cursor into brace-enclosed initializer-list
|
---|
| 1190 | currentObject.enterListInit( listInit->location );
|
---|
| 1191 |
|
---|
| 1192 | assert( listInit->designations.size() == listInit->initializers.size() );
|
---|
| 1193 | for ( unsigned i = 0; i < listInit->designations.size(); ++i ) {
|
---|
[ef5b828] | 1194 | // iterate designations and initializers in pairs, moving the cursor to the current
|
---|
[60aaa51d] | 1195 | // designated object and resolving the initializer against that object
|
---|
[2d11663] | 1196 | listInit = ast::mutate_field_index(
|
---|
[ef5b828] | 1197 | listInit, &ast::ListInit::designations, i,
|
---|
[2d11663] | 1198 | currentObject.findNext( listInit->designations[i] ) );
|
---|
| 1199 | listInit = ast::mutate_field_index(
|
---|
| 1200 | listInit, &ast::ListInit::initializers, i,
|
---|
| 1201 | listInit->initializers[i]->accept( *visitor ) );
|
---|
[60aaa51d] | 1202 | }
|
---|
| 1203 |
|
---|
[2d11663] | 1204 | // move cursor out of brace-enclosed initializer-list
|
---|
| 1205 | currentObject.exitListInit();
|
---|
| 1206 |
|
---|
[60aaa51d] | 1207 | visit_children = false;
|
---|
| 1208 | return listInit;
|
---|
[d76c588] | 1209 | }
|
---|
| 1210 |
|
---|
[0bd3faf] | 1211 | const ast::ConstructorInit * Resolver::previsit( const ast::ConstructorInit * ctorInit ) {
|
---|
[2d11663] | 1212 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::ctor );
|
---|
| 1213 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::dtor );
|
---|
| 1214 |
|
---|
| 1215 | // found a constructor - can get rid of C-style initializer
|
---|
| 1216 | // xxx - Rob suggests this field is dead code
|
---|
| 1217 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::init, nullptr );
|
---|
| 1218 |
|
---|
[ef5b828] | 1219 | // intrinsic single-parameter constructors and destructors do nothing. Since this was
|
---|
| 1220 | // implicitly generated, there's no way for it to have side effects, so get rid of it to
|
---|
[2d11663] | 1221 | // clean up generated code
|
---|
| 1222 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) {
|
---|
| 1223 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::ctor, nullptr );
|
---|
| 1224 | }
|
---|
| 1225 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) {
|
---|
| 1226 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::dtor, nullptr );
|
---|
| 1227 | }
|
---|
| 1228 |
|
---|
| 1229 | return ctorInit;
|
---|
[d76c588] | 1230 | }
|
---|
| 1231 |
|
---|
[0dd9a5e] | 1232 | // suppress error on autogen functions and mark invalid autogen as deleted.
|
---|
[0bd3faf] | 1233 | bool Resolver::on_error(ast::ptr<ast::Decl> & decl) {
|
---|
[0dd9a5e] | 1234 | if (auto functionDecl = decl.as<ast::FunctionDecl>()) {
|
---|
| 1235 | // xxx - can intrinsic gen ever fail?
|
---|
[6668a3e] | 1236 | if (functionDecl->linkage == ast::Linkage::AutoGen) {
|
---|
[0dd9a5e] | 1237 | auto mutDecl = mutate(functionDecl);
|
---|
| 1238 | mutDecl->isDeleted = true;
|
---|
| 1239 | mutDecl->stmts = nullptr;
|
---|
| 1240 | decl = mutDecl;
|
---|
| 1241 | return false;
|
---|
| 1242 | }
|
---|
| 1243 | }
|
---|
| 1244 | return true;
|
---|
| 1245 | }
|
---|
| 1246 |
|
---|
[51b73452] | 1247 | } // namespace ResolvExpr
|
---|
[a32b204] | 1248 |
|
---|
| 1249 | // Local Variables: //
|
---|
| 1250 | // tab-width: 4 //
|
---|
| 1251 | // mode: c++ //
|
---|
| 1252 | // compile-command: "make install" //
|
---|
| 1253 | // End: //
|
---|