Skip to content

Commit

Permalink
Merge pull request #73 from usethesource/c-has-a-global-namespace
Browse files Browse the repository at this point in the history
Fixes #72
  • Loading branch information
jurgenvinju authored Dec 22, 2023
2 parents 9e48227 + 1bab119 commit e340f48
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
59 changes: 40 additions & 19 deletions src/lang/cpp/internal/BindingsResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -175,30 +176,58 @@ 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;
boolean isAtRoot = "cpp+translationUnit".equals(ownerLocation.getScheme());

if ("cpp+translationUnit".equals(ownerLocation.getScheme())) {
location = URIUtil.correctLocation(scheme, "", binding.getName());
// * 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, binding.getName()), 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));
return location;
}

private ISourceLocation resolveOwner(IBinding binding, ISourceLocation origin) throws URISyntaxException {
if (binding == null) {
return translationUnit;
return translationUnitRoot;
}

IBinding owner = binding.getOwner();
if (binding.equals(owner)) {
return URIUtil.correctLocation("circular", "", UUID.randomUUID().toString());
}
if (owner == null) {
return translationUnit;
return translationUnitRoot;
}
else {
return resolveBinding(null, owner, origin);
Expand Down Expand Up @@ -441,7 +470,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 {
Expand Down Expand Up @@ -476,7 +505,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 {
Expand Down Expand Up @@ -610,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(')');
Expand All @@ -621,12 +651,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, decl));
return decl;
return ownedBinding(binding, scheme, parameters.toString(), origin, binding.isStatic());
}

private ISourceLocation resolveICPPFunction(ICPPFunction binding, ISourceLocation origin) throws URISyntaxException {
Expand Down Expand Up @@ -672,11 +697,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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lang/cpp/internal/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/test/fac.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

int fac(int n) {
static int fac(int n) {
if (n <= 0) {
return 1;
}
Expand Down

0 comments on commit e340f48

Please sign in to comment.