diff options
| author | Tim Foley <tfoley@nvidia.com> | 2017-08-09 12:57:37 -0700 |
|---|---|---|
| committer | Tim Foley <tfoley@nvidia.com> | 2017-08-10 13:05:04 -0700 |
| commit | a5a436c4783fb75a0d089a6483219c06db91f593 (patch) | |
| tree | 224c16ad374c5ed533a497beeb75753e7ce2d771 /source/slang/lower.cpp | |
| parent | 6e4830f4d74adef0a47c6503d84dc114240fafa3 (diff) | |
Make source location lightweight
Fixes #24
So far the code has used a representation for source locations that is heavy-weight, but typical of research or hobby compilers: a `struct` type containing a line number and a (heap-allocated) string.
This is actually very convenient for debugging, but it means that any data structure that might contain a source location needs careful memory management (because of those strings) and has a tendency to bloat.
The new represnetation is that a source location is just a pointer-sized integer.
In the simplest mental model, you can think of this as just counting every byte of source text that is passed in, and using those to name locations.
Finding the path and line number that corresponds to a location involves a lookup step, but we can arrange to store all the files in an array sorted by their start locations, and do a binary search.
Finding line numbers inside a file is similarly fast (one you pay a one-time cost to build an array of starting offsets for lines).
More advanced compilers like clang actually go further and create a unique range of source locations to represent a file each time it gets included, so that they can track the include stack and reproduce it in diagnostic messages.
I'm not doing anything that clever here.
Diffstat (limited to 'source/slang/lower.cpp')
| -rw-r--r-- | source/slang/lower.cpp | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/source/slang/lower.cpp b/source/slang/lower.cpp index 611180415..e253abca6 100644 --- a/source/slang/lower.cpp +++ b/source/slang/lower.cpp @@ -318,7 +318,7 @@ class PseudoVarDecl : public RefObject { public: Token Name; - CodePosition Position; + SourceLoc Position; TypeExp type; }; @@ -339,7 +339,7 @@ public: class PseudoExpr : public RefObject { public: - CodePosition Position; + SourceLoc Position; QualType type; }; @@ -384,7 +384,7 @@ public: List<Element> elements; }; -static CodePosition getPosition(LoweredExpr const& expr) +static SourceLoc getPosition(LoweredExpr const& expr) { switch (expr.getFlavor()) { @@ -393,7 +393,7 @@ static CodePosition getPosition(LoweredExpr const& expr) case LoweredExpr::Flavor::VaryingTuple: return expr.getVaryingTupleExpr()->Position; default: SLANG_UNREACHABLE("all cases handled"); - return CodePosition(); + return SourceLoc(); } } @@ -804,7 +804,7 @@ struct LoweringVisitor } RefPtr<Expr> createSimpleVarRef( - CodePosition const& loc, + SourceLoc const& loc, VarDeclBase* decl) { RefPtr<VarExpr> result = new VarExpr(); @@ -816,7 +816,7 @@ struct LoweringVisitor } LoweredExpr createVarRef( - CodePosition const& loc, + SourceLoc const& loc, LoweredDecl const& decl) { switch (decl.getFlavor()) @@ -838,7 +838,7 @@ struct LoweringVisitor LoweredExpr createTupleRef( - CodePosition const& loc, + SourceLoc const& loc, TupleVarDecl* decl) { RefPtr<TupleExpr> result = new TupleExpr(); @@ -868,7 +868,7 @@ struct LoweringVisitor } LoweredExpr createVaryingTupleRef( - CodePosition const& /*loc*/, + SourceLoc const& /*loc*/, VaryingTupleVarDecl* decl) { return decl->expr; |
