Home | History | Annotate | Download | only in hidl
      1 /*
      2  * Copyright (C) 2016 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 TYPE_H_
     18 
     19 #define TYPE_H_
     20 
     21 #include <android-base/macros.h>
     22 #include <string>
     23 #include <utils/Errors.h>
     24 #include <vector>
     25 #include <set>
     26 
     27 namespace android {
     28 
     29 struct Annotation;
     30 struct Formatter;
     31 struct ScalarType;
     32 struct FQName;
     33 
     34 struct Type {
     35     Type();
     36     virtual ~Type();
     37 
     38     virtual bool isArray() const;
     39     virtual bool isBinder() const;
     40     virtual bool isBitField() const;
     41     virtual bool isCompoundType() const;
     42     virtual bool isEnum() const;
     43     virtual bool isHandle() const;
     44     virtual bool isInterface() const;
     45     virtual bool isNamedType() const;
     46     virtual bool isMemory() const;
     47     virtual bool isPointer() const;
     48     virtual bool isScope() const;
     49     virtual bool isScalar() const;
     50     virtual bool isString() const;
     51     virtual bool isTemplatedType() const;
     52     virtual bool isTypeDef() const;
     53     virtual bool isVector() const;
     54 
     55     virtual const ScalarType *resolveToScalarType() const;
     56 
     57     virtual std::string typeName() const = 0;
     58 
     59     bool isValidEnumStorageType() const;
     60     virtual bool isElidableType() const;
     61     virtual bool canCheckEquality() const;
     62 
     63     enum StorageMode {
     64         StorageMode_Stack,
     65         StorageMode_Argument,
     66         StorageMode_Result,
     67     };
     68 
     69     // specifyNamespaces: whether to specify namespaces for built-in types
     70     virtual std::string getCppType(
     71             StorageMode mode,
     72             bool specifyNamespaces) const;
     73 
     74     std::string decorateCppName(
     75             const std::string &name,
     76             StorageMode mode,
     77             bool specifyNamespaces) const;
     78 
     79     std::string getCppStackType(bool specifyNamespaces = true) const;
     80 
     81     std::string getCppResultType(bool specifyNamespaces = true) const;
     82 
     83     std::string getCppArgumentType(bool specifyNamespaces = true) const;
     84 
     85     // For an array type, dimensionality information will be accumulated at the
     86     // end of the returned string.
     87     // if forInitializer == true, actual dimensions are included, i.e. [3][5],
     88     // otherwise (and by default), they are omitted, i.e. [][].
     89     virtual std::string getJavaType(bool forInitializer = false) const;
     90 
     91     virtual std::string getJavaWrapperType() const;
     92     virtual std::string getJavaSuffix() const;
     93 
     94     virtual std::string getVtsType() const;
     95     virtual std::string getVtsValueName() const;
     96 
     97     enum ErrorMode {
     98         ErrorMode_Ignore,
     99         ErrorMode_Goto,
    100         ErrorMode_Break,
    101         ErrorMode_Return,
    102     };
    103     virtual void emitReaderWriter(
    104             Formatter &out,
    105             const std::string &name,
    106             const std::string &parcelObj,
    107             bool parcelObjIsPointer,
    108             bool isReader,
    109             ErrorMode mode) const;
    110 
    111     virtual void emitReaderWriterEmbedded(
    112             Formatter &out,
    113             size_t depth,
    114             const std::string &name,
    115             const std::string &sanitizedName,
    116             bool nameIsPointer,
    117             const std::string &parcelObj,
    118             bool parcelObjIsPointer,
    119             bool isReader,
    120             ErrorMode mode,
    121             const std::string &parentName,
    122             const std::string &offsetText) const;
    123 
    124     virtual void emitResolveReferences(
    125             Formatter &out,
    126             const std::string &name,
    127             bool nameIsPointer,
    128             const std::string &parcelObj,
    129             bool parcelObjIsPointer,
    130             bool isReader,
    131             ErrorMode mode) const;
    132 
    133     virtual void emitResolveReferencesEmbedded(
    134             Formatter &out,
    135             size_t depth,
    136             const std::string &name,
    137             const std::string &sanitizedName,
    138             bool nameIsPointer,
    139             const std::string &parcelObj,
    140             bool parcelObjIsPointer,
    141             bool isReader,
    142             ErrorMode mode,
    143             const std::string &parentName,
    144             const std::string &offsetText) const;
    145 
    146     virtual void emitDump(
    147             Formatter &out,
    148             const std::string &streamName,
    149             const std::string &name) const;
    150 
    151     virtual void emitJavaDump(
    152             Formatter &out,
    153             const std::string &streamName,
    154             const std::string &name) const;
    155 
    156     virtual bool useParentInEmitResolveReferencesEmbedded() const;
    157 
    158     virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
    159 
    160     virtual void emitJavaReaderWriter(
    161             Formatter &out,
    162             const std::string &parcelObj,
    163             const std::string &argName,
    164             bool isReader) const;
    165 
    166     virtual void emitJavaFieldInitializer(
    167             Formatter &out,
    168             const std::string &fieldName) const;
    169 
    170     virtual void emitJavaFieldReaderWriter(
    171             Formatter &out,
    172             size_t depth,
    173             const std::string &parcelName,
    174             const std::string &blobName,
    175             const std::string &fieldName,
    176             const std::string &offset,
    177             bool isReader) const;
    178 
    179     virtual status_t emitTypeDeclarations(Formatter &out) const;
    180 
    181     // Emit any declarations pertaining to this type that have to be
    182     // at global scope, i.e. enum class operators.
    183     virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
    184 
    185     // Emit any declarations pertaining to this type that have to be
    186     // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
    187     virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
    188 
    189     virtual status_t emitTypeDefinitions(
    190             Formatter &out, const std::string prefix) const;
    191 
    192     virtual status_t emitJavaTypeDeclarations(
    193             Formatter &out, bool atTopLevel) const;
    194 
    195     virtual bool needsEmbeddedReadWrite() const;
    196     virtual bool needsResolveReferences() const;
    197     virtual bool resultNeedsDeref() const;
    198 
    199     // Generates type declaration for vts proto file.
    200     // TODO (b/30844146): make it a pure virtual method.
    201     virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
    202     // Generates type declaration as attribute of method (return value or method
    203     // argument) or attribute of compound type for vts proto file.
    204     virtual status_t emitVtsAttributeType(Formatter &out) const;
    205 
    206     // Returns true iff this type is supported through the Java backend.
    207     virtual bool isJavaCompatible() const;
    208     virtual bool containsPointer() const;
    209     virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
    210 
    211     void setAnnotations(std::vector<Annotation *> *annotations);
    212     const std::vector<Annotation *> &annotations() const;
    213 
    214     virtual void appendToExportedTypesVector(
    215             std::vector<const Type *> *exportedTypes) const;
    216 
    217     virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
    218 
    219 protected:
    220     void handleError(Formatter &out, ErrorMode mode) const;
    221 
    222     void emitReaderWriterEmbeddedForTypeName(
    223             Formatter &out,
    224             const std::string &name,
    225             bool nameIsPointer,
    226             const std::string &parcelObj,
    227             bool parcelObjIsPointer,
    228             bool isReader,
    229             ErrorMode mode,
    230             const std::string &parentName,
    231             const std::string &offsetText,
    232             const std::string &typeName,
    233             const std::string &childName,
    234             const std::string &funcNamespace) const;
    235 
    236     void emitJavaReaderWriterWithSuffix(
    237             Formatter &out,
    238             const std::string &parcelObj,
    239             const std::string &argName,
    240             bool isReader,
    241             const std::string &suffix,
    242             const std::string &extra) const;
    243 
    244     void emitDumpWithMethod(
    245             Formatter &out,
    246             const std::string &streamName,
    247             const std::string &methodName,
    248             const std::string &name) const;
    249 
    250 private:
    251     std::vector<Annotation *> *mAnnotations;
    252 
    253     DISALLOW_COPY_AND_ASSIGN(Type);
    254 };
    255 
    256 /* Base type for VectorType and RefType. */
    257 struct TemplatedType : public Type {
    258     void setElementType(Type *elementType);
    259     Type *getElementType() const;
    260     bool isTemplatedType() const override;
    261     virtual bool isCompatibleElementType(Type *elementType) const = 0;
    262     status_t emitVtsTypeDeclarations(Formatter &out) const override;
    263     status_t emitVtsAttributeType(Formatter &out) const override;
    264 protected:
    265     TemplatedType();
    266     Type *mElementType;
    267 private:
    268     DISALLOW_COPY_AND_ASSIGN(TemplatedType);
    269 };
    270 
    271 }  // namespace android
    272 
    273 #endif  // TYPE_H_
    274 
    275