slang_rs_export_type.cpp revision 37dbf5c324322fe2458e63e3fa869fbf903ec6fd
1c383a500aa59423264811be3874461bf8adbfea0Zonr Chang/*
2d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines * Copyright 2010-2012, The Android Open Source Project
3c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
4c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Licensed under the Apache License, Version 2.0 (the "License");
5c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * you may not use this file except in compliance with the License.
6c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * You may obtain a copy of the License at
7c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
8c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *     http://www.apache.org/licenses/LICENSE-2.0
9c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
10c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Unless required by applicable law or agreed to in writing, software
11c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * distributed under the License is distributed on an "AS IS" BASIS,
12c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * See the License for the specific language governing permissions and
14c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * limitations under the License.
15c383a500aa59423264811be3874461bf8adbfea0Zonr Chang */
16c383a500aa59423264811be3874461bf8adbfea0Zonr Chang
176315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include "slang_rs_export_type.h"
186315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr
19e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include <list>
206315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include <vector>
21462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2243730fe3c839af391efe6bdf56b0479860121924Shih-wei Liao#include "clang/AST/ASTContext.h"
2323c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "clang/AST/Attr.h"
24e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/AST/RecordLayout.h"
25462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
269ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#include "llvm/ADT/StringExtras.h"
2723c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/DataLayout.h"
2823c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/DerivedTypes.h"
2923c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/Type.h"
300a3f20ec28ed6f5ae1ed5d61f6b6e3e577f7f5d1Shih-wei Liao
316e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines#include "slang_assert.h"
326315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include "slang_rs_context.h"
336315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include "slang_rs_export_element.h"
34d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines#include "slang_version.h"
35462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
36641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang#define CHECK_PARENT_EQUALITY(ParentClass, E) \
37ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  if (!ParentClass::matchODR(E, true))        \
38641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang    return false;
39641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
40e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hinesnamespace slang {
41462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
42e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hinesnamespace {
43462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
447682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala// For the data types we support:
457682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  Category      - data type category
46277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross//  SName         - "common name" in script (C99)
477682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  RsType        - element name in RenderScript
487682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  RsShortType   - short element name in RenderScript
497682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  SizeInBits    - size in bits
507682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  CName         - reflected C name
517682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  JavaName      - reflected Java name
527682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  JavaArrayElementName - reflected name in Java arrays
537682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  CVecName      - prefix for C vector types
547682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  JavaVecName   - prefix for Java vector type
557682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//  JavaPromotion - unsigned type undergoing Java promotion
567682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//
577682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala// IMPORTANT: The data types in this table should be at the same index as
587682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala// specified by the corresponding DataType enum.
597682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala//
607682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala// TODO: Pull this information out into a separate file.
61fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hinesstatic RSReflectionType gReflectionTypes[] = {
627682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala#define _ nullptr
63277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross  //      Category     SName              RsType       RsST           CName         JN      JAEN       CVN       JVN     JP
642d7f3f95947889e1ea48b0e925c5929af4fbf6b4David Gross{PrimitiveDataType,   "half",         "FLOAT_16",     "F16", 16,     "half",   "short",  "short",   "Half",  "Short", false},
65277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,  "float",         "FLOAT_32",     "F32", 32,    "float",   "float",  "float",  "Float",  "Float", false},
66277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType, "double",         "FLOAT_64",     "F64", 64,   "double",  "double", "double", "Double", "Double", false},
67277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,   "char",         "SIGNED_8",      "I8",  8,   "int8_t",    "byte",   "byte",   "Byte",   "Byte", false},
68277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,  "short",        "SIGNED_16",     "I16", 16,  "int16_t",   "short",  "short",  "Short",  "Short", false},
69277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,    "int",        "SIGNED_32",     "I32", 32,  "int32_t",     "int",    "int",    "Int",    "Int", false},
70277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,   "long",        "SIGNED_64",     "I64", 64,  "int64_t",    "long",   "long",   "Long",   "Long", false},
71277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,  "uchar",       "UNSIGNED_8",      "U8",  8,  "uint8_t",   "short",   "byte",  "UByte",  "Short",  true},
72277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType, "ushort",      "UNSIGNED_16",     "U16", 16, "uint16_t",     "int",  "short", "UShort",    "Int",  true},
73277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,   "uint",      "UNSIGNED_32",     "U32", 32, "uint32_t",    "long",    "int",   "UInt",   "Long",  true},
74277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,  "ulong",      "UNSIGNED_64",     "U64", 64, "uint64_t",    "long",   "long",  "ULong",   "Long", false},
75277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,   "bool",          "BOOLEAN", "BOOLEAN",  8,     "bool", "boolean",   "byte",        _,        _, false},
76277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,        _,   "UNSIGNED_5_6_5",         _, 16,          _,         _,        _,        _,        _, false},
77277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,        _, "UNSIGNED_5_5_5_1",         _, 16,          _,         _,        _,        _,        _, false},
78277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{PrimitiveDataType,        _, "UNSIGNED_4_4_4_4",         _, 16,          _,         _,        _,        _,        _, false},
797682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala
80277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{MatrixDataType, "rs_matrix2x2", "MATRIX_2X2", _,  4*32, "rs_matrix2x2", "Matrix2f", _, _, _, false},
81277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{MatrixDataType, "rs_matrix3x3", "MATRIX_3X3", _,  9*32, "rs_matrix3x3", "Matrix3f", _, _, _, false},
82277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{MatrixDataType, "rs_matrix4x4", "MATRIX_4X4", _, 16*32, "rs_matrix4x4", "Matrix4f", _, _, _, false},
837682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala
847682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala// RS object types are 32 bits in 32-bit RS, but 256 bits in 64-bit RS.
85657d20afa621219c1eed72178d1325fd4409f458David Gross// This is handled specially by the GetElementSizeInBits() method.
86277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,          "RS_ELEMENT",          "ELEMENT", 32,         "Element",         "Element", _, _, _, false},
87277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,             "RS_TYPE",             "TYPE", 32,            "Type",            "Type", _, _, _, false},
88277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,       "RS_ALLOCATION",       "ALLOCATION", 32,      "Allocation",      "Allocation", _, _, _, false},
89277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,          "RS_SAMPLER",          "SAMPLER", 32,         "Sampler",         "Sampler", _, _, _, false},
90277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,           "RS_SCRIPT",           "SCRIPT", 32,          "Script",          "Script", _, _, _, false},
91277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,             "RS_MESH",             "MESH", 32,            "Mesh",            "Mesh", _, _, _, false},
92277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,             "RS_PATH",             "PATH", 32,            "Path",            "Path", _, _, _, false},
93277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _, "RS_PROGRAM_FRAGMENT", "PROGRAM_FRAGMENT", 32, "ProgramFragment", "ProgramFragment", _, _, _, false},
94277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,   "RS_PROGRAM_VERTEX",   "PROGRAM_VERTEX", 32,   "ProgramVertex",   "ProgramVertex", _, _, _, false},
95277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,   "RS_PROGRAM_RASTER",   "PROGRAM_RASTER", 32,   "ProgramRaster",   "ProgramRaster", _, _, _, false},
96277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,    "RS_PROGRAM_STORE",    "PROGRAM_STORE", 32,    "ProgramStore",    "ProgramStore", _, _, _, false},
97277fd5e6545c8ba1272027ee6e6bc55a96316dc0David Gross{ObjectDataType, _,             "RS_FONT",             "FONT", 32,            "Font",            "Font", _, _, _, false},
987682b663581dd8f67b422f6f2f31692ab2f870e3Matt Wala#undef _
99474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet};
100474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet
101474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouilletconst int kMaxVectorSize = 4;
102474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet
103474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouilletstruct BuiltinInfo {
104474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  clang::BuiltinType::Kind builtinTypeKind;
105cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet  DataType type;
106474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  /* TODO If we return std::string instead of llvm::StringRef, we could build
107474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet   * the name instead of duplicating the entries.
108474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet   */
109474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  const char *cname[kMaxVectorSize];
110474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet};
111474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet
112cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet
113474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc BrouilletBuiltinInfo BuiltinInfoTable[] = {
114cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Bool, DataTypeBoolean,
115474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"bool", "bool2", "bool3", "bool4"}},
116cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Char_U, DataTypeUnsigned8,
117474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"uchar", "uchar2", "uchar3", "uchar4"}},
118cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::UChar, DataTypeUnsigned8,
119474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"uchar", "uchar2", "uchar3", "uchar4"}},
120cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Char16, DataTypeSigned16,
121474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"short", "short2", "short3", "short4"}},
122cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Char32, DataTypeSigned32,
123474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"int", "int2", "int3", "int4"}},
124cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::UShort, DataTypeUnsigned16,
125474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"ushort", "ushort2", "ushort3", "ushort4"}},
126cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::UInt, DataTypeUnsigned32,
127474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"uint", "uint2", "uint3", "uint4"}},
128e46265d27df092fbd911075403ec04d9c7ef8de1Tim Murray    {clang::BuiltinType::ULong, DataTypeUnsigned64,
129e46265d27df092fbd911075403ec04d9c7ef8de1Tim Murray     {"ulong", "ulong2", "ulong3", "ulong4"}},
130cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::ULongLong, DataTypeUnsigned64,
131474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"ulong", "ulong2", "ulong3", "ulong4"}},
132474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet
133cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Char_S, DataTypeSigned8,
134474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"char", "char2", "char3", "char4"}},
135cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::SChar, DataTypeSigned8,
136474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"char", "char2", "char3", "char4"}},
137cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Short, DataTypeSigned16,
138474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"short", "short2", "short3", "short4"}},
139cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Int, DataTypeSigned32,
140474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"int", "int2", "int3", "int4"}},
141cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Long, DataTypeSigned64,
142474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"long", "long2", "long3", "long4"}},
143cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::LongLong, DataTypeSigned64,
144474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"long", "long2", "long3", "long4"}},
145e4dd17d7b2a292a600756da7680beecd78f74033Pirama Arumuga Nainar    {clang::BuiltinType::Half, DataTypeFloat16,
146e4dd17d7b2a292a600756da7680beecd78f74033Pirama Arumuga Nainar     {"half", "half2", "half3", "half4"}},
147cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Float, DataTypeFloat32,
148474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"float", "float2", "float3", "float4"}},
149cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {clang::BuiltinType::Double, DataTypeFloat64,
150474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet     {"double", "double2", "double3", "double4"}},
151474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet};
152474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouilletconst int BuiltinInfoTableCount = sizeof(BuiltinInfoTable) / sizeof(BuiltinInfoTable[0]);
153474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet
154474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouilletstruct NameAndPrimitiveType {
155474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  const char *name;
156cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet  DataType dataType;
157474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet};
158474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet
159474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouilletstatic NameAndPrimitiveType MatrixAndObjectDataTypes[] = {
160cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_matrix2x2", DataTypeRSMatrix2x2},
161cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_matrix3x3", DataTypeRSMatrix3x3},
162cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_matrix4x4", DataTypeRSMatrix4x4},
163cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_element", DataTypeRSElement},
164cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_type", DataTypeRSType},
165cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_allocation", DataTypeRSAllocation},
166cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_sampler", DataTypeRSSampler},
167cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_script", DataTypeRSScript},
168cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_mesh", DataTypeRSMesh},
169cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_path", DataTypeRSPath},
170cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_program_fragment", DataTypeRSProgramFragment},
171cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_program_vertex", DataTypeRSProgramVertex},
172cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_program_raster", DataTypeRSProgramRaster},
173cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_program_store", DataTypeRSProgramStore},
174cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    {"rs_font", DataTypeRSFont},
175fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines};
176fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines
177474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouilletconst int MatrixAndObjectDataTypesCount =
178474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    sizeof(MatrixAndObjectDataTypes) / sizeof(MatrixAndObjectDataTypes[0]);
179474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet
18024e79f69125cf87fcaa78c04510a831037203eebStephen Hinesstatic const clang::Type *TypeExportableHelper(
18124e79f69125cf87fcaa78c04510a831037203eebStephen Hines    const clang::Type *T,
18224e79f69125cf87fcaa78c04510a831037203eebStephen Hines    llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
18348d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    slang::RSContext *Context,
18424e79f69125cf87fcaa78c04510a831037203eebStephen Hines    const clang::VarDecl *VD,
18513fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines    const clang::RecordDecl *TopLevelRecord,
18613fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines    ExportKind EK);
18724e79f69125cf87fcaa78c04510a831037203eebStephen Hines
188ee4016d1247d3fbe50822de279d3da273d8aef4cTim Murraytemplate <unsigned N>
189d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouilletstatic void ReportTypeError(slang::RSContext *Context,
19011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines                            const clang::NamedDecl *ND,
19124e79f69125cf87fcaa78c04510a831037203eebStephen Hines                            const clang::RecordDecl *TopLevelRecord,
192ee4016d1247d3fbe50822de279d3da273d8aef4cTim Murray                            const char (&Message)[N],
193d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines                            unsigned int TargetAPI = 0) {
19424e79f69125cf87fcaa78c04510a831037203eebStephen Hines  // Attempt to use the type declaration first (if we have one).
19524e79f69125cf87fcaa78c04510a831037203eebStephen Hines  // Fall back to the variable definition, if we are looking at something
19624e79f69125cf87fcaa78c04510a831037203eebStephen Hines  // like an array declaration that can't be exported.
19724e79f69125cf87fcaa78c04510a831037203eebStephen Hines  if (TopLevelRecord) {
198d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet    Context->ReportError(TopLevelRecord->getLocation(), Message)
199d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        << TopLevelRecord->getName() << TargetAPI;
20011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines  } else if (ND) {
201d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet    Context->ReportError(ND->getLocation(), Message) << ND->getName()
202d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                                                     << TargetAPI;
20324e79f69125cf87fcaa78c04510a831037203eebStephen Hines  } else {
2046e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines    slangAssert(false && "Variables should be validated before exporting");
20524e79f69125cf87fcaa78c04510a831037203eebStephen Hines  }
20624e79f69125cf87fcaa78c04510a831037203eebStephen Hines}
20724e79f69125cf87fcaa78c04510a831037203eebStephen Hines
20824e79f69125cf87fcaa78c04510a831037203eebStephen Hinesstatic const clang::Type *ConstantArrayTypeExportableHelper(
20924e79f69125cf87fcaa78c04510a831037203eebStephen Hines    const clang::ConstantArrayType *CAT,
21024e79f69125cf87fcaa78c04510a831037203eebStephen Hines    llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
21148d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    slang::RSContext *Context,
21224e79f69125cf87fcaa78c04510a831037203eebStephen Hines    const clang::VarDecl *VD,
21313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines    const clang::RecordDecl *TopLevelRecord,
21413fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines    ExportKind EK) {
21524e79f69125cf87fcaa78c04510a831037203eebStephen Hines  // Check element type
216b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *ElementType = GetConstantArrayElementType(CAT);
21724e79f69125cf87fcaa78c04510a831037203eebStephen Hines  if (ElementType->isArrayType()) {
218d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet    ReportTypeError(Context, VD, TopLevelRecord,
2199207a2e495c8363606861e4f034504ec5c153dabLogan Chien                    "multidimensional arrays cannot be exported: '%0'");
2205abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
22124e79f69125cf87fcaa78c04510a831037203eebStephen Hines  } else if (ElementType->isExtVectorType()) {
22224e79f69125cf87fcaa78c04510a831037203eebStephen Hines    const clang::ExtVectorType *EVT =
22324e79f69125cf87fcaa78c04510a831037203eebStephen Hines        static_cast<const clang::ExtVectorType*>(ElementType);
22424e79f69125cf87fcaa78c04510a831037203eebStephen Hines    unsigned numElements = EVT->getNumElements();
22524e79f69125cf87fcaa78c04510a831037203eebStephen Hines
226b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet    const clang::Type *BaseElementType = GetExtVectorElementType(EVT);
22724e79f69125cf87fcaa78c04510a831037203eebStephen Hines    if (!RSExportPrimitiveType::IsPrimitiveType(BaseElementType)) {
228d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      ReportTypeError(Context, VD, TopLevelRecord,
2299207a2e495c8363606861e4f034504ec5c153dabLogan Chien        "vectors of non-primitive types cannot be exported: '%0'");
2305abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
23124e79f69125cf87fcaa78c04510a831037203eebStephen Hines    }
23224e79f69125cf87fcaa78c04510a831037203eebStephen Hines
23324e79f69125cf87fcaa78c04510a831037203eebStephen Hines    if (numElements == 3 && CAT->getSize() != 1) {
234d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      ReportTypeError(Context, VD, TopLevelRecord,
2359207a2e495c8363606861e4f034504ec5c153dabLogan Chien        "arrays of width 3 vector types cannot be exported: '%0'");
2365abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
23724e79f69125cf87fcaa78c04510a831037203eebStephen Hines    }
23824e79f69125cf87fcaa78c04510a831037203eebStephen Hines  }
23924e79f69125cf87fcaa78c04510a831037203eebStephen Hines
24048d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines  if (TypeExportableHelper(ElementType, SPS, Context, VD,
24113fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                           TopLevelRecord, EK) == nullptr) {
2425abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
243d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  } else {
24424e79f69125cf87fcaa78c04510a831037203eebStephen Hines    return CAT;
245d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  }
24624e79f69125cf87fcaa78c04510a831037203eebStephen Hines}
24724e79f69125cf87fcaa78c04510a831037203eebStephen Hines
248474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc BrouilletBuiltinInfo *FindBuiltinType(clang::BuiltinType::Kind builtinTypeKind) {
249474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  for (int i = 0; i < BuiltinInfoTableCount; i++) {
250474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    if (builtinTypeKind == BuiltinInfoTable[i].builtinTypeKind) {
251474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet      return &BuiltinInfoTable[i];
252474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    }
253474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  }
2545abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  return nullptr;
255474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet}
256474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet
25724e79f69125cf87fcaa78c04510a831037203eebStephen Hinesstatic const clang::Type *TypeExportableHelper(
258e67239de8d94975e7e2216ee6860ae2e6cb8b15aStephen Hines    clang::Type const *T,
259e67239de8d94975e7e2216ee6860ae2e6cb8b15aStephen Hines    llvm::SmallPtrSet<clang::Type const *, 8> &SPS,
26048d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    slang::RSContext *Context,
261e67239de8d94975e7e2216ee6860ae2e6cb8b15aStephen Hines    clang::VarDecl const *VD,
26213fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines    clang::RecordDecl const *TopLevelRecord,
26313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines    ExportKind EK) {
2649ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Normalize first
2655abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if ((T = GetCanonicalType(T)) == nullptr)
2665abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
267462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2689ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (SPS.count(T))
2699ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return T;
2701f0d88fbff28e4e2dd563d93c8fe0047381c09ccShih-wei Liao
271b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
272b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet
2739ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  switch (T->getTypeClass()) {
2749ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::Builtin: {
275b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI);
2765abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return FindBuiltinType(BT->getKind()) == nullptr ? nullptr : T;
2779ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
2789ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::Record: {
279cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet      if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
2806315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr        return T;  // RS object type, no further checks are needed
281d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines      }
282462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2839ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Check internal struct
284cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines      if (T->isUnionType()) {
285d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        ReportTypeError(Context, VD, T->getAsUnionType()->getDecl(),
2869207a2e495c8363606861e4f034504ec5c153dabLogan Chien                        "unions cannot be exported: '%0'");
2875abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
288cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines      } else if (!T->isStructureType()) {
2896e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines        slangAssert(false && "Unknown type cannot be exported");
2905abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
291cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines      }
292cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines
29324e79f69125cf87fcaa78c04510a831037203eebStephen Hines      clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
2945abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      if (RD != nullptr) {
2959ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        RD = RD->getDefinition();
2965abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        if (RD == nullptr) {
2975abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes          ReportTypeError(Context, nullptr, T->getAsStructureType()->getDecl(),
2989207a2e495c8363606861e4f034504ec5c153dabLogan Chien                          "struct is not defined in this module");
2995abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes          return nullptr;
3002ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines        }
3012ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines      }
302f8149d9e5a3795e9952717ee6346789a134c55c7Shih-wei Liao
303e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      if (!TopLevelRecord) {
304e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines        TopLevelRecord = RD;
305e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      }
306e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      if (RD->getName().empty()) {
3075abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        ReportTypeError(Context, nullptr, RD,
3089207a2e495c8363606861e4f034504ec5c153dabLogan Chien                        "anonymous structures cannot be exported");
3095abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
310e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      }
311e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
3129ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Fast check
3139ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
3145abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
315f8149d9e5a3795e9952717ee6346789a134c55c7Shih-wei Liao
3169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Insert myself into checking set
3179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      SPS.insert(T);
318f8149d9e5a3795e9952717ee6346789a134c55c7Shih-wei Liao
3199ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Check all element
3209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
3219ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao               FE = RD->field_end();
3229ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao           FI != FE;
3239ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao           FI++) {
3240da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang        const clang::FieldDecl *FD = *FI;
325e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines        const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
326b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet        FT = GetCanonicalType(FT);
327462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
32813fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines        if (!TypeExportableHelper(FT, SPS, Context, VD, TopLevelRecord,
32913fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                  EK)) {
3305abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes          return nullptr;
3310da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang        }
3322ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines
3332ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines        // We don't support bit fields yet
3342ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines        //
3352ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines        // TODO(zonr/srhines): allow bit fields of size 8, 16, 32
3362ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines        if (FD->isBitField()) {
337d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          Context->ReportError(
338d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet              FD->getLocation(),
339d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet              "bit fields are not able to be exported: '%0.%1'")
340d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet              << RD->getName() << FD->getName();
3415abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes          return nullptr;
3422ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines        }
3439ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      }
344462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3459ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      return T;
3469ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
3472907b2a2768bc32f75867513528c8d7419e44780David Gross    case clang::Type::FunctionProto:
348281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross    case clang::Type::FunctionNoProto:
3492907b2a2768bc32f75867513528c8d7419e44780David Gross      ReportTypeError(Context, VD, TopLevelRecord,
3502907b2a2768bc32f75867513528c8d7419e44780David Gross                      "function types cannot be exported: '%0'");
3512907b2a2768bc32f75867513528c8d7419e44780David Gross      return nullptr;
3529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::Pointer: {
353e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      if (TopLevelRecord) {
354d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        ReportTypeError(Context, VD, TopLevelRecord,
355ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines            "structures containing pointers cannot be used as the type of "
356ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines            "an exported global variable or the parameter to an exported "
357ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines            "function: '%0'");
3585abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
359c808a99831115928b4648f4c8b86dc682594217aStephen Hines      }
36024e79f69125cf87fcaa78c04510a831037203eebStephen Hines
361b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
362b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *PointeeType = GetPointeeType(PT);
363462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
364aa82e74b08fcdc2e4e6c1f6796699566b331b656Stephen Hines      if (PointeeType->getTypeClass() == clang::Type::Pointer) {
365d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        ReportTypeError(Context, VD, TopLevelRecord,
366aa82e74b08fcdc2e4e6c1f6796699566b331b656Stephen Hines            "multiple levels of pointers cannot be exported: '%0'");
3675abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
368aa82e74b08fcdc2e4e6c1f6796699566b331b656Stephen Hines      }
36913fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines
37013fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      // Void pointers are forbidden for export, although we must accept
37113fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      // void pointers that come in as arguments to a legacy kernel.
37213fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      if (PointeeType->isVoidType() && EK != LegacyKernelArgument) {
37313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines        ReportTypeError(Context, VD, TopLevelRecord,
37413fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines            "void pointers cannot be exported: '%0'");
37513fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines        return nullptr;
37613fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      }
37713fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines
378281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross      // We don't support pointer with array-type pointee
379281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross      if (PointeeType->isArrayType()) {
380281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross        ReportTypeError(Context, VD, TopLevelRecord,
381281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross            "pointers to arrays cannot be exported: '%0'");
382281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross        return nullptr;
383281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross      }
384281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross
385281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross      // Check for unsupported pointee type
386281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross      if (TypeExportableHelper(PointeeType, SPS, Context, VD,
387281b83d4404ed63f7f3c82647d926e8aaf3380c8David Gross                                TopLevelRecord, EK) == nullptr)
3885abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
3899ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      else
3909ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        return T;
3919ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
3929ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::ExtVector: {
3939ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      const clang::ExtVectorType *EVT =
394b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              static_cast<const clang::ExtVectorType*>(CTI);
3959ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Only vector with size 2, 3 and 4 are supported.
3969ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      if (EVT->getNumElements() < 2 || EVT->getNumElements() > 4)
3975abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
398f8149d9e5a3795e9952717ee6346789a134c55c7Shih-wei Liao
3999ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Check base element type
400b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *ElementType = GetExtVectorElementType(EVT);
401462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
4029ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      if ((ElementType->getTypeClass() != clang::Type::Builtin) ||
40348d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines          (TypeExportableHelper(ElementType, SPS, Context, VD,
40413fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                TopLevelRecord, EK) == nullptr))
4055abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
4069ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      else
4079ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        return T;
4089ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
4092e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    case clang::Type::ConstantArray: {
4102e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang      const clang::ConstantArrayType *CAT =
411b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              static_cast<const clang::ConstantArrayType*>(CTI);
4122e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
41348d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      return ConstantArrayTypeExportableHelper(CAT, SPS, Context, VD,
41413fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                               TopLevelRecord, EK);
4152e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    }
41648d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    case clang::Type::Enum: {
41748d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      // FIXME: We currently convert enums to integers, rather than reflecting
41848d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      // a more complete (and nicer type-safe Java version).
41948d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      return Context->getASTContext().IntTy.getTypePtr();
42048d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    }
4219ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    default: {
42248d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      slangAssert(false && "Unknown type cannot be validated");
4235abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
424462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    }
4259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
4269ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao}
427462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
428e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines// Return the type that can be used to create RSExportType, will always return
4295abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// the canonical type.
4305abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes//
4315abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// If the Type T is not exportable, this function returns nullptr. DiagEngine is
4325abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// used to generate proper Clang diagnostic messages when a non-exportable type
4335abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// is detected. TopLevelRecord is used to capture the highest struct (in the
4345abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// case of a nested hierarchy) for detecting other types that cannot be exported
4355abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// (mostly pointers within a struct).
436e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hinesstatic const clang::Type *TypeExportable(const clang::Type *T,
43748d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines                                         slang::RSContext *Context,
43813fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                         const clang::VarDecl *VD,
43913fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                         ExportKind EK) {
440e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  llvm::SmallPtrSet<const clang::Type*, 8> SPS =
441e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      llvm::SmallPtrSet<const clang::Type*, 8>();
442e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
44313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines  return TypeExportableHelper(T, SPS, Context, VD, nullptr, EK);
44478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines}
44578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
446d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouilletstatic bool ValidateRSObjectInVarDecl(slang::RSContext *Context,
4472907b2a2768bc32f75867513528c8d7419e44780David Gross                                      const clang::VarDecl *VD, bool InCompositeType,
448d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines                                      unsigned int TargetAPI) {
449d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  if (TargetAPI < SLANG_JB_TARGET_API) {
450d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    // Only if we are already in a composite type (like an array or structure).
451d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    if (InCompositeType) {
452d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines      // Only if we are actually exported (i.e. non-static).
45344f10063c2c08dab103a44cded0c3a288d65d43bStephen Hines      if (VD->hasLinkage() &&
45444f10063c2c08dab103a44cded0c3a288d65d43bStephen Hines          (VD->getFormalLinkage() == clang::ExternalLinkage)) {
455d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines        // Only if we are not a pointer to an object.
456b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet        const clang::Type *T = GetCanonicalType(VD->getType().getTypePtr());
457d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines        if (T->getTypeClass() != clang::Type::Pointer) {
4585abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes          ReportTypeError(Context, VD, nullptr,
459d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines                          "arrays/structures containing RS object types "
460d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines                          "cannot be exported in target API < %1: '%0'",
461d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines                          SLANG_JB_TARGET_API);
462d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines          return false;
463d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines        }
464d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines      }
465d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    }
466d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  }
467d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines
468d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  return true;
469d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines}
470d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines
47111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// Helper function for ValidateType(). We do a recursive descent on the
472d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// type hierarchy to ensure that we can properly export/handle the
473d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// declaration.
474d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// \return true if the variable declaration is valid,
475d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines//         false if it is invalid (along with proper diagnostics).
476d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines//
47711274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// C - ASTContext (for diagnostics + builtin types).
47811274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// T - sub-type that we are validating.
47911274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// ND - (optional) top-level named declaration that we are validating.
480d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// SPS - set of types we have already seen/validated.
481d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// InCompositeType - true if we are within an outer composite type.
482d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// UnionDecl - set if we are in a sub-type of a union.
483d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// TargetAPI - target SDK API level.
48411274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// IsFilterscript - whether or not we are compiling for Filterscript
485ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines// IsExtern - is this type externally visible (i.e. extern global or parameter
486ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines//                                             to an extern function)
48711274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hinesstatic bool ValidateTypeHelper(
488d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet    slang::RSContext *Context,
48911274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines    clang::ASTContext &C,
49078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    const clang::Type *&T,
4912907b2a2768bc32f75867513528c8d7419e44780David Gross    const clang::NamedDecl *ND,
49211274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines    clang::SourceLocation Loc,
49378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
494d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    bool InCompositeType,
495d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    clang::RecordDecl *UnionDecl,
49611274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines    unsigned int TargetAPI,
497ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines    bool IsFilterscript,
498ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines    bool IsExtern) {
4995abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if ((T = GetCanonicalType(T)) == nullptr)
50078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    return true;
50178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
50278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  if (SPS.count(T))
50378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    return true;
50478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
505b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
506b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet
50778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  switch (T->getTypeClass()) {
50878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::Record: {
509d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines      if (RSExportPrimitiveType::IsRSObjectType(T)) {
5102907b2a2768bc32f75867513528c8d7419e44780David Gross        const clang::VarDecl *VD = (ND ? llvm::dyn_cast<clang::VarDecl>(ND) : nullptr);
511d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        if (VD && !ValidateRSObjectInVarDecl(Context, VD, InCompositeType,
512d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                                             TargetAPI)) {
513d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines          return false;
514d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines        }
515d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines      }
516d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines
517cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet      if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
51878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        if (!UnionDecl) {
51978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          return true;
52078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        } else if (RSExportPrimitiveType::IsRSObjectType(T)) {
5215abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes          ReportTypeError(Context, nullptr, UnionDecl,
52278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines              "unions containing RS object types are not allowed");
52378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          return false;
52478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        }
52578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      }
52678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
5275abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      clang::RecordDecl *RD = nullptr;
52878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
52978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      // Check internal struct
53078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      if (T->isUnionType()) {
53178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        RD = T->getAsUnionType()->getDecl();
53278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        UnionDecl = RD;
53378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      } else if (T->isStructureType()) {
53478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        RD = T->getAsStructureType()->getDecl();
53578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      } else {
53678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        slangAssert(false && "Unknown type cannot be exported");
53778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        return false;
53878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      }
53978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
5405abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      if (RD != nullptr) {
54178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        RD = RD->getDefinition();
5425abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        if (RD == nullptr) {
54378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          // FIXME
54478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          return true;
54578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        }
54678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      }
54778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
54878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      // Fast check
54978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
55078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        return false;
55178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
55278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      // Insert myself into checking set
55378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      SPS.insert(T);
55478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
55578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      // Check all elements
55678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
55778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines               FE = RD->field_end();
55878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines           FI != FE;
55978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines           FI++) {
56078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        const clang::FieldDecl *FD = *FI;
56178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
562b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet        FT = GetCanonicalType(FT);
56378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
564d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        if (!ValidateTypeHelper(Context, C, FT, ND, Loc, SPS, true, UnionDecl,
565ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                TargetAPI, IsFilterscript, IsExtern)) {
56678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          return false;
56778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        }
56878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      }
56978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
57078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      return true;
57178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
57278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
57378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::Builtin: {
57411274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines      if (IsFilterscript) {
57511274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        clang::QualType QT = T->getCanonicalTypeInternal();
57611274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        if (QT == C.DoubleTy ||
57711274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines            QT == C.LongDoubleTy ||
57811274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines            QT == C.LongTy ||
57911274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines            QT == C.LongLongTy) {
58011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          if (ND) {
581d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet            Context->ReportError(
582d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                Loc,
58311274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines                "Builtin types > 32 bits in size are forbidden in "
584d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                "Filterscript: '%0'")
585d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                << ND->getName();
58611274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          } else {
587d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet            Context->ReportError(
588d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                Loc,
58911274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines                "Builtin types > 32 bits in size are forbidden in "
590d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                "Filterscript");
59111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          }
59211274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          return false;
59311274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        }
59411274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines      }
59578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      break;
59678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
59778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
59878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::Pointer: {
59911274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines      if (IsFilterscript) {
60011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        if (ND) {
601d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          Context->ReportError(Loc,
602d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                               "Pointers are forbidden in Filterscript: '%0'")
603d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet              << ND->getName();
60411274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          return false;
60511274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        } else {
60611274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          // TODO(srhines): Find a better way to handle expressions (i.e. no
60711274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          // NamedDecl) involving pointers in FS that should be allowed.
60811274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          // An example would be calls to library functions like
60911274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          // rsMatrixMultiply() that take rs_matrixNxN * types.
61011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        }
61111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines      }
61211274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines
613ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines      // Forbid pointers in structures that are externally visible.
614ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines      if (InCompositeType && IsExtern) {
615ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        if (ND) {
616ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines          Context->ReportError(Loc,
617ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines              "structures containing pointers cannot be used as the type of "
618ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines              "an exported global variable or the parameter to an exported "
619ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines              "function: '%0'")
620ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines            << ND->getName();
621ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        } else {
622ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines          Context->ReportError(Loc,
623ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines              "structures containing pointers cannot be used as the type of "
624ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines              "an exported global variable or the parameter to an exported "
625ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines              "function");
626ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        }
627ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        return false;
628ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines      }
629ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines
630b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
631b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *PointeeType = GetPointeeType(PT);
63278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
633d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      return ValidateTypeHelper(Context, C, PointeeType, ND, Loc, SPS,
634d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                                InCompositeType, UnionDecl, TargetAPI,
635ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                IsFilterscript, IsExtern);
63678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
63778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
63878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::ExtVector: {
63978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      const clang::ExtVectorType *EVT =
640b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              static_cast<const clang::ExtVectorType*>(CTI);
641b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *ElementType = GetExtVectorElementType(EVT);
642b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines      if (TargetAPI < SLANG_ICS_TARGET_API &&
643b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines          InCompositeType &&
644fdae63e8142f56521813f59e0c506ed3a1636021Stephen Hines          EVT->getNumElements() == 3 &&
645fdae63e8142f56521813f59e0c506ed3a1636021Stephen Hines          ND &&
64644f10063c2c08dab103a44cded0c3a288d65d43bStephen Hines          ND->getFormalLinkage() == clang::ExternalLinkage) {
6475abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        ReportTypeError(Context, ND, nullptr,
648b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines                        "structs containing vectors of dimension 3 cannot "
649b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines                        "be exported at this API level: '%0'");
650b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines        return false;
651b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines      }
652d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
653ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                UnionDecl, TargetAPI, IsFilterscript, IsExtern);
65478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
65578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
65678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::ConstantArray: {
657b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::ConstantArrayType *CAT = static_cast<const clang::ConstantArrayType*>(CTI);
658b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *ElementType = GetConstantArrayElementType(CAT);
659d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
660ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                UnionDecl, TargetAPI, IsFilterscript, IsExtern);
66178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
66278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
66378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    default: {
66478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      break;
66578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
66678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  }
66778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
66878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  return true;
669e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines}
670e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
671e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines}  // namespace
672e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
673eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouilletstd::string CreateDummyName(const char *type, const std::string &name) {
674eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  std::stringstream S;
675eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  S << "<" << type;
676eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  if (!name.empty()) {
677eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet    S << ":" << name;
678eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  }
679eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  S << ">";
680eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  return S.str();
681eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet}
682eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet
683e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines/****************************** RSExportType ******************************/
684e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hinesbool RSExportType::NormalizeType(const clang::Type *&T,
685e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines                                 llvm::StringRef &TypeName,
68648d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines                                 RSContext *Context,
68713fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                 const clang::VarDecl *VD,
68813fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                 ExportKind EK) {
68913fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines  if ((T = TypeExportable(T, Context, VD, EK)) == nullptr) {
690e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    return false;
691e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  }
692e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // Get type name
693e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  TypeName = RSExportType::GetTypeName(T);
69448d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines  if (Context && TypeName.empty()) {
69548d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    if (VD) {
696d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError(VD->getLocation(),
697d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "anonymous types cannot be exported");
69848d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    } else {
699d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError("anonymous types cannot be exported");
700e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    }
701e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    return false;
702e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  }
703e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
704e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  return true;
705e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines}
706e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
707d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouilletbool RSExportType::ValidateType(slang::RSContext *Context, clang::ASTContext &C,
7082907b2a2768bc32f75867513528c8d7419e44780David Gross                                clang::QualType QT, const clang::NamedDecl *ND,
709d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                                clang::SourceLocation Loc,
710ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                unsigned int TargetAPI, bool IsFilterscript,
711ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                bool IsExtern) {
71211274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines  const clang::Type *T = QT.getTypePtr();
71378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  llvm::SmallPtrSet<const clang::Type*, 8> SPS =
71478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      llvm::SmallPtrSet<const clang::Type*, 8>();
71578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
716ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines  // If this is an externally visible variable declaration, we check if the
717ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines  // type is able to be exported first.
71833ea573b6df7b7fe48d2b68d4c479f33082e3c0dStephen Hines  if (auto VD = llvm::dyn_cast_or_null<clang::VarDecl>(ND)) {
719ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines    if (VD->getFormalLinkage() == clang::ExternalLinkage) {
72013fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      if (!TypeExportable(T, Context, VD, NotLegacyKernelArgument)) {
721ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        return false;
722ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines      }
723ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines    }
724ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines  }
7255abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  return ValidateTypeHelper(Context, C, T, ND, Loc, SPS, false, nullptr, TargetAPI,
726ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                            IsFilterscript, IsExtern);
72711274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines}
72811274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines
729d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouilletbool RSExportType::ValidateVarDecl(slang::RSContext *Context,
730d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                                   clang::VarDecl *VD, unsigned int TargetAPI,
73111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines                                   bool IsFilterscript) {
732d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet  return ValidateType(Context, VD->getASTContext(), VD->getType(), VD,
733ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                      VD->getLocation(), TargetAPI, IsFilterscript,
734ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                      (VD->getFormalLinkage() == clang::ExternalLinkage));
73578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines}
73678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
737e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hinesconst clang::Type
738e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines*RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) {
739e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  if (DD) {
7408de1922e037612f2521acac2f4c4289a9f71450dStephen Hines    clang::QualType T = DD->getType();
741e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
742e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    if (T.isNull())
7435abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
744e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    else
745e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      return T.getTypePtr();
746e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  }
7475abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  return nullptr;
748e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines}
749e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
750e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hinesllvm::StringRef RSExportType::GetTypeName(const clang::Type* T) {
751b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  T = GetCanonicalType(T);
7525abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if (T == nullptr)
753e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    return llvm::StringRef();
754e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
755b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
756b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet
757e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  switch (T->getTypeClass()) {
758e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    case clang::Type::Builtin: {
759b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI);
760474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet      BuiltinInfo *info = FindBuiltinType(BT->getKind());
7615abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      if (info != nullptr) {
762474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet        return info->cname[0];
763e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      }
764474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet      slangAssert(false && "Unknown data type of the builtin");
765e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      break;
766e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    }
767e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    case clang::Type::Record: {
768cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines      clang::RecordDecl *RD;
769cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines      if (T->isStructureType()) {
770cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines        RD = T->getAsStructureType()->getDecl();
771dd6206bb61bf8df2ed6b643abe8a29c48a315685Stephen Hines      } else {
772cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines        break;
773cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines      }
774cd440ee9b3bdb7985a20b2cd4b0f8229de34f0a9Stephen Hines
775e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      llvm::StringRef Name = RD->getName();
776e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      if (Name.empty()) {
7775abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        if (RD->getTypedefNameForAnonDecl() != nullptr) {
77883f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao          Name = RD->getTypedefNameForAnonDecl()->getName();
77983f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao        }
78083f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao
78183f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao        if (Name.empty()) {
78283f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao          // Try to find a name from redeclaration (i.e. typedef)
78383f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao          for (clang::TagDecl::redecl_iterator RI = RD->redecls_begin(),
78483f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao                   RE = RD->redecls_end();
78583f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao               RI != RE;
78683f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao               RI++) {
7875abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes            slangAssert(*RI != nullptr && "cannot be NULL object");
78883f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao
78983f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao            Name = (*RI)->getName();
79083f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao            if (!Name.empty())
79183f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao              break;
79283f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao          }
79383f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao        }
794e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      }
795e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      return Name;
796e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    }
797e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    case clang::Type::Pointer: {
798e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      // "*" plus pointee name
799b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::PointerType *P = static_cast<const clang::PointerType*>(CTI);
800b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *PT = GetPointeeType(P);
801e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      llvm::StringRef PointeeName;
80213fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      if (NormalizeType(PT, PointeeName, nullptr, nullptr,
80313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                        NotLegacyKernelArgument)) {
804e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines        char *Name = new char[ 1 /* * */ + PointeeName.size() + 1 ];
805e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines        Name[0] = '*';
806e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines        memcpy(Name + 1, PointeeName.data(), PointeeName.size());
807e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines        Name[PointeeName.size() + 1] = '\0';
808e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines        return Name;
809e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      }
810e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      break;
811e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    }
812e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    case clang::Type::ExtVector: {
813e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      const clang::ExtVectorType *EVT =
814b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              static_cast<const clang::ExtVectorType*>(CTI);
815e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      return RSExportVectorType::GetTypeName(EVT);
816e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      break;
817e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    }
818e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    case clang::Type::ConstantArray : {
819e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      // Construct name for a constant array is too complicated.
820340b5550cf63b6beae3b12c2e91377bce7704c34Stephen Hines      return "<ConstantArray>";
821e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    }
822e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    default: {
823e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      break;
824e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    }
825e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  }
826e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
827e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  return llvm::StringRef();
828e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines}
829e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
830e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
8319ef2f785e0cc490af678dfd685995dec787321ffShih-wei LiaoRSExportType *RSExportType::Create(RSContext *Context,
8329ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                   const clang::Type *T,
83313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                   const llvm::StringRef &TypeName,
83413fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                   ExportKind EK) {
8359ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Lookup the context to see whether the type was processed before.
8369ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Newly created RSExportType will insert into context
8379ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // in RSExportType::RSExportType()
8389ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  RSContext::export_type_iterator ETI = Context->findExportType(TypeName);
8399ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
8409ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (ETI != Context->export_types_end())
8419ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return ETI->second;
8429ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
843b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
844b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet
8455abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  RSExportType *ET = nullptr;
8469ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  switch (T->getTypeClass()) {
8479ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::Record: {
848cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet      DataType dt = RSExportPrimitiveType::GetRSSpecificType(TypeName);
8499ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      switch (dt) {
850cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet        case DataTypeUnknown: {
85192b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          // User-defined types
8529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          ET = RSExportRecordType::Create(Context,
8539ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                          T->getAsStructureType(),
8549ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                          TypeName);
8559ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          break;
8569ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        }
857cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet        case DataTypeRSMatrix2x2: {
85892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          // 2 x 2 Matrix type
85992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          ET = RSExportMatrixType::Create(Context,
86092b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                          T->getAsStructureType(),
86192b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                          TypeName,
86292b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                          2);
86392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          break;
86492b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang        }
865cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet        case DataTypeRSMatrix3x3: {
86692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          // 3 x 3 Matrix type
86792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          ET = RSExportMatrixType::Create(Context,
86892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                          T->getAsStructureType(),
86992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                          TypeName,
87092b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                          3);
87192b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          break;
87292b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang        }
873cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet        case DataTypeRSMatrix4x4: {
87492b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          // 4 x 4 Matrix type
87592b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          ET = RSExportMatrixType::Create(Context,
87692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                          T->getAsStructureType(),
87792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                          TypeName,
87892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                          4);
8799ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          break;
8809ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        }
8819ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        default: {
88292b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang          // Others are primitive types
8839ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          ET = RSExportPrimitiveType::Create(Context, T, TypeName);
8849ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          break;
8859ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        }
8869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      }
8879ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
8889ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
8899ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::Builtin: {
8909ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      ET = RSExportPrimitiveType::Create(Context, T, TypeName);
8919ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
8929ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
8939ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::Pointer: {
8949ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      ET = RSExportPointerType::Create(Context,
895b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet                                       static_cast<const clang::PointerType*>(CTI),
896b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet                                       TypeName);
89792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang      // FIXME: free the name (allocated in RSExportType::GetTypeName)
8989ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      delete [] TypeName.data();
8999ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
9009ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
9019ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::ExtVector: {
9029ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      ET = RSExportVectorType::Create(Context,
903b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet                                      static_cast<const clang::ExtVectorType*>(CTI),
904b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet                                      TypeName);
9059ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
9069ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
9072e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    case clang::Type::ConstantArray: {
9082e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang      ET = RSExportConstantArrayType::Create(
9092e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang              Context,
910b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              static_cast<const clang::ConstantArrayType*>(CTI));
9112e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang      break;
9122e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    }
9139ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    default: {
914d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError("unknown type cannot be exported: '%0'")
915d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          << T->getTypeClassName();
9169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
9179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
9189ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
9199ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
9209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return ET;
921462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
9221f0d88fbff28e4e2dd563d93c8fe0047381c09ccShih-wei Liao
92313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen HinesRSExportType *RSExportType::Create(RSContext *Context, const clang::Type *T,
9242907b2a2768bc32f75867513528c8d7419e44780David Gross                                   ExportKind EK, const clang::VarDecl *VD) {
9259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::StringRef TypeName;
9262907b2a2768bc32f75867513528c8d7419e44780David Gross  if (NormalizeType(T, TypeName, Context, VD, EK)) {
92713fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines    return Create(Context, T, TypeName, EK);
928d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  } else {
9295abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
930d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  }
931462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
932462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
9339ef2f785e0cc490af678dfd685995dec787321ffShih-wei LiaoRSExportType *RSExportType::CreateFromDecl(RSContext *Context,
9349ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                           const clang::VarDecl *VD) {
93513fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines  return RSExportType::Create(Context, GetTypeOfDecl(VD),
9362907b2a2768bc32f75867513528c8d7419e44780David Gross                              NotLegacyKernelArgument, VD);
937462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
938462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
939c95381a2c3b6e9117901eef0687e861e4d533bfeJean-Luc Brouilletsize_t RSExportType::getStoreSize() const {
940b6a143562bda2117824a0515c208504e9a2830f1Pirama Arumuga Nainar  return getRSContext()->getDataLayout().getTypeStoreSize(getLLVMType());
941462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
942462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
943c95381a2c3b6e9117901eef0687e861e4d533bfeJean-Luc Brouilletsize_t RSExportType::getAllocSize() const {
944b6a143562bda2117824a0515c208504e9a2830f1Pirama Arumuga Nainar    return getRSContext()->getDataLayout().getTypeAllocSize(getLLVMType());
945462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
946462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
9476b6320ad5faee29e0f75fe937e40156746ef9e80Zonr ChangRSExportType::RSExportType(RSContext *Context,
9486b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang                           ExportClass Class,
9496b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang                           const llvm::StringRef &Name)
950a41ce1d98094da84643995d40d71c529905123fcZonr Chang    : RSExportable(Context, RSExportable::EX_TYPE),
9516b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang      mClass(Class),
9520da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang      // Make a copy on Name since memory stored @Name is either allocated in
9530da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang      // ASTContext or allocated in GetTypeName which will be destroyed later.
9546315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mName(Name.data(), Name.size()),
9555abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      mLLVMType(nullptr) {
9560da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  // Don't cache the type whose name start with '<'. Those type failed to
9570da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  // get their name since constructing their name in GetTypeName() requiring
9580da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  // complicated work.
959b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  if (!IsDummyName(Name)) {
9600da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang    // TODO(zonr): Need to check whether the insertion is successful or not.
9610da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang    Context->insertExportType(llvm::StringRef(Name), this);
962b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  }
963b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet
964462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
965462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
9663cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Changbool RSExportType::keep() {
9673cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  if (!RSExportable::keep())
9683cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang    return false;
969641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  // Invalidate converted LLVM type.
9705abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  mLLVMType = nullptr;
9713cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  return true;
972641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
973641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
974ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sungbool RSExportType::matchODR(const RSExportType *E, bool /* LookInto */) const {
975ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  return (E->getClass() == getClass());
976641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
977641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
978a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr ChangRSExportType::~RSExportType() {
979a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang}
980a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang
9819ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao/************************** RSExportPrimitiveType **************************/
982b1771ef128b10c4d4575634828006bfba20b1d9cZonr Changllvm::ManagedStatic<RSExportPrimitiveType::RSSpecificTypeMapTy>
983b1771ef128b10c4d4575634828006bfba20b1d9cZonr ChangRSExportPrimitiveType::RSSpecificTypeMap;
9849ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
9859ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liaobool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) {
9865abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if ((T != nullptr) && (T->getTypeClass() == clang::Type::Builtin))
9879ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return true;
9889ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  else
9899ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return false;
990462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
991462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
992cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc BrouilletDataType
993b1771ef128b10c4d4575634828006bfba20b1d9cZonr ChangRSExportPrimitiveType::GetRSSpecificType(const llvm::StringRef &TypeName) {
9949ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (TypeName.empty())
9959ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return DataTypeUnknown;
996462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
997b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  if (RSSpecificTypeMap->empty()) {
998474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    for (int i = 0; i < MatrixAndObjectDataTypesCount; i++) {
9993eb819ad8beec566a73b288204f9b75c2bb1d4e6Stephen Hines      (*RSSpecificTypeMap)[MatrixAndObjectDataTypes[i].name] =
10003eb819ad8beec566a73b288204f9b75c2bb1d4e6Stephen Hines          MatrixAndObjectDataTypes[i].dataType;
1001474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    }
10029ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1003462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
1004b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  RSSpecificTypeMapTy::const_iterator I = RSSpecificTypeMap->find(TypeName);
1005b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  if (I == RSSpecificTypeMap->end())
10069ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return DataTypeUnknown;
10079ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  else
10089ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return I->getValue();
10099ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao}
10109ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1011cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc BrouilletDataType RSExportPrimitiveType::GetRSSpecificType(const clang::Type *T) {
1012b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  T = GetCanonicalType(T);
10135abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if ((T == nullptr) || (T->getTypeClass() != clang::Type::Record))
10149ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return DataTypeUnknown;
10159ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1016b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  return GetRSSpecificType( RSExportType::GetTypeName(T) );
1017b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang}
1018b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang
1019b1771ef128b10c4d4575634828006bfba20b1d9cZonr Changbool RSExportPrimitiveType::IsRSMatrixType(DataType DT) {
1020474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    if (DT < 0 || DT >= DataTypeMax) {
1021474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet        return false;
1022474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    }
1023474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    return gReflectionTypes[DT].category == MatrixDataType;
1024b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang}
1025b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang
1026b1771ef128b10c4d4575634828006bfba20b1d9cZonr Changbool RSExportPrimitiveType::IsRSObjectType(DataType DT) {
1027474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    if (DT < 0 || DT >= DataTypeMax) {
1028474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet        return false;
1029474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    }
1030474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    return gReflectionTypes[DT].category == ObjectDataType;
10319ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao}
10329ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1033feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hinesbool RSExportPrimitiveType::IsStructureTypeWithRSObject(const clang::Type *T) {
1034feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  bool RSObjectTypeSeen = false;
1035feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  while (T && T->isArrayType()) {
1036feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    T = T->getArrayElementTypeNoTypeQual();
1037feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  }
1038feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines
1039feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  const clang::RecordType *RT = T->getAsStructureType();
1040feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  if (!RT) {
1041feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    return false;
1042feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  }
1043b0fabe574945bfa85e688e77e9dcb5341fe08840Stephen Hines
1044feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  const clang::RecordDecl *RD = RT->getDecl();
1045b0fabe574945bfa85e688e77e9dcb5341fe08840Stephen Hines  if (RD) {
1046b0fabe574945bfa85e688e77e9dcb5341fe08840Stephen Hines    RD = RD->getDefinition();
1047b0fabe574945bfa85e688e77e9dcb5341fe08840Stephen Hines  }
1048b0fabe574945bfa85e688e77e9dcb5341fe08840Stephen Hines  if (!RD) {
1049b0fabe574945bfa85e688e77e9dcb5341fe08840Stephen Hines    return false;
1050b0fabe574945bfa85e688e77e9dcb5341fe08840Stephen Hines  }
1051b0fabe574945bfa85e688e77e9dcb5341fe08840Stephen Hines
1052feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1053feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines         FE = RD->field_end();
1054feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines       FI != FE;
1055feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines       FI++) {
1056feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    // We just look through all field declarations to see if we find a
1057feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    // declaration for an RS object type (or an array of one).
1058feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    const clang::FieldDecl *FD = *FI;
1059feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
1060feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    while (FT && FT->isArrayType()) {
1061feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines      FT = FT->getArrayElementTypeNoTypeQual();
1062feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    }
1063feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines
1064cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet    DataType DT = GetRSSpecificType(FT);
1065feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    if (IsRSObjectType(DT)) {
1066feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines      // RS object types definitely need to be zero-initialized
1067feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines      RSObjectTypeSeen = true;
1068feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    } else {
1069feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines      switch (DT) {
1070cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet        case DataTypeRSMatrix2x2:
1071cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet        case DataTypeRSMatrix3x3:
1072cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet        case DataTypeRSMatrix4x4:
1073feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines          // Matrix types should get zero-initialized as well
1074feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines          RSObjectTypeSeen = true;
1075feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines          break;
1076feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines        default:
1077feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines          // Ignore all other primitive types
1078feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines          break;
1079feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines      }
1080feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines      while (FT && FT->isArrayType()) {
1081feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines        FT = FT->getArrayElementTypeNoTypeQual();
1082feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines      }
1083feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines      if (FT->isStructureType()) {
1084feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines        // Recursively handle structs of structs (even though these can't
1085feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines        // be exported, it is possible for a user to have them internally).
1086feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines        RSObjectTypeSeen |= IsStructureTypeWithRSObject(FT);
1087feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines      }
1088feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    }
1089feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  }
1090feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines
1091feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  return RSObjectTypeSeen;
1092feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines}
1093feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines
1094657d20afa621219c1eed72178d1325fd4409f458David Grosssize_t RSExportPrimitiveType::GetElementSizeInBits(const RSExportPrimitiveType *EPT) {
1095474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  int type = EPT->getType();
1096474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  slangAssert((type > DataTypeUnknown && type < DataTypeMax) &&
1097657d20afa621219c1eed72178d1325fd4409f458David Gross              "RSExportPrimitiveType::GetElementSizeInBits : unknown data type");
10989ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines  // All RS object types are 256 bits in 64-bit RS.
10999ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines  if (EPT->isRSObjectType() && EPT->getRSContext()->is64Bit()) {
11009ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines    return 256;
11019ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines  }
1102474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  return gReflectionTypes[type].size_in_bits;
1103462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1104462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
1105cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc BrouilletDataType
11062ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen HinesRSExportPrimitiveType::GetDataType(RSContext *Context, const clang::Type *T) {
11075abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if (T == nullptr)
11089ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return DataTypeUnknown;
1109462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
11109ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  switch (T->getTypeClass()) {
11119ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::Builtin: {
1112be27482cdeaf08576bc39b72a15d35d13014a636Logan      const clang::BuiltinType *BT =
1113b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              static_cast<const clang::BuiltinType*>(T->getCanonicalTypeInternal().getTypePtr());
1114474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet      BuiltinInfo *info = FindBuiltinType(BT->getKind());
11155abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      if (info != nullptr) {
1116474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet        return info->type;
11179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      }
1118474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet      // The size of type WChar depend on platform so we abandon the support
1119474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet      // to them.
1120474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet      Context->ReportError("built-in type cannot be exported: '%0'")
1121474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet          << T->getTypeClassName();
11229ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
11239ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
11249ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::Record: {
11259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // must be RS object type
1126b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang      return RSExportPrimitiveType::GetRSSpecificType(T);
11279ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
11289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    default: {
1129d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError("primitive type cannot be exported: '%0'")
11302ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines          << T->getTypeClassName();
11319ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
1132462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    }
11339ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1134462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
11359ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return DataTypeUnknown;
1136462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1137462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
11389ef2f785e0cc490af678dfd685995dec787321ffShih-wei LiaoRSExportPrimitiveType
11399ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao*RSExportPrimitiveType::Create(RSContext *Context,
11409ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                               const clang::Type *T,
11419ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                               const llvm::StringRef &TypeName,
11429ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                               bool Normalized) {
11432ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines  DataType DT = GetDataType(Context, T);
1144462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
11459ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if ((DT == DataTypeUnknown) || TypeName.empty())
11465abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
11479ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  else
11486b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang    return new RSExportPrimitiveType(Context, ExportClassPrimitive, TypeName,
11492b8fb64be3047df940a219872b331eb11de2758dStephen Hines                                     DT, Normalized);
1150462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1151462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
11529ef2f785e0cc490af678dfd685995dec787321ffShih-wei LiaoRSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context,
11532b8fb64be3047df940a219872b331eb11de2758dStephen Hines                                                     const clang::Type *T) {
11549ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::StringRef TypeName;
115513fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines  if (RSExportType::NormalizeType(T, TypeName, Context, nullptr,
115613fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                  NotLegacyKernelArgument) &&
115713fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      IsPrimitiveType(T)) {
11582b8fb64be3047df940a219872b331eb11de2758dStephen Hines    return Create(Context, T, TypeName);
1159e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  } else {
11605abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
1161e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  }
1162462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1163462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
11647c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liaollvm::Type *RSExportPrimitiveType::convertToLLVMType() const {
11659ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::LLVMContext &C = getRSContext()->getLLVMContext();
11669ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
11679ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (isRSObjectType()) {
11689ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // struct {
11699ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    //   int *p;
11709ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // } __attribute__((packed, aligned(pointer_size)))
11719ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    //
11729ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // which is
11739ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    //
11749ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // <{ [1 x i32] }> in LLVM
11759ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    //
1176d56a92fc2b1d417ce53d9550548fe1661fa37d40Tim Murray    std::vector<llvm::Type *> Elements;
1177d56a92fc2b1d417ce53d9550548fe1661fa37d40Tim Murray    if (getRSContext()->is64Bit()) {
1178d56a92fc2b1d417ce53d9550548fe1661fa37d40Tim Murray      // 64-bit path
1179d56a92fc2b1d417ce53d9550548fe1661fa37d40Tim Murray      Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt64Ty(C), 4));
1180d56a92fc2b1d417ce53d9550548fe1661fa37d40Tim Murray      return llvm::StructType::get(C, Elements, true);
1181d56a92fc2b1d417ce53d9550548fe1661fa37d40Tim Murray    } else {
1182d56a92fc2b1d417ce53d9550548fe1661fa37d40Tim Murray      // 32-bit legacy path
1183d56a92fc2b1d417ce53d9550548fe1661fa37d40Tim Murray      Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt32Ty(C), 1));
1184d56a92fc2b1d417ce53d9550548fe1661fa37d40Tim Murray      return llvm::StructType::get(C, Elements, true);
1185462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    }
11869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1187462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
11889ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  switch (mType) {
1189e4dd17d7b2a292a600756da7680beecd78f74033Pirama Arumuga Nainar    case DataTypeFloat16: {
1190e4dd17d7b2a292a600756da7680beecd78f74033Pirama Arumuga Nainar      return llvm::Type::getHalfTy(C);
1191e4dd17d7b2a292a600756da7680beecd78f74033Pirama Arumuga Nainar      break;
1192e4dd17d7b2a292a600756da7680beecd78f74033Pirama Arumuga Nainar    }
11939ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeFloat32: {
11949ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      return llvm::Type::getFloatTy(C);
11959ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
11969ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
11979ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeFloat64: {
11989ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      return llvm::Type::getDoubleTy(C);
11999ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
12009ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
120191a3783ce1f4eb9ad6e9c1ecdbd27f3d6dc58634Shih-wei Liao    case DataTypeBoolean: {
120291a3783ce1f4eb9ad6e9c1ecdbd27f3d6dc58634Shih-wei Liao      return llvm::Type::getInt1Ty(C);
120391a3783ce1f4eb9ad6e9c1ecdbd27f3d6dc58634Shih-wei Liao      break;
120491a3783ce1f4eb9ad6e9c1ecdbd27f3d6dc58634Shih-wei Liao    }
12059ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeSigned8:
12069ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeUnsigned8: {
12079ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      return llvm::Type::getInt8Ty(C);
12089ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
12099ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
12109ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeSigned16:
12119ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeUnsigned16:
12129ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeUnsigned565:
12139ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeUnsigned5551:
12149ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeUnsigned4444: {
12159ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      return llvm::Type::getInt16Ty(C);
12169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
12179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
12189ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeSigned32:
12199ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case DataTypeUnsigned32: {
12209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      return llvm::Type::getInt32Ty(C);
12219ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
12229ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
1223a5d2c232d56b04292cb51c8fb343aef990f7970fStephen Hines    case DataTypeSigned64:
1224a5d2c232d56b04292cb51c8fb343aef990f7970fStephen Hines    case DataTypeUnsigned64: {
12259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      return llvm::Type::getInt64Ty(C);
12269ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
12279ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
12289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    default: {
12296e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines      slangAssert(false && "Unknown data type");
1230462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    }
12319ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1232462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
12335abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  return nullptr;
1234462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1235462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
1236ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sungbool RSExportPrimitiveType::matchODR(const RSExportType *E,
1237ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung                                     bool /* LookInto */) const {
1238641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  CHECK_PARENT_EQUALITY(RSExportType, E);
1239641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  return (static_cast<const RSExportPrimitiveType*>(E)->getType() == getType());
1240641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
1241641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
1242fdd1ba13a69501a1b91fdc9be31413215d467497Stephen HinesRSReflectionType *RSExportPrimitiveType::getRSReflectionType(DataType DT) {
1243fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines  if (DT > DataTypeUnknown && DT < DataTypeMax) {
1244fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    return &gReflectionTypes[DT];
1245fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines  } else {
12465abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
1247fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines  }
1248fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines}
1249fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines
12509ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao/**************************** RSExportPointerType ****************************/
1251462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
12529ef2f785e0cc490af678dfd685995dec787321ffShih-wei LiaoRSExportPointerType
12539ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao*RSExportPointerType::Create(RSContext *Context,
12549ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                             const clang::PointerType *PT,
12559ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                             const llvm::StringRef &TypeName) {
1256b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *PointeeType = GetPointeeType(PT);
12579ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  const RSExportType *PointeeET;
1258462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
12599ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (PointeeType->getTypeClass() != clang::Type::Pointer) {
126013fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines    PointeeET = RSExportType::Create(Context, PointeeType,
126113fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                     NotLegacyKernelArgument);
12629ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  } else {
12639ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // Double or higher dimension of pointer, export as int*
12642ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines    PointeeET = RSExportPrimitiveType::Create(Context,
12652ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines                    Context->getASTContext().IntTy.getTypePtr());
12669ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1267462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
12685abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if (PointeeET == nullptr) {
12692ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines    // Error diagnostic is emitted for corresponding pointee type
12705abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
12719ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1272462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
12739ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return new RSExportPointerType(Context, TypeName, PointeeET);
1274462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1275462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
12767c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liaollvm::Type *RSExportPointerType::convertToLLVMType() const {
12777c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  llvm::Type *PointeeType = mPointeeType->getLLVMType();
12789ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return llvm::PointerType::getUnqual(PointeeType);
1279462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1280462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
12813cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Changbool RSExportPointerType::keep() {
12823cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  if (!RSExportType::keep())
12833cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang    return false;
1284641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  const_cast<RSExportType*>(mPointeeType)->keep();
12853cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  return true;
1286641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
1287641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
1288ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sungbool RSExportPointerType::matchODR(const RSExportType *E,
1289ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung                                   bool /* LookInto */) const {
1290ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  // Exported types cannot contain pointers
1291ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  slangAssert(false && "Not supposed to perform ODR check on pointers");
1292ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  return false;
1293641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
1294641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
12956315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr/***************************** RSExportVectorType *****************************/
12966315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonrllvm::StringRef
12976315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonrRSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) {
1298b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *ElementType = GetExtVectorElementType(EVT);
1299474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  llvm::StringRef name;
1300462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
13019ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if ((ElementType->getTypeClass() != clang::Type::Builtin))
1302474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    return name;
13039ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1304b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::BuiltinType *BT =
1305b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet          static_cast<const clang::BuiltinType*>(
1306b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              ElementType->getCanonicalTypeInternal().getTypePtr());
1307b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet
1308a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang  if ((EVT->getNumElements() < 1) ||
1309a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang      (EVT->getNumElements() > 4))
1310474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    return name;
13119ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1312474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  BuiltinInfo *info = FindBuiltinType(BT->getKind());
13135abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if (info != nullptr) {
1314474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    int I = EVT->getNumElements() - 1;
1315474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    if (I < kMaxVectorSize) {
1316474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet      name = info->cname[I];
1317474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet    } else {
1318474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet      slangAssert(false && "Max vector is 4");
1319462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    }
13209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1321474655a402e70cb329e1bcd4ebbe00bdc5be4206Jean-Luc Brouillet  return name;
1322462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1323462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
13249ef2f785e0cc490af678dfd685995dec787321ffShih-wei LiaoRSExportVectorType *RSExportVectorType::Create(RSContext *Context,
13259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                               const clang::ExtVectorType *EVT,
13269ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                               const llvm::StringRef &TypeName,
13279ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                               bool Normalized) {
13285abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  slangAssert(EVT != nullptr && EVT->getTypeClass() == clang::Type::ExtVector);
1329462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
1330b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *ElementType = GetExtVectorElementType(EVT);
1331cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet  DataType DT = RSExportPrimitiveType::GetDataType(Context, ElementType);
1332462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
1333cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet  if (DT != DataTypeUnknown)
13349ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return new RSExportVectorType(Context,
13359ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                  TypeName,
13369ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                  DT,
13379ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                  Normalized,
13389ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                  EVT->getNumElements());
13399ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  else
13405abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
1341462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1342462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
13437c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liaollvm::Type *RSExportVectorType::convertToLLVMType() const {
13447c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  llvm::Type *ElementType = RSExportPrimitiveType::convertToLLVMType();
13459ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return llvm::VectorType::get(ElementType, getNumElement());
1346462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1347462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
1348ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sungbool RSExportVectorType::matchODR(const RSExportType *E,
1349ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung                                  bool /* LookInto*/) const {
1350641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  CHECK_PARENT_EQUALITY(RSExportPrimitiveType, E);
1351641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  return (static_cast<const RSExportVectorType*>(E)->getNumElement()
1352641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang              == getNumElement());
1353641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
1354641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
135592b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang/***************************** RSExportMatrixType *****************************/
135692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr ChangRSExportMatrixType *RSExportMatrixType::Create(RSContext *Context,
135792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                               const clang::RecordType *RT,
135892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                               const llvm::StringRef &TypeName,
135992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                               unsigned Dim) {
13605abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  slangAssert((RT != nullptr) && (RT->getTypeClass() == clang::Type::Record));
13616e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines  slangAssert((Dim > 1) && "Invalid dimension of matrix");
136292b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
136392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  // Check whether the struct rs_matrix is in our expected form (but assume it's
136492b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  // correct if we're not sure whether it's correct or not)
136592b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  const clang::RecordDecl* RD = RT->getDecl();
136692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  RD = RD->getDefinition();
13675abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if (RD != nullptr) {
136892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    // Find definition, perform further examination
136992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    if (RD->field_empty()) {
1370d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError(
1371d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          RD->getLocation(),
1372d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          "invalid matrix struct: must have 1 field for saving values: '%0'")
1373d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          << RD->getName();
13745abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
137592b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    }
137692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
137792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    clang::RecordDecl::field_iterator FIT = RD->field_begin();
137892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    const clang::FieldDecl *FD = *FIT;
137992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
13805abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    if ((FT == nullptr) || (FT->getTypeClass() != clang::Type::ConstantArray)) {
1381d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError(RD->getLocation(),
1382d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "invalid matrix struct: first field should"
1383d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           " be an array with constant size: '%0'")
1384d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          << RD->getName();
13855abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
138692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    }
138792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    const clang::ConstantArrayType *CAT =
138892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang      static_cast<const clang::ConstantArrayType *>(FT);
1389b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet    const clang::Type *ElementType = GetConstantArrayElementType(CAT);
13905abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    if ((ElementType == nullptr) ||
139192b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang        (ElementType->getTypeClass() != clang::Type::Builtin) ||
13929207a2e495c8363606861e4f034504ec5c153dabLogan Chien        (static_cast<const clang::BuiltinType *>(ElementType)->getKind() !=
13939207a2e495c8363606861e4f034504ec5c153dabLogan Chien         clang::BuiltinType::Float)) {
1394d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError(RD->getLocation(),
1395d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "invalid matrix struct: first field "
1396d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "should be a float array: '%0'")
1397d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          << RD->getName();
13985abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
139992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    }
140092b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
140192b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    if (CAT->getSize() != Dim * Dim) {
1402d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError(RD->getLocation(),
1403d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "invalid matrix struct: first field "
1404d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "should be an array with size %0: '%1'")
1405d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          << (Dim * Dim) << (RD->getName());
14065abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
140792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    }
140892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
140992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    FIT++;
141092b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    if (FIT != RD->field_end()) {
1411d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError(RD->getLocation(),
1412d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "invalid matrix struct: must have "
1413d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "exactly 1 field: '%0'")
1414d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          << RD->getName();
14155abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
141692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    }
141792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  }
141892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
141992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  return new RSExportMatrixType(Context, TypeName, Dim);
142092b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang}
142192b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
14227c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liaollvm::Type *RSExportMatrixType::convertToLLVMType() const {
142392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  // Construct LLVM type:
142492b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  // struct {
142592b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  //  float X[mDim * mDim];
142692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  // }
142792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
142892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  llvm::LLVMContext &C = getRSContext()->getLLVMContext();
142992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  llvm::ArrayType *X = llvm::ArrayType::get(llvm::Type::getFloatTy(C),
143092b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                            mDim * mDim);
1431a67e4451d0d03b4ab7866b64807d95a8399c73a0Stephen Hines  return llvm::StructType::get(C, X, false);
143292b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang}
143392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
1434ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sungbool RSExportMatrixType::matchODR(const RSExportType *E,
1435ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung                                  bool /* LookInto */) const {
1436641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  CHECK_PARENT_EQUALITY(RSExportType, E);
1437641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  return (static_cast<const RSExportMatrixType*>(E)->getDim() == getDim());
1438641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
1439641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
14402e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang/************************* RSExportConstantArrayType *************************/
14412e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr ChangRSExportConstantArrayType
14422e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang*RSExportConstantArrayType::Create(RSContext *Context,
14432e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                                   const clang::ConstantArrayType *CAT) {
14445abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  slangAssert(CAT != nullptr && CAT->getTypeClass() == clang::Type::ConstantArray);
14452e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
14466e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines  slangAssert((CAT->getSize().getActiveBits() < 32) && "array too large");
14472e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
14482e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  unsigned Size = static_cast<unsigned>(CAT->getSize().getZExtValue());
14496e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines  slangAssert((Size > 0) && "Constant array should have size greater than 0");
14502e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
1451b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *ElementType = GetConstantArrayElementType(CAT);
145213fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines  RSExportType *ElementET = RSExportType::Create(Context, ElementType,
145313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                                 NotLegacyKernelArgument);
14542e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
14555abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if (ElementET == nullptr) {
14565abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
14572e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  }
14582e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
14592e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  return new RSExportConstantArrayType(Context,
14602e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                                       ElementET,
14612e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                                       Size);
14622e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang}
14632e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
14647c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liaollvm::Type *RSExportConstantArrayType::convertToLLVMType() const {
1465657d20afa621219c1eed72178d1325fd4409f458David Gross  return llvm::ArrayType::get(mElementType->getLLVMType(), getNumElement());
14662e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang}
14672e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
14683cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Changbool RSExportConstantArrayType::keep() {
14693cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  if (!RSExportType::keep())
14703cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang    return false;
1471641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  const_cast<RSExportType*>(mElementType)->keep();
14723cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  return true;
1473641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
1474641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
1475ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sungbool RSExportConstantArrayType::matchODR(const RSExportType *E,
1476ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung                                         bool LookInto) const {
1477641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  CHECK_PARENT_EQUALITY(RSExportType, E);
1478a7b7518aa3725d5cff1c1a6319ec7a6b8b244e0eStephen Hines  const RSExportConstantArrayType *RHS =
1479a7b7518aa3725d5cff1c1a6319ec7a6b8b244e0eStephen Hines      static_cast<const RSExportConstantArrayType*>(E);
1480657d20afa621219c1eed72178d1325fd4409f458David Gross  return ((getNumElement() == RHS->getNumElement()) &&
1481ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung          (getElementType()->matchODR(RHS->getElementType(), LookInto)));
1482641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
1483641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
14849ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao/**************************** RSExportRecordType ****************************/
14859ef2f785e0cc490af678dfd685995dec787321ffShih-wei LiaoRSExportRecordType *RSExportRecordType::Create(RSContext *Context,
14869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                               const clang::RecordType *RT,
14879ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                               const llvm::StringRef &TypeName,
14889ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                               bool mIsArtificial) {
14895abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  slangAssert(RT != nullptr && RT->getTypeClass() == clang::Type::Record);
1490462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
14919ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  const clang::RecordDecl *RD = RT->getDecl();
14926e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines  slangAssert(RD->isStruct());
1493462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
14949ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  RD = RD->getDefinition();
14955abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if (RD == nullptr) {
14966e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines    slangAssert(false && "struct is not defined in this module");
14975abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
14989ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1499462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
15000da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  // Struct layout construct by clang. We rely on this for obtaining the
15010da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  // alloc size of a struct and offset of every field in that struct.
15020da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  const clang::ASTRecordLayout *RL =
15039e5b503349719144f63ccb7c62ee9c291a7d83b8Stephen Hines      &Context->getASTContext().getASTRecordLayout(RD);
15045abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  slangAssert((RL != nullptr) &&
1505f2174cfd6a556b51aadf2b8765e50df080e8f18eStephen Hines      "Failed to retrieve the struct layout from Clang.");
15060da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang
15070da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  RSExportRecordType *ERT =
15080da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang      new RSExportRecordType(Context,
15090da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang                             TypeName,
15100da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang                             RD->hasAttr<clang::PackedAttr>(),
15110da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang                             mIsArtificial,
1512c95381a2c3b6e9117901eef0687e861e4d533bfeJean-Luc Brouillet                             RL->getDataSize().getQuantity(),
151368318a14fe6d2debc1b9dce3fe71c42f5916eef5Shih-wei Liao                             RL->getSize().getQuantity());
15149ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  unsigned int Index = 0;
15159ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
15169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
15179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao           FE = RD->field_end();
15189ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao       FI != FE;
151991a3783ce1f4eb9ad6e9c1ecdbd27f3d6dc58634Shih-wei Liao       FI++, Index++) {
15209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
15219ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // FIXME: All fields should be primitive type
152243730fe3c839af391efe6bdf56b0479860121924Shih-wei Liao    slangAssert(FI->getKind() == clang::Decl::Field);
15239ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    clang::FieldDecl *FD = *FI;
15249ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
15252ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines    if (FD->isBitField()) {
15265abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
15272ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines    }
15289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
152937dbf5c324322fe2458e63e3fa869fbf903ec6fdDavid Gross    if (FD->isImplicit() && (FD->getName() == RS_PADDING_FIELD_NAME))
153037dbf5c324322fe2458e63e3fa869fbf903ec6fdDavid Gross      continue;
153137dbf5c324322fe2458e63e3fa869fbf903ec6fdDavid Gross
15329ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // Type
15336315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr    RSExportType *ET = RSExportElement::CreateFromDecl(Context, FD);
15349ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
15355abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    if (ET != nullptr) {
15360da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang      ERT->mFields.push_back(
15370da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang          new Field(ET, FD->getName(), ERT,
15380da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang                    static_cast<size_t>(RL->getFieldOffset(Index) >> 3)));
15392ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines    } else {
154017e3baa8b9504eece8ead022cf51e7a8e3fe25d0Luis A. Lozano      // clang static analysis complains about a potential memory leak
154117e3baa8b9504eece8ead022cf51e7a8e3fe25d0Luis A. Lozano      // for the memory pointed by ERT at the end of this basic
154217e3baa8b9504eece8ead022cf51e7a8e3fe25d0Luis A. Lozano      // block. This is a false warning because the compiler does not
154317e3baa8b9504eece8ead022cf51e7a8e3fe25d0Luis A. Lozano      // see that the pointer to this memory is saved away in the
154417e3baa8b9504eece8ead022cf51e7a8e3fe25d0Luis A. Lozano      // constructor for RSExportRecordType by calling
154517e3baa8b9504eece8ead022cf51e7a8e3fe25d0Luis A. Lozano      // RSContext::newExportable(this). So, we disable this
154617e3baa8b9504eece8ead022cf51e7a8e3fe25d0Luis A. Lozano      // particular instance of the warning.
1547d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError(RD->getLocation(),
1548d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "field type cannot be exported: '%0.%1'")
154917e3baa8b9504eece8ead022cf51e7a8e3fe25d0Luis A. Lozano          << RD->getName() << FD->getName(); // NOLINT
15505abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
15512ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines    }
15529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
15530a3f20ec28ed6f5ae1ed5d61f6b6e3e577f7f5d1Shih-wei Liao
15549ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return ERT;
1555462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1556462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
15577c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liaollvm::Type *RSExportRecordType::convertToLLVMType() const {
15583cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  // Create an opaque type since struct may reference itself recursively.
15593cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang
15607c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  // TODO(sliao): LLVM took out the OpaqueType. Any other to migrate to?
15617c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  std::vector<llvm::Type*> FieldTypes;
1562462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
15633cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  for (const_field_iterator FI = fields_begin(), FE = fields_end();
15649ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao       FI != FE;
15659ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao       FI++) {
15669ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    const Field *F = *FI;
15679ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    const RSExportType *FET = F->getType();
15681f0d88fbff28e4e2dd563d93c8fe0047381c09ccShih-wei Liao
15699ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    FieldTypes.push_back(FET->getLLVMType());
15709ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1571462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
15723cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  llvm::StructType *ST = llvm::StructType::get(getRSContext()->getLLVMContext(),
15733cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang                                               FieldTypes,
15743cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang                                               mIsPacked);
15755abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if (ST != nullptr) {
15767c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao    return ST;
15777c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  } else {
15785abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes    return nullptr;
15797c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  }
1580462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
1581641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
15823cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Changbool RSExportRecordType::keep() {
15833cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  if (!RSExportType::keep())
15843cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang    return false;
1585641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  for (std::list<const Field*>::iterator I = mFields.begin(),
1586641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang          E = mFields.end();
1587641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang       I != E;
1588641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang       I++) {
1589641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang    const_cast<RSExportType*>((*I)->getType())->keep();
1590641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  }
15913cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  return true;
1592641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
1593641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
1594ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sungbool RSExportRecordType::matchODR(const RSExportType *E, bool LookInto) const {
1595641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  CHECK_PARENT_EQUALITY(RSExportType, E);
1596ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  // Enforce ODR checking - the type E represents must hold
1597ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  // *exactly* the same "definition" as the one defined previously. We
1598ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  // say two record types A and B have the same definition iff:
1599ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //
1600ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //  struct A {              struct B {
1601ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //    Type(a1) a1,            Type(b1) b1,
1602ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //    Type(a2) a2,            Type(b1) b2,
1603ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //    ...                     ...
1604ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //    Type(aN) aN             Type(bM) bM,
1605ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //  };                      }
1606ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //  Cond. #0. A = B;
1607ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //  Cond. #1. They have same number of fields, i.e., N = M;
1608ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //  Cond. #2. for (i := 1 to N)
1609ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //              Type(ai).matchODR(Type(bi)) must hold;
1610ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //  Cond. #3. for (i := 1 to N)
1611ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //              Name(ai) = Name(bi) must hold;
1612ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //
1613ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  // where,
1614ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //  Type(F) = the type of field F and
1615ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  //  Name(F) = the field name.
1616641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
1617641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
1618ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  const RSExportRecordType *ERT = static_cast<const RSExportRecordType*>(E);
1619ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  // Cond. #0.
1620ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  if (getName() != ERT->getName())
1621641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang    return false;
1622641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
1623ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  // Examine fields - types and names
1624ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  if (LookInto) {
1625ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung    // Cond. #1
1626ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung    if (ERT->getFields().size() != getFields().size())
1627641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang      return false;
1628641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
1629ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung    for (RSExportRecordType::const_field_iterator AI = fields_begin(),
1630ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung         BI = ERT->fields_begin(), AE = fields_end(); AI != AE; ++AI, ++BI) {
1631ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung      const RSExportType *AITy = (*AI)->getType();
1632ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung      const RSExportType *BITy = (*BI)->getType();
1633ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung      // Cond. #3; field names must agree
1634ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung      if ((*AI)->getName() != (*BI)->getName())
1635ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung        return false;
1636ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung
1637ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung      // Cond. #2; field types must agree recursively until we see another
1638ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung      // next level of RSExportRecordType - such field types will be
1639ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung      // examined and reported later when checkODR() encounters them.
1640ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung      if (!AITy->matchODR(BITy, false))
1641ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung        return false;
1642ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung    }
1643ef1707027a18ed0d91a3854476db71024ae0e6daI-Jui (Ray) Sung  }
1644641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  return true;
1645641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang}
1646e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines
16471b6a0883cd6984e11e59b0c847fb334df1f41afcJason Samsvoid RSExportType::convertToRTD(RSReflectionTypeData *rtd) const {
16481b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    memset(rtd, 0, sizeof(*rtd));
16491b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    rtd->vecSize = 1;
16501b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams
16511b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    switch(getClass()) {
16521b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    case RSExportType::ExportClassPrimitive: {
16531b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            const RSExportPrimitiveType *EPT = static_cast<const RSExportPrimitiveType*>(this);
16541b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            rtd->type = RSExportPrimitiveType::getRSReflectionType(EPT);
16551b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            return;
16561b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams        }
16571b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    case RSExportType::ExportClassPointer: {
16581b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            const RSExportPointerType *EPT = static_cast<const RSExportPointerType*>(this);
16591b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            const RSExportType *PointeeType = EPT->getPointeeType();
16601b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            PointeeType->convertToRTD(rtd);
16611b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            rtd->isPointer = true;
16621b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            return;
16631b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams        }
16641b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    case RSExportType::ExportClassVector: {
16651b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(this);
16661b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            rtd->type = EVT->getRSReflectionType(EVT);
16671b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            rtd->vecSize = EVT->getNumElement();
16681b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            return;
16691b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams        }
16701b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    case RSExportType::ExportClassMatrix: {
16711b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType*>(this);
16721b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            unsigned Dim = EMT->getDim();
16731b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            slangAssert((Dim >= 2) && (Dim <= 4));
16741b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            rtd->type = &gReflectionTypes[15 + Dim-2];
16751b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            return;
16761b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams        }
16771b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    case RSExportType::ExportClassConstantArray: {
16781b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            const RSExportConstantArrayType* CAT =
16791b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams              static_cast<const RSExportConstantArrayType*>(this);
16801b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            CAT->getElementType()->convertToRTD(rtd);
1681657d20afa621219c1eed72178d1325fd4409f458David Gross            rtd->arraySize = CAT->getNumElement();
16821b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            return;
16831b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams        }
16841b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    case RSExportType::ExportClassRecord: {
16851b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            slangAssert(!"RSExportType::ExportClassRecord not implemented");
16861b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            return;// RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + ".Item";
16871b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams        }
16881b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    default: {
16891b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams            slangAssert(false && "Unknown class of type");
16901b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams        }
16911b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    }
16921b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams}
16931b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams
16941b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams
1695e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines}  // namespace slang
1696