// 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; using System.Collections.Generic; namespace PascalABCCompiler.TreeRealization { /// /// Базовый класс для представления констант в выражениях. /// Этот класс представляет только неименованные константы. /// [Serializable] public abstract class constant_node : expression_node, SemanticTree.IConstantNode { private type_node _type; /// /// Конструктор узла. /// /// Тип константы. /// Расположение константы. public constant_node(type_node tn, location loc) : base(null,loc) { _type = tn; } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } internal virtual object get_object_value() { return null; } object SemanticTree.IConstantNode.value { get { return get_object_value(); } } public virtual constant_node get_constant_copy(location loc) { return null; } public override type_node type { get { return _type; } set { _type = value; } } public virtual void SetType(type_node tn) { _type = tn; } public override bool is_addressed { get { return false; } } public static constant_node make_constant(object obj) { return NetHelper.NetHelper.make_constant(obj); } } /// /// Базовый класс для представления констант конкретных типов. /// /// Тип константы. [Serializable] public abstract class concrete_constant : constant_node { /// /// Значение константы. /// private ConstantType _constant_value; /// /// Конструктор узла. /// /// Значение константы. /// Тип константы. public concrete_constant(ConstantType value, location loc) : base(compiled_type_node.get_type_node(typeof(ConstantType)), loc) { _constant_value = value; } /// /// Значение константы. /// public ConstantType constant_value { get { return _constant_value; } set { _constant_value = value; } } internal override object get_object_value() { return _constant_value; } } /// /// Класс для представления булевских констант. /// [Serializable] public class bool_const_node : concrete_constant, SemanticTree.IBoolConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public bool_const_node(bool value, location loc) : base(value,loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.bool_const_node; } } public override constant_node get_constant_copy(location loc) { return new bool_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления int констант. /// [Serializable] public class int_const_node : concrete_constant, SemanticTree.IIntConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public int_const_node(int value,location loc) : base(value,loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.int_const_node; } } public override constant_node get_constant_copy(location loc) { return new int_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления long констант. /// [Serializable] public class long_const_node : concrete_constant, SemanticTree.ILongConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public long_const_node(long value, location loc) : base(value, loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.long_const_node; } } public override constant_node get_constant_copy(location loc) { return new long_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления byte констант. /// [Serializable] public class byte_const_node : concrete_constant, SemanticTree.IByteConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public byte_const_node(byte value, location loc) : base(value, loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.byte_const_node; } } public override constant_node get_constant_copy(location loc) { return new byte_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления signed byte констант. /// [Serializable] public class sbyte_const_node : concrete_constant, SemanticTree.ISByteConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public sbyte_const_node(sbyte value, location loc) : base(value, loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.sbyte_const_node; } } public override constant_node get_constant_copy(location loc) { return new sbyte_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления short констант. /// [Serializable] public class short_const_node : concrete_constant, SemanticTree.IShortConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public short_const_node(short value, location loc) : base(value, loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.short_const_node; } } public override constant_node get_constant_copy(location loc) { return new short_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления unsigned short констант. /// [Serializable] public class ushort_const_node : concrete_constant, SemanticTree.IUShortConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public ushort_const_node(ushort value, location loc) : base(value, loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.ushort_const_node; } } public override constant_node get_constant_copy(location loc) { return new ushort_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления unsigned int констант. /// [Serializable] public class uint_const_node : concrete_constant, SemanticTree.IUIntConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public uint_const_node(uint value, location loc) : base(value, loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.uint_const_node; } } public override constant_node get_constant_copy(location loc) { return new uint_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления unsigned long констант. /// [Serializable] public class ulong_const_node : concrete_constant, SemanticTree.IULongConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public ulong_const_node(ulong value, location loc) : base(value, loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.ulong_const_node; } } public override constant_node get_constant_copy(location loc) { return new ulong_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления double констант. /// [Serializable] public class double_const_node : concrete_constant, SemanticTree.IDoubleConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public double_const_node(double value,location loc) : base(value,loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.double_const_node; } } public override constant_node get_constant_copy(location loc) { return new double_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления float констант. /// [Serializable] public class float_const_node : concrete_constant, SemanticTree.IFloatConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public float_const_node(float value, location loc) : base(value, loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.float_const_node; } } public override constant_node get_constant_copy(location loc) { return new float_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления char констант (этот класс для 2-байтных char - widechar в delphi). /// [Serializable] public class char_const_node : concrete_constant, SemanticTree.ICharConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public char_const_node(char value,location loc) : base(value,loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.char_const_node; } } public override constant_node get_constant_copy(location loc) { return new char_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления строковых констант. /// public class string_const_node : concrete_constant, SemanticTree.IStringConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public string_const_node(string value,location loc) : base(value,loc) { } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.string_const_node; } } public override constant_node get_constant_copy(location loc) { return new string_const_node(this.constant_value,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления константы nil. /// public class null_const_node : constant_node, SemanticTree.INullConstantNode { /// /// Конструктор узла. /// /// Значение константы. /// Расположение контанты. public null_const_node(type_node tn,location loc) : base(tn,loc) { } public static null_const_node get_const_node_with_type(type_node tn, null_const_node cnst) { return new null_const_node(tn, cnst.location); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.null_const_node; } } public override constant_node get_constant_copy(location loc) { return new null_const_node(this.type,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } } /// /// Класс для представления констaнтной записи /// [Serializable] public class record_constant : constant_node, SemanticTree.IRecordConstantNode { private List _field_values = new List(); public List field_values { get { return _field_values; } } internal List record_const_definition_list; public record_constant(List field_values, location loc) :base(null, loc) { _field_values = field_values; } internal record_constant(List record_const_definition_list, location loc) : base(null, loc) { this.record_const_definition_list = record_const_definition_list; } public override constant_node get_constant_copy(location loc) { record_constant rc = new record_constant(this.record_const_definition_list,loc); rc.type = this.type; return rc; } public SemanticTree.IConstantNode[] FieldValues { get { return _field_values.ToArray(); } } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.record_const; } } } public class enum_const_node : constant_node, SemanticTree.IEnumConstNode { private int _constant_value; public enum_const_node(int value, type_node ctn, location loc):base(ctn,loc) { this._constant_value = value; } internal override object get_object_value() { return _constant_value; } public int constant_value { get { return _constant_value; } } public override constant_node get_constant_copy(location loc) { return new enum_const_node(this._constant_value,this.type,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.enum_const; } } } /// /// Класс для представления константного массива /// [Serializable] public class array_const : constant_node, SemanticTree.IArrayConstantNode { private List _element_values; public List element_values { get { return _element_values; } } SemanticTree.IConstantNode[] SemanticTree.IArrayConstantNode.ElementValues { get { return _element_values.ToArray(); } } SemanticTree.ITypeNode SemanticTree.IArrayConstantNode.ElementType { get { return element_type; } } public array_const(List element_values, location loc) : base(null, loc) { this._element_values = element_values; } public override constant_node get_constant_copy(location loc) { array_const ac = new array_const(this._element_values,loc); ac.type = this.type; return ac; } public type_node element_type { get { return element_values[0].type; } } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.array_const; } } } public class compiled_static_field_reference_as_constant : constant_node, SemanticTree.ICompiledStaticFieldReferenceNodeAsConstant { private static_compiled_variable_reference _field_reference; public static_compiled_variable_reference field_reference { get { return _field_reference; } set { _field_reference = value; } } public compiled_static_field_reference_as_constant(static_compiled_variable_reference field_reference, location loc) : base(field_reference.type, loc) { _field_reference = field_reference; } SemanticTree.IStaticCompiledFieldReferenceNode SemanticTree.ICompiledStaticFieldReferenceNodeAsConstant.FieldReference { get { return _field_reference; } } public override constant_node get_constant_copy(location loc) { return new compiled_static_field_reference_as_constant(this.field_reference, loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.compiled_static_field_reference_as_constant; } } } public class compiled_static_method_call_as_constant : constant_node, SemanticTree.ICompiledStaticMethodCallNodeAsConstant { private compiled_static_method_call _method_call; public compiled_static_method_call method_call { get { return _method_call; } set { _method_call = value; } } public compiled_static_method_call_as_constant(compiled_static_method_call method_call, location loc): base(method_call.type, loc) { _method_call = method_call; } SemanticTree.ICompiledStaticMethodCallNode SemanticTree.ICompiledStaticMethodCallNodeAsConstant.MethodCall { get { return _method_call; } } public override constant_node get_constant_copy(location loc) { return new compiled_static_method_call_as_constant(this.method_call,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.compiled_static_method_call_node_as_constant; } } } public class common_namespace_function_call_as_constant : constant_node, SemanticTree.ICommonNamespaceFunctionCallNodeAsConstant { private common_namespace_function_call _method_call; public common_namespace_function_call method_call { get { return _method_call; } set { _method_call = value; } } public common_namespace_function_call_as_constant(common_namespace_function_call method_call, location loc) : base(method_call.type, loc) { _method_call = method_call; } SemanticTree.ICommonNamespaceFunctionCallNode SemanticTree.ICommonNamespaceFunctionCallNodeAsConstant.MethodCall { get { return _method_call; } } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.common_namespace_function_call_node_as_constant; } } } public class basic_function_call_as_constant : constant_node, SemanticTree.IBasicFunctionCallNodeAsConstant { private basic_function_call _method_call; public basic_function_call method_call { get { return _method_call; } set { _method_call = value; } } public basic_function_call_as_constant(basic_function_call method_call, location loc) : base(method_call.type, loc) { _method_call = method_call; } SemanticTree.IBasicFunctionCallNode SemanticTree.IBasicFunctionCallNodeAsConstant.MethodCall { get { return _method_call; } } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.basic_function_call_node_as_constant; } } } public class compiled_constructor_call_as_constant : constant_node, SemanticTree.ICompiledConstructorCallAsConstant { private compiled_constructor_call _method_call; public compiled_constructor_call method_call { get { return _method_call; } set { _method_call = value; } } public compiled_constructor_call_as_constant(compiled_constructor_call method_call, location loc) : base(method_call.type, loc) { _method_call = method_call; } SemanticTree.ICompiledConstructorCall SemanticTree.ICompiledConstructorCallAsConstant.MethodCall { get { return _method_call; } } public override constant_node get_constant_copy(location loc) { return new compiled_constructor_call_as_constant(this.method_call,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.compiled_constructor_call_as_constant; } } } public class namespace_constant_reference : constant_node, SemanticTree.INamespaceConstantReference { private namespace_constant_definition cdn; public namespace_constant_reference(namespace_constant_definition cdn, location loc):base(cdn.type,loc) { this.cdn = cdn; } public namespace_constant_definition constant { get { return cdn; } set { cdn = value; } } SemanticTree.INamespaceConstantDefinitionNode SemanticTree.INamespaceConstantReference.Constant { get { return cdn; } } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.namespace_constant_reference; } } } public class function_constant_reference : constant_node, SemanticTree.IFunctionConstantReference { private function_constant_definition cdn; public function_constant_reference(function_constant_definition cdn, location loc):base(cdn.type,loc) { this.cdn = cdn; } public function_constant_definition constant { get { return cdn; } set { cdn = value; } } SemanticTree.ICommonFunctionConstantDefinitionNode SemanticTree.IFunctionConstantReference.Constant { get { return cdn; } } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.function_constant_reference; } } } public class common_constructor_call_as_constant : constant_node, SemanticTree.ICommonConstructorCallAsConstant { private common_constructor_call _constructor_call; public common_constructor_call constructor_call { get { return _constructor_call; } set { _constructor_call = value; } } public common_constructor_call_as_constant(common_constructor_call constructor_call, location loc) : base(constructor_call.type, loc) { _constructor_call = constructor_call; } SemanticTree.ICommonConstructorCall SemanticTree.ICommonConstructorCallAsConstant.ConstructorCall { get { return _constructor_call; } } public override constant_node get_constant_copy(location loc) { return new common_constructor_call_as_constant(this._constructor_call,loc); } /// /// Метод для обхода дерева посетителем. /// /// Класс - посетитель дерева. public override void visit(SemanticTree.ISemanticVisitor visitor) { visitor.visit(this); } /// /// Тип узла. /// public override semantic_node_type semantic_node_type { get { return semantic_node_type.common_constructor_call_as_constant; } } } }