public ILBasedTypeDescription(ReflectionBasedTypeDescription source) { this.Source = source; if(source.ParameterLessConstructorInfo != null) this.CreateInstanceDelegate = source.ParameterLessConstructorInfo.EmitConstructorInvocationDelegate(); else if(source.Type.IsValueType || source.Type.IsEnum) this.CreateInstanceDelegate = source.Type.EmitConstructorInvocationDelegate(); }
public override void TranslateConstructorInvocation(StringBuilder sb, ConstructorInvocation constructorInvocation) { sb.Append("new "); sb.Append(constructorInvocation.Type.RootValue); sb.Append('('); Expression[] args = constructorInvocation.Args; for (int i = 0; i < args.Length; ++i) { if (i > 0) { sb.Append(", "); } this.TranslateExpression(sb, args[i]); } sb.Append(')'); }
public override void TranslateConstructorInvocation(TranspilerContext sb, ConstructorInvocation constructorInvocation) { sb.Append("new "); sb.Append(this.TranslateType(constructorInvocation.Type)); sb.Append('('); Expression[] args = constructorInvocation.Args; for (int i = 0; i < args.Length; ++i) { if (i > 0) { sb.Append(", "); } this.TranslateExpression(sb, args[i]); } sb.Append(')'); }
public override async Task ProvideCompletionsAsync(CompletionContext context) { if (!context.Document.SupportsSemanticModel) { return; } var syntaxRoot = await context.Document.GetSyntaxRootAsync(); var semanticModel = await context.Document.GetSemanticModelAsync(); var tokenAtCursor = SuggestionHelpers.GetCurrentArgumentListSyntaxToken(syntaxRoot, context.Position); if (!tokenAtCursor.IsKind(SyntaxKind.OpenParenToken)) { return; } var callbackArgumentList = tokenAtCursor.Parent as ArgumentListSyntax; if (callbackArgumentList == null || callbackArgumentList.Arguments.Any()) { return; } var expression = callbackArgumentList.Parent.FindNearestContainer <InvocationExpressionSyntax, ObjectCreationExpressionSyntax>(); if (expression != null) { if (expression is InvocationExpressionSyntax invocationExpression) { if (invocationExpression.ArgumentList.Arguments.Count == 0) { var invocation = new MethodInvocation(invocationExpression); SuggestMethodParameters(context, invocation, semanticModel); } } else if (expression is ObjectCreationExpressionSyntax creationExpression) { if (creationExpression.ArgumentList?.Arguments.Count == 0) { var invocation = new ConstructorInvocation(creationExpression); SuggestMethodParameters(context, invocation, semanticModel); } } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var diagnostic = context.Diagnostics.First(); var token = root.FindToken(diagnostic.Location.SourceSpan.Start); var expression = token.Parent.FindNearestContainer <InvocationExpressionSyntax, ObjectCreationExpressionSyntax>(); if (expression != null) { if (expression is InvocationExpressionSyntax invocationExpression) { if (invocationExpression.ArgumentList.Arguments.Count == 0) { var invocation = new MethodInvocation(invocationExpression); if (invocationExpression.Expression is MemberAccessExpressionSyntax mae) { var methodName = mae.Name.ToFullString(); if (methodName == "Select" || methodName == "ConvertAll") { context.RegisterCodeFix(CodeAction.Create(title: titleWitSelect, createChangedDocument: c => CreateMappingLambda(context.Document, invocationExpression, c), equivalenceKey: titleWitSelect), diagnostic); } } else { context.RegisterCodeFix(CodeAction.Create(title: title, createChangedDocument: c => UseLocalVariablesAsParameters(context.Document, invocation, false, c), equivalenceKey: title), diagnostic); context.RegisterCodeFix(CodeAction.Create(title: titleWithNamed, createChangedDocument: c => UseLocalVariablesAsParameters(context.Document, invocation, true, c), equivalenceKey: titleWithNamed), diagnostic); } } } else if (expression is ObjectCreationExpressionSyntax creationExpression) { if (creationExpression.ArgumentList?.Arguments.Count == 0) { var invocation = new ConstructorInvocation(creationExpression); context.RegisterCodeFix(CodeAction.Create(title: title, createChangedDocument: c => UseLocalVariablesAsParameters(context.Document, invocation, false, c), equivalenceKey: title + "for constructor"), diagnostic); context.RegisterCodeFix(CodeAction.Create(title: titleWithNamed, createChangedDocument: c => UseLocalVariablesAsParameters(context.Document, invocation, true, c), equivalenceKey: titleWithNamed + "for constructor"), diagnostic); } } } }
public override void TranslateConstructorInvocation(StringBuilder sb, ConstructorInvocation constructorInvocation) { StructDefinition structDef = constructorInvocation.StructType; if (structDef == null) { throw new NotImplementedException(); } sb.Append('['); int args = structDef.ArgNames.Length; for (int i = 0; i < args; ++i) { if (i > 0) { sb.Append(", "); } this.TranslateExpression(sb, constructorInvocation.Args[i]); } sb.Append(']'); }
public override void TranslateConstructorInvocation(StringBuilder sb, ConstructorInvocation constructorInvocation) { if (constructorInvocation.StructType.NameToken.Value == "Value") { Expression firstArg = constructorInvocation.Args[0]; if (!(firstArg is InlineConstant)) { throw new InvalidOperationException("Cannot pass in non constant for first arg of Value construction."); } int type = (int)((InlineConstant)firstArg).Value; if (type == 2 || type == 3 || type == 6) { sb.Append("new Value("); this.TranslateExpression(sb, constructorInvocation.Args[1]); sb.Append(')'); return; } } sb.Append("new "); string structType = constructorInvocation.StructType.NameToken.Value; if (structType == "ClassValue") { structType = "org.crayonlang.interpreter.structs.ClassValue"; } sb.Append(structType); sb.Append('('); Expression[] args = constructorInvocation.Args; for (int i = 0; i < args.Length; ++i) { if (i > 0) { sb.Append(", "); } this.TranslateExpression(sb, args[i]); } sb.Append(')'); }
public override void TranslateConstructorInvocation(TranspilerContext sb, ConstructorInvocation constructorInvocation) { if (constructorInvocation.ClassDefinition != null) { throw new NotImplementedException(); } else { sb.Append(constructorInvocation.StructDefinition.NameToken.Value); sb.Append("_new("); Expression[] args = constructorInvocation.Args; for (int i = 0; i < args.Length; ++i) { if (i > 0) { sb.Append(", "); } this.TranslateExpression(sb, args[i]); } sb.Append(')'); } }
public override void TranslateConstructorInvocation(StringBuilder sb, ConstructorInvocation constructorInvocation) { sb.Append("new "); string structType = constructorInvocation.StructType.NameToken.Value; if (structType == "ClassValue") { structType = "org.crayonlang.interpreter.structs.ClassValue"; } sb.Append(structType); sb.Append('('); Expression[] args = constructorInvocation.Args; for (int i = 0; i < args.Length; ++i) { if (i > 0) { sb.Append(", "); } this.TranslateExpression(sb, args[i]); } sb.Append(')'); }
public override void TranslateConstructorInvocation(TranspilerContext sb, ConstructorInvocation constructorInvocation) { StructDefinition structDef = constructorInvocation.StructDefinition; ClassDefinition classDef = constructorInvocation.ClassDefinition; if (structDef == null) { throw new NotImplementedException(); } else { sb.Append('['); int args = structDef.FlatFieldNames.Length; for (int i = 0; i < args; ++i) { if (i > 0) { sb.Append(", "); } this.TranslateExpression(sb, constructorInvocation.Args[i]); } sb.Append(']'); } }
public abstract void TranslateConstructorInvocation(StringBuilder sb, ConstructorInvocation constructorInvocation);
public void TranslateExpression(StringBuilder sb, Expression expression) { string typeName = expression.GetType().Name; switch (typeName) { case "CastExpression": this.TranslateCast(sb, ((CastExpression)expression).Type, ((CastExpression)expression).Expression); break; case "FunctionReference": this.TranslateFunctionReference(sb, (FunctionReference)expression); break; case "NativeFunctionInvocation": this.TranslateNativeFunctionInvocation(sb, (NativeFunctionInvocation)expression); break; case "OpChain": this.TranslateOpChain(sb, (OpChain)expression); break; case "LibraryNativeFunctionInvocation": this.TranslateLibraryNativeFunctionInvocation(sb, (LibraryNativeFunctionInvocation)expression); break; case "InlineIncrement": InlineIncrement ii = (InlineIncrement)expression; this.TranslateInlineIncrement(sb, ii.Expression, ii.IsPrefix, ii.IncrementToken.Value == "++"); break; case "FunctionInvocation": FunctionInvocation funcInvocation = (FunctionInvocation)expression; bool specifyInterpreterScope = false; if (funcInvocation.FirstToken.FileName.StartsWith("LIB:") && funcInvocation.Root is FunctionReference) { FunctionDefinition funcDef = ((FunctionReference)funcInvocation.Root).Function; if (!funcDef.NameToken.FileName.StartsWith("LIB:")) { specifyInterpreterScope = true; } } if (specifyInterpreterScope) { this.TranslateFunctionInvocationInterpreterScoped(sb, (FunctionReference)funcInvocation.Root, funcInvocation.Args); } else { this.TranslateFunctionInvocationLocallyScoped(sb, (FunctionReference)funcInvocation.Root, funcInvocation.Args); } break; case "Variable": Variable v = (Variable)expression; string name = v.Name; char firstChar = name[0]; if (firstChar >= 'A' && firstChar <= 'Z' && name.Contains('_') && name.ToUpper() == name) { this.TranslateGlobalVariable(sb, v); } else { this.TranslateVariable(sb, v); } break; case "ConstructorInvocation": ConstructorInvocation constructor = (ConstructorInvocation)expression; string rootType = constructor.Type.RootValue; switch (rootType) { case "Array": if (constructor.Type.Generics.Length != 1) { throw new Pastel.ParserException(constructor.Type.FirstToken, "Array constructor requires exactly 1 generic type."); } this.TranslateArrayNew(sb, constructor.Type.Generics[0], constructor.Args[0]); break; case "List": if (constructor.Type.Generics.Length != 1) { throw new Pastel.ParserException(constructor.Type.FirstToken, "List constructor requires exactly 1 generic type."); } this.TranslateListNew(sb, constructor.Type.Generics[0]); break; case "Dictionary": if (constructor.Type.Generics.Length != 2) { throw new Pastel.ParserException(constructor.Type.FirstToken, "Dictionary constructor requires exactly 2 generic types."); } PType dictionaryKeyType = constructor.Type.Generics[0]; PType dictionaryValueType = constructor.Type.Generics[1]; this.TranslateDictionaryNew(sb, dictionaryKeyType, dictionaryValueType); break; default: // TODO: throw an exception (in the parser) if generics exist. this.TranslateConstructorInvocation(sb, constructor); break; } break; case "DotField": DotField df = (DotField)expression; StructDefinition structDef = df.StructType; if (structDef == null) { throw new InvalidOperationException(); // should have been thrown by the compiler } string fieldName = df.FieldName.Value; int fieldIndex = structDef.ArgIndexByName[fieldName]; this.TranslateStructFieldDereference(sb, df.Root, structDef, fieldName, fieldIndex); break; case "InlineConstant": InlineConstant ic = (InlineConstant)expression; switch (ic.ResolvedType.RootValue) { case "bool": this.TranslateBooleanConstant(sb, (bool)ic.Value); break; case "char": this.TranslateCharConstant(sb, ((string)ic.Value)[0]); break; case "double": this.TranslateFloatConstant(sb, (double)ic.Value); break; case "int": this.TranslateIntegerConstant(sb, (int)ic.Value); break; case "null": this.TranslateNullConstant(sb); break; case "string": this.TranslateStringConstant(sb, (string)ic.Value); break; default: throw new NotImplementedException(); } break; case "UnaryOp": UnaryOp uo = (UnaryOp)expression; if (uo.OpToken.Value == "-") { this.TranslateNegative(sb, uo); } else { this.TranslateBooleanNot(sb, uo); } break; case "ForcedParenthesis": sb.Append('('); this.TranslateExpression(sb, ((ForcedParenthesis)expression).Expression); sb.Append(')'); break; default: throw new NotImplementedException(typeName); } }
public abstract void TranslateConstructorInvocation(TranspilerContext sb, ConstructorInvocation constructorInvocation);
public void TranslateExpression(TranspilerContext sb, Expression expression) { string typeName = expression.GetType().Name; switch (typeName) { case "CastExpression": this.TranslateCast(sb, ((CastExpression)expression).Type, ((CastExpression)expression).Expression); break; case "FunctionReference": this.TranslateFunctionReference(sb, (FunctionReference)expression); break; case "FunctionPointerInvocation": this.TranslateFunctionPointerInvocation(sb, (FunctionPointerInvocation)expression); break; case "CoreFunctionInvocation": this.TranslateCoreFunctionInvocation(sb, (CoreFunctionInvocation)expression); break; case "OpChain": OpChain oc = (OpChain)expression; if (oc.IsStringConcatenation) { this.TranslateStringConcatenation(sb, oc.Expressions); } else { this.TranslateOpChain(sb, oc); } break; case "ExtensibleFunctionInvocation": this.TranslateExtensibleFunctionInvocation( sb, (ExtensibleFunctionInvocation)expression); break; case "InlineIncrement": InlineIncrement ii = (InlineIncrement)expression; this.TranslateInlineIncrement(sb, ii.Expression, ii.IsPrefix, ii.IncrementToken.Value == "++"); break; case "FunctionInvocation": FunctionInvocation funcInvocation = (FunctionInvocation)expression; string prefix = null; FunctionDefinition funcDef = ((FunctionReference)funcInvocation.Root).Function; PastelContext targetContext = funcDef.Context; PastelContext callerContext = funcInvocation.Owner.Context; if (targetContext != callerContext) { prefix = callerContext.GetDependencyExportPrefix(targetContext); } if (prefix != null) { this.TranslateFunctionInvocationWithPrefix(sb, prefix, (FunctionReference)funcInvocation.Root, funcInvocation.Args); } else { this.TranslateFunctionInvocation(sb, (FunctionReference)funcInvocation.Root, funcInvocation.Args); } break; case "Variable": Variable v = (Variable)expression; this.TranslateVariable(sb, v); break; case "ConstructorInvocation": ConstructorInvocation constructor = (ConstructorInvocation)expression; string rootType = constructor.Type.RootValue; switch (rootType) { case "Array": if (constructor.Type.Generics.Length != 1) { throw new Pastel.ParserException(constructor.Type.FirstToken, "Array constructor requires exactly 1 generic type."); } this.TranslateArrayNew(sb, constructor.Type.Generics[0], constructor.Args[0]); break; case "List": if (constructor.Type.Generics.Length != 1) { throw new Pastel.ParserException(constructor.Type.FirstToken, "List constructor requires exactly 1 generic type."); } this.TranslateListNew(sb, constructor.Type.Generics[0]); break; case "Dictionary": if (constructor.Type.Generics.Length != 2) { throw new Pastel.ParserException(constructor.Type.FirstToken, "Dictionary constructor requires exactly 2 generic types."); } PType dictionaryKeyType = constructor.Type.Generics[0]; PType dictionaryValueType = constructor.Type.Generics[1]; this.TranslateDictionaryNew(sb, dictionaryKeyType, dictionaryValueType); break; case "StringBuilder": if (constructor.Type.Generics.Length != 0) { throw new ParserException(constructor.Type.FirstToken, "StringBuilder constructor does not have any generics."); } this.TranslateStringBuilderNew(sb); break; default: // TODO: throw an exception (in the parser) if generics exist. this.TranslateConstructorInvocation(sb, constructor); break; } break; case "DotField": DotField df = (DotField)expression; StructDefinition structDef = df.StructType; ClassDefinition classDef = df.ClassType; string fieldName = df.FieldName.Value; if (classDef != null) { this.TranslateInstanceFieldDereference(sb, df.Root, classDef, fieldName); } else if (structDef != null) { int fieldIndex = structDef.FlatFieldIndexByName[fieldName]; this.TranslateStructFieldDereference(sb, df.Root, structDef, fieldName, fieldIndex); } else { throw new InvalidOperationException(); // should have been thrown by the compiler } break; case "InlineConstant": InlineConstant ic = (InlineConstant)expression; switch (ic.ResolvedType.RootValue) { case "bool": this.TranslateBooleanConstant(sb, (bool)ic.Value); break; case "char": this.TranslateCharConstant(sb, ((string)ic.Value)[0]); break; case "double": this.TranslateFloatConstant(sb, (double)ic.Value); break; case "int": this.TranslateIntegerConstant(sb, (int)ic.Value); break; case "null": this.TranslateNullConstant(sb); break; case "string": this.TranslateStringConstant(sb, (string)ic.Value); break; default: throw new NotImplementedException(); } break; case "ThisExpression": this.TranslateThis(sb, (ThisExpression)expression); break; case "UnaryOp": UnaryOp uo = (UnaryOp)expression; if (uo.OpToken.Value == "-") { this.TranslateNegative(sb, uo); } else { this.TranslateBooleanNot(sb, uo); } break; case "ForcedParenthesis": sb.Append('('); this.TranslateExpression(sb, ((ForcedParenthesis)expression).Expression); sb.Append(')'); break; default: throw new NotImplementedException(typeName); } }
/// <summary> /// メッセージシンクの処理(同期処理) /// </summary> /// <param name="msg">メソッド呼び出しのIMessage</param> /// <returns>戻り値のIMessage</returns> public IMessage SyncProcessMessage(IMessage msg) { IConstructionCallMessage ccm = msg as IConstructionCallMessage; if (ccm != null) { if (interceptorMap.Contains(ccm.MethodBase)) { ArrayList list = (ArrayList)interceptorMap[ccm.MethodBase]; IConstructorInterceptor[] intercepters = (IConstructorInterceptor[])list.ToArray(typeof(IConstructorInterceptor)); IJoinPoint jointpoint = new ConstructorInvocation(target, ccm, nextSink, intercepters); try { object[] outAuguments = null; jointpoint.FirstProceed(out outAuguments); return(new InternalConstructionResponse(outAuguments, ccm)); } catch (Exception e) { return(new InternalConstructionResponse(e, ccm)); } } else { return(nextSink.SyncProcessMessage(msg)); } } IMethodCallMessage mcm = msg as IMethodCallMessage; if (mcm != null) { if (interceptorMap.Contains(mcm.MethodBase)) { ArrayList list = (ArrayList)interceptorMap[mcm.MethodBase]; IMethodInterceptor[] intercepters = (IMethodInterceptor[])list.ToArray(typeof(IMethodInterceptor)); IJoinPoint jointpoint = new MethodInvocation(target, mcm, nextSink, intercepters); try { object[] outAuguments = null; object returnValue = jointpoint.FirstProceed(out outAuguments); if (outAuguments == null) { return(new ReturnMessage( returnValue, null, 0, mcm.LogicalCallContext, mcm)); } else { return(new ReturnMessage( returnValue, outAuguments, outAuguments.Length, mcm.LogicalCallContext, mcm)); } } catch (Exception e) { return(new ReturnMessage(e, mcm)); } } else { return(nextSink.SyncProcessMessage(msg)); } } return(nextSink.SyncProcessMessage(msg)); }