本文整理汇总了C#中Microsoft.CodeAnalysis.Emit.EmitContext类的典型用法代码示例。如果您正苦于以下问题:C# EmitContext类的具体用法?C# EmitContext怎么用?C# EmitContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EmitContext类属于Microsoft.CodeAnalysis.Emit命名空间,在下文中一共展示了EmitContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetMetadataConstantValue
internal Cci.IMetadataConstant GetMetadataConstantValue(EmitContext context)
{
if (!HasMetadataConstantValue)
{
return null;
}
ConstantValue constant = this.ExplicitDefaultConstantValue;
TypeSymbol type;
if (constant.SpecialType != SpecialType.None)
{
// preserve the exact type of the constant for primitive types,
// e.g. it should be Int16 for [DefaultParameterValue((short)1)]int x
type = this.ContainingAssembly.GetSpecialType(constant.SpecialType);
}
else
{
// default(struct), enum
type = this.Type;
}
return ((PEModuleBuilder)context.Module).CreateConstant(type, constant.Value,
syntaxNodeOpt: (CSharpSyntaxNode)context.SyntaxNodeOpt,
diagnostics: context.Diagnostics);
}
示例2:
Cci.INamedTypeReference Cci.IGenericTypeInstanceReference.GetGenericType(EmitContext context)
{
System.Diagnostics.Debug.Assert(UnderlyingNamedType.OriginalDefinition.IsDefinition);
PEModuleBuilder moduleBeingBuilt = (PEModuleBuilder)context.Module;
return moduleBeingBuilt.Translate(this.UnderlyingNamedType.OriginalDefinition, syntaxNodeOpt: (CSharpSyntaxNode)context.SyntaxNodeOpt,
diagnostics: context.Diagnostics, needDeclaration: true);
}
示例3: PEDeltaAssemblyBuilder
public PEDeltaAssemblyBuilder(
SourceAssemblySymbol sourceAssembly,
string outputName,
OutputKind outputKind,
ModulePropertiesForSerialization serializationProperties,
IEnumerable<ResourceDescription> manifestResources,
Func<AssemblySymbol, AssemblyIdentity> assemblySymbolMapper,
EmitBaseline previousGeneration,
IEnumerable<SemanticEdit> edits)
: base(sourceAssembly, outputName, outputKind, serializationProperties, manifestResources, assemblySymbolMapper, additionalTypes: ImmutableArray<NamedTypeSymbol>.Empty, metadataOnly:false)
{
var context = new EmitContext(this, null, new DiagnosticBag());
var module = previousGeneration.OriginalMetadata;
var compilation = sourceAssembly.DeclaringCompilation;
var metadataAssembly = compilation.GetBoundReferenceManager().CreatePEAssemblyForAssemblyMetadata(AssemblyMetadata.Create(module), MetadataImportOptions.All);
var metadataDecoder = new Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE.MetadataDecoder(metadataAssembly.PrimaryModule);
previousGeneration = EnsureInitialized(previousGeneration, metadataDecoder);
var matchToMetadata = new SymbolMatcher(previousGeneration.AnonymousTypeMap, sourceAssembly, context, metadataAssembly);
SymbolMatcher matchToPrevious = null;
if (previousGeneration.Ordinal > 0)
{
var previousAssembly = ((CSharpCompilation)previousGeneration.Compilation).SourceAssembly;
var previousContext = new EmitContext((PEModuleBuilder)previousGeneration.PEModuleBuilder, null, new DiagnosticBag());
matchToPrevious = new SymbolMatcher(previousGeneration.AnonymousTypeMap, sourceAssembly, context, previousAssembly, previousContext);
}
this.previousDefinitions = new CSharpDefinitionMap(previousGeneration.OriginalMetadata.Module, edits, metadataDecoder, matchToMetadata, matchToPrevious);
this.previousGeneration = previousGeneration;
this.changes = new SymbolChanges(this.previousDefinitions, edits);
}
示例4:
Cci.INestedTypeReference Cci.ISpecializedNestedTypeReference.GetUnspecializedVersion(EmitContext context)
{
Debug.Assert(UnderlyingNamedType.OriginalDefinition.IsDefinition);
var result = ((PEModuleBuilder)context.Module).Translate(this.UnderlyingNamedType.OriginalDefinition,
(CSharpSyntaxNode)context.SyntaxNodeOpt, context.Diagnostics, needDeclaration: true).AsNestedTypeReference;
Debug.Assert(result != null);
return result;
}
示例5: return
Cci.IMethodReference Cci.IGenericMethodInstanceReference.GetGenericMethod(EmitContext context)
{
// NoPia method might come through here.
return ((PEModuleBuilder)context.Module).Translate(
UnderlyingMethod.OriginalDefinition,
syntaxNodeOpt: context.SyntaxNodeOpt,
diagnostics: context.Diagnostics,
needDeclaration: true);
}
示例6: foreach
IEnumerable<Cci.ITypeReference> Cci.IGenericMethodInstanceReference.GetGenericArguments(EmitContext context)
{
PEModuleBuilder moduleBeingBuilt = (PEModuleBuilder)context.Module;
foreach (var arg in UnderlyingMethod.TypeArguments)
{
yield return moduleBeingBuilt.Translate(arg, syntaxNodeOpt: (CSharpSyntaxNode)context.SyntaxNodeOpt, diagnostics: context.Diagnostics);
}
}
示例7: SerializeMethodDebugInfo
public byte[] SerializeMethodDebugInfo(EmitContext context, IMethodBody methodBody, uint methodToken, bool isEncDelta, bool suppressNewCustomDebugInfo, out bool emitExternNamespaces)
{
emitExternNamespaces = false;
// CONSIDER: this may not be the same "first" method as in Dev10, but
// it shouldn't matter since all methods will still forward to a method
// containing the appropriate information.
if (_methodBodyWithModuleInfo == null) //UNDONE: || edit-and-continue
{
// This module level information could go on every method (and does in
// the edit-and-continue case), but - as an optimization - we'll just
// put it on the first method we happen to encounter and then put a
// reference to the first method's token in every other method (so they
// can find the information).
if (context.Module.GetAssemblyReferenceAliases(context).Any())
{
_methodTokenWithModuleInfo = methodToken;
_methodBodyWithModuleInfo = methodBody;
emitExternNamespaces = true;
}
}
var customDebugInfo = ArrayBuilder<MemoryStream>.GetInstance();
SerializeIteratorClassMetadata(methodBody, customDebugInfo);
// NOTE: This is an attempt to match Dev10's apparent behavior. For iterator methods (i.e. the method
// that appears in source, not the synthesized ones), Dev10 only emits the ForwardIterator and IteratorLocal
// custom debug info (e.g. there will be no information about the usings that were in scope).
// NOTE: There seems to be an unusual behavior in ISymUnmanagedWriter where, if all the methods in a type are
// iterator methods, no custom debug info is emitted for any method. Adding a single non-iterator
// method causes the custom debug info to be produced for all methods (including the iterator methods).
// Since we are making the same ISymUnmanagedWriter calls as Dev10, we see the same behavior (i.e. this
// is not a regression).
if (methodBody.StateMachineTypeName == null)
{
SerializeNamespaceScopeMetadata(context, methodBody, customDebugInfo);
SerializeStateMachineLocalScopes(methodBody, customDebugInfo);
}
if (!suppressNewCustomDebugInfo)
{
SerializeDynamicLocalInfo(methodBody, customDebugInfo);
// delta doesn't need this information - we use information recorded by previous generation emit
if (!isEncDelta)
{
var encMethodInfo = MetadataWriter.GetEncMethodDebugInfo(methodBody);
SerializeCustomDebugInformation(encMethodInfo, customDebugInfo);
}
}
byte[] result = SerializeCustomDebugMetadata(customDebugInfo);
customDebugInfo.Free();
return result;
}
示例8: ReferenceEquals
Cci.IAssemblyReference Cci.IModuleReference.GetContainingAssembly(EmitContext context)
{
if (this.moduleBeingBuilt.OutputKind.IsNetModule() &&
ReferenceEquals(moduleBeingBuilt.SourceModule.ContainingAssembly, underlyingModule.ContainingAssembly))
{
return null;
}
return moduleBeingBuilt.Translate(underlyingModule.ContainingAssembly, context.Diagnostics);
}
示例9: PEDeltaAssemblyBuilder
public PEDeltaAssemblyBuilder(
SourceAssemblySymbol sourceAssembly,
EmitOptions emitOptions,
OutputKind outputKind,
Cci.ModulePropertiesForSerialization serializationProperties,
IEnumerable<ResourceDescription> manifestResources,
EmitBaseline previousGeneration,
IEnumerable<SemanticEdit> edits,
Func<ISymbol, bool> isAddedSymbol)
: base(sourceAssembly, emitOptions, outputKind, serializationProperties, manifestResources, additionalTypes: ImmutableArray<NamedTypeSymbol>.Empty)
{
var initialBaseline = previousGeneration.InitialBaseline;
var context = new EmitContext(this, null, new DiagnosticBag());
// Hydrate symbols from initial metadata. Once we do so it is important to reuse these symbols across all generations,
// in order for the symbol matcher to be able to use reference equality once it maps symbols to initial metadata.
var metadataSymbols = GetOrCreateMetadataSymbols(initialBaseline, sourceAssembly.DeclaringCompilation);
var metadataDecoder = (MetadataDecoder)metadataSymbols.MetadataDecoder;
var metadataAssembly = (PEAssemblySymbol)metadataDecoder.ModuleSymbol.ContainingAssembly;
var matchToMetadata = new CSharpSymbolMatcher(metadataSymbols.AnonymousTypes, sourceAssembly, context, metadataAssembly);
CSharpSymbolMatcher matchToPrevious = null;
if (previousGeneration.Ordinal > 0)
{
var previousAssembly = ((CSharpCompilation)previousGeneration.Compilation).SourceAssembly;
var previousContext = new EmitContext((PEModuleBuilder)previousGeneration.PEModuleBuilder, null, new DiagnosticBag());
matchToPrevious = new CSharpSymbolMatcher(
previousGeneration.AnonymousTypeMap,
sourceAssembly: sourceAssembly,
sourceContext: context,
otherAssembly: previousAssembly,
otherContext: previousContext,
otherSynthesizedMembersOpt: previousGeneration.SynthesizedMembers);
}
_previousDefinitions = new CSharpDefinitionMap(previousGeneration.OriginalMetadata.Module, edits, metadataDecoder, matchToMetadata, matchToPrevious);
_previousGeneration = previousGeneration;
_changes = new SymbolChanges(_previousDefinitions, edits, isAddedSymbol);
// Workaround for https://github.com/dotnet/roslyn/issues/3192.
// When compiling state machine we stash types of awaiters and state-machine hoisted variables,
// so that next generation can look variables up and reuse their slots if possible.
//
// When we are about to allocate a slot for a lifted variable while compiling the next generation
// we map its type to the previous generation and then check the slot types that we stashed earlier.
// If the variable type matches we reuse it. In order to compare the previous variable type with the current one
// both need to be completely lowered (translated). Standard translation only goes one level deep.
// Generic arguments are not translated until they are needed by metadata writer.
//
// In order to get the fully lowered form we run the type symbols of stashed variables thru a deep translator
// that translates the symbol recursively.
_deepTranslator = new CSharpSymbolMatcher.DeepTranslator(sourceAssembly.GetSpecialType(SpecialType.System_Object));
}
示例10: VisitTypeReference
private static void VisitTypeReference(Cci.ITypeReference typeReference, EmitContext context)
{
Debug.Assert(typeReference != null);
Cci.IArrayTypeReference arrayType = typeReference as Cci.IArrayTypeReference;
if (arrayType != null)
{
VisitTypeReference(arrayType.GetElementType(context), context);
return;
}
Cci.IPointerTypeReference pointerType = typeReference as Cci.IPointerTypeReference;
if (pointerType != null)
{
VisitTypeReference(pointerType.GetTargetType(context), context);
return;
}
Debug.Assert(!(typeReference is Cci.IManagedPointerTypeReference));
//Cci.IManagedPointerTypeReference managedPointerType = typeReference as Cci.IManagedPointerTypeReference;
//if (managedPointerType != null)
//{
// VisitTypeReference(managedPointerType.GetTargetType(this.context));
// return;
//}
Cci.IModifiedTypeReference modifiedType = typeReference as Cci.IModifiedTypeReference;
if (modifiedType != null)
{
foreach (var custModifier in modifiedType.CustomModifiers)
{
VisitTypeReference(custModifier.GetModifier(context), context);
}
VisitTypeReference(modifiedType.UnmodifiedType, context);
return;
}
// Visit containing type
Cci.INestedTypeReference nestedType = typeReference.AsNestedTypeReference;
if (nestedType != null)
{
VisitTypeReference(nestedType.GetContainingType(context), context);
}
// Visit generic arguments
Cci.IGenericTypeInstanceReference genericInstance = typeReference.AsGenericTypeInstanceReference;
if (genericInstance != null)
{
foreach (var arg in genericInstance.GetGenericArguments(context))
{
VisitTypeReference(arg, context);
}
}
}
示例11: foreach
ImmutableArray<Cci.ITypeReference> Cci.IGenericTypeInstanceReference.GetGenericArguments(EmitContext context)
{
PEModuleBuilder moduleBeingBuilt = (PEModuleBuilder)context.Module;
var builder = ArrayBuilder<Cci.ITypeReference>.GetInstance();
foreach (TypeSymbol type in UnderlyingNamedType.TypeArgumentsNoUseSiteDiagnostics)
{
builder.Add(moduleBeingBuilt.Translate(type, syntaxNodeOpt: (CSharpSyntaxNode)context.SyntaxNodeOpt, diagnostics: context.Diagnostics));
}
return builder.ToImmutableAndFree();
}
示例12:
Cci.PrimitiveTypeCode Cci.ITypeReference.TypeCode(EmitContext context)
{
Debug.Assert(this.IsDefinitionOrDistinct());
if (this.IsDefinition)
{
return this.PrimitiveTypeCode;
}
return Cci.PrimitiveTypeCode.NotPrimitive;
}
示例13: GetConsolidatedTypeArguments
internal static void GetConsolidatedTypeArguments(this ITypeReference typeReference, ArrayBuilder<ITypeReference> consolidatedTypeArguments, EmitContext context)
{
INestedTypeReference nestedTypeReference = typeReference.AsNestedTypeReference;
nestedTypeReference?.GetContainingType(context).GetConsolidatedTypeArguments(consolidatedTypeArguments, context);
IGenericTypeInstanceReference genTypeInstance = typeReference.AsGenericTypeInstanceReference;
if (genTypeInstance != null)
{
consolidatedTypeArguments.AddRange(genTypeInstance.GetGenericArguments(context));
}
}
示例14:
Cci.ITypeReference Cci.IPointerTypeReference.GetTargetType(EmitContext context)
{
var type = ((PEModuleBuilder)context.Module).Translate(this.PointedAtType, syntaxNodeOpt: (CSharpSyntaxNode)context.SyntaxNodeOpt, diagnostics: context.Diagnostics);
if (this.CustomModifiers.Length == 0)
{
return type;
}
else
{
return new Cci.ModifiedTypeReference(type, this.CustomModifiers.As<Cci.ICustomModifier>());
}
}
示例15: return
Cci.ITypeReference Cci.ITypeMemberReference.GetContainingType(EmitContext context)
{
PEModuleBuilder moduleBeingBuilt = (PEModuleBuilder)context.Module;
Debug.Assert(this.IsDefinitionOrDistinct());
if (!this.IsDefinition)
{
return moduleBeingBuilt.Translate(this.ContainingType, context.SyntaxNodeOpt, context.Diagnostics);
}
return (Cci.ITypeReference)this.ContainingType;
}