Skip to content

Commit b5a262a

Browse files
committed
BridJ: make C++ demanglers const-aware
1 parent 967bf89 commit b5a262a

4 files changed

Lines changed: 22 additions & 13 deletions

File tree

src/main/java/org/bridj/demangling/Demangler.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,14 @@ public String toString() {
560560
public static class PointerTypeRef extends TypeRef {
561561

562562
public TypeRef pointedType;
563+
public boolean isReference;
564+
public boolean isConst;
563565

564-
public PointerTypeRef(TypeRef pointedType) {
566+
public PointerTypeRef(TypeRef pointedType, boolean isConst, boolean isReference) {
565567
assert pointedType != null;
566568
this.pointedType = pointedType;
569+
this.isReference = isReference;
570+
this.isConst = isConst;
567571
}
568572

569573
@Override
@@ -573,7 +577,7 @@ public StringBuilder getQualifiedName(StringBuilder b, boolean generic) {
573577

574578
@Override
575579
public String toString() {
576-
return pointedType + "*";
580+
return (isConst ? "const " : "") + pointedType + (isReference ? "&" : "*");
577581
}
578582

579583
@Override
@@ -600,8 +604,8 @@ public boolean matches(Type type, Annotations annotations) {
600604

601605
}
602606

603-
protected static TypeRef pointerType(TypeRef tr) {
604-
return new PointerTypeRef(tr);
607+
protected static TypeRef pointerType(TypeRef tr, boolean isConst, boolean isReference) {
608+
return new PointerTypeRef(tr, isConst, isReference);
605609
}
606610

607611
protected static TypeRef classType(final Class<?> c, Class<? extends Annotation>... annotations) {

src/main/java/org/bridj/demangling/GCC4Demangler.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ private String nextShortcutId() {
9696
return n == -1 ? "_" : Integer.toString(n, 36).toUpperCase() + "_";
9797
}
9898

99-
private TypeRef parsePointerType(boolean memorizePointed) throws DemanglingException {
99+
private TypeRef parsePointerType(boolean memorizePointed, boolean isConst, boolean isReference) throws DemanglingException {
100100
String subId = memorizePointed ? nextShortcutId() : null;
101101
TypeRef pointed = parseType();
102102
if (memorizePointed)
103103
typeShortcuts.put(subId, pointed);
104-
TypeRef res = pointerType(pointed);
104+
TypeRef res = pointerType(pointed, isConst, isReference);
105105
String id = nextShortcutId();
106106
typeShortcuts.put(id, res);
107107
return res;
@@ -180,9 +180,12 @@ public TypeRef parseType() throws DemanglingException {
180180
}
181181
return res;
182182
}
183+
case 'R': // Reference: TODO: treat const ref as a value.
183184
case 'P': {
184185
char nextChar = peekChar();
185-
return parsePointerType(nextChar == 'K' || nextChar == 'N');
186+
boolean isReference = c == 'R';
187+
boolean isConst = nextChar == 'K';
188+
return parsePointerType(isConst || nextChar == 'N', isConst, isReference);
186189
}
187190
case 'F': {
188191
// TODO parse function type correctly !!!
@@ -196,7 +199,7 @@ public TypeRef parseType() throws DemanglingException {
196199
expectChars('E');
197200
return new FunctionTypeRef(mr);
198201
}
199-
case 'K':
202+
case 'K': // const?
200203
return parseType();
201204
case 'v': // char
202205
return classType(Void.TYPE);

src/main/java/org/bridj/demangling/VC9Demangler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ TypeRef parseType(boolean allowVoid) throws DemanglingException {
395395
case 'Q': // const pointer
396396
case 'R': // volatile pointer
397397
case 'S': // const volatile pointer
398+
boolean isReference = c == 'A' || c == 'B';
399+
boolean isConst = c == 'Q' || c == 'S';
398400
if (!consumeCharsIf('$', 'A')) // __gc
399401
{
400402
consumeCharsIf('$', 'B'); // __pin
@@ -408,11 +410,11 @@ TypeRef parseType(boolean allowVoid) throws DemanglingException {
408410
indices[i] = parseNumber(false);
409411
}
410412
}
411-
tr = pointerType(parseType(true));
413+
tr = pointerType(parseType(true), isConst, isReference);
412414
} else {
413415
MemberRef mr = new MemberRef();
414416
parseFunctionProperty(mr);
415-
tr = pointerType(new FunctionTypeRef(mr));
417+
tr = pointerType(new FunctionTypeRef(mr), isConst, isReference);
416418
}
417419
addBackRef(tr);
418420
return tr;

src/test/java/org/bridj/DemanglingTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ public void gccMemoryShortcuts() {
390390
demangle(null, "_Z15shortcutsSimplePPPPccS_S0_S1_S2_PS2_", "null shortcutsSimple(byte****, byte, byte*, byte**, byte***, byte****, byte*****)");
391391

392392
demangle(null, "__Z1fPsS_", "null f(short*, short*)");
393-
demangle(null, "__Z1fPKsS_", "null f(short*, short)");
394-
demangle(null, "__Z1fPKsS0_", "null f(short*, short*)");
395-
demangle(null, "__Z1fPKcS0_S0_PKsS2_PKdS4_", "null f(byte*, byte*, byte*, short*, short*, double*, double*)");
393+
demangle(null, "__Z1fPKsS_", "null f(const short*, short)");
394+
demangle(null, "__Z1fPKsS0_", "null f(const short*, const short*)");
395+
demangle(null, "__Z1fPKcS0_S0_PKsS2_PKdS4_", "null f(const byte*, const byte*, const byte*, const short*, const short*, const double*, const double*)");
396396
demangle(null, "__ZN1AC2EPS_PS0_S1_", "null A.(A*, A**, A**)");
397397
/*
398398
*

0 commit comments

Comments
 (0)