llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
sexp.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <memory>
8 #include <vector>
9 
10 #include <llvm/Support/raw_ostream.h>
11 
12 
13 
17 class SExp {
18 public:
19  enum class Kind {
20  NUMBER,
21  STRING,
22  LIST,
23  };
24 
26  class Number {
27  public:
28  Number(int64_t v) : k_(Kind::NUMBER), v_(v) {}
29 
31  int64_t Get() const { return v_; }
32 
34  void print(llvm::raw_ostream &os) const;
35 
36  private:
38  Kind k_;
40  int64_t v_;
41  };
42 
44  class String {
45  public:
46  String(const std::string &v) : k_(Kind::STRING), v_(v) {}
47 
49  const std::string &Get() const { return v_; }
50 
52  void print(llvm::raw_ostream &os) const;
53 
54  private:
56  Kind k_;
58  std::string v_;
59  };
60 
62  class List {
63  public:
64  List() : k_(Kind::LIST) {}
65 
66  Number *AddNumber(int64_t v);
67  String *AddString(const std::string &v);
68  List *AddList();
69 
70  size_t size() const { return v_.size(); }
71  const SExp &operator[](size_t idx) const { return v_[idx]; }
72 
74  void print(llvm::raw_ostream &os) const;
75 
76  private:
78  Kind k_;
80  std::vector<SExp> v_;
81  };
82 
83 public:
84  SExp() : s_(std::make_unique<Storage>()) {}
85  SExp(int64_t v) : s_(std::make_unique<Storage>(v)) {}
86  SExp(const std::string &v) : s_(std::make_unique<Storage>(v)) {}
87 
88  SExp(SExp &&that) : s_(std::move(that.s_)) {}
89  SExp(const SExp &that) : s_(std::make_unique<Storage>(*that.s_)) {}
90  ~SExp();
91 
93  const Number *AsNumber() const;
95  Number *AsNumber();
97  const String *AsString() const;
99  String *AsString();
101  const List *AsList() const;
103  List *AsList();
104 
106  void print(llvm::raw_ostream &os) const;
107 
108 private:
110  union Storage {
112  Kind K;
114  Number N;
116  String S;
118  List L;
119 
120  Storage() { new (&L) List(); }
121  Storage(int64_t v) { new (&N) Number(v); }
122  Storage(const std::string &v) { new (&S) String(v); }
123  Storage(const Storage &that);
124  ~Storage();
125  };
126 
128  std::unique_ptr<Storage> s_;
129 };
SExp::AsNumber
const Number * AsNumber() const
Returns the node as a number or nullptr.
Definition: sexp.cpp:65
SExp
Definition: sexp.h:17
SExp::Number::Get
int64_t Get() const
Returns the value.
Definition: sexp.h:31
SExp::List
Storage for lists.
Definition: sexp.h:62
SExp::String::Get
const std::string & Get() const
Returns the value.
Definition: sexp.h:49
SExp::print
void print(llvm::raw_ostream &os) const
Print the s-expression.
Definition: sexp.cpp:101
SExp::AsList
const List * AsList() const
Returns the node as a list or nullptr.
Definition: sexp.cpp:89
SExp::String::print
void print(llvm::raw_ostream &os) const
Print the s-expression.
Definition: sexp.cpp:20
SExp::Number
Storage for numbers.
Definition: sexp.h:26
SExp::List::print
void print(llvm::raw_ostream &os) const
Print the s-expression.
Definition: sexp.cpp:47
SExp::AsString
const String * AsString() const
Returns the node as a string or nullptr.
Definition: sexp.cpp:77
SExp::Number::print
void print(llvm::raw_ostream &os) const
Print the s-expression.
Definition: sexp.cpp:14
SExp::String
Storage for strings.
Definition: sexp.h:44