GlueGen: src/java/com/jogamp/gluegen/FunctionEmitter.java Source File

GlueGen v2.6.0-rc-20250822 GlueGen, Native Binding Generator for Java™ (public API).

FunctionEmitter.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2023 JogAmp Community. All rights reserved.
3 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40
41package com.jogamp.gluegen;
42
43import java.util.ArrayList;
44import java.util.Iterator;
45
46import com.jogamp.gluegen.cgram.types.FunctionSymbol;
47
48/**
49 * Generic function emitter to produce C (JNI) or Java code stubs to its {@link CodeUnit}, invoking a native function as described via {@link MethodBinding}.
50 */
51 public abstract class FunctionEmitter {
52
53 public static final EmissionModifier STATIC = new EmissionModifier("static");
54
55 private final boolean isInterface;
56 private final ArrayList<EmissionModifier> modifiers;
57 private CommentEmitter commentEmitter = null;
58 protected final MethodBinding binding;
59 protected final CodeUnit unit;
60 // Only present to provide more clear comments
61 protected final JavaConfiguration cfg;
62
63 /**
64 * Constructs the FunctionEmitter with a CommentEmitter that emits nothing.
65 */
66 public FunctionEmitter(final MethodBinding binding, final CodeUnit unit, final boolean isInterface, final JavaConfiguration configuration) {
67 assert(unit != null);
68 this.isInterface = isInterface;
69 this.modifiers = new ArrayList<EmissionModifier>();
70 this.binding = binding;
71 this.unit = unit;
72 this.cfg = configuration;
73 }
74
75 /**
76 * Makes this FunctionEmitter a copy of the passed one.
77 */
78 public FunctionEmitter(final FunctionEmitter arg) {
79 isInterface = arg.isInterface;
80 modifiers = new ArrayList<EmissionModifier>(arg.modifiers);
81 commentEmitter = arg.commentEmitter;
82 binding = arg.binding;
83 unit = arg.unit;
84 cfg = arg.cfg;
85 }
86
87 public final boolean isInterface() { return isInterface; }
88
89 public final MethodBinding getBinding() { return binding; }
90 public final CodeUnit getUnit() { return unit; }
91
92 public void addModifiers(final Iterator<EmissionModifier> mi) {
93 while (mi.hasNext()) {
94 modifiers.add(mi.next());
95 }
96 }
97 public void addModifier(final EmissionModifier m) { modifiers.add(m); }
98
99 public boolean removeModifier(final EmissionModifier m) { return modifiers.remove(m); }
100
101 public void clearModifiers() { modifiers.clear(); }
102
103 public boolean hasModifier(final EmissionModifier m) { return modifiers.contains(m); }
104
105 public Iterator<EmissionModifier> getModifiers() { return modifiers.iterator(); }
106
107 public abstract String getInterfaceName();
108 public abstract String getImplName();
109 public abstract String getNativeName();
110
111 public abstract FunctionSymbol getCSymbol();
112
113 /**
114 * Emit the function to the {@link #getUnit()}
115 */
116 public final void emit() {
117 emitAdditionalCode();
118 emitDocComment();
119 //output.println(" // Emitter: " + getClass().getName());
120 emitSignature();
121 emitBody();
122 }
123
124 @Override
125 public String toString() {
126 return getClass().getSimpleName()+"["+binding.toString()+"]";
127 }
128
129 /**
130 * Set the object that will emit the comment for this function. If the
131 * parameter is null, no comment will be emitted.
132 */
133 public void setCommentEmitter(final CommentEmitter cEmitter) {
134 commentEmitter = cEmitter;
135 }
136
137 /**
138 * Get the comment emitter for this FunctionEmitter. The return value may be
139 * null, in which case no comment emitter has been set.
140 */
141 public CommentEmitter getCommentEmitter() { return commentEmitter; }
142
143 protected void emitAdditionalCode() { }
144 protected void emitDocComment() {
145
146 if (commentEmitter != null) {
147 unit.emit(getBaseIndentString()); //indent
148
149 unit.emit(getCommentStartString());
150
151 commentEmitter.emit(this, unit.output);
152
153 unit.emit(getBaseIndentString()); //indent
154
155 unit.emitln(getCommentEndString());
156 }
157 }
158
159 protected final void emitSignature() {
160 unit.emit(appendSignature(new StringBuilder()).toString());
161 }
162
163 protected StringBuilder appendSignature(final StringBuilder buf) {
164 buf.append(getBaseIndentString()); // indent method
165
166 final int numEmitted = appendModifiers(buf);
167 if (numEmitted > 0) {
168 buf.append(" ");
169 }
170
171 appendReturnType(buf);
172 buf.append(" ");
173
174 appendName(buf);
175 buf.append("(");
176
177 appendArguments(buf);
178 buf.append(")");
179 return buf;
180 }
181
182 protected final int emitModifiers() {
183 final StringBuilder buf = new StringBuilder();
184 final int n = appendModifiers(buf);
185 unit.emit(buf.toString());
186 return n;
187 }
188 protected int appendModifiers(final StringBuilder buf) {
189 int numEmitted = 0;
190 for (final Iterator<EmissionModifier> it = getModifiers(); it.hasNext(); ) {
191 buf.append(it.next().toString());
192 ++numEmitted;
193 if (it.hasNext()) {
194 buf.append(" ");
195 }
196 }
197 return numEmitted;
198 }
199
200 protected String getBaseIndentString() { return ""; }
201
202 protected String getCommentStartString() { return "/* "; }
203 protected String getCommentEndString() { return " */"; }
204
205 protected final void emitReturnType() {
206 unit.emit(appendReturnType(new StringBuilder()).toString());
207 }
208 protected abstract StringBuilder appendReturnType(StringBuilder buf);
209 protected final void emitName() {
210 unit.emit(appendName(new StringBuilder()).toString());
211 }
212 protected abstract StringBuilder appendName(StringBuilder buf);
213 /** Returns the number of arguments emitted. */
214 protected final int emitArguments() {
215 final StringBuilder buf = new StringBuilder();
216 final int n = appendArguments(buf);
217 unit.emit(buf.toString());
218 return n;
219 }
220 /** Returns the number of arguments emitted. */
221 protected abstract int appendArguments(StringBuilder buf);
222 protected abstract void emitBody();
223
224 public static class EmissionModifier {
225
226 @Override
227 public final String toString() { return emittedForm; }
228
229 private final String emittedForm;
230
231 @Override
232 public int hashCode() {
233 return emittedForm.hashCode();
234 }
235
236 @Override
237 public boolean equals(final Object arg) {
238 if (arg == null || (!(arg instanceof EmissionModifier))) {
239 return false;
240 }
241
242 return emittedForm.equals(((EmissionModifier) arg).emittedForm);
243 }
244
245 protected EmissionModifier(final String emittedForm) { this.emittedForm = emittedForm; }
246 }
247}
248
com.jogamp.gluegen.CodeUnit
General code unit (a generated C or Java source file), covering multiple FunctionEmitter allowing to ...
Definition: CodeUnit.java:42
com.jogamp.gluegen.CodeUnit.emitln
void emitln()
Definition: CodeUnit.java:75
com.jogamp.gluegen.CodeUnit.emit
void emit(final String s)
Definition: CodeUnit.java:81
com.jogamp.gluegen.CodeUnit.output
final PrintWriter output
Definition: CodeUnit.java:44
com.jogamp.gluegen.FunctionEmitter.EmissionModifier
Definition: FunctionEmitter.java:224
com.jogamp.gluegen.FunctionEmitter.EmissionModifier.EmissionModifier
EmissionModifier(final String emittedForm)
Definition: FunctionEmitter.java:245
com.jogamp.gluegen.FunctionEmitter.EmissionModifier.hashCode
int hashCode()
Definition: FunctionEmitter.java:232
com.jogamp.gluegen.FunctionEmitter.EmissionModifier.toString
final String toString()
Definition: FunctionEmitter.java:227
com.jogamp.gluegen.FunctionEmitter.EmissionModifier.equals
boolean equals(final Object arg)
Definition: FunctionEmitter.java:237
com.jogamp.gluegen.FunctionEmitter
Generic function emitter to produce C (JNI) or Java code stubs to its CodeUnit, invoking a native fun...
Definition: FunctionEmitter.java:51
com.jogamp.gluegen.FunctionEmitter.getCommentEmitter
CommentEmitter getCommentEmitter()
Get the comment emitter for this FunctionEmitter.
Definition: FunctionEmitter.java:141
com.jogamp.gluegen.FunctionEmitter.getNativeName
abstract String getNativeName()
com.jogamp.gluegen.FunctionEmitter.emitName
final void emitName()
Definition: FunctionEmitter.java:209
com.jogamp.gluegen.FunctionEmitter.emit
final void emit()
Emit the function to the getUnit().
Definition: FunctionEmitter.java:116
com.jogamp.gluegen.FunctionEmitter.addModifiers
void addModifiers(final Iterator< EmissionModifier > mi)
Definition: FunctionEmitter.java:92
com.jogamp.gluegen.FunctionEmitter.getCommentEndString
String getCommentEndString()
Definition: FunctionEmitter.java:203
com.jogamp.gluegen.FunctionEmitter.removeModifier
boolean removeModifier(final EmissionModifier m)
Definition: FunctionEmitter.java:99
com.jogamp.gluegen.FunctionEmitter.getCommentStartString
String getCommentStartString()
Definition: FunctionEmitter.java:202
com.jogamp.gluegen.FunctionEmitter.appendName
abstract StringBuilder appendName(StringBuilder buf)
com.jogamp.gluegen.FunctionEmitter.setCommentEmitter
void setCommentEmitter(final CommentEmitter cEmitter)
Set the object that will emit the comment for this function.
Definition: FunctionEmitter.java:133
com.jogamp.gluegen.FunctionEmitter.emitArguments
final int emitArguments()
Returns the number of arguments emitted.
Definition: FunctionEmitter.java:214
com.jogamp.gluegen.FunctionEmitter.getCSymbol
abstract FunctionSymbol getCSymbol()
com.jogamp.gluegen.FunctionEmitter.appendSignature
StringBuilder appendSignature(final StringBuilder buf)
Definition: FunctionEmitter.java:163
com.jogamp.gluegen.FunctionEmitter.emitReturnType
final void emitReturnType()
Definition: FunctionEmitter.java:205
com.jogamp.gluegen.FunctionEmitter.appendReturnType
abstract StringBuilder appendReturnType(StringBuilder buf)
com.jogamp.gluegen.FunctionEmitter.cfg
final JavaConfiguration cfg
Definition: FunctionEmitter.java:61
com.jogamp.gluegen.FunctionEmitter.isInterface
final boolean isInterface()
Definition: FunctionEmitter.java:87
com.jogamp.gluegen.FunctionEmitter.FunctionEmitter
FunctionEmitter(final MethodBinding binding, final CodeUnit unit, final boolean isInterface, final JavaConfiguration configuration)
Constructs the FunctionEmitter with a CommentEmitter that emits nothing.
Definition: FunctionEmitter.java:66
com.jogamp.gluegen.FunctionEmitter.getUnit
final CodeUnit getUnit()
Definition: FunctionEmitter.java:90
com.jogamp.gluegen.FunctionEmitter.emitDocComment
void emitDocComment()
Definition: FunctionEmitter.java:144
com.jogamp.gluegen.FunctionEmitter.hasModifier
boolean hasModifier(final EmissionModifier m)
Definition: FunctionEmitter.java:103
com.jogamp.gluegen.FunctionEmitter.addModifier
void addModifier(final EmissionModifier m)
Definition: FunctionEmitter.java:97
com.jogamp.gluegen.FunctionEmitter.emitAdditionalCode
void emitAdditionalCode()
Definition: FunctionEmitter.java:143
com.jogamp.gluegen.FunctionEmitter.getInterfaceName
abstract String getInterfaceName()
com.jogamp.gluegen.FunctionEmitter.unit
final CodeUnit unit
Definition: FunctionEmitter.java:59
com.jogamp.gluegen.FunctionEmitter.emitSignature
final void emitSignature()
Definition: FunctionEmitter.java:159
com.jogamp.gluegen.FunctionEmitter.getBaseIndentString
String getBaseIndentString()
Definition: FunctionEmitter.java:200
com.jogamp.gluegen.FunctionEmitter.appendModifiers
int appendModifiers(final StringBuilder buf)
Definition: FunctionEmitter.java:188
com.jogamp.gluegen.FunctionEmitter.emitBody
abstract void emitBody()
com.jogamp.gluegen.FunctionEmitter.getModifiers
Iterator< EmissionModifier > getModifiers()
Definition: FunctionEmitter.java:105
com.jogamp.gluegen.FunctionEmitter.emitModifiers
final int emitModifiers()
Definition: FunctionEmitter.java:182
com.jogamp.gluegen.FunctionEmitter.binding
final MethodBinding binding
Definition: FunctionEmitter.java:58
com.jogamp.gluegen.FunctionEmitter.FunctionEmitter
FunctionEmitter(final FunctionEmitter arg)
Makes this FunctionEmitter a copy of the passed one.
Definition: FunctionEmitter.java:78
com.jogamp.gluegen.FunctionEmitter.STATIC
static final EmissionModifier STATIC
Definition: FunctionEmitter.java:53
com.jogamp.gluegen.FunctionEmitter.getBinding
final MethodBinding getBinding()
Definition: FunctionEmitter.java:89
com.jogamp.gluegen.FunctionEmitter.clearModifiers
void clearModifiers()
Definition: FunctionEmitter.java:101
com.jogamp.gluegen.FunctionEmitter.getImplName
abstract String getImplName()
com.jogamp.gluegen.FunctionEmitter.appendArguments
abstract int appendArguments(StringBuilder buf)
Returns the number of arguments emitted.
com.jogamp.gluegen.FunctionEmitter.toString
String toString()
Definition: FunctionEmitter.java:125
com.jogamp.gluegen.JavaConfiguration
Parses and provides access to the contents of .cfg files for the JavaEmitter.
Definition: JavaConfiguration.java:63
com.jogamp.gluegen.MethodBinding
Represents the binding of a C function to a Java method.
Definition: MethodBinding.java:54
com.jogamp.gluegen.MethodBinding.toString
String toString()
Returns the signature of this binding.
Definition: MethodBinding.java:719
com.jogamp.gluegen.cgram.types.FunctionSymbol
Describes a function symbol, which includes the name and type.
Definition: FunctionSymbol.java:63
com.jogamp.gluegen.CommentEmitter
Definition: CommentEmitter.java:44
com.jogamp.gluegen.CommentEmitter.emit
void emit(FunctionEmitter funcEmitter, PrintWriter output)
Emit the body of a comment for the specified function; do NOT emit the open (e.g.,...