1 /* 2 * Copyright (C) 2019, 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 #pragma once 18 19 #include "aidl_language.h" 20 #include "aidl_typenames.h" 21 #include "comments.h" 22 #include "io_delegate.h" 23 #include "logging.h" 24 #include "options.h" 25 26 #include <memory> 27 #include <string> 28 #include <vector> 29 30 struct yy_buffer_state; 31 typedef yy_buffer_state* YY_BUFFER_STATE; 32 33 class AidlToken { 34 public: AidlToken(const std::string & text,android::aidl::Comments comments)35 AidlToken(const std::string& text, android::aidl::Comments comments) 36 : text_(text), comments_(std::move(comments)) {} 37 ~AidlToken() = default; 38 39 AidlToken(const AidlToken&) = delete; 40 AidlToken(AidlToken&&) = delete; 41 AidlToken& operator=(const AidlToken&) = delete; 42 AidlToken& operator=(AidlToken&&) = delete; 43 GetText()44 const std::string& GetText() const { return text_; } GetComments()45 const android::aidl::Comments& GetComments() const { return comments_; } 46 47 template <typename T> Append(T && text)48 void Append(T&& text) { 49 text_ += std::forward<T>(text); 50 } 51 52 private: 53 std::string text_; 54 android::aidl::Comments comments_; 55 }; 56 57 using TypeResolver = std::function<bool(const AidlDefinedType*, AidlTypeSpecifier*)>; 58 bool ResolveReferences(const AidlDocument& document, TypeResolver& resolver); 59 60 class Parser { 61 public: 62 // non-copyable, non-assignable 63 Parser(const Parser&) = delete; 64 Parser& operator=(const Parser&) = delete; 65 66 ~Parser(); 67 68 // Parse contents of file |filename|. Should only be called once. 69 static const AidlDocument* Parse(const std::string& filename, 70 const android::aidl::IoDelegate& io_delegate, 71 AidlTypenames& typenames, bool is_preprocessed = false); 72 AddError()73 void AddError() { error_++; } HasError()74 bool HasError() const { return error_ != 0; } 75 FileName()76 const std::string& FileName() const { return filename_; } Scanner()77 void* Scanner() const { return scanner_; } 78 GetDocument()79 AidlDocument* GetDocument() { return document_.get(); } 80 81 // This restricts the grammar to something more reasonable. One alternative 82 // would be to support multiple sets of type specifiers in our AST, but then a 83 // lot of later code would have to deal with this more complicated type. So, 84 // in order to keep the AST simpler, restricting the grammar here. 85 // 86 // Takes ownership of type_args, modifies type. 87 void SetTypeParameters(AidlTypeSpecifier* type, 88 std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args); 89 90 // fully-qualified type names are allowed only in preprocessed files 91 void CheckValidTypeName(const AidlToken& token, const AidlLocation& loc); 92 93 void SetPackage(const std::string& package); Package()94 const std::string& Package() const { return package_; } 95 96 void MakeDocument(const AidlLocation& location, const Comments& comments, 97 std::vector<std::string> imports, 98 std::vector<std::unique_ptr<AidlDefinedType>> defined_types); 99 100 private: 101 explicit Parser(const std::string& filename, std::string& raw_buffer, bool is_preprocessed); 102 103 std::string filename_; 104 bool is_preprocessed_; 105 std::string package_; 106 void* scanner_ = nullptr; 107 YY_BUFFER_STATE buffer_; 108 int error_ = 0; 109 110 std::unique_ptr<AidlDocument> document_; 111 }; 112