Spicy
unit.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <algorithm>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
10 #include <spicy/ast/types/unit.h>
11 #include <spicy/compiler/detail/codegen/production.h>
12 
13 namespace spicy::detail::codegen {
14 class Grammar;
15 } // namespace spicy::detail::codegen
16 
18 
23 public:
24  Unit(const std::string& symbol, type::Unit type, std::vector<Expression> args, std::vector<Production> fields,
25  const Location& l = location::None)
26  : ProductionBase(symbol, l), _type(std::move(type)), _args(std::move(args)), _fields(std::move(fields)) {}
27 
28  const type::Unit& unitType() const { return _type; }
29  const auto& arguments() const { return _args; }
30  const auto& fields() const { return _fields; }
31 
32  // Production API
33  std::vector<std::vector<Production>> rhss() const { return {_fields}; };
34  std::optional<spicy::Type> type() const { return spicy::Type(_type); }
35  bool nullable() const { return production::nullable(rhss()); }
36  bool eodOk() const { return nullable(); }
37  bool atomic() const { return false; }
38  bool supportsSynchronize() const {
39  return hasSize() || (rhss().front().size() && rhss().front().front().supportsSynchronize()) ||
40  _type.propertyItem("synchronize-after") || _type.propertyItem("synchronize-before");
41  }
42  std::string render() const {
43  return hilti::util::join(hilti::util::transform(_fields, [](const auto& p) { return p.symbol(); }), " ");
44  }
45 
46 private:
47  type::Unit _type;
48  std::vector<Expression> _args;
49  std::vector<Production> _fields;
50 };
51 
52 } // namespace spicy::detail::codegen::production
Definition: production.h:24
Definition: unit.h:51
std::optional< unit::item::Property > propertyItem(const std::string &name) const
Definition: unit.h:117
const std::string & symbol() const
Definition: production.h:196
bool hasSize() const
Definition: production.h:185
Definition: production.h:170
Definition: grammar.h:15
std::string join(const T &l, const std::string &delim="")
Definition: util.h:223
Definition: unit.h:22
Definition: location.h:17
auto transform(const std::vector< X > &x, F f)
Definition: util.h:86
ProductionBase(std::string symbol, Location l=location::None)
Definition: production.h:181