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 "io_delegate.h"
22 #include "options.h"
23 #include <android-base/logging.h>
24 
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 struct yy_buffer_state;
30 typedef yy_buffer_state* YY_BUFFER_STATE;
31 
32 class AidlToken {
33  public:
AidlToken(const std::string & text,const std::string & comments)34   AidlToken(const std::string& text, const std::string& comments)
35       : text_(text), comments_(comments) {}
36   ~AidlToken() = default;
37 
38   AidlToken(const AidlToken&) = delete;
39   AidlToken(AidlToken&&) = delete;
40   AidlToken& operator=(const AidlToken&) = delete;
41   AidlToken& operator=(AidlToken&&) = delete;
42 
GetText()43   const std::string& GetText() const { return text_; }
GetComments()44   const std::string& GetComments() const { return comments_; }
45 
46  private:
47   std::string text_;
48   std::string comments_;
49 };
50 
51 class Parser {
52  public:
53   // non-copyable, non-assignable
54   Parser(const Parser&) = delete;
55   Parser& operator=(const Parser&) = delete;
56 
57   ~Parser();
58 
59   // Parse contents of file |filename|. Should only be called once.
60   static std::unique_ptr<Parser> Parse(const std::string& filename,
61                                        const android::aidl::IoDelegate& io_delegate,
62                                        AidlTypenames& typenames);
63 
AddError()64   void AddError() { error_++; }
HasError()65   bool HasError() const { return error_ != 0; }
66 
FileName()67   const std::string& FileName() const { return filename_; }
Scanner()68   void* Scanner() const { return scanner_; }
69 
SetPackage(const std::string & package)70   void SetPackage(const std::string& package) { package_ = package; }
Package()71   const std::string& Package() const { return package_; }
72 
DeferResolution(AidlTypeSpecifier * typespec)73   void DeferResolution(AidlTypeSpecifier* typespec) {
74     unresolved_typespecs_.emplace_back(typespec);
75   }
76 
GetUnresolvedTypespecs()77   const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
78 
79   bool Resolve();
SetDocument(std::unique_ptr<AidlDocument> && document)80   void SetDocument(std::unique_ptr<AidlDocument>&& document) {
81     // The parsed document is owned by typenames_. This parser object only has
82     // a reference to it.
83     document_ = document.get();
84     if (!typenames_.AddDocument(std::move(document))) {
85       document_ = nullptr;
86       AddError();
87     }
88   }
89 
ParsedDocument()90   const AidlDocument& ParsedDocument() const {
91     CHECK(!HasError());
92     return *document_;
93   }
94 
95  private:
96   explicit Parser(const std::string& filename, std::string& raw_buffer,
97                   android::aidl::AidlTypenames& typenames);
98 
99   std::string filename_;
100   std::string package_;
101   AidlTypenames& typenames_;
102 
103   void* scanner_ = nullptr;
104   YY_BUFFER_STATE buffer_;
105   int error_ = 0;
106 
107   vector<AidlTypeSpecifier*> unresolved_typespecs_;
108   const AidlDocument* document_;
109 };
110