Nearly CC
An educational compiler skeleton
symbol.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 SYMBOL_H
22 #define SYMBOL_H
23 
24 #include <string>
25 #include "has_operand.h"
26 class Type;
27 
28 class Symbol : public HasOperand {
29 public:
30  enum Kind {
31  VAR_DEF,
32  FUNC_DEF,
33  FUNC_DECL,
34  STRUCT_TYPE_DEF,
35  UNION_TYPE_DEF,
36  };
37 
38  enum StorageClass {
39  STATIC,
40  EXTERN,
41  AUTO,
42  GLOBAL,
43  NONE,
44  };
45 
46 private:
47  std::string m_name;
48  Kind m_kind;
49  StorageClass m_storage_class;
50  const Type *m_type;
51  std::string m_codegen_name;
52  unsigned m_offset;
53 
54  // copy constructor and assignment operator are not allowed
55  Symbol(const Symbol &);
56  Symbol &operator=(const Symbol &);
57 
58 public:
59  Symbol(const std::string &name, Kind kind, StorageClass storage_class, const Type *type);
60  virtual ~Symbol();
61 
62  const std::string &get_name() const { return m_name; }
63  Kind get_kind() const { return m_kind; }
64  StorageClass get_storage_class() const { return m_storage_class; }
65  const Type *get_type() const { return m_type; }
66 
67  void promote_fn_decl_to_def();
68 
69  void set_codegen_name(const std::string &codegen_name);
70  std::string get_codegen_name() const;
71 
72  void set_offset(unsigned offset) { m_offset = offset; }
73  unsigned get_offset() const { return m_offset; }
74 };
75 
76 
77 #endif // SYMBOL_H
HasOperand is a base class for NodeBase, which in turn is a base class for Node.
Definition: has_operand.h:31
Definition: symbol.h:28
Representation of a C data type.
Definition: type.h:60