From 18a996ce453a714f77e1777de99aafe13d68c284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Bispo?= Date: Wed, 29 May 2024 18:20:43 +0100 Subject: [PATCH] [ClavaWeaver] ClavaJoinPoints.unaryOp("*", $expr) now supports qualified types --- ClavaAst/src/pt/up/fe/specs/clava/Types.java | 10 +- .../clava/test/api/ClavaJoinPointsTest.js | 179 ++++++++++-------- ...st.lara.txt => ClavaJoinPointsTest.js.txt} | 2 + .../test/api/cpp/src/clava_join_points.cpp | 8 +- .../fe/specs/cxxweaver/tests/CxxApiTest.java | 2 +- 5 files changed, 118 insertions(+), 83 deletions(-) rename ClavaWeaver/resources/clava/test/api/cpp/results/{ClavaJoinPointsTest.lara.txt => ClavaJoinPointsTest.js.txt} (94%) diff --git a/ClavaAst/src/pt/up/fe/specs/clava/Types.java b/ClavaAst/src/pt/up/fe/specs/clava/Types.java index 966e984d8..afb226a85 100644 --- a/ClavaAst/src/pt/up/fe/specs/clava/Types.java +++ b/ClavaAst/src/pt/up/fe/specs/clava/Types.java @@ -297,10 +297,12 @@ public static Type inferUnaryType(UnaryOperatorKind op, Type exprType, ClavaFact case AddrOf: return factory.pointerType(exprType); case Deref: - var desugared = exprType.desugarAll(); - SpecsCheck.checkArgument(desugared instanceof PointerType, - () -> "Expected type to be a pointer: " + desugared); - return ((PointerType) exprType).getPointeeType(); + var unqualifiedType = exprType.desugarAll().unqualifiedType(); + + SpecsCheck.checkArgument(unqualifiedType instanceof PointerType, + () -> "Expected type to be a pointer: " + unqualifiedType); + + return ((PointerType) unqualifiedType).getPointeeType(); default: throw new NotImplementedException("Unary op return type inference not implemented for '" + op + "'"); } diff --git a/ClavaWeaver/resources/clava/test/api/ClavaJoinPointsTest.js b/ClavaWeaver/resources/clava/test/api/ClavaJoinPointsTest.js index 96abfd396..af3be3b25 100644 --- a/ClavaWeaver/resources/clava/test/api/ClavaJoinPointsTest.js +++ b/ClavaWeaver/resources/clava/test/api/ClavaJoinPointsTest.js @@ -1,80 +1,105 @@ -import clava.ClavaJoinPoints; -import clava.ClavaNodes; // Test if alias loads correctly +laraImport("clava.ClavaJoinPoints"); +laraImport("clava.ClavaNodes"); // Test if alias loads correctly -aspectdef ClavaJoinPointsTest +var intType = ClavaJoinPoints.builtinType("int"); +var doubleType = ClavaJoinPoints.builtinType("double"); +var intExpr = ClavaJoinPoints.exprLiteral("a", intType); +var intPointerType = ClavaJoinPoints.pointer(intType); +var intPointerExpr = ClavaJoinPoints.exprLiteral("aPointer", intPointerType); - var intType = ClavaJoinPoints.builtinType("int"); - var doubleType = ClavaJoinPoints.builtinType("double"); - var intExpr = ClavaJoinPoints.exprLiteral("a", intType); - var intPointerType = ClavaJoinPoints.pointer(intType); - var intPointerExpr = ClavaJoinPoints.exprLiteral("aPointer", intPointerType); +// Type Literal of user defined type +println("User literal type: " + ClavaJoinPoints.typeLiteral("xpto").code); - // Type Literal of user defined type - println("User literal type: " + ClavaJoinPoints.typeLiteral("xpto").code); - - // Stmt Literal - println("Literal statement: " + ClavaJoinPoints.stmtLiteral("int a = 0;").code); - - // TypedefDecl - var typedefDecl = ClavaJoinPoints.typedefDecl(ClavaJoinPoints.builtinType("int"), "custom_int"); - println("Typedef decl: " + typedefDecl.code); - - // TypedefType - println("Typedef type: " + ClavaJoinPoints.typedefType(typedefDecl).code); - - // Cast - println("C-Style cast: " + ClavaJoinPoints.cStyleCast(doubleType, intExpr).code); - - // If Stmt - println("Empty if:\n" + ClavaJoinPoints.ifStmt("a == 0").code); - println("If with then:\n" + ClavaJoinPoints.ifStmt("a == 0", ClavaJoinPoints.stmtLiteral("a = 1;")).code); - println("If with else:\n" + ClavaJoinPoints.ifStmt("a == 0", undefined, ClavaJoinPoints.stmtLiteral("a = 2;")).code); - - // For Stmt - println("Empty for:\n" + ClavaJoinPoints.forStmt().code); - println("Complete for:\n" + ClavaJoinPoints.forStmt("int i=0;", "i<10;", "i++;", "i = i+1;\ni = i - 1;").code); - - // Unary Operator - var addressOfOp = ClavaJoinPoints.unaryOp("&", intExpr); - println("AddressOf code: " + addressOfOp.code); - println("AddressOf code type: " + addressOfOp.type.code); - - var derefOp = ClavaJoinPoints.unaryOp("*", intPointerExpr); - println("Deref code: " + derefOp.code); - println("Deref code type: " + derefOp.type.code); - - - var type; - type = ClavaJoinPoints.type("int"); - println("Builtin type: " + type.joinPointType); - type = ClavaJoinPoints.type("uint64_t"); - println("Literal type: " + type.node.getClass()); - var type2 = ClavaJoinPoints.type(type); - println("Same type: " + (type === type2)); // They should be the same join point - type2 = ClavaJoinPoints.type(ClavaJoinPoints.varDeclNoInit("dummyVar", type)); - println("Same type again: " + (type.code === type2.code)); // No longer the same join point, but the same node - // Invalid input, only strings or jps - try { - ClavaJoinPoints.type([0 , 1]); - println("fail 1"); - } catch(e) { - println("Rejected invalid input type"); - } - // Invalid input, jp must have type - try { - ClavaJoinPoints.type(ClavaJoinPoints.file("dummyFile")); - println("fail 2"); - } catch(e) { - println("Rejected jp without type"); - } - - // Varref - var $varref = ClavaJoinPoints.varRef("varref_test", type); - println("Varref code: " + $varref.code); - println("Varref type: " + $varref.type.code); - var $varref2 = ClavaJoinPoints.varRef(ClavaJoinPoints.varDeclNoInit("varref_test_2", type)); - println("Varref2 code: " + $varref2.code); - println("Varref2 type: " + $varref2.type.code); - - -end +// Stmt Literal +println("Literal statement: " + ClavaJoinPoints.stmtLiteral("int a = 0;").code); + +// TypedefDecl +var typedefDecl = ClavaJoinPoints.typedefDecl( + ClavaJoinPoints.builtinType("int"), + "custom_int" +); +println("Typedef decl: " + typedefDecl.code); + +// TypedefType +println("Typedef type: " + ClavaJoinPoints.typedefType(typedefDecl).code); + +// Cast +println( + "C-Style cast: " + ClavaJoinPoints.cStyleCast(doubleType, intExpr).code +); + +// If Stmt +println("Empty if:\n" + ClavaJoinPoints.ifStmt("a == 0").code); +println( + "If with then:\n" + + ClavaJoinPoints.ifStmt("a == 0", ClavaJoinPoints.stmtLiteral("a = 1;")).code +); +println( + "If with else:\n" + + ClavaJoinPoints.ifStmt( + "a == 0", + undefined, + ClavaJoinPoints.stmtLiteral("a = 2;") + ).code +); + +// For Stmt +println("Empty for:\n" + ClavaJoinPoints.forStmt().code); +println( + "Complete for:\n" + + ClavaJoinPoints.forStmt("int i=0;", "i<10;", "i++;", "i = i+1;\ni = i - 1;") + .code +); + +// Unary Operator +var addressOfOp = ClavaJoinPoints.unaryOp("&", intExpr); +println("AddressOf code: " + addressOfOp.code); +println("AddressOf code type: " + addressOfOp.type.code); + +var derefOp = ClavaJoinPoints.unaryOp("*", intPointerExpr); +println("Deref code: " + derefOp.code); +println("Deref code type: " + derefOp.type.code); + +const xConstPointerDecl = Query.search("function", "constPointer") + .search("vardecl", "x") + .first(); +const xConstPointerRef = ClavaJoinPoints.varRef(xConstPointerDecl); + +// Deref of qualified pointer +const derefQualified = ClavaJoinPoints.unaryOp("*", xConstPointerRef); +println("Deref code: " + derefQualified.code); +println("Deref code type: " + derefQualified.type.code); + +var type; +type = ClavaJoinPoints.type("int"); +println("Builtin type: " + type.joinPointType); +type = ClavaJoinPoints.type("uint64_t"); +println("Literal type: " + type.node.getClass()); +var type2 = ClavaJoinPoints.type(type); +println("Same type: " + (type === type2)); // They should be the same join point +type2 = ClavaJoinPoints.type(ClavaJoinPoints.varDeclNoInit("dummyVar", type)); +println("Same type again: " + (type.code === type2.code)); // No longer the same join point, but the same node +// Invalid input, only strings or jps +try { + ClavaJoinPoints.type([0, 1]); + println("fail 1"); +} catch (e) { + println("Rejected invalid input type"); +} +// Invalid input, jp must have type +try { + ClavaJoinPoints.type(ClavaJoinPoints.file("dummyFile")); + println("fail 2"); +} catch (e) { + println("Rejected jp without type"); +} + +// Varref +var $varref = ClavaJoinPoints.varRef("varref_test", type); +println("Varref code: " + $varref.code); +println("Varref type: " + $varref.type.code); +var $varref2 = ClavaJoinPoints.varRef( + ClavaJoinPoints.varDeclNoInit("varref_test_2", type) +); +println("Varref2 code: " + $varref2.code); +println("Varref2 type: " + $varref2.type.code); diff --git a/ClavaWeaver/resources/clava/test/api/cpp/results/ClavaJoinPointsTest.lara.txt b/ClavaWeaver/resources/clava/test/api/cpp/results/ClavaJoinPointsTest.js.txt similarity index 94% rename from ClavaWeaver/resources/clava/test/api/cpp/results/ClavaJoinPointsTest.lara.txt rename to ClavaWeaver/resources/clava/test/api/cpp/results/ClavaJoinPointsTest.js.txt index eb2e0ef51..0d4188295 100644 --- a/ClavaWeaver/resources/clava/test/api/cpp/results/ClavaJoinPointsTest.lara.txt +++ b/ClavaWeaver/resources/clava/test/api/cpp/results/ClavaJoinPointsTest.js.txt @@ -30,6 +30,8 @@ AddressOf code: &a AddressOf code type: int * Deref code: *aPointer Deref code type: int +Deref code: *x +Deref code type: int const Builtin type: builtinType Literal type: class pt.up.fe.specs.clava.ast.type.LiteralType Same type: true diff --git a/ClavaWeaver/resources/clava/test/api/cpp/src/clava_join_points.cpp b/ClavaWeaver/resources/clava/test/api/cpp/src/clava_join_points.cpp index 921ea6788..30d2d4422 100644 --- a/ClavaWeaver/resources/clava/test/api/cpp/src/clava_join_points.cpp +++ b/ClavaWeaver/resources/clava/test/api/cpp/src/clava_join_points.cpp @@ -3,4 +3,10 @@ int main() { return 0; -} \ No newline at end of file +} + +void constPointer() { + int a = 10; + int const * const x = &a; +} + diff --git a/ClavaWeaver/test/pt/up/fe/specs/cxxweaver/tests/CxxApiTest.java b/ClavaWeaver/test/pt/up/fe/specs/cxxweaver/tests/CxxApiTest.java index 8d5557e07..17eb1ce82 100644 --- a/ClavaWeaver/test/pt/up/fe/specs/cxxweaver/tests/CxxApiTest.java +++ b/ClavaWeaver/test/pt/up/fe/specs/cxxweaver/tests/CxxApiTest.java @@ -114,7 +114,7 @@ public void testClavaCode() { @Test public void testClavaJoinPointsTest() { - newTester().test("ClavaJoinPointsTest.lara", "clava_join_points.cpp"); + newTester().test("ClavaJoinPointsTest.js", "clava_join_points.cpp"); } @Test