~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Projects/MonoDevelop.Projects.Dom/DomCecilCompilationUnit.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// DomCecilCompilationUnit.cs
 
3
//
 
4
// Author:
 
5
//   Mike Krüger <mkrueger@novell.com>
 
6
//
 
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Collections.Generic;
 
31
using MonoDevelop.Projects.Dom;
 
32
using Mono.Cecil;
 
33
 
 
34
namespace MonoDevelop.Projects.Dom
 
35
{
 
36
        public class DomCecilCompilationUnit : CompilationUnit
 
37
        {
 
38
                AssemblyDefinition assemblyDefinition;
 
39
                
 
40
                public AssemblyDefinition AssemblyDefinition {
 
41
                        get {
 
42
                                return assemblyDefinition;
 
43
                        }
 
44
                }
 
45
                
 
46
                public DomCecilCompilationUnit (AssemblyDefinition assemblyDefinition) : this (true, true, assemblyDefinition)
 
47
                {
 
48
                }
 
49
                
 
50
                public DomCecilCompilationUnit (bool keepDefinitions, bool loadInternals, AssemblyDefinition assemblyDefinition) : base (assemblyDefinition.Name.FullName)
 
51
                {
 
52
                        if (keepDefinitions)
 
53
                                this.assemblyDefinition = assemblyDefinition;
 
54
                        foreach (ModuleDefinition moduleDefinition in assemblyDefinition.Modules) {
 
55
                                AddModuleDefinition (keepDefinitions, loadInternals, moduleDefinition);
 
56
                        }
 
57
                        
 
58
                }
 
59
                
 
60
                public void CleanCecilDefinitions ()
 
61
                {
 
62
                        assemblyDefinition = null;
 
63
                        foreach (IType type in Types) {
 
64
                                DomCecilType cecilType = type as DomCecilType;
 
65
                                if (cecilType != null) 
 
66
                                        cecilType.CleanCecilDefinitions ();
 
67
                        }
 
68
                        System.GC.Collect ();
 
69
                }
 
70
                
 
71
                public static DomCecilCompilationUnit Load (string fileName)
 
72
                {
 
73
                        return Load (fileName, true);
 
74
                }
 
75
                public static DomCecilCompilationUnit Load (string fileName, bool keepDefinitions)
 
76
                {
 
77
                        return Load (fileName, true, true);
 
78
                }
 
79
                
 
80
                public static DomCecilCompilationUnit Load (string fileName, bool keepDefinitions, bool loadInternals)
 
81
                {
 
82
                        if (String.IsNullOrEmpty (fileName))
 
83
                                return null;
 
84
                        DomCecilCompilationUnit result = new DomCecilCompilationUnit (keepDefinitions, loadInternals, AssemblyFactory.GetAssembly (fileName));
 
85
                        result.fileName = fileName;
 
86
                        return result;
 
87
                }
 
88
                
 
89
                public static bool IsInternal (MonoDevelop.Projects.Dom.Modifiers mods)
 
90
                {
 
91
                        return (mods & MonoDevelop.Projects.Dom.Modifiers.Internal) == MonoDevelop.Projects.Dom.Modifiers.Internal ||
 
92
                               (mods & MonoDevelop.Projects.Dom.Modifiers.Private) == MonoDevelop.Projects.Dom.Modifiers.Private
 
93
/*                                      ||
 
94
                               (mods & MonoDevelop.Projects.Dom.Modifiers.ProtectedOrInternal) == MonoDevelop.Projects.Dom.Modifiers.ProtectedOrInternal ||
 
95
                               (mods & MonoDevelop.Projects.Dom.Modifiers.SpecialName) == MonoDevelop.Projects.Dom.Modifiers.SpecialName*/;
 
96
                }
 
97
                
 
98
                void AddModuleDefinition (bool keepDefinitions, bool loadInternal, ModuleDefinition moduleDefinition)
 
99
                {
 
100
                        InstantiatedParamResolver resolver = new InstantiatedParamResolver ();
 
101
                        foreach (TypeDefinition type in moduleDefinition.Types) {
 
102
                                // filter nested types, they're handled in DomCecilType.
 
103
                                if ((type.Attributes & TypeAttributes.NestedPublic) == TypeAttributes.NestedPublic ||
 
104
                                    (type.Attributes & TypeAttributes.NestedFamily) == TypeAttributes.NestedFamily || 
 
105
                                    (type.Attributes & TypeAttributes.NestedAssembly) == TypeAttributes.NestedAssembly ||
 
106
                                    (type.Attributes & TypeAttributes.NestedPrivate) == TypeAttributes.NestedPrivate || 
 
107
                                    (type.Attributes & TypeAttributes.NestedFamANDAssem) == TypeAttributes.NestedFamANDAssem)
 
108
                                        continue;
 
109
                                if (!loadInternal && IsInternal (DomCecilType.GetModifiers (type.Attributes)))
 
110
                                        continue;
 
111
//                              if (type.Name == "SimplePropertyDescriptor")
 
112
//                                      System.Console.WriteLine(type.Attributes + "/" + DomCecilType.GetModifiers (type.Attributes) + "/" + IsInternal (DomCecilType.GetModifiers (type.Attributes)));
 
113
                                DomCecilType loadType = new DomCecilType (keepDefinitions, loadInternal, type);
 
114
                                
 
115
                                Add ((IType)resolver.Visit (loadType, null));
 
116
                        }
 
117
                }
 
118
                
 
119
                class InstantiatedParamResolver: CopyDomVisitor<object>
 
120
                {
 
121
                        Dictionary<string, IType> argTypes;
 
122
                        
 
123
                        public override IDomVisitable Visit (IType type, object data)
 
124
                        {
 
125
                                if (type.TypeParameters.Count > 0) {
 
126
                                        var oldTypes = argTypes;
 
127
                                        if (oldTypes != null)
 
128
                                                argTypes = new Dictionary<string, IType> (oldTypes);
 
129
                                        else
 
130
                                                argTypes = new Dictionary<string, IType> ();
 
131
                                        
 
132
                                        foreach (TypeParameter p in type.TypeParameters)
 
133
                                                argTypes [p.Name] = type;
 
134
                                        
 
135
                                        IDomVisitable res = base.Visit (type, data);
 
136
                                        argTypes = oldTypes;
 
137
                                        return res;
 
138
                                } else
 
139
                                        return base.Visit (type, data);
 
140
                                
 
141
                        }
 
142
                        
 
143
                        public override IDomVisitable Visit (IReturnType type, object data)
 
144
                        {
 
145
                                if (argTypes != null) {
 
146
                                        IType res;
 
147
                                        if (argTypes.TryGetValue (type.FullName, out res)) {
 
148
                                                DomReturnType rt = new DomReturnType (res);
 
149
                                                rt.Parts.Add (new ReturnTypePart (type.FullName, null));
 
150
                                                return rt;
 
151
                                        }
 
152
                                }
 
153
                                return base.Visit (type, data);
 
154
                        }
 
155
                }
 
156
        }
 
157
}