1 /* 2 * Copyright (C) 2015, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <string> 20 #include <vector> 21 22 #include "ast_java.h" 23 #include "type_namespace.h" 24 25 namespace android { 26 namespace aidl { 27 namespace java { 28 29 class JavaTypeNamespace; 30 31 class Type : public ValidatableType { 32 public: 33 // WriteToParcel flags 34 enum { PARCELABLE_WRITE_RETURN_VALUE = 0x0001 }; 35 36 // defaultValue is by default set to "null" because that is the default value 37 // for most of the types like class and array. default values for built-in 38 // types like int, double, boolean, etc. are explicitly set via BasicType 39 Type(const JavaTypeNamespace* types, const std::string& name, int kind, bool canWriteToParcel); 40 Type(const JavaTypeNamespace* types, const std::string& package, const std::string& name, 41 int kind, bool canWriteToParcel, const std::string& declFile = "", int declLine = -1); 42 virtual ~Type() = default; 43 CanWriteToParcel()44 bool CanWriteToParcel() const override { return m_canWriteToParcel; } 45 ArrayType()46 const ValidatableType* ArrayType() const override { return m_array_type.get(); } NullableType()47 const ValidatableType* NullableType() const override { return nullptr; } 48 JavaType()49 virtual std::string JavaType() const { return m_javaType; } 50 virtual std::string InstantiableName() const; 51 52 // The namespace where this type is defined in GetTypeNamespace()53 const JavaTypeNamespace* GetTypeNamespace() const { return m_types; } 54 55 protected: 56 Expression* BuildWriteToParcelFlags(int flags) const; 57 58 const JavaTypeNamespace* m_types; 59 60 std::unique_ptr<Type> m_array_type; 61 62 private: 63 Type(); 64 Type(const Type&); 65 66 std::string m_javaType; 67 std::string m_declFile; 68 bool m_canWriteToParcel; 69 }; 70 71 class BasicArrayType : public Type { 72 public: 73 BasicArrayType(const JavaTypeNamespace* types, const std::string& name, 74 const std::string& writeArrayParcel, 75 const std::string& createArrayParcel, 76 const std::string& readArrayParcel); 77 NullableType()78 const ValidatableType* NullableType() const override { return this; } 79 80 private: 81 std::string m_writeArrayParcel; 82 std::string m_createArrayParcel; 83 std::string m_readArrayParcel; 84 }; 85 86 class BasicType : public Type { 87 public: 88 BasicType(const JavaTypeNamespace* types, const std::string& name, 89 const std::string& marshallParcel, const std::string& unmarshallParcel, 90 const std::string& writeArrayParcel, const std::string& createArrayParcel, 91 const std::string& readArrayParcel); 92 93 private: 94 std::string m_marshallParcel; 95 std::string m_unmarshallParcel; 96 }; 97 98 class FileDescriptorArrayType : public Type { 99 public: 100 explicit FileDescriptorArrayType(const JavaTypeNamespace* types); 101 NullableType()102 const ValidatableType* NullableType() const override { return this; } 103 }; 104 105 class FileDescriptorType : public Type { 106 public: 107 explicit FileDescriptorType(const JavaTypeNamespace* types); 108 }; 109 110 class ParcelFileDescriptorArrayType : public Type { 111 public: 112 explicit ParcelFileDescriptorArrayType(const JavaTypeNamespace* types); 113 NullableType()114 const ValidatableType* NullableType() const override { return this; } 115 }; 116 117 class ParcelFileDescriptorType : public Type { 118 public: 119 explicit ParcelFileDescriptorType(const JavaTypeNamespace* types); 120 NullableType()121 const ValidatableType* NullableType() const override { return this; } 122 }; 123 124 class BooleanArrayType : public Type { 125 public: 126 explicit BooleanArrayType(const JavaTypeNamespace* types); NullableType()127 const ValidatableType* NullableType() const override { return this; } 128 }; 129 130 class BooleanType : public Type { 131 public: 132 explicit BooleanType(const JavaTypeNamespace* types); 133 }; 134 135 class CharArrayType : public Type { 136 public: 137 explicit CharArrayType(const JavaTypeNamespace* types); NullableType()138 const ValidatableType* NullableType() const override { return this; } 139 }; 140 141 class CharType : public Type { 142 public: 143 explicit CharType(const JavaTypeNamespace* types); 144 }; 145 146 class StringArrayType : public Type { 147 public: 148 explicit StringArrayType(const JavaTypeNamespace* types); 149 NullableType()150 const ValidatableType* NullableType() const override { return this; } 151 }; 152 153 class StringType : public Type { 154 public: 155 StringType(const JavaTypeNamespace* types, const std::string& package, 156 const std::string& class_name); 157 JavaType()158 std::string JavaType() const override { return "java.lang.String"; } NullableType()159 const ValidatableType* NullableType() const override { return this; } 160 }; 161 162 class CharSequenceType : public Type { 163 public: 164 explicit CharSequenceType(const JavaTypeNamespace* types); 165 }; 166 167 class RemoteExceptionType : public Type { 168 public: 169 explicit RemoteExceptionType(const JavaTypeNamespace* types); 170 }; 171 172 class RuntimeExceptionType : public Type { 173 public: 174 explicit RuntimeExceptionType(const JavaTypeNamespace* types); 175 }; 176 177 class IBinderArrayType : public Type { 178 public: 179 explicit IBinderArrayType(const JavaTypeNamespace* types); NullableType()180 const ValidatableType* NullableType() const override { return this; } 181 }; 182 183 class IBinderType : public Type { 184 public: 185 explicit IBinderType(const JavaTypeNamespace* types); NullableType()186 const ValidatableType* NullableType() const override { return this; } 187 }; 188 189 class IInterfaceType : public Type { 190 public: 191 explicit IInterfaceType(const JavaTypeNamespace* types); 192 }; 193 194 class BinderType : public Type { 195 public: 196 explicit BinderType(const JavaTypeNamespace* types); 197 }; 198 199 class BinderProxyType : public Type { 200 public: 201 explicit BinderProxyType(const JavaTypeNamespace* types); 202 }; 203 204 class ParcelType : public Type { 205 public: 206 explicit ParcelType(const JavaTypeNamespace* types); NullableType()207 const ValidatableType* NullableType() const override { return this; } 208 }; 209 210 class ParcelableInterfaceType : public Type { 211 public: 212 explicit ParcelableInterfaceType(const JavaTypeNamespace* types); 213 }; 214 215 class MapType : public Type { 216 public: 217 explicit MapType(const JavaTypeNamespace* types); NullableType()218 const ValidatableType* NullableType() const override { return this; } 219 }; 220 221 class ListType : public Type { 222 public: 223 explicit ListType(const JavaTypeNamespace* types); 224 225 std::string InstantiableName() const override; NullableType()226 const ValidatableType* NullableType() const override { return this; } 227 }; 228 229 class UserDataArrayType : public Type { 230 public: 231 UserDataArrayType(const JavaTypeNamespace* types, const std::string& package, 232 const std::string& name, bool builtIn, 233 bool canWriteToParcel, const std::string& declFile = "", 234 int declLine = -1); 235 NullableType()236 const ValidatableType* NullableType() const override { return this; } 237 }; 238 239 class UserDataType : public Type { 240 public: 241 UserDataType(const JavaTypeNamespace* types, const std::string& package, 242 const std::string& name, bool builtIn, bool canWriteToParcel, 243 const std::string& declFile = "", int declLine = -1); 244 NullableType()245 const ValidatableType* NullableType() const override { return this; } 246 }; 247 248 class InterfaceType : public Type { 249 public: 250 InterfaceType(const JavaTypeNamespace* types, const std::string& package, const std::string& name, 251 bool builtIn, const std::string& declFile, int declLine, const Type* stub, 252 const Type* proxy, const Type* defaultImpl); 253 NullableType()254 const ValidatableType* NullableType() const override { return this; } GetStub()255 const Type* GetStub() const { return stub_; } GetProxy()256 const Type* GetProxy() const { return proxy_; } GetDefaultImpl()257 const Type* GetDefaultImpl() const { return defaultImpl_; } 258 259 private: 260 const Type* stub_; 261 const Type* proxy_; 262 const Type* defaultImpl_; 263 }; 264 265 class ClassLoaderType : public Type { 266 public: 267 explicit ClassLoaderType(const JavaTypeNamespace* types); 268 }; 269 270 class GenericListType : public Type { 271 public: 272 GenericListType(const JavaTypeNamespace* types, const Type* arg); 273 274 std::string InstantiableName() const override; JavaType()275 std::string JavaType() const override { 276 return "java.util.List<" + m_contained_type->JavaType() + ">"; 277 } 278 NullableType()279 const ValidatableType* NullableType() const override { return this; } 280 281 private: 282 const Type* m_contained_type; 283 }; 284 285 class JavaTypeNamespace : public LanguageTypeNamespace<Type> { 286 public: 287 JavaTypeNamespace() = default; 288 virtual ~JavaTypeNamespace() = default; 289 290 void Init() override; 291 bool AddParcelableType(const AidlParcelable& p, 292 const std::string& filename) override; 293 bool AddBinderType(const AidlInterface& b, 294 const std::string& filename) override; 295 bool AddListType(const std::string& contained_type_name) override; 296 bool AddMapType(const std::string& key_type_name, 297 const std::string& value_type_name) override; 298 BoolType()299 const Type* BoolType() const { return m_bool_type; } IntType()300 const Type* IntType() const { return m_int_type; } StringType()301 const Type* StringType() const { return m_string_type; } TextUtilsType()302 const Type* TextUtilsType() const { return m_text_utils_type; } RemoteExceptionType()303 const Type* RemoteExceptionType() const { return m_remote_exception_type; } RuntimeExceptionType()304 const Type* RuntimeExceptionType() const { return m_runtime_exception_type; } IBinderType()305 const Type* IBinderType() const { return m_ibinder_type; } IInterfaceType()306 const Type* IInterfaceType() const { return m_iinterface_type; } BinderNativeType()307 const Type* BinderNativeType() const { return m_binder_native_type; } BinderProxyType()308 const Type* BinderProxyType() const { return m_binder_proxy_type; } ParcelType()309 const Type* ParcelType() const { return m_parcel_type; } ParcelableInterfaceType()310 const Type* ParcelableInterfaceType() const { 311 return m_parcelable_interface_type; 312 } ContextType()313 const Type* ContextType() const { return m_context_type; } ClassLoaderType()314 const Type* ClassLoaderType() const { return m_classloader_type; } 315 316 private: 317 const Type* m_bool_type{nullptr}; 318 const Type* m_int_type{nullptr}; 319 const Type* m_string_type{nullptr}; 320 const Type* m_text_utils_type{nullptr}; 321 const Type* m_remote_exception_type{nullptr}; 322 const Type* m_runtime_exception_type{nullptr}; 323 const Type* m_ibinder_type{nullptr}; 324 const Type* m_iinterface_type{nullptr}; 325 const Type* m_binder_native_type{nullptr}; 326 const Type* m_binder_proxy_type{nullptr}; 327 const Type* m_parcel_type{nullptr}; 328 const Type* m_parcelable_interface_type{nullptr}; 329 const Type* m_context_type{nullptr}; 330 const Type* m_classloader_type{nullptr}; 331 332 DISALLOW_COPY_AND_ASSIGN(JavaTypeNamespace); 333 }; 334 335 extern Expression* NULL_VALUE; 336 extern Expression* THIS_VALUE; 337 extern Expression* SUPER_VALUE; 338 extern Expression* TRUE_VALUE; 339 extern Expression* FALSE_VALUE; 340 341 } // namespace java 342 } // namespace aidl 343 } // namespace android 344