本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.BoundFieldAccess类的典型用法代码示例。如果您正苦于以下问题:C# BoundFieldAccess类的具体用法?C# BoundFieldAccess怎么用?C# BoundFieldAccess使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BoundFieldAccess类属于Microsoft.CodeAnalysis.CSharp命名空间,在下文中一共展示了BoundFieldAccess类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeFieldAccess
private BoundExpression MakeFieldAccess(
CSharpSyntaxNode syntax,
BoundExpression rewrittenReceiver,
FieldSymbol fieldSymbol,
ConstantValue constantValueOpt,
LookupResultKind resultKind,
TypeSymbol type,
BoundFieldAccess oldNodeOpt = null)
{
if (fieldSymbol.IsTupleField)
{
return MakeTupleFieldAccess(syntax, fieldSymbol, rewrittenReceiver, constantValueOpt, resultKind);
}
BoundExpression result = oldNodeOpt != null ?
oldNodeOpt.Update(rewrittenReceiver, fieldSymbol, constantValueOpt, resultKind, type) :
new BoundFieldAccess(syntax, rewrittenReceiver, fieldSymbol, constantValueOpt, resultKind, type);
if (fieldSymbol.IsFixed)
{
// a reference to a fixed buffer is translated into its address
result = new BoundConversion(syntax,
new BoundAddressOfOperator(syntax, result, syntax != null && SyntaxFacts.IsFixedStatementExpression(syntax), type, false),
new Conversion(ConversionKind.PointerToPointer), false, false, default(ConstantValue), type, false);
}
return result;
}
示例2: IsNonAgileFieldAccess
/// <remarks>
/// Based on OutputContext::IsNonAgileField.
/// </remarks>
internal static bool IsNonAgileFieldAccess(BoundFieldAccess fieldAccess, CSharpCompilation compilation)
{
// Warn if taking the address of a non-static field with a receiver other than this (possibly cast)
// and a type that descends from System.MarshalByRefObject.
if (IsInstanceFieldAccessWithNonThisReceiver(fieldAccess))
{
// NOTE: We're only trying to produce a warning, so there's no point in producing an
// error if the well-known type we need for the check is missing.
NamedTypeSymbol marshalByRefType = compilation.GetWellKnownType(WellKnownType.System_MarshalByRefObject);
TypeSymbol baseType = fieldAccess.FieldSymbol.ContainingType;
while ((object)baseType != null)
{
if (baseType == marshalByRefType)
{
return true;
}
// NOTE: We're only trying to produce a warning, so there's no point in producing a
// use site diagnostic if we can't walk up the base type hierarchy.
baseType = baseType.BaseTypeNoUseSiteDiagnostics;
}
}
return false;
}
示例3: CheckFieldAsReceiver
/// <remarks>
/// This is for when we are dotting into a field.
/// Distinguish from <see cref="CheckFieldAddress"/>.
///
/// NOTE: dev11 also calls this on string initializers in fixed statements,
/// but never accomplishes anything since string is a reference type. This
/// is probably a bug, but fixing it would be a breaking change.
/// </remarks>
private void CheckFieldAsReceiver(BoundFieldAccess fieldAccess)
{
// From ExpressionBinder.cpp:
// Taking the address of a field is suspect if the type is marshalbyref.
// REVIEW ShonK: Is this really the best way to handle this? It'd be so much more
// bullet proof for ilgen to error when it spits out the ldflda....
FieldSymbol fieldSymbol = fieldAccess.FieldSymbol;
if (IsNonAgileFieldAccess(fieldAccess, _compilation) && !fieldSymbol.Type.IsReferenceType)
{
Error(ErrorCode.WRN_CallOnNonAgileField, fieldAccess, fieldSymbol);
}
}
示例4: CheckFieldAddress
/// <remarks>
/// This is for when we are taking the address of a field.
/// Distinguish from <see cref="CheckFieldAsReceiver"/>.
/// </remarks>
private void CheckFieldAddress(BoundFieldAccess fieldAccess, Symbol consumerOpt)
{
FieldSymbol fieldSymbol = fieldAccess.FieldSymbol;
// We can safely suppress this warning when calling an Interlocked API
if (fieldSymbol.IsVolatile && ((object)consumerOpt == null || !IsInterlockedAPI(consumerOpt)))
{
Error(ErrorCode.WRN_VolatileByRef, fieldAccess, fieldSymbol);
}
if (IsNonAgileFieldAccess(fieldAccess, _compilation))
{
Error(ErrorCode.WRN_ByRefNonAgileField, fieldAccess, fieldSymbol);
}
}
示例5: ConstructFieldLikeEventAccessorBody_Regular
/// <summary>
/// Generate a thread-safe accessor for a regular field-like event.
///
/// DelegateType tmp0 = _event; //backing field
/// DelegateType tmp1;
/// DelegateType tmp2;
/// do {
/// tmp1 = tmp0;
/// tmp2 = (DelegateType)Delegate.Combine(tmp1, value); //Remove for -=
/// tmp0 = Interlocked.CompareExchange<DelegateType>(ref _event, tmp2, tmp1);
/// } while ((object)tmp0 != (object)tmp1);
/// </summary>
internal static BoundBlock ConstructFieldLikeEventAccessorBody_Regular(SourceEventSymbol eventSymbol, bool isAddMethod, CSharpCompilation compilation, DiagnosticBag diagnostics)
{
CSharpSyntaxNode syntax = eventSymbol.CSharpSyntaxNode;
TypeSymbol delegateType = eventSymbol.Type;
MethodSymbol accessor = isAddMethod ? eventSymbol.AddMethod : eventSymbol.RemoveMethod;
ParameterSymbol thisParameter = accessor.ThisParameter;
TypeSymbol boolType = compilation.GetSpecialType(SpecialType.System_Boolean);
MethodSymbol updateMethod = (MethodSymbol)compilation.GetSpecialTypeMember(isAddMethod ? SpecialMember.System_Delegate__Combine : SpecialMember.System_Delegate__Remove);
MethodSymbol compareExchangeMethod = GetConstructedCompareExchangeMethod(delegateType, compilation, accessor.Locations[0], diagnostics);
if ((object)compareExchangeMethod == null)
{
return new BoundBlock(syntax,
locals: ImmutableArray<LocalSymbol>.Empty,
statements: ImmutableArray.Create<BoundStatement>(
new BoundReturnStatement(syntax,
expressionOpt: null)
{ WasCompilerGenerated = true }))
{ WasCompilerGenerated = true };
}
GeneratedLabelSymbol loopLabel = new GeneratedLabelSymbol("loop");
const int numTemps = 3;
LocalSymbol[] tmps = new LocalSymbol[numTemps];
BoundLocal[] boundTmps = new BoundLocal[numTemps];
for (int i = 0; i < numTemps; i++)
{
tmps[i] = new SynthesizedLocal(accessor, delegateType, SynthesizedLocalKind.LoweringTemp);
boundTmps[i] = new BoundLocal(syntax, tmps[i], null, delegateType);
}
BoundThisReference fieldReceiver = eventSymbol.IsStatic ?
null :
new BoundThisReference(syntax, thisParameter.Type) { WasCompilerGenerated = true };
BoundFieldAccess boundBackingField = new BoundFieldAccess(syntax,
receiver: fieldReceiver,
fieldSymbol: eventSymbol.AssociatedField,
constantValueOpt: null)
{ WasCompilerGenerated = true };
BoundParameter boundParameter = new BoundParameter(syntax,
parameterSymbol: accessor.Parameters[0])
{ WasCompilerGenerated = true };
// tmp0 = _event;
BoundStatement tmp0Init = new BoundExpressionStatement(syntax,
expression: new BoundAssignmentOperator(syntax,
left: boundTmps[0],
right: boundBackingField,
type: delegateType)
{ WasCompilerGenerated = true })
{ WasCompilerGenerated = true };
// LOOP:
BoundStatement loopStart = new BoundLabelStatement(syntax,
label: loopLabel)
{ WasCompilerGenerated = true };
// tmp1 = tmp0;
BoundStatement tmp1Update = new BoundExpressionStatement(syntax,
expression: new BoundAssignmentOperator(syntax,
left: boundTmps[1],
right: boundTmps[0],
type: delegateType)
{ WasCompilerGenerated = true })
{ WasCompilerGenerated = true };
// (DelegateType)Delegate.Combine(tmp1, value)
BoundExpression delegateUpdate = BoundConversion.SynthesizedNonUserDefined(syntax,
operand: BoundCall.Synthesized(syntax,
receiverOpt: null,
method: updateMethod,
arguments: ImmutableArray.Create<BoundExpression>(boundTmps[1], boundParameter)),
kind: ConversionKind.ExplicitReference,
type: delegateType);
// tmp2 = (DelegateType)Delegate.Combine(tmp1, value);
BoundStatement tmp2Update = new BoundExpressionStatement(syntax,
expression: new BoundAssignmentOperator(syntax,
left: boundTmps[2],
right: delegateUpdate,
type: delegateType)
//.........这里部分代码省略.........
示例6: ConstructFieldLikeEventAccessorBody_WinRT
/// <summary>
/// Generate a thread-safe accessor for a WinRT field-like event.
///
/// Add:
/// return EventRegistrationTokenTable<Event>.GetOrCreateEventRegistrationTokenTable(ref _tokenTable).AddEventHandler(value);
///
/// Remove:
/// EventRegistrationTokenTable<Event>.GetOrCreateEventRegistrationTokenTable(ref _tokenTable).RemoveEventHandler(value);
/// </summary>
internal static BoundBlock ConstructFieldLikeEventAccessorBody_WinRT(SourceEventSymbol eventSymbol, bool isAddMethod, CSharpCompilation compilation, DiagnosticBag diagnostics)
{
CSharpSyntaxNode syntax = eventSymbol.CSharpSyntaxNode;
MethodSymbol accessor = isAddMethod ? eventSymbol.AddMethod : eventSymbol.RemoveMethod;
Debug.Assert((object)accessor != null);
FieldSymbol field = eventSymbol.AssociatedField;
Debug.Assert((object)field != null);
NamedTypeSymbol fieldType = (NamedTypeSymbol)field.Type;
Debug.Assert(fieldType.Name == "EventRegistrationTokenTable");
MethodSymbol getOrCreateMethod = (MethodSymbol)Binder.GetWellKnownTypeMember(
compilation,
WellKnownMember.System_Runtime_InteropServices_WindowsRuntime_EventRegistrationTokenTable_T__GetOrCreateEventRegistrationTokenTable,
diagnostics,
syntax: syntax);
if ((object)getOrCreateMethod == null)
{
Debug.Assert(diagnostics.HasAnyErrors());
return null;
}
getOrCreateMethod = getOrCreateMethod.AsMember(fieldType);
WellKnownMember processHandlerMember = isAddMethod
? WellKnownMember.System_Runtime_InteropServices_WindowsRuntime_EventRegistrationTokenTable_T__AddEventHandler
: WellKnownMember.System_Runtime_InteropServices_WindowsRuntime_EventRegistrationTokenTable_T__RemoveEventHandler;
MethodSymbol processHandlerMethod = (MethodSymbol)Binder.GetWellKnownTypeMember(
compilation,
processHandlerMember,
diagnostics,
syntax: syntax);
if ((object)processHandlerMethod == null)
{
Debug.Assert(diagnostics.HasAnyErrors());
return null;
}
processHandlerMethod = processHandlerMethod.AsMember(fieldType);
// _tokenTable
BoundFieldAccess fieldAccess = new BoundFieldAccess(
syntax,
field.IsStatic ? null : new BoundThisReference(syntax, accessor.ThisParameter.Type),
field,
constantValueOpt: null)
{ WasCompilerGenerated = true };
// EventRegistrationTokenTable<Event>.GetOrCreateEventRegistrationTokenTable(ref _tokenTable)
BoundCall getOrCreateCall = BoundCall.Synthesized(
syntax,
receiverOpt: null,
method: getOrCreateMethod,
arguments: fieldAccess);
// value
BoundParameter parameterAccess = new BoundParameter(
syntax,
accessor.Parameters.Single());
// EventRegistrationTokenTable<Event>.GetOrCreateEventRegistrationTokenTable(ref _tokenTable).AddHandler(value) // or RemoveHandler
BoundCall processHandlerCall = BoundCall.Synthesized(
syntax,
receiverOpt: getOrCreateCall,
method: processHandlerMethod,
arguments: parameterAccess);
if (isAddMethod)
{
// {
// return EventRegistrationTokenTable<Event>.GetOrCreateEventRegistrationTokenTable(ref _tokenTable).AddHandler(value);
// }
BoundStatement returnStatement = BoundReturnStatement.Synthesized(syntax, processHandlerCall);
return BoundBlock.SynthesizedNoLocals(syntax, returnStatement);
}
else
{
// {
// EventRegistrationTokenTable<Event>.GetOrCreateEventRegistrationTokenTable(ref _tokenTable).RemoveHandler(value);
// return;
// }
BoundStatement callStatement = new BoundExpressionStatement(syntax, processHandlerCall);
BoundStatement returnStatement = new BoundReturnStatement(syntax, expressionOpt: null);
return BoundBlock.SynthesizedNoLocals(syntax, callStatement, returnStatement);
}
}
示例7: ConstructAutoPropertyAccessorBody
/// <summary>
/// Construct a body for an auto-property accessor (updating or returning the backing field).
/// </summary>
internal static BoundBlock ConstructAutoPropertyAccessorBody(SourceMethodSymbol accessor)
{
Debug.Assert(accessor.MethodKind == MethodKind.PropertyGet || accessor.MethodKind == MethodKind.PropertySet);
var property = (SourcePropertySymbol)accessor.AssociatedSymbol;
CSharpSyntaxNode syntax = property.CSharpSyntaxNode;
BoundExpression thisReference = null;
if (!accessor.IsStatic)
{
var thisSymbol = accessor.ThisParameter;
thisReference = new BoundThisReference(syntax, thisSymbol.Type) { WasCompilerGenerated = true };
}
var field = property.BackingField;
var fieldAccess = new BoundFieldAccess(syntax, thisReference, field, ConstantValue.NotAvailable) { WasCompilerGenerated = true };
BoundStatement statement;
if (accessor.MethodKind == MethodKind.PropertyGet)
{
statement = new BoundReturnStatement(syntax, fieldAccess) { WasCompilerGenerated = true };
}
else
{
Debug.Assert(accessor.MethodKind == MethodKind.PropertySet);
var parameter = accessor.Parameters[0];
statement = new BoundExpressionStatement(
syntax,
new BoundAssignmentOperator(
syntax,
fieldAccess,
new BoundParameter(syntax, parameter) { WasCompilerGenerated = true },
property.Type)
{ WasCompilerGenerated = true })
{ WasCompilerGenerated = true };
}
statement = new BoundSequencePoint(accessor.SyntaxNode, statement) { WasCompilerGenerated = true };
return new BoundBlock(syntax, ImmutableArray<LocalSymbol>.Empty, ImmutableArray.Create<BoundStatement>(statement)) { WasCompilerGenerated = true };
}
示例8: VisitFieldAccess
public override BoundNode VisitFieldAccess(BoundFieldAccess node)
{
BoundExpression receiverOpt = (BoundExpression)this.Visit(node.ReceiverOpt);
TypeSymbol type = this.VisitType(node.Type);
if (receiverOpt == null || receiverOpt.Kind != BoundKind.SpillSequence)
{
return node.Update(receiverOpt, node.FieldSymbol, node.ConstantValueOpt, node.ResultKind, type);
}
var spill = (BoundSpillSequence)receiverOpt;
return RewriteSpillSequence(spill,
node.Update(
spill.Value,
node.FieldSymbol,
node.ConstantValueOpt,
node.ResultKind,
type));
}
示例9: VisitFieldAccess
private BoundExpression VisitFieldAccess(BoundFieldAccess node)
{
var receiver = node.FieldSymbol.IsStatic ? _bound.Null(ExpressionType) : Visit(node.ReceiverOpt);
return ExprFactory(
"Field",
receiver, _bound.FieldInfo(node.FieldSymbol));
}
示例10: VisitFieldAccess
public override BoundNode VisitFieldAccess(BoundFieldAccess node)
{
if (node.ReceiverOpt != null && node.ReceiverOpt.Kind == BoundKind.ThisReference)
{
var thisSymbol = topLevelMethod.ThisParameter;
CaptureVariable(thisSymbol, node.Syntax);
}
return base.VisitFieldAccess(node);
}
示例11: MakeEventAccess
private BoundExpression MakeEventAccess(
SyntaxNode syntax,
BoundExpression rewrittenReceiver,
EventSymbol eventSymbol,
ConstantValue constantValueOpt,
LookupResultKind resultKind,
TypeSymbol type)
{
Debug.Assert(eventSymbol.HasAssociatedField);
FieldSymbol fieldSymbol = eventSymbol.AssociatedField;
Debug.Assert((object)fieldSymbol != null);
if (!eventSymbol.IsWindowsRuntimeEvent)
{
return MakeFieldAccess(syntax, rewrittenReceiver, fieldSymbol, constantValueOpt, resultKind, type);
}
NamedTypeSymbol fieldType = (NamedTypeSymbol)fieldSymbol.Type;
Debug.Assert(fieldType.Name == "EventRegistrationTokenTable");
// _tokenTable
BoundFieldAccess fieldAccess = new BoundFieldAccess(
syntax,
fieldSymbol.IsStatic ? null : rewrittenReceiver,
fieldSymbol,
constantValueOpt: null)
{ WasCompilerGenerated = true };
BoundExpression getOrCreateCall;
MethodSymbol getOrCreateMethod;
if (TryGetWellKnownTypeMember(syntax, WellKnownMember.System_Runtime_InteropServices_WindowsRuntime_EventRegistrationTokenTable_T__GetOrCreateEventRegistrationTokenTable, out getOrCreateMethod))
{
getOrCreateMethod = getOrCreateMethod.AsMember(fieldType);
// EventRegistrationTokenTable<Event>.GetOrCreateEventRegistrationTokenTable(ref _tokenTable)
getOrCreateCall = BoundCall.Synthesized(
syntax,
receiverOpt: null,
method: getOrCreateMethod,
arg0: fieldAccess);
}
else
{
getOrCreateCall = new BoundBadExpression(syntax, LookupResultKind.NotInvocable, ImmutableArray<Symbol>.Empty, ImmutableArray.Create<BoundNode>(fieldAccess), ErrorTypeSymbol.UnknownResultType);
}
PropertySymbol invocationListProperty;
if (TryGetWellKnownTypeMember(syntax, WellKnownMember.System_Runtime_InteropServices_WindowsRuntime_EventRegistrationTokenTable_T__InvocationList, out invocationListProperty))
{
MethodSymbol invocationListAccessor = invocationListProperty.GetMethod;
if ((object)invocationListAccessor == null)
{
string accessorName = SourcePropertyAccessorSymbol.GetAccessorName(invocationListProperty.Name,
getNotSet: true,
isWinMdOutput: invocationListProperty.IsCompilationOutputWinMdObj());
_diagnostics.Add(new CSDiagnosticInfo(ErrorCode.ERR_MissingPredefinedMember, invocationListProperty.ContainingType, accessorName), syntax.Location);
}
else
{
invocationListAccessor = invocationListAccessor.AsMember(fieldType);
return _factory.Call(getOrCreateCall, invocationListAccessor);
}
}
return new BoundBadExpression(syntax, LookupResultKind.NotInvocable, ImmutableArray<Symbol>.Empty, ImmutableArray.Create<BoundNode>(getOrCreateCall), ErrorTypeSymbol.UnknownResultType);
}
示例12: IsInstanceFieldAccessWithNonThisReceiver
private static bool IsInstanceFieldAccessWithNonThisReceiver(BoundFieldAccess fieldAccess)
{
BoundExpression receiver = fieldAccess.ReceiverOpt;
if (receiver == null || fieldAccess.FieldSymbol.IsStatic)
{
return false;
}
while (receiver.Kind == BoundKind.Conversion)
{
BoundConversion conversion = (BoundConversion)receiver;
if (conversion.ExplicitCastInCode) break;
receiver = conversion.Operand;
}
return receiver.Kind != BoundKind.ThisReference && receiver.Kind != BoundKind.BaseReference;
}
示例13: TransformReferenceTypeFieldAccess
private BoundFieldAccess TransformReferenceTypeFieldAccess(BoundFieldAccess fieldAccess, BoundExpression receiver, ArrayBuilder<BoundExpression> stores, ArrayBuilder<LocalSymbol> temps)
{
Debug.Assert(receiver.Type.IsReferenceType);
Debug.Assert(receiver.Kind != BoundKind.TypeExpression);
BoundExpression rewrittenReceiver = VisitExpression(receiver);
if (rewrittenReceiver.Type.IsTypeParameter())
{
var memberContainingType = fieldAccess.FieldSymbol.ContainingType;
// From the verifier prospective type parameters do not contain fields or methods.
// the instance must be "boxed" to access the field
// It makes sense to box receiver before storing into a temp - no need to box twice.
rewrittenReceiver = BoxReceiver(rewrittenReceiver, memberContainingType);
}
BoundAssignmentOperator assignmentToTemp;
var receiverTemp = _factory.StoreToTemp(rewrittenReceiver, out assignmentToTemp);
stores.Add(assignmentToTemp);
temps.Add(receiverTemp.LocalSymbol);
return new BoundFieldAccess(fieldAccess.Syntax, receiverTemp, fieldAccess.FieldSymbol, null);
}
示例14: VisitFieldAccess
public override BoundNode VisitFieldAccess(BoundFieldAccess node)
{
BoundExpression rewrittenReceiver = VisitExpression(node.ReceiverOpt);
return MakeFieldAccess(node.Syntax, rewrittenReceiver, node.FieldSymbol, node.ConstantValue, node.ResultKind, node.Type, node);
}
示例15: GetDefaultParameterSpecial
private BoundExpression GetDefaultParameterSpecial(SyntaxNode syntax, ParameterSymbol parameter)
{
// We have a call to a method M([Optional] object x) which omits the argument. The value we generate
// for the argument depends on the presence or absence of other attributes. The rules are:
//
// * If the parameter is marked as [MarshalAs(Interface)], [MarshalAs(IUnknown)] or [MarshalAs(IDispatch)]
// then the argument is null.
// * Otherwise, if the parameter is marked as [IUnknownConstant] then the argument is
// new UnknownWrapper(null)
// * Otherwise, if the parameter is marked as [IDispatchConstant] then the argument is
// new DispatchWrapper(null)
// * Otherwise, the argument is Type.Missing.
BoundExpression defaultValue;
if (parameter.IsMarshalAsObject)
{
// default(object)
defaultValue = new BoundDefaultOperator(syntax, parameter.Type);
}
else if (parameter.IsIUnknownConstant)
{
// new UnknownWrapper(default(object))
var methodSymbol = (MethodSymbol)_compilation.GetWellKnownTypeMember(WellKnownMember.System_Runtime_InteropServices_UnknownWrapper__ctor);
var argument = new BoundDefaultOperator(syntax, parameter.Type);
defaultValue = new BoundObjectCreationExpression(syntax, methodSymbol, argument);
}
else if (parameter.IsIDispatchConstant)
{
// new DispatchWrapper(default(object))
var methodSymbol = (MethodSymbol)_compilation.GetWellKnownTypeMember(WellKnownMember.System_Runtime_InteropServices_DispatchWrapper__ctor);
var argument = new BoundDefaultOperator(syntax, parameter.Type);
defaultValue = new BoundObjectCreationExpression(syntax, methodSymbol, argument);
}
else
{
// Type.Missing
var fieldSymbol = (FieldSymbol)_compilation.GetWellKnownTypeMember(WellKnownMember.System_Type__Missing);
defaultValue = new BoundFieldAccess(syntax, null, fieldSymbol, ConstantValue.NotAvailable);
}
defaultValue = MakeConversionNode(defaultValue, parameter.Type, @checked: false);
return defaultValue;
}