summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/type/class.hh
blob: 37779e8501164a361c9923ae85ce72339b576ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
 ** \file type/class.hh
 ** \brief The class Class.
 */
#pragma once

#include <vector>

#include <misc/symbol.hh>
#include <type/attribute.hh>
#include <type/method.hh>
#include <type/type.hh>

namespace type
{
  /** \brief Class types.
   **
   ** List of Attributes and Methods. */
  class Class : public Type
  {
    /** \name Constructor.
     ** \{ */
  public:
    /** \brief Construct a Class.
     **
     ** \param super Super class. */
    explicit Class(const Class* super = nullptr);
    /** \} */

    /// \name Visitors entry point.
    /** \{ */
    /// Accept a const visitor \a v.
    void accept(ConstVisitor& v) const override;
    /// Accept a non-const visitor \a v.
    void accept(Visitor& v) override;
    /** \} */

    /** \name Attribute and Method elementary manipulation.
     ** \{ */
    /// \brief Return the attribute type associated to \a key.
    ///
    /// The search is performed throughout the super classes.
    const Type* attr_type(misc::symbol key) const;
    /// \brief Return the method type associated to \a key.
    ///
    /// The search is performed throughout the super classes.
    const Type* meth_type(misc::symbol key) const;
    /** \} */

    /** \name Internal Attribute list manipulators.
     ** \{ */
    /// List of Attribute's.
    using attrs_type = std::vector<Attribute>;
    /// List of Method's.
    using meths_type = std::vector<const Method*>;
    /// List of Subclasses's.
    using subclasses_type = std::vector<const Class*>;

    /// Return the list of stored Attributes (read only).
    const attrs_type& attrs_get() const;
    /// Return the list of stored Methods (read only).
    const meths_type& meths_get() const;

    /// \brief Find an attribute using its name, return `nullptr' if
    /// not found.
    ///
    /// The search is performed throughout the super classes.
    const Attribute* attr_find(misc::symbol key) const;
    /// \brief Find a method using its name, return `nullptr' if
    /// not found.
    ///
    /// The search is performed throughout the super classes.
    const Method* meth_find(misc::symbol key) const;

    /// \brief Find an owned attribute using its name, return
    /// `nullptr' if not found.
    ///
    /// The search is restricted to the class.
    const Attribute* owned_attr_find(misc::symbol key) const;

    /// \brief Find an owned method using its name, return `nullptr'
    /// if not found.
    ///
    /// The search is restricted to the class.
    const Method* owned_meth_find(misc::symbol key) const;

    /// Add an already existing Attribute to the list.
    void attr_add(const Attribute& attr);
    /// Create a Attribute then add it to the list.
    void attr_add(const ast::VarDec* def);

    /// Add an already existing Method to the list.
    void meth_add(const Method* method);
    /** \} */

    /// Does this class have actual data (i.e., owned attributes)?
    bool has_data() const;

    /** \name Accessors.
     ** \{ */
    /// Return the unique identifier of the class.
    unsigned id_get() const;

    /// Return the type of the super class.
    const Class* super_get() const;
    /// Set the type of the super class.
    void super_set(const Class* type);

    /// Return (the transitive closure of) the list of subclasses.
    const subclasses_type& subclasses_get() const;

    /// \brief Add a class to the list of subclasses.
    ///
    /// Although this method alters \a this, it is const, since types
    /// are mostly manipulated as const entities.
    void subclass_add(const Class* subclass) const;

    /// \brief Erase all the subclasses.
    ///
    /// This method is const for the same reason as
    /// type::Class::subclass_add.
    void subclasses_clear() const;
    /** \} */

    /** \name Type resolution.
     ** \{ */
    /** \brief Find the common super class.  */
    const Class* common_root(const Class& other) const;

    /** \brief Check that the definition of this class is sound,
     ** i.e. that there is no recursive inheritance.  */
    bool sound() const;
    /** \} */

    // FIXME DONE: Some code was deleted here (Inherited methods).
    bool compatible_with(const Type& other) const override;

    /// Return the unique instance of the class type `Object'.
    static const Class& object_instance();

  private:
    /// Return a fresh identifier.
    static unsigned fresh_id();

    /// Class unique identifier
    unsigned id_;
    /// Super class.
    const Class* super_;
    /// Sub classes.
    mutable subclasses_type subclasses_;
    /// Attributes list.
    attrs_type attrs_;
    /// Methods list.
    meths_type meths_;
  };

} // namespace type

#include <type/class.hxx>