Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement function shadowing #26

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement function shadowing
jwharm committed Dec 29, 2022
commit ed2844c1dbc485ab601d3a2622cb3a8b938626a2
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ public GStreamerExample(String[] args) {

// We add a message handler
bus = pipeline.getBus();
busWatchId = bus.addWatch(this::busCall);
busWatchId = bus.addWatch(0, this::busCall, () -> {});

// We add all elements into the pipeline
// file-source | ogg-demuxer | vorbis-decoder | converter | alsa-output
Original file line number Diff line number Diff line change
@@ -190,11 +190,17 @@ public void startElement(String uri, String lName, String qName, Attributes attr
current = newMember;
}
case "method" -> {
Method newMethod = new Method(current, attr.getValue("name"),
attr.getValue("c:identifier"), attr.getValue("deprecated"),
attr.getValue("throws"));
current.methodList.add(newMethod);
current = newMethod;
if (attr.getValue("shadowed-by") != null) {
// Do not generate shadowed methods
skip = qName;
} else {
Method newMethod = new Method(current, attr.getValue("name"),
attr.getValue("c:identifier"), attr.getValue("deprecated"),
attr.getValue("throws"), attr.getValue("shadowed-by"),
attr.getValue("shadows"));
current.methodList.add(newMethod);
current = newMethod;
}
}
case "namespace" -> {
Namespace newNamespace = new Namespace(current, attr.getValue("name"), attr.getValue("version"),
Original file line number Diff line number Diff line change
@@ -31,6 +31,16 @@ protected static void removeType(Repository repo, String type) {
if (repo.namespace.registeredTypeMap.remove(type) == null) System.err.println("Did not remove " + type + ": Not found");
}

protected static void setReturnVoid(Repository repo, String type, String name) {
Method m = findMethod(repo, type, name);
if (m != null) {
ReturnValue rv = m.returnValue;
rv.type = new Type(rv, "none", "void");
rv.doc = null;
} else
System.err.println("Did not change return type of " + type + "." + name + ": Not found");
}

protected static Method findMethod(Repository repo, String type, String method) {
try {
for (Method m : repo.namespace.registeredTypeMap.get(type).methodList) {
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
public class Constructor extends Method {

public Constructor(GirElement parent, String name, String cIdentifier, String deprecated, String throws_) {
super(parent, name, cIdentifier, deprecated, throws_);
super(parent, name, cIdentifier, deprecated, throws_, null, null);
}

public void generate(Writer writer, boolean isInterface) throws IOException {
Original file line number Diff line number Diff line change
@@ -3,6 +3,6 @@
public class Function extends Method {

public Function(GirElement parent, String name, String cIdentifier, String deprecated, String throws_) {
super(parent, name, cIdentifier, deprecated, throws_);
super(parent, name, cIdentifier, deprecated, throws_, null, null);
}
}
13 changes: 10 additions & 3 deletions generator/src/main/java/io/github/jwharm/javagi/model/Method.java
Original file line number Diff line number Diff line change
@@ -7,16 +7,23 @@

public class Method extends GirElement implements CallableType {

public final String cIdentifier, deprecated, throws_;
public final String cIdentifier;
public final String deprecated;
public final String throws_;
public final String shadowedBy;
public final String shadows;
public ReturnValue returnValue;
public Parameters parameters;

public Method(GirElement parent, String name, String cIdentifier, String deprecated, String throws_) {
public Method(GirElement parent, String name, String cIdentifier, String deprecated,
String throws_, String shadowedBy, String shadows) {
super(parent);
this.name = name;
this.name = shadows == null ? name : shadows; // Language bindings are expected to rename a function to the shadowed function
this.cIdentifier = cIdentifier;
this.deprecated = deprecated;
this.throws_ = throws_;
this.shadowedBy = shadowedBy;
this.shadows = shadows;

// Handle empty names. (For example, GLib.g_iconv is named "".)
if ("".equals(name)) {
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ public class Signal extends Method implements Closure {


public Signal(GirElement parent, String name, String when, String detailed, String deprecated, String throws_) {
super(parent, name, null, deprecated, throws_);
super(parent, name, null, deprecated, throws_, null, null);
this.when = when;
this.detailed = "1".equals(detailed);

Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ public class Type extends GirElement {
public String girElementType;

/** Example: gboolean, const char*, GdkRectangle* */
public final String cType;
public String cType;

/** This is the type name from the gir file. For example: Gdk.Rectangle */
public String qualifiedName;
Original file line number Diff line number Diff line change
@@ -3,6 +3,6 @@
public class VirtualMethod extends Method {

public VirtualMethod(GirElement parent, String name, String deprecated, String throws_) {
super(parent, name, null, deprecated, throws_);
super(parent, name, null, deprecated, throws_, null, null);
}
}
3 changes: 3 additions & 0 deletions gtk4/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -92,6 +92,9 @@ val genSources by tasks.registering {
renameMethod(repo, "MenuButton", "get_direction", "get_arrow_direction")
renameMethod(repo, "PrintUnixDialog", "get_settings", "get_print_settings")
renameMethod(repo, "PrintSettings", "get", "get_string")
// This method returns void in interface ActionGroup, but returns boolean in class Widget.
// Subclasses from Widget that implement ActionGroup throw a compile error.
setReturnVoid(repo, "Widget", "activate_action");
}
}),
source("Adw-1", "org.gnome.adw", true, "adwaita-1", patches = object: PatchSet() {