big part of type_chack_visitor done

This commit is contained in:
ProgramSnail 2023-05-06 19:26:14 +03:00
parent adccf6feec
commit f7080ba856
7 changed files with 427 additions and 171 deletions

View file

@ -131,13 +131,15 @@ private:
utils::IdType TypeInContext(utils::IdType type,
const std::unordered_map<std::string, utils::IdType>& context);
void CheckPattern(Pattern& node, const BaseNode& base_node);
private:
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
info::TypeInfoContextManager& context_manager_;
utils::IdType current_type_;
std::optional<utils::IdType> returned_type_;
std::optional<bool> is_const_definition_;
std::optional<utils::IsConstModifier> is_const_definition_;
bool is_in_statement_ = false;
};

View file

@ -11,12 +11,16 @@
namespace info {
// TODO: remember about typespointers
// TODO: remember about type pointers
class TypeInfoContextManager {
public:
template<typename T>
utils::IdType AddType(const T&& type) {
return type_manager_.AddType(std::forward(type));
utils::IdType AddType(T&& type, utils::ValueType value_type) {
return type_manager_.AddType(std::forward(type), value_type);
}
utils::IdType AddAnyType(type::Type&& type, utils::ValueType value_type) {
return type_manager_.AddType(std::move(type), value_type);
}
template<typename T>
@ -28,6 +32,10 @@ public:
return type_manager_.GetAnyType(type_id);
}
utils::ValueType GetValueType(utils::IdType type_id) {
return type_manager_.GetValueType(type_id);
}
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
return type_manager_.AddTypeRequirement(type, requrement);
}
@ -36,6 +44,10 @@ public:
return type_manager_.EqualTypes(first_type, second_type);
}
utils::IdType ToModifiedType(utils::IdType type_id, utils::ValueType new_value_type) {
return AddAnyType(type::Type(*GetAnyType(type_id)), new_value_type);
}
type::TypeManager* GetTypeManager() {
return &type_manager_;
}
@ -56,10 +68,9 @@ public:
contexts_.clear();
}
// TODO: variable modifiers
bool DefineVariable(const std::string& name, utils::IdType type_id, bool is_const) {
bool DefineVariable(const std::string& name, utils::IdType type_id) {
// check in previous contexts ??
return contexts_.back().DefineVariable(name, type_id, is_const);
return contexts_.back().DefineVariable(name, type_id);
}
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
@ -80,14 +91,14 @@ public:
return false;
}
void EnterVariableContext(const std::string& name,
utils::IdType type_id, bool is_const) { // for variable namespaces, for loops
contexts_.push_back(false);
void EnterVariableContext(const std::string& name, utils::IdType type_id) {
// for variable namespaces, for loops
contexts_.emplace_back();
DefineVariable(name, type_id, is_const);
DefineVariable(name, type_id);
}
std::optional<std::pair<utils::IdType, bool>> GetVariableInfo(const std::string& name) {
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
auto maybe_type = contexts_[i].GetVariableInfo(name);
if (maybe_type.has_value()) {
@ -110,13 +121,13 @@ public:
private:
class Context {
public:
Context(bool hide_previous) : hide_previous_(hide_previous) {}
Context() = default;
bool DefineVariable(const std::string& name, utils::IdType type_id, bool is_const) {
bool DefineVariable(const std::string& name, utils::IdType type_id) {
if (variables_.count(name) > 0) {
return false;
}
variables_[name] = {type_id, is_const};
variables_[name] = type_id;
return true;
}
@ -132,7 +143,7 @@ private:
return variables_.erase(name);
}
std::optional<std::pair<utils::IdType, bool>> GetVariableInfo(const std::string& name) {
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
auto variable_iter = variables_.find(name);
if (variable_iter == variables_.end()) {
@ -151,11 +162,8 @@ private:
return local_abstract_type_iter->second;
}
bool IsFirst() { return hide_previous_; }
private:
bool hide_previous_;
std::unordered_map<std::string, std::pair<utils::IdType, bool>> variables_;
std::unordered_map<std::string, utils::IdType> variables_;
std::unordered_map<std::string, utils::IdType> local_abstract_types_;
};

View file

@ -31,6 +31,9 @@ public:
bool Same(const AbstractType& type) const;
bool operator<(const AbstractType& type) const;
bool operator>(const AbstractType& type) const;
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
private:
utils::AbstractTypeModifier modifier_;
std::string name_;
@ -50,6 +53,8 @@ public:
bool operator<(const DefinedType& type) const;
bool operator>(const DefinedType& type) const;
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
utils::IdType GetTypeId() const {
return type_id_;
}
@ -86,6 +91,8 @@ public:
bool operator<(const TupleType& type) const;
bool operator>(const TupleType& type) const;
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
const std::vector<std::pair<std::optional<std::string>, utils::IdType>>& GetFields() const {
return fields_;
}
@ -100,21 +107,29 @@ class VariantType {
public:
VariantType() = default;
VariantType(const std::optional<std::string>& name,
const std::vector<TupleType>& constructors)
: name_(name), constructors_(constructors){}
const std::vector<TupleType>& constructors,
std::optional<size_t> current_constructor)
: name_(name), constructors_(constructors), current_constructor_(current_constructor) {}
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
bool Same(const VariantType& type) const;
bool operator<(const VariantType& type) const;
bool operator>(const VariantType& type) const;
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
const std::vector<TupleType>& GetConstructors() const {
return constructors_;
}
const void SetCurrentConstructor(size_t constructor) {
current_constructor_ = constructor;
}
private:
std::optional<std::string> name_;
std::vector<TupleType> constructors_;
std::optional<size_t> current_constructor_;
};
class OptionalType {
@ -129,6 +144,8 @@ public:
bool operator<(const OptionalType& type) const;
bool operator>(const OptionalType& type) const;
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
private:
utils::IdType type_;
TypeManager* type_manager_ = nullptr;
@ -147,6 +164,8 @@ public:
bool operator<(const ReferenceToType& type) const;
bool operator>(const ReferenceToType& type) const;
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
private:
std::vector<utils::ReferenceModifier> references_;
utils::IdType type_;
@ -168,6 +187,8 @@ public:
bool operator<(const FunctionType& type) const;
bool operator>(const FunctionType& type) const;
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
private:
std::vector<utils::IdType> argument_types_;
utils::IdType return_type_;
@ -187,6 +208,8 @@ public:
bool operator<(const ArrayType& type) const;
bool operator>(const ArrayType& type) const;
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
utils::IdType GetElementsType() {
return elements_type_;
}
@ -199,6 +222,8 @@ private:
class Type {
public:
Type() = default;
template<typename T>
explicit Type(T&& type) : type_(std::forward(type)) {}
@ -207,6 +232,8 @@ public:
bool operator<(const Type& type) const; // TODO: rule exceptions
bool operator>(const Type& type) const;
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
private:
std::variant<AbstractType,
DefinedType,
@ -215,24 +242,29 @@ private:
VariantType,
ReferenceToType,
FunctionType,
ArrayType> type_;
ArrayType,
OptionalType> type_;
};
class TypeManager {
public:
template<typename T>
utils::IdType AddType(const T&& type);
utils::IdType AddType(T&& type, utils::ValueType value_type);
utils::IdType AddAnyType(Type&& type, utils::ValueType value_type);
template<typename T>
std::optional<T*> GetType(utils::IdType type_id);
Type* GetAnyType(utils::IdType type_id);
utils::ValueType GetValueType(utils::IdType type_id);
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement);
bool EqualTypes(utils::IdType first_type, utils::IdType second_type);
private:
std::vector<info::type::Type> types_;
std::vector<std::pair<info::type::Type, utils::ValueType>> types_;
};
} // namespace info::type

View file

@ -1,5 +1,6 @@
#pragma once
#include <cstdlib>
#include <vector>
#include <unordered_map>
@ -17,6 +18,18 @@ enum class AliasModifier { Alias = 0, Type = 1, Let = 2 };
enum class AbstractTypeModifier { Basic = 0, Abstract = 1 };
enum class FunctionTypeModifier { Function = 0, Operator = 1 };
enum class ValueType { Const = 0, Var = 1, Tmp = 2 };
inline ValueType IsConstModifierToValueType(IsConstModifier modifier) {
switch (modifier) {
case IsConstModifier::Const:
return ValueType::Const;
case IsConstModifier::Var:
return ValueType::Var;
}
// unreachable
}
template<typename T>
class Storage {
public: