Spicy
declaration.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <utility>
7 #include <vector>
8 
9 #include <hilti/rt/filesystem.h>
10 
11 #include <hilti/ast/declarations/all.h>
12 #include <hilti/ast/function.h>
13 #include <hilti/ast/statements/declaration.h>
14 #include <hilti/ast/types/function.h>
15 
16 namespace hilti::builder {
17 
18 inline auto import(std::string module, const Meta& m = Meta()) {
19  return declaration::ImportedModule(hilti::ID(std::move(module), m), std::string(".hlt"), m);
20 }
21 
22 inline auto import(std::string module, const std::string& parse_extension, const Meta& m = Meta()) {
23  return declaration::ImportedModule(hilti::ID(std::move(module), m), parse_extension, m);
24 }
25 
26 inline auto import(std::string module, const std::string& parse_extension,
27  std::vector<hilti::rt::filesystem::path> search_dirs, const Meta& m = Meta()) {
28  return declaration::ImportedModule(hilti::ID(std::move(module), m), parse_extension, {}, std::move(search_dirs), m);
29 }
30 
31 inline auto import(std::string module, const std::string& parse_extension, std::optional<ID> search_scope,
32  std::vector<hilti::rt::filesystem::path> search_dirs, const Meta& m = Meta()) {
33  return declaration::ImportedModule(hilti::ID(std::move(module), m), parse_extension, std::move(search_scope),
34  std::move(search_dirs), m);
35 }
36 
37 inline auto local(ID id_, Type t, Meta m = Meta()) {
38  return statement::Declaration(declaration::LocalVariable(std::move(id_), std::move(t), {}, false, std::move(m)));
39 }
40 
41 inline auto local(ID id_, Expression init, Meta m = Meta()) {
42  return statement::Declaration(declaration::LocalVariable(std::move(id_), std::move(init), false, std::move(m)));
43 }
44 
45 inline auto local(ID id_, Type t, Expression init, Meta m = Meta()) {
46  return statement::Declaration(
47  declaration::LocalVariable(std::move(id_), std::move(t), std::move(init), false, std::move(m)));
48 }
49 
50 inline auto local(ID id_, Type t, std::vector<hilti::Expression> args, Meta m = Meta()) {
51  return statement::Declaration(
52  declaration::LocalVariable(std::move(id_), std::move(t), std::move(args), {}, false, std::move(m)));
53 }
54 
55 inline auto global(ID id_, Type t, declaration::Linkage linkage = declaration::Linkage::Private, Meta m = Meta()) {
56  return declaration::GlobalVariable(std::move(id_), std::move(t), {}, linkage, std::move(m));
57 }
58 
59 inline auto global(ID id_, Expression init, declaration::Linkage linkage = declaration::Linkage::Private,
60  Meta m = Meta()) {
61  return declaration::GlobalVariable(std::move(id_), std::move(init), linkage, std::move(m));
62 }
63 
64 inline auto global(ID id_, Type t, Expression init, declaration::Linkage linkage = declaration::Linkage::Private,
65  Meta m = Meta()) {
66  return declaration::GlobalVariable(std::move(id_), std::move(t), std::move(init), linkage, std::move(m));
67 }
68 
69 inline auto global(ID id_, Type t, std::vector<hilti::Expression> args,
70  declaration::Linkage linkage = declaration::Linkage::Private, Meta m = Meta()) {
71  return declaration::GlobalVariable(std::move(id_), std::move(t), std::move(args), {}, linkage, std::move(m));
72 }
73 
74 inline auto type(ID id, ::hilti::Type type, declaration::Linkage linkage = declaration::Linkage::Private,
75  Meta m = Meta()) {
76  return declaration::Type(std::move(id), std::move(type), linkage, std::move(m));
77 }
78 
79 inline auto type(ID id, ::hilti::Type type, std::optional<AttributeSet> attrs,
80  declaration::Linkage linkage = declaration::Linkage::Private, Meta m = Meta()) {
81  return declaration::Type(std::move(id), std::move(type), std::move(attrs), linkage, std::move(m));
82 }
83 
84 inline auto constant(ID id_, Expression init, declaration::Linkage linkage = declaration::Linkage::Private,
85  Meta m = Meta()) {
86  return declaration::Constant(std::move(id_), std::move(init), linkage, std::move(m));
87 }
88 
89 inline auto parameter(ID id, Type type, type::function::parameter::Kind kind = type::function::parameter::Kind::In,
90  Meta m = Meta()) {
91  return type::function::Parameter(std::move(id), std::move(type), kind, {}, std::move(m));
92 }
93 
94 inline auto parameter(ID id, Type type, Expression default_,
95  type::function::parameter::Kind kind = type::function::parameter::Kind::In, Meta m = Meta()) {
96  return type::function::Parameter(std::move(id), std::move(type), kind, std::move(default_), std::move(m));
97 }
98 
99 template<typename... Params>
100 static auto parameters(Params&&... params) {
101  return std::vector<hilti::type::function::Parameter>{std::forward<Params>(params)...};
102 }
103 
104 inline auto function(ID id, Type result, const std::vector<type::function::Parameter>& params,
105  type::function::Flavor flavor = type::function::Flavor::Standard,
106  declaration::Linkage linkage = declaration::Linkage::Private,
107  function::CallingConvention cc = function::CallingConvention::Standard,
108  std::optional<AttributeSet> attrs = {}, const Meta& m = Meta()) {
109  auto ft = type::Function(type::function::Result(std::move(result), m), params, flavor, m);
110  auto f = Function(std::move(id), std::move(ft), {}, cc, std::move(attrs), m);
111  return declaration::Function(std::move(f), linkage, m);
112 }
113 
114 inline auto function(ID id, Type result, const std::vector<type::function::Parameter>& params, Statement body,
115  type::function::Flavor flavor = type::function::Flavor::Standard,
116  declaration::Linkage linkage = declaration::Linkage::Private,
117  function::CallingConvention cc = function::CallingConvention::Standard,
118  std::optional<AttributeSet> attrs = {}, const Meta& m = Meta()) {
119  auto ft = type::Function(type::function::Result(std::move(result), m), params, flavor, m);
120  auto f = Function(std::move(id), std::move(ft), std::move(body), cc, std::move(attrs), m);
121  return declaration::Function(std::move(f), linkage, m);
122 }
123 
124 inline auto function(ID id, type::Function ftype, Statement body,
125  declaration::Linkage linkage = declaration::Linkage::Private,
126  function::CallingConvention cc = function::CallingConvention::Standard,
127  std::optional<AttributeSet> attrs = {}, const Meta& m = Meta()) {
128  auto f = Function(std::move(id), std::move(ftype), std::move(body), cc, std::move(attrs), m);
129  return declaration::Function(std::move(f), linkage, m);
130 }
131 
132 } // namespace hilti::builder
Definition: type.h:158
Definition: builder.h:20
Definition: id.h:18