using System.Collections;
using System.Text;
+#if NET_2_0
+ using System.Collections.Generic;
+#endif
+
internal class CSharpCodeGenerator
: CodeGenerator
{
+#if NET_2_0
+ IDictionary <string, string> providerOptions;
+#endif
+
// It is used for beautiful "for" syntax
bool dont_write_semicolon;
dont_write_semicolon = false;
}
+#if NET_2_0
+ public CSharpCodeGenerator (IDictionary <string, string> providerOptions)
+ {
+ this.providerOptions = providerOptions;
+ }
+
+ protected IDictionary <string, string> ProviderOptions {
+ get { return providerOptions; }
+ }
+#endif
+
//
// Properties
//
protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression expression)
{
- GenerateExpression (expression.TargetObject);
+ if (expression.TargetObject != null)
+ GenerateExpression (expression.TargetObject);
Output.Write ('(');
OutputExpressionList (expression.Parameters);
Output.Write (')');
{
Output.WriteLine ();
Output.WriteLine ("#line default");
+#if NET_2_0
+ Output.WriteLine ("#line hidden");
+#endif
}
protected override void GenerateEvent (CodeMemberEvent eventRef, CodeTypeDeclaration declaration)
GenerateGenericsConstraints (method.TypeParameters);
#endif
- if ((attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract || declaration.IsInterface)
+ if (IsAbstract (attributes) || declaration.IsInterface)
output.WriteLine (';');
else {
OutputStartBrace ();
}
}
+ static bool IsAbstract (MemberAttributes attributes)
+ {
+ return (attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract;
+ }
+
protected override void GenerateProperty (CodeMemberProperty property,
CodeTypeDeclaration declaration)
{
OutputParameters(property.Parameters);
output.Write(']');
} else {
- output.Write (property.Name);
+ output.Write (GetSafeName (property.Name));
}
OutputStartBrace ();
++Indent;
- if (declaration.IsInterface)
+ if (declaration.IsInterface || IsAbstract (property.Attributes))
{
if (property.HasGet) output.WriteLine("get;");
if (property.HasSet) output.WriteLine("set;");
private void OutputAttributes (CodeAttributeDeclarationCollection attributes, string prefix, bool inline)
{
+#if NET_2_0
+ bool params_set = false;
+#endif
+
foreach (CodeAttributeDeclaration att in attributes) {
+#if NET_2_0
+ if (att.Name == "System.ParamArrayAttribute") {
+ params_set = true;
+ continue;
+ }
+#endif
+
GenerateAttributeDeclarationsStart (attributes);
if (prefix != null) {
Output.Write (prefix);
Output.WriteLine ();
}
}
+
+#if NET_2_0
+ if (params_set) {
+ if (prefix != null)
+ Output.Write (prefix);
+ Output.Write ("params");
+ if (inline)
+ Output.Write (" ");
+ else
+ Output.WriteLine ();
+ }
+#endif
}
private void OutputAttributeDeclaration (CodeAttributeDeclaration attribute)
protected override string GetTypeOutput (CodeTypeReference type)
{
+#if NET_2_0
+ if ((type.Options & CodeTypeReferenceOptions.GenericTypeParameter) != 0)
+ return type.BaseType;
+#endif
+
string typeOutput = null;
if (type.ArrayElementType != null) {
}
typeOutput += ']';
}
+
return typeOutput;
}
// skip ` character
i++;
// determine number of type arguments to output
- int typeArgCount = baseType[i] - '0';
+ int end = i;
+ while (end < baseType.Length && Char.IsDigit (baseType [end]))
+ end++;
+ int typeArgCount = Int32.Parse (baseType.Substring (i, end - i));
// output type arguments
OutputTypeArguments (type.TypeArguments, sb, typeArgCount);
// skip type argument indicator
- i++;
+ i = end;
// if next character is . or +, then append .
if ((i < baseType.Length) && ((baseType[i] == '+') || (baseType[i] == '.'))) {
sb.Append ('.');
return typeOutput;
}
+ static bool is_identifier_start_character (char c)
+ {
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == '@' || Char.IsLetter (c);
+ }
+
+ static bool is_identifier_part_character (char c)
+ {
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c)
+;
+ }
+
protected override bool IsValidIdentifier (string identifier)
{
+ if (identifier == null || identifier.Length == 0)
+ return false;
+
if (keywordsTable == null)
FillKeywordTable ();
- return !keywordsTable.Contains (identifier);
+ if (keywordsTable.Contains (identifier))
+ return false;
+
+ if (!is_identifier_start_character (identifier [0]))
+ return false;
+
+ for (int i = 1; i < identifier.Length; i ++)
+ if (! is_identifier_part_character (identifier [i]))
+ return false;
+
+ return true;
}
protected override bool Supports (GeneratorSupport supports)
if (count == 0)
return;
- ++Indent;
- foreach (CodeTypeParameter p in parameters) {
- if (p.Constraints.Count == 0)
- continue;
+ bool indented = false;
+
+ for (int i = 0; i < count; i++) {
+ CodeTypeParameter p = parameters [i];
+ bool hasConstraints = (p.Constraints.Count != 0);
Output.WriteLine ();
+ if (!hasConstraints && !p.HasConstructorConstraint)
+ continue;
+
+ if (!indented) {
+ ++Indent;
+ indented = true;
+ }
+
Output.Write ("where ");
Output.Write (p.Name);
Output.Write (" : ");
- bool is_first = true;
- foreach (CodeTypeReference r in p.Constraints) {
- if (is_first)
- is_first = false;
- else
+ for (int j = 0; j < p.Constraints.Count; j++) {
+ if (j > 0)
Output.Write (", ");
- OutputType (r);
+ OutputType (p.Constraints [j]);
}
+
if (p.HasConstructorConstraint) {
- if (!is_first)
+ if (hasConstraints)
Output.Write (", ");
- Output.Write ("new ()");
+ Output.Write ("new");
+ if (hasConstraints)
+ Output.Write (" ");
+ Output.Write ("()");
}
-
}
- --Indent;
+
+ if (indented)
+ --Indent;
}
string GetTypeArguments (CodeTypeReferenceCollection collection)
{
if (count == 0) {
return;
+ } else if (typeArguments.Count == 0) {
+ // generic type definition
+ sb.Append ("<>");
+ return;
}
sb.Append ('<');