From 30de6adb0ae05851d7dc5f2b0c46d22097bf00fc Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Thu, 21 Dec 2023 17:44:03 +0100 Subject: [PATCH 1/4] an attempt at fixing #72 but we are losing some info in the containment relation now --- src/lang/cpp/internal/BindingsResolver.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lang/cpp/internal/BindingsResolver.java b/src/lang/cpp/internal/BindingsResolver.java index 102feca5..0306557a 100644 --- a/src/lang/cpp/internal/BindingsResolver.java +++ b/src/lang/cpp/internal/BindingsResolver.java @@ -135,6 +135,7 @@ public class BindingsResolver { public ISetWriter containment; private ISourceLocation translationUnit; + private ISourceLocation translationUnitRoot = URIUtil.rootLocation("cpp+translationUnit"); private void out(String msg) { // stdOut.println(spaces() + msg.replace("\n", "\n" + spaces())); @@ -180,17 +181,19 @@ private ISourceLocation ownedBinding(IBinding binding, String scheme, ISourceLoc if ("cpp+translationUnit".equals(ownerLocation.getScheme())) { location = URIUtil.correctLocation(scheme, "", binding.getName()); + containment.append(vf.tuple(translationUnit, location)); } else { location = URIUtil.changeScheme(URIUtil.getChildLocation(ownerLocation, binding.getName()), scheme); + containment.append(vf.tuple(ownerLocation, location)); } - containment.append(vf.tuple(ownerLocation, location)); + return location; } private ISourceLocation resolveOwner(IBinding binding, ISourceLocation origin) throws URISyntaxException { if (binding == null) { - return translationUnit; + return translationUnitRoot; } IBinding owner = binding.getOwner(); @@ -198,7 +201,7 @@ private ISourceLocation resolveOwner(IBinding binding, ISourceLocation origin) t return URIUtil.correctLocation("circular", "", UUID.randomUUID().toString()); } if (owner == null) { - return translationUnit; + return translationUnitRoot; } else { return resolveBinding(null, owner, origin); From 4f4763e50ae8fd9c93c842778c981b2a5a75bc69 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Thu, 21 Dec 2023 19:40:12 +0100 Subject: [PATCH 2/4] fixed containment relation --- src/lang/cpp/internal/BindingsResolver.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lang/cpp/internal/BindingsResolver.java b/src/lang/cpp/internal/BindingsResolver.java index 0306557a..def54940 100644 --- a/src/lang/cpp/internal/BindingsResolver.java +++ b/src/lang/cpp/internal/BindingsResolver.java @@ -181,13 +181,13 @@ private ISourceLocation ownedBinding(IBinding binding, String scheme, ISourceLoc if ("cpp+translationUnit".equals(ownerLocation.getScheme())) { location = URIUtil.correctLocation(scheme, "", binding.getName()); - containment.append(vf.tuple(translationUnit, location)); + ownerLocation = translationUnit; } else { location = URIUtil.changeScheme(URIUtil.getChildLocation(ownerLocation, binding.getName()), scheme); - containment.append(vf.tuple(ownerLocation, location)); } + containment.append(vf.tuple(ownerLocation, location)); return location; } @@ -628,7 +628,7 @@ private ISourceLocation resolveCFunction(CFunction binding, ISourceLocation orig ISourceLocation decl = URIUtil.changeScheme(URIUtil.getChildLocation(owner, binding.getName()), scheme); decl = URIUtil.changePath(decl, decl.getPath() + parameters.toString()); - containment.append(vf.tuple(owner, decl)); + containment.append(vf.tuple(owner.getScheme().equals("cpp+translationUnit") ? translationUnit : owner, decl)); return decl; } From 0d64babc4b9c824d1e8d05742f97473c634d7cd7 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Fri, 22 Dec 2023 10:41:51 +0100 Subject: [PATCH 3/4] factored in owner semantics for ICPPFunction and ICFunction in the bindings resolver, and that allowed us to implement the semantics of `static` in one place --- src/lang/cpp/internal/BindingsResolver.java | 30 +++++++++------------ src/test/fac.cpp | 2 +- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/lang/cpp/internal/BindingsResolver.java b/src/lang/cpp/internal/BindingsResolver.java index def54940..c9ff4da8 100644 --- a/src/lang/cpp/internal/BindingsResolver.java +++ b/src/lang/cpp/internal/BindingsResolver.java @@ -176,15 +176,20 @@ public BindingsResolver(IValueFactory vf, PrintWriter stdOut, PrintWriter stdErr } private ISourceLocation ownedBinding(IBinding binding, String scheme, ISourceLocation origin) throws URISyntaxException { + return ownedBinding(binding, scheme, "", origin, false); + } + + private ISourceLocation ownedBinding(IBinding binding, String scheme, String postfix, ISourceLocation origin, boolean isStatic) throws URISyntaxException { + String name = binding.getName() + postfix; ISourceLocation ownerLocation = resolveOwner(binding, origin); ISourceLocation location = null; - - if ("cpp+translationUnit".equals(ownerLocation.getScheme())) { - location = URIUtil.correctLocation(scheme, "", binding.getName()); + + if (!isStatic && "cpp+translationUnit".equals(ownerLocation.getScheme())) { + location = URIUtil.correctLocation(scheme, "", name); ownerLocation = translationUnit; } else { - location = URIUtil.changeScheme(URIUtil.getChildLocation(ownerLocation, binding.getName()), scheme); + location = URIUtil.changeScheme(URIUtil.getChildLocation(ownerLocation, name), scheme); } containment.append(vf.tuple(ownerLocation, location)); @@ -444,7 +449,7 @@ private ISourceLocation resolveIParameter(IParameter binding, ISourceLocation or } private ISourceLocation resolveCVariable(CVariable binding, ISourceLocation origin) throws URISyntaxException { - return ownedBinding(binding, "c+variable", origin); + return ownedBinding(binding, "c+variable", "", origin, binding.isStatic()); } private ISourceLocation resolveICPPVariable(ICPPVariable binding, ISourceLocation origin) throws URISyntaxException { @@ -479,7 +484,7 @@ else if (binding instanceof ICPPVariableTemplatePartialSpecialization) { scheme = "cpp+variable"; } - return ownedBinding(binding, scheme, origin); + return ownedBinding(binding, scheme, "", origin, binding.isStatic()); } private ISourceLocation resolveICPPUsingDeclaration(ICPPUsingDeclaration binding, ISourceLocation origin) throws URISyntaxException { @@ -624,12 +629,7 @@ private ISourceLocation resolveCFunction(CFunction binding, ISourceLocation orig parameters = new StringBuilder("($$internalError)"); } - ISourceLocation owner = resolveOwner(binding, origin); - ISourceLocation decl = URIUtil.changeScheme(URIUtil.getChildLocation(owner, binding.getName()), scheme); - decl = URIUtil.changePath(decl, decl.getPath() + parameters.toString()); - - containment.append(vf.tuple(owner.getScheme().equals("cpp+translationUnit") ? translationUnit : owner, decl)); - return decl; + return ownedBinding(binding, scheme, parameters.toString(), origin, binding.isStatic()); } private ISourceLocation resolveICPPFunction(ICPPFunction binding, ISourceLocation origin) throws URISyntaxException { @@ -675,11 +675,7 @@ else if (binding.getName().startsWith("~")) { } parameters.append(')'); - ISourceLocation parentDecl = resolveOwner(binding, origin); - ISourceLocation decl = URIUtil.changeScheme(URIUtil.getChildLocation(parentDecl, binding.getName()), scheme); - decl = URIUtil.changePath(decl, decl.getPath() + parameters.toString()); - containment.append(vf.tuple(parentDecl, decl)); - return decl; + return ownedBinding(binding, scheme, parameters.toString(), origin, binding.isStatic()); } private ISourceLocation resolveCEnumeration(CEnumeration binding, ISourceLocation origin) throws URISyntaxException { diff --git a/src/test/fac.cpp b/src/test/fac.cpp index c062f4d6..e92f29c3 100644 --- a/src/test/fac.cpp +++ b/src/test/fac.cpp @@ -1,5 +1,5 @@ -int fac(int n) { +static int fac(int n) { if (n <= 0) { return 1; } From 1bab11991ba4e74c9f27a42e6224a981998d806e Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Fri, 22 Dec 2023 10:59:17 +0100 Subject: [PATCH 4/4] refactored ownedBinding to implement semantics of static for all occurrences --- src/lang/cpp/internal/BindingsResolver.java | 34 +++++++++++++++++---- src/lang/cpp/internal/Parser.java | 2 +- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/lang/cpp/internal/BindingsResolver.java b/src/lang/cpp/internal/BindingsResolver.java index c9ff4da8..e6ee7175 100644 --- a/src/lang/cpp/internal/BindingsResolver.java +++ b/src/lang/cpp/internal/BindingsResolver.java @@ -183,13 +183,34 @@ private ISourceLocation ownedBinding(IBinding binding, String scheme, String pos String name = binding.getName() + postfix; ISourceLocation ownerLocation = resolveOwner(binding, origin); ISourceLocation location = null; - - if (!isStatic && "cpp+translationUnit".equals(ownerLocation.getScheme())) { - location = URIUtil.correctLocation(scheme, "", name); - ownerLocation = translationUnit; + boolean isAtRoot = "cpp+translationUnit".equals(ownerLocation.getScheme()); + + // * When we are at the root, we want a different parent from |cpp+translationUnit:///| as containment parent; name it should contain the file name from `translationUnit` + // * Also when we are the root, `static` variables and functions need to be prefixed with the file name because they are local to the current translationUnit + + if (isStatic) { + if (isAtRoot) { + // add prefix + location = URIUtil.changeScheme(URIUtil.getChildLocation(translationUnit, name), scheme); + // set long containment parent + ownerLocation = translationUnit; + } + else { + // the owner is the prefix; because we are inside some nested declaration + location = URIUtil.changeScheme(URIUtil.getChildLocation(ownerLocation, name), scheme); + } } else { - location = URIUtil.changeScheme(URIUtil.getChildLocation(ownerLocation, name), scheme); + if (isAtRoot) { + // do not add the prefix, because of global C[++] namespace while linking + location = URIUtil.correctLocation(scheme, "", name); + // set long containment parent + ownerLocation = translationUnit; + } + else { + // the owner is the prefix; because we are inside some nested declaration + location = URIUtil.changeScheme(URIUtil.getChildLocation(ownerLocation, name), scheme); + } } containment.append(vf.tuple(ownerLocation, location)); @@ -618,8 +639,9 @@ private ISourceLocation resolveCFunction(CFunction binding, ISourceLocation orig StringBuilder parameters = new StringBuilder("("); try { for (IParameter parameter : binding.getParameters()) {// getParameters can throw ClassCastException - if (parameters.length() > 1) + if (parameters.length() > 1) { parameters.append(','); + } parameters.append(printType(parameter.getType())); } parameters.append(')'); diff --git a/src/lang/cpp/internal/Parser.java b/src/lang/cpp/internal/Parser.java index 62026f07..d200160f 100644 --- a/src/lang/cpp/internal/Parser.java +++ b/src/lang/cpp/internal/Parser.java @@ -2073,7 +2073,7 @@ private int visit(IASTElaboratedTypeSpecifier declSpec) { private int visit(IASTEnumerationSpecifier declSpec) { at(declSpec); - if (declSpec instanceof ICPPASTEnumerationSpecifier) + if (declSpec instanceof ICPPASTEnumerationSpecifier) visit((ICPPASTEnumerationSpecifier) declSpec); else if (declSpec instanceof ICASTEnumerationSpecifier) visit((ICASTEnumerationSpecifier) declSpec);