Class: AdLint::Cc1::FunctionTable

Inherits:
Object
  • Object
show all
Defined in:
lib/adlint/cc1/object.rb

Instance Method Summary collapse

Constructor Details

#initialize(mem_pool) ⇒ FunctionTable

Returns a new instance of FunctionTable.



1093
1094
1095
1096
1097
# File 'lib/adlint/cc1/object.rb', line 1093

def initialize(mem_pool)
  @memory_pool = mem_pool
  @functions   = [{}]
  @scope_stack = [GlobalScope.new]
end

Instance Method Details

#declare_explicitly(dcl) ⇒ Object



1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/adlint/cc1/object.rb', line 1111

def declare_explicitly(dcl)
  if fun = lookup(dcl.identifier.value) and fun.explicit?
    fun.declarations_and_definitions.push(dcl)
    return fun
  end

  define(ExplicitFunction.new(dcl))
end

#declare_implicitly(fun) ⇒ Object



1120
1121
1122
1123
1124
1125
# File 'lib/adlint/cc1/object.rb', line 1120

def declare_implicitly(fun)
  if fun.named? && fun.implicit?
    define(fun, true)
  end
  fun
end

#define(fun, in_global_scope = false) ⇒ Object



1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
# File 'lib/adlint/cc1/object.rb', line 1127

def define(fun, in_global_scope = false)
  # NOTE: A function has a starting address in the TEXT segment.
  #       This is ad-hoc implementation, but it's enough for analysis.
  fun.bind_to(@memory_pool.allocate_static(0))

  if in_global_scope
    @functions.first[fun.name] = fun
  else
    @functions.last[fun.name] = fun if fun.named?
  end

  fun
end

#designatorsObject



1150
1151
1152
# File 'lib/adlint/cc1/object.rb', line 1150

def designators
  @functions.map { |hash| hash.keys }.flatten.to_set
end

#enter_scopeObject



1099
1100
1101
1102
# File 'lib/adlint/cc1/object.rb', line 1099

def enter_scope
  @functions.push({})
  @scope_stack.push(Scope.new(@scope_stack.size))
end

#leave_scopeObject



1104
1105
1106
1107
1108
1109
# File 'lib/adlint/cc1/object.rb', line 1104

def leave_scope
  @functions.pop.each_value do |fun|
    @memory_pool.free(fun.binding.memory)
  end
  @scope_stack.pop
end

#lookup(name_str) ⇒ Object



1141
1142
1143
1144
1145
1146
1147
1148
# File 'lib/adlint/cc1/object.rb', line 1141

def lookup(name_str)
  @functions.reverse_each do |hash|
    if fun = hash[name_str]
      return fun
    end
  end
  nil
end