@@ -17,7 +17,7 @@ import (
1717)
1818
1919type CompileResult struct {
20- Stage string
20+ Stage ast. StageID
2121 Result * ast.Module
2222 Error string
2323}
@@ -32,52 +32,13 @@ func (cr *CompileResult) FormattedResult() string {
3232 return string (formatted )
3333}
3434
35- type stage struct { name , metricName string }
36-
37- // NOTE(sr): copied from 0.68.0. When upgrading OPA to v1.14.0, let's use its v1/ast.AllStages().
38- var stages = []stage {
39- {"ResolveRefs" , "compile_stage_resolve_refs" },
40- {"InitLocalVarGen" , "compile_stage_init_local_var_gen" },
41- {"RewriteRuleHeadRefs" , "compile_stage_rewrite_rule_head_refs" },
42- {"CheckKeywordOverrides" , "compile_stage_check_keyword_overrides" },
43- {"CheckDuplicateImports" , "compile_stage_check_duplicate_imports" },
44- {"RemoveImports" , "compile_stage_remove_imports" },
45- {"SetModuleTree" , "compile_stage_set_module_tree" },
46- {"SetRuleTree" , "compile_stage_set_rule_tree" },
47- {"RewriteLocalVars" , "compile_stage_rewrite_local_vars" },
48- {"RewriteTemplateStrings" , "compile_stage_rewrite_template_strings" },
49- {"CheckVoidCalls" , "compile_stage_check_void_calls" },
50- {"RewritePrintCalls" , "compile_stage_rewrite_print_calls" },
51- {"RewriteExprTerms" , "compile_stage_rewrite_expr_terms" },
52- {"ParseMetadataBlocks" , "compile_stage_parse_metadata_blocks" },
53- {"SetAnnotationSet" , "compile_stage_set_annotationset" },
54- {"RewriteRegoMetadataCalls" , "compile_stage_rewrite_rego_metadata_calls" },
55- {"SetGraph" , "compile_stage_set_graph" },
56- {"RewriteComprehensionTerms" , "compile_stage_rewrite_comprehension_terms" },
57- {"RewriteRefsInHead" , "compile_stage_rewrite_refs_in_head" },
58- {"RewriteWithValues" , "compile_stage_rewrite_with_values" },
59- {"CheckRuleConflicts" , "compile_stage_check_rule_conflicts" },
60- {"CheckUndefinedFuncs" , "compile_stage_check_undefined_funcs" },
61- {"CheckSafetyRuleHeads" , "compile_stage_check_safety_rule_heads" },
62- {"CheckSafetyRuleBodies" , "compile_stage_check_safety_rule_bodies" },
63- {"RewriteEquals" , "compile_stage_rewrite_equals" },
64- {"RewriteDynamicTerms" , "compile_stage_rewrite_dynamic_terms" },
65- {"RewriteTestRulesForTracing" , "compile_stage_rewrite_test_rules_for_tracing" }, // must run after RewriteDynamicTerms
66- {"CheckRecursion" , "compile_stage_check_recursion" },
67- {"CheckTypes" , "compile_stage_check_types" },
68- {"CheckUnsafeBuiltins" , "compile_state_check_unsafe_builtins" },
69- {"CheckDeprecatedBuiltins" , "compile_state_check_deprecated_builtins" },
70- {"BuildRuleIndices" , "compile_stage_rebuild_indices" },
71- {"BuildComprehensionIndices" , "compile_stage_rebuild_comprehension_indices" },
72- {"BuildRequiredCapabilities" , "compile_stage_build_required_capabilities" },
73- }
74-
7535func CompilerStages (path , rego string , useStrict , useAnno , usePrint bool ) []CompileResult {
7636 c := regal_compile .NewCompilerWithRegalBuiltins ().
7737 WithStrict (useStrict ).
7838 WithEnablePrintStatements (usePrint ).
7939 WithUseTypeCheckAnnotations (useAnno )
8040
41+ stages := ast .AllStages ()
8142 result := append (make ([]CompileResult , 0 , len (stages )+ 1 ), CompileResult {Stage : "ParseModule" })
8243 opts := parse .ParserOptions ()
8344 opts .ProcessAnnotation = useAnno
@@ -93,11 +54,11 @@ func CompilerStages(path, rego string, useStrict, useAnno, usePrint bool) []Comp
9354
9455 for i := range stages {
9556 stage := stages [i ]
96- c = c .WithStageAfter (stage . name , ast.CompilerStageDefinition {
97- Name : stage . name + "Record" ,
98- MetricName : stage . metricName + "_record" ,
57+ c = c .WithStageAfterID (stage , ast.CompilerStageDefinition {
58+ Name : string ( stage ) + "Record" ,
59+ MetricName : string ( stage ) + "_record" ,
9960 Stage : func (c0 * ast.Compiler ) * ast.Error {
100- result = append (result , CompileResult {Stage : stage . name , Result : getOne (c0 .Modules )})
61+ result = append (result , CompileResult {Stage : stage , Result : getOne (c0 .Modules )})
10162
10263 return nil
10364 },
@@ -107,7 +68,7 @@ func CompilerStages(path, rego string, useStrict, useAnno, usePrint bool) []Comp
10768 if c .Compile (map [string ]* ast.Module {path : mod }); len (c .Errors ) > 0 {
10869 // stage after the last than ran successfully
10970 stage := stages [len (result )- 1 ]
110- result = append (result , CompileResult {Stage : stage . name + ": Failure" , Error : c .Errors .Error ()})
71+ result = append (result , CompileResult {Stage : stage + ": Failure" , Error : c .Errors .Error ()})
11172 }
11273
11374 return result
0 commit comments