public void SubVisit(FCall fCall) { int stackFrameSize = this.FunctionScope.GetStackFrameSize(); var scope = this.GlobalScope.Get(fCall.Id, Classification.Function)?.Link; if (scope == null) { string inheritedClass = fCall.Id.Split(':')[0]; string functionName = fCall.Id.Split(':').Last(); scope = this.GlobalScope.Get(inheritedClass, Classification.Class)?.Link; var functionEntry = scope.Get(functionName, Classification.Function); string newLink = $"{functionEntry.OriginalFunctionOwner}::{functionName}"; scope = this.GlobalScope.Get(newLink, Classification.Function).Link; fCall.Id = newLink; } int paramOffset = stackFrameSize + 4 + scope.Get("retval", Classification.SubCalculationStackSpace).EntryMemorySize; foreach (var expression in fCall.Parameters.Expressions) { this.LoadAndStore(expression, paramOffset, expression.NodeMemorySize, $"Passing parameter {expression}"); paramOffset += expression.NodeMemorySize; } InstructionStream.Add($"addi r14, r14, {stackFrameSize}"); InstructionStream.Add($"jl r15, function_{fCall.Id.Replace(':', '_')}", $"Call the function {fCall.Id}"); InstructionStream.Add($"subi r14, r14, {stackFrameSize}"); this.LoadAndStore(stackFrameSize + 4, fCall, fCall.NodeMemorySize, "Retrieve the returnvalue"); }
protected object EvalExpression(Expression exp) { currentExpression = exp; if (exp is StringLiteral) { return(((StringLiteral)exp).Content); } else if (exp is Name) { object obj = variables[((Name)exp).Id]; return(obj); } else if (exp is FieldAccess) { FieldAccess fa = (FieldAccess)exp; object obj = variables[fa.Exp]; string propertyName = fa.Field; return(EvalProperty(obj, propertyName)); } else if (exp is IntLiteral) { return(((IntLiteral)exp).Value); } else if (exp is FCall) { FCall fcall = (FCall)exp; if (!functions.ContainsKey(fcall.Name)) { string msg = string.Format("Function {0} is not defined", fcall.Name); WriteError(msg, exp.Line, exp.Col); return(null); } TemplateFunction func = functions[fcall.Name]; object[] values = new object[fcall.Args.Length]; for (int i = 0; i < values.Length; i++) { values[i] = EvalExpression(fcall.Args[i]); } return(func(values)); } else if (exp is StringExpression) { StringExpression stringExp = (StringExpression)exp; StringBuilder sb = new StringBuilder(); foreach (Expression ex in stringExp.Expressions) { sb.Append(EvalExpression(ex)); } return(sb.ToString()); } return(null); }
[System.Security.SecurityCritical] // auto-generated private void Dispose(bool disposing) { if (this.disposed) { return; } if (disposing) { if (this.threadHandle != null) { this.threadHandle.Dispose(); this.threadHandle = null; } } if (this.isImpersonating) { FCall.RevertToSelf(); } this.disposed = true; }
private object EvalExpression(Expression exp) { CurrentExpression = exp; try { if (exp is StringLiteral) { return(((StringLiteral)exp).Content); } else if (exp is Name) { return(GetValue(((Name)exp).Id)); } else if (exp is FieldAccess) { FieldAccess fa = (FieldAccess)exp; object obj = EvalExpression(fa.Exp); string propertyName = fa.Field; return(ProcessProperty(obj, propertyName)); } else if (exp is MCall) { MCall ma = (MCall)exp; object obj = EvalExpression(ma.CallObject); string methodName = ma.Name; return(ProcessMCall(obj, methodName, ProcessArguments(ma.Args))); } else if (exp is IntLiteral) { return(((IntLiteral)exp).Value); } else if (exp is DoubleLiteral) { return(((DoubleLiteral)exp).Value); } else if (exp is FCall) { FCall fcall = (FCall)exp; if (!_fnTbl.ContainsKey(fcall.Name)) { string msg = string.Format("Function {0} is not defined", fcall.Name); throw new TmplException(msg, exp.Line, exp.Col); } FunctionDefinition func = _fnTbl[fcall.Name]; object[] values = ProcessArguments(fcall.Args); return(func(values)); } else if (exp is StringExpression) { StringExpression stringExp = (StringExpression)exp; StringBuilder sb = new StringBuilder(); foreach (Expression ex in stringExp.Expressions) { sb.Append(EvalExpression(ex)); } return(sb.ToString()); } else if (exp is BinaryExpression) { return(ProcessBinaryExpression(exp as BinaryExpression)); } else if (exp is ArrayAccess) { return(ProcessArrayAccess(exp as ArrayAccess)); } else { throw new TmplException("Invalid expression type: " + exp.GetType().Name, exp.Line, exp.Col); } } catch (TmplException ex) { DisplayError(ex); return(null); } catch (Exception ex) { string _Message = "Message=" + ex.Message + "," + "Source=" + ex.Source + ",StackTrace=" + ex.StackTrace + ",TargetSite=" + ex.TargetSite + ""; DisplayError(new TmplException(_Message, CurrentExpression.Line, CurrentExpression.Col)); return(null); } }
public TlsContents() { int error = 0; int cachingError = 0; bool success = true; if (processHandle.IsInvalid) { lock ( syncRoot ) { if (processHandle.IsInvalid) { SafeAccessTokenHandle localProcessHandle; if (false == Win32Native.OpenProcessToken( Win32Native.GetCurrentProcess(), TokenAccessLevels.Duplicate, out localProcessHandle)) { cachingError = Marshal.GetLastWin32Error(); success = false; } processHandle = localProcessHandle; } } } RuntimeHelpers.PrepareConstrainedRegions(); try { // Make the sequence non-interruptible } finally { try { // // Open the thread token; if there is no thread token, get one from // the process token by impersonating self. // SafeAccessTokenHandle threadHandleBefore = this.threadHandle; error = FCall.OpenThreadToken( TokenAccessLevels.Query | TokenAccessLevels.AdjustPrivileges, WinSecurityContext.Process, out this.threadHandle); unchecked { error &= ~(int)0x80070000; } if (error != 0) { if (success == true) { this.threadHandle = threadHandleBefore; if (error != Win32Native.ERROR_NO_TOKEN) { success = false; } Contract.Assert(this.isImpersonating == false, "Incorrect isImpersonating state"); if (success == true) { error = 0; if (false == Win32Native.DuplicateTokenEx( processHandle, TokenAccessLevels.Impersonate | TokenAccessLevels.Query | TokenAccessLevels.AdjustPrivileges, IntPtr.Zero, Win32Native.SECURITY_IMPERSONATION_LEVEL.Impersonation, System.Security.Principal.TokenType.TokenImpersonation, ref this.threadHandle)) { error = Marshal.GetLastWin32Error(); success = false; } } if (success == true) { error = FCall.SetThreadToken(this.threadHandle); unchecked { error &= ~(int)0x80070000; } if (error != 0) { success = false; } } if (success == true) { this.isImpersonating = true; } } else { error = cachingError; } } else { success = true; } } finally { if (!success) { Dispose(); } } } if (error == Win32Native.ERROR_NOT_ENOUGH_MEMORY) { throw new OutOfMemoryException(); } else if (error == Win32Native.ERROR_ACCESS_DENIED || error == Win32Native.ERROR_CANT_OPEN_ANONYMOUS) { throw new UnauthorizedAccessException(); } else if (error != 0) { Contract.Assert(false, string.Format(CultureInfo.InvariantCulture, "WindowsIdentity.GetCurrentThreadToken() failed with unrecognized error code {0}", error)); throw new InvalidOperationException(); } }
Expression PrimaryExpression() { if (Current.TokenKind == TokenKind.StringStart) { return(ReadString()); } else if (Current.TokenKind == TokenKind.ID) { Token id = Consume(); Expression exp = null; // if ( follows ID, we have a function call if (Current.TokenKind == TokenKind.LParen) { Consume(); // consume LParen Expression[] args = ReadArguments(); Consume(TokenKind.RParen); exp = new FCall(id.Line, id.Col, id.Data, args); } else // else, we just have id { exp = new Name(id.Line, id.Col, id.Data); } // while we have ".", keep chaining up field access or method call while (Current.TokenKind == TokenKind.Dot || Current.TokenKind == TokenKind.LBracket) { if (Current.TokenKind == TokenKind.Dot) { Consume(); // consume DOT Token field = Consume(TokenKind.ID); // consume ID after dot // if "(" after ID, then it's a method call if (Current.TokenKind == TokenKind.LParen) { Consume(); // consume "(" Expression[] args = ReadArguments(); Consume(TokenKind.RParen); // read ")" exp = new MethodCall(field.Line, field.Col, exp, field.Data, args); } else { exp = new FieldAccess(field.Line, field.Col, exp, field.Data); } } else // must be LBracket { // array access Token bracket = Current; Consume(); // consume [ Expression indexExp = TopExpression(); Consume(TokenKind.RBracket); exp = new ArrayAccess(bracket.Line, bracket.Col, exp, indexExp); } } return(exp); } else if (Current.TokenKind == TokenKind.Integer) { int value = Int32.Parse(Current.Data); IntLiteral intLiteral = new IntLiteral(Current.Line, Current.Col, value); Consume(); // consume int return(intLiteral); } else if (Current.TokenKind == TokenKind.Double) { double value = Double.Parse(Current.Data); DoubleLiteral dLiteral = new DoubleLiteral(Current.Line, Current.Col, value); Consume(); // consume int return(dLiteral); } else if (Current.TokenKind == TokenKind.LParen) { Consume(); // eat ( Expression exp = TopExpression(); Consume(TokenKind.RParen); // eat ) return(exp); } else { throw new ParseException("Invalid token in expression: " + Current.TokenKind + ". Was expecting ID or string.", Current.Line, Current.Col); } }
private void visitExpression(Expression expression) { if (expression is Name) { WriteLine("Name: " + ((Name)expression).Id); } else if (expression is FCall) { FCall fcall = (FCall)expression; WriteLine("FCall: " + fcall.Name); WriteLine("Parameters: "); foreach (Expression exp in fcall.Args) { visitExpression(exp); } } else if (expression is FieldAccess) { FieldAccess fa = (FieldAccess)expression; WriteLine("FieldAccess: " + fa.Exp + "." + fa.Field); } else if (expression is StringLiteral) { StringLiteral literal = (StringLiteral)expression; if (literal.Content.Length > 50) { WriteLine("String: " + literal.Content.Substring(0, 50) + "..."); } else { WriteLine("String: " + literal.Content); } } else if (expression is StringExpression) { StringExpression sexp = (StringExpression)expression; WriteLine("StringExpression"); foreach (Expression exp in sexp.Expressions) { visitExpression(exp); } } else if (expression is BinaryExpression) { BinaryExpression sexp = (BinaryExpression)expression; WriteLine("BinaryExpression"); visitExpression(sexp.Lhs); WriteLine("Operator " + sexp.Operator.ToString()); visitExpression(sexp.Rhs); } else { WriteLine("Expression: " + expression.GetType().ToString()); } }
private Expression AtomExpression() { if (Current.TokenKind == TokenKind.StringStart) { return(ReadString()); } else if (Current.TokenKind == TokenKind.ID) { Token id = Consume(); Expression exp = null; if (Current.TokenKind == TokenKind.LParen) { Consume(); Expression[] args = ReadArguments(); Consume(TokenKind.RParen); exp = new FCall(id.Line, id.Col, id.Data, args); } else { exp = new Name(id.Line, id.Col, id.Data); } while (Current.TokenKind == TokenKind.Dot || Current.TokenKind == TokenKind.LBracket) { if (Current.TokenKind == TokenKind.Dot) { Consume(); Token field = Consume(TokenKind.ID); if (Current.TokenKind == TokenKind.LParen) { Consume(); Expression[] args = ReadArguments(); Consume(TokenKind.RParen); exp = new MCall(field.Line, field.Col, exp, field.Data, args); } else { exp = new FieldAccess(field.Line, field.Col, exp, field.Data); } } else { Token bracket = Current; Consume(); Expression indexExp = TopExpression(); Consume(TokenKind.RBracket); exp = new ArrayAccess(bracket.Line, bracket.Col, exp, indexExp); } } return(exp); } else if (Current.TokenKind == TokenKind.Integer) { int value = int.Parse(Current.Data); IntLiteral intLiteral = new IntLiteral(Current.Line, Current.Col, value); Consume(); return(intLiteral); } else if (Current.TokenKind == TokenKind.Double) { double value = double.Parse(Current.Data); DoubleLiteral dLiteral = new DoubleLiteral(Current.Line, Current.Col, value); Consume(); return(dLiteral); } else if (Current.TokenKind == TokenKind.LParen) { Consume(); Expression exp = TopExpression(); Consume(TokenKind.RParen); return(exp); } else { throw new VoltException(string.Format("Invalid token in expression: " + Current.TokenKind + ". Was expecting ID or string.L{0}/C{1}", Current.Line, Current.Col), Current.Line, Current.Col); } }
/// <summary> /// evaluates expression. /// This method is used by TemplateManager extensibility. /// </summary> public object EvalExpression(Expression exp) { currentExpression = exp; try { if (exp is StringLiteral) { return(((StringLiteral)exp).Content); } else if (exp is Name) { return(GetValue(((Name)exp).Id)); } else if (exp is FieldAccess) { FieldAccess fa = (FieldAccess)exp; object obj = EvalExpression(fa.Exp); string propertyName = fa.Field; return(EvalProperty(obj, propertyName)); } else if (exp is MethodCall) { MethodCall ma = (MethodCall)exp; object obj = EvalExpression(ma.CallObject); string methodName = ma.Name; return(EvalMethodCall(obj, methodName, EvalArguments(ma.Args))); } else if (exp is IntLiteral) { return(((IntLiteral)exp).Value); } else if (exp is DoubleLiteral) { return(((DoubleLiteral)exp).Value); } else if (exp is FCall) { FCall fcall = (FCall)exp; if (!functions.ContainsKey(fcall.Name)) { string msg = string.Format("函数 '{0}' 未定义.", fcall.Name); throw new TemplateRuntimeException(msg, exp.Line, exp.Col); } TemplateFunction func = functions[fcall.Name]; object[] values = EvalArguments(fcall.Args); return(func(values)); } else if (exp is StringExpression) { StringExpression stringExp = (StringExpression)exp; StringBuilder sb = new StringBuilder(); foreach (Expression ex in stringExp.Expressions) { sb.Append(EvalExpression(ex)); } return(sb.ToString()); } else if (exp is BinaryExpression) { return(EvalBinaryExpression(exp as BinaryExpression)); } else if (exp is ArrayAccess) { return(EvalArrayAccess(exp as ArrayAccess)); } else { throw new TemplateRuntimeException("无效的表达式类型: " + exp.GetType().Name, exp.Line, exp.Col); } } catch (TemplateRuntimeException ex) { DisplayError(ex); return(null); } catch (Exception ex) { DisplayError(new TemplateRuntimeException(ex.Message, currentExpression.Line, currentExpression.Col)); return(null); } }
public virtual void Visit(FCall fCall) { }