/// <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) //^ ensures this.containingBlock == containingBlock; { this.arguments = new List <Expression>(template.arguments); this.targets = template.targets; this.type = (AttributeTypeExpression)template.type.MakeCopyFor(containingBlock); 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) //^ ensures this.containingBlock == containingBlock; { this.arguments = new List<Expression>(template.arguments); this.targets = template.targets; this.type = (AttributeTypeExpression)template.type.MakeCopyFor(containingBlock); this.containingBlock = containingBlock; }
internal PlatformInvokeInformation(ITypeDeclarationMember declaringMember, SourceCustomAttribute dllImportAttribute) { this.importModule = Dummy.ModuleReference; this.importName = declaringMember.Name.Name; this.noMangle = false; this.pinvokeCallingConvention = PInvokeCallingConvention.WinApi; this.stringFormat = StringFormatKind.Unspecified; this.useBestFit = null; this.throwExceptionForUnmappableChar = null; INameTable nameTable = dllImportAttribute.ContainingBlock.NameTable; int bestFitMappingKey = nameTable.GetNameFor("BestFitMapping").UniqueKey; int callingConventionKey = nameTable.GetNameFor("CallingConvention").UniqueKey; int charSetKey = nameTable.GetNameFor("CharSet").UniqueKey; int entryPointKey = nameTable.GetNameFor("EntryPoint").UniqueKey; int exactSpellingKey = nameTable.GetNameFor("ExactSpelling").UniqueKey; int setLastErrorKey = nameTable.GetNameFor("SetLastError").UniqueKey; int throwOnUnmappableCharKey = nameTable.GetNameFor("ThrowOnUnmappableChar").UniqueKey; foreach (Expression expr in dllImportAttribute.Arguments) { CompileTimeConstant cc = expr as CompileTimeConstant; if (cc != null && cc.Value is string) { IName moduleName = expr.NameTable.GetNameFor((string)cc.Value); this.importModule = new Immutable.ModuleReference(dllImportAttribute.ContainingBlock.Compilation.HostEnvironment, new ModuleIdentity(moduleName, string.Empty)); continue; } NamedArgument narg = expr as NamedArgument; if (narg == null) continue; int key = narg.ArgumentName.Name.UniqueKey; if (key == bestFitMappingKey) { if (narg.ArgumentValue.Value is bool) this.useBestFit = (bool)narg.ArgumentValue.Value; continue; } if (key == callingConventionKey) { if (narg.ArgumentValue.Value is int) { switch ((CallingConvention)(int)narg.ArgumentValue.Value) { case CallingConvention.C: this.pinvokeCallingConvention = PInvokeCallingConvention.CDecl; break; case CallingConvention.Standard: this.pinvokeCallingConvention = PInvokeCallingConvention.StdCall; break; case CallingConvention.ExplicitThis: this.pinvokeCallingConvention = PInvokeCallingConvention.ThisCall; break; case CallingConvention.FastCall: this.pinvokeCallingConvention = PInvokeCallingConvention.FastCall; break; } } continue; } if (key == charSetKey) { if (narg.ArgumentValue.Value is int) { switch ((CharSet)(int)narg.ArgumentValue.Value) { case CharSet.Ansi: this.stringFormat = StringFormatKind.Ansi; break; case CharSet.Auto: this.stringFormat = StringFormatKind.AutoChar; break; case CharSet.Unicode: this.stringFormat = StringFormatKind.Unicode; break; } } continue; } if (key == entryPointKey) { string/*?*/ importName = narg.ArgumentValue.Value as string; if (importName != null) { this.importName = nameTable.GetNameFor(importName); } continue; } if (key == exactSpellingKey) { if (narg.ArgumentValue.Value is bool) this.noMangle = (bool)narg.ArgumentValue.Value; continue; } if (key == setLastErrorKey) { if (narg.ArgumentValue.Value is bool) this.supportsLastError = (bool)narg.ArgumentValue.Value; continue; } if (key == throwOnUnmappableCharKey) { if (narg.ArgumentValue.Value is bool) this.throwExceptionForUnmappableChar = (bool)narg.ArgumentValue.Value; continue; } } }
/// <summary> /// Constructs a metadata custom attribute that is a projection of a corresponding source custom attribute. /// </summary> /// <param name="sourceAttribute">The source custom attribute to project onto the symbol table attribute being constructed.</param> public CustomAttribute(SourceCustomAttribute sourceAttribute) { this.sourceAttribute = sourceAttribute; }
internal PlatformInvokeInformation(ITypeDeclarationMember declaringMember, SourceCustomAttribute dllImportAttribute) { this.importModule = Dummy.ModuleReference; this.importName = declaringMember.Name.Name; this.noMangle = false; this.pinvokeCallingConvention = PInvokeCallingConvention.WinApi; this.stringFormat = StringFormatKind.Unspecified; this.useBestFit = null; this.throwExceptionForUnmappableChar = null; INameTable nameTable = dllImportAttribute.ContainingBlock.NameTable; int bestFitMappingKey = nameTable.GetNameFor("BestFitMapping").UniqueKey; int callingConventionKey = nameTable.GetNameFor("CallingConvention").UniqueKey; int charSetKey = nameTable.GetNameFor("CharSet").UniqueKey; int entryPointKey = nameTable.GetNameFor("EntryPoint").UniqueKey; int exactSpellingKey = nameTable.GetNameFor("ExactSpelling").UniqueKey; int setLastErrorKey = nameTable.GetNameFor("SetLastError").UniqueKey; int throwOnUnmappableCharKey = nameTable.GetNameFor("ThrowOnUnmappableChar").UniqueKey; foreach (Expression expr in dllImportAttribute.Arguments) { CompileTimeConstant cc = expr as CompileTimeConstant; if (cc != null && cc.Value is string) { IName moduleName = expr.NameTable.GetNameFor((string)cc.Value); this.importModule = new Immutable.ModuleReference(dllImportAttribute.ContainingBlock.Compilation.HostEnvironment, new ModuleIdentity(moduleName, string.Empty)); continue; } NamedArgument narg = expr as NamedArgument; if (narg == null) { continue; } int key = narg.ArgumentName.Name.UniqueKey; if (key == bestFitMappingKey) { if (narg.ArgumentValue.Value is bool) { this.useBestFit = (bool)narg.ArgumentValue.Value; } continue; } if (key == callingConventionKey) { if (narg.ArgumentValue.Value is int) { switch ((CallingConvention)(int)narg.ArgumentValue.Value) { case CallingConvention.C: this.pinvokeCallingConvention = PInvokeCallingConvention.CDecl; break; case CallingConvention.Standard: this.pinvokeCallingConvention = PInvokeCallingConvention.StdCall; break; case CallingConvention.ExplicitThis: this.pinvokeCallingConvention = PInvokeCallingConvention.ThisCall; break; case CallingConvention.FastCall: this.pinvokeCallingConvention = PInvokeCallingConvention.FastCall; break; } } continue; } if (key == charSetKey) { if (narg.ArgumentValue.Value is int) { switch ((CharSet)(int)narg.ArgumentValue.Value) { case CharSet.Ansi: this.stringFormat = StringFormatKind.Ansi; break; case CharSet.Auto: this.stringFormat = StringFormatKind.AutoChar; break; case CharSet.Unicode: this.stringFormat = StringFormatKind.Unicode; break; } } continue; } if (key == entryPointKey) { string /*?*/ importName = narg.ArgumentValue.Value as string; if (importName != null) { this.importName = nameTable.GetNameFor(importName); } continue; } if (key == exactSpellingKey) { if (narg.ArgumentValue.Value is bool) { this.noMangle = (bool)narg.ArgumentValue.Value; } continue; } if (key == setLastErrorKey) { if (narg.ArgumentValue.Value is bool) { this.supportsLastError = (bool)narg.ArgumentValue.Value; } continue; } if (key == throwOnUnmappableCharKey) { if (narg.ArgumentValue.Value is bool) { this.throwExceptionForUnmappableChar = (bool)narg.ArgumentValue.Value; } continue; } } }
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; }