SourceManager: add functions that compare SourceLocs and SourceRanges

Swift SVN r6994
This commit is contained in:
Dmitri Hrybenko
2013-08-07 20:06:12 +00:00
parent 1659de21a1
commit 686f9ec7fc
6 changed files with 56 additions and 36 deletions

View File

@@ -63,6 +63,23 @@ public:
unsigned getHashbangBufferID() const {
return HashbangBufferID;
}
/// Returns true if \c LHS is before \c RHS in the source buffer.
bool isBeforeInBuffer(SourceLoc LHS, SourceLoc RHS) const {
return LHS.Value.getPointer() < RHS.Value.getPointer();
}
/// Returns true if range \c R contains the location \c Loc.
bool rangeContainsLoc(SourceRange R, SourceLoc Loc) const {
return Loc == R.Start || Loc == R.End ||
(isBeforeInBuffer(R.Start, Loc) && isBeforeInBuffer(Loc, R.End));
}
/// Returns true if range \c Enclosing contains the range \c Inner.
bool rangeContains(SourceRange Enclosing, SourceRange Inner) const {
return rangeContainsLoc(Enclosing, Inner.Start) &&
rangeContainsLoc(Enclosing, Inner.End);
}
};
} // namespace swift

View File

@@ -15,7 +15,7 @@
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/SourceManager.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/AST.h"
#include "swift/AST/ASTVisitor.h"
@@ -269,15 +269,16 @@ static void lookupTypeMembers(Type BaseType, VisibleDeclConsumer &Consumer,
namespace {
struct FindLocalVal : public StmtVisitor<FindLocalVal> {
const SourceManager &SM;
SourceLoc Loc;
VisibleDeclConsumer &Consumer;
FindLocalVal(SourceLoc Loc, VisibleDeclConsumer &Consumer)
: Loc(Loc), Consumer(Consumer) {}
FindLocalVal(const SourceManager &SM, SourceLoc Loc,
VisibleDeclConsumer &Consumer)
: SM(SM), Loc(Loc), Consumer(Consumer) {}
bool IntersectsRange(SourceRange R) {
return R.Start.Value.getPointer() <= Loc.Value.getPointer() &&
R.End.Value.getPointer() >= Loc.Value.getPointer();
return SM.rangeContainsLoc(R, Loc);
}
void checkValueDecl(ValueDecl *D) {
@@ -397,6 +398,7 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
const DeclContext *DC,
SourceLoc Loc) {
const Module &M = *DC->getParentModule();
const SourceManager &SM = DC->getASTContext().SourceMgr;
// If we are inside of a method, check to see if there are any ivars in scope,
// and if so, whether this is a reference to one of them.
@@ -407,14 +409,14 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
Type ExtendedType;
if (auto FE = dyn_cast<FuncExpr>(DC)) {
for (auto *P : FE->getArgParamPatterns())
FindLocalVal(Loc, Consumer).checkPattern(P);
FindLocalVal(SM, Loc, Consumer).checkPattern(P);
// Look for local variables; normally, the parser resolves these
// for us, but it can't do the right thing inside local types.
// FIXME: when we can parse and typecheck the function body partially for
// code completion, FE->getBody() check can be removed.
if (Loc.isValid() && FE->getBody()) {
FindLocalVal(Loc, Consumer).visit(FE->getBody());
FindLocalVal(SM, Loc, Consumer).visit(FE->getBody());
}
FuncDecl *FD = FE->getDecl();
@@ -433,7 +435,7 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
GenericParams = FD->getGenericParams();
} else if (auto CE = dyn_cast<PipeClosureExpr>(DC)) {
if (Loc.isValid()) {
FindLocalVal(Loc, Consumer).visit(CE->getBody());
FindLocalVal(SM, Loc, Consumer).visit(CE->getBody());
}
} else if (auto ED = dyn_cast<ExtensionDecl>(DC)) {
ExtendedType = ED->getExtendedType();
@@ -447,7 +449,7 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
// Look for local variables; normally, the parser resolves these
// for us, but it can't do the right thing inside local types.
if (Loc.isValid()) {
FindLocalVal(Loc, Consumer).visit(CD->getBody());
FindLocalVal(SM, Loc, Consumer).visit(CD->getBody());
}
BaseDecl = CD->getImplicitThisDecl();
@@ -461,7 +463,7 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
// Look for local variables; normally, the parser resolves these
// for us, but it can't do the right thing inside local types.
if (Loc.isValid()) {
FindLocalVal(Loc, Consumer).visit(CD->getBody());
FindLocalVal(SM, Loc, Consumer).visit(CD->getBody());
}
BaseDecl = DD->getImplicitThisDecl();
@@ -480,7 +482,7 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
// Check the generic parameters for something with the given name.
if (GenericParams) {
FindLocalVal(Loc, Consumer).checkGenericParams(GenericParams);
FindLocalVal(SM, Loc, Consumer).checkGenericParams(GenericParams);
}
DC = DC->getParent();
@@ -491,7 +493,7 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
// Look for local variables in top-level code; normally, the parser
// resolves these for us, but it can't do the right thing for
// local types.
FindLocalVal(Loc, Consumer).checkTranslationUnit(TU);
FindLocalVal(SM, Loc, Consumer).checkTranslationUnit(TU);
}
}

View File

@@ -15,7 +15,7 @@
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/SourceManager.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/AST.h"
#include "swift/AST/ASTVisitor.h"
@@ -136,16 +136,16 @@ void swift::removeShadowedDecls(SmallVectorImpl<ValueDecl*> &decls,
}
struct FindLocalVal : public StmtVisitor<FindLocalVal> {
const SourceManager &SM;
SourceLoc Loc;
Identifier Name;
ValueDecl *MatchingValue;
FindLocalVal(SourceLoc Loc, Identifier Name)
: Loc(Loc), Name(Name), MatchingValue(nullptr) {}
FindLocalVal(const SourceManager &SM, SourceLoc Loc, Identifier Name)
: SM(SM), Loc(Loc), Name(Name), MatchingValue(nullptr) {}
bool IntersectsRange(SourceRange R) {
return R.Start.Value.getPointer() <= Loc.Value.getPointer() &&
R.End.Value.getPointer() >= Loc.Value.getPointer();
return SM.rangeContainsLoc(R, Loc);
}
void checkValueDecl(ValueDecl *D) {
@@ -273,6 +273,7 @@ UnqualifiedLookup::UnqualifiedLookup(Identifier Name, DeclContext *DC,
typedef UnqualifiedLookupResult Result;
Module &M = *DC->getParentModule();
const SourceManager &SM = DC->getASTContext().SourceMgr;
// Never perform local lookup for operators.
if (Name.isOperator())
@@ -291,7 +292,7 @@ UnqualifiedLookup::UnqualifiedLookup(Identifier Name, DeclContext *DC,
// FIXME: when we can parse and typecheck the function body partially for
// code completion, FE->getBody() check can be removed.
if (Loc.isValid() && FE->getBody()) {
FindLocalVal localVal(Loc, Name);
FindLocalVal localVal(SM, Loc, Name);
localVal.visit(FE->getBody());
if (!localVal.MatchingValue) {
for (Pattern *P : FE->getBodyParamPatterns())
@@ -324,7 +325,7 @@ UnqualifiedLookup::UnqualifiedLookup(Identifier Name, DeclContext *DC,
// Look for local variables; normally, the parser resolves these
// for us, but it can't do the right thing inside local types.
if (Loc.isValid()) {
FindLocalVal localVal(Loc, Name);
FindLocalVal localVal(SM, Loc, Name);
localVal.visit(CE->getBody());
if (!localVal.MatchingValue) {
localVal.checkPattern(CE->getParams());
@@ -346,7 +347,7 @@ UnqualifiedLookup::UnqualifiedLookup(Identifier Name, DeclContext *DC,
// Look for local variables; normally, the parser resolves these
// for us, but it can't do the right thing inside local types.
if (Loc.isValid()) {
FindLocalVal localVal(Loc, Name);
FindLocalVal localVal(SM, Loc, Name);
localVal.visit(CD->getBody());
if (!localVal.MatchingValue)
localVal.checkPattern(CD->getArguments());
@@ -367,7 +368,7 @@ UnqualifiedLookup::UnqualifiedLookup(Identifier Name, DeclContext *DC,
// Look for local variables; normally, the parser resolves these
// for us, but it can't do the right thing inside local types.
if (Loc.isValid()) {
FindLocalVal localVal(Loc, Name);
FindLocalVal localVal(SM, Loc, Name);
localVal.visit(CD->getBody());
if (!localVal.MatchingValue)
localVal.checkPattern(CD->getArguments());
@@ -388,7 +389,7 @@ UnqualifiedLookup::UnqualifiedLookup(Identifier Name, DeclContext *DC,
// Check the generic parameters for something with the given name.
if (GenericParams) {
FindLocalVal localVal(Loc, Name);
FindLocalVal localVal(SM, Loc, Name);
localVal.checkGenericParams(GenericParams);
if (localVal.MatchingValue) {
@@ -463,7 +464,7 @@ UnqualifiedLookup::UnqualifiedLookup(Identifier Name, DeclContext *DC,
->getInstanceType()->getAnyNominal()
: ExtendedType->getAnyNominal();
if (nominal && nominal->getGenericParams()) {
FindLocalVal localVal(Loc, Name);
FindLocalVal localVal(SM, Loc, Name);
localVal.checkGenericParams(nominal->getGenericParams());
if (localVal.MatchingValue) {
@@ -481,7 +482,7 @@ UnqualifiedLookup::UnqualifiedLookup(Identifier Name, DeclContext *DC,
// Look for local variables in top-level code; normally, the parser
// resolves these for us, but it can't do the right thing for
// local types.
FindLocalVal localVal(Loc, Name);
FindLocalVal localVal(SM, Loc, Name);
localVal.checkTranslationUnit(TU);
if (localVal.MatchingValue) {
Results.push_back(Result::getLocalDecl(localVal.MatchingValue));

View File

@@ -973,9 +973,7 @@ namespace {
llvm_unreachable("impossible parent node");
}
// FIXME: This is a very ugly way to check inclusion.
if (Enclosing.Start.Value.getPointer() > Current.Start.Value.getPointer()
|| Enclosing.End.Value.getPointer() < Current.End.Value.getPointer()){
if (!Ctx.SourceMgr.rangeContains(Enclosing, Current)) {
Out << "child source range not contained within its parent: ";
printEntity();
Out << "\n parent range: ";

View File

@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "swift/IDE/SyntaxColoring.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Module.h"
@@ -65,8 +66,7 @@ SyntaxColoringContext::SyntaxColoringContext(SourceManager &SM,
}
assert(Tok.getLoc().isValid());
assert(Nodes.empty() ||
Nodes.back().Loc.Value.getPointer()<Tok.getLoc().Value.getPointer());
assert(Nodes.empty() || SM.isBeforeInBuffer(Nodes.back().Loc, Tok.getLoc()));
Nodes.emplace_back(Kind, Tok.getLoc(), Tok.getLength());
}
@@ -80,12 +80,14 @@ SyntaxColoringContext::~SyntaxColoringContext() {
namespace {
class ColorASTWalker : public ASTWalker {
const SourceManager &SM;
public:
SyntaxColorWalker &SCWalker;
ArrayRef<SyntaxNode> TokenNodes;
ColorASTWalker(SyntaxColorWalker &SCWalker)
: SCWalker(SCWalker) { }
ColorASTWalker(const SourceManager &SM, SyntaxColorWalker &SCWalker)
: SM(SM), SCWalker(SCWalker) { }
void visitTranslationUnit(TranslationUnit &TU, ArrayRef<SyntaxNode> Tokens);
@@ -100,7 +102,7 @@ private:
} // anonymous namespace
bool SyntaxColoringContext::walk(SyntaxColorWalker &Walker) {
ColorASTWalker ASTWalk(Walker);
ColorASTWalker ASTWalk(TU.Ctx.SourceMgr, Walker);
ASTWalk.visitTranslationUnit(TU, Impl.TokenNodes);
return true;
}
@@ -132,8 +134,7 @@ bool ColorASTWalker::passTokenNodesUntil(SourceLoc Loc, bool Inclusive) {
unsigned I = 0;
for (unsigned E = TokenNodes.size(); I != E; ++I) {
SourceLoc TokLoc = TokenNodes[I].Loc;
if (TokLoc.Value.getPointer() > Loc.Value.getPointer() ||
(!Inclusive && TokLoc.Value.getPointer() == Loc.Value.getPointer())) {
if (SM.isBeforeInBuffer(Loc, TokLoc) || (!Inclusive && TokLoc == Loc)) {
break;
}
if (!passNode(TokenNodes[I]))

View File

@@ -16,6 +16,7 @@
#include "ConstraintSystem.h"
#include "TypeChecker.h"
#include "swift/Basic/SourceManager.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/NameLookup.h"
@@ -1053,8 +1054,8 @@ static void suggestExplicitConformance(TypeChecker &tc,
// FIXME: < on character pointers is a horrible hack.
assert(owner != witnessOwner && "Owners cannot match here.");
if (witnessOwner->getLoc().Value.getPointer() <
owner->getLoc().Value.getPointer())
if (tc.Context.SourceMgr.isBeforeInBuffer(witnessOwner->getLoc(),
owner->getLoc()))
owner = witnessOwner;
}