Skip to content

Commit a30e1b4

Browse files
committed
AST: Create a 'ValueName' concept.
This will allow ValueDecls to have compound names, such as for selector-style function decls. NFC yet. Swift SVN r14528
1 parent 3b913f1 commit a30e1b4

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

include/swift/AST/Decl.h

+72-6
Original file line numberDiff line numberDiff line change
@@ -1443,16 +1443,78 @@ class IfConfigDecl : public Decl {
14431443
return D->getKind() == DeclKind::IfConfig;
14441444
}
14451445
};
1446-
1446+
1447+
/// A declaration name, which may comprise one or more identifier pieces.
1448+
class ValueName {
1449+
// Either a single identifier piece stored inline, or a reference to an array
1450+
// of identifier pieces owned by the ASTContext.
1451+
llvm::PointerUnion<Identifier, Identifier*> First;
1452+
// The size of a referenced component array, or zero if the identifier is
1453+
// stored in-line.
1454+
unsigned Count;
1455+
1456+
public:
1457+
/// Build a null name.
1458+
ValueName() : First(Identifier()), Count(0) {}
1459+
1460+
/// Build a simple value name with one component.
1461+
/*implicit*/ ValueName(Identifier simpleName)
1462+
: First(simpleName), Count(0) {}
1463+
1464+
/// Build a compound value name.
1465+
explicit ValueName(MutableArrayRef<Identifier> components)
1466+
: First(components.data()), Count(components.size())
1467+
{
1468+
assert(Count > 0 && "must have at least one name component");
1469+
}
1470+
1471+
/// Get the first name component. This is the name that is looked up in
1472+
/// "C-style" property accesses, such as 'foo.bar' or 'foo.bar(1, 2)'.
1473+
///
1474+
/// TODO: Eventually compound names should not return a name here.
1475+
Identifier getSimpleName() const {
1476+
if (Count)
1477+
return *First.get<Identifier*>();
1478+
1479+
return First.get<Identifier>();
1480+
}
1481+
1482+
/// Get the components array.
1483+
ArrayRef<Identifier> getComponents() const {
1484+
if (Count)
1485+
return {First.get<Identifier*>(), Count};
1486+
auto name = First.get<Identifier>();
1487+
if (name.get())
1488+
return name;
1489+
return {};
1490+
}
1491+
1492+
explicit operator bool() const {
1493+
if (Count)
1494+
return true;
1495+
return First.get<Identifier>().get();
1496+
}
1497+
1498+
/// True if this is a simple one-component name.
1499+
bool isSimpleName() const {
1500+
return Count <= 1;
1501+
}
1502+
1503+
/// True if this name is an operator.
1504+
bool isOperator() const {
1505+
return isSimpleName() && getSimpleName().isOperator();
1506+
}
1507+
};
1508+
14471509
/// ValueDecl - All named decls that are values in the language. These can
14481510
/// have a type, etc.
14491511
class ValueDecl : public Decl {
1450-
Identifier Name;
1512+
ValueName Name;
14511513
SourceLoc NameLoc;
14521514
Type Ty;
14531515

14541516
protected:
1455-
ValueDecl(DeclKind K, DeclContext *DC, Identifier name, SourceLoc NameLoc)
1517+
ValueDecl(DeclKind K, DeclContext *DC, ValueName name, SourceLoc NameLoc)
14561518
: Decl(K, DC), Name(name), NameLoc(NameLoc) {
14571519
ValueDeclBits.ConformsToProtocolRequrement = false;
14581520
}
@@ -1466,10 +1528,14 @@ class ValueDecl : public Decl {
14661528
/// swift code.
14671529
bool isDefinition() const;
14681530

1469-
bool hasName() const { return !Name.empty(); }
1470-
Identifier getName() const { return Name; }
1531+
bool hasName() const { return bool(Name); }
1532+
/// TODO: Rename to getSimpleName?
1533+
Identifier getName() const { return Name.getSimpleName(); }
14711534
bool isOperator() const { return Name.isOperator(); }
1472-
1535+
/// TODO: Rename to getName?
1536+
ValueName getFullName() const { return Name; }
1537+
1538+
14731539
SourceLoc getNameLoc() const { return NameLoc; }
14741540
SourceLoc getLoc() const { return NameLoc; }
14751541

0 commit comments

Comments
 (0)