Skip to content

Commit 8a0a083

Browse files
authored
Merge pull request swiftlang#77797 from xedin/locatable-types
[AST/Sema] Introduce a new type that has associated location in source
2 parents 36e33a8 + df6950a commit 8a0a083

File tree

19 files changed

+300
-21
lines changed

19 files changed

+300
-21
lines changed

include/swift/AST/TypeNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ UNCHECKED_TYPE(ErrorUnion, Type)
210210
ALWAYS_CANONICAL_TYPE(Integer, Type)
211211
ABSTRACT_SUGARED_TYPE(Sugar, Type)
212212
SUGARED_TYPE(TypeAlias, SugarType)
213+
SUGARED_TYPE(Locatable, SugarType)
213214
ABSTRACT_SUGARED_TYPE(SyntaxSugar, SugarType)
214215
ABSTRACT_SUGARED_TYPE(UnarySyntaxSugar, SyntaxSugarType)
215216
SUGARED_TYPE(ArraySlice, UnarySyntaxSugarType)

include/swift/AST/TypeTransform.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,19 @@ case TypeKind::Id:
533533
newUnderlyingTy);
534534
}
535535

536+
case TypeKind::Locatable: {
537+
auto locatable = cast<LocatableType>(base);
538+
Type oldUnderlyingTy = Type(locatable->getSinglyDesugaredType());
539+
Type newUnderlyingTy = doIt(oldUnderlyingTy, pos);
540+
if (!newUnderlyingTy)
541+
return Type();
542+
543+
if (oldUnderlyingTy.getPointer() == newUnderlyingTy.getPointer())
544+
return t;
545+
546+
return LocatableType::get(locatable->getLoc(), newUnderlyingTy);
547+
}
548+
536549
case TypeKind::ErrorUnion: {
537550
auto errorUnion = cast<ErrorUnionType>(base);
538551
bool anyChanged = false;

include/swift/AST/Types.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ enum class ReferenceCounting : uint8_t;
8484
enum class ResilienceExpansion : unsigned;
8585
class SILModule;
8686
class SILType;
87+
class SourceLoc;
8788
class TypeAliasDecl;
8889
class TypeDecl;
8990
class NominalTypeDecl;
@@ -2322,6 +2323,28 @@ class TypeAliasType final
23222323
}
23232324
};
23242325

2326+
/// A type has been introduced at some fixed location in the AST.
2327+
class LocatableType final : public SugarType, public llvm::FoldingSetNode {
2328+
SourceLoc Loc;
2329+
2330+
LocatableType(SourceLoc loc, Type underlying,
2331+
RecursiveTypeProperties properties);
2332+
2333+
public:
2334+
SourceLoc getLoc() const { return Loc; }
2335+
2336+
static LocatableType *get(SourceLoc loc, Type underlying);
2337+
2338+
void Profile(llvm::FoldingSetNodeID &id) const;
2339+
2340+
static void Profile(llvm::FoldingSetNodeID &id, SourceLoc loc,
2341+
Type underlying);
2342+
2343+
static bool classof(const TypeBase *T) {
2344+
return T->getKind() == TypeKind::Locatable;
2345+
}
2346+
};
2347+
23252348
/// The various spellings of ownership modifier that can be used in source.
23262349
enum class ParamSpecifier : uint8_t {
23272350
/// No explicit ownership specifier was provided. The parameter will use the

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,6 +3501,8 @@ class ConstraintSystem {
35013501
return !solverState || solverState->recordFixes;
35023502
}
35033503

3504+
bool inSalvageMode() const { return solverState && solverState->recordFixes; }
3505+
35043506
ArrayRef<ConstraintFix *> getFixes() const { return Fixes.getArrayRef(); }
35053507

35063508
bool shouldSuppressDiagnostics() const {

lib/AST/ASTContext.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ struct ASTContext::Implementation {
530530

531531
llvm::DenseMap<Type, ErrorType *> ErrorTypesWithOriginal;
532532
llvm::FoldingSet<TypeAliasType> TypeAliasTypes;
533+
llvm::FoldingSet<LocatableType> LocatableTypes;
533534
llvm::FoldingSet<TupleType> TupleTypes;
534535
llvm::FoldingSet<PackType> PackTypes;
535536
llvm::FoldingSet<PackExpansionType> PackExpansionTypes;
@@ -3272,6 +3273,7 @@ void ASTContext::Implementation::Arena::dump(llvm::raw_ostream &os) const {
32723273

32733274
SIZE_AND_BYTES(ErrorTypesWithOriginal);
32743275
SIZE(TypeAliasTypes);
3276+
SIZE(LocatableTypes);
32753277
SIZE(TupleTypes);
32763278
SIZE(PackTypes);
32773279
SIZE(PackExpansionTypes);
@@ -3454,6 +3456,45 @@ void TypeAliasType::Profile(
34543456
id.AddPointer(underlying.getPointer());
34553457
}
34563458

3459+
LocatableType::LocatableType(SourceLoc loc, Type underlying,
3460+
RecursiveTypeProperties properties)
3461+
: SugarType(TypeKind::Locatable, underlying, properties), Loc(loc) {
3462+
ASSERT(loc.isValid());
3463+
}
3464+
3465+
LocatableType *LocatableType::get(SourceLoc loc, Type underlying) {
3466+
auto properties = underlying->getRecursiveProperties();
3467+
3468+
// Figure out which arena this type will go into.
3469+
auto &ctx = underlying->getASTContext();
3470+
auto arena = getArena(properties);
3471+
3472+
// Profile the type.
3473+
llvm::FoldingSetNodeID id;
3474+
LocatableType::Profile(id, loc, underlying);
3475+
3476+
// Did we already record this type?
3477+
void *insertPos;
3478+
auto &types = ctx.getImpl().getArena(arena).LocatableTypes;
3479+
if (auto result = types.FindNodeOrInsertPos(id, insertPos))
3480+
return result;
3481+
3482+
// Build a new type.
3483+
auto result = new (ctx, arena) LocatableType(loc, underlying, properties);
3484+
types.InsertNode(result, insertPos);
3485+
return result;
3486+
}
3487+
3488+
void LocatableType::Profile(llvm::FoldingSetNodeID &id) const {
3489+
Profile(id, Loc, Type(getSinglyDesugaredType()));
3490+
}
3491+
3492+
void LocatableType::Profile(llvm::FoldingSetNodeID &id, SourceLoc loc,
3493+
Type underlying) {
3494+
id.AddPointer(loc.getOpaquePointerValue());
3495+
id.AddPointer(underlying.getPointer());
3496+
}
3497+
34573498
// Simple accessors.
34583499
Type ErrorType::get(const ASTContext &C) { return C.TheErrorType; }
34593500

lib/AST/ASTDumper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4094,6 +4094,18 @@ namespace {
40944094
printFoot();
40954095
}
40964096

4097+
void visitLocatableType(LocatableType *T, StringRef label) {
4098+
printCommon("locatable_type", label);
4099+
printFieldQuotedRaw(
4100+
[&](raw_ostream &OS) {
4101+
auto &C = T->getASTContext();
4102+
T->getLoc().print(OS, C.SourceMgr);
4103+
},
4104+
"loc");
4105+
printRec(T->getSinglyDesugaredType(), "underlying");
4106+
printFoot();
4107+
}
4108+
40974109
void visitPackType(PackType *T, StringRef label) {
40984110
printCommon("pack_type", label);
40994111

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
12961296
case TypeKind::BuiltinUnsafeValueBuffer:
12971297
return appendOperator("BB");
12981298
case TypeKind::BuiltinUnboundGeneric:
1299+
case TypeKind::Locatable:
12991300
llvm_unreachable("not a real type");
13001301
case TypeKind::BuiltinFixedArray: {
13011302
auto bfa = cast<BuiltinFixedArrayType>(tybase);

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6006,6 +6006,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60066006
}
60076007
}
60086008

6009+
void visitLocatableType(LocatableType *T) {
6010+
visit(T->getSinglyDesugaredType());
6011+
}
6012+
60096013
void visitPackType(PackType *T) {
60106014
if (Options.PrintExplicitPackTypes || Options.PrintTypesForDebugging)
60116015
Printer << "Pack{";

lib/AST/Type.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,8 @@ Type SugarType::getSinglyDesugaredTypeSlow() {
19211921
#include "swift/AST/TypeNodes.def"
19221922
case TypeKind::TypeAlias:
19231923
llvm_unreachable("bound type alias types always have an underlying type");
1924+
case TypeKind::Locatable:
1925+
llvm_unreachable("locatable types always have an underlying type");
19241926
case TypeKind::ArraySlice:
19251927
case TypeKind::VariadicSequence:
19261928
implDecl = Context->getArrayDecl();

lib/AST/TypeWalker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Traversal : public TypeVisitor<Traversal, bool>
5050
return false;
5151

5252
}
53+
bool visitLocatableType(LocatableType *ty) { return false; }
5354
bool visitSILTokenType(SILTokenType *ty) { return false; }
5455

5556
bool visitPackType(PackType *ty) {

0 commit comments

Comments
 (0)