Class: AdLint::Cc1::ConditionalExpression

Inherits:
Expression show all
Defined in:
lib/adlint/cc1/syntax.rb

Instance Attribute Summary collapse

Attributes inherited from SyntaxNode

#head_token, #subsequent_sequence_point, #tail_token

Instance Method Summary collapse

Methods inherited from Expression

#constant?, #full=, #object_specifiers

Methods inherited from SyntaxNode

#head_location, #short_class_name, #tail_location

Methods included from LocationHolder

#analysis_target?

Methods included from Visitable

#accept

Constructor Details

#initialize(cond, then_expr, else_expr, question_mark) ⇒ ConditionalExpression



2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
# File 'lib/adlint/cc1/syntax.rb', line 2055

def initialize(cond, then_expr, else_expr, question_mark)
  super()
  @condition = cond
  @then_expression = then_expr
  @else_expression = else_expr
  @question_mark = question_mark

  # NOTE: The ISO C99 standard says;
  #
  # 6.5.15 Conditional operator
  #
  # Semantics
  #
  # 4 The first operand is evaluated; there is a sequence poit after its
  #   evaluation.  The second operand is evaluated only if the first
  #   compares unequal to 0; the third operand is evaluated only if the
  #   first compares equal to 0; thre result is the value of the second or
  #   third operand (whichever is evaluated), converted to the type
  #   described below.  If an attempt is made to modify the result of a
  #   conditional operator or to access it after the next sequence point,
  #   the behavior is undefined.
  @condition.append_sequence_point!

  # NOTE: Add extra sequence points in order not to warn about side-effects
  #       in both the 2nd and 3rd expressions because only one of the 2nd
  #       and 3rd expressions is actually executed.
  @then_expression.append_sequence_point!
  @else_expression.append_sequence_point!
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



2085
2086
2087
# File 'lib/adlint/cc1/syntax.rb', line 2085

def condition
  @condition
end

#else_expressionObject (readonly)

Returns the value of attribute else_expression.



2087
2088
2089
# File 'lib/adlint/cc1/syntax.rb', line 2087

def else_expression
  @else_expression
end

#then_expressionObject (readonly)

Returns the value of attribute then_expression.



2086
2087
2088
# File 'lib/adlint/cc1/syntax.rb', line 2086

def then_expression
  @then_expression
end

Instance Method Details

#arithmetic?Boolean



2103
2104
2105
# File 'lib/adlint/cc1/syntax.rb', line 2103

def arithmetic?
  @then_expression.arithmetic? || @else_expression.arithmetic?
end

#bitwise?Boolean



2107
2108
2109
# File 'lib/adlint/cc1/syntax.rb', line 2107

def bitwise?
  @then_expression.bitwise? || @else_expression.bitwise?
end

#have_side_effect?Boolean



2093
2094
2095
2096
2097
# File 'lib/adlint/cc1/syntax.rb', line 2093

def have_side_effect?
  @condition.have_side_effect? ||
    @then_expression.have_side_effect? ||
    @else_expression.have_side_effect?
end

#inspect(indent = 0) ⇒ Object



2129
2130
2131
2132
2133
2134
# File 'lib/adlint/cc1/syntax.rb', line 2129

def inspect(indent = 0)
  " " * indent + "#{short_class_name}\n" +
    @condition.inspect(indent + 1) + "\n" +
    @then_expression.inspect(indent + 1) + "\n" +
    @else_expression.inspect(indent + 1)
end

#locationObject



2089
2090
2091
# File 'lib/adlint/cc1/syntax.rb', line 2089

def location
  @question_mark.location
end

#logical?Boolean



2099
2100
2101
# File 'lib/adlint/cc1/syntax.rb', line 2099

def logical?
  @then_expression.logical? || @else_expression.logical?
end

#to_complemental_logicalObject



2120
2121
2122
# File 'lib/adlint/cc1/syntax.rb', line 2120

def to_complemental_logical
  self
end

#to_normalized_logical(parent_expr = nil) ⇒ Object



2111
2112
2113
2114
2115
2116
2117
2118
# File 'lib/adlint/cc1/syntax.rb', line 2111

def to_normalized_logical(parent_expr = nil)
  case parent_expr
  when nil, LogicalAndExpression, LogicalOrExpression
    create_normalized_logical_of(self)
  else
    self
  end
end

#to_sObject



2124
2125
2126
2127
# File 'lib/adlint/cc1/syntax.rb', line 2124

def to_s
  "#{@condition.to_s} ? " +
    "#{@then_expression.to_s} : #{@else_expression.to_s}"
end