aboutsummaryrefslogtreecommitdiff
path: root/source/20-codegen-compiler/CodegenInput.inl
blob: 0809e7f97b1e7e61cd99fcf8f5a7d546973b8276 (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
62
63
64
65
66
67
68
69
#pragma once

#include "CodegenConfig.hpp"
#include "CodegenDecl.hpp"

#include "CodegenUtils.inl"

#include <Utils.hpp>

#include <robin_hood.h>
#include <cinttypes>
#include <string>
#include <string_view>
#include <vector>

using namespace std::literals;

class CodegenInput {
private:
	std::vector<DeclEnum> mEnums;
	robin_hood::unordered_flat_map<std::string, size_t, StringHash, StringEqual> mDeclByName;
	robin_hood::unordered_node_map<std::string, DeclNamespace, StringHash, StringEqual> mNamespaces;

public:
	void AddEnum(std::string fullname, DeclEnum decl) {
#if CODEGEN_DEBUG_PRINT
		printf("Committed enum '%s'\n", decl.name.c_str());
		for (auto& elm : decl.elements) {
			printf("  - element %s = %" PRId64 "\n", elm.name.c_str(), elm.value);
		}
#endif

		mDeclByName.try_emplace(std::move(fullname), mEnums.size());
		mEnums.push_back(std::move(decl));
	}

	DeclNamespace* AddNamespace(DeclNamespace ns) {
		auto path = Utils::MakeFullName(""sv, &ns);
		auto [iter, success] = mNamespaces.try_emplace(std::move(path), std::move(ns));
		auto& nsRef = iter->second;
		if (success) {
			nsRef.fullname = iter->first;
		}
		return &nsRef;
	}

	const DeclEnum* FindEnumByName(std::string_view name) const {
		// TODO handle multiple kinds of decl
		auto iter = mDeclByName.find(name);
		if (iter != mDeclByName.end()) {
			return &mEnums[iter->second];
		} else {
			return nullptr;
		}
	}

	const DeclNamespace* FindNamespace(std::string_view fullname) const {
		auto iter = mNamespaces.find(fullname);
		if (iter != mNamespaces.end()) {
			return &iter->second;
		} else {
			return nullptr;
		}
	}

	DeclNamespace* FindNamespace(std::string_view name) {
		return const_cast<DeclNamespace*>(const_cast<const CodegenInput*>(this)->FindNamespace(name));
	}
};