using System;
using System.Runtime.CompilerServices;
-using System.Collections;
+using System.Collections.Generic;
using System.IO;
namespace Mono.CompilerServices.SymbolWriter
{
- public interface ISourceFile
- {
- SourceFileEntry Entry {
- get;
- }
- }
-
- public interface ICompileUnit
- {
- CompileUnitEntry Entry {
- get;
- }
- }
-
- public interface ISourceMethod
- {
- string Name {
- get;
- }
-
- int NamespaceID {
- get;
- }
-
- int Token {
- get;
- }
- }
-
public class MonoSymbolWriter
{
- ArrayList methods = null;
- ArrayList sources = null;
- ArrayList comp_units = null;
+ List<SourceMethodBuilder> methods;
+ List<SourceFileEntry> sources;
+ List<CompileUnitEntry> comp_units;
protected readonly MonoSymbolFile file;
- string filename = null;
+ string filename;
- LineNumberEntry [] current_method_lines;
- int current_method_lines_pos = 0;
-
- private SourceMethod current_method = null;
+ private SourceMethodBuilder current_method;
+ Stack<SourceMethodBuilder> current_method_stack = new Stack<SourceMethodBuilder> ();
public MonoSymbolWriter (string filename)
{
- this.methods = new ArrayList ();
- this.sources = new ArrayList ();
- this.comp_units = new ArrayList ();
+ this.methods = new List<SourceMethodBuilder> ();
+ this.sources = new List<SourceFileEntry> ();
+ this.comp_units = new List<CompileUnitEntry> ();
this.file = new MonoSymbolFile ();
this.filename = filename + ".mdb";
-
- this.current_method_lines = new LineNumberEntry [50];
}
public MonoSymbolFile SymbolFile {
current_method.AddScopeVariable (scope, index);
}
- public void MarkSequencePoint (int offset, int file, int line, int column)
- {
- if (current_method == null)
- throw new ArgumentNullException ();
-
- if (current_method_lines_pos == current_method_lines.Length) {
- LineNumberEntry [] tmp = current_method_lines;
- current_method_lines = new LineNumberEntry [current_method_lines.Length * 2];
- Array.Copy (tmp, current_method_lines, current_method_lines_pos);
- }
-
- current_method_lines [current_method_lines_pos++] = new LineNumberEntry (file, line, offset);
- }
-
- public void MarkSequencePoint (int offset, int file, int line, int column, bool is_hidden)
+ public void MarkSequencePoint (int offset, SourceFileEntry file, int line, int column,
+ bool is_hidden)
{
if (current_method == null)
return;
- if (current_method_lines_pos == current_method_lines.Length) {
- LineNumberEntry [] tmp = current_method_lines;
- current_method_lines = new LineNumberEntry [current_method_lines.Length * 2];
- Array.Copy (tmp, current_method_lines, current_method_lines_pos);
- }
-
- current_method_lines [current_method_lines_pos++] = new LineNumberEntry (
- file, line, offset, is_hidden);
+ current_method.MarkSequencePoint (offset, file, line, column, is_hidden);
}
- public void OpenMethod (ICompileUnit file, ISourceMethod method)
+ public SourceMethodBuilder OpenMethod (ICompileUnit file, int ns_id, IMethodDef method)
{
- SourceMethod source = new SourceMethod (file, method);
-
- current_method = source;
+ SourceMethodBuilder builder = new SourceMethodBuilder (file, ns_id, method);
+ current_method_stack.Push (current_method);
+ current_method = builder;
methods.Add (current_method);
- }
-
- public void SetRealMethodName (string name)
- {
- current_method.RealMethodName = name;
- }
-
- public void SetCompilerGenerated ()
- {
- current_method.SetCompilerGenerated ();
+ return builder;
}
public void CloseMethod ()
{
- if (current_method == null) {
- Console.WriteLine ("Some clown is calling CloseMethod() while " +
- "current_method == null: {0}", Environment.StackTrace);
- return;
- }
-
- current_method.SetLineNumbers (
- current_method_lines, current_method_lines_pos);
- current_method_lines_pos = 0;
-
- current_method = null;
+ current_method = (SourceMethodBuilder) current_method_stack.Pop ();
}
public SourceFileEntry DefineDocument (string url)
public void WriteSymbolFile (Guid guid)
{
- foreach (SourceMethod method in methods)
+ foreach (SourceMethodBuilder method in methods)
method.DefineMethod (file);
try {
file.CreateSymbolFile (guid, fs);
}
}
-
- protected class SourceMethod
- {
- LineNumberEntry [] lines;
- private ArrayList _locals;
- private ArrayList _blocks;
- private ArrayList _scope_vars;
- private Stack _block_stack;
- private string _real_name;
- private ISourceMethod _method;
- private ICompileUnit _comp_unit;
- private MethodEntry.Flags _method_flags;
-
- public SourceMethod (ICompileUnit comp_unit, ISourceMethod method)
- {
- this._comp_unit = comp_unit;
- this._method = method;
- }
-
- public void StartBlock (CodeBlockEntry.Type type, int start_offset)
- {
- if (_block_stack == null)
- _block_stack = new Stack ();
- if (_blocks == null)
- _blocks = new ArrayList ();
-
- int parent = CurrentBlock != null ? CurrentBlock.Index : -1;
-
- CodeBlockEntry block = new CodeBlockEntry (
- _blocks.Count + 1, parent, type, start_offset);
-
- _block_stack.Push (block);
- _blocks.Add (block);
- }
-
- public void EndBlock (int end_offset)
- {
- CodeBlockEntry block = (CodeBlockEntry) _block_stack.Pop ();
- block.Close (end_offset);
- }
-
- public CodeBlockEntry[] Blocks {
- get {
- if (_blocks == null)
- return new CodeBlockEntry [0];
-
- CodeBlockEntry[] retval = new CodeBlockEntry [_blocks.Count];
- _blocks.CopyTo (retval, 0);
- return retval;
- }
- }
-
- public CodeBlockEntry CurrentBlock {
- get {
- if ((_block_stack != null) && (_block_stack.Count > 0))
- return (CodeBlockEntry) _block_stack.Peek ();
- else
- return null;
- }
- }
-
- public LineNumberEntry[] Lines {
- get {
- return lines;
- }
- }
-
- public LocalVariableEntry[] Locals {
- get {
- if (_locals == null)
- return new LocalVariableEntry [0];
- else {
- LocalVariableEntry[] retval =
- new LocalVariableEntry [_locals.Count];
- _locals.CopyTo (retval, 0);
- return retval;
- }
- }
- }
-
- public void AddLocal (int index, string name)
- {
- if (_locals == null)
- _locals = new ArrayList ();
- int block_idx = CurrentBlock != null ? CurrentBlock.Index : 0;
- _locals.Add (new LocalVariableEntry (index, name, block_idx));
- }
-
- public ScopeVariable[] ScopeVariables {
- get {
- if (_scope_vars == null)
- return new ScopeVariable [0];
-
- ScopeVariable[] retval = new ScopeVariable [_scope_vars.Count];
- _scope_vars.CopyTo (retval);
- return retval;
- }
- }
-
- public void AddScopeVariable (int scope, int index)
- {
- if (_scope_vars == null)
- _scope_vars = new ArrayList ();
- _scope_vars.Add (
- new ScopeVariable (scope, index));
- }
-
- public string RealMethodName {
- get { return _real_name; }
- set { _real_name = value; }
- }
-
- public void SetCompilerGenerated ()
- {
- _method_flags |= MethodEntry.Flags.IsCompilerGenerated;
- }
-
- public ICompileUnit SourceFile {
- get { return _comp_unit; }
- }
-
- public ISourceMethod Method {
- get { return _method; }
- }
-
- internal void SetLineNumbers (LineNumberEntry [] lns, int count)
- {
- lines = new LineNumberEntry [count];
- Array.Copy (lns, lines, count);
- }
-
- public void DefineMethod (MonoSymbolFile file)
- {
- MethodEntry entry = new MethodEntry (
- file, _comp_unit.Entry, _method.Token, ScopeVariables,
- Locals, Lines, Blocks, RealMethodName, _method_flags,
- _method.NamespaceID);
-
- file.AddMethod (entry);
- }
- }
}
}