Skip to content

Commit 28ae508

Browse files
committed
Handle manually given typedefs
This is made necessary at least by the gpointer c:type. The problem is that its name does not contain any *s, but it represents a pointer type, so this simple heuristic is not sufficient. A type with the c:type gpointer may not be treated as a pointer, resulting in functions like gst_object_unref() being skipped because it is believed to take a struct by value. This mechanism fixes the issue described.
1 parent 984b894 commit 28ae508

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

c-typedefs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This should eventually be generated automatically from gir <c:include>s.
2+
# Columns: original, new (like in C typedefs)
3+
4+
void*,gpointer

src/gir2java/ConvertedType.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ public ConvertedType forCType(ParsingContext context, String ctype) {
121121
return this;
122122
}
123123

124+
String ctypeAfterTypedef = context.applyTypedefs(ctype);
125+
124126
ConvertedType pointerConvType = new ConvertedType(this);
125127

126128
JType currentJType = getJType();
@@ -135,14 +137,14 @@ public ConvertedType forCType(ParsingContext context, String ctype) {
135137
}
136138

137139
JType indirectJType = getJType();
138-
int indirection = NameUtils.getIndirectionLevel(ctype) - currentIndirection;
140+
int indirection = NameUtils.getIndirectionLevel(ctypeAfterTypedef) - currentIndirection;
139141

140142
for (; indirection > 0; indirection--) {
141143
indirectJType = context.getCm().ref(Pointer.class).narrow(indirectJType);
142144
}
143145

144146
pointerConvType.setJType(indirectJType);
145-
pointerConvType.setCtype(ctype);
147+
pointerConvType.setCtype(ctypeAfterTypedef);
146148
return pointerConvType;
147149
}
148150

src/gir2java/GirParser.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public GirParser(JCodeModel cm) {
9999
referencedTypes = new HashSet<String>();
100100

101101
fillStaticTypeMappings();
102+
fillStaticTypedefs();
102103
}
103104

104105
public void outputTypes(File found, File referenced, File undefined) {
@@ -193,6 +194,40 @@ private void fillStaticTypeMappings() {
193194
foundTypes.add("none");
194195
}
195196

197+
private void fillStaticTypedefs() {
198+
BufferedReader staticTypedefs;
199+
try {
200+
staticTypedefs = new BufferedReader(new InputStreamReader(new FileInputStream("c-typedefs")));
201+
} catch (FileNotFoundException e) {
202+
System.err.println("No typedefs file found, not using any typedefs");
203+
return;
204+
}
205+
206+
String line;
207+
try {
208+
line = staticTypedefs.readLine();
209+
while (line != null) {
210+
//Skip lines containing only whitespace and/or a comment
211+
String preprocessedLine = line.trim().replaceFirst("#.*$", "");
212+
if (preprocessedLine.length() == 0) {
213+
line = staticTypedefs.readLine();
214+
continue;
215+
}
216+
217+
StringTokenizer st = new StringTokenizer(preprocessedLine, ",");
218+
String orig = st.nextToken();
219+
String alias = st.nextToken();
220+
221+
typeRegistry.addTypedef(orig, alias);
222+
223+
line = staticTypedefs.readLine();
224+
}
225+
} catch (IOException e) {
226+
System.err.println("Error while reading typedefs file, not using all typedefs");
227+
e.printStackTrace();
228+
}
229+
}
230+
196231
/**
197232
* Get a descriptor of the namespace defined in the given .gir Document. This assumes
198233
* there is at most one such namespace.

src/gir2java/ParsingContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public JDefinedClass getCurrentNamespaceClass() {
7272
return types.getNamespaceClass((String)getExtra(Constants.CONTEXT_EXTRA_NAMESPACE), this);
7373
}
7474

75+
public String applyTypedefs(String ctype) {
76+
return types.applyTypedefs(ctype);
77+
}
78+
7579
public Object getExtra(String key) {
7680
return extras.get(key);
7781
}

src/gir2java/TypeRegistry.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class TypeRegistry {
1818

1919
Map<String, Map<String, ConvertedType>> store = new HashMap<String, Map<String, ConvertedType>>();
2020
Map<String, JDefinedClass> namespaceClasses = new HashMap<String, JDefinedClass>();
21+
Map<String, String> ctypedefs = new HashMap<String, String>();
2122

2223
public void registerType(ConvertedType type) {
2324
registerTypeAs(type, type.getNamespace(), type.getType());
@@ -100,4 +101,18 @@ public JDefinedClass getNamespaceClass(String namespace, ParsingContext context)
100101
}
101102
return ret;
102103
}
104+
105+
public void addTypedef(String ctype, String alias) {
106+
ctypedefs.put(alias, ctype);
107+
}
108+
109+
public String applyTypedefs(String ctype) {
110+
String ret = ctype;
111+
String next = ctypedefs.get(ret);
112+
while(next != null) {
113+
ret = next;
114+
next = ctypedefs.get(next);
115+
}
116+
return ret;
117+
}
103118
}

0 commit comments

Comments
 (0)