Beispiel #1
0
            internal void AddLocalConstant(LocalConstantDefinition constant)
            {
                if (LocalConstants == null)
                {
                    LocalConstants = ImmutableArray.CreateBuilder <LocalConstantDefinition>(1);
                }

                LocalConstants.Add(constant);
            }
            internal void AddLocalConstant(LocalConstantDefinition constant)
            {
                if (_localConstants == null)
                {
                    _localConstants = ImmutableArray.CreateBuilder <LocalConstantDefinition>(1);
                }

                Debug.Assert(constant.Name != null);

                _localConstants.Add(constant);
            }
            internal void AddLocalConstant(LocalConstantDefinition constant)
            {
                LocalScopeInfo scope = (LocalScopeInfo)CurrentScope;

                scope.AddLocalConstant(constant);
            }
Beispiel #4
0
        private LocalDefinition DefineLocal(LocalSymbol local, SyntaxNode syntaxNode)
        {
            var dynamicTransformFlags = !local.IsCompilerGenerated && local.Type.ContainsDynamic() ?
                CSharpCompilation.DynamicTransformsEncoder.Encode(local.Type, _module.Compilation.GetSpecialType(SpecialType.System_Boolean), 0, RefKind.None) :
                ImmutableArray<TypedConstant>.Empty;
            var tupleElementNames = !local.IsCompilerGenerated && local.Type.ContainsTupleNames() ?
                CSharpCompilation.TupleNamesEncoder.Encode(local.Type, _module.Compilation.GetSpecialType(SpecialType.System_String)) :
                ImmutableArray<TypedConstant>.Empty;

            if (local.IsConst)
            {
                Debug.Assert(local.HasConstantValue);
                MetadataConstant compileTimeValue = _module.CreateConstant(local.Type, local.ConstantValue, syntaxNode, _diagnostics);
                LocalConstantDefinition localConstantDef = new LocalConstantDefinition(
                    local.Name,
                    local.Locations.FirstOrDefault() ?? Location.None,
                    compileTimeValue,
                    dynamicTransformFlags: dynamicTransformFlags,
                    tupleElementNames: tupleElementNames);
                _builder.AddLocalConstantToScope(localConstantDef);
                return null;
            }

            if (IsStackLocal(local))
            {
                return null;
            }

            LocalSlotConstraints constraints;
            Cci.ITypeReference translatedType;

            if (local.DeclarationKind == LocalDeclarationKind.FixedVariable && local.IsPinned) // Excludes pointer local and string local in fixed string case.
            {
                Debug.Assert(local.RefKind == RefKind.None);
                Debug.Assert(local.Type.IsPointerType());

                constraints = LocalSlotConstraints.ByRef | LocalSlotConstraints.Pinned;
                PointerTypeSymbol pointerType = (PointerTypeSymbol)local.Type;
                TypeSymbol pointedAtType = pointerType.PointedAtType;

                // We can't declare a reference to void, so if the pointed-at type is void, use native int
                // (represented here by IntPtr) instead.
                translatedType = pointedAtType.SpecialType == SpecialType.System_Void
                    ? _module.GetSpecialType(SpecialType.System_IntPtr, syntaxNode, _diagnostics)
                    : _module.Translate(pointedAtType, syntaxNode, _diagnostics);
            }
            else
            {
                constraints = (local.IsPinned ? LocalSlotConstraints.Pinned : LocalSlotConstraints.None) |
                    (local.RefKind != RefKind.None ? LocalSlotConstraints.ByRef : LocalSlotConstraints.None);
                translatedType = _module.Translate(local.Type, syntaxNode, _diagnostics);
            }

            // Even though we don't need the token immediately, we will need it later when signature for the local is emitted.
            // Also, requesting the token has side-effect of registering types used, which is critical for embedded types (NoPia, VBCore, etc).
            _module.GetFakeSymbolTokenForIL(translatedType, syntaxNode, _diagnostics);

            LocalDebugId localId;
            var name = GetLocalDebugName(local, out localId);

            var localDef = _builder.LocalSlotManager.DeclareLocal(
                type: translatedType,
                symbol: local,
                name: name,
                kind: local.SynthesizedKind,
                id: localId,
                pdbAttributes: local.SynthesizedKind.PdbAttributes(),
                constraints: constraints,
                dynamicTransformFlags: dynamicTransformFlags,
                tupleElementNames: tupleElementNames,
                isSlotReusable: local.SynthesizedKind.IsSlotReusable(_ilEmitStyle != ILEmitStyle.Release));

            // If named, add it to the local debug scope.
            if (localDef.Name != null)
            {
                _builder.AddLocalToScope(localDef);
            }

            return localDef;
        }
Beispiel #5
0
        private LocalDefinition DefineLocal(LocalSymbol local, CSharpSyntaxNode syntaxNode)
        {
            var transformFlags = default(ImmutableArray<TypedConstant>);
            bool hasDynamic = local.Type.ContainsDynamic();
            var isDynamicSourceLocal = hasDynamic && !local.IsCompilerGenerated;
            if (isDynamicSourceLocal)
            {
                NamedTypeSymbol booleanType = this.module.Compilation.GetSpecialType(SpecialType.System_Boolean);
                transformFlags = CSharpCompilation.DynamicTransformsEncoder.Encode(local.Type, booleanType, 0, RefKind.None);
            }

            if (local.IsConst)
            {
                Debug.Assert(local.HasConstantValue);
                MetadataConstant compileTimeValue = this.module.CreateConstant(local.Type, local.ConstantValue, syntaxNode, diagnostics);
                LocalConstantDefinition localConstantDef = new LocalConstantDefinition(local.Name, local.Locations.FirstOrDefault() ?? Location.None, compileTimeValue, isDynamicSourceLocal, transformFlags);
                builder.AddLocalConstantToScope(localConstantDef);
                return null;
            }

            if (IsStackLocal(local))
            {
                return null;
            }

            var name = GetLocalDebugName(local);
                LocalSlotConstraints constraints;
            Cci.ITypeReference translatedType;

            if (local.DeclarationKind == LocalDeclarationKind.FixedVariable && local.IsPinned) // Excludes pointer local and string local in fixed string case.
                {
                    Debug.Assert(local.RefKind == RefKind.None);
                    Debug.Assert(local.Type.IsPointerType());

                    constraints = LocalSlotConstraints.ByRef | LocalSlotConstraints.Pinned;
                    PointerTypeSymbol pointerType = (PointerTypeSymbol)local.Type;
                    TypeSymbol pointedAtType = pointerType.PointedAtType;

                    // We can't declare a reference to void, so if the pointed-at type is void, use native int
                    // (represented here by IntPtr) instead.
                    translatedType = pointedAtType.SpecialType == SpecialType.System_Void
                        ? this.module.GetSpecialType(SpecialType.System_IntPtr, syntaxNode, diagnostics)
                        : this.module.Translate(pointedAtType, syntaxNode, diagnostics);
                }
                else
                {
                    constraints = (local.IsPinned ? LocalSlotConstraints.Pinned : LocalSlotConstraints.None) |
                        (local.RefKind != RefKind.None ? LocalSlotConstraints.ByRef : LocalSlotConstraints.None);
                    translatedType = this.module.Translate(local.Type, syntaxNode, diagnostics);
                }

                // Even though we don't need the token immediately, we will need it later when signature for the local is emitted.
                // Also, requesting the token has side-effect of registering types used, which is critical for embedded types (NoPia, VBCore, etc).
                this.module.GetFakeSymbolTokenForIL(translatedType, syntaxNode, diagnostics);

                var localDef = builder.LocalSlotManager.DeclareLocal(
                        type: translatedType,
                        identity: local,
                        name: name,
                    isCompilerGenerated: local.SynthesizedLocalKind != SynthesizedLocalKind.None,
                        constraints: constraints,
                        isDynamic: isDynamicSourceLocal,
                        dynamicTransformFlags: transformFlags);

            // If named, add it to the scope. It will be emitted to the PDB.
                if (name != null)
                {
                    builder.AddLocalToScope(localDef);
                }

                return localDef;
            }
Beispiel #6
0
 /// <summary>
 /// Puts local constant into current scope.
 /// </summary>
 internal void AddLocalConstantToScope(LocalConstantDefinition localConstant)
 {
     HasDynamicLocal |= !localConstant.DynamicTransformFlags.IsEmpty;
     _scopeManager.AddLocalConstant(localConstant);
 }
Beispiel #7
0
 /// <summary>
 /// Puts local constant into current scope.
 /// </summary>
 internal void AddLocalConstantToScope(LocalConstantDefinition localConstant)
 {
     HasDynamicLocal |= localConstant.IsDynamic;
     _scopeManager.AddLocalConstant(localConstant);
 }
Beispiel #8
0
 /// <summary>
 /// Puts local constant into current scope.
 /// </summary>
 internal void AddLocalConstantToScope(LocalConstantDefinition localConstant)
 {
     HasDynamicLocal |= localConstant.IsDynamic;
     _scopeManager.AddLocalConstant(localConstant);
 }
Beispiel #9
0
 /// <summary>
 /// Puts local constant into current scope.
 /// </summary>
 internal void AddLocalConstantToScope(LocalConstantDefinition localConstant)
 {
     HasDynamicLocal |= !localConstant.DynamicTransformFlags.IsEmpty;
     _scopeManager.AddLocalConstant(localConstant);
 }