serene/src/serene/source_mgr.cpp

207 lines
7.0 KiB
C++
Raw Normal View History

/*
* Serene programming language.
*
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "serene/source_mgr.h"
2021-09-12 14:01:02 +01:00
#include "serene/errors/constants.h"
#include "serene/namespace.h"
2021-09-12 14:01:02 +01:00
#include "serene/reader/location.h"
#include "serene/reader/reader.h"
#include "serene/utils.h"
#include <llvm/Support/FormatVariadic.h>
#include <llvm/Support/Locale.h>
2021-09-15 15:04:11 +01:00
#include <llvm/Support/MemoryBufferRef.h>
#include <llvm/Support/Path.h>
2021-09-15 15:04:11 +01:00
#include <mlir/Support/LogicalResult.h>
#include <system_error>
namespace serene {
std::string inline SourceMgr::convertNamespaceToPath(std::string ns_name) {
std::replace(ns_name.begin(), ns_name.end(), '.', '/');
llvm::SmallString<256> path;
path.append(ns_name);
llvm::sys::path::native(path);
return std::string(path);
};
NSPtr SourceMgr::readNamespace(SereneContext &ctx, std::string name,
2021-09-15 15:04:11 +01:00
reader::LocationRange importLoc) {
std::string importedFile;
auto path = convertNamespaceToPath(name);
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> newBufOrErr(
std::make_error_code(std::errc::no_such_file_or_directory));
SMGR_LOG("Attempt to load namespace: " + name);
2021-09-15 15:04:11 +01:00
// If the file didn't exist directly, see if it's in an include path.
for (unsigned i = 0, e = loadPaths.size(); i != e && !newBufOrErr; ++i) {
// TODO: Ugh, Udgly, fix this using llvm::sys::path functions
2021-09-15 15:04:11 +01:00
importedFile = loadPaths[i] + llvm::sys::path::get_separator().data() +
path + "." + DEFAULT_SUFFIX;
2021-09-15 15:04:11 +01:00
SMGR_LOG("Try to load the ns from: " + importedFile);
newBufOrErr = llvm::MemoryBuffer::getFile(importedFile);
}
if (!newBufOrErr) {
2021-09-12 14:01:02 +01:00
auto msg = llvm::formatv("Couldn't find namespace '{0}'", name);
ctx.diagEngine->emitSyntaxError(importLoc, errors::NSLoadError,
llvm::StringRef(msg));
return nullptr;
}
auto bufferId = AddNewSourceBuffer(std::move(*newBufOrErr), importLoc);
UNUSED(nsTable.insert_or_assign(name, bufferId));
if (bufferId == 0) {
2021-09-12 14:01:02 +01:00
auto msg = llvm::formatv("Couldn't add namespace '{0}'", name);
ctx.diagEngine->emitSyntaxError(importLoc, errors::NSAddToSMError,
llvm::StringRef(msg));
return nullptr;
}
// Since we moved the buffer to be added as the source storage we
// need to get a pointer to it again
auto *buf = getMemoryBuffer(bufferId);
// Read the content of the buffer by passing it the reader
auto maybeAst = reader::read(ctx, buf->getBuffer(), name,
2021-09-15 15:04:11 +01:00
llvm::Optional(llvm::StringRef(importedFile)));
if (!maybeAst) {
SMGR_LOG("Couldn't Read namespace: " + name)
return nullptr;
}
// Create the NS and set the AST
auto ns =
2021-09-15 15:04:11 +01:00
makeNamespace(ctx, name, llvm::Optional(llvm::StringRef(importedFile)));
if (mlir::failed(ns->setTree(maybeAst.getValue()))) {
SMGR_LOG("Couldn't set the AST for namespace: " + name)
return nullptr;
}
return ns;
};
2021-09-12 14:01:02 +01:00
unsigned SourceMgr::AddNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> f,
reader::LocationRange includeLoc) {
SrcBuffer nb;
nb.buffer = std::move(f);
2021-09-15 15:04:11 +01:00
nb.importLoc = includeLoc;
2021-09-12 14:01:02 +01:00
buffers.push_back(std::move(nb));
return buffers.size();
};
template <typename T>
static std::vector<T> &GetOrCreateOffsetCache(void *&offsetCache,
llvm::MemoryBuffer *buffer) {
if (offsetCache)
return *static_cast<std::vector<T> *>(offsetCache);
// Lazily fill in the offset cache.
auto *offsets = new std::vector<T>();
size_t sz = buffer->getBufferSize();
2021-09-15 15:04:11 +01:00
// TODO: Replace this assert with a realtime check
assert(sz <= std::numeric_limits<T>::max());
2021-09-15 15:04:11 +01:00
llvm::StringRef s = buffer->getBuffer();
for (size_t n = 0; n < sz; ++n) {
if (s[n] == '\n')
offsets->push_back(static_cast<T>(n));
}
2021-09-12 14:01:02 +01:00
offsetCache = offsets;
return *offsets;
}
2021-09-12 14:01:02 +01:00
template <typename T>
const char *SourceMgr::SrcBuffer::getPointerForLineNumberSpecialized(
unsigned lineNo) const {
std::vector<T> &offsets =
GetOrCreateOffsetCache<T>(offsetCache, buffer.get());
2021-09-12 14:01:02 +01:00
// We start counting line and column numbers from 1.
if (lineNo != 0)
--lineNo;
2021-09-12 14:01:02 +01:00
const char *bufStart = buffer->getBufferStart();
2021-09-12 14:01:02 +01:00
// The offset cache contains the location of the \n for the specified line,
// we want the start of the line. As such, we look for the previous entry.
if (lineNo == 0)
return bufStart;
if (lineNo > offsets.size())
return nullptr;
return bufStart + offsets[lineNo - 1] + 1;
}
2021-09-12 14:01:02 +01:00
/// Return a pointer to the first character of the specified line number or
/// null if the line number is invalid.
const char *
SourceMgr::SrcBuffer::getPointerForLineNumber(unsigned lineNo) const {
size_t sz = buffer->getBufferSize();
if (sz <= std::numeric_limits<uint8_t>::max())
return getPointerForLineNumberSpecialized<uint8_t>(lineNo);
else if (sz <= std::numeric_limits<uint16_t>::max())
return getPointerForLineNumberSpecialized<uint16_t>(lineNo);
else if (sz <= std::numeric_limits<uint32_t>::max())
return getPointerForLineNumberSpecialized<uint32_t>(lineNo);
else
return getPointerForLineNumberSpecialized<uint64_t>(lineNo);
}
SourceMgr::SrcBuffer::SrcBuffer(SourceMgr::SrcBuffer &&other)
: buffer(std::move(other.buffer)), offsetCache(other.offsetCache),
2021-09-15 15:04:11 +01:00
importLoc(other.importLoc) {
other.offsetCache = nullptr;
}
SourceMgr::SrcBuffer::~SrcBuffer() {
if (offsetCache) {
size_t sz = buffer->getBufferSize();
if (sz <= std::numeric_limits<uint8_t>::max())
delete static_cast<std::vector<uint8_t> *>(offsetCache);
else if (sz <= std::numeric_limits<uint16_t>::max())
delete static_cast<std::vector<uint16_t> *>(offsetCache);
else if (sz <= std::numeric_limits<uint32_t>::max())
delete static_cast<std::vector<uint32_t> *>(offsetCache);
else
delete static_cast<std::vector<uint64_t> *>(offsetCache);
offsetCache = nullptr;
}
}
}; // namespace serene