From 216a4602a251179f430d4f1440c994846e9615d3 Mon Sep 17 00:00:00 2001 From: Thomas Didriksen Date: Tue, 17 Apr 2012 11:22:23 +0200 Subject: [PATCH] Began handling of feature specifications in type checker. --- src/type_checker/tbon_tc_error.e | 1 + src/type_checker/tbon_tc_text_items.e | 15 +++ src/type_checker/textual_bon_type_checker.e | 115 +++++++++++++++++++- src/type_checker/types/tbon_tc_class_type.e | 20 +++- src/type_checker/types/tbon_tc_feature.e | 31 +++++- src/type_checker/types/tbon_tc_generic.e | 16 ++- 6 files changed, 194 insertions(+), 4 deletions(-) diff --git a/src/type_checker/tbon_tc_error.e b/src/type_checker/tbon_tc_error.e index 7ef43c2..cfeffee 100644 --- a/src/type_checker/tbon_tc_error.e +++ b/src/type_checker/tbon_tc_error.e @@ -50,6 +50,7 @@ feature -- Error codes err_code_cluster_not_in_cluster_or_system, err_code_creator_does_not_exist, err_code_involved_class_does_not_exist, + err_code_selective_export_class_does_not_exist, err_code_target_does_not_exist, err_code_undefined: INTEGER = unique diff --git a/src/type_checker/tbon_tc_text_items.e b/src/type_checker/tbon_tc_text_items.e index 767d3a5..5afc176 100644 --- a/src/type_checker/tbon_tc_text_items.e +++ b/src/type_checker/tbon_tc_text_items.e @@ -8,6 +8,9 @@ class TBON_TC_TEXT_ITEMS feature -- Type names + any_type_name: STRING = "ANY" + -- Type name for an ANY type. + boolean_type_name: STRING = "BOOLEAN" -- Type name for a BOOLEAN type. @@ -17,6 +20,9 @@ feature -- Type names integer_type_name: STRING = "INTEGER" -- Type name for an INTEGER type. + none_type_name: STRING = "NONE" + -- Type name for a NONE type. + real_type_name: STRING = "REAL" -- Type name for a REAL type. @@ -142,6 +148,15 @@ feature -- Error messages Result.append_string (".") end + err_selective_export_class_does_not_exist (a_class_name, an_enclosing_class_name: STRING): STRING + do + Result := "Specified selective export class " + Result.append_string (a_class_name.string) + Result.append_string (" in class specification ") + Result.append_string (an_enclosing_class_name.string) + Result.append_string (" does not exist.") + end + err_target_does_not_exist (a_chart_name, a_class_name: STRING): STRING do Result := "Specified target class " diff --git a/src/type_checker/textual_bon_type_checker.e b/src/type_checker/textual_bon_type_checker.e index e082267..a4ce7d3 100644 --- a/src/type_checker/textual_bon_type_checker.e +++ b/src/type_checker/textual_bon_type_checker.e @@ -53,6 +53,7 @@ feature -- Initialization integer_type: TBON_TC_INTEGER_TYPE real_type: TBON_TC_REAL_TYPE string_type: TBON_TC_STRING_TYPE + any_type: TBON_TC_CLASS_TYPE do -- Create type contexts create informal_type_context.default_create @@ -64,6 +65,8 @@ feature -- Initialization create integer_type.make create real_type.make create string_type.make + -- Create ANY + create any_type.make (any_type_name) -- Add default value types to context add_to_informal_type_context (boolean_type) @@ -71,6 +74,14 @@ feature -- Initialization add_to_informal_type_context (integer_type) add_to_informal_type_context (real_type) add_to_informal_type_context (string_type) + add_to_informal_type_context (any_type) + + add_to_formal_type_context (boolean_type) + add_to_formal_type_context (character_type) + add_to_formal_type_context (integer_type) + add_to_formal_type_context (real_type) + add_to_formal_type_context (string_type) + add_to_formal_type_context (any_type) create value_context.default_create @@ -91,6 +102,8 @@ feature {NONE} -- Contexts extended_ids: LIST[STRING] + unresolved_features: MML_SET[TBON_TC_FEATURE] + feature -- Status report first_phase: BOOLEAN -- Is the type checker in the first phase? @@ -870,7 +883,7 @@ feature -- Type checking, static diagrams Result := True - -- Add ancestors as ancestors to enclosing class + -- Add ancestors to enclosing class if an_element.parent_count > 0 then ancestors := an_element.parents @@ -887,6 +900,14 @@ feature -- Type checking, static diagrams end end +-- Idea: +-- Put all features with unresolved types in a set. +-- Every time a new type is added to the context, +-- give corresponding features a pointer to that type. +-- If the feature set is not empty after first phase, +-- an error has occurred, as a feature is mentioning +-- a non-existing type. + else Result := False end @@ -1045,6 +1066,58 @@ feature -- Type checking, static diagrams end end + check_feature_clause (an_element: FEATURE_CLAUSE; enclosing_class: TBON_TC_CLASS_TYPE): BOOLEAN + -- Does `an_element' type check as a type FEATURE_CLAUSE? + note + rule: "[ + In an environment where all enclosed features specifications are OK, + and all classes mentioned in the selective export exist, + `an_element' is OK. + ]" + do + if first_phase then + + Result := True + + from an_element.feature_specifications.start until an_element.feature_specifications.after + loop + Result := check_feature_specification (an_element.feature_specifications.item_for_iteration, an_element.selective_export, enclosing_class) and Result + + an_element.feature_specifications.forth + end + + elseif second_phase then + + Result := True + + from an_element.feature_specifications.start until an_element.feature_specifications.after + loop + Result := check_feature_specification (an_element.feature_specifications.item_for_iteration, an_element.selective_export, enclosing_class) and Result + + an_element.feature_specifications.forth + end + + Result := check_selective_export (an_element.selective_export, enclosing_class) + + end + end + + check_feature_specification (an_element: FEATURE_SPECIFICATION; selective_export: CLASS_NAME_LIST; enclosing_class: TBON_TC_CLASS_TYPE): BOOLEAN + -- Does `an_element' type check as a type FEATURE_SPECIFICATION? + note + rule: "[ + In an environment where the name of `an_element' is unique in its enclosing class, + and the type of the feature is in the environment, + and all feature arguments are OK, + and if renamed is renamed consistently, + + ]" + do + if then + + end + end + check_formal_generics (an_element: FORMAL_GENERIC_LIST; enclosing_class: TBON_TC_CLASS_TYPE): BOOLEAN -- Does `an_element' type check as a type FORMAL_GENERIC_LIST? note @@ -1066,6 +1139,46 @@ feature -- Type checking, static diagrams check false end end + check_selective_export (an_element: CLASS_NAME_LIST; enclosing_class: TBON_TC_CLASS_TYPE): BOOLEAN + -- Does `an_element' type check as a type CLASS_NAME_LIST? + note + rule: "[ + In an environment where all the classes mentioned in the selective export exist, + or are otherwise allowable (e.g. NONE), + `an_element' is OK. + ]" + do + if first_phase then + + Result := True + -- Nothing to be checked for first phase + + elseif second_phase then + + from an_element.start until an_element.after + loop + if not class_name_exists_in_context (an_element.item_for_iteration, formal_type_context) and + not (an_element.item_for_iteration ~ none_type_name) then + add_error (err_code_selective_export_class_does_not_exist, err_selective_export_class_does_not_exist (an_element.item_for_iteration, enclosing_class.name)) + end + an_element.forth + end + + else + Result := False + end + ensure + all_selective_export_classes_exist: + an_element.for_all ( + agent (export_class_name: STRING): BOOLEAN + do + Result := class_name_exists_in_context (export_class_name, formal_type_context) xor + export_class_name ~ none_type_name + end + ) + -- For all classes it holds that it exists in the environment or is of type NONE. + end + check_static_block (some_elements: STATIC_COMPONENTS): BOOLEAN -- Does each element in `some_elements' type check as their respective types? note diff --git a/src/type_checker/types/tbon_tc_class_type.e b/src/type_checker/types/tbon_tc_class_type.e index 8fcf2b1..f77580a 100644 --- a/src/type_checker/types/tbon_tc_class_type.e +++ b/src/type_checker/types/tbon_tc_class_type.e @@ -10,7 +10,8 @@ class inherit TBON_TC_TYPE redefine - is_model_equal + is_model_equal, + is_equal end create @@ -140,4 +141,21 @@ feature -- Status report Result := name ~ other.name end + is_equal (other: like Current): BOOLEAN + -- Is this model mathematically equal to `other'? + do + Result := name ~ other.name and (generics /= Void implies generics.for_all ( + agent (generic: TBON_TC_GENERIC; l_other_class: TBON_TC_CLASS_TYPE): BOOLEAN + do + Result := l_other_class.generics.exists (agent (this_generic, other_generic: TBON_TC_GENERIC): BOOLEAN + do + Result := this_generic |=| (other_generic) + end (generic, ?) + ) + end (?, other) + )) + -- If other class is a class type and has generics, both names and generics must be equal + -- For all generics of Current, there must exist an equal generic in other. + end + end diff --git a/src/type_checker/types/tbon_tc_feature.e b/src/type_checker/types/tbon_tc_feature.e index 1a1b02d..5e2b3d7 100644 --- a/src/type_checker/types/tbon_tc_feature.e +++ b/src/type_checker/types/tbon_tc_feature.e @@ -7,19 +7,35 @@ note class TBON_TC_FEATURE +inherit + MML_MODEL + redefine + is_model_equal + end + create make feature -- Initialization - make (a_name: STRING; a_type: TBON_TC_CLASS_TYPE) + make (a_name: STRING; a_type: TBON_TC_CLASS_TYPE; an_enclosing_class: TBON_TC_CLASS_TYPE) + require + a_name /= Void and then not a_name.is_empty + a_type /= Void + an_enclosing_class /= Void do name := a_name.string type := a_type + enclosing_class := an_enclosing_class + + create {LINKED_LIST[STRING]} selective_export.make + create {LINKED_LIST[TBON_TC_FEATURE_ARGUMENT]} arguments.make end feature -- Access name: STRING + enclosing_class: TBON_TC_CLASS_TYPE + selective_export: LIST[STRING] type: TBON_TC_CLASS_TYPE @@ -35,6 +51,12 @@ feature -- Status report is_redefined: BOOLEAN + is_model_equal alias "|=|" (other: TBON_TC_FEATURE): BOOLEAN + -- Is this model mathematically equal to `other'? + do + Result := name ~ other.name and enclosing_class |=| other.enclosing_class + end + feature -- Element change set_is_deferred do @@ -70,6 +92,13 @@ feature -- Element change is_renamed: is_renamed end + set_type (a_type: TBON_TC_CLASS_TYPE) + require + a_type /= Void + do + type := a_type + end + feature -- Renaming is_renamed: BOOLEAN diff --git a/src/type_checker/types/tbon_tc_generic.e b/src/type_checker/types/tbon_tc_generic.e index 6757e37..16b9bfa 100644 --- a/src/type_checker/types/tbon_tc_generic.e +++ b/src/type_checker/types/tbon_tc_generic.e @@ -7,14 +7,22 @@ note class TBON_TC_GENERIC +inherit + MML_MODEL + redefine + is_model_equal + end + create make feature -- Initialization make (a_formal_generic_name: STRING; an_actual_generic_type: like actual_generic_type) -- Initialize `Current'. + require + a_formal_generic_name /= Void do - formal_generic_name := a_formal_generic_name + formal_generic_name := a_formal_generic_name.string actual_generic_type := an_actual_generic_type end @@ -32,6 +40,12 @@ feature -- Status report Result := actual_generic_type /= Void end + is_model_equal alias "|=|" (other: TBON_TC_GENERIC): BOOLEAN + -- Is this model mathematically equal to `other'? + do + Result := formal_generic_name ~ other.formal_generic_name and actual_generic_type ~ other.actual_generic_type + end + invariant formal_generic_name /= Void