From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- tiger-compiler/src/type/README.txt | 92 ++++ tiger-compiler/src/type/array.cc | 28 ++ tiger-compiler/src/type/array.hh | 30 ++ tiger-compiler/src/type/array.hxx | 18 + tiger-compiler/src/type/attribute.cc | 16 + tiger-compiler/src/type/attribute.hh | 44 ++ tiger-compiler/src/type/attribute.hxx | 25 ++ tiger-compiler/src/type/builtin-types.cc | 22 + tiger-compiler/src/type/builtin-types.hh | 40 ++ tiger-compiler/src/type/class.cc | 123 ++++++ tiger-compiler/src/type/class.hh | 159 +++++++ tiger-compiler/src/type/class.hxx | 88 ++++ tiger-compiler/src/type/default-visitor.hh | 65 +++ tiger-compiler/src/type/default-visitor.hxx | 95 +++++ tiger-compiler/src/type/field.cc | 17 + tiger-compiler/src/type/field.hh | 45 ++ tiger-compiler/src/type/field.hxx | 16 + tiger-compiler/src/type/function.cc | 38 ++ tiger-compiler/src/type/function.hh | 59 +++ tiger-compiler/src/type/function.hxx | 16 + tiger-compiler/src/type/fwd.hh | 31 ++ tiger-compiler/src/type/libtype.cc | 19 + tiger-compiler/src/type/libtype.hh | 19 + tiger-compiler/src/type/local.am | 29 ++ tiger-compiler/src/type/method.cc | 48 +++ tiger-compiler/src/type/method.hh | 90 ++++ tiger-compiler/src/type/method.hxx | 27 ++ tiger-compiler/src/type/named.cc | 61 +++ tiger-compiler/src/type/named.hh | 90 ++++ tiger-compiler/src/type/named.hxx | 28 ++ tiger-compiler/src/type/nil.cc | 34 ++ tiger-compiler/src/type/nil.hh | 47 ++ tiger-compiler/src/type/pretty-printer.cc | 127 ++++++ tiger-compiler/src/type/pretty-printer.hh | 67 +++ tiger-compiler/src/type/record.cc | 52 +++ tiger-compiler/src/type/record.hh | 69 +++ tiger-compiler/src/type/record.hxx | 34 ++ tiger-compiler/src/type/tasks.cc | 22 + tiger-compiler/src/type/tasks.hh | 29 ++ tiger-compiler/src/type/type-checker.cc | 639 ++++++++++++++++++++++++++++ tiger-compiler/src/type/type-checker.hh | 279 ++++++++++++ tiger-compiler/src/type/type-checker.hxx | 127 ++++++ tiger-compiler/src/type/type.cc | 18 + tiger-compiler/src/type/type.hh | 66 +++ tiger-compiler/src/type/type.hxx | 37 ++ tiger-compiler/src/type/types.hh | 18 + tiger-compiler/src/type/visitor.hh | 57 +++ tiger-compiler/src/type/visitor.hxx | 29 ++ 48 files changed, 3229 insertions(+) create mode 100644 tiger-compiler/src/type/README.txt create mode 100644 tiger-compiler/src/type/array.cc create mode 100644 tiger-compiler/src/type/array.hh create mode 100644 tiger-compiler/src/type/array.hxx create mode 100644 tiger-compiler/src/type/attribute.cc create mode 100644 tiger-compiler/src/type/attribute.hh create mode 100644 tiger-compiler/src/type/attribute.hxx create mode 100644 tiger-compiler/src/type/builtin-types.cc create mode 100644 tiger-compiler/src/type/builtin-types.hh create mode 100644 tiger-compiler/src/type/class.cc create mode 100644 tiger-compiler/src/type/class.hh create mode 100644 tiger-compiler/src/type/class.hxx create mode 100644 tiger-compiler/src/type/default-visitor.hh create mode 100644 tiger-compiler/src/type/default-visitor.hxx create mode 100644 tiger-compiler/src/type/field.cc create mode 100644 tiger-compiler/src/type/field.hh create mode 100644 tiger-compiler/src/type/field.hxx create mode 100644 tiger-compiler/src/type/function.cc create mode 100644 tiger-compiler/src/type/function.hh create mode 100644 tiger-compiler/src/type/function.hxx create mode 100644 tiger-compiler/src/type/fwd.hh create mode 100644 tiger-compiler/src/type/libtype.cc create mode 100644 tiger-compiler/src/type/libtype.hh create mode 100644 tiger-compiler/src/type/local.am create mode 100644 tiger-compiler/src/type/method.cc create mode 100644 tiger-compiler/src/type/method.hh create mode 100644 tiger-compiler/src/type/method.hxx create mode 100644 tiger-compiler/src/type/named.cc create mode 100644 tiger-compiler/src/type/named.hh create mode 100644 tiger-compiler/src/type/named.hxx create mode 100644 tiger-compiler/src/type/nil.cc create mode 100644 tiger-compiler/src/type/nil.hh create mode 100644 tiger-compiler/src/type/pretty-printer.cc create mode 100644 tiger-compiler/src/type/pretty-printer.hh create mode 100644 tiger-compiler/src/type/record.cc create mode 100644 tiger-compiler/src/type/record.hh create mode 100644 tiger-compiler/src/type/record.hxx create mode 100644 tiger-compiler/src/type/tasks.cc create mode 100644 tiger-compiler/src/type/tasks.hh create mode 100644 tiger-compiler/src/type/type-checker.cc create mode 100644 tiger-compiler/src/type/type-checker.hh create mode 100644 tiger-compiler/src/type/type-checker.hxx create mode 100644 tiger-compiler/src/type/type.cc create mode 100644 tiger-compiler/src/type/type.hh create mode 100644 tiger-compiler/src/type/type.hxx create mode 100644 tiger-compiler/src/type/types.hh create mode 100644 tiger-compiler/src/type/visitor.hh create mode 100644 tiger-compiler/src/type/visitor.hxx (limited to 'tiger-compiler/src/type') diff --git a/tiger-compiler/src/type/README.txt b/tiger-compiler/src/type/README.txt new file mode 100644 index 0000000..12bc011 --- /dev/null +++ b/tiger-compiler/src/type/README.txt @@ -0,0 +1,92 @@ +* README + +Hierarchy of types for the Tiger language, with (most of) their +interfaces. + + +ostream& operator<<(ostream& ostr, const Type& t) +bool operator==(const Type& lhs, const Type& rhs); +bool operator!=(const Type& lhs, const Type& rhs); + +/Type/ + virtual ~Type() {} + virtual const Type& actual() const; + virtual bool compatible_with(const Type& other) const; + virtual void accept(ConstVisitor& v) const; + virtual void accept(Visitor& v); + + Nil + bool compatible_with(const Type& other) const override; + const Type* record_type_get() const; + void set_record_type(const Type& type) const; + + Void + + Int + + String + + Named(misc::symbol name, const Type* type) + const Type* type_get() const; + void type_set(const Type *type) const; + misc::symbol name_get() const; + void name_set(misc::symbol name); + const Type& actual() const override; + const bool sound() const; + bool compatible_with(const Type& other) const override; + + Array(const Type& type) + const Type& type_get() const; + + Record() + const Type* field_type(misc::symbol key) const; + int field_index(misc::symbol key) const; + const list fields_get() const; + void field_add(const Field& field); + void field_add(misc::symbol name, const Type& type); + const_iterator begin() const; + const_iterator end() const; + + Class(const Class* super = nullptr) + const Type* attr_type(misc::symbol key) const; + const Type* meth_type(misc::symbol key) const; + const attrs_type& attrs_get() const; + const meths_type& meths_get() const; + const Attribute* attr_find(misc::symbol key) const; + const Attribute* owner_attr_find(misc::symbol key) const; + const Method* owned_meth_find(misc::symbol key) const; + void attr_add(const Attribute& attr); + void attr_add(const VarDec* def); + void meth_add(const Method* method); + bool has_data() const; + unsigned id_get() const; + const Class* super_get() const; + void super_set(const Class* type); + const subclasses_type& subclasses_get() const; + void subclass_add(const Class* subclass) const; + void subclasses_clear() const; + const Class* common_root(const Class& other) const; + bool sound() const; + static const Class& object_instance(); + + Function(const Record* formals, const Type& result) + + Method(misc::symbol name, const Class* owner, const Record* formals, + const Type& result, MethodDec* def) + misc::symbol name_get() const; + const Class* owner_get() const; + const Type& type_get() const; + const MethodDec* def_get() const; + MethodDec* def_get(); + void name_set(misc::symbol name); + void def_set(MethodDec* def); + +Field(misc::symbol name, const Type& type) + misc::symbol name_get() const; + const Type& type_get() const; + +Attribute(const ast::VarDec* def) + misc::symbol name_get() const; + const Type& type_get() const; + const ast::VarDec* def_get() const; + void def_set(const ast::VarDec* def); diff --git a/tiger-compiler/src/type/array.cc b/tiger-compiler/src/type/array.cc new file mode 100644 index 0000000..1cf51ca --- /dev/null +++ b/tiger-compiler/src/type/array.cc @@ -0,0 +1,28 @@ +/** + ** \file type/array.cc + ** \brief Implementation for type/array.hh. + */ + +#include +#include + +namespace type +{ + + // FIXME DONE: Some code was deleted here. + + Array::Array(const Type* const element_type) + : element_type_(element_type) + {} + + void Array::accept(ConstVisitor& v) const + { + v(*this); + } + + void Array::accept(Visitor& v) + { + v(*this); + } + +} // namespace type diff --git a/tiger-compiler/src/type/array.hh b/tiger-compiler/src/type/array.hh new file mode 100644 index 0000000..417eeee --- /dev/null +++ b/tiger-compiler/src/type/array.hh @@ -0,0 +1,30 @@ +/** + ** \file type/array.hh + ** \brief The class Array. + */ +#pragma once + +#include +#include + +namespace type +{ + /// Array types. + class Array : public Type + { + // FIXME DONE: Some code was deleted here. + public: + Array(const Type* element_type); + + void accept(ConstVisitor& v) const override; + void accept(Visitor& v) override; + + inline const Type* get_element_type() const; + + protected: + const Type* const element_type_; + }; + +} // namespace type + +#include diff --git a/tiger-compiler/src/type/array.hxx b/tiger-compiler/src/type/array.hxx new file mode 100644 index 0000000..1ed65da --- /dev/null +++ b/tiger-compiler/src/type/array.hxx @@ -0,0 +1,18 @@ +/** + ** \file type/array.hxx + ** \brief Inline methods for type::Array. + */ +#pragma once + +#include +#include + +namespace type +{ + // FIXME DONE: Some code was deleted here. + inline const Type* Array::get_element_type() const + { + return element_type_; + } + +} // namespace type diff --git a/tiger-compiler/src/type/attribute.cc b/tiger-compiler/src/type/attribute.cc new file mode 100644 index 0000000..bfbe537 --- /dev/null +++ b/tiger-compiler/src/type/attribute.cc @@ -0,0 +1,16 @@ +/** + ** \file type/attribute.cc + ** \brief Implementation for type/attribute.hh. + */ + +#include + +#include + +namespace type +{ + Attribute::Attribute(const ast::VarDec* def) + : def_(def) + {} + +} // namespace type diff --git a/tiger-compiler/src/type/attribute.hh b/tiger-compiler/src/type/attribute.hh new file mode 100644 index 0000000..d252da4 --- /dev/null +++ b/tiger-compiler/src/type/attribute.hh @@ -0,0 +1,44 @@ +/** + ** \file type/attribute.hh + ** \brief The class Attribute (of a class type). + */ +#pragma once + +#include + +namespace type +{ + + /** \brief The base object for Class attributes. + ** + ** Very much like Named, but it is *not* a Type. */ + class Attribute + { + /** \name Constructor. + ** \{ */ + public: + /** \brief Construct a Attribute. + ** \param def attribute's definition site.*/ + explicit Attribute(const ast::VarDec* def); + /** \} */ + + /** \name Accessors. + ** \{ */ + public: + /// Return the attribute's name. + misc::symbol name_get() const; + /// Return the attribute's type. + const Type& type_get() const; + /// Return attribute's definition site. + const ast::VarDec* def_get() const; + /// Set the attribute's definition site. + void def_set(const ast::VarDec* def); + /** \} */ + + private: + const ast::VarDec* def_; + }; + +} // namespace type + +#include diff --git a/tiger-compiler/src/type/attribute.hxx b/tiger-compiler/src/type/attribute.hxx new file mode 100644 index 0000000..e59177d --- /dev/null +++ b/tiger-compiler/src/type/attribute.hxx @@ -0,0 +1,25 @@ +/** + ** \file type/attribute.hxx + ** \brief Inline methods for type::Attribute. + */ +#pragma once + +#include + +namespace type +{ + inline misc::symbol Attribute::name_get() const { return def_->name_get(); } + + inline const Type& Attribute::type_get() const + { + // FIXME DONE: Some code was deleted here. + precondition(def_->type_get() != nullptr); + + return *def_->type_get(); + } + + inline const ast::VarDec* Attribute::def_get() const { return def_; } + + inline void Attribute::def_set(const ast::VarDec* def) { def_ = def; } + +} // namespace type diff --git a/tiger-compiler/src/type/builtin-types.cc b/tiger-compiler/src/type/builtin-types.cc new file mode 100644 index 0000000..7ea70ad --- /dev/null +++ b/tiger-compiler/src/type/builtin-types.cc @@ -0,0 +1,22 @@ +/** + ** \file type/builtin-types.cc + ** \brief Implementation for type/builtin-types.hh. + */ + +#include + +#include +#include + +namespace type +{ + // FIXME DONE: Some code was deleted here (Int, String, Void). + void Int::accept(ConstVisitor& v) const { v(instance()); } + void Int::accept(Visitor& v) { v(instance()); } + + void Void::accept(ConstVisitor& v) const { v(instance()); } + void Void::accept(Visitor& v) { v(instance()); } + + void String::accept(ConstVisitor& v) const { v(instance()); } + void String::accept(Visitor& v) { v(instance()); } +} // namespace type diff --git a/tiger-compiler/src/type/builtin-types.hh b/tiger-compiler/src/type/builtin-types.hh new file mode 100644 index 0000000..40295eb --- /dev/null +++ b/tiger-compiler/src/type/builtin-types.hh @@ -0,0 +1,40 @@ +/** + ** \file type/builtin-types.hh + ** \brief The classes Int, String, Void. + */ +#pragma once + +#include +#include +#include + +namespace type +{ + // FIXME DONE: Some code was deleted here (Other types : Int, String, Void). + + class Int + : public Type + , public misc::Singleton + { + public: + void accept(ConstVisitor& v) const override; + void accept(Visitor& v) override; + }; + class Void + : public Type + , public misc::Singleton + { + public: + void accept(ConstVisitor& v) const override; + void accept(Visitor& v) override; + }; + class String + : public Type + , public misc::Singleton + { + public: + void accept(ConstVisitor& v) const override; + void accept(Visitor& v) override; + }; + +} // namespace type diff --git a/tiger-compiler/src/type/class.cc b/tiger-compiler/src/type/class.cc new file mode 100644 index 0000000..a1876d3 --- /dev/null +++ b/tiger-compiler/src/type/class.cc @@ -0,0 +1,123 @@ +/** + ** \file type/class.cc + ** \brief Implementation for type/class.hh. + */ + +#include + +#include +#include +#include + +namespace type +{ + Class::Class(const Class* super) + : Type() + , id_(fresh_id()) + , super_(super) + , subclasses_() + {} + + void Class::accept(ConstVisitor& v) const { v(*this); } + + void Class::accept(Visitor& v) { v(*this); } + + const Type* Class::attr_type(misc::symbol key) const + { + // FIXME DONE: Some code was deleted here. + const auto& attribute = std::ranges::find_if( + attrs_, + [&key] (const misc::symbol& name) { return name == key; }, + [] (const Attribute& attr) { return attr.name_get(); } + ); + + return attribute != attrs_.end() ? &attribute->type_get() : nullptr; + } + + const Type* Class::meth_type(misc::symbol key) const + { + // FIXME DONE: Some code was deleted here. + const auto& method = std::ranges::find_if( + meths_, + [&key] (const misc::symbol& name) { return name == key; }, + [] (const Method* const meth) { return meth->name_get(); } + ); + + return method != meths_.end() ? &(*method)->type_get() : nullptr; + } + + // FIXME DONE: Some code was deleted here (Find common super class). + const Class* Class::common_root(const Class& other) const + { + // temporary note: i'm tired so maybe the algo sucks ass :c + if (*this == other) + { + return this; + } + + std::vector super_classes; + + for (auto this_sc = this; this_sc != nullptr; this_sc = this_sc->super_) + { + super_classes.push_back(this_sc); + } + + for (auto other_sc = &other; other_sc != nullptr; other_sc = other_sc->super_) + { + if (auto matching = std::ranges::find(super_classes, other_sc); + matching != super_classes.end()) + { + return *matching; + } + } + + return nullptr; + } + + // FIXME DONE: Some code was deleted here (Super class soundness test). + bool Class::sound() const + { + auto reference = this; + std::vector previous; + + while (reference != nullptr) + { + if (std::ranges::find(previous, reference) != previous.end()) + { + return false; + } + + previous.push_back(reference); + reference = reference->super_; + } + + return true; + } + + // FIXME DONE: Some code was deleted here (Special implementation of "compatible_with" for Class). + bool Class::compatible_with(const Type& other) const + { + if (Type::compatible_with(other) || dynamic_cast(&other)) + { + return true; + } + + const auto ptr = dynamic_cast(&other); + + return ptr && common_root(*ptr); + } + + const Class& Class::object_instance() + { + // FIXME DONE: Some code was deleted here. + static const Class object { nullptr }; + return object; + } + + unsigned Class::fresh_id() + { + static unsigned counter_ = 0; + return counter_++; + } + +} // namespace type diff --git a/tiger-compiler/src/type/class.hh b/tiger-compiler/src/type/class.hh new file mode 100644 index 0000000..37779e8 --- /dev/null +++ b/tiger-compiler/src/type/class.hh @@ -0,0 +1,159 @@ +/** + ** \file type/class.hh + ** \brief The class Class. + */ +#pragma once + +#include + +#include +#include +#include +#include + +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; + /// List of Method's. + using meths_type = std::vector; + /// List of Subclasses's. + using subclasses_type = std::vector; + + /// 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 diff --git a/tiger-compiler/src/type/class.hxx b/tiger-compiler/src/type/class.hxx new file mode 100644 index 0000000..0a41d17 --- /dev/null +++ b/tiger-compiler/src/type/class.hxx @@ -0,0 +1,88 @@ +/** + ** \file type/class.hxx + ** \brief Inline methods for type::Class. + */ +#pragma once + +#include + +#include +#include + +namespace type +{ + inline const Class::attrs_type& Class::attrs_get() const { return attrs_; } + + inline const Attribute* Class::attr_find(misc::symbol key) const + { + for (const Class* cur = this; cur; cur = cur->super_get()) + { + const Attribute* attr = cur->owned_attr_find(key); + if (attr) + return attr; + } + return nullptr; + } + + inline const Method* Class::meth_find(misc::symbol key) const + { + for (const Class* cur = this; cur; cur = cur->super_get()) + { + const Method* meth = cur->owned_meth_find(key); + if (meth) + return meth; + } + return nullptr; + } + + inline const Attribute* Class::owned_attr_find(misc::symbol key) const + { + for (const Attribute& at : attrs_get()) + if (at.name_get() == key) + return &at; + return nullptr; + } + + inline const Method* Class::owned_meth_find(misc::symbol key) const + { + for (const Method* m : meths_get()) + if (m->name_get() == key) + return m; + return nullptr; + } + + inline void Class::attr_add(const Attribute& attr) + { + attrs_.emplace_back(attr); + } + + inline void Class::attr_add(const ast::VarDec* def) + { + attrs_.emplace_back(def); + } + + inline const Class::meths_type& Class::meths_get() const { return meths_; } + + inline void Class::meth_add(const Method* meth) { meths_.emplace_back(meth); } + + inline bool Class::has_data() const { return !attrs_.empty(); } + + inline unsigned Class::id_get() const { return id_; } + + inline const Class* Class::super_get() const { return super_; } + + inline void Class::super_set(const Class* super) { super_ = super; } + + inline const Class::subclasses_type& Class::subclasses_get() const + { + return subclasses_; + } + + inline void Class::subclass_add(const Class* subclass) const + { + subclasses_.emplace_back(subclass); + } + + inline void Class::subclasses_clear() const { subclasses_.clear(); } + +} // namespace type diff --git a/tiger-compiler/src/type/default-visitor.hh b/tiger-compiler/src/type/default-visitor.hh new file mode 100644 index 0000000..dbcfc0c --- /dev/null +++ b/tiger-compiler/src/type/default-visitor.hh @@ -0,0 +1,65 @@ +/** + ** \file type/default-visitor.hh + ** \brief Traverse a Type, doing nothing. + */ + +#pragma once + +#include + +namespace type +{ + template