hidl-gen: add support for predefined types

b/31221346 Add support for predefined types in HIDL

Change-Id: I090c1ab2f10d4748e53b58485b1ef05114554d01
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/AST.cpp b/AST.cpp
index d13167a..3ba33c6 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -21,6 +21,7 @@
 #include "FQName.h"
 #include "HandleType.h"
 #include "Interface.h"
+#include "PredefinedType.h"
 #include "Scope.h"
 #include "TypeDef.h"
 
diff --git a/Android.mk b/Android.mk
index 08d6696..ce597f8 100644
--- a/Android.mk
+++ b/Android.mk
@@ -24,6 +24,7 @@
     Interface.cpp               \
     Method.cpp                  \
     NamedType.cpp               \
+    PredefinedType.cpp          \
     ScalarType.cpp              \
     Scope.cpp                   \
     StringType.cpp              \
diff --git a/PredefinedType.cpp b/PredefinedType.cpp
new file mode 100644
index 0000000..ea557a4
--- /dev/null
+++ b/PredefinedType.cpp
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "PredefinedType.h"
+
+#include "Formatter.h"
+
+#include <android-base/logging.h>
+
+namespace android {
+
+PredefinedType::PredefinedType(const char *name)
+    : mName(name) {
+}
+
+std::string PredefinedType::getCppType(
+        StorageMode mode, std::string *extra) const {
+    extra->clear();
+
+    const std::string base = mName;
+
+    switch (mode) {
+        case StorageMode_Stack:
+            return base;
+
+        case StorageMode_Argument:
+            return "const " + base + "&";
+
+        case StorageMode_Result:
+            return "const " + base + "*";
+    }
+}
+
+std::string PredefinedType::getJavaType() const {
+    CHECK(!"Should not be here");
+    return std::string();
+}
+
+void PredefinedType::emitReaderWriter(
+        Formatter &out,
+        const std::string &name,
+        const std::string &parcelObj,
+        bool parcelObjIsPointer,
+        bool isReader,
+        ErrorMode mode) const {
+    const std::string parentName = "_hidl_" + name + "_parent";
+
+    out << "size_t " << parentName << ";\n\n";
+
+    const std::string parcelObjDeref =
+        parcelObj + (parcelObjIsPointer ? "->" : ".");
+
+    if (isReader) {
+        out << name
+            << " = (const "
+            << mName
+            << " *)"
+            << parcelObjDeref
+            << "readBuffer("
+            << "&"
+            << parentName
+            << ");\n";
+
+        out << "if ("
+            << name
+            << " == nullptr) {\n";
+
+        out.indent();
+
+        out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
+        handleError2(out, mode);
+
+        out.unindent();
+        out << "}\n\n";
+    } else {
+        out << "_hidl_err = "
+            << parcelObjDeref
+            << "writeBuffer(&"
+            << name
+            << ", sizeof("
+            << name
+            << "), &"
+            << parentName
+            << ");\n";
+
+        handleError(out, mode);
+    }
+
+    emitReaderWriterEmbedded(
+            out,
+            name,
+            isReader /* nameIsPointer */,
+            parcelObj,
+            parcelObjIsPointer,
+            isReader,
+            mode,
+            parentName,
+            "0 /* parentOffset */");
+}
+
+void PredefinedType::emitReaderWriterEmbedded(
+        Formatter &out,
+        const std::string &name,
+        bool nameIsPointer,
+        const std::string &parcelObj,
+        bool parcelObjIsPointer,
+        bool isReader,
+        ErrorMode mode,
+        const std::string &parentName,
+        const std::string &offsetText) const {
+    emitReaderWriterEmbeddedForTypeName(
+            out,
+            name,
+            nameIsPointer,
+            parcelObj,
+            parcelObjIsPointer,
+            isReader,
+            mode,
+            parentName,
+            offsetText,
+            mName,
+            "" /* childName */);
+}
+
+bool PredefinedType::isJavaCompatible() const {
+    return false;
+}
+
+bool PredefinedType::needsEmbeddedReadWrite() const {
+    return true;
+}
+
+bool PredefinedType::resultNeedsDeref() const {
+    return true;
+}
+
+}  // namespace android
+
diff --git a/PredefinedType.h b/PredefinedType.h
new file mode 100644
index 0000000..5915eb3
--- /dev/null
+++ b/PredefinedType.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PREDEFINED_TYPE_H_
+
+#define PREDEFINED_TYPE_H_
+
+#include "Type.h"
+
+namespace android {
+
+struct PredefinedType : public Type {
+    PredefinedType(const char *name);
+
+    std::string getCppType(StorageMode mode, std::string *extra) const override;
+    std::string getJavaType() const override;
+
+    void emitReaderWriter(
+            Formatter &out,
+            const std::string &name,
+            const std::string &parcelObj,
+            bool parcelObjIsPointer,
+            bool isReader,
+            ErrorMode mode) const override;
+
+    void emitReaderWriterEmbedded(
+            Formatter &out,
+            const std::string &name,
+            bool nameIsPointer,
+            const std::string &parcelObj,
+            bool parcelObjIsPointer,
+            bool isReader,
+            ErrorMode mode,
+            const std::string &parentName,
+            const std::string &offsetText) const override;
+
+    bool isJavaCompatible() const override;
+
+    bool needsEmbeddedReadWrite() const override;
+    bool resultNeedsDeref() const override;
+
+private:
+    std::string mName;
+
+    DISALLOW_COPY_AND_ASSIGN(PredefinedType);
+};
+
+}  // namespace android
+
+#endif  // PREDEFINED_TYPE_H_