class RDoc::Context

def <=>(other)

def <=>(other)
  full_name <=> other.full_name
end

def add_alias(an_alias)

def add_alias(an_alias)
  meth = find_instance_method_named(an_alias.old_name)
  if meth then
    add_alias_impl an_alias, meth
  else
    add_to @aliases, an_alias
    unmatched_alias_list = @unmatched_alias_lists[an_alias.old_name] ||= []
    unmatched_alias_list.push an_alias
  end
  an_alias
end

def add_alias_impl(an_alias, meth)

def add_alias_impl(an_alias, meth)
  new_meth = RDoc::AnyMethod.new an_alias.text, an_alias.new_name
  new_meth.is_alias_for = meth
  new_meth.singleton    = meth.singleton
  new_meth.params       = meth.params
  new_meth.comment      = an_alias.comment
  meth.add_alias new_meth
  add_method new_meth
  # aliases don't use ongoing visibility
  new_meth.visibility = meth.visibility
  new_meth
end

def add_attribute(attribute)

def add_attribute(attribute)
  add_to @attributes, attribute
end

def add_class(class_type, name, superclass = 'Object')

def add_class(class_type, name, superclass = 'Object')
  klass = add_class_or_module @classes, class_type, name, superclass
  existing = klass.superclass
  existing = existing.name if existing and not String === existing
  if superclass != existing and superclass != 'Object' then
    klass.superclass = superclass
  end
  # If the parser encounters Container::Item before encountering
  # Container, then it assumes that Container is a module.  This may not
  # be the case, so remove Container from the module list if present and
  # transfer any contained classes and modules to the new class.
  mod = RDoc::TopLevel.modules_hash.delete klass.full_name
  if mod then
    klass.classes_hash.update mod.classes_hash
    klass.modules_hash.update mod.modules_hash
    klass.method_list.concat mod.method_list
    @modules.delete klass.name
  end
  RDoc::TopLevel.classes_hash[klass.full_name] = klass
  klass
end

def add_class_or_module(collection, class_type, name, superclass = nil)

def add_class_or_module(collection, class_type, name, superclass = nil)
  full_name = child_name name
  mod = collection[name]
  if mod then
    mod.superclass = superclass unless mod.module?
  else
    all = if class_type == RDoc::NormalModule then
            RDoc::TopLevel.modules_hash
          else
            RDoc::TopLevel.classes_hash
          end
    mod = all[full_name]
    unless mod then
      mod = class_type.new name, superclass
    else
      # If the class has been encountered already, check that its
      # superclass has been set (it may not have been, depending on the
      # context in which it was encountered).
      if class_type == RDoc::NormalClass then
        mod.superclass = superclass unless mod.superclass
      end
    end
    unless @done_documenting then
      all[full_name] = mod
      collection[name] = mod
    end
    mod.section = @current_section
    mod.parent = self
  end
  mod
end

def add_constant(constant)

def add_constant(constant)
  add_to @constants, constant
end

def add_include(include)

def add_include(include)
  add_to @includes, include
end

def add_method(method)

def add_method(method)
  method.visibility = @visibility
  add_to @method_list, method
  unmatched_alias_list = @unmatched_alias_lists[method.name]
  if unmatched_alias_list then
    unmatched_alias_list.each do |unmatched_alias|
      add_alias_impl unmatched_alias, method
      @aliases.delete unmatched_alias
    end
    @unmatched_alias_lists.delete method.name
  end
end

def add_module(class_type, name)

def add_module(class_type, name)
  return @classes[name] if @classes.key? name
  add_class_or_module @modules, class_type, name, nil
end

def add_module_alias from, name

def add_module_alias from, name
  to_name = child_name name
  unless @done_documenting then
    if from.module? then
      RDoc::TopLevel.modules_hash
    else
      RDoc::TopLevel.classes_hash
    end[to_name] = from
    if from.module? then
      @modules
    else
      @classes
    end[name] = from
  end
  from
end

def add_require(require)

def add_require(require)
  if RDoc::TopLevel === self then
    add_to @requires, require
  else
    parent.add_require require
  end
end

def add_to(array, thing)

def add_to(array, thing)
  array << thing if @document_self and not @done_documenting
  thing.parent = self
  thing.section = @current_section
end

def child_name name

def child_name name
  if RDoc::TopLevel === self then
    name
  else
    "#{self.full_name}::#{name}"
  end
end

def classes

def classes
  @classes.values
end

def classes_and_modules

def classes_and_modules
  classes + modules
end

def classes_hash

def classes_hash
  @classes
end

def defined_in?(file)

def defined_in?(file)
  @in_files.include?(file)
end

def each_attribute # :yields: attribute

:yields: attribute
def each_attribute # :yields: attribute
  @attributes.each { |a| yield a }
end

def each_classmodule(&block) # :yields: module

:yields: module
def each_classmodule(&block) # :yields: module
  classes_and_modules.sort.each(&block)
end

def each_constant # :yields: constant

:yields: constant
def each_constant # :yields: constant
  @constants.each {|c| yield c}
end

def each_include # :yields: include

:yields: include
def each_include # :yields: include
  @includes.each do |i| yield i end
end

def each_method # :yields: method

:yields: method
def each_method # :yields: method
  @method_list.sort.each {|m| yield m}
end

def find_attribute_named(name)

def find_attribute_named(name)
  @attributes.find { |m| m.name == name }
end

def find_class_method_named(name)

def find_class_method_named(name)
  @method_list.find { |meth| meth.singleton && meth.name == name }
end

def find_constant_named(name)

def find_constant_named(name)
  @constants.find {|m| m.name == name}
end

def find_enclosing_module_named(name)

def find_enclosing_module_named(name)
  parent && parent.find_module_named(name)
end

def find_file_named(name)

def find_file_named(name)
  top_level.class.find_file_named(name)
end

def find_instance_method_named(name)

def find_instance_method_named(name)
  @method_list.find { |meth| !meth.singleton && meth.name == name }
end

def find_local_symbol(symbol)

def find_local_symbol(symbol)
  find_method_named(symbol) or
  find_constant_named(symbol) or
  find_attribute_named(symbol) or
  find_module_named(symbol) or
  find_file_named(symbol)
end

def find_method_named(name)

def find_method_named(name)
  case name
  when /\A#/ then
    find_instance_method_named name[1..-1]
  when /\A::/ then
    find_class_method_named name[2..-1]
  else
    @method_list.find { |meth| meth.name == name }
  end
end

def find_module_named(name)

def find_module_named(name)
  res = @modules[name] || @classes[name]
  return res if res
  return self if self.name == name
  find_enclosing_module_named name
end

def find_symbol(symbol, method = nil)

def find_symbol(symbol, method = nil)
  result = nil
  case symbol
  when /^::([A-Z].*)/ then
    result = top_level.find_symbol($1)
  when /::/ then
    modules = symbol.split(/::/)
    unless modules.empty? then
      module_name = modules.shift
      result = find_module_named(module_name)
      if result then
        modules.each do |name|
          result = result.find_module_named name
          break unless result
        end
      end
    end
  end
  unless result then
    # if a method is specified, then we're definitely looking for
    # a module, otherwise it could be any symbol
    if method then
      result = find_module_named symbol
    else
      result = find_local_symbol symbol
      if result.nil? then
        if symbol =~ /^[A-Z]/ then
          result = parent
          while result && result.name != symbol do
            result = result.parent
          end
        end
      end
    end
  end
  result = result.find_local_symbol method if result and method
  result
end

def full_name

def full_name
  '(unknown)'
end

def http_url(prefix)

def http_url(prefix)
  path = full_name
  path = path.gsub(/<<\s*(\w*)/, 'from-\1') if path =~ /<</
  path = [prefix] + path.split('::')
  File.join(*path.compact) + '.html'
end

def initialize

def initialize
  super
  @in_files = []
  @name    ||= "unknown"
  @comment ||= ""
  @parent  = nil
  @visibility = :public
  @current_section = Section.new self, nil, nil
  @sections = [@current_section]
  initialize_methods_etc
  initialize_classes_and_modules
end

def initialize_classes_and_modules

def initialize_classes_and_modules
  @classes = {}
  @modules = {}
end

def initialize_methods_etc

def initialize_methods_etc
  @method_list = []
  @attributes  = []
  @aliases     = []
  @requires    = []
  @includes    = []
  @constants   = []
  # This Hash maps a method name to a list of unmatched aliases (aliases of
  # a method not yet encountered).
  @unmatched_alias_lists = {}
end

def methods_by_type

def methods_by_type
  methods = {}
  TYPES.each do |type|
    visibilities = {}
    VISIBILITIES.each do |vis|
      visibilities[vis] = []
    end
    methods[type] = visibilities
  end
  each_method do |method|
    methods[method.type][method.visibility] << method
  end
  methods
end

def methods_matching(methods, singleton = false)

def methods_matching(methods, singleton = false)
  count = 0
  @method_list.each do |m|
    if methods.include? m.name and m.singleton == singleton then
      yield m
      count += 1
    end
  end
  return if count == methods.size || singleton
  @attributes.each do |a|
    yield a if methods.include? a.name
  end
end

def modules

def modules
  @modules.values
end

def modules_hash

def modules_hash
  @modules
end

def ongoing_visibility=(visibility)

def ongoing_visibility=(visibility)
  @visibility = visibility
end

def record_location(top_level)

def record_location(top_level)
  @in_files << top_level unless @in_files.include?(top_level)
end

def remove_classes_and_modules

def remove_classes_and_modules
  initialize_classes_and_modules
end

def remove_methods_etc

def remove_methods_etc
  initialize_methods_etc
end

def set_current_section(title, comment)

def set_current_section(title, comment)
  @current_section = Section.new self, title, comment
  @sections << @current_section
end

def set_visibility_for(methods, visibility, singleton = false)

def set_visibility_for(methods, visibility, singleton = false)
  methods_matching methods, singleton do |m|
    m.visibility = visibility
  end
end

def top_level

def top_level
  return @top_level if defined? @top_level
  @top_level = self
  @top_level = @top_level.parent until RDoc::TopLevel === @top_level
  @top_level
end