Nearly CC
An educational compiler skeleton
node_base.h
1 // Copyright (c) 2021-2023, David H. Hovemeyer <david.hovemeyer@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 // OTHER DEALINGS IN THE SOFTWARE.
20 
21 #ifndef NODE_BASE_H
22 #define NODE_BASE_H
23 
24 #ifdef SOLUTION
25 #include <cassert>
26 #endif
27 #include <memory>
28 #include "type.h"
29 #include "symtab.h"
30 #include "literal_value.h"
31 #include "has_operand.h"
32 
43 class NodeBase : public HasOperand {
44 private:
45 #ifdef SOLUTION
46  Symbol *m_symbol;
47  std::shared_ptr<Type> m_type;
48  bool m_lvalue;
49  LiteralValue m_literal_value;
50  Operand m_operand;
51  int m_strnum; // for string constants
52 #else
53  // TODO: fields (pointer to Type, pointer to Symbol, etc.)
54 #endif
55 
56  // copy ctor and assignment operator not supported
57  NodeBase(const NodeBase &);
58  NodeBase &operator=(const NodeBase &);
59 
60 public:
61  NodeBase();
62  virtual ~NodeBase();
63 
64 #ifdef SOLUTION
65  void set_symbol(Symbol *symbol);
66  void set_type(const std::shared_ptr<Type> &type);
67 
68  void reset_symbol(Symbol *symbol) {
69  assert(has_symbol());
70  m_symbol = symbol;
71  }
72 
73  bool has_symbol() const;
74  Symbol *get_symbol() const;
75  std::shared_ptr<Type> get_type() const;
76 
77  bool is_lvalue() const;
78  void set_lvalue(bool lvalue);
79 
80  bool has_literal_value() const;
81  void set_literal_value(const LiteralValue &literal_value);
82  LiteralValue get_literal_value() const;
83 
84  void set_strnum(int strnum) { m_strnum = strnum; }
85  int get_strnum() const { return m_strnum; }
86 #else
87  // TODO: add member functions
88 #endif
89 };
90 
91 #endif // NODE_BASE_H
HasOperand is a base class for NodeBase, which in turn is a base class for Node.
Definition: has_operand.h:31
Helper class for representing literal values (integer, character, and string).
Definition: literal_value.h:45
The Node class will inherit from this type, so you can use it to define any attributes and methods th...
Definition: node_base.h:43
Definition: operand.h:31
Definition: symbol.h:28
Support for working with literal values appearing in source code.