Class: AdLint::Cc1::StatementInterpreter

Inherits:
SubInterpreter show all
Includes:
BranchGroupOptions, BranchOptions, SyntaxNodeCollector
Defined in:
lib/adlint/cc1/interp.rb

Constant Summary

Constants included from BranchGroupOptions

BranchGroupOptions::COMPLETE, BranchGroupOptions::ITERATION

Constants included from BranchOptions

BranchOptions::COMPLEMENTAL, BranchOptions::FINAL, BranchOptions::FIRST, BranchOptions::IMPLICIT_COND, BranchOptions::NARROWING, BranchOptions::SMOTHER_BREAK, BranchOptions::WIDENING

Constants included from InterpreterOptions

InterpreterOptions::QUIET, InterpreterOptions::WITHOUT_SIDE_EFFECTS

Instance Attribute Summary

Attributes inherited from SubInterpreter

#target_node_class

Instance Method Summary collapse

Methods included from SyntaxNodeCollector

collect_additive_expressions, collect_array_declarators, collect_compound_assignment_expressions, collect_constant_specifiers, collect_equality_expressions, collect_function_declarators, collect_generic_labeled_statements, collect_goto_statements, collect_identifier_declarators, collect_if_else_statements, collect_if_statements, collect_logical_and_expressions, collect_logical_or_expressions, collect_object_specifiers, collect_postfix_decrement_expressions, collect_postfix_increment_expressions, collect_prefix_decrement_expressions, collect_prefix_increment_expressions, collect_relational_expressions, collect_simple_assignment_expressions, collect_typedef_type_specifiers

Methods included from LogUtil

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Methods included from MonitorUtil

#checkpoint, #monitored_region

Methods included from Conversion

#do_conversion, #do_default_argument_promotion, #do_integer_promotion, #do_usual_arithmetic_conversion, #untyped_pointer_conversion?

Methods included from InterpreterMediator

#constant_expression?, #current_branch, #interpret, #object_to_pointer, #object_to_variable, #pointer_value_of, #reset_environment, #scalar_value_of, #scalar_value_of_arbitrary, #scalar_value_of_false, #scalar_value_of_null, #scalar_value_of_true, #value_of

Methods included from InterpSyntaxBridge

#_interp_syntax_bridge_

Methods included from InterpObjectBridge

#_interp_object_bridge_

Methods included from ArithmeticAccessor

#arithmetic, #logical_right_shift?

Methods included from FunctionTableMediator

#declare_explicit_function, #declare_implicit_function, #define_anonymous_function, #define_explicit_function

Methods included from VariableTableMediator

#create_tmpvar, #local_variables

Methods included from MemoryPoolMediator

#pointee_of

Methods inherited from SyntaxTreeVisitor

#visit_abbreviated_function_declarator, #visit_additive_expression, #visit_address_expression, #visit_alignof_expression, #visit_alignof_type_expression, #visit_and_expression, #visit_ansi_function_declarator, #visit_ansi_function_definition, #visit_array_abstract_declarator, #visit_array_declarator, #visit_array_subscript_expression, #visit_bit_access_by_pointer_expression, #visit_bit_access_by_value_expression, #visit_cast_expression, #visit_comma_separated_expression, #visit_compound_assignment_expression, #visit_compound_literal_expression, #visit_conditional_expression, #visit_constant_specifier, #visit_declaration, #visit_declaration_specifiers, #visit_enum_specifier, #visit_enum_type_declaration, #visit_enumerator, #visit_equality_expression, #visit_error_expression, #visit_error_statement, #visit_exclusive_or_expression, #visit_function_abstract_declarator, #visit_function_call_expression, #visit_function_declaration, #visit_grouped_abstract_declarator, #visit_grouped_declarator, #visit_grouped_expression, #visit_identifier_declarator, #visit_inclusive_or_expression, #visit_indirection_expression, #visit_init_declarator, #visit_initializer, #visit_kandr_function_declarator, #visit_kandr_function_definition, #visit_logical_and_expression, #visit_logical_or_expression, #visit_member_access_by_pointer_expression, #visit_member_access_by_value_expression, #visit_member_declaration, #visit_multiplicative_expression, #visit_null_constant_specifier, #visit_object_specifier, #visit_parameter_declaration, #visit_parameter_definition, #visit_parameter_type_list, #visit_pointer_abstract_declarator, #visit_postfix_decrement_expression, #visit_postfix_increment_expression, #visit_prefix_decrement_expression, #visit_prefix_increment_expression, #visit_relational_expression, #visit_shift_expression, #visit_simple_assignment_expression, #visit_sizeof_expression, #visit_sizeof_type_expression, #visit_specifier_qualifier_list, #visit_standard_type_specifier, #visit_string_literal_specifier, #visit_struct_declaration, #visit_struct_declarator, #visit_struct_specifier, #visit_struct_type_declaration, #visit_switch_statement, #visit_translation_unit, #visit_type_name, #visit_typedef_declaration, #visit_typedef_type_specifier, #visit_typeof_type_specifier, #visit_unary_arithmetic_expression, #visit_union_specifier, #visit_union_type_declaration, #visit_variable_declaration, #visit_variable_definition

Constructor Details

#initialize(owner) ⇒ StatementInterpreter

Returns a new instance of StatementInterpreter.



971
972
973
974
975
976
977
# File 'lib/adlint/cc1/interp.rb', line 971

def initialize(owner)
  super(owner, Statement)

  # NOTE: All effective controlling expressions in the executing
  #       iteration-statements.
  @effective_ctrlexpr_stack = []
end

Instance Method Details

#visit_break_statement(node) ⇒ Object



1238
1239
1240
1241
1242
1243
# File 'lib/adlint/cc1/interp.rb', line 1238

def visit_break_statement(node)
  checkpoint(node.location)

  node.executed = true
  BreakEvent.of_break.throw
end

#visit_c99_for_statement(node) ⇒ Object



1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'lib/adlint/cc1/interp.rb', line 1197

def visit_c99_for_statement(node)
  checkpoint(node.location)

  scoped_eval do
    interpret(node.declaration)

    node.executed = true
    notify_c99_for_stmt_started(node)

    if ctrlexpr_val = interpret_for_ctrlexpr(node)
      notify_c99_for_ctrlexpr_evaled(node, ctrlexpr_val)
    else
      ctrlexpr_val = scalar_value_of_true
    end

    case
    when ctrlexpr_val.test_must_be_true.true?
      interpret_for_body_statement(node, true)
    when ctrlexpr_val.test_may_be_true.true?
      interpret_for_body_statement(node, false)
    end
  end
ensure
  notify_c99_for_stmt_ended(node)
end

#visit_case_labeled_statement(node) ⇒ Object



989
990
991
992
993
994
995
996
997
998
# File 'lib/adlint/cc1/interp.rb', line 989

def visit_case_labeled_statement(node)
  checkpoint(node.location)

  node.executed = true
  ctrlexpr = node.expression
  ctrlexpr_var = object_to_variable(interpret(ctrlexpr, QUIET), ctrlexpr)
  notify_case_ctrlexpr_evaled(node, ctrlexpr_var)

  interpret(node.statement)
end

#visit_compound_statement(node) ⇒ Object



1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/adlint/cc1/interp.rb', line 1007

def visit_compound_statement(node)
  checkpoint(node.location)

  node.executed = true
  scoped_eval do
    begin
      notify_block_started(node)
      node.block_items.each { |item| interpret(item) }
    ensure
      notify_block_ended(node)
    end
  end
end

#visit_continue_statement(node) ⇒ Object



1231
1232
1233
1234
1235
1236
# File 'lib/adlint/cc1/interp.rb', line 1231

def visit_continue_statement(node)
  checkpoint(node.location)

  node.executed = true
  BreakEvent.of_continue.throw
end

#visit_default_labeled_statement(node) ⇒ Object



1000
1001
1002
1003
1004
1005
# File 'lib/adlint/cc1/interp.rb', line 1000

def visit_default_labeled_statement(node)
  checkpoint(node.location)

  node.executed = true
  interpret(node.statement)
end

#visit_do_statement(node) ⇒ Object



1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
# File 'lib/adlint/cc1/interp.rb', line 1145

def visit_do_statement(node)
  checkpoint(node.location)

  node.executed = true
  notify_do_stmt_started(node)

  widen_varying_variable_value_domain(node)

  orig_ctrlexpr, * = node.deduct_controlling_expression

  begin
    enter_iteration_statement(orig_ctrlexpr)
    branch_opts = [ITERATION, NARROWING, FINAL, IMPLICIT_COND, COMPLETE]
    branched_eval(nil, *branch_opts) { interpret(node.statement) }
  ensure
    leave_iteration_statement(orig_ctrlexpr)
  end

  ctrlexpr_obj = interpret(node.expression)
  ctrlexpr_var = object_to_variable(ctrlexpr_obj, node.expression)
  ctrlexpr_val = value_of(ctrlexpr_var)
  notify_variable_value_referred(node.expression, ctrlexpr_var)
  notify_sequence_point_reached(SequencePoint.new(node.expression))
  notify_do_ctrlexpr_evaled(node, ctrlexpr_val)
ensure
  notify_do_stmt_ended(node)
end

#visit_expression_statement(node) ⇒ Object



1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'lib/adlint/cc1/interp.rb', line 1021

def visit_expression_statement(node)
  checkpoint(node.location)

  node.executed = true
  notify_expression_stmt_started(node)

  interpret(node.expression) if node.expression
ensure
  notify_expression_stmt_ended(node)
end

#visit_for_statement(node) ⇒ Object



1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
# File 'lib/adlint/cc1/interp.rb', line 1173

def visit_for_statement(node)
  checkpoint(node.location)

  node.executed = true
  notify_for_stmt_started(node)

  node.initial_statement.accept(self)

  if ctrlexpr_val = interpret_for_ctrlexpr(node)
    notify_for_ctrlexpr_evaled(node, ctrlexpr_val)
  else
    ctrlexpr_val = scalar_value_of_true
  end

  case
  when ctrlexpr_val.test_must_be_true.true?
    interpret_for_body_statement(node, true)
  when ctrlexpr_val.test_may_be_true.true?
    interpret_for_body_statement(node, false)
  end
ensure
  notify_for_stmt_ended(node)
end

#visit_generic_labeled_statement(node) ⇒ Object



979
980
981
982
983
984
985
986
987
# File 'lib/adlint/cc1/interp.rb', line 979

def visit_generic_labeled_statement(node)
  checkpoint(node.location)

  node.executed = true
  notify_label_defined(node)

  uninitialize_block_local_variables(node)
  interpret(node.statement)
end

#visit_goto_statement(node) ⇒ Object



1223
1224
1225
1226
1227
1228
1229
# File 'lib/adlint/cc1/interp.rb', line 1223

def visit_goto_statement(node)
  checkpoint(node.location)

  # TODO: Must implement goto semantics.
  node.executed = true
  notify_goto_stmt_evaled(node, node.identifier.value)
end

#visit_if_else_statement(node) ⇒ Object



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'lib/adlint/cc1/interp.rb', line 1066

def visit_if_else_statement(node)
  checkpoint(node.location)

  node.executed = true

  orig_ctrlexpr = node.expression
  if orig_ctrlexpr == effective_ctrlexpr
    ctrlexpr_val = scalar_value_of_arbitrary
    ctrlexpr = nil
  else
    ctrlexpr_obj = interpret(orig_ctrlexpr)
    ctrlexpr_var = object_to_variable(ctrlexpr_obj, orig_ctrlexpr)
    ctrlexpr_val = value_of(ctrlexpr_var)
    notify_variable_value_referred(orig_ctrlexpr, ctrlexpr_var)
    notify_sequence_point_reached(SequencePoint.new(orig_ctrlexpr))
    ctrlexpr = orig_ctrlexpr.to_normalized_logical
  end
  notify_if_else_ctrlexpr_evaled(node, ctrlexpr_val)

  case
  when ctrlexpr_val.test_must_be_true.true?
    branched_eval(ctrlexpr, NARROWING, FINAL, IMPLICIT_COND, COMPLETE) do
      interpret(node.then_statement)
    end
    return
  when ctrlexpr_val.test_may_be_true.true?
    branched_eval(ctrlexpr, NARROWING, IMPLICIT_COND) do
      interpret(node.then_statement)
    end
  end

  case node.else_statement
  when IfStatement, IfElseStatement
    interpret(node.else_statement)
  else
    branched_eval(nil, NARROWING, COMPLEMENTAL, FINAL, COMPLETE) do
      interpret(node.else_statement)
    end
  end
end

#visit_if_statement(node) ⇒ Object



1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# File 'lib/adlint/cc1/interp.rb', line 1032

def visit_if_statement(node)
  checkpoint(node.location)

  node.executed = true

  orig_ctrlexpr = node.expression
  if orig_ctrlexpr == effective_ctrlexpr
    ctrlexpr_val = scalar_value_of_arbitrary
    ctrlexpr = nil
  else
    ctrlexpr_obj = interpret(orig_ctrlexpr)
    ctrlexpr_var = object_to_variable(ctrlexpr_obj, orig_ctrlexpr)
    ctrlexpr_val = value_of(ctrlexpr_var)
    notify_variable_value_referred(orig_ctrlexpr, ctrlexpr_var)
    notify_sequence_point_reached(SequencePoint.new(orig_ctrlexpr))
    ctrlexpr = orig_ctrlexpr.to_normalized_logical
  end
  notify_if_ctrlexpr_evaled(node, ctrlexpr_val)

  case
  when ctrlexpr_val.test_must_be_true.true?
    branched_eval(ctrlexpr, NARROWING, FINAL, IMPLICIT_COND, COMPLETE) do
      interpret(node.statement)
    end
  when ctrlexpr_val.test_may_be_true.true?
    branched_eval(ctrlexpr, NARROWING, FINAL, IMPLICIT_COND) do
      interpret(node.statement)
    end
  else
    # NOTE: To end the current branch group of else-if sequence.
    branched_eval(nil, NARROWING, FINAL) {}
  end
end

#visit_return_statement(node) ⇒ Object



1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
# File 'lib/adlint/cc1/interp.rb', line 1245

def visit_return_statement(node)
  checkpoint(node.location)

  node.executed = true

  unless node.expression
    notify_return_stmt_evaled(node, nil)
    BreakEvent.of_return.throw
  end

  var = object_to_variable(interpret(node.expression), node.expression)
  notify_variable_value_referred(node.expression, var)

  if active_fun = interpreter._active_function and
      ret_type = active_fun.type.return_type
    if var.type.same_as?(ret_type)
      conved = var
    else
      conved = do_conversion(var, ret_type) || create_tmpvar(ret_type)
      notify_implicit_conv_performed(node.expression, var, conved)
    end
  else
    conved = var
  end

  notify_sequence_point_reached(SequencePoint.new(node))
  notify_return_stmt_evaled(node, var)
  BreakEvent.of_return.throw
end

#visit_while_statement(node) ⇒ Object



1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
# File 'lib/adlint/cc1/interp.rb', line 1107

def visit_while_statement(node)
  checkpoint(node.location)

  node.executed = true
  notify_while_stmt_started(node)

  ctrlexpr_obj = interpret_iteration_ctrlexpr(node, node.expression)
  ctrlexpr_var = object_to_variable(ctrlexpr_obj, node.expression)
  ctrlexpr_val = value_of(ctrlexpr_var)

  notify_variable_value_referred(node.expression, ctrlexpr_var)
  notify_sequence_point_reached(SequencePoint.new(node.expression))
  notify_while_ctrlexpr_evaled(node, ctrlexpr_val)

  orig_ctrlexpr, ctrlexpr = node.deduct_controlling_expression

  case
  when ctrlexpr_val.test_must_be_true.true?
    begin
      enter_iteration_statement(orig_ctrlexpr)
      branch_opts = [ITERATION, NARROWING, FINAL, IMPLICIT_COND, COMPLETE]
      branched_eval(ctrlexpr, *branch_opts) { interpret(node.statement) }
    ensure
      leave_iteration_statement(orig_ctrlexpr)
    end
  when ctrlexpr_val.test_may_be_true.true?
    begin
      enter_iteration_statement(orig_ctrlexpr)
      branch_opts = [ITERATION, NARROWING, FINAL, IMPLICIT_COND]
      branched_eval(ctrlexpr, *branch_opts) { interpret(node.statement) }
    ensure
      leave_iteration_statement(orig_ctrlexpr)
    end
  end
ensure
  notify_while_stmt_ended(node)
end