/* * Copyright (c) 2002-2004 LWJGL Project * 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. */ package org.lwjgl.generator; /** * $Id$ * * This class represent a parameter configuration. There are multiple * TypeInfos in case of multityped parameters. * * @author elias_naur * @version $Revision$ */ import com.sun.mirror.declaration.*; import com.sun.mirror.type.*; import java.util.*; import java.nio.*; public class TypeInfo { public static final String UNSIGNED_PARAMETER_NAME = "unsigned"; private final Signedness signedness; private final Class type; private final String auto_type; private TypeInfo(Class type, Signedness signedness, String auto_type) { this.type = type; this.signedness = signedness; this.auto_type = auto_type; } public Class getType() { return type; } public Signedness getSignedness() { return signedness; } public String getAutoType() { if (auto_type == null) throw new RuntimeException("No auto type assigned"); return auto_type; } private static Class getTypeFromPrimitiveKind(PrimitiveType.Kind kind) { Class type; switch (kind) { case INT: type = int.class; break; case FLOAT: type = float.class; break; case DOUBLE: type = double.class; break; case SHORT: type = short.class; break; case BYTE: type = byte.class; break; default: throw new RuntimeException(kind + " is not allowed"); } return type; } private static Class getBufferTypeFromPrimitiveKind(PrimitiveType.Kind kind) { Class type; switch (kind) { case INT: type = IntBuffer.class; break; case FLOAT: type = FloatBuffer.class; break; case DOUBLE: type = DoubleBuffer.class; break; case SHORT: type = ShortBuffer.class; break; case BYTE: /* fall through */ case BOOLEAN: type = ByteBuffer.class; break; default: throw new RuntimeException(kind + " is not allowed"); } return type; } private static TypeInfo getDefaultTypeInfo(TypeMirror t) { Class java_type = Utils.getJavaType(t); return new TypeInfo(java_type, Signedness.NONE, null); } public static Map getDefaultTypeInfoMap(MethodDeclaration method) { Map map = new HashMap(); for (ParameterDeclaration param : method.getParameters()) { TypeInfo type_info = getDefaultTypeInfo(param.getType()); map.put(param, type_info); } return map; } private static Collection getTypeInfos(TypeMap type_map, Declaration param, TypeMirror decl_type) { Collection annotations = Utils.getSortedAnnotations(param.getAnnotationMirrors()); Map types = new HashMap(); Collection multityped_result = new ArrayList(); boolean add_default_type = true; for (AnnotationMirror annotation : annotations) { NativeType native_type_annotation = NativeTypeTranslator.getAnnotation(annotation, NativeType.class); if (native_type_annotation != null) { Class annotation_type = NativeTypeTranslator.getClassFromType(annotation.getAnnotationType()); Signedness signedness = type_map.getSignednessFromType(annotation_type); Class inverse_type = type_map.getInverseType(annotation_type); String auto_type = type_map.getAutoTypeFromAnnotation(annotation); if (inverse_type != null) { if (types.containsKey(inverse_type)) { TypeInfo inverse_type_info = types.get(inverse_type); String inverse_auto_type = inverse_type_info.getAutoType(); auto_type = signedness == Signedness.UNSIGNED ? auto_type + " : " + inverse_auto_type : inverse_auto_type + " : " + auto_type; auto_type = UNSIGNED_PARAMETER_NAME + " ? " + auto_type; signedness = Signedness.BOTH; types.remove(inverse_type); multityped_result.remove(inverse_type_info); } } Class type; PrimitiveType.Kind kind = type_map.getPrimitiveTypeFromNativeType(annotation_type); if (Utils.getNIOBufferType(decl_type) != null) type = getBufferTypeFromPrimitiveKind(kind); else type = getTypeFromPrimitiveKind(kind); TypeInfo type_info = new TypeInfo(type, signedness, auto_type); types.put(annotation_type, type_info); multityped_result.add(type_info); add_default_type = false; } } if (add_default_type) { TypeInfo default_type_info = getDefaultTypeInfo(decl_type); Collection result = new ArrayList(); result.add(default_type_info); return result; } else return multityped_result; } private static Map> getTypeInfoMap(TypeMap type_map, MethodDeclaration method) { Map> map = new HashMap>(); for (ParameterDeclaration param : method.getParameters()) { Collection types = getTypeInfos(type_map, param, param.getType()); map.put(param, types); } return map; } public static Collection> getTypeInfoCrossProduct(TypeMap type_map, MethodDeclaration method) { Collection parameter_collection = method.getParameters(); ParameterDeclaration[] parameters = new ParameterDeclaration[parameter_collection.size()]; parameter_collection.toArray(parameters); Collection> cross_product = new ArrayList>(); getCrossProductRecursive(0, parameters, getTypeInfoMap(type_map, method), new HashMap(), cross_product); return cross_product; } private static void getCrossProductRecursive(int index, ParameterDeclaration[] parameters, Map> typeinfos_map, Map current_instance, Collection> cross_product) { if (index == parameters.length) { cross_product.add(current_instance); return; } ParameterDeclaration param = parameters[index]; Collection typeinfos = typeinfos_map.get(param); if (typeinfos != null) { for (TypeInfo typeinfo : typeinfos) { Map instance = new HashMap(current_instance); instance.put(param, typeinfo); getCrossProductRecursive(index + 1, parameters, typeinfos_map, instance, cross_product); } } } }