Performs static checks, including resolution of identifiers in file in the environment defined by module. Syntax must be resolved before it is evaluated. @param file file whose statements are to be resolved. Mutated by this method: Identifier nodes get bindings, resolver
(StarlarkFile file, Module module)
| 1365 | * to have consistent behavior for {@link Module#resolve}. |
| 1366 | */ |
| 1367 | public static void resolveFile(StarlarkFile file, Module module) { |
| 1368 | Resolver r = new Resolver(file.errors, module, file.getOptions(), file.docCommentsMap); |
| 1369 | ImmutableList<Statement> stmts = file.getStatements(); |
| 1370 | |
| 1371 | // Check that load statements are on top. |
| 1372 | if (r.options.requireLoadStatementsFirst()) { |
| 1373 | r.checkLoadAfterStatement(stmts); |
| 1374 | } |
| 1375 | |
| 1376 | ArrayList<Binding> frame = new ArrayList<>(); |
| 1377 | r.pushLocalBlock(file, frame, /*freevars=*/ null); |
| 1378 | |
| 1379 | // First pass: creating bindings for statements in this block. |
| 1380 | r.createBindingsForBlock(stmts); |
| 1381 | |
| 1382 | // Second pass: visit all references. |
| 1383 | r.visitAll(stmts); |
| 1384 | |
| 1385 | r.popLocalBlock(); |
| 1386 | |
| 1387 | // If the final statement is an expression, synthesize a return statement. |
| 1388 | int n = stmts.size(); |
| 1389 | if (n > 0 && stmts.get(n - 1) instanceof ExpressionStatement) { |
| 1390 | Expression expr = ((ExpressionStatement) stmts.get(n - 1)).getExpression(); |
| 1391 | stmts = |
| 1392 | ImmutableList.<Statement>builder() |
| 1393 | .addAll(stmts.subList(0, n - 1)) |
| 1394 | .add(ReturnStatement.make(expr)) |
| 1395 | .build(); |
| 1396 | } |
| 1397 | |
| 1398 | // Annotate with resolved information about the toplevel function. |
| 1399 | file.setResolvedFunction( |
| 1400 | new Function( |
| 1401 | "<toplevel>", |
| 1402 | file.getStartLocation(), |
| 1403 | /* params= */ ImmutableList.of(), |
| 1404 | /* functionType= */ NO_PARAMS_CALLABLE, |
| 1405 | /* body= */ stmts, |
| 1406 | /* hasVarargs= */ false, |
| 1407 | /* hasKwargs= */ false, |
| 1408 | /* numKeywordOnlyParams= */ 0, |
| 1409 | frame, |
| 1410 | /* freevars= */ ImmutableList.of(), |
| 1411 | r.globals)); |
| 1412 | } |
| 1413 | |
| 1414 | /** |
| 1415 | * Performs static checks, including resolution of identifiers in {@code expr} in the environment |