/// <summary> /// Gets the name of the anonymous class. /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> private string GetAnonymousClassName(MacroStatement macro) { if (macro.GetAncestor(NodeType.MacroStatement) == null) { Errors.Add( CompilerErrorFactory.CustomError(macro.LexicalInfo, GetType().Name + " must have a single parameter, the name of the " + GetType().Name)); return(null); } isAnonymous = true; string name = typeof(T).Name.Replace("Abstract", "").Replace("Operation", ""); if (macro["anonymous_name_index"] == null) { macro["anonymous_name_index"] = Context.GetUniqueName(); } return("Anonymous_" + name + "_" + macro["anonymous_name_index"]); }
public override Statement Expand(MacroStatement macro) { if (0 == macro.Arguments.Count) { throw Boo.Lang.Compiler.CompilerErrorFactory.InvalidLockMacroArguments(macro); } Block resulting = macro.Block; ExpressionCollection args = macro.Arguments; for (int i = args.Count; i > 0; --i) { Expression arg = args[i - 1]; resulting = CreateLockedBlock(arg, resulting); } return(resulting); }
/// <summary> /// Gets the name of the class that we will generate /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> protected virtual string GetClassName(MacroStatement macro) { if (macro.Arguments.Count == 0 || (macro.Arguments[0] is ReferenceExpression) == false) { return(GetAnonymousClassName(macro)); } argumentStartIndex = 1; ReferenceExpression referenceExpression = macro.Arguments[0] as ReferenceExpression; if (referenceExpression == null) { Errors.Add( CompilerErrorFactory.CustomError(macro.LexicalInfo, GetType().Name + " first parameter must be a valid name.")); return(null); } return(referenceExpression.Name); }
protected override bool ExpandExtension(ref MethodInvocationExpression extension, MacroStatement macro, MacroStatement parent, ref Statement expansion) { if (macro.Block != null) { PromoteExtensions(macro, parent); HashConfigurationBuilder builder = new HashConfigurationBuilder(); if (builder.BuildConfig(macro.Block, Errors) && builder.HasConfiguration) { extension.Arguments.Add(builder.HashConfiguration); return(true); } } return(false); }
private bool ProcessAttributes(MacroStatement macro) { var builder = new HashConfigurationBuilder(); if (!builder.BuildAttributes(macro.Body, Errors)) { return(false); } if (builder.HasConfiguration) { var extension = new MethodInvocationExpression( AstUtil.CreateReferenceExpression(typeof(ConfigurationExtension).FullName) ); extension.Arguments.Add(builder.HashConfiguration); RegisterExtension(macro, extension); } return(true); }
public override Statement Expand(MacroStatement macro) { // try: // assignment // <the rest> // ensure: // ... TryStatement stmt = new TryStatement(macro.LexicalInfo); stmt.EnsureBlock = new Block(macro.LexicalInfo); foreach (Expression expression in macro.Arguments) { Expression reference; if (expression is ReferenceExpression) { reference = expression; } else { if (IsAssignmentToReference(expression)) { reference = ((BinaryExpression)expression).Left.CloneNode(); stmt.ProtectedBlock.Add(expression); } else { string tempName = string.Format("__using{0}__", _context.AllocIndex()); reference = new ReferenceExpression(expression.LexicalInfo, tempName); stmt.ProtectedBlock.Add(new BinaryExpression(expression.LexicalInfo, BinaryOperatorType.Assign, reference.CloneNode(), expression)); } } AugmentEnsureBlock(stmt.EnsureBlock, reference); } stmt.ProtectedBlock.Add(macro.Block); return(stmt); }
public override Statement Expand(MacroStatement macro) { MacroStatement parent; Statement expansion = null; if (ValidateParent(macro, out parent) && (!_noStatements || EnsureNoStatements(macro, _name))) { MethodInvocationExpression extension = CreateExtension(); if (ExpandExtension(ref extension, macro, parent, ref expansion) && extension != null) { RegisterExtension(parent, extension); } } return(expansion); }
/// <summary> /// Perform the actual expansion of the macro /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> protected override Statement DoExpand(MacroStatement macro) { if (macro.Arguments.Count != 1) { Errors.Add( CompilerErrorFactory.CustomError(macro.LexicalInfo, "Only a single argument allowed for on statement")); return(null); } Method mergeRowsMethod = new Method("MergeRows"); mergeRowsMethod.Modifiers = TypeMemberModifiers.Override; mergeRowsMethod.Parameters.Add(new ParameterDeclaration("left", new SimpleTypeReference(typeof(Row).FullName))); mergeRowsMethod.Parameters.Add(new ParameterDeclaration("right", new SimpleTypeReference(typeof(Row).FullName))); CodeBuilder.DeclareLocal(mergeRowsMethod, "row", TypeSystemServices.Map(typeof(Row))); mergeRowsMethod.Body.Add( new BinaryExpression(BinaryOperatorType.Assign, new ReferenceExpression("row"), new MethodInvocationExpression( AstUtil.CreateReferenceExpression(typeof(Row).FullName)) ) ); mergeRowsMethod.Body.Add(macro.Body); mergeRowsMethod.Body.Add(new ReturnStatement(new ReferenceExpression("row"))); ParentMethods.Add(mergeRowsMethod); Method matchJoinConditionMethod = new Method("MatchJoinCondition"); matchJoinConditionMethod.ReturnType = new SimpleTypeReference(typeof(bool).FullName); matchJoinConditionMethod.Parameters.Add(new ParameterDeclaration("left", new SimpleTypeReference(typeof(Row).FullName))); matchJoinConditionMethod.Parameters.Add(new ParameterDeclaration("right", new SimpleTypeReference(typeof(Row).FullName))); matchJoinConditionMethod.Body.Add( new ReturnStatement(macro.Arguments[0]) ); ParentMethods.Add(matchJoinConditionMethod); return(null); }
public void MacroStatement_with_arguments_is_transformed_to_MethodInvocationExpression() { const string methodInBlockName = "some_method"; MacroStatement statementInBlock = new MacroStatement(); statementInBlock.Name = methodInBlockName; statementInBlock.Arguments.Add(new StringLiteralExpression("arg1")); MacroStatement fixture = GetMacroStatement("DoStuff", statementInBlock); BlockToArgumentsTransformer transformer = new BlockToArgumentsTransformer("DoStuff"); transformer.Visit(fixture); MethodInvocationExpression mie = fixture.Arguments[0] as MethodInvocationExpression; Assert.NotNull(mie);//, "Could not cast argument one of MacroStatement to MethodInvocationExpression." Assert.Equal(methodInBlockName, (mie.Target as ReferenceExpression).Name); }
public override IEnumerable <Node> ExpandGenerator(MacroStatement macro) { IEnumerable <Node> nodes = ExpandGeneratorImpl(macro); if (null != nodes) { foreach (Node n in nodes) { if (IsEmptyBlock(n)) { continue; } if (null != n) { n.LexicalInfo = macro.LexicalInfo; } yield return(n); } } }
private IEntity ResolveMacroName(MacroStatement node) { var macroTypeName = BuildMacroTypeName(node.Name); var entity = ResolvePreferringInternalMacros(macroTypeName) ?? ResolvePreferringInternalMacros(node.Name); if (entity is IType) { return(entity); } if (entity == null) { return(null); } //we got something interesting, check if it is/has an extension method //that resolves a nested macro extension return(ResolveMacroExtensionType(node, entity as IMethod) ?? ResolveMacroExtensionType(node, entity as Ambiguous) ?? entity); //return as-is }
private bool ValidateParent(MacroStatement macro, out MacroStatement parent) { parent = macro.ParentNode.ParentNode as MacroStatement; if (parent != null) { foreach (string validParent in _validParents) { if (parent.Name.Equals(validParent, StringComparison.InvariantCultureIgnoreCase)) { return(true); } } } AddCompilerError(macro.LexicalInfo, string.Format("A {0} statement can appear only under a {1}", _name, string.Join(" | ", _validParents))); return(false); }
public void MacroStatement_with_block_is_transformed_to_MethodInvocationExpression() { const string methodInBlockName = "some_method"; const string doStuff = "DoStuff"; Expression argInBlockExpression = new StringLiteralExpression("argInBlock"); Statement argInBlockStatement = new ExpressionStatement(argInBlockExpression); MacroStatement statementInBlock = GetMacroStatement(methodInBlockName, argInBlockStatement); MacroStatement doStuffStatement = GetMacroStatement(doStuff, statementInBlock); BlockToArgumentsTransformer transformer = new BlockToArgumentsTransformer(doStuff, methodInBlockName); transformer.Visit(doStuffStatement); MethodInvocationExpression mie = doStuffStatement.Arguments[0] as MethodInvocationExpression; Assert.NotNull(mie); Assert.Equal(methodInBlockName, (mie.Target as ReferenceExpression).Name); Assert.IsAssignableFrom(typeof(BlockExpression), mie.Arguments[0]); }
protected Statement Expand(MacroStatement macro, Expression writePrototype, Expression writeLinePrototype) { LexicalInfo li = macro.LexicalInfo; int argc = macro.Arguments.Count; if (argc < 2) { MethodInvocationExpression mie = new MethodInvocationExpression( li, writeLinePrototype.CloneNode()); mie.Arguments = macro.Arguments; return(new ExpressionStatement(mie)); } Block block = new Block(); for (int i = 0; i < argc - 1; ++i) { block.Add( AstUtil.CreateMethodInvocationExpression( li, writePrototype.CloneNode(), macro.Arguments[i])); block.Add( AstUtil.CreateMethodInvocationExpression( li, writePrototype.CloneNode(), new StringLiteralExpression(" "))); } block.Add( AstUtil.CreateMethodInvocationExpression( li, writeLinePrototype.CloneNode(), macro.Arguments[-1])); return(block); }
public override Statement Expand(MacroStatement macro) { if (macro.Arguments.Count != 1) { throw new ScriptParsingException("'with' must be called with only a single argument followed by a block."); } var arg = macro.Arguments[0]; var instanceExpression = ConvertExpressionToTemporaryVariable(arg, macro.Body); var runnableParser = new RunnableParser(NameResolutionService); if (runnableParser.IsRunnable(arg)) { AddRunExpressionToBody(macro.Body, instanceExpression); } var visitor = new OmittedReferenceVisitor(instanceExpression); visitor.Visit(macro.Body); return(macro.Body); }
/// <summary> /// Expands the specified macro /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> public override Statement Expand(MacroStatement macro) { classDefinition = CreateClassDefinition(macro); CreateMethodFromMacroBlock(macro, classDefinition); Module ancestor = (Module)macro.GetAncestor(NodeType.Module); ancestor.Members.Add(classDefinition); AddMemberMethods(macro); if (isAnonymous == false) { return(null); } ReferenceExpression typeName = AstUtil.CreateReferenceExpression(GetClassName(macro)); MethodInvocationExpression createInstance = new MethodInvocationExpression(typeName); return(new ExpressionStatement(createInstance)); }
IEntity ResolveMacroExtensionType(MacroStatement node, IMethod extension) { if (null == extension) { return(null); } IType extendedMacroType = GetExtendedMacroType(extension); if (null == extendedMacroType) { return(null); } //ok now check if extension is correctly nested under parent foreach (MacroStatement parent in node.GetAncestors <MacroStatement>()) { if (ResolveMacroName(parent) == extendedMacroType) { return(GetExtensionMacroType(extension)); } } return(null); }
private void ProcessMacro(Type actualType, MacroStatement node) { // HACK: workaround for mono if (-1 == Array.IndexOf(actualType.GetInterfaces(), typeof(IAstMacro))) // if (!typeof(IAstMacro).IsAssignableFrom(actualType)) { Errors.Add(CompilerErrorFactory.InvalidMacro(node, actualType.FullName)); return; } try { Statement replacement = ExpandMacro(actualType, node); if (null != node.Modifier) { replacement = NormalizeStatementModifiers.CreateModifiedStatement(node.Modifier, replacement); } ReplaceCurrentNode(replacement); } catch (Exception error) { Errors.Add(CompilerErrorFactory.MacroExpansionError(node, error)); } }
protected override bool ExpandExtension(ref MethodInvocationExpression extension, MacroStatement macro, MacroStatement parent, ref Statement expansion) { Expression eventName = ObtainEventName(macro); if (eventName == null) { AddCompilerError(macro.LexicalInfo, "A wireEvent statement must be in the form wireEvent eventName"); return(false); } HashLiteralExpression listeners = ObtainListeners(macro); if (listeners != null && listeners.Items.Count > 0) { extension.Arguments.Add(eventName); extension.Arguments.Add(listeners); return(true); } return(false); }
/// <summary> /// Moves the constructor arguments from the macro to the superInvocation method invocation /// </summary> /// <param name="constructor">The constructor.</param> /// <param name="superInvocation">The create.</param> /// <param name="macro">The macro.</param> protected void MoveConstructorArguments( Constructor constructor, MethodInvocationExpression superInvocation, MacroStatement macro) { for (int i = argumentStartIndex; i < macro.Arguments.Count; i++) { Expression argument = macro.Arguments[i]; BinaryExpression assign; MethodInvocationExpression call; if (TryGetAssignment(argument, out assign)) { constructor.Body.Add(assign); } else if (TryGetCall(argument, out call)) { constructor.Body.Add(call); } else { superInvocation.Arguments.Add(argument); } } }
/// <summary> /// Perform the actual expansion of the macro /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> protected override Statement DoExpand(MacroStatement macro) { Block body = (Block)macro.GetAncestor(NodeType.Block); if (macro.Body.Statements.Count < 1) { Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "Join" + name + " section must contain at least a single expression statement")); return(null); } foreach (Statement statement in macro.Body.Statements) { ExpressionStatement exprStmt = statement as ExpressionStatement; if (exprStmt == null) { Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "Join" + name + " section can only contain expressions")); return(null); } Expression expr = exprStmt.Expression; MethodInvocationExpression expression = new MethodInvocationExpression(new ReferenceExpression(name), expr); body.Add(new ExpressionStatement(expression)); } return(null); }
protected override bool ExpandExtension(ref MethodInvocationExpression extension, MacroStatement macro, MacroStatement parent, ref Statement expansion) { if (macro.Arguments.Count < 1) { AddMissingLifestyleTypeError(macro); return(false); } var lifestyle = macro.Arguments[0] as ReferenceExpression; if (lifestyle == null) { AddMissingLifestyleTypeError(macro); return(false); } macro.Arguments.RemoveAt(0); var fqLifestyle = String.Format("{0}.{1}", typeof(LifestyleExtension).Namespace, lifestyle.Name); var entity = NameResolutionService.ResolveQualifiedName(fqLifestyle); if (entity == null) { entity = NameResolutionService.Resolve(lifestyle.Name); } if (entity == null || entity.EntityType != EntityType.Type) { AddCompilerError(macro.LexicalInfo, string.Format( "{0} or {1} is not a valid lifestyle in the current scope", fqLifestyle, lifestyle.Name)); return(false); } var lifestyleType = ((ExternalType)entity).ActualType; return(InitializeLifestyleExtension(ref extension, lifestyle, lifestyleType) && ArgumentsToCreateNamedArguments(macro.Arguments, extension)); }
private void AddMissingLifestyleTypeError(MacroStatement macro) { AddCompilerError(macro.LexicalInfo, "A lifestyle statement must specify a valid lifestyle"); }
private static bool MacroDefinitionContainsMacroApplication(TypeDefinition definition, MacroStatement statement) { return(statement.GetAncestors <TypeDefinition>().Any(ancestor => ancestor == definition)); }
private static bool LooksLikeOldStyleFieldDeclaration(MacroStatement node) { return(node.Arguments.Count == 0 && node.Body.IsEmpty); }
private void ExpandKnownMacro(MacroStatement node, IType macroType) { ExpandChildrenOfMacroOnMacroNamespace(node, macroType); ProcessMacro(macroType, node); }
override public void LeaveMacroStatement(MacroStatement node) { LeaveStatement(node); }
protected override Statement ExpandImpl(MacroStatement @case) { CaseStatement statement; if (@case == null) { throw new ArgumentNullException("case"); } this.__macro = @case; CaseStatement statement1 = statement = new CaseStatement(); ExpressionCollection collection1 = statement.Expressions = @case.get_Arguments(); Block block1 = statement.Body = @case.get_Body(); return statement; }
protected abstract Statement ExpandImpl(MacroStatement macro);
private static bool IsTypeMemberMacro(MacroStatement node) { return(node.ParentNode.NodeType == NodeType.StatementTypeMember); }
public override Statement Expand(MacroStatement macro) { Debug.Assert(0 == macro.Arguments.Count); macro.Block["checked"] = true; return(macro.Block); }
protected override Statement ExpandImpl(MacroStatement @default) { DefaultStatement statement; if (@default == null) { throw new ArgumentNullException("default"); } this.__macro = @default; if (this.__macro.get_Arguments().get_Count() != 0) { throw new Exception("`default` macro invocation argument(s) did not match definition: `default()`"); } DefaultStatement statement1 = statement = new DefaultStatement(); Block block1 = statement.Body = @default.get_Body(); return statement; }