blob: f1b6dd8cbcc07d27411a9cfa05e2ab1cb9e356cd [file] [log] [blame]
Jiyong Parke5c45292020-05-26 19:06:24 +09001/*
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"
Steven Moreland21780812020-09-11 01:29:45 +000022#include "logging.h"
Jiyong Parke5c45292020-05-26 19:06:24 +090023#include "options.h"
24
25#include <memory>
26#include <string>
27#include <vector>
28
29struct yy_buffer_state;
30typedef yy_buffer_state* YY_BUFFER_STATE;
31
Jiyong Park187bcc92020-07-22 18:40:30 +090032class AidlToken {
33 public:
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
43 const std::string& GetText() const { return text_; }
44 const std::string& GetComments() const { return comments_; }
45
Jooyung Han9bcf80e2020-10-05 16:25:34 +090046 template <typename T>
47 void Append(T&& text) {
48 text_ += std::forward<T>(text);
49 }
50
Jiyong Park187bcc92020-07-22 18:40:30 +090051 private:
52 std::string text_;
53 std::string comments_;
54};
55
Jooyung Han29813842020-12-08 01:28:03 +090056using TypeResolver = std::function<bool(const AidlDocument*, AidlTypeSpecifier*)>;
57
Jiyong Parke5c45292020-05-26 19:06:24 +090058class Parser {
59 public:
60 // non-copyable, non-assignable
61 Parser(const Parser&) = delete;
62 Parser& operator=(const Parser&) = delete;
63
64 ~Parser();
65
66 // Parse contents of file |filename|. Should only be called once.
67 static std::unique_ptr<Parser> Parse(const std::string& filename,
68 const android::aidl::IoDelegate& io_delegate,
69 AidlTypenames& typenames);
70
71 void AddError() { error_++; }
Jiyong Park8e79b7f2020-07-20 20:52:38 +090072 bool HasError() const { return error_ != 0; }
Jiyong Parke5c45292020-05-26 19:06:24 +090073
74 const std::string& FileName() const { return filename_; }
75 void* Scanner() const { return scanner_; }
76
Steven Moreland6c07b832020-10-29 23:39:53 +000077 // This restricts the grammar to something more reasonable. One alternative
78 // would be to support multiple sets of type specifiers in our AST, but then a
79 // lot of later code would have to deal with this more complicated type. So,
80 // in order to keep the AST simpler, restricting the grammar here.
81 //
82 // Takes ownership of type_args, modifies type.
83 void SetTypeParameters(AidlTypeSpecifier* type,
84 std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args);
85
Jiyong Park18132182020-06-08 20:24:40 +090086 void SetPackage(const std::string& package) { package_ = package; }
87 const std::string& Package() const { return package_; }
Jiyong Parke5c45292020-05-26 19:06:24 +090088
89 void DeferResolution(AidlTypeSpecifier* typespec) {
90 unresolved_typespecs_.emplace_back(typespec);
91 }
92
93 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
94
Jooyung Han29813842020-12-08 01:28:03 +090095 bool Resolve(TypeResolver& type_resolver);
Jiyong Park62515512020-06-08 15:57:11 +090096 void SetDocument(std::unique_ptr<AidlDocument>&& document) {
Jiyong Park8e79b7f2020-07-20 20:52:38 +090097 // The parsed document is owned by typenames_. This parser object only has
98 // a reference to it.
99 document_ = document.get();
100 if (!typenames_.AddDocument(std::move(document))) {
101 document_ = nullptr;
102 AddError();
Jiyong Parke5c45292020-05-26 19:06:24 +0900103 }
104 }
105
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900106 const AidlDocument& ParsedDocument() const {
Steven Moreland21780812020-09-11 01:29:45 +0000107 AIDL_FATAL_IF(HasError(), FileName());
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900108 return *document_;
109 }
Jiyong Parke5c45292020-05-26 19:06:24 +0900110
111 private:
112 explicit Parser(const std::string& filename, std::string& raw_buffer,
113 android::aidl::AidlTypenames& typenames);
114
115 std::string filename_;
Jiyong Park18132182020-06-08 20:24:40 +0900116 std::string package_;
Jiyong Parke5c45292020-05-26 19:06:24 +0900117 AidlTypenames& typenames_;
118
119 void* scanner_ = nullptr;
120 YY_BUFFER_STATE buffer_;
121 int error_ = 0;
122
Jiyong Parke5c45292020-05-26 19:06:24 +0900123 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900124 const AidlDocument* document_;
Jiyong Parke5c45292020-05-26 19:06:24 +0900125};