part of type_check_visitor

This commit is contained in:
ProgramSnail 2023-05-04 16:11:25 +03:00
parent 94ef8702fb
commit f7e985a448
10 changed files with 377 additions and 88 deletions

View file

@ -0,0 +1,31 @@
#pragma once
#include <vector>
// for clangd
#include "types.hpp"
#include "utils.hpp"
// TODO
namespace info {
struct BuiltinFunction {
public:
BuiltinFunction(const std::vector<utils::IdType>& argument_types, utils::IdType return_type)
: argument_types(argument_types), return_type(return_type) {}
static std::optional<BuiltinFunction> FindMethod(const std::string& name, info::type::TypeManager& type_manager) {
return std::nullopt;
}
static std::optional<BuiltinFunction> FindStaticMethod(const std::string& name, info::type::TypeManager& type_manager) {
return std::nullopt;
}
public:
std::vector<utils::IdType> argument_types;
utils::IdType return_type;
};
} // namespace info

View file

@ -17,6 +17,8 @@ namespace interpreter {
namespace info::definition {
struct Namespace;
struct TypeUsage {
interpreter::tokens::TypeExpression* node;
};
@ -42,6 +44,8 @@ struct AnyType {
Parameter type;
std::vector<Parameter> parameters;
interpreter::tokens::AnyType* value;
Namespace* parent_namespace = nullptr;
};
struct Type {
@ -52,6 +56,7 @@ struct Constructor {
std::string name;
size_t order;
utils::IdType type_id;
std::optional<interpreter::tokens::TupleType*> constructor_tuple_node;
};
struct FunctionDeclaration {
@ -90,7 +95,7 @@ struct Namespace {
std::unordered_map<std::string, Namespace> namespaces;
std::unordered_map<std::string, Namespace> variable_namespaces;
Namespace* parent_namespace = nullptr;;
Namespace* parent_namespace = nullptr;
std::optional<Modifier> modifier; // modifier => variable namespace
std::string type_name;

View file

@ -6,6 +6,7 @@
// for clangd
#include "definitions.hpp"
#include "interpreter_tree.hpp"
#include "utils.hpp"
namespace info {
@ -82,6 +83,10 @@ public:
const std::vector<std::string>& GetCurrentPath() {
return current_path_;
}
definition::Namespace* GetCurrentNamespace() {
return namespace_stack_.back();
}
private:
NamespaceVisitor(GlobalInfo& global_info) : global_info_(global_info),
namespace_stack_({&global_info.global_namespace_}) {}
@ -104,23 +109,31 @@ public:
}
// remember about vector realloc
definition::Function* GetFunctionInfo(utils::IdType id) {
return &functions_[id];
const definition::Function& GetFunctionInfo(utils::IdType id) {
return functions_.at(id);
}
template<typename T>
const std::optional<T>& GetTypeInfo(utils::IdType id) {
if (!std::holds_alternative<T>(types_.at(id).type)) {
return std::nullopt;
}
return std::get<T>(types_[id].type);
}
// remember about vector realloc
definition::Type* GetTypeInfo(utils::IdType id) {
return &types_[id];
const definition::Type& GetAnyTypeInfo(utils::IdType id) {
return types_.at(id);
}
// remember about vector realloc
definition::Typeclass* GetTypeclassInfo(utils::IdType id) {
return &typeclasses_[id];
const definition::Typeclass& GetTypeclassInfo(utils::IdType id) {
return typeclasses_.at(id);
}
// remember about vector realloc
definition::Constructor* GetConstructorInfo(utils::IdType id) {
return &constructors_[id];
const definition::Constructor& GetConstructorInfo(utils::IdType id) {
return constructors_.at(id);
}
private:

View file

@ -231,7 +231,6 @@ struct ParametrizedTypeclass;
// Typeclass & Type
struct ParametrizedType;
// TypeSubExpression -> ParametrizedType
//

View file

@ -1,6 +1,7 @@
#pragma once
// for clangd
#include "interpreter_tree.hpp"
#include "type_info_contexts.hpp"
#include "utils.hpp"
#include "visitor.hpp"
@ -119,6 +120,17 @@ private:
void Visit(CharLiteral* node) override;
void Visit(UnitLiteral* node) override;
//
void CollectTypeContext(const ParametrizedType& type,
std::unordered_map<std::string, utils::IdType>& context);
void CollectTypeExpressionContext(const TypeExpression& type_expression,
std::unordered_map<std::string, utils::IdType>& context);
utils::IdType TypeInContext(utils::IdType type,
const std::unordered_map<std::string, utils::IdType>& context);
private:
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
info::TypeInfoContextManager& context_manager_;

View file

@ -48,9 +48,14 @@ public:
bool operator<(const DefinedType& type) const;
bool operator>(const DefinedType& type) const;
utils::IdType GetTypeId() {
utils::IdType GetTypeId() const {
return type_id_;
}
utils::IdType GetType() const {
return type_;
}
private:
utils::IdType type_id_; // in defined types
utils::IdType type_; // in types manager, created using context types (if specific type)
@ -82,6 +87,7 @@ public:
const std::vector<std::pair<std::optional<std::string>, utils::IdType>>& GetFields() const {
return fields_;
}
private:
std::optional<std::string> name_;
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields_;
@ -103,6 +109,7 @@ public:
const std::vector<TupleType>& GetConstructors() const {
return constructors_;
}
private:
std::optional<std::string> name_;
std::vector<TupleType> constructors_;
@ -119,6 +126,7 @@ public:
bool Same(const OptionalType& type) const;
bool operator<(const OptionalType& type) const;
bool operator>(const OptionalType& type) const;
private:
utils::IdType type_;
TypeManager* type_manager_ = nullptr;
@ -136,6 +144,7 @@ public:
bool Same(const ReferenceToType& type) const;
bool operator<(const ReferenceToType& type) const;
bool operator>(const ReferenceToType& type) const;
private:
std::vector<utils::ReferenceType> references_;
utils::IdType type_;
@ -156,6 +165,7 @@ public:
bool Same(const FunctionType& type) const;
bool operator<(const FunctionType& type) const;
bool operator>(const FunctionType& type) const;
private:
std::vector<utils::IdType> argument_types_;
utils::IdType return_type_;
@ -178,6 +188,7 @@ public:
utils::IdType GetElementsType() {
return elements_type_;
}
private:
size_t size_; // = 0 for dynamic
utils::IdType elements_type_;
@ -193,6 +204,7 @@ public:
bool Same(const Type& type) const;
bool operator<(const Type& type) const; // TODO: rule exceptions
bool operator>(const Type& type) const;
private:
std::variant<AbstractType,
DefinedType,
@ -216,6 +228,7 @@ public:
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_;
};