1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.internal.clang; 25 26 import clang.Index.CXType; 27 import java.foreign.Scope; 28 import java.foreign.memory.Pointer; 29 30 public class Type { 31 private final CXType type; 32 Type(CXType type) { 33 this.type = type; 34 } 35 36 // Function Types 37 public boolean isVariadic() { 38 return LibClang.lib.clang_isFunctionTypeVariadic(type) != 0; 39 } 40 public Type resultType() { 41 return new Type(LibClang.lib.clang_getResultType(type)); 42 } 43 public int numberOfArgs() { 44 return LibClang.lib.clang_getNumArgTypes(type); 45 } 46 public Type argType(int idx) { 47 return new Type(LibClang.lib.clang_getArgType(type, idx)); 48 } 49 private int getCallingConvention1() { 50 return LibClang.lib.clang_getFunctionTypeCallingConv(type); 51 } 52 53 public CallingConvention getCallingConvention() { 54 int v = getCallingConvention1(); 55 return CallingConvention.valueOf(v); 56 } 57 58 // Pointer type 59 public Type getPointeeType() { 60 return new Type(LibClang.lib.clang_getPointeeType(type)); 61 } 62 63 // array/vector type 64 public Type getElementType() { 65 return new Type(LibClang.lib.clang_getElementType(type)); 66 } 67 public long getNumberOfElements() { 68 return LibClang.lib.clang_getNumElements(type); 69 } 70 71 // Struct/RecordType 72 public long getOffsetOf(String fieldName) { 73 try (Scope s = Scope.newNativeScope()) { 74 Pointer<Byte> cfname = s.toCString(fieldName); 75 return LibClang.lib.clang_Type_getOffsetOf(type, cfname); 76 } 77 } 78 79 // Typedef 80 public Type canonicalType() { 81 return new Type(LibClang.lib.clang_getCanonicalType(type)); 82 } 83 84 public String spelling() { 85 return LibClang.CXStrToString(LibClang.lib.clang_getTypeSpelling(type)); 86 } 87 88 public int kind1() { 89 return type.kind$get(); 90 } 91 92 public long size() { 93 return LibClang.lib.clang_Type_getSizeOf(type); 94 } 95 96 public TypeKind kind() { 97 int v = kind1(); 98 return TypeKind.valueOf(v); 99 } 100 101 public Cursor getDeclarationCursor() { 102 return new Cursor(LibClang.lib.clang_getTypeDeclaration(type)); 103 } 104 105 @Override 106 public boolean equals(Object other) { 107 if (this == other) { 108 return true; 109 } 110 if (!(other instanceof Type)) { 111 return false; 112 } 113 return LibClang.lib.clang_equalTypes(type, ((Type)other).type) != 0; 114 } 115 116 @Override 117 public int hashCode() { 118 return spelling().hashCode(); 119 } 120 }