Forráskód Böngészése

JSSpecCompiler: Restrict usage of NodeSubtreePointer

This class stores a non-owning raw pointer to a member of `Node`, so
extra care is needed to ensure that referenced `Node`s will be alive
by the time `NodeSubtreePointer` is used. Since we only need to use this
class while traversing AST in `RecursiveASTVisitor`, access to class
methods can be restricted using `Badge<RecursiveASTVisitor>`.
Dan Klishch 1 éve
szülő
commit
1c4cd34320

+ 3 - 2
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/AST.h

@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include <AK/Badge.h>
 #include <AK/RefCounted.h>
 #include <AK/RefPtr.h>
 #include <AK/Vector.h>
@@ -27,9 +28,9 @@ public:
     {
     }
 
-    Tree& get() { return *m_tree_ptr; }
+    Tree& get(Badge<RecursiveASTVisitor>) { return *m_tree_ptr; }
 
-    void replace_subtree(Tree tree) { *m_tree_ptr = move(tree); }
+    void replace_subtree(Badge<RecursiveASTVisitor>, Tree tree) { *m_tree_ptr = move(tree); }
 
 private:
     Tree* m_tree_ptr;

+ 2 - 2
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.cpp

@@ -18,7 +18,7 @@ void RecursiveASTVisitor::run_in_subtree(Tree& tree)
 
 void RecursiveASTVisitor::replace_current_node_with(Tree tree)
 {
-    m_current_subtree_pointer->replace_subtree(move(tree));
+    m_current_subtree_pointer->replace_subtree({}, move(tree));
 }
 
 RecursionDecision RecursiveASTVisitor::recurse(Tree root, NodeSubtreePointer& pointer)
@@ -30,7 +30,7 @@ RecursionDecision RecursiveASTVisitor::recurse(Tree root, NodeSubtreePointer& po
 
     if (decision == RecursionDecision::Recurse) {
         for (auto& child : root->subtrees()) {
-            if (recurse(child.get(), child) == RecursionDecision::Break)
+            if (recurse(child.get({}), child) == RecursionDecision::Break)
                 return RecursionDecision::Break;
         }
     }

+ 3 - 0
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h

@@ -37,6 +37,9 @@ class Variable;
 class FunctionPointer;
 using FunctionPointerRef = NonnullRefPtr<FunctionPointer>;
 
+// Compiler/GenericASTPass.h
+class RecursiveASTVisitor;
+
 // Parser/SpecParser.h
 class AlgorithmStep;
 class AlgorithmStepList;