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