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 #ifndef AIDL_TYPE_JAVA_H_ 18 #define AIDL_TYPE_JAVA_H_ 19 20 #include <string> 21 #include <vector> 22 23 #include "ast_java.h" 24 #include "type_namespace.h" 25 26 namespace android { 27 namespace aidl { 28 namespace java { 29 30 class JavaTypeNamespace; 31 32 class Type : public ValidatableType { 33 public: 34 // WriteToParcel flags 35 enum { PARCELABLE_WRITE_RETURN_VALUE = 0x0001 }; 36 37 Type(const JavaTypeNamespace* types, const std::string& name, int kind, 38 bool canWriteToParcel, bool canBeOut); 39 Type(const JavaTypeNamespace* types, const std::string& package, 40 const std::string& name, int kind, bool canWriteToParcel, bool canBeOut, 41 const std::string& declFile = "", int declLine = -1); 42 virtual ~Type() = default; 43 CanBeOutParameter()44 bool CanBeOutParameter() const override { return m_canBeOut; } CanWriteToParcel()45 bool CanWriteToParcel() const override { return m_canWriteToParcel; } 46 ArrayType()47 const ValidatableType* ArrayType() const override { return m_array_type.get(); } NullableType()48 const ValidatableType* NullableType() const override { return nullptr; } 49 JavaType()50 virtual std::string JavaType() const { return m_javaType; } 51 virtual std::string CreatorName() const; 52 virtual std::string InstantiableName() const; 53 54 virtual void WriteToParcel(StatementBlock* addTo, Variable* v, 55 Variable* parcel, int flags) const; 56 virtual void CreateFromParcel(StatementBlock* addTo, Variable* v, 57 Variable* parcel, Variable** cl) const; 58 virtual void ReadFromParcel(StatementBlock* addTo, Variable* v, 59 Variable* parcel, Variable** cl) const; 60 61 protected: 62 Expression* BuildWriteToParcelFlags(int flags) const; 63 64 const JavaTypeNamespace* m_types; 65 66 std::unique_ptr<Type> m_array_type; 67 68 private: 69 Type(); 70 Type(const Type&); 71 72 std::string m_javaType; 73 std::string m_declFile; 74 bool m_canWriteToParcel; 75 bool m_canBeOut; 76 }; 77 78 class BasicArrayType : public Type { 79 public: 80 BasicArrayType(const JavaTypeNamespace* types, const std::string& name, 81 const std::string& writeArrayParcel, 82 const std::string& createArrayParcel, 83 const std::string& readArrayParcel); 84 85 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 86 int flags) const override; 87 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 88 Variable** cl) const override; 89 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 90 Variable** cl) const override; NullableType()91 const ValidatableType* NullableType() const override { return this; } 92 93 private: 94 std::string m_writeArrayParcel; 95 std::string m_createArrayParcel; 96 std::string m_readArrayParcel; 97 }; 98 99 class BasicType : public Type { 100 public: 101 BasicType(const JavaTypeNamespace* types, const std::string& name, 102 const std::string& marshallParcel, 103 const std::string& unmarshallParcel, 104 const std::string& writeArrayParcel, 105 const std::string& createArrayParcel, 106 const std::string& readArrayParcel); 107 108 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 109 int flags) const override; 110 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 111 Variable** cl) const override; 112 113 private: 114 std::string m_marshallParcel; 115 std::string m_unmarshallParcel; 116 }; 117 118 class FileDescriptorArrayType : public Type { 119 public: 120 explicit FileDescriptorArrayType(const JavaTypeNamespace* types); 121 122 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 123 int flags) const override; 124 void CreateFromParcel(StatementBlock* addTo, Variable* v, 125 Variable* parcel, Variable** cl) const override; 126 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 127 Variable** cl) const override; NullableType()128 const ValidatableType* NullableType() const override { return this; } 129 }; 130 131 class FileDescriptorType : public Type { 132 public: 133 explicit FileDescriptorType(const JavaTypeNamespace* types); 134 135 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 136 int flags) const override; 137 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 138 Variable** cl) const override; 139 }; 140 141 class BooleanArrayType : public Type { 142 public: 143 explicit BooleanArrayType(const JavaTypeNamespace* types); 144 145 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 146 int flags) const override; 147 void CreateFromParcel(StatementBlock* addTo, Variable* v, 148 Variable* parcel, Variable** cl) const override; 149 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 150 Variable** cl) const override; NullableType()151 const ValidatableType* NullableType() const override { return this; } 152 }; 153 154 class BooleanType : public Type { 155 public: 156 explicit BooleanType(const JavaTypeNamespace* types); 157 158 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 159 int flags) const override; 160 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 161 Variable** cl) const override; 162 }; 163 164 class CharArrayType : public Type { 165 public: 166 explicit CharArrayType(const JavaTypeNamespace* types); 167 168 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 169 int flags) const override; 170 void CreateFromParcel(StatementBlock* addTo, Variable* v, 171 Variable* parcel, Variable** cl) const override; 172 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 173 Variable** cl) const override; NullableType()174 const ValidatableType* NullableType() const override { return this; } 175 }; 176 177 class CharType : public Type { 178 public: 179 explicit CharType(const JavaTypeNamespace* types); 180 181 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 182 int flags) const override; 183 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 184 Variable** cl) const override; 185 }; 186 187 class StringArrayType : public Type { 188 public: 189 explicit StringArrayType(const JavaTypeNamespace* types); 190 191 std::string CreatorName() const override; 192 193 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 194 int flags) const override; 195 void CreateFromParcel(StatementBlock* addTo, Variable* v, 196 Variable* parcel, Variable** cl) const override; 197 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 198 Variable** cl) const override; NullableType()199 const ValidatableType* NullableType() const override { return this; } 200 }; 201 202 class StringType : public Type { 203 public: 204 StringType(const JavaTypeNamespace* types, const std::string& package, 205 const std::string& class_name); 206 JavaType()207 std::string JavaType() const override { return "java.lang.String"; } 208 std::string CreatorName() const override; 209 210 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 211 int flags) const override; 212 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 213 Variable** cl) const override; NullableType()214 const ValidatableType* NullableType() const override { return this; } 215 }; 216 217 class CharSequenceType : public Type { 218 public: 219 explicit CharSequenceType(const JavaTypeNamespace* types); 220 221 std::string CreatorName() const override; 222 223 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 224 int flags) const override; 225 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 226 Variable** cl) const override; 227 }; 228 229 class RemoteExceptionType : public Type { 230 public: 231 explicit RemoteExceptionType(const JavaTypeNamespace* types); 232 233 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 234 int flags) const override; 235 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 236 Variable** cl) const override; 237 }; 238 239 class RuntimeExceptionType : public Type { 240 public: 241 explicit RuntimeExceptionType(const JavaTypeNamespace* types); 242 243 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 244 int flags) const override; 245 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 246 Variable** cl) const override; 247 }; 248 249 class IBinderArrayType : public Type { 250 public: 251 explicit IBinderArrayType(const JavaTypeNamespace* types); 252 253 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 254 int flags) const override; 255 void CreateFromParcel(StatementBlock* addTo, Variable* v, 256 Variable* parcel, Variable** cl) const override; 257 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 258 Variable** cl) const override; NullableType()259 const ValidatableType* NullableType() const override { return this; } 260 }; 261 262 class IBinderType : public Type { 263 public: 264 explicit IBinderType(const JavaTypeNamespace* types); 265 266 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 267 int flags) const override; 268 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 269 Variable** cl) const override; NullableType()270 const ValidatableType* NullableType() const override { return this; } 271 }; 272 273 class IInterfaceType : public Type { 274 public: 275 explicit IInterfaceType(const JavaTypeNamespace* types); 276 277 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 278 int flags) const override; 279 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 280 Variable** cl) const override; 281 }; 282 283 class BinderType : public Type { 284 public: 285 explicit BinderType(const JavaTypeNamespace* types); 286 287 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 288 int flags) const override; 289 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 290 Variable** cl) const override; 291 }; 292 293 class BinderProxyType : public Type { 294 public: 295 explicit BinderProxyType(const JavaTypeNamespace* types); 296 297 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 298 int flags) const override; 299 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 300 Variable** cl) const override; 301 }; 302 303 class ParcelType : public Type { 304 public: 305 explicit ParcelType(const JavaTypeNamespace* types); 306 307 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 308 int flags) const override; 309 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 310 Variable** cl) const override; NullableType()311 const ValidatableType* NullableType() const override { return this; } 312 }; 313 314 class ParcelableInterfaceType : public Type { 315 public: 316 explicit ParcelableInterfaceType(const JavaTypeNamespace* types); 317 318 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 319 int flags) const override; 320 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 321 Variable** cl) const override; 322 }; 323 324 class MapType : public Type { 325 public: 326 explicit MapType(const JavaTypeNamespace* types); 327 328 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 329 int flags) const override; 330 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 331 Variable** cl) const override; 332 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 333 Variable** cl) const override; NullableType()334 const ValidatableType* NullableType() const override { return this; } 335 }; 336 337 class ListType : public Type { 338 public: 339 explicit ListType(const JavaTypeNamespace* types); 340 341 std::string InstantiableName() const override; 342 343 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 344 int flags) const override; 345 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 346 Variable** cl) const override; 347 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 348 Variable** cl) const override; NullableType()349 const ValidatableType* NullableType() const override { return this; } 350 }; 351 352 class UserDataArrayType : public Type { 353 public: 354 UserDataArrayType(const JavaTypeNamespace* types, const std::string& package, 355 const std::string& name, bool builtIn, 356 bool canWriteToParcel, const std::string& declFile = "", 357 int declLine = -1); 358 359 std::string CreatorName() const override; 360 361 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 362 int flags) const override; 363 void CreateFromParcel(StatementBlock* addTo, Variable* v, 364 Variable* parcel, Variable** cl) const override; 365 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 366 Variable** cl) const override; NullableType()367 const ValidatableType* NullableType() const override { return this; } 368 }; 369 370 class UserDataType : public Type { 371 public: 372 UserDataType(const JavaTypeNamespace* types, const std::string& package, 373 const std::string& name, bool builtIn, bool canWriteToParcel, 374 const std::string& declFile = "", int declLine = -1); 375 376 std::string CreatorName() const override; 377 378 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 379 int flags) const override; 380 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 381 Variable** cl) const override; 382 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 383 Variable** cl) const override; NullableType()384 const ValidatableType* NullableType() const override { return this; } 385 }; 386 387 class InterfaceType : public Type { 388 public: 389 InterfaceType(const JavaTypeNamespace* types, const std::string& package, 390 const std::string& name, bool builtIn, bool oneway, 391 const std::string& declFile, int declLine, const Type* stub, 392 const Type* proxy); 393 394 bool OneWay() const; 395 396 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 397 int flags) const override; 398 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 399 Variable** cl) const override; NullableType()400 const ValidatableType* NullableType() const override { return this; } GetStub()401 const Type* GetStub() const { return stub_; } GetProxy()402 const Type* GetProxy() const { return proxy_; } 403 404 private: 405 bool m_oneway; 406 const Type* stub_; 407 const Type* proxy_; 408 }; 409 410 class ClassLoaderType : public Type { 411 public: 412 explicit ClassLoaderType(const JavaTypeNamespace* types); 413 }; 414 415 class GenericListType : public Type { 416 public: 417 GenericListType(const JavaTypeNamespace* types, const Type* arg); 418 419 std::string CreatorName() const override; 420 std::string InstantiableName() const override; JavaType()421 std::string JavaType() const override { 422 return "java.util.List<" + m_contained_type->JavaType() + ">"; 423 } 424 425 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 426 int flags) const override; 427 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 428 Variable** cl) const override; 429 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 430 Variable** cl) const override; NullableType()431 const ValidatableType* NullableType() const override { return this; } 432 433 private: 434 const Type* m_contained_type; 435 const std::string m_creator; 436 }; 437 438 class JavaTypeNamespace : public LanguageTypeNamespace<Type> { 439 public: 440 JavaTypeNamespace() = default; 441 virtual ~JavaTypeNamespace() = default; 442 443 void Init() override; 444 bool AddParcelableType(const AidlParcelable& p, 445 const std::string& filename) override; 446 bool AddBinderType(const AidlInterface& b, 447 const std::string& filename) override; 448 bool AddListType(const std::string& contained_type_name) override; 449 bool AddMapType(const std::string& key_type_name, 450 const std::string& value_type_name) override; 451 BoolType()452 const Type* BoolType() const { return m_bool_type; } IntType()453 const Type* IntType() const { return m_int_type; } StringType()454 const Type* StringType() const { return m_string_type; } TextUtilsType()455 const Type* TextUtilsType() const { return m_text_utils_type; } RemoteExceptionType()456 const Type* RemoteExceptionType() const { return m_remote_exception_type; } RuntimeExceptionType()457 const Type* RuntimeExceptionType() const { return m_runtime_exception_type; } IBinderType()458 const Type* IBinderType() const { return m_ibinder_type; } IInterfaceType()459 const Type* IInterfaceType() const { return m_iinterface_type; } BinderNativeType()460 const Type* BinderNativeType() const { return m_binder_native_type; } BinderProxyType()461 const Type* BinderProxyType() const { return m_binder_proxy_type; } ParcelType()462 const Type* ParcelType() const { return m_parcel_type; } ParcelableInterfaceType()463 const Type* ParcelableInterfaceType() const { 464 return m_parcelable_interface_type; 465 } ContextType()466 const Type* ContextType() const { return m_context_type; } ClassLoaderType()467 const Type* ClassLoaderType() const { return m_classloader_type; } 468 469 private: 470 const Type* m_bool_type{nullptr}; 471 const Type* m_int_type{nullptr}; 472 const Type* m_string_type{nullptr}; 473 const Type* m_text_utils_type{nullptr}; 474 const Type* m_remote_exception_type{nullptr}; 475 const Type* m_runtime_exception_type{nullptr}; 476 const Type* m_ibinder_type{nullptr}; 477 const Type* m_iinterface_type{nullptr}; 478 const Type* m_binder_native_type{nullptr}; 479 const Type* m_binder_proxy_type{nullptr}; 480 const Type* m_parcel_type{nullptr}; 481 const Type* m_parcelable_interface_type{nullptr}; 482 const Type* m_context_type{nullptr}; 483 const Type* m_classloader_type{nullptr}; 484 485 DISALLOW_COPY_AND_ASSIGN(JavaTypeNamespace); 486 }; 487 488 extern Expression* NULL_VALUE; 489 extern Expression* THIS_VALUE; 490 extern Expression* SUPER_VALUE; 491 extern Expression* TRUE_VALUE; 492 extern Expression* FALSE_VALUE; 493 494 } // namespace java 495 } // namespace aidl 496 } // namespace android 497 498 #endif // AIDL_TYPE_JAVA_H_ 499