Skip to content

Commit

Permalink
Updated mcs. Note: Switch ast changed in mcs. (does not affect the
Browse files Browse the repository at this point in the history
nrefactory AST)
  • Loading branch information
Mike Krüger committed Mar 19, 2013
1 parent 4496f95 commit df1fa13
Show file tree
Hide file tree
Showing 19 changed files with 4,048 additions and 3,917 deletions.
71 changes: 40 additions & 31 deletions ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1822,40 +1822,31 @@ public override object Visit (Switch switchStatement)
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
if (location != null && location.Count > 2)
result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.LBrace), Roles.LBrace);
if (switchStatement.Sections != null) {
foreach (var section in switchStatement.Sections) {
var newSection = new SwitchSection ();
if (section.Labels != null) {
foreach (var caseLabel in section.Labels) {
var newLabel = new CaseLabel ();
if (caseLabel.Label != null) {
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), CaseLabel.CaseKeywordRole), CaseLabel.CaseKeywordRole);
if (caseLabel.Label != null)
newLabel.AddChild ((Expression)caseLabel.Label.Accept (this), Roles.Expression);
var colonLocation = LocationsBag.GetLocations (caseLabel);
if (colonLocation != null)
newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0]), Roles.Colon), Roles.Colon);
} else {
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location), CaseLabel.DefaultKeywordRole), CaseLabel.DefaultKeywordRole);
newLabel.AddChild (new CSharpTokenNode (new TextLocation (caseLabel.Location.Row, caseLabel.Location.Column + "default".Length), Roles.Colon), Roles.Colon);
}
newSection.AddChild (newLabel, SwitchSection.CaseLabelRole);
}
SwitchSection newSection = null;
bool lastWasCase = false, added = true;

foreach (var child in switchStatement.Block.Statements) {
var statement = child.Accept(this);
var caseLabel = statement as CaseLabel;
if (caseLabel != null) {
if (!lastWasCase) {
newSection = new SwitchSection();
added = false;
}

var blockStatement = section.Block;
var bodyBlock = new BlockStatement ();
int curLocal = 0;
AddBlockChildren (bodyBlock, blockStatement, ref curLocal);
foreach (var statement in bodyBlock.Statements) {
statement.Remove ();
newSection.AddChild (statement, Roles.EmbeddedStatement);

newSection.AddChild (caseLabel, SwitchSection.CaseLabelRole);
lastWasCase = true;
} else {
if (lastWasCase) {
result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
lastWasCase = false;
added = true;
}
result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
newSection.AddChild((Statement)statement, Roles.EmbeddedStatement);
}
}

if (!added)
result.AddChild (newSection, SwitchStatement.SwitchSectionRole);

if (location != null && location.Count > 3) {
result.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RBrace), Roles.RBrace);
} else {
Expand All @@ -1865,7 +1856,25 @@ public override object Visit (Switch switchStatement)

return result;
}


public override object Visit(SwitchLabel switchLabel)
{
var newLabel = new CaseLabel ();
if (!switchLabel.IsDefault) {
newLabel.AddChild (new CSharpTokenNode (Convert (switchLabel.Location), CaseLabel.CaseKeywordRole), CaseLabel.CaseKeywordRole);
if (switchLabel.Label != null)
newLabel.AddChild ((Expression)switchLabel.Label.Accept (this), Roles.Expression);
var colonLocation = LocationsBag.GetLocations (switchLabel);
if (colonLocation != null)
newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0]), Roles.Colon), Roles.Colon);
} else {
newLabel.AddChild (new CSharpTokenNode (Convert (switchLabel.Location), CaseLabel.DefaultKeywordRole), CaseLabel.DefaultKeywordRole);
newLabel.AddChild (new CSharpTokenNode (new TextLocation (switchLabel.Location.Row, switchLabel.Location.Column + "default".Length), Roles.Colon), Roles.Colon);
}
return newLabel;
}


public override object Visit (Lock lockStatement)
{
var result = new LockStatement ();
Expand Down
23 changes: 22 additions & 1 deletion ICSharpCode.NRefactory.CSharp/Parser/mcs/anonymous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,17 @@ public Expression Compatible (ResolveContext ec, TypeSpec type)
}
} else {
am = body.Compatible (ec);

if (body.DirectMethodGroupConversion != null) {
var errors_printer = new SessionReportPrinter ();
var old = ec.Report.SetPrinter (errors_printer);
var expr = new ImplicitDelegateCreation (delegate_type, body.DirectMethodGroupConversion, loc) {
AllowSpecialMethodsInvocation = true
}.Resolve (ec);
ec.Report.SetPrinter (old);
if (expr != null && errors_printer.ErrorsCount == 0)
am = expr;
}
}
} catch (CompletionResult) {
throw;
Expand Down Expand Up @@ -1568,6 +1579,14 @@ public override string ContainerType {
get { return "anonymous method"; }
}

//
// Method-group instance for lambdas which can be replaced with
// simple method group call
//
public MethodGroupExpr DirectMethodGroupConversion {
get; set;
}

public override bool IsIterator {
get {
return false;
Expand All @@ -1590,7 +1609,9 @@ public TypeInferenceContext ReturnTypeInference {
}

public override AnonymousMethodStorey Storey {
get { return storey; }
get {
return storey;
}
}

#endregion
Expand Down
14 changes: 13 additions & 1 deletion ICSharpCode.NRefactory.CSharp/Parser/mcs/assign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ public Expression Source {
}
}

public override Location StartLocation {
get {
return target.StartLocation;
}
}

public override bool ContainsEmitWithAwait ()
{
return target.ContainsEmitWithAwait () || source.ContainsEmitWithAwait ();
Expand Down Expand Up @@ -541,7 +547,7 @@ public override ExplicitBlock ConstructorBlock {
// Keep resolved value because field initializers have their own rules
//
ExpressionStatement resolved;
IMemberContext mc;
FieldBase mc;

public FieldInitializer (FieldBase mc, Expression expression, Location loc)
: base (new FieldExpr (mc.Spec, expression.Location), expression, loc)
Expand All @@ -551,6 +557,12 @@ public FieldInitializer (FieldBase mc, Expression expression, Location loc)
((FieldExpr)target).InstanceExpression = new CompilerGeneratedThis (mc.CurrentType, expression.Location);
}

public override Location StartLocation {
get {
return loc;
}
}

protected override Expression DoResolve (ResolveContext ec)
{
// Field initializer can be resolved (fail) many times
Expand Down
46 changes: 36 additions & 10 deletions ICSharpCode.NRefactory.CSharp/Parser/mcs/cfold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,51 @@ static public Constant BinaryFold (ResolveContext ec, Binary.Operator oper,
break;

case Binary.Operator.Addition:
if (lt == InternalType.NullLiteral)
return right;

if (rt == InternalType.NullLiteral)
return left;

//
// If both sides are strings, then concatenate, if
// one is a string, and the other is not, then defer
// to runtime concatenation
// If both sides are strings, then concatenate
//
// string operator + (string x, string y)
//
if (lt.BuiltinType == BuiltinTypeSpec.Type.String || rt.BuiltinType == BuiltinTypeSpec.Type.String){
if (lt == rt)
return new StringConstant (ec.BuiltinTypes, (string)left.GetValue () + (string)right.GetValue (),
left.Location);


if (lt == InternalType.NullLiteral)
return new StringConstant (ec.BuiltinTypes, "" + right.GetValue (), left.Location);

if (rt == InternalType.NullLiteral)
return new StringConstant (ec.BuiltinTypes, left.GetValue () + "", left.Location);

return null;
}

//
// string operator + (string x, object y)
//
if (lt == InternalType.NullLiteral) {
if (rt.BuiltinType == BuiltinTypeSpec.Type.Object)
return new StringConstant (ec.BuiltinTypes, "" + right.GetValue (), left.Location);

if (lt == rt) {
ec.Report.Error (34, loc, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
"+", lt.GetSignatureForError (), rt.GetSignatureForError ());
return null;
}

return right;
}

//
// string operator + (object x, string y)
//
if (rt == InternalType.NullLiteral) {
if (lt.BuiltinType == BuiltinTypeSpec.Type.Object)
return new StringConstant (ec.BuiltinTypes, right.GetValue () + "", left.Location);

return left;
}

//
// handle "E operator + (E x, U y)"
// handle "E operator + (Y y, E x)"
Expand Down
1 change: 1 addition & 0 deletions ICSharpCode.NRefactory.CSharp/Parser/mcs/class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ enum CachedMethods

public int DynamicSitesCounter;
public int AnonymousMethodsCounter;
public int MethodGroupsCounter;

static readonly string[] attribute_targets = new string[] { "type" };

Expand Down
3 changes: 1 addition & 2 deletions ICSharpCode.NRefactory.CSharp/Parser/mcs/convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,8 +1310,7 @@ static Expression ImplicitConversionStandard (ResolveContext ec, Expression expr
if (ec.Module.Compiler.Settings.Version != LanguageVersion.ISO_1){
MethodGroupExpr mg = expr as MethodGroupExpr;
if (mg != null)
return ImplicitDelegateCreation.Create (
ec, mg, target_type, loc);
return new ImplicitDelegateCreation (target_type, mg, loc).Resolve (ec);
}
}

Expand Down
Loading

0 comments on commit df1fa13

Please sign in to comment.