/* * Copyright (C) 2019 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. */ #pragma once #include #include #include #include #include "Reference.h" namespace android { struct CompoundType; struct Coordinator; struct Formatter; struct FQName; struct Interface; struct Method; struct NamedType; struct Scope; struct Type; struct FieldWithVersion { const NamedReference* field; // name of the field appended by the std::string fullName; std::pair version; }; struct ProcessedCompoundType { // map modified name to field. This modified name is old.new std::vector fields; std::set subTypes; }; struct ReplacedTypeInfo { // if a HIDL type is replaced, this returns the new AIDL type // android.hardware.safe_enum@1.0::Monostate -> boolean std::string aidlReplacedType; // if a HIDL type is replaced, this is the FQName of the new AIDL type // android.hardware.safe_enum@1.0::Monostate -> std::nullopt std::optional aidlReplacedFQName; // if a HIDL type is replaced, this returns the function needed to generate translation std::optional> translateField; }; enum class AidlBackend { UNKNOWN, NDK, CPP, JAVA }; struct AidlHelper { /* FQName helpers */ // getAidlName returns the type names // android.hardware.foo@1.0::IBar.Baz -> IBarBaz static std::string getAidlName(const FQName& fqName); // getAidlPackage returns the AIDL package // android.hardware.foo@1.x -> android.hardware.foo // android.hardware.foo@2.x -> android.hardware.foo2 static std::string getAidlPackage(const FQName& fqName); // returns getAidlPackage(fqName) with '.' replaced by '/' // android.hardware.foo@1.x -> android/hardware/foo static std::string getAidlPackagePath(const FQName& fqName); // getAidlFQName = getAidlPackage + "." + getAidlName static std::optional getAidlFQName(const FQName& fqName); // if a HIDL type is replaced, this returns the ReplacedTypeInfo for the new AIDL type static std::optional getAidlReplacedType(const FQName& fqName); static void emitFileHeader( Formatter& out, const NamedType& type, const std::map& processedTypes); static void importLocallyReferencedType(const Type& type, std::set* imports); static Formatter getFileWithHeader( const NamedType& namedType, const Coordinator& coordinator, const std::map& processedTypes); /* Methods for Type */ static std::string getAidlType(const Type& type, const FQName& relativeTo); /* Methods for NamedType */ static void emitAidl( const NamedType& namedType, const Coordinator& coordinator, const std::map& processedTypes); /* Methods for Interface */ static void emitAidl(const Interface& interface, const Coordinator& coordinator, const std::map&); // Returns all methods that would exist in an AIDL equivalent interface static std::vector getUserDefinedMethods(Formatter& out, const Interface& interface); static void processCompoundType(const CompoundType& compoundType, ProcessedCompoundType* processedType, const std::string& fieldNamePrefix); static Formatter& notes(); static void setNotes(Formatter* formatter); // return the full file names for the header/source files based on the backend static std::string translateHeaderFile(const FQName& fqName, AidlBackend backend); static std::string translateSourceFile(const FQName& fqName, AidlBackend backend); static void emitTranslation( const Coordinator& coordinator, const FQName& fqName, const std::set& namedTypesInPackage, const std::map& processedTypes); static void setFileHeader(const std::string& file); static void emitFileHeader(Formatter& out); static void setExpandExtended(bool expand) { expandExtended = expand; }; static bool isExpandExtended() { return expandExtended; }; static bool shouldBeExpanded(const FQName& source, const FQName& extended); private: // This is the formatter to use for additional conversion output static Formatter* notesFormatter; static std::string fileHeader; static bool expandExtended; }; } // namespace android