Allow variables with a getter or setter to be declared in a

struct/oneof/class extension, making them instance methods. Normal
variables are still not allowed in these contexts, however.

The fact that we set DeclContexts late causes some consternation here,
because it's not clear just how far we need to recurse in
DeclContext. I've done enough to make properties work, but I'm still
rather uneasy about the current state of affairs.


Swift SVN r1423
This commit is contained in:
Doug Gregor
2012-04-13 21:45:27 +00:00
parent 0cc17a2481
commit bbd5dfcfdf
5 changed files with 66 additions and 3 deletions

View File

@@ -104,7 +104,7 @@ public:
DeclKind getKind() const { return DeclKind(DeclBits.Kind); }
DeclContext *getDeclContext() const { return Context; }
void setDeclContext(DeclContext *DC) { Context = DC; }
void setDeclContext(DeclContext *DC);
/// getASTContext - Return the ASTContext that this decl lives in.
ASTContext &getASTContext() const {
@@ -218,6 +218,12 @@ class PatternBindingDecl : public Decl {
Pattern *Pat; // The pattern which this decl binds
Expr *Init; // Initializer for the variables
friend class Decl;
/// \brief Update the DeclContext for any variables in the pattern to
/// refer to the same DeclContext as this declaration.
void updateDeclContextForVars();
public:
PatternBindingDecl(SourceLoc VarLoc, Pattern *Pat, Expr *E,
DeclContext *Parent)
@@ -425,6 +431,9 @@ private:
GetSetRecord *GetSet;
friend class Decl;
void updateDeclContextForGetSet();
public:
VarDecl(SourceLoc VarLoc, Identifier Name, Type Ty, DeclContext *DC,
bool IsModuleScope)
@@ -540,6 +549,14 @@ public:
};
inline void Decl::setDeclContext(DeclContext *DC) {
Context = DC;
if (PatternBindingDecl *PBD = dyn_cast<PatternBindingDecl>(this))
PBD->updateDeclContextForVars();
else if (VarDecl *Var = dyn_cast<VarDecl>(this))
Var->updateDeclContextForGetSet();
}
} // end namespace swift
#endif