GlueGen v2.6.0-rc-20250712
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 */
51public 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() {
119 //output.println(" // Emitter: " + getClass().getName());
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
150
151 commentEmitter.emit(this, unit.output);
152
153 unit.emit(getBaseIndentString()); //indent
154
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
General code unit (a generated C or Java source file), covering multiple FunctionEmitter allowing to ...
Definition: CodeUnit.java:42
void emit(final String s)
Definition: CodeUnit.java:81
final PrintWriter output
Definition: CodeUnit.java:44
Generic function emitter to produce C (JNI) or Java code stubs to its CodeUnit, invoking a native fun...
CommentEmitter getCommentEmitter()
Get the comment emitter for this FunctionEmitter.
final void emit()
Emit the function to the getUnit().
void addModifiers(final Iterator< EmissionModifier > mi)
boolean removeModifier(final EmissionModifier m)
abstract StringBuilder appendName(StringBuilder buf)
void setCommentEmitter(final CommentEmitter cEmitter)
Set the object that will emit the comment for this function.
final int emitArguments()
Returns the number of arguments emitted.
abstract FunctionSymbol getCSymbol()
StringBuilder appendSignature(final StringBuilder buf)
abstract StringBuilder appendReturnType(StringBuilder buf)
FunctionEmitter(final MethodBinding binding, final CodeUnit unit, final boolean isInterface, final JavaConfiguration configuration)
Constructs the FunctionEmitter with a CommentEmitter that emits nothing.
boolean hasModifier(final EmissionModifier m)
void addModifier(final EmissionModifier m)
abstract String getInterfaceName()
int appendModifiers(final StringBuilder buf)
Iterator< EmissionModifier > getModifiers()
FunctionEmitter(final FunctionEmitter arg)
Makes this FunctionEmitter a copy of the passed one.
static final EmissionModifier STATIC
abstract int appendArguments(StringBuilder buf)
Returns the number of arguments emitted.
Parses and provides access to the contents of .cfg files for the JavaEmitter.
Represents the binding of a C function to a Java method.
String toString()
Returns the signature of this binding.
Describes a function symbol, which includes the name and type.
void emit(FunctionEmitter funcEmitter, PrintWriter output)
Emit the body of a comment for the specified function; do NOT emit the open (e.g.,...