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         PARCELABLE,
     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 canBeOut);
     28                     Type(const string& package, const string& name,
     29                             int kind, bool canWriteToParcel, 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     CanBeMarshalled() const     { return m_canWriteToParcel; }
     40     inline bool     CanBeOutParameter() const   { return m_canBeOut; }
     41 
     42     virtual string  ImportType() const;
     43     virtual string  CreatorName() const;
     44     virtual string  InstantiableName() const;
     45 
     46     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
     47                                     Variable* parcel, int flags);
     48     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
     49                                     Variable* parcel, Variable** cl);
     50     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
     51                                     Variable* parcel, Variable** cl);
     52 
     53     virtual bool    CanBeArray() const;
     54 
     55     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
     56                                     Variable* parcel, int flags);
     57     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
     58                                     Variable* parcel, Variable** cl);
     59     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
     60                                     Variable* parcel, Variable** cl);
     61 
     62 protected:
     63     void SetQualifiedName(const string& qualified);
     64     Expression* BuildWriteToParcelFlags(int flags);
     65 
     66 private:
     67     Type();
     68     Type(const Type&);
     69 
     70     string m_package;
     71     string m_name;
     72     string m_qualifiedName;
     73     string m_declFile;
     74     int m_declLine;
     75     int m_kind;
     76     bool m_canWriteToParcel;
     77     bool m_canBeOut;
     78 };
     79 
     80 class BasicType : public Type
     81 {
     82 public:
     83                     BasicType(const string& name, const string& marshallMethod,
     84                               const string& unmarshallMethod,
     85                               const string& writeArray,
     86                               const string& createArray,
     87                               const string& readArray);
     88 
     89     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
     90                                     Variable* parcel, int flags);
     91     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
     92                                     Variable* parcel, Variable** cl);
     93 
     94     virtual bool    CanBeArray() const;
     95 
     96     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
     97                                     Variable* parcel, int flags);
     98     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
     99                                     Variable* parcel, Variable** cl);
    100     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    101                                     Variable* parcel, Variable** cl);
    102 
    103 private:
    104     string m_marshallMethod;
    105     string m_unmarshallMethod;
    106     string m_writeArrayMethod;
    107     string m_createArrayMethod;
    108     string m_readArrayMethod;
    109 };
    110 
    111 class BooleanType : public Type
    112 {
    113 public:
    114                     BooleanType();
    115 
    116     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    117                                     Variable* parcel, int flags);
    118     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    119                                     Variable* parcel, Variable** cl);
    120 
    121     virtual bool    CanBeArray() const;
    122 
    123     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    124                                     Variable* parcel, int flags);
    125     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    126                                     Variable* parcel, Variable** cl);
    127     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    128                                     Variable* parcel, Variable** cl);
    129 };
    130 
    131 class CharType : public Type
    132 {
    133 public:
    134                     CharType();
    135 
    136     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    137                                     Variable* parcel, int flags);
    138     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    139                                     Variable* parcel, Variable** cl);
    140 
    141     virtual bool    CanBeArray() const;
    142 
    143     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    144                                     Variable* parcel, int flags);
    145     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    146                                     Variable* parcel, Variable** cl);
    147     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    148                                     Variable* parcel, Variable** cl);
    149 };
    150 
    151 
    152 class StringType : public Type
    153 {
    154 public:
    155                     StringType();
    156 
    157     virtual string  CreatorName() const;
    158 
    159     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    160                                     Variable* parcel, int flags);
    161     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    162                                     Variable* parcel, Variable** cl);
    163 
    164     virtual bool    CanBeArray() const;
    165 
    166     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    167                                     Variable* parcel, int flags);
    168     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    169                                     Variable* parcel, Variable** cl);
    170     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    171                                     Variable* parcel, Variable** cl);
    172 };
    173 
    174 class CharSequenceType : public Type
    175 {
    176 public:
    177                     CharSequenceType();
    178 
    179     virtual string  CreatorName() const;
    180 
    181     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    182                                     Variable* parcel, int flags);
    183     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    184                                     Variable* parcel, Variable** cl);
    185 };
    186 
    187 class RemoteExceptionType : public Type
    188 {
    189 public:
    190                     RemoteExceptionType();
    191 
    192     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    193                                     Variable* parcel, int flags);
    194     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    195                                     Variable* parcel, Variable** cl);
    196 };
    197 
    198 class RuntimeExceptionType : public Type
    199 {
    200 public:
    201                     RuntimeExceptionType();
    202 
    203     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    204                                     Variable* parcel, int flags);
    205     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    206                                     Variable* parcel, Variable** cl);
    207 };
    208 
    209 class IBinderType : public Type
    210 {
    211 public:
    212                     IBinderType();
    213 
    214     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    215                                     Variable* parcel, int flags);
    216     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    217                                     Variable* parcel, Variable** cl);
    218 
    219     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    220                                     Variable* parcel, int flags);
    221     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    222                                     Variable* parcel, Variable** cl);
    223     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    224                                     Variable* parcel, Variable** cl);
    225 };
    226 
    227 class IInterfaceType : public Type
    228 {
    229 public:
    230                     IInterfaceType();
    231 
    232     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    233                                     Variable* parcel, int flags);
    234     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    235                                     Variable* parcel, Variable** cl);
    236 };
    237 
    238 class BinderType : public Type
    239 {
    240 public:
    241                     BinderType();
    242 
    243     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    244                                     Variable* parcel, int flags);
    245     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    246                                     Variable* parcel, Variable** cl);
    247 };
    248 
    249 class BinderProxyType : public Type
    250 {
    251 public:
    252                     BinderProxyType();
    253 
    254     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    255                                     Variable* parcel, int flags);
    256     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    257                                     Variable* parcel, Variable** cl);
    258 };
    259 
    260 class ParcelType : public Type
    261 {
    262 public:
    263                     ParcelType();
    264 
    265     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    266                                     Variable* parcel, int flags);
    267     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    268                                     Variable* parcel, Variable** cl);
    269 };
    270 
    271 class ParcelableInterfaceType : public Type
    272 {
    273 public:
    274                     ParcelableInterfaceType();
    275 
    276     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    277                                     Variable* parcel, int flags);
    278     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    279                                     Variable* parcel, Variable** cl);
    280 };
    281 
    282 class MapType : public Type
    283 {
    284 public:
    285                     MapType();
    286 
    287     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    288                                     Variable* parcel, int flags);
    289     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    290                                     Variable* parcel, Variable** cl);
    291     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
    292                                     Variable* parcel, Variable** cl);
    293 };
    294 
    295 class ListType : public Type
    296 {
    297 public:
    298                     ListType();
    299 
    300     virtual string  InstantiableName() const;
    301 
    302     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    303                                     Variable* parcel, int flags);
    304     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    305                                     Variable* parcel, Variable** cl);
    306     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
    307                                     Variable* parcel, Variable** cl);
    308 };
    309 
    310 class ParcelableType : public Type
    311 {
    312 public:
    313                     ParcelableType(const string& package, const string& name,
    314                             bool builtIn, const string& declFile, int declLine);
    315 
    316     virtual string  CreatorName() const;
    317 
    318     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    319                                     Variable* parcel, int flags);
    320     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    321                                     Variable* parcel, Variable** cl);
    322     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
    323                                     Variable* parcel, Variable** cl);
    324 
    325     virtual bool    CanBeArray() const;
    326 
    327     virtual void    WriteArrayToParcel(StatementBlock* addTo, Variable* v,
    328                                     Variable* parcel, int flags);
    329     virtual void    CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
    330                                     Variable* parcel, Variable** cl);
    331     virtual void    ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
    332                                     Variable* parcel, Variable** cl);
    333 };
    334 
    335 class InterfaceType : public Type
    336 {
    337 public:
    338                     InterfaceType(const string& package, const string& name,
    339                             bool builtIn, bool oneway,
    340                             const string& declFile, int declLine);
    341 
    342     bool            OneWay() const;
    343 
    344     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    345                                     Variable* parcel, int flags);
    346     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    347                                     Variable* parcel, Variable** cl);
    348 
    349 private:
    350     bool m_oneway;
    351 };
    352 
    353 
    354 class GenericType : public Type
    355 {
    356 public:
    357                     GenericType(const string& package, const string& name,
    358                                  const vector<Type*>& args);
    359 
    360     string          GenericArguments() const;
    361 
    362     virtual string  ImportType() 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 private:
    372     string m_genericArguments;
    373     string m_importName;
    374     vector<Type*> m_args;
    375 };
    376 
    377 
    378 class GenericListType : public GenericType
    379 {
    380 public:
    381                     GenericListType(const string& package, const string& name,
    382                                  const vector<Type*>& args);
    383 
    384     virtual string  CreatorName() const;
    385     virtual string  InstantiableName() const;
    386 
    387     virtual void    WriteToParcel(StatementBlock* addTo, Variable* v,
    388                                     Variable* parcel, int flags);
    389     virtual void    CreateFromParcel(StatementBlock* addTo, Variable* v,
    390                                     Variable* parcel, Variable** cl);
    391     virtual void    ReadFromParcel(StatementBlock* addTo, Variable* v,
    392                                     Variable* parcel, Variable** cl);
    393 
    394 private:
    395     string m_creator;
    396 };
    397 
    398 class ClassLoaderType : public Type
    399 {
    400 public:
    401                     ClassLoaderType();
    402 };
    403 
    404 class Namespace
    405 {
    406 public:
    407             Namespace();
    408             ~Namespace();
    409     void    Add(Type* type);
    410 
    411     // args is the number of template types (what is this called?)
    412     void    AddGenericType(const string& package, const string& name, int args);
    413 
    414     // lookup a specific class name
    415     Type*   Find(const string& name) const;
    416     Type*   Find(const char* package, const char* name) const;
    417 
    418     // try to search by either a full name or a partial name
    419     Type*   Search(const string& name);
    420 
    421     void    Dump() const;
    422 
    423 private:
    424     struct Generic {
    425         string package;
    426         string name;
    427         string qualified;
    428         int args;
    429     };
    430 
    431     const Generic* search_generic(const string& name) const;
    432 
    433     vector<Type*> m_types;
    434     vector<Generic> m_generics;
    435 };
    436 
    437 extern Namespace NAMES;
    438 
    439 extern Type* VOID_TYPE;
    440 extern Type* BOOLEAN_TYPE;
    441 extern Type* CHAR_TYPE;
    442 extern Type* INT_TYPE;
    443 extern Type* LONG_TYPE;
    444 extern Type* FLOAT_TYPE;
    445 extern Type* DOUBLE_TYPE;
    446 extern Type* STRING_TYPE;
    447 extern Type* CHAR_SEQUENCE_TYPE;
    448 extern Type* TEXT_UTILS_TYPE;
    449 extern Type* REMOTE_EXCEPTION_TYPE;
    450 extern Type* RUNTIME_EXCEPTION_TYPE;
    451 extern Type* IBINDER_TYPE;
    452 extern Type* IINTERFACE_TYPE;
    453 extern Type* BINDER_NATIVE_TYPE;
    454 extern Type* BINDER_PROXY_TYPE;
    455 extern Type* PARCEL_TYPE;
    456 extern Type* PARCELABLE_INTERFACE_TYPE;
    457 
    458 extern Expression* NULL_VALUE;
    459 extern Expression* THIS_VALUE;
    460 extern Expression* SUPER_VALUE;
    461 extern Expression* TRUE_VALUE;
    462 extern Expression* FALSE_VALUE;
    463 
    464 void register_base_types();
    465 
    466 #endif // AIDL_TYPE_H
    467