2005-02-15 06:05:36 -05:00
|
|
|
/*
|
2008-04-07 14:36:09 -04:00
|
|
|
* Copyright (c) 2002-2008 LWJGL Project
|
2005-02-15 06:05:36 -05:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2005-02-20 14:28:34 -05:00
|
|
|
package org.lwjgl.util.generator;
|
2005-02-15 06:05:36 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2005-02-15 12:13:05 -05:00
|
|
|
* A TypeVisitor that translates types (and optional native type
|
2005-02-15 06:05:36 -05:00
|
|
|
* annotations) to the native type string.
|
|
|
|
*
|
|
|
|
* @author elias_naur <elias_naur@users.sourceforge.net>
|
|
|
|
* @version $Revision$
|
2006-03-23 14:32:21 -05:00
|
|
|
* $Id$
|
2005-02-15 06:05:36 -05:00
|
|
|
*/
|
|
|
|
|
2010-09-26 19:43:24 -04:00
|
|
|
import org.lwjgl.PointerBuffer;
|
2005-02-15 06:05:36 -05:00
|
|
|
|
2010-09-26 19:43:24 -04:00
|
|
|
import java.lang.annotation.Annotation;
|
2005-02-15 06:05:36 -05:00
|
|
|
import java.nio.*;
|
2010-09-26 19:43:24 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2005-02-15 06:05:36 -05:00
|
|
|
|
2010-09-26 19:43:24 -04:00
|
|
|
import com.sun.mirror.declaration.AnnotationMirror;
|
|
|
|
import com.sun.mirror.declaration.Declaration;
|
|
|
|
import com.sun.mirror.type.*;
|
|
|
|
import com.sun.mirror.util.TypeVisitor;
|
2005-02-15 06:05:36 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* $Id$
|
2010-09-26 19:43:24 -04:00
|
|
|
* <p/>
|
2005-02-15 06:05:36 -05:00
|
|
|
* A TypeVisitor that translates (annotated) TypeMirrors to
|
|
|
|
* native types
|
|
|
|
*
|
|
|
|
* @author elias_naur <elias_naur@users.sourceforge.net>
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
|
|
|
public class NativeTypeTranslator implements TypeVisitor {
|
2010-09-26 19:43:24 -04:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
private Collection<Class> native_types;
|
|
|
|
private boolean is_indirect;
|
|
|
|
private final Declaration declaration;
|
|
|
|
private final TypeMap type_map;
|
2005-02-15 12:13:05 -05:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public NativeTypeTranslator(TypeMap type_map, Declaration declaration) {
|
|
|
|
this.declaration = declaration;
|
|
|
|
this.type_map = type_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSignature() {
|
2010-09-26 19:43:24 -04:00
|
|
|
return getSignature(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSignature(final boolean skipConst) {
|
2005-02-15 06:05:36 -05:00
|
|
|
StringBuilder signature = new StringBuilder();
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( !skipConst && declaration.getAnnotation(Const.class) != null )
|
2005-02-15 06:05:36 -05:00
|
|
|
signature.append("const ");
|
2009-08-12 09:06:11 -04:00
|
|
|
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( declaration.getAnnotation(PointerWrapper.class) != null ) {
|
|
|
|
signature.append(declaration.getAnnotation(PointerWrapper.class).value());
|
|
|
|
} else if ( declaration.getAnnotation(NativeType.class) != null ) {
|
|
|
|
signature.append(declaration.getAnnotation(NativeType.class).value());
|
2009-08-12 09:06:11 -04:00
|
|
|
} else {
|
|
|
|
// Use the name of the native type annotation as the C type name
|
|
|
|
signature.append(getAnnotationType().getSimpleName());
|
|
|
|
}
|
|
|
|
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( is_indirect )
|
2005-02-15 06:05:36 -05:00
|
|
|
signature.append(" *");
|
|
|
|
return signature.toString();
|
|
|
|
}
|
|
|
|
|
2006-11-27 18:01:01 -05:00
|
|
|
public Class getAnnotationType() {
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( native_types.size() != 1 )
|
2006-11-27 18:01:01 -05:00
|
|
|
throw new RuntimeException("Expected only one native type for declaration " + declaration +
|
2010-09-26 19:43:24 -04:00
|
|
|
", but got " + native_types.size());
|
2006-11-27 18:01:01 -05:00
|
|
|
return native_types.iterator().next();
|
|
|
|
}
|
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public void visitAnnotationType(AnnotationType t) {
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void visitArrayType(ArrayType t) {
|
2010-09-26 19:43:24 -04:00
|
|
|
final Class<?> type = Utils.getJavaType(t).getComponentType();
|
|
|
|
|
|
|
|
if ( CharSequence.class.isAssignableFrom(type) ) {
|
2010-03-12 16:55:13 -05:00
|
|
|
is_indirect = true;
|
|
|
|
native_types = new ArrayList<Class>();
|
2010-09-26 19:43:24 -04:00
|
|
|
native_types.add(type_map.getStringArrayType());
|
|
|
|
} else if ( Buffer.class.isAssignableFrom(type) ) {
|
|
|
|
is_indirect = true;
|
|
|
|
native_types = new ArrayList<Class>();
|
|
|
|
native_types.add(type_map.getByteBufferArrayType());
|
|
|
|
} else if ( org.lwjgl.PointerWrapper.class.isAssignableFrom(type) ) {
|
|
|
|
is_indirect = false;
|
2010-03-12 16:55:13 -05:00
|
|
|
} else
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
2005-02-15 06:05:36 -05:00
|
|
|
}
|
2005-02-15 12:13:05 -05:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public static PrimitiveType.Kind getPrimitiveKindFromBufferClass(Class c) {
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( IntBuffer.class.equals(c) )
|
2005-02-15 06:05:36 -05:00
|
|
|
return PrimitiveType.Kind.INT;
|
2010-09-26 19:43:24 -04:00
|
|
|
else if ( DoubleBuffer.class.equals(c) )
|
2005-02-15 06:05:36 -05:00
|
|
|
return PrimitiveType.Kind.DOUBLE;
|
2010-09-26 19:43:24 -04:00
|
|
|
else if ( ShortBuffer.class.equals(c) )
|
2005-02-15 06:05:36 -05:00
|
|
|
return PrimitiveType.Kind.SHORT;
|
2010-09-26 19:43:24 -04:00
|
|
|
else if ( ByteBuffer.class.equals(c) || PointerBuffer.class.equals(c) )
|
2005-02-15 06:05:36 -05:00
|
|
|
return PrimitiveType.Kind.BYTE;
|
2010-09-26 19:43:24 -04:00
|
|
|
else if ( FloatBuffer.class.equals(c) )
|
2005-02-15 06:05:36 -05:00
|
|
|
return PrimitiveType.Kind.FLOAT;
|
2010-09-26 19:43:24 -04:00
|
|
|
else if ( LongBuffer.class.equals(c) )
|
2006-05-25 09:03:35 -04:00
|
|
|
return PrimitiveType.Kind.LONG;
|
2005-02-15 06:05:36 -05:00
|
|
|
else
|
|
|
|
throw new RuntimeException(c + " is not allowed");
|
|
|
|
}
|
|
|
|
|
2010-09-26 19:43:24 -04:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static Class<? extends Annotation> getClassFromType(DeclaredType t) {
|
2005-02-15 06:05:36 -05:00
|
|
|
try {
|
2010-09-26 19:43:24 -04:00
|
|
|
return (Class<? extends Annotation>)Class.forName(t.getDeclaration().getQualifiedName());
|
2005-02-15 06:05:36 -05:00
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getNativeTypeFromAnnotatedPrimitiveType(PrimitiveType.Kind kind) {
|
|
|
|
native_types = translateAnnotations();
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( native_types.size() == 0 )
|
2005-02-15 06:05:36 -05:00
|
|
|
native_types.add(type_map.getNativeTypeFromPrimitiveType(kind));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void visitClassType(ClassType t) {
|
2009-08-12 09:06:11 -04:00
|
|
|
is_indirect = true;
|
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
Class<?> c = getClassFromType(t);
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( String.class.equals(c) ) {
|
2005-02-15 06:05:36 -05:00
|
|
|
native_types = new ArrayList<Class>();
|
|
|
|
native_types.add(type_map.getStringElementType());
|
2010-09-26 19:43:24 -04:00
|
|
|
} else if ( Buffer.class.equals(c) ) {
|
2005-02-15 06:05:36 -05:00
|
|
|
native_types = new ArrayList<Class>();
|
|
|
|
native_types.add(type_map.getVoidType());
|
2013-07-22 18:57:41 -04:00
|
|
|
} else if ( Buffer.class.isAssignableFrom(c) || PointerBuffer.class.isAssignableFrom(c) ) {
|
2005-02-15 06:05:36 -05:00
|
|
|
PrimitiveType.Kind kind = getPrimitiveKindFromBufferClass(c);
|
|
|
|
getNativeTypeFromAnnotatedPrimitiveType(kind);
|
2010-09-26 19:43:24 -04:00
|
|
|
} else if ( org.lwjgl.PointerWrapper.class.isAssignableFrom(c) ) {
|
|
|
|
native_types = new ArrayList<Class>();
|
|
|
|
native_types.add(PointerWrapper.class);
|
2009-08-12 09:06:11 -04:00
|
|
|
|
|
|
|
is_indirect = false;
|
2005-02-15 06:05:36 -05:00
|
|
|
} else
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
|
|
|
}
|
2005-02-15 12:13:05 -05:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public void visitPrimitiveType(PrimitiveType t) {
|
|
|
|
getNativeTypeFromAnnotatedPrimitiveType(t.getKind());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void visitDeclaredType(DeclaredType t) {
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
|
|
|
}
|
2005-02-15 12:13:05 -05:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public void visitEnumType(EnumType t) {
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
|
|
|
}
|
2005-02-15 12:13:05 -05:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public void visitInterfaceType(InterfaceType t) {
|
2012-08-07 11:14:03 -04:00
|
|
|
// See ARB_debug_label.glObjectPtrLabel
|
|
|
|
Class<?> c = getClassFromType(t);
|
|
|
|
if ( org.lwjgl.PointerWrapper.class.isAssignableFrom(c) ) {
|
|
|
|
native_types = new ArrayList<Class>();
|
|
|
|
native_types.add(PointerWrapper.class);
|
|
|
|
|
|
|
|
is_indirect = false;
|
|
|
|
} else
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
2005-02-15 06:05:36 -05:00
|
|
|
}
|
2005-02-15 12:13:05 -05:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
// Check if the annotation is itself annotated with a certain annotation type
|
2010-09-26 19:43:24 -04:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public static <T extends Annotation> T getAnnotation(AnnotationMirror annotation, Class<T> type) {
|
|
|
|
return annotation.getAnnotationType().getDeclaration().getAnnotation(type);
|
|
|
|
}
|
|
|
|
|
2005-02-15 12:13:05 -05:00
|
|
|
private static Class translateAnnotation(AnnotationMirror annotation) {
|
2005-02-15 06:05:36 -05:00
|
|
|
NativeType native_type = getAnnotation(annotation, NativeType.class);
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( native_type != null ) {
|
2005-02-15 06:05:36 -05:00
|
|
|
return getClassFromType(annotation.getAnnotationType());
|
|
|
|
} else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Collection<Class> translateAnnotations() {
|
|
|
|
Collection<Class> result = new ArrayList<Class>();
|
2010-09-26 19:43:24 -04:00
|
|
|
for ( AnnotationMirror annotation : Utils.getSortedAnnotations(declaration.getAnnotationMirrors()) ) {
|
2005-02-15 06:05:36 -05:00
|
|
|
Class translated_result = translateAnnotation(annotation);
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( translated_result != null ) {
|
2005-02-15 06:05:36 -05:00
|
|
|
result.add(translated_result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void visitReferenceType(ReferenceType t) {
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
|
|
|
}
|
2006-05-25 09:03:35 -04:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public void visitTypeMirror(TypeMirror t) {
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
|
|
|
}
|
2005-02-15 12:13:05 -05:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public void visitTypeVariable(TypeVariable t) {
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
|
|
|
}
|
2005-02-15 12:13:05 -05:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public void visitVoidType(VoidType t) {
|
|
|
|
native_types = translateAnnotations();
|
2010-09-26 19:43:24 -04:00
|
|
|
if ( native_types.size() == 0 )
|
2005-02-15 06:05:36 -05:00
|
|
|
native_types.add(void.class);
|
|
|
|
}
|
2005-02-15 12:13:05 -05:00
|
|
|
|
2005-02-15 06:05:36 -05:00
|
|
|
public void visitWildcardType(WildcardType t) {
|
|
|
|
throw new RuntimeException(t + " is not allowed");
|
|
|
|
}
|
|
|
|
}
|