From a5a436c4783fb75a0d089a6483219c06db91f593 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Wed, 9 Aug 2017 12:57:37 -0700 Subject: 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. --- source/core/slang-string.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'source/core/slang-string.cpp') diff --git a/source/core/slang-string.cpp b/source/core/slang-string.cpp index 459396f69..9bc9e3a54 100644 --- a/source/core/slang-string.cpp +++ b/source/core/slang-string.cpp @@ -51,6 +51,12 @@ namespace Slang , endIndex(0) {} + StringSlice::StringSlice(String const& str) + : representation(str.buffer) + , beginIndex(0) + , endIndex(str.Length()) + {} + StringSlice::StringSlice(String const& str, UInt beginIndex, UInt endIndex) : representation(str.buffer) , beginIndex(beginIndex) -- cgit v1.2.3