/// <summary> /// Allocates a custom attribute as it appears in source code. This may have special meaning to the compiler and might not be translated to a metadata custom attribute. /// </summary> /// <param name="targets">The kinds of symbols that are the targetted by this attribute. Usually a single target will be specified.</param> /// <param name="type"></param> /// <param name="arguments"></param> /// <param name="sourceLocation"></param> public SourceCustomAttribute(AttributeTargets targets, AttributeTypeExpression type, List <Expression> arguments, ISourceLocation sourceLocation) : base(sourceLocation) { this.arguments = arguments; this.targets = targets; this.type = type; }
private void ParseAttributes(ref List<SourceCustomAttribute>/*?*/ sourceAttributes, bool globalAttributes, TokenSet followers) { while (this.currentToken == Token.LeftBracket) { int position = this.scanner.CurrentDocumentPosition(); this.GetNextToken(); AttributeTargets target = this.ParseAttributeTarget(); if (globalAttributes) { if (target != AttributeTargets.Assembly && target != AttributeTargets.Module) { this.scanner.RestoreDocumentPosition(position); this.currentToken = Token.None; this.GetNextToken(); return; } } while (true) { Expression expr = this.ParseExpression(followers|Token.Comma|Token.RightBracket); MethodCall/*?*/ mcall = expr as MethodCall; if (mcall != null && (mcall.MethodExpression is SimpleName || mcall.MethodExpression is QualifiedName || mcall.MethodExpression is AliasQualifiedName)) { AttributeTypeExpression type = new AttributeTypeExpression(mcall.MethodExpression); List<Expression> arguments = new List<Expression>(mcall.OriginalArguments); bool seenNamedArgument = false; for (int i = 0, n = arguments.Count; i < n; i++) { Assignment/*?*/ assignment = arguments[i] as Assignment; if (assignment == null) { if (seenNamedArgument) this.HandleError(arguments[i].SourceLocation, Error.NamedArgumentExpected); continue; } SimpleName/*?*/ name = assignment.Target.Expression as SimpleName; if (name == null) { this.HandleError(assignment.Target.SourceLocation, Error.ExpectedIdentifier); name = new SimpleName(Dummy.Name, assignment.Target.SourceLocation, false); } seenNamedArgument = true; arguments[i] = new NamedArgument(name, assignment.Source, assignment.SourceLocation); } if (sourceAttributes == null) sourceAttributes = new List<SourceCustomAttribute>(1); sourceAttributes.Add(new SourceCustomAttribute(target, type, arguments, mcall.SourceLocation)); } else if (expr is SimpleName || expr is QualifiedName || expr is AliasQualifiedName) { AttributeTypeExpression type = new AttributeTypeExpression(expr); if (sourceAttributes == null) sourceAttributes = new List<SourceCustomAttribute>(1); sourceAttributes.Add(new SourceCustomAttribute(target, type, new List<Expression>(0), expr.SourceLocation)); } else { this.HandleError(expr.SourceLocation, Error.ExpectedIdentifier); } if (this.currentToken != Token.Comma) break; this.GetNextToken(); } this.Skip(Token.RightBracket); } if (sourceAttributes != null) sourceAttributes.TrimExcess(); }
/// <summary> /// Allocates a custom attribute as it appears in source code. This may have special meaning to the compiler and might not be translated to a metadata custom attribute. /// </summary> /// <param name="targets">The kinds of symbols that are the targetted by this attribute. Usually a single target will be specified.</param> /// <param name="type"></param> /// <param name="arguments"></param> /// <param name="sourceLocation"></param> public SourceCustomAttribute(AttributeTargets targets, AttributeTypeExpression type, List<Expression> arguments, ISourceLocation sourceLocation) : base(sourceLocation) { this.arguments = arguments; this.targets = targets; this.type = type; }
//^ ensures this.containingBlock == containingBlock; /// <summary> /// A copy constructor that allocates an instance that is the same as the given template, except for its containing block. /// </summary> /// <param name="containingBlock">A new value for containing block. This replaces template.ContainingBlock in the resulting copy of template.</param> /// <param name="template">The template to copy.</param> protected SourceCustomAttribute(BlockStatement containingBlock, SourceCustomAttribute template) : base(template.SourceLocation) { this.arguments = new List<Expression>(template.arguments); this.targets = template.targets; this.type = (AttributeTypeExpression)template.type.MakeCopyFor(containingBlock); this.containingBlock = containingBlock; }
internal static List<SourceCustomAttribute> ConvertSpecifiersIntoAttributes(IEnumerable<Specifier> specifiers, Expression/*!*/ containingExpression) { List<SourceCustomAttribute> result = new List<SourceCustomAttribute>(1); foreach (Specifier specifier in specifiers) { DeclspecSpecifier/*?*/ declSpec = specifier as DeclspecSpecifier; if (declSpec != null) { List<Expression> arguments = new List<Expression>(declSpec.Modifiers); if (arguments.Count < 1) continue; Expression attributeTypeName = arguments[0]; SimpleName/*?*/ simpleName = attributeTypeName as SimpleName; if (!(simpleName != null || attributeTypeName is QualifiedName || attributeTypeName is AliasQualifiedName)) continue; if (simpleName != null && IsUnsupportedDeclspec(simpleName.Name.Value)) continue; AttributeTypeExpression attributeType = new AttributeTypeExpression(attributeTypeName); arguments.RemoveAt(0); SourceCustomAttribute custAttr = new SourceCustomAttribute(AttributeTargets.All, attributeType, arguments, declSpec.SourceLocation); custAttr.SetContainingExpression(containingExpression); result.Add(custAttr); } else { SpecDeclspecSpecifier specTokenSpec = specifier as SpecDeclspecSpecifier; if (specTokenSpec != null) { var attrTypeName = NamespaceHelper.CreateInSystemDiagnosticsContractsCodeContractExpr(containingExpression.ContainingBlock.Compilation.NameTable, "StringVccAttr"); AttributeTypeExpression attrType = new AttributeTypeExpression(attrTypeName); var argument = new CompileTimeConstant(specTokenSpec.Argument, specTokenSpec.SourceLocation); List<Expression> args = new List<Expression> { new CompileTimeConstant(specTokenSpec.Token, specTokenSpec.SourceLocation), argument }; SourceCustomAttribute custAttr = new SourceCustomAttribute(AttributeTargets.All, attrType, args, specTokenSpec.SourceLocation); custAttr.SetContainingExpression(containingExpression); result.Add(custAttr); } } } result.TrimExcess(); return result; }