class AdLint::Cc1::TypeResolver

Attributes

type_table[R]

Public Class Methods

new(type_tbl) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 42
def initialize(type_tbl)
  @type_table = type_tbl
end

Public Instance Methods

resolve(ast) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 48
def resolve(ast)
  ast.accept(self)
  @type_table
end
visit_ansi_function_definition(node) click to toggle source
Calls superclass method
# File lib/adlint/cc1/resolver.rb, line 190
def visit_ansi_function_definition(node)
  checkpoint(node.location)
  super
  node.type = lookup_function_type(node)
end
visit_compound_statement(node) click to toggle source
Calls superclass method
# File lib/adlint/cc1/resolver.rb, line 221
def visit_compound_statement(node)
  @type_table.enter_scope
  super
  @type_table.leave_scope
end
visit_enum_type_declaration(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 93
def visit_enum_type_declaration(node)
  checkpoint(node.location)
  node.type = @type_table.install_enum_type(node)
  node.enumerators.each { |enum| enum.type = node.type }
end
visit_function_declaration(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 106
def visit_function_declaration(node)
  checkpoint(node.location)
  node.declaration_specifiers.accept(self) if node.declaration_specifiers
  node.init_declarator.accept(self)

  if dcl_specs = node.declaration_specifiers
    type_quals = dcl_specs.type_qualifiers
    type_specs = dcl_specs.type_specifiers
  else
    type_quals = []
    type_specs = []
  end

  node.type = lookup_variable_type(type_quals, type_specs,
                                   node.init_declarator.declarator)
end
visit_kandr_function_definition(node) click to toggle source
Calls superclass method
# File lib/adlint/cc1/resolver.rb, line 196
def visit_kandr_function_definition(node)
  checkpoint(node.location)
  super
  node.type = lookup_function_type(node)
end
visit_member_declaration(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 69
def visit_member_declaration(node)
  checkpoint(node.location)
  node.specifier_qualifier_list.accept(self)
  node.struct_declarator.accept(self) if node.struct_declarator

  type_quals = node.specifier_qualifier_list.type_qualifiers
  type_specs = node.specifier_qualifier_list.type_specifiers
  struct_dcr = node.struct_declarator

  type = lookup_variable_type(type_quals, type_specs,
                              struct_dcr ? struct_dcr.declarator : nil)
  type = @type_table.pointer_type(type) if type.function?

  if struct_dcr and struct_dcr.bitfield?
    if bit_width = compute_bitfield_width(struct_dcr)
      type = @type_table.bitfield_type(type, bit_width)
    else
      type = fallback_type
    end
  end

  node.type = type
end
visit_parameter_declaration(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 123
def visit_parameter_declaration(node)
  checkpoint(node.location)
  node.declaration_specifiers.accept(self) if node.declaration_specifiers
  node.declarator.accept(self) if node.declarator

  if dcl_specs = node.declaration_specifiers
    type_quals = dcl_specs.type_qualifiers
    type_specs = dcl_specs.type_specifiers
  else
    type_quals = []
    type_specs = []
  end

  type = lookup_variable_type(type_quals, type_specs, node.declarator)

  if type.function?
    node.type = @type_table.pointer_type(type)
  else
    node.type = type
  end
end
visit_parameter_definition(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 202
def visit_parameter_definition(node)
  checkpoint(node.location)
  node.declaration_specifiers.accept(self) if node.declaration_specifiers
  node.declarator.accept(self) if node.declarator

  type = @type_table.lookup_parameter_type(node, interpreter)
  type = @type_table.pointer_type(type) if type.function?
  node.type = type
end
visit_struct_declaration(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 65
def visit_struct_declaration(node)
  node.items.each { |item| item.accept(self) }
end
visit_struct_type_declaration(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 53
def visit_struct_type_declaration(node)
  checkpoint(node.location)
  node.struct_declarations.each { |dcl| dcl.accept(self) }
  node.type = @type_table.install_struct_type(node)
end
visit_type_name(node) click to toggle source
Calls superclass method
# File lib/adlint/cc1/resolver.rb, line 212
def visit_type_name(node)
  checkpoint(node.location)
  super
  type_quals = node.specifier_qualifier_list.type_qualifiers
  type_specs = node.specifier_qualifier_list.type_specifiers
  node.type = lookup_variable_type(type_quals, type_specs,
                                   node.abstract_declarator)
end
visit_typedef_declaration(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 99
def visit_typedef_declaration(node)
  checkpoint(node.location)
  node.declaration_specifiers.accept(self)
  node.init_declarator.accept(self)
  node.type = @type_table.install_user_type(node)
end
visit_union_type_declaration(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 59
def visit_union_type_declaration(node)
  checkpoint(node.location)
  node.struct_declarations.each { |dcl| dcl.accept(self) }
  node.type = @type_table.install_union_type(node)
end
visit_variable_declaration(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 145
def visit_variable_declaration(node)
  checkpoint(node.location)
  node.declaration_specifiers.accept(self) if node.declaration_specifiers
  node.declarator.accept(self)

  if dcl_specs = node.declaration_specifiers
    type_quals = dcl_specs.type_qualifiers
    type_specs = dcl_specs.type_specifiers
  else
    type_quals = []
    type_specs = []
  end

  type = lookup_variable_type(type_quals, type_specs, node.declarator)

  if type.function?
    node.type = @type_table.pointer_type(type)
  else
    node.type = type
  end
end
visit_variable_definition(node) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 167
def visit_variable_definition(node)
  checkpoint(node.location)
  node.declaration_specifiers.accept(self) if node.declaration_specifiers
  node.init_declarator.accept(self)

  if dcl_specs = node.declaration_specifiers
    type_quals = dcl_specs.type_qualifiers
    type_specs = dcl_specs.type_specifiers
  else
    type_quals = []
    type_specs = []
  end

  type = lookup_variable_type(type_quals, type_specs,
                              node.init_declarator.declarator)

  if type.function?
    node.type = @type_table.pointer_type(type)
  else
    node.type = type
  end
end

Private Instance Methods

compute_bitfield_width(struct_dcl) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 237
def compute_bitfield_width(struct_dcl)
  if expr = struct_dcl.expression
    if interpreter
      obj = interpreter.execute(expr)
    else
      obj = Interpreter.new(@type_table).execute(expr)
    end

    if obj.variable? && obj.value.scalar?
      return obj.value.unique_sample
    end
  end
  nil
end
lookup_function_type(fun_def) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 233
def lookup_function_type(fun_def)
  @type_table.lookup_function_type(fun_def) || fallback_type
end
lookup_variable_type(type_quals, type_specs, dcl) click to toggle source
# File lib/adlint/cc1/resolver.rb, line 228
def lookup_variable_type(type_quals, type_specs, dcl)
  @type_table.lookup_or_install_type(
    type_quals, type_specs, dcl, interpreter) || fallback_type
end