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