public static Object read(PushbackTextReader r, IPersistentMap opts) { return read(r, !opts.containsKey(EOF), opts.valAt(EOF), false, opts); }
private static void MaybeReflectionWarn(IPersistentMap spanMap, MethodBase method, string methodName) { if ( method == null && RT.booleanCast(RT.WARN_ON_REFLECTION.deref()) ) RT.errPrintWriter().WriteLine(string.Format("Reflection warning, {0}:{1} - call to {2} can't be resolved.\n", Compiler.SOURCE_PATH.deref(), spanMap != null ? (int)spanMap.valAt(RT.START_LINE_KEY, 0) : 0, methodName)); }
public Expression MaybeAddDebugInfo(Expression expr, IPersistentMap spanMap) { if (_isDebuggable && spanMap != null & _docInfo != null) { int startLine; int startCol; int finishLine; int finishCol; if (Compiler.GetLocations(spanMap, out startLine, out startCol, out finishLine, out finishCol)) return AstUtils.AddDebugInfo(expr, _docInfo, new Microsoft.Scripting.SourceLocation(0, (int)spanMap.valAt(RT.StartLineKey), (int)spanMap.valAt(RT.StartColumnKey)), new Microsoft.Scripting.SourceLocation(0, (int)spanMap.valAt(RT.EndLineKey), (int)spanMap.valAt(RT.EndColumnKey))); } return expr; }
internal Expression GenVar(GenContext context, Var var) { int i = (int)_vars.valAt(var); return(GenConstant(context, i, var)); }
internal static Expression MaybeAddDebugInfo(Expression expr, IPersistentMap spanMap, bool isDebuggable) { if ( isDebuggable && spanMap != null & Compiler.DocInfo() != null) { int startLine; int startCol; int finishLine; int finishCol; if (GetLocations(spanMap, out startLine, out startCol, out finishLine, out finishCol)) return AstUtils.AddDebugInfo(expr, Compiler.DocInfo(), new Microsoft.Scripting.SourceLocation(0, (int)spanMap.valAt(RT.START_LINE_KEY), (int)spanMap.valAt(RT.START_COLUMN_KEY)), new Microsoft.Scripting.SourceLocation(0, (int)spanMap.valAt(RT.END_LINE_KEY), (int)spanMap.valAt(RT.END_COLUMN_KEY))); } return expr; }
public Expression MaybeAddDebugInfo(Expression expr, IPersistentMap spanMap) { if (_isDebuggable && spanMap != null & _docInfo != null) { if (Compiler.GetLocations(spanMap, out int startLine, out int startCol, out int finishLine, out int finishCol)) { return(AstUtils.AddDebugInfo(expr, _docInfo, new Microsoft.Scripting.SourceLocation(0, (int)spanMap.valAt(RT.StartLineKey), (int)spanMap.valAt(RT.StartColumnKey)), new Microsoft.Scripting.SourceLocation(0, (int)spanMap.valAt(RT.EndLineKey), (int)spanMap.valAt(RT.EndColumnKey)))); } } return(expr); }
public Expr Parse(ParserContext pcon, object form) { // (def x) or (def x initexpr) or (def x "docstring" initexpr) string docstring = null; if (RT.count(form) == 4 && (RT.third(form) is String)) { docstring = (String)RT.third(form); form = RT.list(RT.first(form), RT.second(form), RT.fourth(form)); } if (RT.count(form) > 3) { throw new ParseException("Too many arguments to def"); } if (RT.count(form) < 2) { throw new ParseException("Too few arguments to def"); } Symbol sym = RT.second(form) as Symbol; if (sym == null) { throw new ParseException("First argument to def must be a Symbol."); } //Console.WriteLine("Def {0}", sym.Name); Var v = Compiler.LookupVar(sym, true); if (v == null) { throw new ParseException("Can't refer to qualified var that doesn't exist"); } if (!v.Namespace.Equals(Compiler.CurrentNamespace)) { if (sym.Namespace == null) { v = Compiler.CurrentNamespace.intern(sym); } //throw new Exception(string.Format("Name conflict, can't def {0} because namespace: {1} refers to: {2}", // sym, Compiler.CurrentNamespace.Name, v)); else { throw new ParseException("Can't create defs outside of current namespace"); } } IPersistentMap mm = sym.meta(); bool isDynamic = RT.booleanCast(RT.get(mm, Compiler.DynamicKeyword)); if (isDynamic) { v.setDynamic(); } if (!isDynamic && sym.Name.StartsWith("*") && sym.Name.EndsWith("*") && sym.Name.Length > 1) { RT.errPrintWriter().WriteLine("Warning: {0} not declared dynamic and thus is not dynamically rebindable, " + "but its name suggests otherwise. Please either indicate ^:dynamic {0} or change the name. ({1}:{2}\n", sym, Compiler.SourcePathVar.get(), Compiler.LineVar.get()); } if (RT.booleanCast(RT.get(mm, Compiler.ArglistsKeyword))) { IPersistentMap vm = v.meta(); //vm = (IPersistentMap)RT.assoc(vm, Compiler.STATIC_KEY, true); // drop quote vm = (IPersistentMap)RT.assoc(vm, Compiler.ArglistsKeyword, RT.second(mm.valAt(Compiler.ArglistsKeyword))); v.setMeta(vm); } Object source_path = Compiler.SourcePathVar.get(); source_path = source_path ?? "NO_SOURCE_FILE"; mm = (IPersistentMap)RT.assoc(mm, RT.LineKey, Compiler.LineVar.get()) .assoc(RT.FileKey, source_path); //.assoc(RT.SOURCE_SPAN_KEY,Compiler.SOURCE_SPAN.deref()); if (docstring != null) { mm = (IPersistentMap)RT.assoc(mm, RT.DocKey, docstring); } //mm = mm.without(RT.DOC_KEY) // .without(Keyword.intern(null, "arglists")) // .without(RT.FILE_KEY) // .without(RT.LINE_KEY) // .without(Keyword.intern(null, "ns")) // .without(Keyword.intern(null, "name")) // .without(Keyword.intern(null, "added")) // .without(Keyword.intern(null, "static")); mm = (IPersistentMap)Compiler.ElideMeta(mm); Expr meta = mm == null || mm.count() == 0 ? null : Compiler.Analyze(pcon.EvalOrExpr(), mm); Expr init = Compiler.Analyze(pcon.EvalOrExpr(), RT.third(form), v.Symbol.Name); bool initProvided = RT.count(form) == 3; return(new DefExpr( (string)Compiler.SourceVar.deref(), (int)Compiler.LineVar.deref(), v, init, meta, initProvided, isDynamic)); }
internal Expression GenKeyword(GenContext context, Keyword kw) { int i = (int)_keywords.valAt(kw); return(GenConstant(context, i, kw)); }
public static Object read(PushbackTextReader r, IPersistentMap opts) { return(read(r, !opts.containsKey(EOF), opts.valAt(EOF), false, opts)); }
public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg) { Label?loopLabel = (Label)Compiler.LoopLabelVar.deref(); if (loopLabel == null) { throw new InvalidOperationException("Recur not in proper context."); } { for (int i = 0; i < _loopLocals.count(); i++) { LocalBinding lb = (LocalBinding)_loopLocals.nth(i); Expr arg = (Expr)_args.nth(i); Type primt = lb.PrimitiveType; if (primt != null) { MaybePrimitiveExpr mpeArg = arg as MaybePrimitiveExpr; Type pt = Compiler.MaybePrimitiveType(arg); if (pt == primt) { mpeArg.EmitUnboxed(RHC.Expression, objx, ilg); } else if (primt == typeof(long) && pt == typeof(int)) { mpeArg.EmitUnboxed(RHC.Expression, objx, ilg); ilg.Emit(OpCodes.Conv_I8); } else if (primt == typeof(double) && pt == typeof(float)) { mpeArg.EmitUnboxed(RHC.Expression, objx, ilg); ilg.Emit(OpCodes.Conv_R8); } else if (primt == typeof(int) && pt == typeof(long)) { mpeArg.EmitUnboxed(RHC.Expression, objx, ilg); //ilg.EmitCall(Compiler.Method_RT_intCast_long); ilg.Emit(OpCodes.Conv_I4); } else if (primt == typeof(float) && pt == typeof(double)) { mpeArg.EmitUnboxed(RHC.Expression, objx, ilg); ilg.Emit(OpCodes.Conv_R4); } else { throw new ArgumentException(String.Format( "{0}:{1} recur arg for primitive local: {2} is not matching primitive, had: {3}, needed {4}", _source, _spanMap != null ? (int)_spanMap.valAt(RT.StartLineKey, 0) : 0, lb.Name, (arg.HasClrType ? arg.ClrType.Name : "Object"), primt.Name)); } } else { arg.Emit(RHC.Expression, objx, ilg); } } } for (int i = _loopLocals.count() - 1; i >= 0; i--) { LocalBinding lb = (LocalBinding)_loopLocals.nth(i); //Type primt = lb.PrimitiveType; if (lb.IsArg) { //ilg.Emit(OpCodes.Starg, lb.Index - (objx.IsStatic ? 0 : 1)); if (lb.DeclaredType != typeof(Object) && !lb.DeclaredType.IsPrimitive) { ilg.Emit(OpCodes.Castclass, lb.DeclaredType); } ilg.EmitStoreArg(lb.Index); } else { ilg.Emit(OpCodes.Stloc, lb.LocalVar); } } ilg.Emit(OpCodes.Br, loopLabel.Value); }
public Expr Parse(ParserContext pcon, object frm) { string source = (string)Compiler.SourceVar.deref(); IPersistentMap spanMap = (IPersistentMap)Compiler.SourceSpanVar.deref(); // Compiler.GetSourceSpanMap(form); ISeq form = (ISeq)frm; IPersistentVector loopLocals = (IPersistentVector)Compiler.LoopLocalsVar.deref(); if (pcon.Rhc != RHC.Return || loopLocals == null) { throw new ParseException("Can only recur from tail position"); } if (Compiler.NoRecurVar.deref() != null) { throw new ParseException("Cannot recur across try"); } IPersistentVector args = PersistentVector.EMPTY; for (ISeq s = RT.seq(form.next()); s != null; s = s.next()) { args = args.cons(Compiler.Analyze(pcon.SetRhc(RHC.Expression).SetAssign(false), s.first())); } if (args.count() != loopLocals.count()) { throw new ParseException(string.Format("Mismatched argument count to recur, expected: {0} args, got {1}", loopLocals.count(), args.count())); } for (int i = 0; i < loopLocals.count(); i++) { LocalBinding lb = (LocalBinding)loopLocals.nth(i); Type primt = lb.PrimitiveType; if (primt != null) { bool mismatch = false; Type pt = Compiler.MaybePrimitiveType((Expr)args.nth(i)); if (primt == typeof(long)) { if (!(pt == typeof(long) || pt == typeof(int) || pt == typeof(short) || pt == typeof(uint) || pt == typeof(ushort) || pt == typeof(ulong) || pt == typeof(char) || pt == typeof(byte) || pt == typeof(sbyte))) { mismatch = true; } } else if (primt == typeof(double)) { if (!(pt == typeof(double) || pt == typeof(float))) { mismatch = true; } } if (mismatch) { lb.RecurMismatch = true; if (RT.booleanCast(RT.WarnOnReflectionVar.deref())) { RT.errPrintWriter().WriteLine("{0}:{1} recur arg for primitive local: {2} is not matching primitive, had: {3}, needed {4}", source, spanMap != null ? (int)spanMap.valAt(RT.StartLineKey, 0) : 0, lb.Name, pt != null ? pt.Name : "Object", primt.Name); } } } } return(new RecurExpr(source, spanMap, loopLocals, args)); }
private static void EmitExposers(TypeBuilder proxyTB, Type superClass, IPersistentMap exposesFields) { for (ISeq s = RT.seq(exposesFields); s != null; s = s.next()) { IMapEntry me = (IMapEntry)s.first(); Symbol protectedFieldSym = (Symbol)me.key(); IPersistentMap accessMap = (IPersistentMap)me.val(); string fieldName = protectedFieldSym.Name; Symbol getterSym = (Symbol)accessMap.valAt(_getKw, null); Symbol setterSym = (Symbol)accessMap.valAt(_setKW, null); FieldInfo fld = null; if (getterSym != null || setterSym != null) { fld = superClass.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Instance); } if (getterSym != null) { MethodAttributes attribs = MethodAttributes.Public; if (fld.IsStatic) { attribs |= MethodAttributes.Static; } MethodBuilder mb = proxyTB.DefineMethod(getterSym.Name, attribs, fld.FieldType, Type.EmptyTypes); CljILGen gen = new CljILGen(mb.GetILGenerator()); //if (fld.IsStatic) // gen.Emit(OpCodes.Ldsfld, fld); //else //{ // gen.Emit(OpCodes.Ldarg_0); // gen.Emit(OpCodes.Ldfld, fld); //} if (!fld.IsStatic) { gen.EmitLoadArg(0); } gen.EmitFieldGet(fld); gen.Emit(OpCodes.Ret); } if (setterSym != null) { MethodAttributes attribs = MethodAttributes.Public; if (fld.IsStatic) { attribs |= MethodAttributes.Static; } MethodBuilder mb = proxyTB.DefineMethod(setterSym.Name, attribs, typeof(void), new Type[] { fld.FieldType }); CljILGen gen = new CljILGen(mb.GetILGenerator()); if (fld.IsStatic) { gen.Emit(OpCodes.Ldarg_0); //gen.Emit(OpCodes.Stsfld, fld); } else { gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Ldarg_1); //gen.Emit(OpCodes.Stfld, fld); } gen.EmitFieldSet(fld); gen.Emit(OpCodes.Ret); } } }
private static void EmitMethods(TypeBuilder proxyTB, List <MethodSignature> sigs, Dictionary <string, List <MethodSignature> > overloads, Dictionary <string, FieldBuilder> varMap, IPersistentMap exposesMethods) { foreach (MethodSignature sig in sigs) { FieldBuilder regularFB = varMap[sig.Name]; FieldBuilder overloadFB = null; if (overloads.ContainsKey(sig.Name)) { overloadFB = varMap[OverloadName(sig)]; } switch (sig.Source) { case "super": EmitForwardingMethod(proxyTB, false, regularFB, overloadFB, sig, delegate(CljILGen gen) { gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0); for (int i = 0; i < sig.ParamTypes.Length; i++) { gen.EmitLoadArg(i + 1); // gen.Emit(OpCodes.Ldarg, (i + 1)); } gen.Emit(OpCodes.Call, sig.Method); // not gen.EmitCall(sig.Method) -- we need call versus callvirt }); break; case "interface": EmitForwardingMethod(proxyTB, false, regularFB, overloadFB, sig, delegate(CljILGen gen) { EmitUnsupported(gen, sig.Name); }); break; default: EmitForwardingMethod(proxyTB, sig.IsStatic, regularFB, overloadFB, sig, delegate(CljILGen gen) { EmitUnsupported(gen, sig.Name); }); break; } } if (exposesMethods != null) { foreach (MethodSignature sig in sigs) { if (sig.Source == "super") { Symbol name = Symbol.intern(sig.Name); if (exposesMethods.containsKey(name)) { CreateSuperCall(proxyTB, (Symbol)exposesMethods.valAt(name), sig.Method); } } } } }
/// <summary> /// Get the method for a dispatch value and cache it. /// </summary> /// <param name="dispatchVal">The disaptch value.</param> /// <returns>The mest method.</returns> private IFn FindAndCacheBestMethod(object dispatchVal) { _rw.EnterWriteLock(); object bestValue; IPersistentMap mt = _methodTable; IPersistentMap pt = _preferTable; object ch = _cachedHierarchy; try { IMapEntry bestEntry = null; foreach (IMapEntry me in MethodTable) { if (IsA(dispatchVal, me.key())) { if (bestEntry == null || Dominates(me.key(), bestEntry.key())) { bestEntry = me; } if (!Dominates(bestEntry.key(), me.key())) { throw new ArgumentException(String.Format("Multiple methods in multimethod {0} match dispatch value: {1} -> {2} and {3}, and neither is preferred", _name, dispatchVal, me.key(), bestEntry.key())); } } } if (bestEntry == null) { bestValue = _methodTable.valAt(_defaultDispatchVal); if (bestValue == null) { return(null); } } else { bestValue = bestEntry.val(); } } finally { _rw.ExitWriteLock(); } // ensure basis has stayed stable throughout, else redo _rw.EnterWriteLock(); try { if (mt == _methodTable && pt == _preferTable && ch == _cachedHierarchy && _cachedHierarchy == _hierarchy.deref()) { // place in cache _methodCache = _methodCache.assoc(dispatchVal, bestValue); return((IFn)bestValue); } else { ResetCache(); return(FindAndCacheBestMethod(dispatchVal)); } } finally { _rw.ExitWriteLock(); } }
static bool GetLocation(IPersistentMap spanMap, Keyword key, out int val) { object oval = spanMap.valAt(key); if (oval != null && oval is int) { val = (int)oval; return true; } val = -1; return false; }
/// <summary> /// Get the value for the key (= the key itself, or null if not present). /// </summary> /// <param name="key">The value to test for membership in the set.</param> /// <returns>the key if the key is in the set, else null.</returns> public object get(object key) { return(_impl.valAt(key)); }
private static void EmitMethods(TypeBuilder proxyTB, List<MethodSignature> sigs, Dictionary<string,List<MethodSignature>> overloads, Dictionary<string,FieldBuilder> varMap, IPersistentMap exposesMethods) { foreach (MethodSignature sig in sigs) { FieldBuilder regularFB = varMap[sig.Name]; FieldBuilder overloadFB = null; if (overloads.ContainsKey(sig.Name)) overloadFB = varMap[OverloadName(sig)]; switch (sig.Source) { case "super": EmitForwardingMethod(proxyTB, false, regularFB, overloadFB, sig, delegate(ILGen gen) { gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0); for (int i = 0; i < sig.ParamTypes.Length; i++) gen.EmitLoadArg(i + 1); // gen.Emit(OpCodes.Ldarg, (i + 1)); gen.Emit(OpCodes.Call, sig.Method); // not gen.EmitCall(sig.Method) -- we need call versus callvirt }); break; case "interface": EmitForwardingMethod(proxyTB, false, regularFB, overloadFB, sig, delegate(ILGen gen) { EmitUnsupported(gen, sig.Name); }); break; default: EmitForwardingMethod(proxyTB, sig.IsStatic, regularFB, overloadFB, sig, delegate(ILGen gen) { EmitUnsupported(gen, sig.Name); }); break; } } if (exposesMethods != null) { foreach (MethodSignature sig in sigs) { if (sig.Source == "super") { Symbol name = Symbol.intern(sig.Name); if (exposesMethods.containsKey(name)) CreateSuperCall(proxyTB, (Symbol)exposesMethods.valAt(name), sig.Method); } } } }
public InvokeExpr(string source, IPersistentMap spanMap, Symbol tag, Expr fexpr, IPersistentVector args) { _source = source; _spanMap = spanMap; _fexpr = fexpr; _args = args; VarExpr varFexpr = fexpr as VarExpr; if (varFexpr != null) { Var fvar = varFexpr.Var; Var pvar = (Var)RT.get(fvar.meta(), Compiler.ProtocolKeyword); if (pvar != null && Compiler.ProtocolCallsitesVar.isBound) { _isProtocol = true; _siteIndex = Compiler.RegisterProtocolCallsite(fvar); Object pon = RT.get(pvar.get(), _onKey); _protocolOn = HostExpr.MaybeType(pon, false); if (_protocolOn != null) { IPersistentMap mmap = (IPersistentMap)RT.get(pvar.get(), _methodMapKey); Keyword mmapVal = (Keyword)mmap.valAt(Keyword.intern(fvar.sym)); if (mmapVal == null) { throw new ArgumentException(String.Format("No method of interface: {0} found for function: {1} of protocol: {2} (The protocol method may have been defined before and removed.)", _protocolOn.FullName, fvar.Symbol, pvar.Symbol)); } String mname = Compiler.munge(mmapVal.Symbol.ToString()); IList <MethodBase> methods = Reflector.GetMethods(_protocolOn, mname, null, args.count() - 1, false); if (methods.Count != 1) { throw new ArgumentException(String.Format("No single method: {0} of interface: {1} found for function: {2} of protocol: {3}", mname, _protocolOn.FullName, fvar.Symbol, pvar.Symbol)); } _onMethod = (MethodInfo)methods[0]; } } } //_tag = tag ?? (varFexpr != null ? varFexpr.Tag : null); if (tag != null) { _tag = tag; } else if (varFexpr != null) { object arglists = RT.get(RT.meta(varFexpr.Var), Compiler.ArglistsKeyword); object sigTag = null; for (ISeq s = RT.seq(arglists); s != null; s = s.next()) { APersistentVector sig = (APersistentVector)s.first(); int restOffset = sig.IndexOf(Compiler.AmpersandSym); if (args.count() == sig.count() || (restOffset > -1 && args.count() >= restOffset)) { sigTag = Compiler.TagOf(sig); break; } } _tag = sigTag ?? varFexpr.Tag; } else { _tag = null; } }