~ubuntu-branches/ubuntu/lucid/docky/lucid-proposed

« back to all changes in this revision

Viewing changes to lib/gio-sharp/generator/Method.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-02-17 15:10:07 UTC
  • Revision ID: james.westby@ubuntu.com-20100217151007-msxpd0lsj300ndde
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// GtkSharp.Generation.Method.cs - The Method Generatable.
 
2
//
 
3
// Author: Mike Kestner <mkestner@speakeasy.net>
 
4
//
 
5
// Copyright (c) 2001-2003 Mike Kestner
 
6
// Copyright (c) 2003-2004 Novell, Inc.
 
7
//
 
8
// This program is free software; you can redistribute it and/or
 
9
// modify it under the terms of version 2 of the GNU General Public
 
10
// License as published by the Free Software Foundation.
 
11
//
 
12
// This program is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
// General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public
 
18
// License along with this program; if not, write to the
 
19
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
// Boston, MA 02111-1307, USA.
 
21
 
 
22
 
 
23
namespace GtkSharp.Generation {
 
24
 
 
25
        using System;
 
26
        using System.Collections;
 
27
        using System.IO;
 
28
        using System.Xml;
 
29
 
 
30
        public class Method : MethodBase  {
 
31
                
 
32
                private ReturnValue retval;
 
33
 
 
34
                private string call;
 
35
                private bool is_get, is_set;
 
36
                private bool deprecated = false;
 
37
 
 
38
                public Method (XmlElement elem, ClassBase container_type) : base (elem, container_type)
 
39
                {
 
40
                        this.retval = new ReturnValue (elem["return-type"]);
 
41
                        
 
42
                        if (!container_type.IsDeprecated && elem.HasAttribute ("deprecated")) {
 
43
                                string attr = elem.GetAttribute ("deprecated");
 
44
                                deprecated = attr == "1" || attr == "true";
 
45
                        }
 
46
                        
 
47
                        if (Name == "GetType")
 
48
                                Name = "GetGType";
 
49
                }
 
50
 
 
51
                public bool IsDeprecated {
 
52
                        get {
 
53
                                return deprecated;
 
54
                        }
 
55
                }
 
56
 
 
57
                public bool IsGetter {
 
58
                        get {
 
59
                                return is_get;
 
60
                        }
 
61
                }
 
62
 
 
63
                public bool IsSetter {
 
64
                        get {
 
65
                                return is_set;
 
66
                        }
 
67
                }
 
68
 
 
69
                public string ReturnType {
 
70
                        get {
 
71
                                return retval.CSType;
 
72
                        }
 
73
                }
 
74
 
 
75
                public override bool Validate ()
 
76
                {
 
77
                        if (!retval.Validate () || !base.Validate ()) {
 
78
                                Console.Write(" in method " + Name + " ");
 
79
                                return false;
 
80
                        }
 
81
 
 
82
                        Parameters parms = Parameters;
 
83
                        is_get = ((((parms.IsAccessor && retval.IsVoid) || (parms.Count == 0 && !retval.IsVoid)) || (parms.Count == 0 && !retval.IsVoid)) && HasGetterName);
 
84
                        is_set = ((parms.IsAccessor || (parms.VisibleCount == 1 && retval.IsVoid)) && HasSetterName);
 
85
 
 
86
                        call = "(" + (IsStatic ? "" : container_type.CallByName () + (parms.Count > 0 ? ", " : "")) + Body.GetCallString (is_set) + ")";
 
87
 
 
88
                        return true;
 
89
                }
 
90
                
 
91
                private Method GetComplement ()
 
92
                {
 
93
                        char complement;
 
94
                        if (is_get)
 
95
                                complement = 'S';
 
96
                        else
 
97
                                complement = 'G';
 
98
                        
 
99
                        return container_type.GetMethod (complement + BaseName.Substring (1));
 
100
                }
 
101
                
 
102
                public string Declaration {
 
103
                        get {
 
104
                                return retval.CSType + " " + Name + " (" + (Signature != null ? Signature.ToString() : "") + ");";
 
105
                        }
 
106
                }
 
107
 
 
108
                private void GenerateDeclCommon (StreamWriter sw, ClassBase implementor)
 
109
                {
 
110
                        if (IsStatic)
 
111
                                sw.Write("static ");
 
112
                        sw.Write (Safety);
 
113
                        Method dup = null;
 
114
                        if (container_type != null)
 
115
                                dup = container_type.GetMethodRecursively (Name);
 
116
                        if (implementor != null)
 
117
                                dup = implementor.GetMethodRecursively (Name);
 
118
 
 
119
                        if (Name == "ToString" && Parameters.Count == 0)
 
120
                                sw.Write("override ");
 
121
                        else if (Name == "GetGType" && container_type is ObjectGen)
 
122
                                sw.Write("new ");
 
123
                        else if (Modifiers == "new " || (dup != null && ((dup.Signature != null && Signature != null && dup.Signature.ToString() == Signature.ToString()) || (dup.Signature == null && Signature == null))))
 
124
                                sw.Write("new ");
 
125
 
 
126
                        if (is_get || is_set) {
 
127
                                if (retval.IsVoid)
 
128
                                        sw.Write (Parameters.AccessorReturnType);
 
129
                                else
 
130
                                        sw.Write(retval.CSType);
 
131
                                sw.Write(" ");
 
132
                                if (Name.StartsWith ("Get") || Name.StartsWith ("Set"))
 
133
                                        sw.Write (Name.Substring (3));
 
134
                                else {
 
135
                                        int dot = Name.LastIndexOf ('.');
 
136
                                        if (dot != -1 && (Name.Substring (dot + 1, 3) == "Get" || Name.Substring (dot + 1, 3) == "Set"))
 
137
                                                sw.Write (Name.Substring (0, dot + 1) + Name.Substring (dot + 4));
 
138
                                        else
 
139
                                                sw.Write (Name);
 
140
                                }
 
141
                                sw.WriteLine(" { ");
 
142
                        } else if (IsAccessor) {
 
143
                                sw.Write (Signature.AccessorType + " " + Name + "(" + Signature.AsAccessor + ")");
 
144
                        } else {
 
145
                                sw.Write(retval.CSType + " " + Name + "(" + (Signature != null ? Signature.ToString() : "") + ")");
 
146
                        }
 
147
                }
 
148
 
 
149
                public void GenerateDecl (StreamWriter sw)
 
150
                {
 
151
                        if (IsStatic)
 
152
                                return;
 
153
 
 
154
                        if (is_get || is_set)
 
155
                        {
 
156
                                Method comp = GetComplement ();
 
157
                                if (comp != null && is_set)
 
158
                                        return;
 
159
                        
 
160
                                sw.Write("\t\t");
 
161
                                GenerateDeclCommon (sw, null);
 
162
 
 
163
                                sw.Write("\t\t\t");
 
164
                                sw.Write ((is_get) ? "get;" : "set;");
 
165
 
 
166
                                if (comp != null && comp.is_set)
 
167
                                        sw.WriteLine (" set;");
 
168
                                else
 
169
                                        sw.WriteLine ();
 
170
 
 
171
                                sw.WriteLine ("\t\t}");
 
172
                        }
 
173
                        else
 
174
                        {
 
175
                                sw.Write("\t\t");
 
176
                                GenerateDeclCommon (sw, null);
 
177
                                sw.WriteLine (";");
 
178
                        }
 
179
 
 
180
                        Statistics.MethodCount++;
 
181
                }
 
182
 
 
183
                public void GenerateImport (StreamWriter sw)
 
184
                {
 
185
                        string import_sig = IsStatic ? "" : container_type.MarshalType + " raw";
 
186
                        import_sig += !IsStatic && Parameters.Count > 0 ? ", " : "";
 
187
                        import_sig += Parameters.ImportSignature.ToString();
 
188
                        sw.WriteLine("\t\t[DllImport(\"" + LibraryName + "\")]");
 
189
                        if (retval.MarshalType.StartsWith ("[return:"))
 
190
                                sw.WriteLine("\t\t" + retval.MarshalType + " static extern " + Safety + retval.CSType + " " + CName + "(" + import_sig + ");");
 
191
                        else
 
192
                                sw.WriteLine("\t\tstatic extern " + Safety + retval.MarshalType + " " + CName + "(" + import_sig + ");");
 
193
                        sw.WriteLine();
 
194
                }
 
195
 
 
196
                public void Generate (GenerationInfo gen_info, ClassBase implementor)
 
197
                {
 
198
                        if (!Validate ())
 
199
                                return;
 
200
 
 
201
                        Method comp = null;
 
202
 
 
203
                        gen_info.CurrentMember = Name;
 
204
 
 
205
                        /* we are generated by the get Method, if there is one */
 
206
                        if (is_set || is_get)
 
207
                        {
 
208
                                if (Modifiers != "new " && container_type.GetPropertyRecursively (Name.Substring (3)) != null)
 
209
                                        return;
 
210
                                comp = GetComplement ();
 
211
                                if (comp != null && is_set) {
 
212
                                        if (Parameters.AccessorReturnType == comp.ReturnType)
 
213
                                                return;
 
214
                                        else {
 
215
                                                is_set = false;
 
216
                                                call = "(Handle, " + Body.GetCallString (false) + ")";
 
217
                                                comp = null;
 
218
                                        }
 
219
                                }
 
220
                                /* some setters take more than one arg */
 
221
                                if (comp != null && !comp.is_set)
 
222
                                        comp = null;
 
223
                        }
 
224
                        
 
225
                        GenerateImport (gen_info.Writer);
 
226
                        if (comp != null && retval.CSType == comp.Parameters.AccessorReturnType)
 
227
                                comp.GenerateImport (gen_info.Writer);
 
228
 
 
229
                        if (IsDeprecated)
 
230
                                gen_info.Writer.WriteLine("\t\t[Obsolete]");
 
231
                        gen_info.Writer.Write("\t\t");
 
232
                        if (Protection != "")
 
233
                                gen_info.Writer.Write("{0} ", Protection);
 
234
                        GenerateDeclCommon (gen_info.Writer, implementor);
 
235
 
 
236
                        if (is_get || is_set)
 
237
                        {
 
238
                                gen_info.Writer.Write ("\t\t\t");
 
239
                                gen_info.Writer.Write ((is_get) ? "get" : "set");
 
240
                                GenerateBody (gen_info, implementor, "\t");
 
241
                        }
 
242
                        else
 
243
                                GenerateBody (gen_info, implementor, "");
 
244
                        
 
245
                        if (is_get || is_set)
 
246
                        {
 
247
                                if (comp != null && retval.CSType == comp.Parameters.AccessorReturnType)
 
248
                                {
 
249
                                        gen_info.Writer.WriteLine ();
 
250
                                        gen_info.Writer.Write ("\t\t\tset");
 
251
                                        comp.GenerateBody (gen_info, implementor, "\t");
 
252
                                }
 
253
                                gen_info.Writer.WriteLine ();
 
254
                                gen_info.Writer.WriteLine ("\t\t}");
 
255
                        }
 
256
                        else
 
257
                                gen_info.Writer.WriteLine();
 
258
                        
 
259
                        gen_info.Writer.WriteLine();
 
260
 
 
261
                        Statistics.MethodCount++;
 
262
                }
 
263
 
 
264
                public void GenerateBody (GenerationInfo gen_info, ClassBase implementor, string indent)
 
265
                {
 
266
                        StreamWriter sw = gen_info.Writer;
 
267
                        sw.WriteLine(" {");
 
268
                        if (!IsStatic && implementor != null)
 
269
                                implementor.Prepare (sw, indent + "\t\t\t");
 
270
                        if (IsAccessor)
 
271
                                Body.InitAccessor (sw, Signature, indent);
 
272
                        Body.Initialize(gen_info, is_get, is_set, indent);
 
273
 
 
274
                        sw.Write(indent + "\t\t\t");
 
275
                        if (retval.IsVoid)
 
276
                                sw.WriteLine(CName + call + ";");
 
277
                        else {
 
278
                                sw.WriteLine(retval.MarshalType + " raw_ret = " + CName + call + ";");
 
279
                                sw.WriteLine(indent + "\t\t\t" + retval.CSType + " ret = " + retval.FromNative ("raw_ret") + ";");
 
280
                        }
 
281
 
 
282
                        if (!IsStatic && implementor != null)
 
283
                                implementor.Finish (sw, indent + "\t\t\t");
 
284
                        Body.Finish (sw, indent);
 
285
                        Body.HandleException (sw, indent);
 
286
 
 
287
                        if (is_get && Parameters.Count > 0) 
 
288
                                sw.WriteLine (indent + "\t\t\treturn " + Parameters.AccessorName + ";");
 
289
                        else if (!retval.IsVoid)
 
290
                                sw.WriteLine (indent + "\t\t\treturn ret;");
 
291
                        else if (IsAccessor)
 
292
                                Body.FinishAccessor (sw, Signature, indent);
 
293
 
 
294
                        sw.Write(indent + "\t\t}");
 
295
                }
 
296
 
 
297
                bool IsAccessor { 
 
298
                        get { 
 
299
                                return retval.IsVoid && Signature.IsAccessor; 
 
300
                        } 
 
301
                }
 
302
        }
 
303
}
 
304