blob: 074d6d503f2e8ce12a81d8ae14ead1314bcf65f9 [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"
Jooyung Han8451a202021-01-16 03:07:06 +090021#include "comments.h"
Jiyong Parke5c45292020-05-26 19:06:24 +090022#include "io_delegate.h"
Steven Moreland21780812020-09-11 01:29:45 +000023#include "logging.h"
Jiyong Parke5c45292020-05-26 19:06:24 +090024#include "options.h"
25
26#include <memory>
27#include <string>
28#include <vector>
29
30struct yy_buffer_state;
31typedef yy_buffer_state* YY_BUFFER_STATE;
32
Jiyong Park187bcc92020-07-22 18:40:30 +090033class AidlToken {
34 public:
Jooyung Han8451a202021-01-16 03:07:06 +090035 AidlToken(const std::string& text, android::aidl::Comments comments)
36 : text_(text), comments_(std::move(comments)) {}
Jiyong Park187bcc92020-07-22 18:40:30 +090037 ~AidlToken() = default;
38
39 AidlToken(const AidlToken&) = delete;
40 AidlToken(AidlToken&&) = delete;
41 AidlToken& operator=(const AidlToken&) = delete;
42 AidlToken& operator=(AidlToken&&) = delete;
43
44 const std::string& GetText() const { return text_; }
Jooyung Han8451a202021-01-16 03:07:06 +090045 const android::aidl::Comments& GetComments() const { return comments_; }
Jiyong Park187bcc92020-07-22 18:40:30 +090046
Jooyung Han9bcf80e2020-10-05 16:25:34 +090047 template <typename T>
48 void Append(T&& text) {
49 text_ += std::forward<T>(text);
50 }
51
Jiyong Park187bcc92020-07-22 18:40:30 +090052 private:
53 std::string text_;
Jooyung Han8451a202021-01-16 03:07:06 +090054 android::aidl::Comments comments_;
Jiyong Park187bcc92020-07-22 18:40:30 +090055};
56
Jooyung Hance6733e2021-05-22 02:44:17 +090057using TypeResolver = std::function<bool(const AidlDefinedType*, AidlTypeSpecifier*)>;
58bool ResolveReferences(const AidlDocument& document, TypeResolver& resolver);
Jooyung Han29813842020-12-08 01:28:03 +090059
Jiyong Parke5c45292020-05-26 19:06:24 +090060class 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.
Jooyung Hance6733e2021-05-22 02:44:17 +090069 static const AidlDocument* Parse(const std::string& filename,
70 const android::aidl::IoDelegate& io_delegate,
Jooyung Han93f48f02021-06-05 00:11:16 +090071 AidlTypenames& typenames, bool is_preprocessed = false);
Jiyong Parke5c45292020-05-26 19:06:24 +090072
73 void AddError() { error_++; }
Jiyong Park8e79b7f2020-07-20 20:52:38 +090074 bool HasError() const { return error_ != 0; }
Jiyong Parke5c45292020-05-26 19:06:24 +090075
76 const std::string& FileName() const { return filename_; }
77 void* Scanner() const { return scanner_; }
78
Steven Moreland6c07b832020-10-29 23:39:53 +000079 // This restricts the grammar to something more reasonable. One alternative
80 // would be to support multiple sets of type specifiers in our AST, but then a
81 // lot of later code would have to deal with this more complicated type. So,
82 // in order to keep the AST simpler, restricting the grammar here.
83 //
84 // Takes ownership of type_args, modifies type.
85 void SetTypeParameters(AidlTypeSpecifier* type,
86 std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args);
87
Jooyung Han93f48f02021-06-05 00:11:16 +090088 // fully-qualified type names are allowed only in preprocessed files
89 void CheckValidTypeName(const AidlToken& token, const AidlLocation& loc);
90
Steven Moreland85762bf2021-10-08 15:04:22 -070091 void SetPackage(const std::string& package);
Jiyong Park18132182020-06-08 20:24:40 +090092 const std::string& Package() const { return package_; }
Jiyong Parke5c45292020-05-26 19:06:24 +090093
Jooyung Han35784982021-06-29 06:26:12 +090094 void MakeDocument(const AidlLocation& location, const Comments& comments,
Jooyung Hanf08c15f2022-01-28 14:57:13 +090095 std::vector<std::string> imports,
Jooyung Han35784982021-06-29 06:26:12 +090096 std::vector<std::unique_ptr<AidlDefinedType>> defined_types);
Jiyong Parke5c45292020-05-26 19:06:24 +090097
Jiyong Parke5c45292020-05-26 19:06:24 +090098 private:
Jooyung Han93f48f02021-06-05 00:11:16 +090099 explicit Parser(const std::string& filename, std::string& raw_buffer, bool is_preprocessed);
Jiyong Parke5c45292020-05-26 19:06:24 +0900100
101 std::string filename_;
Jooyung Han93f48f02021-06-05 00:11:16 +0900102 bool is_preprocessed_;
Jiyong Park18132182020-06-08 20:24:40 +0900103 std::string package_;
Jiyong Parke5c45292020-05-26 19:06:24 +0900104 void* scanner_ = nullptr;
105 YY_BUFFER_STATE buffer_;
106 int error_ = 0;
107
Jooyung Han93f48f02021-06-05 00:11:16 +0900108 std::unique_ptr<AidlDocument> document_;
Jiyong Parke5c45292020-05-26 19:06:24 +0900109};