diff --git a/ICSharpCode.NRefactory/CSharp/Ast/PrimitiveType.cs b/ICSharpCode.NRefactory/CSharp/Ast/PrimitiveType.cs index 19f2e595f1..8bd0e208a4 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/PrimitiveType.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/PrimitiveType.cs @@ -43,6 +43,12 @@ namespace ICSharpCode.NRefactory.CSharp this.Keyword = keyword; } + public PrimitiveType(string keyword, AstLocation location) + { + this.Keyword = keyword; + this.Location = location; + } + public override AstLocation StartLocation { get { return Location; diff --git a/ICSharpCode.NRefactory/CSharp/Ast/SimpleType.cs b/ICSharpCode.NRefactory/CSharp/Ast/SimpleType.cs index 2111ea533b..b8e93c0ed4 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/SimpleType.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/SimpleType.cs @@ -42,6 +42,11 @@ namespace ICSharpCode.NRefactory.CSharp this.Identifier = identifier; } + public SimpleType(string identifier, AstLocation location) + { + SetChildByRole (Roles.Identifier, new Identifier(identifier, location)); + } + public string Identifier { get { return GetChildByRole (Roles.Identifier).Name; diff --git a/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs b/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs index b46eee821c..f5dac29c2b 100644 --- a/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs +++ b/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs @@ -67,6 +67,68 @@ namespace ICSharpCode.NRefactory.CSharp #region Global Stack namespaceStack = new Stack (); + void AddTypeArguments (ATypeNameExpression texpr, AstType result) + { + if (!texpr.HasTypeArguments) + return; + foreach (var arg in texpr.TypeArguments.Args) { + result.AddChild (ConvertToType (arg), AstType.Roles.TypeArgument); + } + } + + AstType ConvertToType (Mono.CSharp.Expression typeName) + { + if (typeName is TypeExpression) { + var typeExpr = (Mono.CSharp.TypeExpression)typeName; + return new PrimitiveType (typeExpr.GetSignatureForError (), Convert (typeExpr.Location)); + } + + if (typeName is Mono.CSharp.QualifiedAliasMember) { + var qam = (Mono.CSharp.QualifiedAliasMember)typeName; + return new SimpleType (qam.Name, Convert (qam.Location)); + } + + if (typeName is MemberAccess) { + MemberAccess ma = (MemberAccess)typeName; + + var memberType = new MemberType (); + memberType.AddChild (ConvertToType (ma.LeftExpression), MemberType.TargetRole); + memberType.MemberName = ma.Name; + + AddTypeArguments (ma, memberType); + return memberType; + } + + if (typeName is SimpleName) { + var sn = (SimpleName)typeName; + var result = new SimpleType (sn.Name, Convert (sn.Location)); + AddTypeArguments (sn, result); + return result; + } + + if (typeName is ComposedCast) { + var cc = (ComposedCast)typeName; + var baseType = ConvertToType (cc.Left); + var result = new ComposedType () { BaseType = baseType }; + + if (cc.Spec.IsNullable) { + result.HasNullableSpecifier = true; + } else if (cc.Spec.IsPointer) { + result.PointerRank++; + } else { + result.ArraySpecifiers = new ArraySpecifier[] { + new ArraySpecifier () { + Dimensions = cc.Spec.Dimension - 1 + } + }; + } + return result; + } + + System.Console.WriteLine ("Error while converting :" + typeName + " - unknown type name"); + return new SimpleType ("unknown"); + } + public override void Visit (UsingsBag.Namespace nspace) { NamespaceDeclaration nDecl = null; @@ -242,7 +304,7 @@ namespace ICSharpCode.NRefactory.CSharp AddModifiers (newDelegate, location); if (location != null) newDelegate.AddChild (new CSharpTokenNode (Convert (location[0]), "delegate".Length), TypeDeclaration.Roles.Keyword); - newDelegate.AddChild ((AstType)d.ReturnType.Accept (this), AstNode.Roles.Type); + newDelegate.AddChild (ConvertToType (d.ReturnType), AstNode.Roles.Type); newDelegate.AddChild (new Identifier (d.Name, Convert (d.MemberName.Location)), AstNode.Roles.Identifier); if (d.MemberName.TypeArguments != null) { var typeArgLocation = LocationsBag.GetLocations (d.MemberName); @@ -347,7 +409,7 @@ namespace ICSharpCode.NRefactory.CSharp AddModifiers (newField, location); if (location != null) newField.AddChild (new CSharpTokenNode (Convert (location[0]), "fixed".Length), FieldDeclaration.Roles.Keyword); - newField.AddChild ((AstType)f.TypeName.Accept (this), FieldDeclaration.Roles.Type); + newField.AddChild (ConvertToType (f.TypeName), FieldDeclaration.Roles.Type); VariableInitializer variable = new VariableInitializer (); variable.AddChild (new Identifier (f.MemberName.Name, Convert (f.MemberName.Location)), FieldDeclaration.Roles.Identifier); @@ -379,7 +441,7 @@ namespace ICSharpCode.NRefactory.CSharp FieldDeclaration newField = new FieldDeclaration (); AddModifiers (newField, location); - newField.AddChild ((AstType)f.TypeName.Accept (this), FieldDeclaration.Roles.Type); + newField.AddChild (ConvertToType (f.TypeName), FieldDeclaration.Roles.Type); VariableInitializer variable = new VariableInitializer (); variable.AddChild (new Identifier (f.MemberName.Name, Convert (f.MemberName.Location)), FieldDeclaration.Roles.Identifier); @@ -420,7 +482,7 @@ namespace ICSharpCode.NRefactory.CSharp AddModifiers (newField, location); if (location != null) newField.AddChild (new CSharpTokenNode (Convert (location[0]), "const".Length), FieldDeclaration.Roles.Keyword); - newField.AddChild ((AstType)f.TypeName.Accept (this), FieldDeclaration.Roles.Type); + newField.AddChild (ConvertToType (f.TypeName), FieldDeclaration.Roles.Type); VariableInitializer variable = new VariableInitializer (); variable.AddChild (new Identifier (f.MemberName.Name, Convert (f.MemberName.Location)), VariableInitializer.Roles.Identifier); @@ -462,7 +524,7 @@ namespace ICSharpCode.NRefactory.CSharp AddModifiers (newOperator, location); - newOperator.AddChild ((AstType)o.TypeName.Accept (this), AstNode.Roles.Type); + newOperator.AddChild (ConvertToType (o.TypeName), AstNode.Roles.Type); if (o.OperatorType == Operator.OpType.Implicit) { if (location != null) { @@ -519,7 +581,7 @@ namespace ICSharpCode.NRefactory.CSharp var location = LocationsBag.GetMemberLocation (indexer); AddModifiers (newIndexer, location); - newIndexer.AddChild ((AstType)indexer.TypeName.Accept (this), AstNode.Roles.Type); + newIndexer.AddChild (ConvertToType (indexer.TypeName), AstNode.Roles.Type); if (location != null) newIndexer.AddChild (new CSharpTokenNode (Convert (location[0]), 1), IndexerDeclaration.Roles.LBracket); @@ -573,7 +635,7 @@ namespace ICSharpCode.NRefactory.CSharp var location = LocationsBag.GetMemberLocation (m); AddModifiers (newMethod, location); - newMethod.AddChild ((AstType)m.TypeName.Accept (this), AstNode.Roles.Type); + newMethod.AddChild (ConvertToType (m.TypeName), AstNode.Roles.Type); newMethod.AddChild (new Identifier (m.Name, Convert (m.Location)), AstNode.Roles.Identifier); if (m.MemberName.TypeArguments != null) { @@ -641,7 +703,7 @@ namespace ICSharpCode.NRefactory.CSharp var location = LocationsBag.GetMemberLocation (p); AddModifiers (newProperty, location); - newProperty.AddChild ((AstType)p.TypeName.Accept (this), AstNode.Roles.Type); + newProperty.AddChild (ConvertToType (p.TypeName), AstNode.Roles.Type); newProperty.AddChild (new Identifier (p.MemberName.Name, Convert (p.MemberName.Location)), AstNode.Roles.Identifier); if (location != null) newProperty.AddChild (new CSharpTokenNode (Convert (location[0]), 1), MethodDeclaration.Roles.LBrace); @@ -729,7 +791,7 @@ namespace ICSharpCode.NRefactory.CSharp if (location != null) newEvent.AddChild (new CSharpTokenNode (Convert (location[0]), "event".Length), EventDeclaration.Roles.Keyword); - newEvent.AddChild ((AstType)e.TypeName.Accept (this), AstNode.Roles.Type); + newEvent.AddChild (ConvertToType (e.TypeName), AstNode.Roles.Type); newEvent.AddChild (new Identifier (e.MemberName.Name, Convert (e.MemberName.Location)), EventDeclaration.Roles.Identifier); if (location != null) newEvent.AddChild (new CSharpTokenNode (Convert (location[1]), ";".Length), EventDeclaration.Roles.Semicolon); @@ -746,7 +808,7 @@ namespace ICSharpCode.NRefactory.CSharp if (location != null) newEvent.AddChild (new CSharpTokenNode (Convert (location[0]), "event".Length), CustomEventDeclaration.Roles.Keyword); - newEvent.AddChild ((AstType)ep.TypeName.Accept (this), CustomEventDeclaration.Roles.Type); + newEvent.AddChild (ConvertToType (ep.TypeName), CustomEventDeclaration.Roles.Type); newEvent.AddChild (new Identifier (ep.MemberName.Name, Convert (ep.MemberName.Location)), CustomEventDeclaration.Roles.Identifier); if (location != null && location.Count >= 2) newEvent.AddChild (new CSharpTokenNode (Convert (location[1]), 1), CustomEventDeclaration.Roles.LBrace); @@ -789,7 +851,7 @@ namespace ICSharpCode.NRefactory.CSharp public override object Visit (BlockVariableDeclaration blockVariableDeclaration) { var result = new VariableDeclarationStatement (); - result.AddChild ((AstType)blockVariableDeclaration.TypeExpression.Accept (this), VariableDeclarationStatement.Roles.Type); + result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), VariableDeclarationStatement.Roles.Type); var varInit = new VariableInitializer (); var location = LocationsBag.GetLocations (blockVariableDeclaration); @@ -828,7 +890,7 @@ namespace ICSharpCode.NRefactory.CSharp public override object Visit (BlockConstantDeclaration blockVariableDeclaration) { var result = new VariableDeclarationStatement (); - result.AddChild ((AstType)blockVariableDeclaration.TypeExpression.Accept (this), VariableDeclarationStatement.Roles.Type); + result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), VariableDeclarationStatement.Roles.Type); var varInit = new VariableInitializer (); var location = LocationsBag.GetLocations (blockVariableDeclaration); @@ -1101,13 +1163,13 @@ namespace ICSharpCode.NRefactory.CSharp usingResult.AddChild (new CSharpTokenNode (Convert (u.loc), "using".Length), UsingStatement.Roles.Keyword); usingResult.AddChild (new CSharpTokenNode (Convert (blockStatement.StartLocation), 1), UsingStatement.Roles.LPar); if (u.Variables != null) { - usingResult.AddChild ((AstType)u.Variables.TypeExpression.Accept (this), UsingStatement.Roles.Type); + usingResult.AddChild (ConvertToType (u.Variables.TypeExpression), UsingStatement.Roles.Type); usingResult.AddChild (new Identifier (u.Variables.Variable.Name, Convert (u.Variables.Variable.Location)), UsingStatement.Roles.Identifier); var loc = LocationsBag.GetLocations (u.Variables); if (loc != null) usingResult.AddChild (new CSharpTokenNode (Convert (loc[1]), 1), ContinueStatement.Roles.Assign); if (u.Variables.Initializer != null) - usingResult.AddChild ((AstType)u.Variables.Initializer.Accept (this), UsingStatement.ResourceAcquisitionRole); + usingResult.AddChild (ConvertToType (u.Variables.Initializer), UsingStatement.ResourceAcquisitionRole); } cur = u.Statement; @@ -1241,7 +1303,7 @@ namespace ICSharpCode.NRefactory.CSharp /* if (fixedStatement.Variables != null) { - result.AddChild ((AstType)fixedStatement.Variables.TypeExpression.Accept (this), UsingStatement.Roles.Type); + result.AddChild (ConvertToType (fixedStatement.Variables.TypeExpression.Accept (this), UsingStatement.Roles.Type); result.AddChild (new Identifier (fixedStatement.Variables.Variable.Name, Convert (fixedStatement.Variables.Variable.Location)), UsingStatement.Roles.Identifier); var loc = LocationsBag.GetLocations (fixedStatement.Variables); if (loc != null) @@ -1285,7 +1347,7 @@ namespace ICSharpCode.NRefactory.CSharp if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), CatchClause.Roles.LPar); - result.AddChild ((AstType)ctch.TypeExpression.Accept (this), CatchClause.Roles.Type); + result.AddChild (ConvertToType (ctch.TypeExpression), CatchClause.Roles.Type); if (ctch.Variable != null && !string.IsNullOrEmpty (ctch.Variable.Name)) result.AddChild (new Identifier (ctch.Variable.Name, Convert (ctch.Variable.Location)), CatchClause.Roles.Identifier); @@ -1341,7 +1403,7 @@ namespace ICSharpCode.NRefactory.CSharp result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), ForeachStatement.Roles.LPar); if (foreachStatement.TypeExpr == null) - result.AddChild ((AstType)foreachStatement.TypeExpr.Accept (this), ForeachStatement.Roles.Type); + result.AddChild (ConvertToType (foreachStatement.TypeExpr), ForeachStatement.Roles.Type); if (foreachStatement.Variable != null) result.AddChild (new Identifier (foreachStatement.Variable.Name, Convert (foreachStatement.Variable.Location)), ForeachStatement.Roles.Identifier); @@ -1567,7 +1629,7 @@ namespace ICSharpCode.NRefactory.CSharp var result = new IsExpression (); result.AddChild ((Expression)isExpression.Expr.Accept (this), IsExpression.Roles.Expression); result.AddChild (new CSharpTokenNode (Convert (isExpression.Location), "is".Length), IsExpression.Roles.Keyword); - result.AddChild ((AstType)isExpression.ProbeType.Accept (this), IsExpression.Roles.Type); + result.AddChild (ConvertToType (isExpression.ProbeType), IsExpression.Roles.Type); return result; } @@ -1576,7 +1638,7 @@ namespace ICSharpCode.NRefactory.CSharp var result = new AsExpression (); result.AddChild ((Expression)asExpression.Expr.Accept (this), AsExpression.Roles.Expression); result.AddChild (new CSharpTokenNode (Convert (asExpression.Location), "as".Length), AsExpression.Roles.Keyword); - result.AddChild ((AstType)asExpression.ProbeType.Accept (this), AsExpression.Roles.Type); + result.AddChild (ConvertToType (asExpression.ProbeType), AsExpression.Roles.Type); return result; } @@ -1587,7 +1649,7 @@ namespace ICSharpCode.NRefactory.CSharp result.AddChild (new CSharpTokenNode (Convert (castExpression.Location), 1), CastExpression.Roles.LPar); if (castExpression.TargetType != null) - result.AddChild ((AstType)castExpression.TargetType.Accept (this), CastExpression.Roles.Type); + result.AddChild (ConvertToType (castExpression.TargetType), CastExpression.Roles.Type); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), CastExpression.Roles.RPar); if (castExpression.Expr != null) @@ -1598,7 +1660,7 @@ namespace ICSharpCode.NRefactory.CSharp public override object Visit (ComposedCast composedCast) { var result = new ComposedType (); - result.AddChild ((AstType)composedCast.Left.Accept (this), ComposedType.Roles.Type); + result.AddChild (ConvertToType (composedCast.Left), ComposedType.Roles.Type); var spec = composedCast.Spec; while (spec != null) { @@ -1627,7 +1689,7 @@ namespace ICSharpCode.NRefactory.CSharp var location = LocationsBag.GetLocations (defaultValueExpression); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), CastExpression.Roles.LPar); - result.AddChild ((AstType)defaultValueExpression.Expr.Accept (this), CastExpression.Roles.Type); + result.AddChild (ConvertToType (defaultValueExpression.Expr), CastExpression.Roles.Type); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), CastExpression.Roles.RPar); return result; @@ -1770,7 +1832,7 @@ namespace ICSharpCode.NRefactory.CSharp break; } if (p.TypeExpression != null) // lambdas may have no types (a, b) => ... - parameterDeclarationExpression.AddChild ((AstType)p.TypeExpression.Accept (this), ParameterDeclaration.Roles.Type); + parameterDeclarationExpression.AddChild (ConvertToType (p.TypeExpression), ParameterDeclaration.Roles.Type); parameterDeclarationExpression.AddChild (new Identifier (p.Name, Convert (p.Location)), ParameterDeclaration.Roles.Identifier); if (p.HasDefaultValue) { if (location != null) @@ -1791,7 +1853,7 @@ namespace ICSharpCode.NRefactory.CSharp var arg = typeArguments.Args[i]; if (arg == null) continue; - parent.AddChild ((AstType)arg.Accept (this), InvocationExpression.Roles.TypeArgument); + parent.AddChild (ConvertToType (arg), InvocationExpression.Roles.TypeArgument); } } @@ -1805,7 +1867,7 @@ namespace ICSharpCode.NRefactory.CSharp var arg = typeArguments.Args[i]; if (arg == null) continue; - parent.AddChild ((AstType)arg.Accept (this), InvocationExpression.Roles.TypeArgument); + parent.AddChild (ConvertToType (arg), InvocationExpression.Roles.TypeArgument); } } @@ -1821,7 +1883,7 @@ namespace ICSharpCode.NRefactory.CSharp parent.AddChild (new Identifier (c.TypeParameter.Value, Convert (c.TypeParameter.Location)), InvocationExpression.Roles.Identifier); parent.AddChild (new CSharpTokenNode (Convert (location[1]), 1), Constraint.ColonRole); foreach (var expr in c.ConstraintExpressions) - parent.AddChild ((AstType)expr.Accept (this), Constraint.BaseTypeRole); + parent.AddChild (ConvertToType (expr), Constraint.BaseTypeRole); } } @@ -1878,7 +1940,7 @@ namespace ICSharpCode.NRefactory.CSharp result.AddChild (new CSharpTokenNode (Convert (newExpression.Location), "new".Length), ObjectCreateExpression.Roles.Keyword); if (newExpression.TypeRequested != null) - result.AddChild ((AstType)newExpression.TypeRequested.Accept (this), ObjectCreateExpression.Roles.Type); + result.AddChild (ConvertToType (newExpression.TypeRequested), ObjectCreateExpression.Roles.Type); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), ObjectCreateExpression.Roles.LPar); AddArguments (result, location, newExpression.Arguments); @@ -1898,7 +1960,7 @@ namespace ICSharpCode.NRefactory.CSharp result.AddChild (new CSharpTokenNode (Convert (newInitializeExpression.Location), "new".Length), ObjectCreateExpression.Roles.Keyword); if (newInitializeExpression.TypeRequested != null) - result.AddChild ((AstType)newInitializeExpression.TypeRequested.Accept (this), ObjectCreateExpression.Roles.Type); + result.AddChild (ConvertToType (newInitializeExpression.TypeRequested), ObjectCreateExpression.Roles.Type); var location = LocationsBag.GetLocations (newInitializeExpression); if (location != null) @@ -1919,7 +1981,7 @@ namespace ICSharpCode.NRefactory.CSharp result.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Location), "new".Length), ArrayCreateExpression.Roles.Keyword); if (arrayCreationExpression.NewType != null) - result.AddChild ((AstType)arrayCreationExpression.NewType.Accept (this), ArrayCreateExpression.Roles.Type); + result.AddChild (ConvertToType (arrayCreationExpression.NewType), ArrayCreateExpression.Roles.Type); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), ArrayCreateExpression.Roles.LBracket); if (arrayCreationExpression.Arguments != null) { @@ -1989,7 +2051,7 @@ namespace ICSharpCode.NRefactory.CSharp var location = LocationsBag.GetLocations (typeOfExpression); result.AddChild (new CSharpTokenNode (Convert (typeOfExpression.Location), "typeof".Length), TypeOfExpression.Roles.Keyword); result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), TypeOfExpression.Roles.LPar); - result.AddChild ((AstType)typeOfExpression.TypeExpression.Accept (this), TypeOfExpression.Roles.Type); + result.AddChild (ConvertToType (typeOfExpression.TypeExpression), TypeOfExpression.Roles.Type); result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), TypeOfExpression.Roles.RPar); return result; } @@ -2001,7 +2063,7 @@ namespace ICSharpCode.NRefactory.CSharp result.AddChild (new CSharpTokenNode (Convert (sizeOfExpression.Location), "sizeof".Length), TypeOfExpression.Roles.Keyword); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), TypeOfExpression.Roles.LPar); - result.AddChild ((AstType)sizeOfExpression.QueriedType.Accept (this), TypeOfExpression.Roles.Type); + result.AddChild (ConvertToType (sizeOfExpression.QueriedType), TypeOfExpression.Roles.Type); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), TypeOfExpression.Roles.RPar); return result; @@ -2060,7 +2122,7 @@ namespace ICSharpCode.NRefactory.CSharp var location = LocationsBag.GetLocations (stackAllocExpression); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), "stackalloc".Length), StackAllocExpression.Roles.Keyword); - result.AddChild ((AstType)stackAllocExpression.TypeExpression.Accept (this), StackAllocExpression.Roles.Type); + result.AddChild (ConvertToType (stackAllocExpression.TypeExpression), StackAllocExpression.Roles.Type); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), StackAllocExpression.Roles.LBracket); result.AddChild ((Expression)stackAllocExpression.CountExpression.Accept (this), StackAllocExpression.Roles.Expression);