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) \
37641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  if (!ParentClass::equals(E))                \
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:
3482907b2a2768bc32f75867513528c8d7419e44780David Gross      ReportTypeError(Context, VD, TopLevelRecord,
3492907b2a2768bc32f75867513528c8d7419e44780David Gross                      "function types cannot be exported: '%0'");
3502907b2a2768bc32f75867513528c8d7419e44780David Gross      return nullptr;
3519ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::Pointer: {
352e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      if (TopLevelRecord) {
353d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        ReportTypeError(Context, VD, TopLevelRecord,
354ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines            "structures containing pointers cannot be used as the type of "
355ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines            "an exported global variable or the parameter to an exported "
356ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines            "function: '%0'");
3575abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
358c808a99831115928b4648f4c8b86dc682594217aStephen Hines      }
35924e79f69125cf87fcaa78c04510a831037203eebStephen Hines
360b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
361b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *PointeeType = GetPointeeType(PT);
362462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
363aa82e74b08fcdc2e4e6c1f6796699566b331b656Stephen Hines      if (PointeeType->getTypeClass() == clang::Type::Pointer) {
364d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        ReportTypeError(Context, VD, TopLevelRecord,
365aa82e74b08fcdc2e4e6c1f6796699566b331b656Stephen Hines            "multiple levels of pointers cannot be exported: '%0'");
3665abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
367aa82e74b08fcdc2e4e6c1f6796699566b331b656Stephen Hines      }
36813fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines
36913fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      // Void pointers are forbidden for export, although we must accept
37013fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      // void pointers that come in as arguments to a legacy kernel.
37113fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      if (PointeeType->isVoidType() && EK != LegacyKernelArgument) {
37213fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines        ReportTypeError(Context, VD, TopLevelRecord,
37313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines            "void pointers cannot be exported: '%0'");
37413fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines        return nullptr;
37513fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      }
37613fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines
3772e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang      // We don't support pointer with array-type pointee or unsupported pointee
3782e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang      // type
3792e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang      if (PointeeType->isArrayType() ||
38048d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines          (TypeExportableHelper(PointeeType, SPS, Context, VD,
38113fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                TopLevelRecord, EK) == nullptr))
3825abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
3839ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      else
3849ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        return T;
3859ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
3869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    case clang::Type::ExtVector: {
3879ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      const clang::ExtVectorType *EVT =
388b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              static_cast<const clang::ExtVectorType*>(CTI);
3899ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Only vector with size 2, 3 and 4 are supported.
3909ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      if (EVT->getNumElements() < 2 || EVT->getNumElements() > 4)
3915abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
392f8149d9e5a3795e9952717ee6346789a134c55c7Shih-wei Liao
3939ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Check base element type
394b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *ElementType = GetExtVectorElementType(EVT);
395462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3969ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      if ((ElementType->getTypeClass() != clang::Type::Builtin) ||
39748d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines          (TypeExportableHelper(ElementType, SPS, Context, VD,
39813fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                TopLevelRecord, EK) == nullptr))
3995abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        return nullptr;
4009ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      else
4019ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        return T;
4029ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
4032e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    case clang::Type::ConstantArray: {
4042e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang      const clang::ConstantArrayType *CAT =
405b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              static_cast<const clang::ConstantArrayType*>(CTI);
4062e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
40748d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      return ConstantArrayTypeExportableHelper(CAT, SPS, Context, VD,
40813fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                               TopLevelRecord, EK);
4092e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    }
41048d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    case clang::Type::Enum: {
41148d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      // FIXME: We currently convert enums to integers, rather than reflecting
41248d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      // a more complete (and nicer type-safe Java version).
41348d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      return Context->getASTContext().IntTy.getTypePtr();
41448d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    }
4159ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    default: {
41648d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines      slangAssert(false && "Unknown type cannot be validated");
4175abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
418462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    }
4199ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
4209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao}
421462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
422e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines// Return the type that can be used to create RSExportType, will always return
4235abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// the canonical type.
4245abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes//
4255abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// If the Type T is not exportable, this function returns nullptr. DiagEngine is
4265abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// used to generate proper Clang diagnostic messages when a non-exportable type
4275abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// is detected. TopLevelRecord is used to capture the highest struct (in the
4285abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// case of a nested hierarchy) for detecting other types that cannot be exported
4295abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes// (mostly pointers within a struct).
430e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hinesstatic const clang::Type *TypeExportable(const clang::Type *T,
43148d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines                                         slang::RSContext *Context,
43213fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                         const clang::VarDecl *VD,
43313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                         ExportKind EK) {
434e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  llvm::SmallPtrSet<const clang::Type*, 8> SPS =
435e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      llvm::SmallPtrSet<const clang::Type*, 8>();
436e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
43713fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines  return TypeExportableHelper(T, SPS, Context, VD, nullptr, EK);
43878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines}
43978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
440d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouilletstatic bool ValidateRSObjectInVarDecl(slang::RSContext *Context,
4412907b2a2768bc32f75867513528c8d7419e44780David Gross                                      const clang::VarDecl *VD, bool InCompositeType,
442d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines                                      unsigned int TargetAPI) {
443d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  if (TargetAPI < SLANG_JB_TARGET_API) {
444d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    // Only if we are already in a composite type (like an array or structure).
445d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    if (InCompositeType) {
446d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines      // Only if we are actually exported (i.e. non-static).
44744f10063c2c08dab103a44cded0c3a288d65d43bStephen Hines      if (VD->hasLinkage() &&
44844f10063c2c08dab103a44cded0c3a288d65d43bStephen Hines          (VD->getFormalLinkage() == clang::ExternalLinkage)) {
449d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines        // Only if we are not a pointer to an object.
450b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet        const clang::Type *T = GetCanonicalType(VD->getType().getTypePtr());
451d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines        if (T->getTypeClass() != clang::Type::Pointer) {
4525abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes          ReportTypeError(Context, VD, nullptr,
453d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines                          "arrays/structures containing RS object types "
454d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines                          "cannot be exported in target API < %1: '%0'",
455d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines                          SLANG_JB_TARGET_API);
456d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines          return false;
457d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines        }
458d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines      }
459d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    }
460d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  }
461d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines
462d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines  return true;
463d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines}
464d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines
46511274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// Helper function for ValidateType(). We do a recursive descent on the
466d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// type hierarchy to ensure that we can properly export/handle the
467d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// declaration.
468d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// \return true if the variable declaration is valid,
469d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines//         false if it is invalid (along with proper diagnostics).
470d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines//
47111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// C - ASTContext (for diagnostics + builtin types).
47211274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// T - sub-type that we are validating.
47311274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// ND - (optional) top-level named declaration that we are validating.
474d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// SPS - set of types we have already seen/validated.
475d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// InCompositeType - true if we are within an outer composite type.
476d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// UnionDecl - set if we are in a sub-type of a union.
477d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines// TargetAPI - target SDK API level.
47811274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines// IsFilterscript - whether or not we are compiling for Filterscript
479ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines// IsExtern - is this type externally visible (i.e. extern global or parameter
480ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines//                                             to an extern function)
48111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hinesstatic bool ValidateTypeHelper(
482d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet    slang::RSContext *Context,
48311274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines    clang::ASTContext &C,
48478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    const clang::Type *&T,
4852907b2a2768bc32f75867513528c8d7419e44780David Gross    const clang::NamedDecl *ND,
48611274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines    clang::SourceLocation Loc,
48778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
488d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    bool InCompositeType,
489d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines    clang::RecordDecl *UnionDecl,
49011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines    unsigned int TargetAPI,
491ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines    bool IsFilterscript,
492ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines    bool IsExtern) {
4935abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  if ((T = GetCanonicalType(T)) == nullptr)
49478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    return true;
49578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
49678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  if (SPS.count(T))
49778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    return true;
49878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
499b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet  const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
500b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet
50178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  switch (T->getTypeClass()) {
50278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::Record: {
503d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines      if (RSExportPrimitiveType::IsRSObjectType(T)) {
5042907b2a2768bc32f75867513528c8d7419e44780David Gross        const clang::VarDecl *VD = (ND ? llvm::dyn_cast<clang::VarDecl>(ND) : nullptr);
505d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        if (VD && !ValidateRSObjectInVarDecl(Context, VD, InCompositeType,
506d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                                             TargetAPI)) {
507d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines          return false;
508d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines        }
509d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines      }
510d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines
511cec9b65aa890dea58e39951900ae13efb8d11703Jean-Luc Brouillet      if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
51278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        if (!UnionDecl) {
51378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          return true;
51478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        } else if (RSExportPrimitiveType::IsRSObjectType(T)) {
5155abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes          ReportTypeError(Context, nullptr, UnionDecl,
51678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines              "unions containing RS object types are not allowed");
51778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          return false;
51878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        }
51978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      }
52078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
5215abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      clang::RecordDecl *RD = nullptr;
52278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
52378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      // Check internal struct
52478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      if (T->isUnionType()) {
52578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        RD = T->getAsUnionType()->getDecl();
52678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        UnionDecl = RD;
52778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      } else if (T->isStructureType()) {
52878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        RD = T->getAsStructureType()->getDecl();
52978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      } else {
53078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        slangAssert(false && "Unknown type cannot be exported");
53178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        return false;
53278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      }
53378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
5345abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      if (RD != nullptr) {
53578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        RD = RD->getDefinition();
5365abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        if (RD == nullptr) {
53778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          // FIXME
53878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          return true;
53978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        }
54078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      }
54178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
54278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      // Fast check
54378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
54478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        return false;
54578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
54678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      // Insert myself into checking set
54778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      SPS.insert(T);
54878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
54978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      // Check all elements
55078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
55178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines               FE = RD->field_end();
55278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines           FI != FE;
55378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines           FI++) {
55478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        const clang::FieldDecl *FD = *FI;
55578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
556b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet        FT = GetCanonicalType(FT);
55778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
558d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet        if (!ValidateTypeHelper(Context, C, FT, ND, Loc, SPS, true, UnionDecl,
559ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                TargetAPI, IsFilterscript, IsExtern)) {
56078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines          return false;
56178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines        }
56278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      }
56378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
56478e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      return true;
56578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
56678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
56778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::Builtin: {
56811274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines      if (IsFilterscript) {
56911274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        clang::QualType QT = T->getCanonicalTypeInternal();
57011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        if (QT == C.DoubleTy ||
57111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines            QT == C.LongDoubleTy ||
57211274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines            QT == C.LongTy ||
57311274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines            QT == C.LongLongTy) {
57411274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          if (ND) {
575d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet            Context->ReportError(
576d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                Loc,
57711274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines                "Builtin types > 32 bits in size are forbidden in "
578d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                "Filterscript: '%0'")
579d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                << ND->getName();
58011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          } else {
581d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet            Context->ReportError(
582d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                Loc,
58311274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines                "Builtin types > 32 bits in size are forbidden in "
584d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                "Filterscript");
58511274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          }
58611274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          return false;
58711274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        }
58811274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines      }
58978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      break;
59078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
59178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
59278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::Pointer: {
59311274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines      if (IsFilterscript) {
59411274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        if (ND) {
595d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet          Context->ReportError(Loc,
596d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                               "Pointers are forbidden in Filterscript: '%0'")
597d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet              << ND->getName();
59811274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          return false;
59911274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        } else {
60011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          // TODO(srhines): Find a better way to handle expressions (i.e. no
60111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          // NamedDecl) involving pointers in FS that should be allowed.
60211274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          // An example would be calls to library functions like
60311274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines          // rsMatrixMultiply() that take rs_matrixNxN * types.
60411274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines        }
60511274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines      }
60611274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines
607ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines      // Forbid pointers in structures that are externally visible.
608ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines      if (InCompositeType && IsExtern) {
609ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        if (ND) {
610ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines          Context->ReportError(Loc,
611ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines              "structures containing pointers cannot be used as the type of "
612ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines              "an exported global variable or the parameter to an exported "
613ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines              "function: '%0'")
614ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines            << ND->getName();
615ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        } else {
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");
620ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        }
621ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        return false;
622ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines      }
623ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines
624b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
625b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *PointeeType = GetPointeeType(PT);
62678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
627d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      return ValidateTypeHelper(Context, C, PointeeType, ND, Loc, SPS,
628d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                                InCompositeType, UnionDecl, TargetAPI,
629ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                IsFilterscript, IsExtern);
63078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
63178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
63278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::ExtVector: {
63378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      const clang::ExtVectorType *EVT =
634b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet              static_cast<const clang::ExtVectorType*>(CTI);
635b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *ElementType = GetExtVectorElementType(EVT);
636b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines      if (TargetAPI < SLANG_ICS_TARGET_API &&
637b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines          InCompositeType &&
638fdae63e8142f56521813f59e0c506ed3a1636021Stephen Hines          EVT->getNumElements() == 3 &&
639fdae63e8142f56521813f59e0c506ed3a1636021Stephen Hines          ND &&
64044f10063c2c08dab103a44cded0c3a288d65d43bStephen Hines          ND->getFormalLinkage() == clang::ExternalLinkage) {
6415abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes        ReportTypeError(Context, ND, nullptr,
642b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines                        "structs containing vectors of dimension 3 cannot "
643b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines                        "be exported at this API level: '%0'");
644b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines        return false;
645b13b85e31ac978254cb1ed7653f149f8a3eef460Stephen Hines      }
646d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
647ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                UnionDecl, TargetAPI, IsFilterscript, IsExtern);
64878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
64978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
65078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    case clang::Type::ConstantArray: {
651b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::ConstantArrayType *CAT = static_cast<const clang::ConstantArrayType*>(CTI);
652b095e05fef8f0230ab42eaed7a06c3b2d698189aJean-Luc Brouillet      const clang::Type *ElementType = GetConstantArrayElementType(CAT);
653d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
654ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                UnionDecl, TargetAPI, IsFilterscript, IsExtern);
65578e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
65678e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
65778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    default: {
65878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      break;
65978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines    }
66078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  }
66178e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
66278e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  return true;
663e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines}
664e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
665e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines}  // namespace
666e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
667eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouilletstd::string CreateDummyName(const char *type, const std::string &name) {
668eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  std::stringstream S;
669eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  S << "<" << type;
670eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  if (!name.empty()) {
671eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet    S << ":" << name;
672eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  }
673eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  S << ">";
674eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet  return S.str();
675eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet}
676eca0534a31b6185d6ab758f5e97acd7a4cb21e8eJean-Luc Brouillet
677e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines/****************************** RSExportType ******************************/
678e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hinesbool RSExportType::NormalizeType(const clang::Type *&T,
679e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines                                 llvm::StringRef &TypeName,
68048d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines                                 RSContext *Context,
68113fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                 const clang::VarDecl *VD,
68213fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines                                 ExportKind EK) {
68313fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines  if ((T = TypeExportable(T, Context, VD, EK)) == nullptr) {
684e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    return false;
685e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  }
686e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // Get type name
687e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  TypeName = RSExportType::GetTypeName(T);
68848d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines  if (Context && TypeName.empty()) {
68948d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    if (VD) {
690d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError(VD->getLocation(),
691d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           "anonymous types cannot be exported");
69248d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines    } else {
693d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet      Context->ReportError("anonymous types cannot be exported");
694e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    }
695e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    return false;
696e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  }
697e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
698e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  return true;
699e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines}
700e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
701d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouilletbool RSExportType::ValidateType(slang::RSContext *Context, clang::ASTContext &C,
7022907b2a2768bc32f75867513528c8d7419e44780David Gross                                clang::QualType QT, const clang::NamedDecl *ND,
703d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                                clang::SourceLocation Loc,
704ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                unsigned int TargetAPI, bool IsFilterscript,
705ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                                bool IsExtern) {
70611274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines  const clang::Type *T = QT.getTypePtr();
70778e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  llvm::SmallPtrSet<const clang::Type*, 8> SPS =
70878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines      llvm::SmallPtrSet<const clang::Type*, 8>();
70978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
710ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines  // If this is an externally visible variable declaration, we check if the
711ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines  // type is able to be exported first.
71233ea573b6df7b7fe48d2b68d4c479f33082e3c0dStephen Hines  if (auto VD = llvm::dyn_cast_or_null<clang::VarDecl>(ND)) {
713ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines    if (VD->getFormalLinkage() == clang::ExternalLinkage) {
71413fad85b3c99a37c17d8acfec72f46b8ee64e912Stephen Hines      if (!TypeExportable(T, Context, VD, NotLegacyKernelArgument)) {
715ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines        return false;
716ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines      }
717ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines    }
718ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines  }
7195abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  return ValidateTypeHelper(Context, C, T, ND, Loc, SPS, false, nullptr, TargetAPI,
720ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                            IsFilterscript, IsExtern);
72111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines}
72211274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines
723d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouilletbool RSExportType::ValidateVarDecl(slang::RSContext *Context,
724d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                                   clang::VarDecl *VD, unsigned int TargetAPI,
72511274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines                                   bool IsFilterscript) {
726d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet  return ValidateType(Context, VD->getASTContext(), VD->getType(), VD,
727ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                      VD->getLocation(), TargetAPI, IsFilterscript,
728ab94bccca64c9b126cbd1b732aa5e681d8639b99Stephen Hines                      (VD->getFormalLinkage() == clang::ExternalLinkage));
72978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines}
73078e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
731e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hinesconst clang::Type
732e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines*RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) {
733e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  if (DD) {
7348de1922e037612f2521acac2f4c4289a9f71450dStephen Hines    clang::QualType T = DD->getType();
735e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
736e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    if (T.isNull())
7375abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes      return nullptr;
738e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines    else
739e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines      return T.getTypePtr();
740e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  }
7415abbe0e9ca2508260b627ffef2bf01e2554e8357Chris Wailes  return nullptr;
742e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines}
743e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines
744e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hinesllvm::