From 6add6091077140e8fda5c809d2763c06b89af5d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Th=C3=A9venoux?= Date: Thu, 20 Jun 2024 14:48:51 +0200 Subject: [PATCH] Rework QualExpr xref equation Resolve QualExpr's prefix by calling xref_type_equation instead of a simple call to designated_type. This allows to correctly bind names references used in QualExpr's prefix. --- ada/ast.py | 33 ++++++------- .../name_resolution/qual_expr_prefix/test.adb | 18 +++++++ .../name_resolution/qual_expr_prefix/test.out | 47 +++++++++++++++++++ .../qual_expr_prefix/test.yaml | 2 + 4 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 testsuite/tests/name_resolution/qual_expr_prefix/test.adb create mode 100644 testsuite/tests/name_resolution/qual_expr_prefix/test.out create mode 100644 testsuite/tests/name_resolution/qual_expr_prefix/test.yaml diff --git a/ada/ast.py b/ada/ast.py index 437fd7c38..491e4a058 100644 --- a/ada/ast.py +++ b/ada/ast.py @@ -20977,27 +20977,22 @@ def general_xref_equation(root=(T.Name, No(T.Name))): @langkit_property(return_type=Equation) def xref_equation(): - typ = Var(Entity.prefix.designated_type_impl) - return ( - Entity.suffix.sub_equation - & Bind(Self.prefix.ref_var, typ) - & Bind(Self.suffix.expected_type_var, typ) + Entity.prefix.xref_type_equation + & Entity.suffix.sub_equation + & Bind(Self.suffix.expected_type_var, Self.prefix.ref_var) & Entity.suffix.matches_expected_type - & Bind( - Self.type_var, - If( - # A qualified expression that appears as a statement - # denotes a machine code insertion, in GNAT, it is parsed - # as a parameterless procedure call. In that case, - # Self.type_var shouldn't denote any type. Note that we are - # more flexible than Ada since we allow any type to be code - # statements whereas Ada restricts that to types defined in - # package `System.Machine_Code` (see :rmlink:`13.8`). - Entity.parent.is_a(T.CallStmt), - No(AdaNode.entity), - typ - ) + & If( + # A qualified expression that appears as a statement + # denotes a machine code insertion, in GNAT, it is parsed + # as a parameterless procedure call. In that case, + # Self.type_var shouldn't denote any type. Note that we are + # more flexible than Ada since we allow any type to be code + # statements whereas Ada restricts that to types defined in + # package `System.Machine_Code` (see :rmlink:`13.8`). + Entity.parent.is_a(T.CallStmt), + LogicTrue(), + Bind(Self.type_var, Self.prefix.ref_var) ) ) diff --git a/testsuite/tests/name_resolution/qual_expr_prefix/test.adb b/testsuite/tests/name_resolution/qual_expr_prefix/test.adb new file mode 100644 index 000000000..0ec6d131c --- /dev/null +++ b/testsuite/tests/name_resolution/qual_expr_prefix/test.adb @@ -0,0 +1,18 @@ +procedure P (N : Natural) is + type Root is tagged record + My_Val : Natural := 0; + end record; + + type Any_Root_Access is access Root'Class; + + Obj : Root; + + Param : Root'Class := Root'Class (Obj); + + X : Any_Root_Access; +begin + -- All the names in the expression below must be resolved + + X := new P.Root'Class'(Param); + pragma Test_Statement; +end; diff --git a/testsuite/tests/name_resolution/qual_expr_prefix/test.out b/testsuite/tests/name_resolution/qual_expr_prefix/test.out new file mode 100644 index 000000000..99675b150 --- /dev/null +++ b/testsuite/tests/name_resolution/qual_expr_prefix/test.out @@ -0,0 +1,47 @@ +Analyzing test.adb +################## + +Resolving xrefs for node +********************************************************* + +Expr: + references: + type: + expected type: None +Expr: + type: + expected type: +Expr: + references: + type: + expected type: None +Expr: + references: + type: None + expected type: None +Expr: + references: + type: None + expected type: None +Expr: + references: + type: None + expected type: None +Expr: + references: + type: None + expected type: None +Expr: + references: None + type: None + expected type: None +Expr: + type: + expected type: +Expr: + references: + type: + expected type: + + +Done. diff --git a/testsuite/tests/name_resolution/qual_expr_prefix/test.yaml b/testsuite/tests/name_resolution/qual_expr_prefix/test.yaml new file mode 100644 index 000000000..173e325ff --- /dev/null +++ b/testsuite/tests/name_resolution/qual_expr_prefix/test.yaml @@ -0,0 +1,2 @@ +driver: name-resolution +input_sources: [test.adb]