blob: 726922d740e0bd39cd438501a1d6fdab055056dc [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
Jiyong Parke5c45292020-05-26 19:06:24 +090056class Parser {
57 public:
58 // non-copyable, non-assignable
59 Parser(const Parser&) = delete;
60 Parser& operator=(const Parser&) = delete;
61
62 ~Parser();
63
64 // Parse contents of file |filename|. Should only be called once.
65 static std::unique_ptr<Parser> Parse(const std::string& filename,
66 const android::aidl::IoDelegate& io_delegate,
67 AidlTypenames& typenames);
68
69 void AddError() { error_++; }
Jiyong Park8e79b7f2020-07-20 20:52:38 +090070 bool HasError() const { return error_ != 0; }
Jiyong Parke5c45292020-05-26 19:06:24 +090071
72 const std::string& FileName() const { return filename_; }
73 void* Scanner() const { return scanner_; }
74
Steven Moreland6c07b832020-10-29 23:39:53 +000075 // This restricts the grammar to something more reasonable. One alternative
76 // would be to support multiple sets of type specifiers in our AST, but then a
77 // lot of later code would have to deal with this more complicated type. So,
78 // in order to keep the AST simpler, restricting the grammar here.
79 //
80 // Takes ownership of type_args, modifies type.
81 void SetTypeParameters(AidlTypeSpecifier* type,
82 std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args);
83
Jiyong Park18132182020-06-08 20:24:40 +090084 void SetPackage(const std::string& package) { package_ = package; }
85 const std::string& Package() const { return package_; }
Jiyong Parke5c45292020-05-26 19:06:24 +090086
87 void DeferResolution(AidlTypeSpecifier* typespec) {
88 unresolved_typespecs_.emplace_back(typespec);
89 }
90
91 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
92
93 bool Resolve();
Jiyong Park62515512020-06-08 15:57:11 +090094 void SetDocument(std::unique_ptr<AidlDocument>&& document) {
Jiyong Park8e79b7f2020-07-20 20:52:38 +090095 // The parsed document is owned by typenames_. This parser object only has
96 // a reference to it.
97 document_ = document.get();
98 if (!typenames_.AddDocument(std::move(document))) {
99 document_ = nullptr;
100 AddError();
Jiyong Parke5c45292020-05-26 19:06:24 +0900101 }
102 }
103
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900104 const AidlDocument& ParsedDocument() const {
Steven Moreland21780812020-09-11 01:29:45 +0000105 AIDL_FATAL_IF(HasError(), FileName());
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900106 return *document_;
107 }
Jiyong Parke5c45292020-05-26 19:06:24 +0900108
109 private:
110 explicit Parser(const std::string& filename, std::string& raw_buffer,
111 android::aidl::AidlTypenames& typenames);
112
113 std::string filename_;
Jiyong Park18132182020-06-08 20:24:40 +0900114 std::string package_;
Jiyong Parke5c45292020-05-26 19:06:24 +0900115 AidlTypenames& typenames_;
116
117 void* scanner_ = nullptr;
118 YY_BUFFER_STATE buffer_;
119 int error_ = 0;
120
Jiyong Parke5c45292020-05-26 19:06:24 +0900121 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900122 const AidlDocument* document_;
Jiyong Parke5c45292020-05-26 19:06:24 +0900123};