public SubSeqEnumerator(IImSeq <T> seq, int start, int length) { this.seq = seq; this.start = start; this.length = length; i = -1; }
private static Map <T, int> MergeMaps <T>(IImSeq <IMap <T, int> > maps) { var res = new Map <T, int>(); foreach (var map in maps) { foreach (var kv in map) { var c = default(int); if (res.TryGetValue(kv.Key, out c)) { res[kv.Key] = Math.Min(kv.Value, c); } else { res.Add(kv.Key, kv.Value); } } foreach (var kv in res) { if (!map.ContainsKey(kv.Key)) { res[kv.Key] = -1; } } } return(res); }
public CallContext(CompilationEnvironment outerCompEnv, CompilationEnvironment inlinedCompEnv, IImSeq <Expression> arguments) { var paramMap = new Map <JST.Identifier, int>(); for (var i = 0; i < inlinedCompEnv.Method.Arity; i++) { paramMap.Add(inlinedCompEnv.ValueParameterIds[i], i); } Parameters = paramMap; var argumentEffects = new Seq <JST.Effects>(inlinedCompEnv.Method.Arity); SeenParameters = new Seq <bool?>(inlinedCompEnv.Method.Arity); AllArgumentEffects = JST.Effects.Bottom; var allReadOnly = true; foreach (var e in arguments) { var fxCtxt = new JST.EffectsContext(null); e.AccumEffects(fxCtxt, null, null); argumentEffects.Add(fxCtxt.AccumEffects); AllArgumentEffects = AllArgumentEffects.Lub(fxCtxt.AccumEffects); if (!fxCtxt.AccumEffects.IsReadOnly) { allReadOnly = false; } SeenParameters.Add(e.IsValue(outerCompEnv) ? default(bool?) : false); } ArgumentEffects = argumentEffects; AllReadOnly = allReadOnly; IsOk = true; }
public void Merge(IImSeq <Usage> altUsages) { foreach (var kv in MergeMaps(altUsages.Select(u => u.Assemblies).ToSeq())) { Add(Assemblies, kv.Key, kv.Value, true); } foreach (var kv in MergeMaps(altUsages.Select(u => u.Types).ToSeq())) { Add(Types, kv.Key, kv.Value, true); } foreach (var kv in MergeMaps(altUsages.Select(u => u.Methods).ToSeq())) { Add(Methods, kv.Key, kv.Value, true); } foreach (var kv in MergeMaps(altUsages.Select(u => u.Fields).ToSeq())) { Add(Fields, kv.Key, kv.Value, true); } foreach (var kv in MergeMaps(altUsages.Select(u => u.Variables).ToSeq())) { Add(Variables, kv.Key, kv.Value, true); } foreach (var kv in MergeMaps(altUsages.Select(u => u.VariablePointers).ToSeq())) { Add(VariablePointers, kv.Key, kv.Value, true); } }
public override Signature PrimSubstitute(IImSeq <TypeRef> typeBoundArguments, IImSeq <TypeRef> methodBoundArguments) { var newParameters = Parameters.Select(t => t.PrimSubstitute(typeBoundArguments, methodBoundArguments)).ToSeq(); var newResult = Result == null ? default(TypeRef) : Result.PrimSubstitute(typeBoundArguments, methodBoundArguments); return(new PropertySignature(Name, IsStatic, newParameters, newResult)); }
public PropertySignature(string name, bool isStatic, IImSeq <TypeRef> parameters, TypeRef result) : base(name) { IsStatic = isStatic; Parameters = parameters ?? Constants.EmptyTypeRefs; Result = result; }
private TypeCompilerEnvironment (CST.Global global, IImSeq <CST.SkolemDef> skolemDefs, CST.AssemblyDef assembly, CST.TypeDef type, IImSeq <CST.TypeRef> typeBoundArguments, CompilerEnvironment env, JST.NameSupply nameSupply, JST.Identifier rootId, JST.Identifier assemblyId, JST.Identifier typeId, IImSeq <JST.Identifier> typeBoundTypeParameterIds, TypeTrace typeTrace) : base( global, skolemDefs, assembly, type, typeBoundArguments) { this.env = env; NameSupply = nameSupply; this.rootId = rootId; this.assemblyId = assemblyId; this.typeId = typeId; TypeBoundTypeParameterIds = typeBoundTypeParameterIds; boundAssemblies = new Map <CST.AssemblyName, JST.Expression>(); boundTypes = new Map <CST.TypeRef, ExpressionAndPhase>(); this.typeTrace = typeTrace; }
public CallContext(IImSeq <Identifier> parameters, IImSeq <Expression> arguments, Func <Expression, bool> isValue) { var paramMap = new Map <Identifier, int>(); for (var i = 0; i < parameters.Count; i++) { paramMap.Add(parameters[i], i); } Parameters = paramMap; var argumentEffects = new Seq <Effects>(parameters.Count); SeenParameters = new Seq <bool>(parameters.Count); var allReadOnly = true; AllArgumentEffects = Effects.Bottom; foreach (var e in arguments) { var fxCtxt = new EffectsContext(isValue); e.AccumEffects(fxCtxt, null, null); argumentEffects.Add(fxCtxt.AccumEffects); AllArgumentEffects = AllArgumentEffects.Lub(fxCtxt.AccumEffects); if (!fxCtxt.AccumEffects.IsReadOnly) { allReadOnly = false; } SeenParameters.Add(false); } ArgumentEffects = argumentEffects; AllReadOnly = allReadOnly; IsOk = true; }
internal override TypeRef PrimSubstitute(IImSeq <TypeRef> typeBoundArguments, IImSeq <TypeRef> methodBoundArguments) { switch (ParameterFlavor) { case ParameterFlavor.Type: if (typeBoundArguments == null) { throw new InvalidOperationException("unexpected free type-bound type parameter"); } if (Index >= typeBoundArguments.Count) { throw new InvalidOperationException("invalid type type parameter index"); } return(typeBoundArguments[Index]); case ParameterFlavor.Method: if (methodBoundArguments == null) { throw new InvalidOperationException("unexpected free method-bound type parameter"); } if (Index >= methodBoundArguments.Count) { throw new InvalidOperationException("invalid method type parameter index"); } return(methodBoundArguments[Index]); default: throw new ArgumentOutOfRangeException(); } }
public EventDef(IImSeq <Annotation> annotations, ISeq <CustomAttribute> customAttributes, string name, bool isStatic, MethodSignature add, MethodSignature remove, TypeRef handlerType) : base(annotations, customAttributes, name, isStatic) { Add = add; Remove = remove; HandlerType = handlerType; }
protected MemberDef(IImSeq <Annotation> annotations, ISeq <CustomAttribute> customAttributes, string name, bool isStatic) { Annotations = annotations ?? Constants.EmptyAnnotations; CustomAttributes = customAttributes ?? new Seq <CustomAttribute>(); Name = name; IsStatic = isStatic; }
public PropertyDef(IImSeq <Annotation> annotations, ISeq <CustomAttribute> customAttributes, string name, bool isStatic, MethodSignature get, MethodSignature set, TypeRef fieldType) : base(annotations, customAttributes, name, isStatic) { Get = get; Set = set; FieldType = fieldType; }
public TryBasicBlock(int id, TryBasicBlock parent, MachineState initialState) : base(id, new Instructions(initialState)) { Parent = parent; Handlers = new Seq <TBBHandler>(); targetsCache = null; }
private TypeCompilerEnvironment (CST.Global global, IImSeq<CST.SkolemDef> skolemDefs, CST.AssemblyDef assembly, CST.TypeDef type, IImSeq<CST.TypeRef> typeBoundArguments, CompilerEnvironment env, JST.NameSupply nameSupply, JST.Identifier rootId, JST.Identifier assemblyId, JST.Identifier typeId, IImSeq<JST.Identifier> typeBoundTypeParameterIds, TypeTrace typeTrace) : base( global, skolemDefs, assembly, type, typeBoundArguments) { this.env = env; NameSupply = nameSupply; this.rootId = rootId; this.assemblyId = assemblyId; this.typeId = typeId; TypeBoundTypeParameterIds = typeBoundTypeParameterIds; boundAssemblies = new Map<CST.AssemblyName, JST.Expression>(); boundTypes = new Map<CST.TypeRef, ExpressionAndPhase>(); this.typeTrace = typeTrace; }
public static bool AllCommutable(IImSeq <Effects> effects) { // Ideally, we'd just check all pairwise combinations, but that would be to expensive. // If any exceptions, no luck. Otherwise, if all args, locals and the head are read-only, we are good. var noneWrite = true; foreach (var e in effects) { if (e.MayThrow.Value) { return(false); } if (!e.IsReadOnly) { noneWrite = false; } } if (noneWrite) { return(true); } // At this point we could check if reads and writes are all to distinct locations, but for now // lets just give up return(false); }
protected MemberDef(IImSeq<Annotation> annotations, ISeq<CustomAttribute> customAttributes, string name, bool isStatic) { Annotations = annotations ?? Constants.EmptyAnnotations; CustomAttributes = customAttributes ?? new Seq<CustomAttribute>(); Name = name; IsStatic = isStatic; }
public CompilingAugmentedAssemblyMessage(AssemblyName originalAssemblyName, string originalFileName, AssemblyName renamedAssemblyName, IImSeq <string> augmentedFileNames) : base(null, Severity.Warning, "1010") { OriginalAssemblyName = originalAssemblyName; OriginalFileName = originalFileName; RenamedAssemblyName = renamedAssemblyName; AugmentedFileNames = augmentedFileNames; }
public TypeRef ApplyTo(IImSeq <Annotation> annotations, params TypeRef[] arguments) { if (Arguments.Count > 0) { throw new InvalidOperationException("cannot apply partial-application to type arguments"); } return(new NamedTypeRef(annotations, QualifiedTypeName, arguments)); }
public override TypeRef WithArguments(IImSeq <TypeRef> newArguments) { if (newArguments != null && newArguments.Count > 0) { throw new InvalidOperationException("type parameters cannot be higher-kinded"); } return(this); }
public MethodSignature(string name, bool isStatic, int typeArity, IImSeq <TypeRef> parameters, TypeRef result) : base(name) { IsStatic = isStatic; TypeArity = typeArity; Parameters = parameters ?? Constants.EmptyTypeRefs; Result = result; }
public override TypeRef WithArguments(IImSeq <TypeRef> arguments) { if (arguments != null && arguments.Count > 0) { throw new InvalidOperationException("skolem types cannot be higher-kinded"); } return(this); }
public override void Append(CSTWriter w, IImSeq <TypeRef> methodTypeArguments) { if (methodTypeArguments != null) { throw new InvalidOperationException("signature does not accept type arguments"); } w.Append("event "); w.AppendName(Name); }
public override void FixupTargets(BasicBlock origBlock, BasicBlock newBlock) { targetsCache = null; if (Body.Equals(origBlock)) { Body = newBlock; } foreach (var h in Handlers) { h.FixupTargets(origBlock, newBlock); } }
protected TranslationContext(TranslationContext parent, int start) { Parent = parent; Start = start; ResultType = parent.ResultType; Instructions = parent.Instructions; OffsetToIndex = parent.OffsetToIndex; TryOffsets = parent.TryOffsets; IndexToHandler = parent.IndexToHandler; IndexToFilter = parent.IndexToFilter; Handlers = parent.Handlers; }
public Seq(IImSeq <T> ts) { if (ts == null || ts.Count == 0) { elems = null; c = 0; } else { elems = new T[ts.Count]; ts.CopyTo(elems, 0); c = ts.Count; } }
public AssemblyDef(Global global, IImSeq<Annotation> annotations, ISeq<CustomAttribute> customAttributes, AssemblyName name, IImSeq<AssemblyName> references, IImSeq<TypeDef> types, MethodRef entryPoint) { Annotations = annotations ?? Constants.EmptyAnnotations; CustomAttributes = customAttributes ?? new Seq<CustomAttribute>(); Name = name; References = references ?? Constants.EmptyStrongAssemblyNames; Types = types ?? Constants.EmptyTypeDefs; EntryPoint = entryPoint; nameToTypeDefCache = new Map<TypeName, TypeDef>(); if (types != null) { foreach (var t in types) nameToTypeDefCache.Add(t.EffectiveName(global), t); } }
public override void FixupTargets(BasicBlock origBlock, BasicBlock newBlock) { for (var i = 0; i < CaseTargets.Count; i++) { if (CaseTargets[i].Equals(origBlock)) { CaseTargets[i] = newBlock; targetsCache = null; } } if (Fallthrough.Equals(origBlock)) { Fallthrough = newBlock; targetsCache = null; } }
public override MemberRef PrimSubstitute(IImSeq <TypeRef> typeBoundArguments, IImSeq <TypeRef> methodBoundArguments) { var methodTypeArguments = default(Seq <TypeRef>); if (MethodTypeArguments.Count > 0) { methodTypeArguments = MethodTypeArguments.Select(type => type.PrimSubstitute(typeBoundArguments, methodBoundArguments)). ToSeq(); } return(new MethodRef (Annotations, DefiningType.PrimSubstitute(typeBoundArguments, methodBoundArguments), signature, methodTypeArguments)); }
public static TypeRef CodePointerFrom(Global global, IImSeq <TypeRef> arguments, TypeRef result) { if (result == null) { return(new NamedTypeRef (global.CodePointerTypeConstructorName(CodePointerFlavor.Action, arguments.Count), arguments)); } else { var allArguments = arguments.ToSeq(); allArguments.Add(result); return(new NamedTypeRef (global.CodePointerTypeConstructorName(CodePointerFlavor.Function, allArguments.Count), allArguments)); } }
public override void Append(CSTWriter w, IImSeq <TypeRef> methodTypeArguments) { w.Append("method "); w.Append(IsStatic ? "static " : "instance "); w.AppendName(Name); if (methodTypeArguments != null) { if (methodTypeArguments.Count > 0) { w.Append('<'); for (var i = 0; i < methodTypeArguments.Count; i++) { if (i > 0) { w.Append(','); } methodTypeArguments[i].Append(w); } w.Append('>'); } } else { if (TypeArity > 0) { w.Append("<["); w.Append(TypeArity); w.Append("]>"); } } w.Append('('); for (var i = 0; i < Parameters.Count; i++) { if (i > 0) { w.Append(','); } Parameters[i].Append(w); } w.Append(')'); if (Result != null) { w.Append(':'); Result.Append(w); } }
public AssemblyDef(Global global, IImSeq <Annotation> annotations, ISeq <CustomAttribute> customAttributes, AssemblyName name, IImSeq <AssemblyName> references, IImSeq <TypeDef> types, MethodRef entryPoint) { Annotations = annotations ?? Constants.EmptyAnnotations; CustomAttributes = customAttributes ?? new Seq <CustomAttribute>(); Name = name; References = references ?? Constants.EmptyStrongAssemblyNames; Types = types ?? Constants.EmptyTypeDefs; EntryPoint = entryPoint; nameToTypeDefCache = new Map <TypeName, TypeDef>(); if (types != null) { foreach (var t in types) { nameToTypeDefCache.Add(t.EffectiveName(global), t); } } }
private MethodCompilerEnvironment (CST.Global global, IImSeq <CST.SkolemDef> skolemDefs, CST.AssemblyDef assembly, CST.TypeDef type, IImSeq <CST.TypeRef> typeBoundArguments, CST.MethodDef method, IImSeq <CST.TypeRef> methodBoundArguments, IMap <JST.Identifier, CST.Variable> variables, IImSeq <JST.Identifier> valueParameterIds, IImSeq <JST.Identifier> localIds, CompilerEnvironment env, JST.NameSupply nameSupply, JST.Identifier rootId, JST.Identifier assemblyId, JST.Identifier typeDefinitionId, JST.Identifier methodId, IImSeq <JST.Identifier> typeBoundTypeParameterIds, IImSeq <JST.Identifier> methodBoundTypeParameterIds, TypeTrace typeTrace) : base( global, skolemDefs, assembly, type, typeBoundArguments, method, methodBoundArguments, variables, valueParameterIds, localIds) { this.env = env; NameSupply = nameSupply; this.rootId = rootId; this.assemblyId = assemblyId; this.typeDefinitionId = typeDefinitionId; MethodId = methodId; TypeBoundTypeParameterIds = typeBoundTypeParameterIds; MethodBoundTypeParameterIds = methodBoundTypeParameterIds; boundAssemblies = new Map <CST.AssemblyName, JST.Expression>(); boundTypes = new Map <CST.TypeRef, JST.Expression>(); boundVariablePointers = new Map <JST.Identifier, JST.Expression>(); this.typeTrace = typeTrace; }
protected static int CompareLists(IImSeq <TypeRef> left, IImSeq <TypeRef> right) { var i = left.Count.CompareTo(right.Count); if (i != 0) { return(i); } for (var j = 0; j < left.Count; j++) { i = left[j].CompareTo(right[j]); if (i != 0) { return(i); } } return(0); }
public MethodDef (IImSeq <Annotation> annotations, ISeq <CustomAttribute> customAttributes, string name, bool isStatic, IImSeq <ParameterTypeDef> typeParameters, IImSeq <ParameterOrLocalOrResult> valueParameters, ParameterOrLocalOrResult result, MethodStyle methodStyle, bool hasNewSlot, MethodCodeFlavor codeFlavor, bool isSyncronized, bool noInlining, bool isInitLocals, IImSeq <ParameterOrLocalOrResult> locals, PE.MethodBody methodBody) : base(annotations, customAttributes, name, isStatic) { TypeParameters = typeParameters ?? Constants.EmptyParameterTypeDefs; ValueParameters = valueParameters ?? Constants.EmptyParameterOrLocals; Result = result; this.methodStyle = methodStyle; HasNewSlot = hasNewSlot; CodeFlavor = codeFlavor; var noBody = methodStyle == MethodStyle.Abstract || CodeFlavor != MethodCodeFlavor.Managed; IsSyncronized = isSyncronized; NoInlining = noInlining; IsInitLocals = isInitLocals; if (noBody && locals != null && locals.Count > 0) { throw new InvalidOperationException("unexpected locals in method"); } Locals = locals ?? Constants.EmptyParameterOrLocals; if (noBody && methodBody != null) { throw new InvalidOperationException("unexpected instructions in extern method definition"); } if (!noBody && methodBody == null) { throw new InvalidOperationException("missing instructions in method definition"); } MethodBody = methodBody; }
public TranslationContext(TypeRef resultType, PE.Instruction[] instructions, IImSeq<PE.ExceptionHandlingClause> handlers) { Parent = null; Start = 0; ResultType = resultType; Instructions = instructions; Handlers = handlers; var offsetToIndex = new Map<int, int>(); for (var i = 0; i < instructions.Length; i++) { if (offsetToIndex.ContainsKey(instructions[i].Offset)) throw new InvalidOperationException("instructions share same offset"); offsetToIndex.Add(instructions[i].Offset, i); } OffsetToIndex = offsetToIndex; var tryOffsets = new Set<int>(); var indexToHandler = new Map<int, PE.ExceptionHandlingClause>(); var indexToFilter = new Map<int, PE.ExceptionHandlingClause>(); foreach (var ehc in handlers) { if (!tryOffsets.Contains(ehc.TryOffset)) tryOffsets.Add(ehc.TryOffset); var i = OffsetToIndex[ehc.HandlerOffset]; indexToHandler.Add(i, ehc); if (ehc.Flags == PE.CorILExceptionClause.Filter) { var j = OffsetToIndex[ehc.FilterOffset]; indexToHandler.Add(j, ehc); } } TryOffsets = tryOffsets; IndexToHandler = indexToHandler; IndexToFilter = indexToFilter; }
public CallContext(CompilationEnvironment outerCompEnv, CompilationEnvironment inlinedCompEnv, IImSeq<Expression> arguments) { var paramMap = new Map<JST.Identifier, int>(); for (var i = 0; i < inlinedCompEnv.Method.Arity; i++) paramMap.Add(inlinedCompEnv.ValueParameterIds[i], i); Parameters = paramMap; var argumentEffects = new Seq<JST.Effects>(inlinedCompEnv.Method.Arity); SeenParameters = new Seq<bool?>(inlinedCompEnv.Method.Arity); AllArgumentEffects = JST.Effects.Bottom; var allReadOnly = true; foreach (var e in arguments) { var fxCtxt = new JST.EffectsContext(null); e.AccumEffects(fxCtxt, null, null); argumentEffects.Add(fxCtxt.AccumEffects); AllArgumentEffects = AllArgumentEffects.Lub(fxCtxt.AccumEffects); if (!fxCtxt.AccumEffects.IsReadOnly) allReadOnly = false; SeenParameters.Add(e.IsValue(outerCompEnv) ? default(bool?) : false); } ArgumentEffects = argumentEffects; AllReadOnly = allReadOnly; IsOk = true; }
public CallContext(IImSeq<Identifier> parameters, IImSeq<Expression> arguments, Func<Expression, bool> isValue) { var paramMap = new Map<Identifier, int>(); for (var i = 0; i < parameters.Count; i++) paramMap.Add(parameters[i], i); Parameters = paramMap; var argumentEffects = new Seq<Effects>(parameters.Count); SeenParameters = new Seq<bool>(parameters.Count); var allReadOnly = true; AllArgumentEffects = Effects.Bottom; foreach (var e in arguments) { var fxCtxt = new EffectsContext(isValue); e.AccumEffects(fxCtxt, null, null); argumentEffects.Add(fxCtxt.AccumEffects); AllArgumentEffects = AllArgumentEffects.Lub(fxCtxt.AccumEffects); if (!fxCtxt.AccumEffects.IsReadOnly) allReadOnly = false; SeenParameters.Add(false); } ArgumentEffects = argumentEffects; AllReadOnly = allReadOnly; IsOk = true; }
public MachineState CloneForward(IImSeq<StackEntryState> stack) { return new MachineState(RootEnv, nArgs, nLocals, innerState.Value.CloneForward(stack)); }
public InnerMachineState CloneWithArgLocalPointsTo(IImSeq<StackEntryState> stack, ArgLocal argLocal, int index, PointsTo pointsTo) { return new InnerMachineState(stack, ArgsLocalsState.CloneWithArgLocalPointsTo(argLocal, index, pointsTo)); }
public JST.Expression AppendImport(JST.NameSupply nameSupply, JST.Identifier rootId, CST.AssemblyDef assemblyDef, CST.TypeDef typeDef, CST.MethodDef methodDef, ISeq<JST.Statement> body, IImSeq<JST.Expression> arguments) { var ctxt = CST.MessageContextBuilders.Member(env.Global, assemblyDef, typeDef, methodDef); CheckParameterAndReturnTypesAreImportableExportable(assemblyDef, typeDef, methodDef); var script = default(JST.Expression); attributeHelper.GetValueFromMethod (assemblyDef, typeDef, methodDef, attributeHelper.ImportAttributeRef, attributeHelper.TheScriptProperty, true, false, ref script); if (!methodDef.IsStatic && methodDef.IsConstructor) { // Constructor if (script == null) { var creation = default(Creation); attributeHelper.GetValueFromMethod (assemblyDef, typeDef, methodDef, attributeHelper.ImportAttributeRef, attributeHelper.TheCreationProperty, true, false, ref creation); switch (creation) { case Creation.Constructor: script = PrefixName(assemblyDef, typeDef, methodDef, null, false); break; case Creation.Object: if (arguments.Count > 0) { env.Log (new InvalidInteropMessage (ctxt, "imported constructors for object literals cannot have arguments")); throw new DefinitionException(); } script = Constants.Object.ToE(); break; case Creation.Array: script = Constants.Array.ToE(); break; default: throw new ArgumentOutOfRangeException(); } var call = AppendFinalImport (nameSupply, rootId, assemblyDef, typeDef, methodDef, script, body, arguments); return new JST.NewExpression(call); } else if (script is JST.FunctionExpression) return AppendFinalImport (nameSupply, rootId, assemblyDef, typeDef, methodDef, script, body, arguments); else { script = PrefixName(assemblyDef, typeDef, methodDef, script, false); var call = AppendFinalImport (nameSupply, rootId, assemblyDef, typeDef, methodDef, script, body, arguments); return new JST.NewExpression(call); } } else { var outer = typeDef.OuterPropertyOrEvent(methodDef.MethodSignature); if (outer != null) { var isOnMethod = attributeHelper.MethodHasAttribute (assemblyDef, typeDef, methodDef, attributeHelper.ImportAttributeRef, false, false); var localScript = default(JST.Expression); if (isOnMethod) attributeHelper.GetValueFromMethod (assemblyDef, typeDef, methodDef, attributeHelper.ImportAttributeRef, attributeHelper.TheScriptProperty, false, false, ref localScript); switch (outer.Flavor) { case CST.MemberDefFlavor.Property: { var propDef = (CST.PropertyDef)outer; if (propDef.Get != null && methodDef.Signature.Equals(propDef.Get)) { // Getter if (isOnMethod) { script = PrefixName (assemblyDef, typeDef, methodDef, GetterSetterAdderRemoverNameFromMethod (assemblyDef, typeDef, methodDef, "get", localScript), false); return AppendFinalImport (nameSupply, rootId, assemblyDef, typeDef, methodDef, script, body, arguments); } else if (script != null && script is JST.FunctionExpression) { env.Log (new InvalidInteropMessage (ctxt, "property import script cannot be a function")); throw new DefinitionException(); } else { if (script == null && arguments.Count == 2 && !methodDef.IsStatic) return new JST.IndexExpression(arguments[0], arguments[1]); else { script = PrefixName (assemblyDef, typeDef, methodDef, RecasePropertyEvent(assemblyDef, typeDef, methodDef, script), false); if (methodDef.IsStatic && arguments.Count == 0) return script; else if (!methodDef.IsStatic && arguments.Count == 1) return JST.Expression.Dot (arguments[0], JST.Expression.ExplodePath(script)); else { env.Log (new InvalidInteropMessage (ctxt, "additional getter parameters not supported for default getters")); throw new DefinitionException(); } } } } else if (propDef.Set != null && methodDef.Signature.Equals(propDef.Set)) { // Setter if (isOnMethod) { script = PrefixName (assemblyDef, typeDef, methodDef, GetterSetterAdderRemoverNameFromMethod (assemblyDef, typeDef, methodDef, "set", localScript), false); return AppendFinalImport (nameSupply, rootId, assemblyDef, typeDef, methodDef, script, body, arguments); } else if (script != null && script is JST.FunctionExpression) { env.Log (new InvalidInteropMessage (ctxt, "property import script cannot be a function")); throw new DefinitionException(); } else { if (script == null && arguments.Count == 3 && !methodDef.IsStatic) return new JST.BinaryExpression (new JST.IndexExpression(arguments[0], arguments[1]), JST.BinaryOp.Assignment, arguments[2]); else { script = PrefixName (assemblyDef, typeDef, methodDef, RecasePropertyEvent(assemblyDef, typeDef, methodDef, script), false); if (methodDef.IsStatic && arguments.Count == 1) return new JST.BinaryExpression (script, JST.BinaryOp.Assignment, arguments[0]); else if (!methodDef.IsStatic && arguments.Count == 2) return new JST.BinaryExpression (JST.Expression.Dot(arguments[0], JST.Expression.ExplodePath(script)), JST.BinaryOp.Assignment, arguments[1]); else { env.Log (new InvalidInteropMessage (ctxt, "additional setter parameters not supported for default setters")); throw new DefinitionException(); } } } } else throw new InvalidOperationException(); } case CST.MemberDefFlavor.Event: { var eventDef = (CST.EventDef)outer; // XREF1201 if (eventDef.Add != null && methodDef.Signature.Equals(eventDef.Add)) { // Adder if (isOnMethod) { script = PrefixName (assemblyDef, typeDef, methodDef, GetterSetterAdderRemoverNameFromMethod (assemblyDef, typeDef, methodDef, "add", localScript), false); return AppendFinalImport (nameSupply, rootId, assemblyDef, typeDef, methodDef, script, body, arguments); } else if (script != null && script is JST.FunctionExpression) { env.Log (new InvalidInteropMessage(ctxt, "event import script cannot be a function")); throw new DefinitionException(); } else { // The delegate argument has already taken account of the combine, so // just a field assignment script = PrefixName (assemblyDef, typeDef, methodDef, RecasePropertyEvent(assemblyDef, typeDef, methodDef, script), false); if (methodDef.IsStatic && arguments.Count == 1) return new JST.BinaryExpression(script, JST.BinaryOp.Assignment, arguments[0]); else if (!methodDef.IsStatic && arguments.Count == 2) return new JST.BinaryExpression (JST.Expression.Dot(arguments[0], JST.Expression.ExplodePath(script)), JST.BinaryOp.Assignment, arguments[1]); else throw new InvalidOperationException("mismatched event adder arity"); } } else if (eventDef.Remove != null && methodDef.Signature.Equals(eventDef.Remove)) { // Remover if (isOnMethod) { script = PrefixName (assemblyDef, typeDef, methodDef, GetterSetterAdderRemoverNameFromMethod (assemblyDef, typeDef, methodDef, "remove", localScript), false); return AppendFinalImport (nameSupply, rootId, assemblyDef, typeDef, methodDef, script, body, arguments); } else if (script != null && script is JST.FunctionExpression) { env.Log (new InvalidInteropMessage(ctxt, "event import script cannot be a function")); throw new DefinitionException(); } else { // The delegate argument has already taken account of the delete, so // just a field assignment script = PrefixName (assemblyDef, typeDef, methodDef, RecasePropertyEvent(assemblyDef, typeDef, methodDef, script), false); if (methodDef.IsStatic && arguments.Count == 1) return new JST.BinaryExpression(script, JST.BinaryOp.Assignment, arguments[0]); else if (!methodDef.IsStatic && arguments.Count == 2) return new JST.BinaryExpression (JST.Expression.Dot(arguments[0], JST.Expression.ExplodePath(script)), JST.BinaryOp.Assignment, arguments[1]); else throw new InvalidOperationException("mismatched event remover arity"); } } else throw new InvalidOperationException(); } case CST.MemberDefFlavor.Field: case CST.MemberDefFlavor.Method: throw new InvalidOperationException("outer is not property or event"); default: throw new ArgumentOutOfRangeException(); } } else { // Normal method script = PrefixName (assemblyDef, typeDef, methodDef, RecaseMethod(assemblyDef, typeDef, methodDef, script), false); return AppendFinalImport (nameSupply, rootId, assemblyDef, typeDef, methodDef, script, body, arguments); } } }
// Take acccount of: // - PassRootAsArgument // - PassInstanceAsArgument // - InlineParamsArray private JST.Expression AppendFinalImport(JST.NameSupply nameSupply, JST.Identifier rootId, CST.AssemblyDef assemblyDef, CST.TypeDef typeDef, CST.MethodDef methodDef, JST.Expression script, ISeq<JST.Statement> body, IImSeq<JST.Expression> arguments) { var isInstanceMethod = !(methodDef.IsStatic || methodDef.IsConstructor); var scriptExpectsRoot = default(bool); attributeHelper.GetValueFromMethod (assemblyDef, typeDef, methodDef, attributeHelper.ImportAttributeRef, attributeHelper.ThePassRootAsArgumentProperty, true, false, ref scriptExpectsRoot); var passInstAsArg = default(bool); attributeHelper.GetValueFromMethod (assemblyDef, typeDef, methodDef, attributeHelper.ImportAttributeRef, attributeHelper.ThePassInstanceAsArgumentProperty, true, false, ref passInstAsArg); var instanceIsThis = isInstanceMethod && !passInstAsArg; var inlineParams = default(bool); attributeHelper.GetValueFromMethod (assemblyDef, typeDef, methodDef, attributeHelper.ImportAttributeRef, attributeHelper.TheInlineParamsArrayProperty, true, false, ref inlineParams); var lastArgIsParamsArray = methodDef.HasParamsArray(rootEnv) && inlineParams; var funcScript = script as JST.FunctionExpression; var nextArg = 0; var instArg = default(JST.Expression); if (instanceIsThis) { // Instance argument will be the first arg to 'call' or 'apply', or the target of a '.' call. instArg = arguments[nextArg++]; if (lastArgIsParamsArray && !instArg.IsDuplicatable) { // Make sure instance argument is evaluated before the remaining arguments var instId = nameSupply.GenSym(); body.Add(JST.Statement.Var(instId, instArg)); instArg = instId.ToE(); } } else { if (lastArgIsParamsArray) instArg = new JST.NullExpression(); } var knownArgs = 0; var call = default(JST.Expression); if (lastArgIsParamsArray) { // We mush build script args at runtime var argsId = nameSupply.GenSym(); body.Add(JST.Statement.Var(argsId, new JST.ArrayLiteral())); if (scriptExpectsRoot) { body.Add(JST.Statement.DotCall(argsId.ToE(), Constants.push, rootId.ToE())); knownArgs++; } while (nextArg < arguments.Count - 1) { body.Add(JST.Statement.DotCall(argsId.ToE(), Constants.push, arguments[nextArg++])); knownArgs++; } var arrArg = arguments[nextArg]; if (!arrArg.IsDuplicatable) { var arrId = nameSupply.GenSym(); body.Add(JST.Statement.Var(arrId, arrArg)); arrArg = arrId.ToE(); } var iId = nameSupply.GenSym(); body.Add (new JST.IfStatement (JST.Expression.IsNotNull(arrArg), new JST.Statements (new JST.ForStatement (new JST.ForVarLoopClause (iId, new JST.NumericLiteral(0), new JST.BinaryExpression (iId.ToE(), JST.BinaryOp.LessThan, JST.Expression.Dot(arrArg, Constants.length)), new JST.UnaryExpression(iId.ToE(), JST.UnaryOp.PostIncrement)), new JST.Statements (JST.Statement.DotCall (argsId.ToE(), Constants.push, new JST.IndexExpression(arrArg, iId.ToE()))))))); if (funcScript != null) { // var args = ...; var x = script; x.apply(this/null, args) var scriptId = nameSupply.GenSym(); body.Add(JST.Statement.Var(scriptId, funcScript)); call = JST.Expression.DotCall(scriptId.ToE(), Constants.apply, instArg, argsId.ToE()); } else { if (instanceIsThis) { // var args = ...; (this.script).apply(this, args); call = JST.Expression.DotCall (JST.Expression.Dot(instArg, JST.Expression.ExplodePath(script)), Constants.apply, instArg, argsId.ToE()); } else { // var args = ...; script.apply(null, args) call = JST.Expression.DotCall(script, Constants.apply, instArg, argsId.ToE()); } } } else { var callArgs = new Seq<JST.Expression>(); if (instanceIsThis && funcScript != null) callArgs.Add(instArg); if (scriptExpectsRoot) { callArgs.Add(rootId.ToE()); knownArgs++; } while (nextArg < arguments.Count) { callArgs.Add(arguments[nextArg++]); knownArgs++; } if (instanceIsThis) { if (funcScript != null) { // var x = script; x.call(this, arg1, ..., argn) var scriptId = nameSupply.GenSym(); body.Add(JST.Statement.Var(scriptId, funcScript)); call = JST.Expression.DotCall(scriptId.ToE(), Constants.call, callArgs); } else // this.script(arg1, ..., angn) call = new JST.CallExpression (JST.Expression.Dot(instArg, JST.Expression.ExplodePath(script)), callArgs); } else // script(arg1, ..., argn) call = new JST.CallExpression(script, callArgs); } if (funcScript != null) { if (funcScript.Parameters.Count < knownArgs) { var ctxt = CST.MessageContextBuilders.Member(env.Global, assemblyDef, typeDef, methodDef); env.Log(new InvalidInteropMessage(ctxt, "script accepts too few arguments")); throw new DefinitionException(); } } return call; }
public CompilingAugmentedAssemblyMessage(AssemblyName originalAssemblyName, string originalFileName, AssemblyName renamedAssemblyName, IImSeq<string> augmentedFileNames) : base(null, Severity.Warning, "1010") { OriginalAssemblyName = originalAssemblyName; OriginalFileName = originalFileName; RenamedAssemblyName = renamedAssemblyName; AugmentedFileNames = augmentedFileNames; }
public ParameterOrLocalOrResult(TypeRef type) { Annotations = Constants.EmptyAnnotations; CustomAttributes = new Seq<CustomAttribute>(); Type = type; }
public PropertyDef(IImSeq<Annotation> annotations, ISeq<CustomAttribute> customAttributes, string name, bool isStatic, MethodSignature get, MethodSignature set, TypeRef fieldType) : base(annotations, customAttributes, name, isStatic) { Get = get; Set = set; FieldType = fieldType; }
// Return the index of the instruction at or before i in block which begins the sequence which leaves // n entries on top of the stack, or -1 if top n entries were not placed by a single contiguous series // of instructions private int FindStartOfStackPrefix(IImSeq<Instruction> instructions, int i, int n) { while (i >= 0) { var pops = instructions[i].Pops; var pushes = instructions[i].Pushes; if (pops == 0 && pushes == 1 && n == 1) return i; n -= pushes; if (n < 0) return -1; n += pops; i--; } return -1; }
public override MemberRef PrimReference(Global global, AssemblyDef assemblyDef, TypeDef typeDef, IImSeq<TypeRef> typeBoundArguments) { return new PolymorphicMethodRef (typeDef.PrimReference(global, assemblyDef, typeBoundArguments), Name, IsStatic, TypeArity, ValueParameters.Select(p => p.Type).ToSeq(), Result == null ? null : Result.Type); }
// Array of custom attributes private JST.Expression CustomAttributesExpression(Seq<JST.Statement> body, TypeCompilerEnvironment innerTypeCompEnv, MessageContext ctxt, IImSeq<CST.CustomAttribute> attributes) { var objs = new Seq<JST.Expression>(); foreach (var attribute in attributes) { var obj = CustomAttributeExpression(body, innerTypeCompEnv, ctxt, attribute); if (obj != null) objs.Add(obj); } return new JST.ArrayLiteral(objs); }
// ---------------------------------------------------------------------- // Methods // ---------------------------------------------------------------------- public JST.Expression MethodCallExpression(CST.MethodRef methodRef, JST.NameSupply localNameSupply, bool isFactory, IImSeq<JST.Expression> arguments) { return env.JSTHelpers.DefaultMethodCallExpression(this, localNameSupply, methodRef, isFactory, arguments); }
public MethodDef (IImSeq<Annotation> annotations, ISeq<CustomAttribute> customAttributes, string name, bool isStatic, IImSeq<ParameterTypeDef> typeParameters, IImSeq<ParameterOrLocalOrResult> valueParameters, ParameterOrLocalOrResult result, MethodStyle methodStyle, bool hasNewSlot, MethodCodeFlavor codeFlavor, bool isSyncronized, bool noInlining, bool isInitLocals, IImSeq<ParameterOrLocalOrResult> locals, PE.MethodBody methodBody) : base(annotations, customAttributes, name, isStatic) { TypeParameters = typeParameters ?? Constants.EmptyParameterTypeDefs; ValueParameters = valueParameters ?? Constants.EmptyParameterOrLocals; Result = result; this.methodStyle = methodStyle; HasNewSlot = hasNewSlot; CodeFlavor = codeFlavor; var noBody = methodStyle == MethodStyle.Abstract || CodeFlavor != MethodCodeFlavor.Managed; IsSyncronized = isSyncronized; NoInlining = noInlining; IsInitLocals = isInitLocals; if (noBody && locals != null && locals.Count > 0) throw new InvalidOperationException("unexpected locals in method"); Locals = locals ?? Constants.EmptyParameterOrLocals; if (noBody && methodBody != null) throw new InvalidOperationException("unexpected instructions in extern method definition"); if (!noBody && methodBody == null) throw new InvalidOperationException("missing instructions in method definition"); MethodBody = methodBody; }
public ParameterOrLocalOrResult(IImSeq<Annotation> annotations, ISeq<CustomAttribute> customAttributes, TypeRef type) { Annotations = annotations ?? Constants.EmptyAnnotations; CustomAttributes = customAttributes ?? new Seq<CustomAttribute>(); Type = type; }
public TryTranslationContext(TranslationContext parent, int start, IImSeq<PE.ExceptionHandlingClause> clauses) : base(parent, start) { Clauses = clauses; }
public EventDef(IImSeq<Annotation> annotations, ISeq<CustomAttribute> customAttributes, string name, bool isStatic, MethodSignature add, MethodSignature remove, TypeRef handlerType) : base(annotations, customAttributes, name, isStatic) { Add = add; Remove = remove; HandlerType = handlerType; }
public void Load(IImSeq<string> fileNames, out CCI.AssemblyNode mscorlib, out CCI.AssemblyNode jsTypes) { foreach (var fileName in fileNames) { var canonicalFileName = CanonicalFileName(fileName); if (fileNameToAssembly.ContainsKey(canonicalFileName)) { env.Log(new DuplicateAssemblyFileNameMessage(fileName, canonicalFileName)); throw new ExitException(); } else fileNameToAssembly.Add(canonicalFileName, null); } // ---------------------------------------- // Which assembly should we use for mscorlib and JSTypes? // ---------------------------------------- var mscorlibCanonicalName = default(string); var jsTypesCanonicalName = default(string); foreach (var kv in fileNameToAssembly) { var baseName = Path.GetFileNameWithoutExtension(kv.Key); if (baseName.ToLower().Contains(Constants.MsCorLibSimpleName.ToLower())) { if (mscorlibCanonicalName != null) { env.Log(new DuplicateSpecialAssemblyMessage(Constants.MsCorLibSimpleName, mscorlibCanonicalName, kv.Key)); throw new ExitException(); } mscorlibCanonicalName = kv.Key; } else if (baseName.ToLower().Contains(Constants.JSTypesSimpleName.ToLower())) { if (jsTypesCanonicalName != null) { env.Log(new DuplicateSpecialAssemblyMessage(Constants.JSTypesSimpleName, jsTypesCanonicalName, kv.Key)); throw new ExitException(); } jsTypesCanonicalName = kv.Key; } } if (mscorlibCanonicalName == null) { env.Log(new MissingSpecialAssemblyMessage(Constants.MsCorLibSimpleName)); throw new ExitException(); } if (jsTypesCanonicalName == null) { env.Log(new MissingSpecialAssemblyMessage(Constants.JSTypesSimpleName)); throw new ExitException(); } // ---------------------------------------- // Initialize CCI, which will implicitly load mscorlib // ---------------------------------------- var frameworkDir = Path.GetDirectoryName(mscorlibCanonicalName); if (!Directory.Exists(frameworkDir)) { env.Log(new UnloadableAssemblyMessage(frameworkDir, "directory does not exist")); throw new ExitException(); } // These special CCI assemblies, and mscorlib, will be picked up from the framework directory CCI.SystemDataAssemblyLocation.Location = null; CCI.SystemXmlAssemblyLocation.Location = null; CCI.TargetPlatform.SetToV2(frameworkDir); // At this point we could "fixup" CCI's hard-wired system assembly references: // // foreach (var asmRefs in CCI.TargetPlatform.AssemblyReferenceFor.GetEnumerator()) // { // var asmRef = (CCI.AssemblyReference)asmRefs.Value; // asmRef.Location = <the right place>; // } // SystemAssemblyLocation.Location = <the right place>; // SystemXmlAssemblyLocation.Location = <the right place>; // // But so far that doesn't seem necessary CCI.SystemTypes.Initialize(false, true, ResolveReference); // ---------------------------------------- // Account for mscorlib being loaded // ---------------------------------------- mscorlib = CCI.SystemTypes.SystemAssembly; if (mscorlib == null || mscorlib.Directory == null) { env.Log(new UnloadableAssemblyMessage(frameworkDir, "cannot load mscorlib")); throw new ExitException(); } env.Log(new FoundSpecialAssemblyMessage(Constants.MsCorLibSimpleName, mscorlib.StrongName)); fileNameToAssembly[mscorlibCanonicalName] = mscorlib; strongNameToInfo.Add (mscorlib.StrongName, new Info { Assembly = mscorlib, FileName = mscorlibCanonicalName }); // ---------------------------------------- // Load the remaining registered assemblies // ---------------------------------------- var pending = new Seq<string>(); foreach (var kv in fileNameToAssembly) { if (kv.Value == null) pending.Add(kv.Key); // else: must have been mscorlib, which we loaded above } jsTypes = null; foreach (var canonicalFileName in pending) { var assembly = CCI.AssemblyNode.GetAssembly(canonicalFileName, null, false, true, true); if (assembly == null) { env.Log(new UnloadableAssemblyMessage(canonicalFileName, "CCI cannot load assembly")); throw new ExitException(); } var info = default(Info); if (strongNameToInfo.TryGetValue(assembly.StrongName, out info)) { env.Log(new DuplicateAssemblyStrongNameMessage(canonicalFileName, assembly.StrongName, info.FileName)); throw new ExitException(); } fileNameToAssembly[canonicalFileName] = assembly; strongNameToInfo.Add(assembly.StrongName, new Info { Assembly = assembly, FileName = canonicalFileName }); assembly.AssemblyReferenceResolution += ResolveReference; if (canonicalFileName.Equals(jsTypesCanonicalName)) { jsTypes = assembly; env.Log(new FoundSpecialAssemblyMessage(Constants.JSTypesSimpleName, jsTypes.StrongName)); } } #if false // ---------------------------------------- // Check all references resolve to known definitions // ---------------------------------------- foreach (var kv in strongNameToInfo) { new CCI.StandardVisitor().Visit(kv.Value.Assembly); foreach (var reference in kv.Value.Assembly.AssemblyReferences) { if (reference.Assembly == null || !reference.Assembly.StrongName.Equals(reference.StrongName, StringComparison.OrdinalIgnoreCase) || reference.Assembly.Location.Equals("unknown:location", StringComparison.OrdinalIgnoreCase)) { env.Log(new UnresolvableReferenceMessage(kv.Key, kv.Value.FileName, reference.StrongName)); throw new ExitException(); } } foreach (var typeDefn in kv.Value.Assembly.Types) CheckTypeDefn(kv.Value.Assembly, typeDefn); env.Log(new LoadedAssemblyMessage(kv.Key, kv.Value.FileName)); } #endif if (loadFailed) throw new ExitException(); }
public override MemberRef PrimReference(Global global, AssemblyDef assemblyDef, TypeDef typeDef, IImSeq<TypeRef> typeBoundArguments) { return new EventRef(typeDef.PrimReference(global, assemblyDef, typeBoundArguments), Name); }
public MethodRef PrimMethodReference(Global global, AssemblyDef assemblyDef, TypeDef typeDef, IImSeq<TypeRef> typeBoundArguments, IImSeq<TypeRef> methodBoundArguments) { return new MethodRef (typeDef.PrimReference(global, assemblyDef, typeBoundArguments), Name, IsStatic, methodBoundArguments, ValueParameters.Select(p => p.Type).ToSeq(), Result == null ? null : Result.Type); }
public override MemberRef PrimReference(Global global, AssemblyDef assemblyDef, TypeDef typeDef, IImSeq<TypeRef> typeBoundArguments) { var sig = PropertySignature; return new PropertyRef (typeDef.PrimReference(global, assemblyDef, typeBoundArguments), sig.Name, sig.IsStatic, sig.Parameters, sig.Result); }
public FieldDef(IImSeq<Annotation> annotations, ISeq<CustomAttribute> customAttributes, string name, bool isStatic, TypeRef fieldType, FieldInit init) : base(annotations, customAttributes, name, isStatic) { FieldType = fieldType; Init = init; }
// // References and environments // public abstract MemberRef PrimReference(Global global, AssemblyDef assemblyDef, TypeDef typeDef, IImSeq<TypeRef> typeBoundArguments);
public MachineState CloneWithArgLocalPointsTo(IImSeq<StackEntryState> stack, ArgLocal argLocal, int index, PointsTo pointsTo) { return new MachineState(RootEnv, nArgs, nLocals, innerState.Value.CloneWithArgLocalPointsTo(stack, argLocal, index, pointsTo)); }
// ---------------------------------------------------------------------- // Third pass: Reduce basic blocks // ---------------------------------------------------------------------- private Instructions Peephole(MachineState initialState, IImSeq<Instruction> instructions) { return new Instructions(initialState, instructions).Peephole(tracer); }