本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetBinderFactory方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpCompilation.GetBinderFactory方法的具体用法?C# CSharpCompilation.GetBinderFactory怎么用?C# CSharpCompilation.GetBinderFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.CSharpCompilation
的用法示例。
在下文中一共展示了CSharpCompilation.GetBinderFactory方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SyntaxTreeSemanticModel
internal SyntaxTreeSemanticModel(CSharpCompilation compilation, SyntaxTree syntaxTree)
{
_compilation = compilation;
_syntaxTree = syntaxTree;
if (!this.Compilation.SyntaxTrees.Contains(syntaxTree))
{
throw new ArgumentOutOfRangeException("tree", CSharpResources.TreeNotPartOfCompilation);
}
_binderFactory = compilation.GetBinderFactory(SyntaxTree);
}
示例2: SyntaxTreeSemanticModel
internal SyntaxTreeSemanticModel(CSharpCompilation compilation, SyntaxTree syntaxTree, bool ignoreAccessibility = false)
{
_compilation = compilation;
_syntaxTree = syntaxTree;
_ignoresAccessibility = ignoreAccessibility;
if (!this.Compilation.SyntaxTrees.Contains(syntaxTree))
{
throw new ArgumentOutOfRangeException(nameof(syntaxTree), CSharpResources.TreeNotPartOfCompilation);
}
_binderFactory = compilation.GetBinderFactory(SyntaxTree);
}
示例3: BindConstructorInitializer
//.........这里部分代码省略.........
{
if (baseType.SpecialType == SpecialType.System_Object)
{
return GenerateObjectConstructorInitializer(constructor, diagnostics);
}
else if (baseType.IsErrorType() || baseType.IsStatic)
{
// If the base type is bad and there is no initializer then we can just bail.
// We have no expressions we need to analyze to report errors on.
return null;
}
}
// Either our base type is not object, or we have an initializer syntax, or both. We're going to
// need to do overload resolution on the set of constructors of the base type, either on
// the provided initializer syntax, or on an implicit ": base()" syntax.
// SPEC ERROR: The specification states that if you have the situation
// SPEC ERROR: class B { ... } class D1 : B {} then the default constructor
// SPEC ERROR: generated for D1 must call an accessible *parameterless* constructor
// SPEC ERROR: in B. However, it also states that if you have
// SPEC ERROR: class B { ... } class D2 : B { D2() {} } or
// SPEC ERROR: class B { ... } class D3 : B { D3() : base() {} } then
// SPEC ERROR: the compiler performs *overload resolution* to determine
// SPEC ERROR: which accessible constructor of B is called. Since B might have
// SPEC ERROR: a ctor with all optional parameters, overload resolution might
// SPEC ERROR: succeed even if there is no parameterless constructor. This
// SPEC ERROR: is unintentionally inconsistent, and the native compiler does not
// SPEC ERROR: implement this behavior. Rather, we should say in the spec that
// SPEC ERROR: if there is no ctor in D1, then a ctor is created for you exactly
// SPEC ERROR: as though you'd said "D1() : base() {}".
// SPEC ERROR: This is what we now do in Roslyn.
// Now, in order to do overload resolution, we're going to need a binder. There are
// three possible situations:
//
// class D1 : B { }
// class D2 : B { D2(int x) { } }
// class D3 : B { D3(int x) : base(x) { } }
//
// In the first case the binder needs to be the binder associated with
// the *body* of D1 because if the base class ctor is protected, we need
// to be inside the body of a derived class in order for it to be in the
// accessibility domain of the protected base class ctor.
//
// In the second case the binder could be the binder associated with
// the body of D2; since the implicit call to base() will have no arguments
// there is no need to look up "x".
//
// In the third case the binder must be the binder that knows about "x"
// because x is in scope.
Binder outerBinder;
if ((object)sourceConstructor == null)
{
// The constructor is implicit. We need to get the binder for the body
// of the enclosing class.
CSharpSyntaxNode containerNode = constructor.GetNonNullSyntaxNode();
SyntaxToken bodyToken;
if (containerNode.Kind == SyntaxKind.ClassDeclaration)
{
bodyToken = ((ClassDeclarationSyntax)containerNode).OpenBraceToken;
}
else if (containerNode.Kind == SyntaxKind.StructDeclaration)
{
bodyToken = ((StructDeclarationSyntax)containerNode).OpenBraceToken;
}
else if (containerNode.Kind == SyntaxKind.EnumDeclaration)
{
// We're not going to find any non-default ctors, but we'll look anyway.
bodyToken = ((EnumDeclarationSyntax)containerNode).OpenBraceToken;
}
else
{
Debug.Assert(false, "How did we get an implicit constructor added to something that is neither a class nor a struct?");
bodyToken = containerNode.GetFirstToken();
}
outerBinder = compilation.GetBinderFactory(containerNode.SyntaxTree).GetBinder(containerNode, bodyToken.Position);
}
else if (initializerArgumentListOpt == null)
{
// We have a ctor in source but no explicit constructor initializer. We can't just use the binder for the
// type containing the ctor because the ctor might be marked unsafe. Use the binder for the parameter list
// as an approximation - the extra symbols won't matter because there are no identifiers to bind.
outerBinder = compilation.GetBinderFactory(sourceConstructor.SyntaxTree).GetBinder(syntax.Kind == SyntaxKind.ParameterList ?
syntax :
((ConstructorDeclarationSyntax)syntax).ParameterList);
}
else
{
outerBinder = compilation.GetBinderFactory(sourceConstructor.SyntaxTree).GetBinder(initializerArgumentListOpt);
}
//wrap in ConstructorInitializerBinder for appropriate errors
Binder initializerBinder = outerBinder.WithAdditionalFlagsAndContainingMemberOrLambda(BinderFlags.ConstructorInitializer, constructor);
return initializerBinder.BindConstructorInitializer(initializerArgumentListOpt, constructor, diagnostics);
}
示例4: BindScriptFieldInitializers
/// <summary>
/// In script C#, some field initializers are assignments to fields and others are global
/// statements. There are no restrictions on accessing instance members.
/// </summary>
private static void BindScriptFieldInitializers(CSharpCompilation compilation, MethodSymbol scriptCtor,
ImmutableArray<ImmutableArray<FieldInitializer>> initializers, ArrayBuilder<BoundInitializer> boundInitializers, DiagnosticBag diagnostics,
bool generateDebugInfo, out ConsList<Imports> firstDebugImports)
{
Debug.Assert((object)scriptCtor != null);
firstDebugImports = null;
for (int i = 0; i < initializers.Length; i++)
{
ImmutableArray<FieldInitializer> siblingInitializers = initializers[i];
// All sibling initializers share the same parent node and tree so we can reuse the binder
// factory across siblings. Unfortunately, we cannot reuse the binder itself, because
// individual fields might have their own binders (e.g. because of being declared unsafe).
BinderFactory binderFactory = null;
for (int j = 0; j < siblingInitializers.Length; j++)
{
var initializer = siblingInitializers[j];
var fieldSymbol = initializer.Field;
if ((object)fieldSymbol != null && fieldSymbol.IsConst)
{
// Constants do not need field initializers.
continue;
}
var syntaxRef = initializer.Syntax;
Debug.Assert(syntaxRef.SyntaxTree.Options.Kind != SourceCodeKind.Regular);
var initializerNode = (CSharpSyntaxNode)syntaxRef.GetSyntax();
if (binderFactory == null)
{
binderFactory = compilation.GetBinderFactory(syntaxRef.SyntaxTree);
}
Binder scriptClassBinder = binderFactory.GetBinder(initializerNode);
Debug.Assert(((ImplicitNamedTypeSymbol)scriptClassBinder.ContainingMemberOrLambda).IsScriptClass);
if (generateDebugInfo && firstDebugImports == null)
{
firstDebugImports = scriptClassBinder.ImportsList;
}
Binder parentBinder = new ExecutableCodeBinder((CSharpSyntaxNode)syntaxRef.SyntaxTree.GetRoot(), scriptCtor, scriptClassBinder);
BoundInitializer boundInitializer;
if ((object)fieldSymbol != null)
{
boundInitializer = BindFieldInitializer(
new LocalScopeBinder(parentBinder).WithAdditionalFlagsAndContainingMemberOrLambda(parentBinder.Flags | BinderFlags.FieldInitializer, fieldSymbol),
fieldSymbol,
(EqualsValueClauseSyntax)initializerNode,
diagnostics);
}
else if (initializerNode.Kind == SyntaxKind.LabeledStatement)
{
// TODO: labels in interactive
var boundStatement = new BoundBadStatement(initializerNode, ImmutableArray<BoundNode>.Empty, true);
boundInitializer = new BoundGlobalStatementInitializer(initializerNode, boundStatement);
}
else
{
var collisionDetector = new LocalScopeBinder(parentBinder);
boundInitializer = BindGlobalStatement(collisionDetector, (StatementSyntax)initializerNode, diagnostics,
isLast: i == initializers.Length - 1 && j == siblingInitializers.Length - 1);
}
boundInitializers.Add(boundInitializer);
}
}
}
示例5: BindRegularCSharpFieldInitializers
/// <summary>
/// In regular C#, all field initializers are assignments to fields and the assigned expressions
/// may not reference instance members.
/// </summary>
private static void BindRegularCSharpFieldInitializers(
CSharpCompilation compilation,
ImmutableArray<ImmutableArray<FieldInitializer>> initializers,
ArrayBuilder<BoundInitializer> boundInitializers,
DiagnosticBag diagnostics,
bool generateDebugInfo,
out ConsList<Imports> firstDebugImports)
{
firstDebugImports = null;
foreach (ImmutableArray<FieldInitializer> siblingInitializers in initializers)
{
// All sibling initializers share the same parent node and tree so we can reuse the binder
// factory across siblings. Unfortunately, we cannot reuse the binder itself, because
// individual fields might have their own binders (e.g. because of being declared unsafe).
BinderFactory binderFactory = null;
foreach (FieldInitializer initializer in siblingInitializers)
{
FieldSymbol fieldSymbol = initializer.Field;
Debug.Assert((object)fieldSymbol != null);
// A constant field of type decimal needs a field initializer, so
// check if it is a metadata constant, not just a constant to exclude
// decimals. Other constants do not need field initializers.
if (!fieldSymbol.IsMetadataConstant)
{
//Can't assert that this is a regular C# compilation, because we could be in a nested type of a script class.
SyntaxReference syntaxRef = initializer.Syntax;
var initializerNode = (CSharpSyntaxNode)syntaxRef.GetSyntax();
if (binderFactory == null)
{
binderFactory = compilation.GetBinderFactory(syntaxRef.SyntaxTree);
}
Binder parentBinder = binderFactory.GetBinder(initializerNode);
Debug.Assert(parentBinder.ContainingMemberOrLambda == fieldSymbol.ContainingType || //should be the binder for the type
fieldSymbol.ContainingType.IsImplicitClass); //however, we also allow fields in namespaces to help support script scenarios
if (generateDebugInfo && firstDebugImports == null)
{
firstDebugImports = parentBinder.ImportsList;
}
BoundInitializer boundInitializer = BindFieldInitializer(
new LocalScopeBinder(parentBinder.WithPrimaryConstructorParametersIfNecessary(fieldSymbol.ContainingType, shadowBackingFields: true)).
WithAdditionalFlagsAndContainingMemberOrLambda(parentBinder.Flags | BinderFlags.FieldInitializer, fieldSymbol),
fieldSymbol,
(EqualsValueClauseSyntax)initializerNode,
diagnostics);
boundInitializers.Add(boundInitializer);
}
}
}
}
示例6: GetAttributesToBind
/// <summary>
/// Method to merge attributes from the given attributesSyntaxLists and filter out attributes by attribute target.
/// This is the first step in attribute binding.
/// </summary>
/// <remarks>
/// This method can generate diagnostics for few cases where we have an invalid target specifier and the parser hasn't generated the necessary diagnostics.
/// It should not perform any bind operations as it can lead to an attribute binding cycle.
/// </remarks>
private ImmutableArray<AttributeSyntax> GetAttributesToBind(
OneOrMany<SyntaxList<AttributeListSyntax>> attributeDeclarationSyntaxLists,
AttributeLocation symbolPart,
DiagnosticBag diagnostics,
CSharpCompilation compilation,
out ImmutableArray<Binder> binders)
{
var attributeTarget = (IAttributeTargetSymbol)this;
ArrayBuilder<AttributeSyntax> syntaxBuilder = null;
ArrayBuilder<Binder> bindersBuilder = null;
int attributesToBindCount = 0;
for (int listIndex = 0; listIndex < attributeDeclarationSyntaxLists.Count; listIndex++)
{
var attributeDeclarationSyntaxList = attributeDeclarationSyntaxLists[listIndex];
if (attributeDeclarationSyntaxList.Any())
{
int prevCount = attributesToBindCount;
foreach (var attributeDeclarationSyntax in attributeDeclarationSyntaxList)
{
// We bind the attribute only if it has a matching target for the given ownerSymbol and attributeLocation.
if (MatchAttributeTarget(attributeTarget, symbolPart, attributeDeclarationSyntax.Target, diagnostics))
{
if (syntaxBuilder == null)
{
syntaxBuilder = new ArrayBuilder<AttributeSyntax>();
bindersBuilder = new ArrayBuilder<Binder>();
}
var attributesToBind = attributeDeclarationSyntax.Attributes;
syntaxBuilder.AddRange(attributesToBind);
attributesToBindCount += attributesToBind.Count;
}
}
if (attributesToBindCount != prevCount)
{
Debug.Assert(attributeDeclarationSyntaxList.Node != null);
Debug.Assert(bindersBuilder != null);
var syntaxTree = attributeDeclarationSyntaxList.Node.SyntaxTree;
var binder = compilation.GetBinderFactory(syntaxTree).GetBinder((CSharpSyntaxNode)attributeDeclarationSyntaxList.Node);
binder = new ContextualAttributeBinder(binder, this);
Debug.Assert(!binder.InAttributeArgument, "Possible cycle in attribute binding");
for (int i = 0; i < attributesToBindCount - prevCount; i++)
{
bindersBuilder.Add(binder);
}
}
}
}
if (syntaxBuilder != null)
{
binders = bindersBuilder.ToImmutableAndFree();
return syntaxBuilder.ToImmutableAndFree();
}
else
{
binders = ImmutableArray<Binder>.Empty;
return ImmutableArray<AttributeSyntax>.Empty;
}
}
示例7: BindScriptFieldInitializers
/// <summary>
/// In script C#, some field initializers are assignments to fields and others are global
/// statements. There are no restrictions on accessing instance members.
/// </summary>
private static void BindScriptFieldInitializers(
CSharpCompilation compilation,
SynthesizedInteractiveInitializerMethod scriptInitializer,
ImmutableArray<ImmutableArray<FieldOrPropertyInitializer>> initializers,
ArrayBuilder<BoundInitializer> boundInitializers,
DiagnosticBag diagnostics,
out ImportChain firstDebugImports)
{
firstDebugImports = null;
for (int i = 0; i < initializers.Length; i++)
{
ImmutableArray<FieldOrPropertyInitializer> siblingInitializers = initializers[i];
// All sibling initializers share the same parent node and tree so we can reuse the binder
// factory across siblings. Unfortunately, we cannot reuse the binder itself, because
// individual fields might have their own binders (e.g. because of being declared unsafe).
BinderFactory binderFactory = null;
// Label instances must be shared across all global statements.
ScriptLocalScopeBinder.Labels labels = null;
for (int j = 0; j < siblingInitializers.Length; j++)
{
var initializer = siblingInitializers[j];
var fieldSymbol = initializer.FieldOpt;
if ((object)fieldSymbol != null && fieldSymbol.IsConst)
{
// Constants do not need field initializers.
continue;
}
var syntaxRef = initializer.Syntax;
var syntaxTree = syntaxRef.SyntaxTree;
Debug.Assert(syntaxTree.Options.Kind != SourceCodeKind.Regular);
var syntax = (CSharpSyntaxNode)syntaxRef.GetSyntax();
var syntaxRoot = syntaxTree.GetCompilationUnitRoot();
if (binderFactory == null)
{
binderFactory = compilation.GetBinderFactory(syntaxTree);
labels = new ScriptLocalScopeBinder.Labels(scriptInitializer, syntaxRoot);
}
Binder scriptClassBinder = binderFactory.GetBinder(syntax);
Debug.Assert(((NamedTypeSymbol)scriptClassBinder.ContainingMemberOrLambda).IsScriptClass);
if (firstDebugImports == null)
{
firstDebugImports = scriptClassBinder.ImportChain;
}
Binder parentBinder = new ExecutableCodeBinder(
syntaxRoot,
scriptInitializer,
new ScriptLocalScopeBinder(labels, scriptClassBinder));
BoundInitializer boundInitializer;
if ((object)fieldSymbol != null)
{
boundInitializer = BindFieldInitializer(
parentBinder.WithAdditionalFlagsAndContainingMemberOrLambda(BinderFlags.FieldInitializer, fieldSymbol),
fieldSymbol,
(EqualsValueClauseSyntax)syntax,
diagnostics);
}
else
{
boundInitializer = BindGlobalStatement(
parentBinder,
scriptInitializer,
(StatementSyntax)syntax,
diagnostics,
isLast: i == initializers.Length - 1 && j == siblingInitializers.Length - 1);
}
boundInitializers.Add(boundInitializer);
}
}
}
示例8: GetFieldInitializerInfos
internal static ImmutableArray<LocalSymbol> GetFieldInitializerInfos(
CSharpCompilation compilation,
FieldInitializers siblingInitializers,
ArrayBuilder<FieldInitializerInfo> infos,
bool generateDebugInfo,
ref ConsList<Imports> firstDebugImports)
{
// All sibling initializers share the same parent node and tree so we can reuse the binder
// factory across siblings. Unfortunately, we cannot reuse the binder itself, because
// individual fields might have their own binders (e.g. because of being declared unsafe).
BinderFactory binderFactory = null;
foreach (FieldInitializer initializer in siblingInitializers.Initializers)
{
FieldSymbol fieldSymbol = initializer.Field;
Debug.Assert((object)fieldSymbol != null);
// A constant field of type decimal needs a field initializer, so
// check if it is a metadata constant, not just a constant to exclude
// decimals. Other constants do not need field initializers.
if (!fieldSymbol.IsMetadataConstant)
{
//Can't assert that this is a regular C# compilation, because we could be in a nested type of a script class.
SyntaxReference syntaxRef = initializer.Syntax;
var initializerNode = (EqualsValueClauseSyntax)syntaxRef.GetSyntax();
if (binderFactory == null)
{
binderFactory = compilation.GetBinderFactory(syntaxRef.SyntaxTree);
}
Binder parentBinder = binderFactory.GetBinder(initializerNode);
Debug.Assert(parentBinder.ContainingMemberOrLambda == fieldSymbol.ContainingType || //should be the binder for the type
fieldSymbol.ContainingType.IsImplicitClass); //however, we also allow fields in namespaces to help support script scenarios
if (generateDebugInfo && firstDebugImports == null)
{
firstDebugImports = parentBinder.ImportsList;
}
parentBinder = new LocalScopeBinder(parentBinder).WithAdditionalFlagsAndContainingMemberOrLambda(parentBinder.Flags | BinderFlags.FieldInitializer, fieldSymbol);
if (!fieldSymbol.IsConst && !fieldSymbol.IsStatic)
{
parentBinder = parentBinder.WithPrimaryConstructorParametersIfNecessary(fieldSymbol.ContainingType);
}
infos.Add(new FieldInitializerInfo(initializer, parentBinder, initializerNode));
}
}
// See if there are locals that we need to bring into the scope.
var locals = default(ImmutableArray<LocalSymbol>);
if (siblingInitializers.TypeDeclarationSyntax != null)
{
locals = GetInitializationScopeLocals(infos);
if (!locals.IsDefaultOrEmpty)
{
for (int i = 0; i < infos.Count; i++)
{
FieldInitializerInfo info = infos[i];
// Constant initializers is not part of the initialization scope.
if (!info.Initializer.Field.IsConst)
{
infos[i] = new FieldInitializerInfo(info.Initializer,
new SimpleLocalScopeBinder(locals, info.Binder),
info.EqualsValue);
}
}
}
}
return locals;
}