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, std::vector<hilti::rt::filesystem::path> search_dirs, const Meta& m = Meta()) {
23  return declaration::ImportedModule(hilti::ID(std::move(module), m), std::string(".hlt"), {}, std::move(search_dirs),
24  m);
25 }
26 
27 inline auto import(std::string module, std::optional<ID> search_scope,
28  std::vector<hilti::rt::filesystem::path> search_dirs, const Meta& m = Meta()) {
29  return declaration::ImportedModule(hilti::ID(std::move(module), m), std::string(".hlt"), std::move(search_scope),
30  std::move(search_dirs), m);
31 }
32 
33 inline auto local(ID id_, Type t, Meta m = Meta()) {
34  return statement::Declaration(declaration::LocalVariable(std::move(id_), std::move(t), {}, false, std::move(m)));
35 }
36 
37 inline auto local(ID id_, Expression init, Meta m = Meta()) {
38  return statement::Declaration(declaration::LocalVariable(std::move(id_), std::move(init), false, std::move(m)));
39 }
40 
41 inline auto local(ID id_, Type t, Expression init, Meta m = Meta()) {
42  return statement::Declaration(
43  declaration::LocalVariable(std::move(id_), std::move(t), std::move(init), false, std::move(m)));
44 }
45 
46 inline auto local(ID id_, Type t, std::vector<hilti::Expression> args, Meta m = Meta()) {
47  return statement::Declaration(
48  declaration::LocalVariable(std::move(id_), std::move(t), std::move(args), {}, false, std::move(m)));
49 }
50 
51 inline auto global(ID id_, Type t, declaration::Linkage linkage = declaration::Linkage::Private, Meta m = Meta()) {
52  return declaration::GlobalVariable(std::move(id_), std::move(t), {}, linkage, std::move(m));
53 }
54 
55 inline auto global(ID id_, Expression init, declaration::Linkage linkage = declaration::Linkage::Private,
56  Meta m = Meta()) {
57  return declaration::GlobalVariable(std::move(id_), std::move(init), linkage, std::move(m));
58 }
59 
60 inline auto global(ID id_, Type t, Expression init, declaration::Linkage linkage = declaration::Linkage::Private,
61  Meta m = Meta()) {
62  return declaration::GlobalVariable(std::move(id_), std::move(t), std::move(init), linkage, std::move(m));
63 }
64 
65 inline auto global(ID id_, Type t, std::vector<hilti::Expression> args,
66  declaration::Linkage linkage = declaration::Linkage::Private, Meta m = Meta()) {
67  return declaration::GlobalVariable(std::move(id_), std::move(t), std::move(args), {}, linkage, std::move(m));
68 }
69 
70 inline auto type(ID id, ::hilti::Type type, declaration::Linkage linkage = declaration::Linkage::Private,
71  Meta m = Meta()) {
72  return declaration::Type(std::move(id), std::move(type), linkage, std::move(m));
73 }
74 
75 inline auto type(ID id, ::hilti::Type type, std::optional<AttributeSet> attrs,
76  declaration::Linkage linkage = declaration::Linkage::Private, Meta m = Meta()) {
77  return declaration::Type(std::move(id), std::move(type), std::move(attrs), linkage, std::move(m));
78 }
79 
80 inline auto constant(ID id_, Expression init, declaration::Linkage linkage = declaration::Linkage::Private,
81  Meta m = Meta()) {
82  return declaration::Constant(std::move(id_), std::move(init), linkage, std::move(m));
83 }
84 
85 inline auto parameter(ID id, Type type, type::function::parameter::Kind kind = type::function::parameter::Kind::In,
86  Meta m = Meta()) {
87  return type::function::Parameter(std::move(id), std::move(type), kind, {}, std::move(m));
88 }
89 
90 inline auto parameter(ID id, Type type, Expression default_,
91  type::function::parameter::Kind kind = type::function::parameter::Kind::In, Meta m = Meta()) {
92  return type::function::Parameter(std::move(id), std::move(type), kind, std::move(default_), std::move(m));
93 }
94 
95 template<typename... Params>
96 static auto parameters(Params&&... params) {
97  return std::vector<hilti::type::function::Parameter>{std::forward<Params>(params)...};
98 }
99 
100 inline auto function(ID id, Type result, const std::vector<type::function::Parameter>& params,
101  type::function::Flavor flavor = type::function::Flavor::Standard,
102  declaration::Linkage linkage = declaration::Linkage::Private,
103  function::CallingConvention cc = function::CallingConvention::Standard,
104  std::optional<AttributeSet> attrs = {}, const Meta& m = Meta()) {
105  auto ft = type::Function(type::function::Result(std::move(result), m), params, flavor, m);
106  auto f = Function(std::move(id), std::move(ft), {}, cc, std::move(attrs), m);
107  return declaration::Function(std::move(f), linkage, m);
108 }
109 
110 inline auto function(ID id, Type result, const std::vector<type::function::Parameter>& params, Statement body,
111  type::function::Flavor flavor = type::function::Flavor::Standard,
112  declaration::Linkage linkage = declaration::Linkage::Private,
113  function::CallingConvention cc = function::CallingConvention::Standard,
114  std::optional<AttributeSet> attrs = {}, const Meta& m = Meta()) {
115  auto ft = type::Function(type::function::Result(std::move(result), m), params, flavor, m);
116  auto f = Function(std::move(id), std::move(ft), std::move(body), cc, std::move(attrs), m);
117  return declaration::Function(std::move(f), linkage, m);
118 }
119 
120 inline auto function(ID id, type::Function ftype, Statement body,
121  declaration::Linkage linkage = declaration::Linkage::Private,
122  function::CallingConvention cc = function::CallingConvention::Standard,
123  std::optional<AttributeSet> attrs = {}, const Meta& m = Meta()) {
124  auto f = Function(std::move(id), std::move(ftype), std::move(body), cc, std::move(attrs), m);
125  return declaration::Function(std::move(f), linkage, m);
126 }
127 
128 } // namespace hilti::builder
Definition: builder.h:20
Definition: id.h:18