Skip to content

Commit 571c9b3

Browse files
committed
Split 'type' keyword into 'static' and 'class'
rdar://15911697 Swift SVN r13908
1 parent efe54a1 commit 571c9b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+765
-284
lines changed

docs/Import.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fails, we fall back to qualified lookup into the module::
146146
import Foo // exports bas
147147

148148
class Foo {
149-
type func bar()
149+
class func bar()
150150
}
151151

152152
Foo.bar() // bar method from Foo class

docs/LangRef.html

+18-10
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ <h3 id="decl-var">var Declarations</h3>
786786
<!-- ===================================================================== -->
787787

788788
<pre class="grammar">
789-
decl-var ::= <a href="#attribute-list">attribute-list</a> 'type'? var' <a href="#pattern">pattern</a> initializer? (',' pattern initializer?)*
789+
decl-var ::= <a href="#attribute-list">attribute-list</a> ('static' | 'class')? var' <a href="#pattern">pattern</a> initializer? (',' pattern initializer?)*
790790

791791
decl-var ::= <a href="#attribute-list">attribute-list</a> 'var' <a href="#identifier">identifier</a> ':' <a href="#type">type-annotation</a> <a href="#brace-item-list">brace-item-list</a>
792792

@@ -869,13 +869,17 @@ <h3 id="decl-var">var Declarations</h3>
869869
</pre>
870870

871871
<p>Note that both 'get' and 'set' are context-sensitive keywords.</p>
872-
872+
873+
<p>'static' keyword is allowed inside structs and enums, and extensions of those.</p>
874+
875+
<p>'class' keyword is allowed inside classes, class extensions, and protocols.</p>
876+
873877
<!-- ===================================================================== -->
874878
<h3 id="decl-func">func Declarations</h3>
875879
<!-- ===================================================================== -->
876880

877881
<pre class="grammar">
878-
decl-func ::= <a href="#attribute-list">attribute-list</a> 'type'? 'func' <a href="#any-identifier">any-identifier</a> <a href="#generic-params">generic-params</a>? <a href="#func-signature">func-signature</a> <a href="#brace-item-list">brace-item-list</a>?
882+
decl-func ::= <a href="#attribute-list">attribute-list</a> ('static' | 'class')? 'func' <a href="#any-identifier">any-identifier</a> <a href="#generic-params">generic-params</a>? <a href="#func-signature">func-signature</a> <a href="#brace-item-list">brace-item-list</a>?
879883
</pre>
880884

881885
<p>'func' is a declaration for a function. The argument list and
@@ -892,13 +896,17 @@ <h3 id="decl-func">func Declarations</h3>
892896
in other places that are semantically equivalent to an extension) implicitly
893897
get a 'self' argument with these rules ... [todo]</p>
894898

895-
<p>'type' functions are only allowed in an <a
899+
<p>'static' and 'class' functions are only allowed in an <a
896900
href="#decl-extension">extension</a> of some type (or in other places
897901
that are semantically equivalent to an extension). They indicate that
898902
the function is actually defined on the <a href="#metatype">metatype</a>
899903
for the type, not on the
900904
type itself. Thus its implicit 'self' argument is actually of
901905
metatype type.</p>
906+
907+
<p>'static' keyword is allowed inside structs and enums, and extensions of those.</p>
908+
909+
<p>'class' keyword is allowed inside classes, class extensions, and protocols.</p>
902910

903911
<p>TODO: Func should be an immutable name binding, it should implicitly add
904912
an attribute immutable when it exists.</p>
@@ -987,14 +995,14 @@ <h4 id="func-signature">Function signatures</h4>
987995
struct bankaccount {
988996
amount : Int
989997

990-
type func bankaccount() -> bankaccount {
998+
static func bankaccount() -> bankaccount {
991999
// Custom 'constructor' logic goes here.
9921000
}
9931001
func deposit(arg : Int) {
9941002
amount = amount + arg
9951003
}
9961004

997-
type func someMetatypeMethod() {}
1005+
static func someMetatypeMethod() {}
9981006
}
9991007

10001008
<i>// Dot syntax on metatype.</i>
@@ -1692,19 +1700,19 @@ <h3>Metatypes</h3>
16921700
<p id="metatype">Each type has a corresponding <i>metatype</i>, with the same
16931701
name as the type, that is injected into the standard name lookup scope when
16941702
a type is <a href="#decl">declared</a>. This allows access to '<a
1695-
href="#decl-func">type functions</a>' through dot syntax. For example:</p>
1703+
href="#decl-func">static functions</a>' through dot syntax. For example:</p>
16961704

16971705
<pre class="example">
16981706
// Declares a type 'foo' as well as its metatype.
16991707
struct foo {
1700-
type func bar() {}
1708+
static func bar() {}
17011709
}
17021710

17031711
// Declares x to be of type foo. A reference to a name in type context
17041712
// refers to the type itself.
17051713
var x : foo
17061714

1707-
// Accesses a type function on the foo metatype. In a value context, the
1715+
// Accesses a static function on the foo metatype. In a value context, the
17081716
// name of its type refers to its metatype.
17091717
foo.bar()
17101718
</pre>
@@ -2824,7 +2832,7 @@ <h3 id="expr-identifier">Identifiers</h3>
28242832
<i>// A generic struct.</i>
28252833
struct Dict&lt;K,V&gt; {
28262834
init() {}
2827-
type func fromKeysAndValues(keys:K[], values:T[]) -&gt; Dict&lt;K,V&gt; {}
2835+
static func fromKeysAndValues(keys:K[], values:T[]) -&gt; Dict&lt;K,V&gt; {}
28282836
}
28292837

28302838
<i>// Construct an instance of the generic struct.</i>

docs/Literals.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ library's Policy.swift::
4141
// NOTE: the compiler has builtin knowledge of this protocol
4242
protocol StringLiteralConvertible {
4343
typealias StringLiteralType : BuiltinStringLiteralConvertible
44-
type func convertFromStringLiteral(value : StringLiteralType) -> Self
44+
class func convertFromStringLiteral(value : StringLiteralType) -> Self
4545
}
4646

4747
Curiously, the protocol is not defined in terms of primitive types, but in
@@ -62,9 +62,9 @@ Policy.swift contains a second protocol::
6262

6363
// NOTE: the compiler has builtin knowledge of this protocol
6464
protocol BuiltinStringLiteralConvertible {
65-
type func _convertFromBuiltinStringLiteral(value : Builtin.RawPointer,
66-
byteSize : Builtin.Int64,
67-
isASCII: Builtin.Int1) -> Self
65+
class func _convertFromBuiltinStringLiteral(value : Builtin.RawPointer,
66+
byteSize : Builtin.Int64,
67+
isASCII: Builtin.Int1) -> Self
6868
}
6969

7070
The use of builtin types makes it clear that this is *only* for use in the

docs/classes.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Syntax overview
1818
func f(x : Int) -> Int {
1919
return MyVar + x
2020
}
21-
type func getAMyclass() -> MyClass {
21+
class func getAMyclass() -> MyClass {
2222
return new MyClass
2323
}
2424
constructor() {

docs/proposals/Enums.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ models the traditional relationship between a C enum and its raw type::
262262
/// conforming type.
263263
/// Returns None if the raw value has no corresponding conforming type
264264
/// value.
265-
type func fromRaw(_:RawType) -> Self?
265+
class func fromRaw(_:RawType) -> Self?
266266
}
267267

268268
Any type may manually conform to the RawRepresentable protocol following the above
@@ -330,7 +330,7 @@ methods. The NSChangeDictionaryKey definition behaves as if defined::
330330
}
331331
}
332332

333-
type func fromRaw(s:String) -> NSChangeDictionaryKey? {
333+
static func fromRaw(s:String) -> NSChangeDictionaryKey? {
334334
switch s {
335335
case "NSKeyValueChangeKindKey":
336336
return .NSKeyValueChangeKindKey

docs/proposals/OptionSets.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ For example::
170170
AlignHeightInward : Bool = false
171171

172172
// convenience combinations
173-
type func NSAlignAllEdgesInward() {
173+
static func NSAlignAllEdgesInward() {
174174
return NSAlignmentOptions(AlignMinXInward: true,
175175
AlignMaxXInward: true,
176176
AlignMinYInward: true,
@@ -265,7 +265,7 @@ bitwise operations can be applied to them.
265265
struct MyOptions : OptionSet {
266266
var Foo, Bar, Bas : Bool = false
267267
268-
type func Foobar() -> MyOptions {
268+
static func Foobar() -> MyOptions {
269269
return MyOptions(Foo: true, Bar: true)
270270
}
271271
}
@@ -281,14 +281,14 @@ individual option::
281281
// Stored properties of instances
282282
var Foo, Bar, Bas : Bool = false
283283

284-
type func Foobar() -> MyOptions {
284+
static func Foobar() -> MyOptions {
285285
return MyOptions(Foo: true, Bar: true)
286286
}
287287

288288
// Implicitly-generated static properties?
289-
type func Foo() -> MyOptions { return MyOptions(Foo: true) }
290-
type func Bar() -> MyOptions { return MyOptions(Bar: true) }
291-
type func Bas() -> MyOptions { return MyOptions(Bas: true) }
289+
static func Foo() -> MyOptions { return MyOptions(Foo: true) }
290+
static func Bar() -> MyOptions { return MyOptions(Bar: true) }
291+
static func Bas() -> MyOptions { return MyOptions(Bas: true) }
292292
}
293293

294294
var x: MyOptions = .Foobar() | .Bas()

include/swift/AST/Decl.h

+56-15
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ enum class CircularityCheck {
111111
Checked
112112
};
113113

114+
/// Describes which spelling was used in the source for the 'static' or 'class'
115+
/// keyword.
116+
enum class StaticSpellingKind : uint8_t {
117+
None,
118+
KeywordStatic,
119+
KeywordClass,
120+
};
121+
122+
/// Diagnostic printing of \c StaticSpellingKind.
123+
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, StaticSpellingKind SSK);
124+
114125
/// Decl - Base class for all declarations in Swift.
115126
class alignas(8) Decl {
116127
// alignas(8) because we use three tag bits on Decl*.
@@ -142,13 +153,16 @@ class alignas(8) Decl {
142153
/// \brief Whether this pattern binding declares static variables.
143154
unsigned IsStatic : 1;
144155

156+
/// \brief Whether 'static' or 'class' was used.
157+
unsigned StaticSpelling : 2;
158+
145159
/// \brief Whether this pattern binding declares a variable with storage.
146160
unsigned HasStorage : 1;
147161

148162
/// \brief Whether this pattern binding appears in a conditional statement.
149163
unsigned Conditional : 1;
150164
};
151-
enum { NumPatternBindingDeclBits = NumDeclBits + 3 };
165+
enum { NumPatternBindingDeclBits = NumDeclBits + 5 };
152166
static_assert(NumPatternBindingDeclBits <= 32, "fits in an unsigned");
153167

154168
class ValueDeclBitfields {
@@ -165,8 +179,8 @@ class alignas(8) Decl {
165179

166180
/// \brief Whether this property is a type property (currently unfortunately
167181
/// called 'static').
168-
unsigned Static : 1;
169-
182+
unsigned IsStatic : 1;
183+
170184
/// \brief Whether this is a 'let' property, which can only be initialized
171185
/// in its declaration, and never assigned to, making it immutable.
172186
unsigned IsLet : 1;
@@ -200,13 +214,17 @@ class alignas(8) Decl {
200214
unsigned : NumAbstractFunctionDeclBits;
201215

202216
/// Whether this function is a 'static' method.
203-
unsigned Static : 1;
217+
unsigned IsStatic : 1;
218+
219+
/// \brief Whether 'static' or 'class' was used.
220+
unsigned StaticSpelling : 2;
221+
204222
/// Whether this function is a 'mutating' method.
205223
unsigned Mutating : 1;
206224
/// Whether this function has a \c DynamicSelf return type.
207225
unsigned HasDynamicSelf : 1;
208226
};
209-
enum { NumFuncDeclBits = NumAbstractFunctionDeclBits + 3 };
227+
enum { NumFuncDeclBits = NumAbstractFunctionDeclBits + 5 };
210228
static_assert(NumFuncDeclBits <= 32, "fits in an unsigned");
211229

212230
class ConstructorDeclBitfields {
@@ -1252,15 +1270,18 @@ class PatternBindingDecl : public Decl {
12521270
friend class Decl;
12531271

12541272
public:
1255-
PatternBindingDecl(SourceLoc StaticLoc,
1256-
SourceLoc VarLoc, Pattern *Pat, Expr *E,
1273+
PatternBindingDecl(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
1274+
SourceLoc VarLoc,
1275+
Pattern *Pat, Expr *E,
12571276
bool hasStorage,
12581277
bool isConditional,
12591278
DeclContext *Parent)
12601279
: Decl(DeclKind::PatternBinding, Parent),
12611280
StaticLoc(StaticLoc), VarLoc(VarLoc), Pat(Pat),
12621281
InitAndChecked(E, false) {
12631282
PatternBindingDeclBits.IsStatic = StaticLoc.isValid();
1283+
PatternBindingDeclBits.StaticSpelling =
1284+
static_cast<unsigned>(StaticSpelling);
12641285
PatternBindingDeclBits.HasStorage = hasStorage;
12651286
PatternBindingDeclBits.Conditional = isConditional;
12661287
}
@@ -1292,6 +1313,13 @@ class PatternBindingDecl : public Decl {
12921313
bool isStatic() const { return PatternBindingDeclBits.IsStatic; }
12931314
void setStatic(bool s) { PatternBindingDeclBits.IsStatic = s; }
12941315
SourceLoc getStaticLoc() const { return StaticLoc; }
1316+
/// \returns the way 'static'/'class' was spelled in the source.
1317+
StaticSpellingKind getStaticSpelling() const {
1318+
return static_cast<StaticSpellingKind>(
1319+
PatternBindingDeclBits.StaticSpelling);
1320+
}
1321+
/// \returns the way 'static'/'class' should be spelled for this declaration.
1322+
StaticSpellingKind getCorrectStaticSpelling() const;
12951323

12961324
static bool classof(const Decl *D) {
12971325
return D->getKind() == DeclKind::PatternBinding;
@@ -2633,7 +2661,7 @@ class VarDecl : public AbstractStorageDecl {
26332661
VarDecl(bool IsStatic, bool IsLet, SourceLoc NameLoc, Identifier Name,
26342662
Type Ty, DeclContext *DC)
26352663
: AbstractStorageDecl(DeclKind::Var, DC, Name, NameLoc) {
2636-
VarDeclBits.Static = IsStatic;
2664+
VarDeclBits.IsStatic = IsStatic;
26372665
VarDeclBits.IsLet = IsLet;
26382666
VarDeclBits.IsDebuggerVar = false;
26392667
setType(Ty);
@@ -2670,8 +2698,11 @@ class VarDecl : public AbstractStorageDecl {
26702698
bool isAnonClosureParam() const;
26712699

26722700
/// Is this a type ('static') variable?
2673-
bool isStatic() const { return VarDeclBits.Static; }
2674-
void setStatic(bool IsStatic) { VarDeclBits.Static = IsStatic; }
2701+
bool isStatic() const { return VarDeclBits.IsStatic; }
2702+
void setStatic(bool IsStatic) { VarDeclBits.IsStatic = IsStatic; }
2703+
2704+
/// \returns the way 'static'/'class' should be spelled for this declaration.
2705+
StaticSpellingKind getCorrectStaticSpelling() const;
26752706

26762707
/// Is this an immutable 'let' property?
26772708
bool isLet() const { return VarDeclBits.IsLet; }
@@ -3053,14 +3084,16 @@ class FuncDecl : public AbstractFunctionDecl {
30533084
FuncDecl *OverriddenDecl;
30543085
OperatorDecl *Operator;
30553086

3056-
FuncDecl(SourceLoc StaticLoc, SourceLoc FuncLoc, Identifier Name,
3087+
FuncDecl(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
3088+
SourceLoc FuncLoc, Identifier Name,
30573089
SourceLoc NameLoc, unsigned NumParamPatterns,
30583090
GenericParamList *GenericParams, Type Ty, DeclContext *Parent)
30593091
: AbstractFunctionDecl(DeclKind::Func, Parent, Name, NameLoc,
30603092
NumParamPatterns, GenericParams),
30613093
StaticLoc(StaticLoc), FuncLoc(FuncLoc),
30623094
OverriddenDecl(nullptr), Operator(nullptr) {
3063-
FuncDeclBits.Static = StaticLoc.isValid() || getName().isOperator();
3095+
FuncDeclBits.IsStatic = StaticLoc.isValid() || getName().isOperator();
3096+
FuncDeclBits.StaticSpelling = static_cast<unsigned>(StaticSpelling);
30643097
assert(NumParamPatterns > 0 && "Must have at least an empty tuple arg");
30653098
setType(Ty);
30663099
FuncDeclBits.Mutating = false;
@@ -3070,27 +3103,35 @@ class FuncDecl : public AbstractFunctionDecl {
30703103
public:
30713104
/// Factory function only for use by deserialization.
30723105
static FuncDecl *createDeserialized(ASTContext &Context, SourceLoc StaticLoc,
3106+
StaticSpellingKind StaticSpelling,
30733107
SourceLoc FuncLoc, Identifier Name,
30743108
SourceLoc NameLoc,
30753109
GenericParamList *GenericParams, Type Ty,
30763110
unsigned NumParamPatterns,
30773111
DeclContext *Parent);
30783112

30793113
static FuncDecl *create(ASTContext &Context, SourceLoc StaticLoc,
3114+
StaticSpellingKind StaticSpelling,
30803115
SourceLoc FuncLoc, Identifier Name, SourceLoc NameLoc,
30813116
GenericParamList *GenericParams, Type Ty,
30823117
ArrayRef<Pattern *> ArgParams,
30833118
ArrayRef<Pattern *> BodyParams,
30843119
TypeLoc FnRetType, DeclContext *Parent);
30853120

30863121
bool isStatic() const {
3087-
return FuncDeclBits.Static;
3122+
return FuncDeclBits.IsStatic;
3123+
}
3124+
/// \returns the way 'static'/'class' was spelled in the source.
3125+
StaticSpellingKind getStaticSpelling() const {
3126+
return static_cast<StaticSpellingKind>(FuncDeclBits.StaticSpelling);
30883127
}
3128+
/// \returns the way 'static'/'class' should be spelled for this declaration.
3129+
StaticSpellingKind getCorrectStaticSpelling() const;
30893130
bool isMutating() const {
30903131
return FuncDeclBits.Mutating;
30913132
}
3092-
void setStatic(bool Static = true) {
3093-
FuncDeclBits.Static = Static;
3133+
void setStatic(bool IsStatic = true) {
3134+
FuncDeclBits.IsStatic = IsStatic;
30943135
}
30953136
void setMutating(bool Mutating = true) {
30963137
FuncDeclBits.Mutating = Mutating;

0 commit comments

Comments
 (0)