You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* SP004: implement initialize list translation to ctor
- We synthesize a member-wise constructor for each struct follow
the rules described in SP004.
- Add logic to translate the initialize list to constructor invoke
- Add cuda-host decoration for the synthesized constructor
- Remove the default constructor when we have a valid member init constructor
- Disable -zero-initialize option, will re-implement it in followup (#6109).
- Fix the overload lookup issue
When creating invoke expression for ctor, we need to call
ResolveInvoke() to find us the best candidates, however
the existing lookup logic could find us the base constructor
for child struct, we should eliminate this case by providing
the LookupOptions::IgnoreInheritance to lookup, this requires
us to create a subcontext on SemanticsVisitor to indicate that
we only want to use this option on looking the constructor.
- Do not implicit initialize a struct that doesn't have explicit default
constructor.
Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Copy file name to clipboardexpand all lines: docs/proposals/004-initialization.md
+36-2
Original file line number
Diff line number
Diff line change
@@ -129,6 +129,39 @@ MyType x = MyType(y); // equivalent to `x = y`.
129
129
130
130
The compiler will attempt to resolve all type casts using type coercion rules, if that failed, will fall back to resolve it as a constructor call.
131
131
132
+
### Inheritance Initialization
133
+
For derived structs, slang will synthesized the constructor by bringing the parameters from the base struct's constructor if the base struct also has a synthesized constructor. For example:
134
+
```csharp
135
+
structBase
136
+
{
137
+
intx;
138
+
// compiler synthesizes:
139
+
// __init(int x) { ... }
140
+
}
141
+
structDerived : Base
142
+
{
143
+
inty;
144
+
// compiler synthesizes:
145
+
// __init(int x, int y) { ... }
146
+
}
147
+
```
148
+
149
+
However, if the base struct has explicit ctors, the compiler will not synthesize a constructor for the derived struct.
150
+
For example, given
151
+
```csharp
152
+
structBase { intx; __init(intx) { this.x=x; } }
153
+
structDerived : Base { inty;}
154
+
```
155
+
The compiler will not synthesize a constructor for `Derived`, and the following code will fail to compile:
156
+
```csharp
157
+
158
+
Derivedd= {1}; // error, no matching ctor.
159
+
Derivedd= {1, 2}; // error, no matching ctor.
160
+
Derivedd=Derived(1); // error, no matching ctor.
161
+
Derivedd=Derived(1, 2); // error, no matching ctor.
162
+
```
163
+
164
+
132
165
### Initialization List
133
166
134
167
Slang allows initialization of a variable by assigning it with an initialization list.
@@ -167,7 +200,8 @@ If the above code passes type check, then it will be used as the way to initiali
167
200
168
201
If the above code does not pass type check, and if there is only one constructor for`MyType` that is synthesized as described in the previous section (and therefore marked as `[Synthesized]`, Slang continues to check if `S` meets the standard of a "legacy C-style struct` type.
169
202
A type is a "legacy C-Style struct" if all of the following conditions are met:
170
-
- It is a user-defined struct type or a basic scalar, vector or matrix type, e.g. `int`, `float4x4`.
203
+
- It is a user-defined struct type or an enum, a basic scalar, vector or matrix type, e.g. `int`, `float4x4`.
204
+
- It does not inherit from any other types.
171
205
- It does not contain any explicit constructors defined by the user.
172
206
- All its members have the same visibility as the type itself.
173
207
- All its members are legacy C-Style structs or arrays of legacy C-style structs.
@@ -405,4 +439,4 @@ Alternatives Considered
405
439
406
440
One important decision point is whether or not Slang should allow variables to be left in uninitialized state after its declaration as it is allowed in C++. In contrast, C# forces everything to be default initialized at its declaration site, which come at the cost of incurring the burden to developers to come up with a way to define the default value for each type.
407
441
Our opinion is we want to allow things as uninitialized, and to have the compiler validation checks to inform
408
-
the developer something is wrong if they try to use a variable in uninitialized state. We believe it is desirable to tell the developer what's wrong instead of using a heavyweight mechanism to ensure everything is initialized at declaration sites, which can have non-trivial performance consequences for GPU programs, especially when the variable is declared in groupshared memory.
442
+
the developer something is wrong if they try to use a variable in uninitialized state. We believe it is desirable to tell the developer what's wrong instead of using a heavyweight mechanism to ensure everything is initialized at declaration sites, which can have non-trivial performance consequences for GPU programs, especially when the variable is declared in groupshared memory.
0 commit comments