1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <functional>
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 #include "parser/parser.h" // for FileCtx
13 
14 constexpr size_t kMaxArgs = 8;
15 
16 extern const std::map<std::string, std::string> rust_overrides;
17 extern const std::map<std::string, std::string> rust_primitives;
18 extern const std::map<std::string, std::string> rust_reserved_words;
19 
20 struct ArraySpec {
21     enum Kind : uint32_t {
22         IN,
23         OUT,
24         INOUT
25     };
26 
27     Kind kind;
28     // Size of the array is specified in one of the two ways:
29     //    1) a number in the range 1--9 ('count' is non-zero, 'multipliers' is empty)
30     //    2) a sequence of one or more identifiers separated by '*',
31     //       e.g. "foo * bar" or "foo" ('count' is zero, 'multipliers' is non-empty)
32     uint32_t count;
33     std::vector<std::string> multipliers;
34 
35     std::string kind_str() const;
36     std::string kind_lowercase_str() const;
37     bool assign_kind(const std::vector<std::string>& attrs);
38     std::string to_string() const;
39 };
40 
41 struct TypeSpec {
42     std::string name;
43     std::string type;
44     std::vector<std::string> attributes;
45     std::unique_ptr<ArraySpec> arr_spec;
46 
47     std::string to_string() const;
48     std::string as_cpp_declaration(bool is_wrapped) const;
49     std::string as_rust_declaration() const;
50     std::string as_cpp_cast(const std::string& arg) const;
51 };
52 
53 // The tokens of a line of requirements.
54 using Requirement = std::vector<std::string>;
55 
56 // The tokens of a line of top-of-md description in the "NAME" block.
57 using TopDescription = std::vector<std::string>;
58 
59 struct Syscall {
60     // Move-only.
61     Syscall(Syscall&&) = default;
62     Syscall(const Syscall&) = delete;
63     Syscall& operator=(const Syscall&) = delete;
64 
65     FileCtx fc;
66     std::string name;
67     int index = -1;
68     std::vector<TypeSpec> ret_spec;
69     std::vector<TypeSpec> arg_spec;
70     std::vector<std::string> attributes;
71     std::vector<Requirement> requirements;
72     TopDescription top_description;
73 
SyscallSyscall74     Syscall(const FileCtx& sc_fc, const std::string& sc_name)
75         : fc(sc_fc), name(sc_name) {}
76 
77     bool is_vdso() const;
78     bool is_noreturn() const;
79     bool is_blocking() const;
80     bool is_internal() const;
81     size_t num_kernel_args() const;
82     void for_each_kernel_arg(const std::function<void(const TypeSpec&)>& cb) const;
83     void for_each_return(const std::function<void(const TypeSpec&)>& cb) const;
84     bool validate() const;
85     void assign_index(int* next_index);
86     bool validate_array_spec(const TypeSpec& ts) const;
87     void print_error(const char* what) const;
88     std::string return_type() const;
89     bool is_void_return() const;
90 };
91 
92 const std::string map_override(
93     const std::string& name, const std::map<std::string, std::string>& overrides);
94 bool has_attribute(const char* attr, const std::vector<std::string>& attrs);
95 void dump_attributes(const std::vector<std::string>& attrs);
96