Skip to content

Commit eb12654

Browse files
committed
Filling types with [Embedded].
1 parent 06d4e01 commit eb12654

13 files changed

+623
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project>
2+
3+
<ItemGroup>
4+
<Compile Include="$(MSBuildThisFileDirectory)assets\embedded-system-types\*.cs" Link="(System Types)\%(Filename)" />
5+
</ItemGroup>
6+
7+
<!-- MSBuild should discover the .editorconfig itself from Microsoft.Managed.Core.targets. If it does not, the IncludeSystemTypesEditorConfig property can be set. -->
8+
<ItemGroup Condition="'$(IncludeSystemTypesEditorConfig)'=='True'">
9+
<EditorConfigFiles Include="$(MSBuildThisFileDirectory)assets\embedded-system-types\.editorconfig" />
10+
</ItemGroup>
11+
12+
</Project>

src/PostSharp.Engineering.Sdk/PostSharp.Engineering.Sdk.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<None Include="*.targets;*.props;*.snk" Pack="True" PackagePath="sdk" />
1414
<None Include="assets/*" Pack="True" PackagePath="sdk/assets" />
1515
<None Include="assets/system-types/*" Pack="True" PackagePath="sdk/assets/system-types" />
16+
<None Include="assets/embedded-system-types/*" Pack="True" PackagePath="sdk/assets/embedded-system-types" />
1617
<Compile Remove="assets/**/*.cs" />
1718
</ItemGroup>
1819

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root = true
2+
3+
[*.cs]
4+
dotnet_analyzer_diagnostic.severity = none
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2020-2025 SharpCrafters s.r.o. and contributors.
2+
// SharpCrafters s.r.o. licenses this file to you under either the MIT license or a proprietary license, depending on the repository from which it was obtained.
3+
// Refer to LICENSE.md in the repository root for complete details.
4+
5+
#if !NETCOREAPP
6+
using Microsoft.CodeAnalysis;
7+
8+
// ReSharper disable once CheckNamespace
9+
namespace System.Runtime.CompilerServices;
10+
11+
[AttributeUsage( AttributeTargets.Parameter )]
12+
[Embedded]
13+
internal sealed class CallerArgumentExpressionAttribute : Attribute
14+
{
15+
public CallerArgumentExpressionAttribute( string parameterName )
16+
{
17+
this.ParameterName = parameterName;
18+
}
19+
20+
public string ParameterName { get; }
21+
}
22+
23+
#endif
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#if !NET7_0_OR_GREATER
5+
6+
using Microsoft.CodeAnalysis;
7+
8+
namespace System.Runtime.CompilerServices
9+
{
10+
/// <summary>
11+
/// Indicates that compiler support for a particular feature is required for the location where this attribute is applied.
12+
/// </summary>
13+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
14+
[Embedded]
15+
internal sealed class CompilerFeatureRequiredAttribute : Attribute
16+
{
17+
public CompilerFeatureRequiredAttribute(string featureName)
18+
{
19+
FeatureName = featureName;
20+
}
21+
22+
/// <summary>
23+
/// The name of the compiler feature.
24+
/// </summary>
25+
public string FeatureName { get; }
26+
27+
/// <summary>
28+
/// If true, the compiler can choose to allow access to the location where this attribute is applied if it does not understand <see cref="FeatureName"/>.
29+
/// </summary>
30+
public bool IsOptional { get; init; }
31+
32+
/// <summary>
33+
/// The <see cref="FeatureName"/> used for the ref structs C# feature.
34+
/// </summary>
35+
public const string RefStructs = nameof(RefStructs);
36+
37+
/// <summary>
38+
/// The <see cref="FeatureName"/> used for the required members C# feature.
39+
/// </summary>
40+
public const string RequiredMembers = nameof(RequiredMembers);
41+
}
42+
}
43+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace Microsoft.CodeAnalysis
4+
{
5+
[AttributeUsage( AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate )]
6+
internal sealed partial class EmbeddedAttribute : Attribute
7+
{
8+
}
9+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#if !NETCOREAPP && !NETSTANDARD2_1
6+
7+
using System.Runtime.CompilerServices;
8+
using Microsoft.CodeAnalysis;
9+
10+
namespace System
11+
{
12+
/// <summary>Represent a type can be used to index a collection either from the start or the end.</summary>
13+
/// <remarks>
14+
/// Index is used by the C# compiler to support the new index syntax
15+
/// <code>
16+
/// int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ;
17+
/// int lastElement = someArray[^1]; // lastElement = 5
18+
/// </code>
19+
/// </remarks>
20+
[Embedded]
21+
internal readonly struct Index : IEquatable<Index>
22+
{
23+
private readonly int _value;
24+
25+
/// <summary>Construct an Index using a value and indicating if the index is from the start or from the end.</summary>
26+
/// <param name="value">The index value. it has to be zero or positive number.</param>
27+
/// <param name="fromEnd">Indicating if the index is from the start or from the end.</param>
28+
/// <remarks>
29+
/// If the Index constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element.
30+
/// </remarks>
31+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32+
public Index(int value, bool fromEnd = false)
33+
{
34+
if (value < 0)
35+
{
36+
throw new ArgumentOutOfRangeException(nameof(value), value, "Non-negative number required.");
37+
}
38+
39+
if (fromEnd)
40+
_value = ~value;
41+
else
42+
_value = value;
43+
}
44+
45+
// The following private constructors mainly created for perf reason to avoid the checks
46+
private Index(int value)
47+
{
48+
_value = value;
49+
}
50+
51+
/// <summary>Create an Index pointing at first element.</summary>
52+
public static Index Start => new Index(0);
53+
54+
/// <summary>Create an Index pointing at beyond last element.</summary>
55+
public static Index End => new Index(~0);
56+
57+
/// <summary>Create an Index from the start at the position indicated by the value.</summary>
58+
/// <param name="value">The index value from the start.</param>
59+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60+
public static Index FromStart(int value)
61+
{
62+
if (value < 0)
63+
{
64+
throw new ArgumentOutOfRangeException(nameof(value), value, "Non-negative number required.");
65+
}
66+
67+
return new Index(value);
68+
}
69+
70+
/// <summary>Create an Index from the end at the position indicated by the value.</summary>
71+
/// <param name="value">The index value from the end.</param>
72+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
73+
public static Index FromEnd(int value)
74+
{
75+
if (value < 0)
76+
{
77+
throw new ArgumentOutOfRangeException(nameof(value), value, "Non-negative number required.");
78+
}
79+
80+
return new Index(~value);
81+
}
82+
83+
/// <summary>Returns the index value.</summary>
84+
public int Value
85+
{
86+
get
87+
{
88+
if (_value < 0)
89+
return ~_value;
90+
else
91+
return _value;
92+
}
93+
}
94+
95+
/// <summary>Indicates whether the index is from the start or the end.</summary>
96+
public bool IsFromEnd => _value < 0;
97+
98+
/// <summary>Calculate the offset from the start using the giving collection length.</summary>
99+
/// <param name="length">The length of the collection that the Index will be used with. length has to be a positive value</param>
100+
/// <remarks>
101+
/// For performance reason, we don't validate the input length parameter and the returned offset value against negative values.
102+
/// we don't validate either the returned offset is greater than the input length.
103+
/// It is expected Index will be used with collections which always have non negative length/count. If the returned offset is negative and
104+
/// then used to index a collection will get out of range exception which will be same affect as the validation.
105+
/// </remarks>
106+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
107+
public int GetOffset(int length)
108+
{
109+
int offset = _value;
110+
if (IsFromEnd)
111+
{
112+
// offset = length - (~value)
113+
// offset = length + (~(~value) + 1)
114+
// offset = length + value + 1
115+
116+
offset += length + 1;
117+
}
118+
return offset;
119+
}
120+
121+
/// <summary>Indicates whether the current Index object is equal to another object of the same type.</summary>
122+
/// <param name="value">An object to compare with this object</param>
123+
public override bool Equals(object? value) => value is Index && _value == ((Index)value)._value;
124+
125+
/// <summary>Indicates whether the current Index object is equal to another Index object.</summary>
126+
/// <param name="other">An object to compare with this object</param>
127+
public bool Equals(Index other) => _value == other._value;
128+
129+
/// <summary>Returns the hash code for this instance.</summary>
130+
public override int GetHashCode() => _value;
131+
132+
/// <summary>Converts integer number to an Index.</summary>
133+
public static implicit operator Index(int value) => FromStart(value);
134+
135+
/// <summary>Converts the value of the current Index object to its equivalent string representation.</summary>
136+
public override string ToString()
137+
{
138+
if (IsFromEnd)
139+
return $"^{((uint)Value).ToString()}";
140+
141+
return ((uint)Value).ToString();
142+
}
143+
}
144+
}
145+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2020-2025 SharpCrafters s.r.o. and contributors.
2+
// SharpCrafters s.r.o. licenses this file to you under either the MIT license or a proprietary license, depending on the repository from which it was obtained.
3+
// Refer to LICENSE.md in the repository root for complete details.
4+
5+
#if !NET6_0_OR_GREATER
6+
using Microsoft.CodeAnalysis;
7+
8+
// ReSharper disable once CheckNamespace
9+
namespace System.Runtime.CompilerServices;
10+
11+
[AttributeUsage( AttributeTargets.Parameter )]
12+
[Embedded]
13+
public sealed class InterpolatedStringHandlerArgumentAttribute : Attribute
14+
{
15+
public InterpolatedStringHandlerArgumentAttribute( string argument )
16+
{
17+
this.Arguments = [argument];
18+
}
19+
20+
public InterpolatedStringHandlerArgumentAttribute( params string[] arguments )
21+
{
22+
this.Arguments = arguments;
23+
}
24+
25+
public string[] Arguments { get; }
26+
}
27+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2020-2025 SharpCrafters s.r.o. and contributors.
2+
// SharpCrafters s.r.o. licenses this file to you under either the MIT license or a proprietary license, depending on the repository from which it was obtained.
3+
// Refer to LICENSE.md in the repository root for complete details.
4+
5+
#if !NET6_0_OR_GREATER // ReSharper disable once CheckNamespace
6+
7+
using Microsoft.CodeAnalysis;
8+
namespace System.Runtime.CompilerServices;
9+
10+
[AttributeUsage( AttributeTargets.Class | AttributeTargets.Struct, Inherited = false )]
11+
[Embedded]
12+
internal sealed class InterpolatedStringHandlerAttribute : Attribute;
13+
#endif
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) SharpCrafters s.r.o. All rights reserved.
2+
// This project is not open source. Please see the LICENSE.md file in the repository root for details.
3+
4+
#if !NET5_0_OR_GREATER
5+
using System.ComponentModel;
6+
using System.Reflection;
7+
using Microsoft.CodeAnalysis;
8+
9+
// ReSharper disable All
10+
11+
namespace System.Runtime.CompilerServices
12+
{
13+
/// <summary>
14+
/// Reserved to be used by the compiler for tracking metadata.
15+
/// This class should not be used by developers in source code.
16+
/// </summary>
17+
[EditorBrowsable( EditorBrowsableState.Never )]
18+
[Obfuscation( Exclude = true )]
19+
[Embedded]
20+
internal static class IsExternalInit { }
21+
}
22+
#else
23+
using System.Runtime.CompilerServices;
24+
25+
[assembly: TypeForwardedTo( typeof(IsExternalInit) )]
26+
#endif

0 commit comments

Comments
 (0)