public virtual object Visit (ArrayInitializer arrayInitializer)
{
return null;
}
开发者ID:KAW0,项目名称:Alter-Native,代码行数:4,代码来源:visit.cs
示例3: CreateDynamicBinderArguments
public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
{
Location loc = Location.Null;
var all = new ArrayInitializer (args.Count, loc);
MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (rc, loc);
foreach (Argument a in args) {
Arguments dargs = new Arguments (2);
// CSharpArgumentInfoFlags.None = 0
const string info_flags_enum = "CSharpArgumentInfoFlags";
Expression info_flags = new IntLiteral (rc.BuiltinTypes, 0, loc);
if (a.Expr is Constant) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc));
} else if (a.ArgType == Argument.AType.Ref) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc));
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
} else if (a.ArgType == Argument.AType.Out) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc));
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
} else if (a.ArgType == Argument.AType.DynamicTypeName) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc));
}
TypeSpec arg_type;
if (rc.FileType == SourceFileType.PlayScript &&
a.Expr is ArrayInitializer || a.Expr is AsObjectInitializer) {
if (a.Expr is ArrayInitializer) {
arg_type = rc.Module.PredefinedTypes.AsArray.Resolve();
} else {
arg_type = rc.Module.PredefinedTypes.AsObject.Resolve();
}
} else {
arg_type = a.Expr.Type;
}
if (arg_type.BuiltinType != BuiltinTypeSpec.Type.Dynamic && arg_type != InternalType.NullLiteral) {
MethodGroupExpr mg = a.Expr as MethodGroupExpr;
bool wasConverted = false;
// In PlayScript, we try to implicity convert to dynamic, which handles conversions of method groups to delegates, and
// anon methods to delegates.
if (rc.FileType == SourceFileType.PlayScript && (mg != null || arg_type == InternalType.AnonymousMethod)) {
var expr = Convert.ImplicitConversion (rc, a.Expr, rc.BuiltinTypes.Dynamic, loc);
if (expr != null) {
a.Expr = expr;
arg_type = rc.BuiltinTypes.Dynamic;
wasConverted = true;
}
}
// Failed.. check the C# error
if (!wasConverted) {
if (mg != null) {
rc.Report.Error (1976, a.Expr.Location,
"The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
mg.Name);
} else if (arg_type == InternalType.AnonymousMethod) {
rc.Report.Error (1977, a.Expr.Location,
"An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
} else if (arg_type.Kind == MemberKind.Void || arg_type == InternalType.Arglist || arg_type.IsPointer) {
rc.Report.Error (1978, a.Expr.Location,
"An expression of type `{0}' cannot be used as an argument of dynamic operation",
arg_type.GetSignatureForError ());
}
}
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
}
string named_value;
NamedArgument na = a as NamedArgument;
if (na != null) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc));
named_value = na.Name;
} else {
named_value = null;
}
dargs.Add (new Argument (info_flags));
dargs.Add (new Argument (new StringLiteral (rc.BuiltinTypes, named_value, loc)));
all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
}
return all;
}
public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
{
Location loc = Location.Null;
var all = new ArrayInitializer (args.Count, loc);
MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
foreach (Argument a in args) {
Arguments dargs = new Arguments (2);
// CSharpArgumentInfoFlags.None = 0
const string info_flags_enum = "CSharpArgumentInfoFlags";
Expression info_flags = new IntLiteral (0, loc);
var constant = a.Expr as Constant;
if (constant != null && constant.IsLiteral) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc), loc);
} else if (a.ArgType == Argument.AType.Ref) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc), loc);
} else if (a.ArgType == Argument.AType.Out) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc), loc);
} else if (a.ArgType == Argument.AType.DynamicTypeName) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc), loc);
}
var arg_type = a.Expr.Type;
if (arg_type != InternalType.Dynamic) {
MethodGroupExpr mg = a.Expr as MethodGroupExpr;
if (mg != null) {
rc.Report.Error (1976, a.Expr.Location,
"The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
mg.Name);
} else if (arg_type == InternalType.AnonymousMethod) {
rc.Report.Error (1977, a.Expr.Location,
"An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
} else if (arg_type == TypeManager.void_type || arg_type == InternalType.Arglist || arg_type.IsPointer) {
rc.Report.Error (1978, a.Expr.Location,
"An expression of type `{0}' cannot be used as an argument of dynamic operation",
TypeManager.CSharpName (arg_type));
}
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
}
string named_value;
NamedArgument na = a as NamedArgument;
if (na != null) {
info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc), loc);
named_value = na.Name;
} else {
named_value = null;
}
dargs.Add (new Argument (info_flags));
dargs.Add (new Argument (new StringLiteral (named_value, loc)));
all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
}
return all;
}
public Expression CreateCallSiteBinder(ResolveContext ec, Arguments args)
{
Arguments binder_args = new Arguments (member != null ? 5 : 3);
bool is_member_access = member is MemberAccess;
CSharpBinderFlags call_flags;
if (!is_member_access && member is SimpleName) {
call_flags = CSharpBinderFlags.InvokeSimpleName;
is_member_access = true;
} else {
call_flags = 0;
}
binder_args.Add (new Argument (new BinderFlags (call_flags, this)));
if (is_member_access)
binder_args.Add (new Argument (new StringLiteral (member.Name, member.Location)));
if (member != null && member.HasTypeArguments) {
TypeArguments ta = member.TypeArguments;
if (ta.Resolve (ec)) {
var targs = new ArrayInitializer (ta.Count, loc);
foreach (TypeSpec t in ta.Arguments)
targs.Add (new TypeOf (new TypeExpression (t, loc), loc));
binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", targs, loc)));
}
} else if (is_member_access) {
binder_args.Add (new Argument (new NullLiteral (loc)));
}
binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
Expression real_args;
if (args == null) {
// Cannot be null because .NET trips over
real_args = new ArrayCreation (
new MemberAccess (GetBinderNamespace (loc), "CSharpArgumentInfo", loc), "[]",
new ArrayInitializer (0, loc), loc);
} else {
real_args = new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), loc);
}
binder_args.Add (new Argument (real_args));
return new Invocation (GetBinder (is_member_access ? "InvokeMember" : "Invoke", loc), binder_args);
}
开发者ID:speier,项目名称:shake,代码行数:47,代码来源:dynamic.cs
示例8: Visit
public override object Visit (ArrayInitializer arrayInitializer)
{
var result = new ArrayInitializerExpression ();
var location = LocationsBag.GetLocations (arrayInitializer);
var commaLocations = LocationsBag.GetLocations (arrayInitializer.Elements);
for (int i = 0; i < arrayInitializer.Count; i++) {
result.AddChild((Expression)arrayInitializer[i].Accept(this), ArrayInitializerExpression.Roles.Expression);
}
return result;
}
// IMPORTANT NOTE:
// The grammar consists of a few LALR(1) conflicts. These issues are, however, correctly handled, due to the fact that the grammar
// is defined in a specific order making the already added parser actions have precedence over the other.
//
// Known conflicts that are correctly handled:
//
// - ELSE: Shift/Reduce conflict Dangling ELSE problem. Lots of articles are around on the internet.
// The shift action is taken here.
//
// - CLOSE_PARENS: Shift/Reduce conflict. This is due to the fact that the explicit cast expression is like the parenthesized
// expression. The shift action is taken here.
//
// - STAR: Reduce/Reduce conflict, between VariableType -> TypeNameExpression and PrimaryExpression -> TypeNameExpression,
// due to the fact variable types can have '*', and look therefore like a binary operator expression.
// The first reduce action is taken here.
public CSharpGrammar()
{
// Please let me know if there is a better way of tidying this :s
TokenMapping.Add((int)ERROR, Error);
#region Definitions to use later
var statementList = new GrammarDefinition("StatementList");
var statementListOptional = new GrammarDefinition("StatementListOptional",
rule: null
| statementList);
var blockStatement = new GrammarDefinition("BlockStatement");
var variableDeclarator = new GrammarDefinition("VariableDeclarator");
var variableDeclaratorList = new GrammarDefinition("VariableDeclaratorList");
variableDeclaratorList.Rule = variableDeclarator
| variableDeclaratorList
+ ToElement(COMMA)
+ variableDeclarator;
var variableDeclaration = new GrammarDefinition("VariableDeclaration");
var variableInitializer = new GrammarDefinition("VariableInitializer");
var arrayInitializer = new GrammarDefinition("ArrayInitializer");
var arrayInitializerOptional = new GrammarDefinition("ArrayInitializerOptional",
rule: null | arrayInitializer);
var identifierInsideBody = new GrammarDefinition("IdentifierInsideBody",
rule: ToElement(IDENTIFIER),
createNode: node => ToIdentifier(node.Children[0].Result));
var identifierInsideBodyOptional = new GrammarDefinition("IdentifierInsideBodyOptional",
rule: null | identifierInsideBody);
variableDeclarator.Rule = identifierInsideBody
| identifierInsideBody
+ ToElement(EQUALS)
+ variableInitializer;
variableDeclarator.ComputeResult = node =>
{
var result = new VariableDeclarator((Identifier) node.Children[0].Result);
if (node.Children.Count > 1)
{
result.OperatorToken = (AstToken) node.Children[1].Result;
result.Value = (Expression) node.Children[2].Result;
}
return result;
};
var typeReference = new GrammarDefinition("TypeReference");
var identifierExpression = new GrammarDefinition("IdentifierExpression",
rule: identifierInsideBody,
createNode: node => new IdentifierExpression((Identifier) node.Children[0].Result));
var usingDirectiveListOptional = new GrammarDefinition("UsingDirectiveListOptional");
#endregion
#region Type References
var namespaceOrTypeExpression = new GrammarDefinition("NamespaceOrTypeExpression");
namespaceOrTypeExpression.Rule = identifierExpression |
namespaceOrTypeExpression
+ ToElement(DOT)
+ ToElement(IDENTIFIER);
namespaceOrTypeExpression.ComputeResult = node =>
{
if (node.Children.Count == 1)
return ToTypeReference((IConvertibleToType) node.Children[0].Result);
var result = new MemberTypeReference();
result.Target = (TypeReference) node.Children[0].Result;
result.AddChild(AstNodeTitles.Accessor, node.Children[1].Result);
result.Identifier = ToIdentifier(node.Children[2].Result);
return result;
};
ComputeResultDelegate createPrimitiveTypeExpression = node =>
{
if (node.Children[0].Result is PrimitiveTypeReference)
return node.Children[0].Result;
return new PrimitiveTypeReference
{
Identifier = ToIdentifier(node.Children[0].Result),
//.........这里部分代码省略.........
public bool VerifyArgumentsCompat(ResolveContext ec, ref Arguments arguments,
int arg_count, MethodSpec method,
bool chose_params_expanded,
bool may_fail, Location loc)
{
AParametersCollection pd = method.Parameters;
int param_count = GetApplicableParametersCount (method, pd);
int errors = ec.Report.Errors;
Parameter.Modifier p_mod = 0;
TypeSpec pt = null;
int a_idx = 0, a_pos = 0;
Argument a = null;
ArrayInitializer params_initializers = null;
bool has_unsafe_arg = method.ReturnType.IsPointer;
for (; a_idx < arg_count; a_idx++, ++a_pos) {
a = arguments [a_idx];
if (p_mod != Parameter.Modifier.PARAMS) {
p_mod = pd.FixedParameters [a_idx].ModFlags;
pt = pd.Types [a_idx];
has_unsafe_arg |= pt.IsPointer;
if (p_mod == Parameter.Modifier.PARAMS) {
if (chose_params_expanded) {
params_initializers = new ArrayInitializer (arg_count - a_idx, a.Expr.Location);
pt = TypeManager.GetElementType (pt);
}
}
}
//
// Types have to be identical when ref or out modifer is used
//
if (a.Modifier != 0 || (p_mod & ~Parameter.Modifier.PARAMS) != 0) {
if ((p_mod & ~Parameter.Modifier.PARAMS) != a.Modifier)
break;
if (!TypeManager.IsEqual (a.Expr.Type, pt))
break;
continue;
} else {
NamedArgument na = a as NamedArgument;
if (na != null) {
int name_index = pd.GetParameterIndexByName (na.Name);
if (name_index < 0 || name_index >= param_count) {
if (DeclaringType != null && TypeManager.IsDelegateType (DeclaringType)) {
ec.Report.SymbolRelatedToPreviousError (DeclaringType);
ec.Report.Error (1746, na.Location,
"The delegate `{0}' does not contain a parameter named `{1}'",
TypeManager.CSharpName (DeclaringType), na.Name);
} else {
ec.Report.SymbolRelatedToPreviousError (best_candidate);
ec.Report.Error (1739, na.Location,
"The best overloaded method match for `{0}' does not contain a parameter named `{1}'",
TypeManager.CSharpSignature (method), na.Name);
}
} else if (arguments[name_index] != a) {
if (DeclaringType != null && TypeManager.IsDelegateType (DeclaringType))
ec.Report.SymbolRelatedToPreviousError (DeclaringType);
else
ec.Report.SymbolRelatedToPreviousError (best_candidate);
ec.Report.Error (1744, na.Location,
"Named argument `{0}' cannot be used for a parameter which has positional argument specified",
na.Name);
}
}
}
if (a.Expr.Type == InternalType.Dynamic)
continue;
if (delegate_type != null && !Delegate.IsTypeCovariant (a.Expr, pt))
break;
Expression conv = Convert.ImplicitConversion (ec, a.Expr, pt, loc);
if (conv == null)
break;
//
// Convert params arguments to an array initializer
//
if (params_initializers != null) {
// we choose to use 'a.Expr' rather than 'conv' so that
// we don't hide the kind of expression we have (esp. CompoundAssign.Helper)
params_initializers.Add (a.Expr);
arguments.RemoveAt (a_idx--);
--arg_count;
continue;
}
// Update the argument with the implicit conversion
a.Expr = conv;
}
if (a_idx != arg_count) {
if (!may_fail && ec.Report.Errors == errors) {
if (CustomErrorHandler != null)
//.........这里部分代码省略.........
开发者ID:speier,项目名称:shake,代码行数:101,代码来源:ecore.cs
示例19: Visit
public override object Visit(ArrayInitializer arrayInitializer)
{
var result = new ArrayInitializerExpression();
var location = LocationsBag.GetLocations(arrayInitializer);
result.AddChild(new CSharpTokenNode(Convert(arrayInitializer.Location), Roles.LBrace), Roles.LBrace);
var commaLocations = LocationsBag.GetLocations(arrayInitializer.Elements);
for (int i = 0; i < arrayInitializer.Count; i++) {
var init = arrayInitializer [i];
if (init == null)
continue;
result.AddChild((Expression)init.Accept(this), Roles.Expression);
if (commaLocations != null && i < commaLocations.Count)
result.AddChild(new CSharpTokenNode(Convert(commaLocations [i]), Roles.Comma), Roles.Comma);
}
if (location != null) {
if (location.Count == 2) // optional comma
result.AddChild(new CSharpTokenNode(Convert(location [0]), Roles.Comma), Roles.Comma);
result.AddChild(new CSharpTokenNode(Convert(location [location.Count - 1]), Roles.RBrace), Roles.RBrace);
}
return result;
}
请发表评论