// Copyright (c) Ivan Bondarev, Stanislav Mihalkovich (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
namespace PascalABCCompiler.TreeRealization
{
///
/// Каждый узел семантического дерева должен уметь возвращать свой тип.
/// Это список всех возможных типов узлов семантического дерева.
///
public enum semantic_node_type
{
none, namespace_constant_definition, class_constant_definition, function_constant_definition, compiled_class_constant_definition,
bool_const_node, long_const_node, byte_const_node, int_const_node, double_const_node, char_const_node,
sbyte_const_node, short_const_node, ushort_const_node, uint_const_node, ulong_const_node, float_const_node,
basic_function_call, common_namespace_function_call, common_in_function_function_call, common_method_call,
common_static_method_call, compiled_function_call, compiled_static_method_call,
basic_function_node, common_namespace_function_node, common_in_function_function_node, common_method_node,
compiled_function_node, common_namespace_node, compiled_namespace_node, program_node, dll_node,
common_property_node, basic_property_node, compiled_property_node, if_node, while_node, repeat_node, for_node,
statements_list, basic_type_node, common_type_node, compiled_type_node, common_unit_node, compiled_unit_node, local_variable_reference,
namespace_variable_reference, class_field_reference, static_class_field_reference, compiled_variable_reference,
static_compiled_variable_reference, common_parameter_reference, local_variable, namespace_variable, class_field,
compiled_variable_definition, basic_parameter, compiled_parameter, common_parameter, basic_interface_node,
empty_statement, this_node, return_node, string_const_node, empty_function_node, common_constructor_call,
compiled_constructor_call, pseudo_function, while_break_node, repeat_break_node, for_break_node,
while_continue_node, repeat_continue_node, for_continue_node, compiled_constructor_node, simple_array,
static_property_reference, non_static_property_reference, simple_array_indexing,
external_statement_node, ref_type_node, get_addr_node, deref_node, wrap_def, throw_statement,
switch_node, case_variant_node, case_range_node, null_const_node, null_type_node, unsized_array, delegated_method,
convert_types_function_node, runtime_statement, typed_expression, is_node, as_node, sizeof_operator, typeof_operator,
exit_procedure, exception_filter, try_block, template_type, common_event, compiled_event, static_event_reference,
nonstatic_event_reference, array_const, statement_expression_node, question_colon_expression, record_const,
type_synonym, label, labeled_statement, goto_statement, compiled_static_method_call_node_as_constant, enum_const,
common_namespace_function_call_node_as_constant, foreach_node, lock_statement, local_block_variable,
local_block_variable_reference,compiled_constructor_call_as_constant, rethrow_statement, short_string,
foreach_break_node, foreach_continue_node, generic_indicator, namespace_constant_reference, function_constant_reference,
common_constructor_call_as_constant, array_initializer, record_initializer, default_operator, attribute_node,
pinvoke_node, basic_function_call_node_as_constant, compiled_static_field_reference_as_constant,
common_namespace_event, indefinite_definition_node, indefinite_type, indefinite_function_call, indefinite_reference,
wrapped_statement, wrapped_expression
};
///
/// Для работы часто необходимо знать лищь обобщенный тип узла.
/// Например, часто достаточно знать что конкретный узел является выражением и не важно каким.
/// Каждый узел семантического дерева должен уметь возвращать свой обобщенный тип.
/// Это список всех возможных обобщенных типов узлов.
///
public enum general_node_type {type_node, function_node, namespace_node, unit_node, variable_node,
property_node, constant_definition, statement, expression, program_node, interface_node,
template_type, event_node, type_synonym, label, generic_indicator, attribute_node};
///
/// Базовый абстрактный класс для всех выражений.
///
[Serializable]
public abstract class semantic_node : SemanticTree.ISemanticNode
{
///
/// Обобщенный тип узла.
///
public abstract general_node_type general_node_type
{
get;
}
///
/// Тип узла.
///
public abstract semantic_node_type semantic_node_type
{
get;
}
///
/// Метод для обхода дерева посетителем.
///
/// Класс - посетитель дерева.
public virtual void visit(SemanticTree.ISemanticVisitor visitor)
{
visitor.visit(this);
}
}
///
/// Базовый класс для всех определений в программе.
/// К определениям относятся переменные, константы, свойства, методы и обычные функции, поля, пространства имен,
/// сама программа или dll, типы и модули.
///
[Serializable]
public abstract class definition_node : semantic_node, SemanticTree.IDefinitionNode
{
protected string doc;
protected attributes_list _attributes;
///
/// Метод для обхода дерева посетителем.
///
/// Класс - посетитель дерева.
public override void visit(SemanticTree.ISemanticVisitor visitor)
{
visitor.visit(this);
}
public attributes_list attributes
{
get
{
if (_attributes == null)
_attributes = new attributes_list();
return _attributes;
}
}
public SemanticTree.IAttributeNode[] Attributes
{
get
{
return attributes.ToArray();
}
}
public virtual string documentation
{
get
{
return doc;
}
set
{
doc = value;
}
}
string SemanticTree.IDefinitionNode.Documentation
{
get
{
return doc;
}
}
public virtual semantic_node find_by_location(int line, int col)
{
return null;
}
}
///
/// Базовый класс для всех statement-ов.
///
[Serializable]
public abstract class statement_node : semantic_node, SemanticTree.IStatementNode
{
///
/// Расположение statement-а в программе.
///
private location _loc;
///
/// Конструктор узла.
///
/// Расположение statement-а в программе.
public statement_node()
{
}
public statement_node(location loc)
{
_loc = loc;
}
///
/// Расположение statement-а в программе.
///
public location location
{
get
{
return _loc;
}
set
{
_loc=value;
}
}
///
/// Расположение statement-а в программе.
///
SemanticTree.ILocation SemanticTree.ILocated.Location
{
get
{
return _loc;
}
}
///
/// Обобщенный тип узла.
///
public override general_node_type general_node_type
{
get
{
return general_node_type.statement;
}
}
///
/// Метод для обхода дерева посетителем.
///
/// Класс - посетитель дерева.
public override void visit(SemanticTree.ISemanticVisitor visitor)
{
visitor.visit(this);
}
}
///
/// Базовый класс для всех выражений.
///
[Serializable]
public abstract class expression_node : statement_node, SemanticTree.IExpressionNode
{
///
/// Тип выражения.
///
private type_node _tn;
private type_node _conversion_tn;
///
/// Конструктор выражения.
///
/// Тип выражения.
/// Расположение выражения.
public expression_node()//30_01_2010_Tasha
{
}
public expression_node(type_node tn, location loc) : base(loc)
{
_tn=tn;
}
///
/// Тип выражения. Используется при построении дерева.
///
public virtual type_node type
{
get
{
return _tn;
}
set
{
_tn = value;
}
}
public virtual type_node conversion_type
{
get
{
return _conversion_tn;
}
set
{
_conversion_tn = value;
}
}
///
/// Обобщенный тип узла.
///
public override general_node_type general_node_type
{
get
{
return general_node_type.expression;
}
}
///
/// Является ли это выражение lvalue.
/// Т.е. может ли это выражение стоять в левой части оператора присваивания и передаваться по ссылке.
/// По умолчанию false.
///
public virtual bool is_addressed
{
get
{
return false;
}
}
///
/// Метод для обхода дерева посетителем.
///
/// Класс - посетитель дерева.
public override void visit(SemanticTree.ISemanticVisitor visitor)
{
visitor.visit(this);
}
///
/// Тип выражения. Используется посетителем при обходе дерева.
///
SemanticTree.ITypeNode SemanticTree.IExpressionNode.type
{
get
{
return this.type;
}
}
SemanticTree.ITypeNode SemanticTree.IExpressionNode.conversion_type
{
get
{
return this.conversion_type;
}
}
}
[Serializable]
public class typed_expression : expression_node
{
public typed_expression(type_node type, location loc)
: base(type, loc)
{
}
public override semantic_node_type semantic_node_type
{
get
{
return semantic_node_type.typed_expression;
}
}
}
///
/// Базовый класс для адресных выражений.
/// Т.е. тех, которые могут стоять в левой части оператора присваивания и передаваться по ссылке.
///
[Serializable]
public abstract class addressed_expression : expression_node, SemanticTree.IAddressedExpressionNode
{
///
/// Конструктор адресного выражения.
///
/// Тип выражения.
/// Расположение выражения.
public addressed_expression(type_node tn, location loc) : base(tn,loc)
{
}
///
/// Является ли это выражение lvalue.
/// Т.е. может ли это выражение стоять в левой части оператора присваивания и передаваться по ссылке.
/// По умолчанию true.
///
public override bool is_addressed
{
get
{
return true;
}
}
///
/// Метод для обхода дерева посетителем.
///
/// Класс - посетитель дерева.
public override void visit(SemanticTree.ISemanticVisitor visitor)
{
visitor.visit(this);
}
}
///
/// Класс, представляющий ссылку на объект внутри его метода.
///
[Serializable]
public class this_node : expression_node, SemanticTree.IThisNode
{
///
/// Конструктор this_node.
///
/// Тип объекта.
/// Расположение выражения.
public this_node(type_node type, location loc) : base(type,loc)
{
}
///
/// Тип узла.
///
public override semantic_node_type semantic_node_type
{
get
{
return semantic_node_type.this_node;
}
}
public override bool is_addressed
{
get
{
if (type.is_value_type)
return true;
return false;
}
}
///
/// Метод для обхода дерева посетителем.
///
/// Класс - посетитель дерева.
public override void visit(SemanticTree.ISemanticVisitor visitor)
{
visitor.visit(this);
}
}
///
/// Класс, представляющий операцию получения адреса объекта.
///
[Serializable]
public class get_addr_node : expression_node, SemanticTree.IGetAddrNode
{
///
/// Выражение, адрес которого мы получаем.
///
private readonly expression_node _addr_of;
///
/// Конструктор узла.
///
/// Выражение, адрес которого мы получаем.
/// Расположение узла.
public get_addr_node(expression_node addr_of, location loc) :
base(addr_of.type.ref_type, loc)
{
_addr_of = addr_of;
}
///
/// Тип узла.
///
public override semantic_node_type semantic_node_type
{
get
{
return semantic_node_type.get_addr_node;
}
}
///
/// Выражение, адрес которого мы получаем.
/// Это свойство используется при генерации дерева.
///
public expression_node addr_of
{
get
{
return _addr_of;
}
}
///
/// Выражение, адрес которого мы получаем.
/// Испоьзуется при обходе дерева посетителем.
///
SemanticTree.IExpressionNode SemanticTree.IGetAddrNode.addr_of_expr
{
get
{
return _addr_of;
}
}
///
/// Метод для обхода дерева посетителем.
///
/// Класс - посетитель дерева.
public override void visit(SemanticTree.ISemanticVisitor visitor)
{
visitor.visit(this);
}
}
///
/// Класс, представляющий операцию разыменования объекта.
///
[Serializable]
public class dereference_node : addressed_expression, SemanticTree.IDereferenceNode
{
///
/// Выражение-указатель.
///
private expression_node _deref_expr;
///
/// Конструктор узла.
///
/// Выражение-указатель.
/// Расположение выражения.
public dereference_node(expression_node deref_expr, location loc) :
//base(PascalABCCompiler.SystemLibrary.SystemLibrary.get_pointed_type_by_type(deref_expr.type),loc)
base( (deref_expr.type as ref_type_node).pointed_type, loc)
{
_deref_expr = deref_expr;
}
///
/// Тип узла.
///
public override semantic_node_type semantic_node_type
{
get
{
return semantic_node_type.deref_node;
}
}
///
/// Выражение-указатель, которое мы разыменовываем.
/// Это свойство используется при генерации дерева.
///
public expression_node deref_expr
{
get
{
return _deref_expr;
}
set
{
_deref_expr = value;
}
}
///
/// Выражение-указатель, которое мы разыменовываем.
/// Это свойство используется при обходе дерева посетителем.
///
SemanticTree.IExpressionNode SemanticTree.IDereferenceNode.derefered_expr
{
get
{
return _deref_expr;
}
}
///
/// Метод для обхода дерева посетителем.
///
/// Класс - посетитель дерева.
public override void visit(SemanticTree.ISemanticVisitor visitor)
{
visitor.visit(this);
}
}
}