Home | History | Annotate | Download | only in aidl
      1 #ifndef AIDL_TYPE_H
      2 #define AIDL_TYPE_H
      3 
      4 #include "AST.h"
      5 #include <string>
      6 #include <vector>
      7 
      8 using namespace std;
      9 
     10 class Type
     11 {
     12 public:
     13     // kinds
     14     enum {
     15         BUILT_IN,
     16         USERDATA,
     17         INTERFACE,
     18         GENERATED
     19     };
     20 
     21     // WriteToParcel flags
     22     enum {
     23         PARCELABLE_WRITE_RETURN_VALUE = 0x0001
     24     };
     25 
     26                     Type(const string& name, int kind, bool canWriteToParcel,
     27                             bool canWriteToRpcData, bool canBeOut);
     28                     Type(const string& package, const string& name,
     29                             int kind, bool canWriteToParcel, bool canWriteToRpcData, bool canBeOut,
     30                             const string& declFile = "", int declLine = -1);
     31     virtual         ~Type();
     32 
     33     inline string   Package() const             { return m_package; }
     34     inline string   Name() const                { return m_name; }
     35     inline string   QualifiedName() const       { return m_qualifiedName; }
     36     inline int      Kind() const                { return m_kind; }
     37     inline string   DeclFile() const            { return m_declFile; }
     38     inline int      DeclLine() const            { return m_declLine; }
     39     inline bool     CanWriteToParcel() const    { return m_canWriteToParcel; }
     40     inline bool     CanWriteToRpcData() const   { return m_canWriteToRpcData; }
     41     inline bool     CanBeOutParameter() const   { return m_canBeOut; }
     42 
     43     virtual string  ImportType() const;
     44     virtual string  CreatorName() const;
     45     virtual string  RpcCreatorName() const;
     46     virtual string  InstantiableName() const;
     47 
     48     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
     49                                     Variable* parcel, int flags);
     50     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
     51                                     Variable* parcel, Variable** cl);
     52     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
     53                                     Variable* parcel, Variable** cl);
     54 
     55     virtual bool    CanBeArray() const;
     56 
     57     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
     58                                     Variable* parcel, int flags);
     59     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
     60                                     Variable* parcel, Variable** cl);
     61     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
     62                                     Variable* parcel, Variable** cl);
     63 
     64     virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
     65                                     Variable* data, int flags);
     66     virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
     67                                     Variable* data, Variable** cl);
     68 
     69 protected:
     70     void SetQualifiedName(const string& qualified);
     71     Expression* BuildWriteToParcelFlags(int flags);
     72 
     73 private:
     74     Type();
     75     Type(const Type&);
     76 
     77     string m_package;
     78     string m_name;
     79     string m_qualifiedName;
     80     string m_declFile;
     81     int m_declLine;
     82     int m_kind;
     83     bool m_canWriteToParcel;
     84     bool m_canWriteToRpcData;
     85     bool m_canBeOut;
     86 };
     87 
     88 class BasicType : public Type
     89 {
     90 public:
     91                     BasicType(const string& name,
     92                               const string& marshallParcel,
     93                               const string& unmarshallParcel,
     94                               const string& writeArrayParcel,
     95                               const string& createArrayParcel,
     96                               const string& readArrayParcel,
     97                               const string& marshallRpc,
     98                               const string& unmarshallRpc,
     99                               const string& writeArrayRpc,
    100                               const string& createArrayRpc,
    101                               const string& readArrayRpc);
    102 
    103     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    104                                     Variable* parcel, int flags);
    105     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    106                                     Variable* parcel, Variable** cl);
    107 
    108     virtual bool    CanBeArray() const;
    109 
    110     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    111                                     Variable* parcel, int flags);
    112     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    113                                     Variable* parcel, Variable** cl);
    114     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    115                                     Variable* parcel, Variable** cl);
    116 
    117     virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    118                                     Variable* data, int flags);
    119     virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    120                                     Variable* data, Variable** cl);
    121 
    122 private:
    123     string m_marshallParcel;
    124     string m_unmarshallParcel;
    125     string m_writeArrayParcel;
    126     string m_createArrayParcel;
    127     string m_readArrayParcel;
    128     string m_marshallRpc;
    129     string m_unmarshallRpc;
    130     string m_writeArrayRpc;
    131     string m_createArrayRpc;
    132     string m_readArrayRpc;
    133 };
    134 
    135 class BooleanType : public Type
    136 {
    137 public:
    138                     BooleanType();
    139 
    140     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    141                                     Variable* parcel, int flags);
    142     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    143                                     Variable* parcel, Variable** cl);
    144 
    145     virtual bool    CanBeArray() const;
    146 
    147     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    148                                     Variable* parcel, int flags);
    149     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    150                                     Variable* parcel, Variable** cl);
    151     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    152                                     Variable* parcel, Variable** cl);
    153 
    154     virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    155                                     Variable* data, int flags);
    156     virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    157                                     Variable* data, Variable** cl);
    158 };
    159 
    160 class CharType : public Type
    161 {
    162 public:
    163                     CharType();
    164 
    165     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    166                                     Variable* parcel, int flags);
    167     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    168                                     Variable* parcel, Variable** cl);
    169 
    170     virtual bool    CanBeArray() const;
    171 
    172     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    173                                     Variable* parcel, int flags);
    174     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    175                                     Variable* parcel, Variable** cl);
    176     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    177                                     Variable* parcel, Variable** cl);
    178 
    179     virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    180                                     Variable* data, int flags);
    181     virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    182                                     Variable* data, Variable** cl);
    183 };
    184 
    185 
    186 class StringType : public Type
    187 {
    188 public:
    189                     StringType();
    190 
    191     virtual string  CreatorName() const;
    192 
    193     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    194                                     Variable* parcel, int flags);
    195     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    196                                     Variable* parcel, Variable** cl);
    197 
    198     virtual bool    CanBeArray() const;
    199 
    200     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    201                                     Variable* parcel, int flags);
    202     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    203                                     Variable* parcel, Variable** cl);
    204     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    205                                     Variable* parcel, Variable** cl);
    206 
    207     virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    208                                     Variable* data, int flags);
    209     virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    210                                     Variable* data, Variable** cl);
    211 };
    212 
    213 class CharSequenceType : public Type
    214 {
    215 public:
    216                     CharSequenceType();
    217 
    218     virtual string  CreatorName() const;
    219 
    220     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    221                                     Variable* parcel, int flags);
    222     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    223                                     Variable* parcel, Variable** cl);
    224 };
    225 
    226 class RemoteExceptionType : public Type
    227 {
    228 public:
    229                     RemoteExceptionType();
    230 
    231     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    232                                     Variable* parcel, int flags);
    233     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    234                                     Variable* parcel, Variable** cl);
    235 };
    236 
    237 class RuntimeExceptionType : public Type
    238 {
    239 public:
    240                     RuntimeExceptionType();
    241 
    242     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    243                                     Variable* parcel, int flags);
    244     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    245                                     Variable* parcel, Variable** cl);
    246 };
    247 
    248 class IBinderType : public Type
    249 {
    250 public:
    251                     IBinderType();
    252 
    253     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    254                                     Variable* parcel, int flags);
    255     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    256                                     Variable* parcel, Variable** cl);
    257 
    258     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    259                                     Variable* parcel, int flags);
    260     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    261                                     Variable* parcel, Variable** cl);
    262     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    263                                     Variable* parcel, Variable** cl);
    264 };
    265 
    266 class IInterfaceType : public Type
    267 {
    268 public:
    269                     IInterfaceType();
    270 
    271     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    272                                     Variable* parcel, int flags);
    273     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    274                                     Variable* parcel, Variable** cl);
    275 };
    276 
    277 class BinderType : public Type
    278 {
    279 public:
    280                     BinderType();
    281 
    282     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    283                                     Variable* parcel, int flags);
    284     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    285                                     Variable* parcel, Variable** cl);
    286 };
    287 
    288 class BinderProxyType : public Type
    289 {
    290 public:
    291                     BinderProxyType();
    292 
    293     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    294                                     Variable* parcel, int flags);
    295     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    296                                     Variable* parcel, Variable** cl);
    297 };
    298 
    299 class ParcelType : public Type
    300 {
    301 public:
    302                     ParcelType();
    303 
    304     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    305                                     Variable* parcel, int flags);
    306     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    307                                     Variable* parcel, Variable** cl);
    308 };
    309 
    310 class ParcelableInterfaceType : public Type
    311 {
    312 public:
    313                     ParcelableInterfaceType();
    314 
    315     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    316                                     Variable* parcel, int flags);
    317     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    318                                     Variable* parcel, Variable** cl);
    319 };
    320 
    321 class MapType : public Type
    322 {
    323 public:
    324                     MapType();
    325 
    326     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    327                                     Variable* parcel, int flags);
    328     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    329                                     Variable* parcel, Variable** cl);
    330     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
    331                                     Variable* parcel, Variable** cl);
    332 };
    333 
    334 class ListType : public Type
    335 {
    336 public:
    337                     ListType();
    338 
    339     virtual string  InstantiableName() const;
    340 
    341     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    342                                     Variable* parcel, int flags);
    343     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    344                                     Variable* parcel, Variable** cl);
    345     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
    346                                     Variable* parcel, Variable** cl);
    347 
    348     virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    349                                     Variable* data, int flags);
    350     virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    351                                     Variable* data, Variable** cl);
    352 };
    353 
    354 class UserDataType : public Type
    355 {
    356 public:
    357                     UserDataType(const string& package, const string& name,
    358                             bool builtIn, bool canWriteToParcel, bool canWriteToRpcData,
    359                             const string& declFile = "", int declLine = -1);
    360 
    361     virtual string  CreatorName() const;
    362     virtual string  RpcCreatorName() const;
    363 
    364     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    365                                     Variable* parcel, int flags);
    366     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    367                                     Variable* parcel, Variable** cl);
    368     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
    369                                     Variable* parcel, Variable** cl);
    370 
    371     virtual bool    CanBeArray() const;
    372 
    373     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    374                                     Variable* parcel, int flags);
    375     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    376                                     Variable* parcel, Variable** cl);
    377     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    378                                     Variable* parcel, Variable** cl);
    379 
    380     virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    381                                     Variable* data, int flags);
    382     virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    383                                     Variable* data, Variable** cl);
    384 };
    385 
    386 class InterfaceType : public Type
    387 {
    388 public:
    389                     InterfaceType(const string& package, const string& name,
    390                             bool builtIn, bool oneway,
    391                             const string& declFile, int declLine);
    392 
    393     bool            OneWay() const;
    394 
    395     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    396                                     Variable* parcel, int flags);
    397     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    398                                     Variable* parcel, Variable** cl);
    399 
    400 private:
    401     bool m_oneway;
    402 };
    403 
    404 
    405 class GenericType : public Type
    406 {
    407 public:
    408                     GenericType(const string& package, const string& name,
    409                                  const vector<Type*>& args);
    410 
    411     const vector<Type*>& GenericArgumentTypes() const;
    412     string          GenericArguments() const;
    413 
    414     virtual string  ImportType() const;
    415 
    416     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    417                                     Variable* parcel, int flags);
    418     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    419                                     Variable* parcel, Variable** cl);
    420     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
    421                                     Variable* parcel, Variable** cl);
    422 
    423 private:
    424     string m_genericArguments;
    425     string m_importName;
    426     vector<Type*> m_args;
    427 };
    428 
    429 class RpcDataType : public UserDataType
    430 {
    431 public:
    432                     RpcDataType();
    433 
    434     virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    435                                     Variable* data, int flags);
    436     virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    437                                     Variable* data, Variable** cl);
    438 };
    439 
    440 class ClassLoaderType : public Type
    441 {
    442 public:
    443                     ClassLoaderType();
    444 };
    445 
    446 class GenericListType : public GenericType
    447 {
    448 public:
    449                     GenericListType(const string& package, const string& name,
    450                                  const vector<Type*>& args);
    451 
    452     virtual string  CreatorName() const;
    453     virtual string  InstantiableName() const;
    454 
    455     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    456                                     Variable* parcel, int flags);
    457     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    458                                     Variable* parcel, Variable** cl);
    459     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
    460                                     Variable* parcel, Variable** cl);
    461 
    462     virtual void    WriteToRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    463                                     Variable* data, int flags);
    464     virtual void    CreateFromRpcData(StatementBlock* addTo, Expression* k, Variable* v,
    465                                     Variable* data, Variable** cl);
    466 
    467 private:
    468     string m_creator;
    469 };
    470 
    471 class Namespace
    472 {
    473 public:
    474             Namespace();
    475             ~Namespace();
    476     void    Add(Type* type);
    477 
    478     // args is the number of template types (what is this called?)
    479     void    AddGenericType(const string& package, const string& name, int args);
    480 
    481     // lookup a specific class name
    482     Type*   Find(const string& name) const;
    483     Type*   Find(const char* package, const char* name) const;
    484 
    485     // try to search by either a full name or a partial name
    486     Type*   Search(const string& name);
    487 
    488     void    Dump() const;
    489 
    490 private:
    491     struct Generic {
    492         string package;
    493         string name;
    494         string qualified;
    495         int args;
    496     };
    497 
    498     const Generic* search_generic(const string& name) const;
    499 
    500     vector<Type*> m_types;
    501     vector<Generic> m_generics;
    502 };
    503 
    504 extern Namespace NAMES;
    505 
    506 extern Type* VOID_TYPE;
    507 extern Type* BOOLEAN_TYPE;
    508 extern Type* BYTE_TYPE;
    509 extern Type* CHAR_TYPE;
    510 extern Type* INT_TYPE;
    511 extern Type* LONG_TYPE;
    512 extern Type* FLOAT_TYPE;
    513 extern Type* DOUBLE_TYPE;
    514 extern Type* OBJECT_TYPE;
    515 extern Type* STRING_TYPE;
    516 extern Type* CHAR_SEQUENCE_TYPE;
    517 extern Type* TEXT_UTILS_TYPE;
    518 extern Type* REMOTE_EXCEPTION_TYPE;
    519 extern Type* RUNTIME_EXCEPTION_TYPE;
    520 extern Type* IBINDER_TYPE;
    521 extern Type* IINTERFACE_TYPE;
    522 extern Type* BINDER_NATIVE_TYPE;
    523 extern Type* BINDER_PROXY_TYPE;
    524 extern Type* PARCEL_TYPE;
    525 extern Type* PARCELABLE_INTERFACE_TYPE;
    526 
    527 extern Type* CONTEXT_TYPE;
    528 
    529 extern Type* RPC_DATA_TYPE;
    530 extern Type* RPC_ERROR_TYPE;
    531 extern Type* RPC_CONTEXT_TYPE;
    532 extern Type* EVENT_FAKE_TYPE;
    533 
    534 extern Expression* NULL_VALUE;
    535 extern Expression* THIS_VALUE;
    536 extern Expression* SUPER_VALUE;
    537 extern Expression* TRUE_VALUE;
    538 extern Expression* FALSE_VALUE;
    539 
    540 void register_base_types();
    541 
    542 #endif // AIDL_TYPE_H
    543