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 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 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 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 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 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 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 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 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 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 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 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 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; 270 }; 271 272 class IInterfaceType : public Type { 273 public: 274 IInterfaceType(const JavaTypeNamespace* types); 275 276 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 277 int flags) const override; 278 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 279 Variable** cl) const override; 280 }; 281 282 class BinderType : public Type { 283 public: 284 BinderType(const JavaTypeNamespace* types); 285 286 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 287 int flags) const override; 288 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 289 Variable** cl) const override; 290 }; 291 292 class BinderProxyType : public Type { 293 public: 294 BinderProxyType(const JavaTypeNamespace* types); 295 296 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 297 int flags) const override; 298 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 299 Variable** cl) const override; 300 }; 301 302 class ParcelType : public Type { 303 public: 304 ParcelType(const JavaTypeNamespace* types); 305 306 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 307 int flags) const override; 308 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 309 Variable** cl) const override; NullableType()310 const ValidatableType* NullableType() const override { return this; } 311 }; 312 313 class ParcelableInterfaceType : public Type { 314 public: 315 ParcelableInterfaceType(const JavaTypeNamespace* types); 316 317 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 318 int flags) const override; 319 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 320 Variable** cl) const override; 321 }; 322 323 class MapType : public Type { 324 public: 325 MapType(const JavaTypeNamespace* types); 326 327 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 328 int flags) const override; 329 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 330 Variable** cl) const override; 331 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 332 Variable** cl) const override; NullableType()333 const ValidatableType* NullableType() const override { return this; } 334 }; 335 336 class ListType : public Type { 337 public: 338 ListType(const JavaTypeNamespace* types); 339 340 std::string InstantiableName() const override; 341 342 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 343 int flags) const override; 344 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 345 Variable** cl) const override; 346 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 347 Variable** cl) const override; NullableType()348 const ValidatableType* NullableType() const override { return this; } 349 }; 350 351 class UserDataArrayType : public Type { 352 public: 353 UserDataArrayType(const JavaTypeNamespace* types, const std::string& package, 354 const std::string& name, bool builtIn, 355 bool canWriteToParcel, const std::string& declFile = "", 356 int declLine = -1); 357 358 std::string CreatorName() const override; 359 360 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 361 int flags) const override; 362 void CreateFromParcel(StatementBlock* addTo, Variable* v, 363 Variable* parcel, Variable** cl) const override; 364 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 365 Variable** cl) const override; NullableType()366 const ValidatableType* NullableType() const override { return this; } 367 }; 368 369 class UserDataType : public Type { 370 public: 371 UserDataType(const JavaTypeNamespace* types, const std::string& package, 372 const std::string& name, bool builtIn, bool canWriteToParcel, 373 const std::string& declFile = "", int declLine = -1); 374 375 std::string CreatorName() const override; 376 377 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 378 int flags) const override; 379 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 380 Variable** cl) const override; 381 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 382 Variable** cl) const override; NullableType()383 const ValidatableType* NullableType() const override { return this; } 384 }; 385 386 class InterfaceType : public Type { 387 public: 388 InterfaceType(const JavaTypeNamespace* types, const std::string& package, 389 const std::string& name, bool builtIn, bool oneway, 390 const std::string& declFile, int declLine, const Type* stub, 391 const Type* proxy); 392 393 bool OneWay() const; 394 395 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 396 int flags) const override; 397 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 398 Variable** cl) const override; GetStub()399 const Type* GetStub() const { return stub_; } GetProxy()400 const Type* GetProxy() const { return proxy_; } 401 402 private: 403 bool m_oneway; 404 const Type* stub_; 405 const Type* proxy_; 406 }; 407 408 class ClassLoaderType : public Type { 409 public: 410 ClassLoaderType(const JavaTypeNamespace* types); 411 }; 412 413 class GenericListType : public Type { 414 public: 415 GenericListType(const JavaTypeNamespace* types, const Type* arg); 416 417 std::string CreatorName() const override; 418 std::string InstantiableName() const override; JavaType()419 std::string JavaType() const override { 420 return "java.util.List<" + m_contained_type->JavaType() + ">"; 421 } 422 423 void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 424 int flags) const override; 425 void CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 426 Variable** cl) const override; 427 void ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, 428 Variable** cl) const override; NullableType()429 const ValidatableType* NullableType() const override { return this; } 430 431 private: 432 const Type* m_contained_type; 433 const std::string m_creator; 434 }; 435 436 class JavaTypeNamespace : public LanguageTypeNamespace<Type> { 437 public: 438 JavaTypeNamespace() = default; 439 virtual ~JavaTypeNamespace() = default; 440 441 void Init() override; 442 bool AddParcelableType(const AidlParcelable& p, 443 const std::string& filename) override; 444 bool AddBinderType(const AidlInterface& b, 445 const std::string& filename) override; 446 bool AddListType(const std::string& contained_type_name) override; 447 bool AddMapType(const std::string& key_type_name, 448 const std::string& value_type_name) override; 449 BoolType()450 const Type* BoolType() const { return m_bool_type; } IntType()451 const Type* IntType() const { return m_int_type; } StringType()452 const Type* StringType() const { return m_string_type; } TextUtilsType()453 const Type* TextUtilsType() const { return m_text_utils_type; } RemoteExceptionType()454 const Type* RemoteExceptionType() const { return m_remote_exception_type; } RuntimeExceptionType()455 const Type* RuntimeExceptionType() const { return m_runtime_exception_type; } IBinderType()456 const Type* IBinderType() const { return m_ibinder_type; } IInterfaceType()457 const Type* IInterfaceType() const { return m_iinterface_type; } BinderNativeType()458 const Type* BinderNativeType() const { return m_binder_native_type; } BinderProxyType()459 const Type* BinderProxyType() const { return m_binder_proxy_type; } ParcelType()460 const Type* ParcelType() const { return m_parcel_type; } ParcelableInterfaceType()461 const Type* ParcelableInterfaceType() const { 462 return m_parcelable_interface_type; 463 } ContextType()464 const Type* ContextType() const { return m_context_type; } ClassLoaderType()465 const Type* ClassLoaderType() const { return m_classloader_type; } 466 467 private: 468 const Type* m_bool_type{nullptr}; 469 const Type* m_int_type{nullptr}; 470 const Type* m_string_type{nullptr}; 471 const Type* m_text_utils_type{nullptr}; 472 const Type* m_remote_exception_type{nullptr}; 473 const Type* m_runtime_exception_type{nullptr}; 474 const Type* m_ibinder_type{nullptr}; 475 const Type* m_iinterface_type{nullptr}; 476 const Type* m_binder_native_type{nullptr}; 477 const Type* m_binder_proxy_type{nullptr}; 478 const Type* m_parcel_type{nullptr}; 479 const Type* m_parcelable_interface_type{nullptr}; 480 const Type* m_context_type{nullptr}; 481 const Type* m_classloader_type{nullptr}; 482 483 DISALLOW_COPY_AND_ASSIGN(JavaTypeNamespace); 484 }; 485 486 extern Expression* NULL_VALUE; 487 extern Expression* THIS_VALUE; 488 extern Expression* SUPER_VALUE; 489 extern Expression* TRUE_VALUE; 490 extern Expression* FALSE_VALUE; 491 492 } // namespace java 493 } // namespace aidl 494 } // namespace android 495 496 #endif // AIDL_TYPE_JAVA_H_ 497