summaryrefslogtreecommitdiff
path: root/willow/tools/willowc/include/sourcemanager.hpp
blob: 25d412886fbb6855ea9751aeef3c413fa0111a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef WILLOWC_INCLUDE_SOURCEMANAGER_HPP
#define WILLOWC_INCLUDE_SOURCEMANAGER_HPP

#include <memory>
#include <string>
#include <unordered_map>
#include <map>
#include <vector>
#include <optional>

#include <willow/IR/Location.h>

namespace willowc {

using FileID = std::uint32_t;

class LineCache {
public:
private:
  std::unordered_map<std::size_t, std::size_t> cache_;
};

class SourceManager {
public:
struct File {
  std::string path;
  // TODO: remove this, but find something better than using the abs path
  std::string display_path;
  std::unique_ptr<char[]> buf;
  size_t size;

  willow::Location getLoc(std::size_t offset);
  std::map<std::size_t, uint32_t> linecache_;

  File(std::string path, std::string display_path, std::unique_ptr<char[]> buf,
       size_t size)
      : path(std::move(path)), display_path(std::move(display_path)),
        buf(std::move(buf)), size(size), linecache_{{{0, 1}}} {}
};

  std::optional<FileID> addFile(std::string_view path);
  std::optional<FileID> addStdIn();
  std::optional<FileID> getFileID(const std::string& path);
  std::string_view getBuf(FileID file) const {
    const File &f = file_table.at(file);
    return std::string_view(f.buf.get(), f.size);
  }
  File &getFile(FileID id) { return file_table.at(id); }   

  const std::vector<File>& files() const { return file_table; }
  std::vector<File>& files() { return file_table; }

  size_t numFiles() { return file_table.size(); }
private:
  std::vector<File> file_table;
  std::unordered_map<std::string, FileID> ids;
};

} // namespace willowc

#endif // WILLOWC_INCLUDE_SOURCEMANAGER_HPP