Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/frontend/cxx/frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
}

if (toolchainId == "darwin" || toolchainId == "macos") {
auto macosToolchain = std::make_unique<MacOSToolchain>(preprocessor);
std::string host;
#ifdef __aarch64__
host = "aarch64";
#elif __x86_64__
// on macOS we default to aarch64, since it is the most common
std::string host = "aarch64";

#if __x86_64__
host = "x86_64";
#endif
macosToolchain->setArch(cli.getSingle("-arch").value_or(host));
toolchain = std::move(macosToolchain);

toolchain = std::make_unique<MacOSToolchain>(
preprocessor, cli.getSingle("-arch").value_or(host));

} else if (toolchainId == "wasm32") {
auto wasmToolchain = std::make_unique<Wasm32WasiToolchain>(preprocessor);
Expand Down Expand Up @@ -219,17 +219,25 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {

toolchain = std::move(wasmToolchain);
} else if (toolchainId == "linux") {
std::string host;
// on linux we default to x86_64, unless the host is aarch64
std::string host = "x86_64";

#ifdef __aarch64__
host = "aarch64";
#elif __x86_64__
host = "x86_64";
#endif

std::string arch = cli.getSingle("-arch").value_or(host);
toolchain = std::make_unique<GCCLinuxToolchain>(preprocessor, arch);
toolchain = std::make_unique<GCCLinuxToolchain>(
preprocessor, cli.getSingle("-arch").value_or(host));
} else if (toolchainId == "windows") {
auto windowsToolchain = std::make_unique<WindowsToolchain>(preprocessor);
// on linux we default to x86_64, unless the host is aarch64
std::string host = "x86_64";

#ifdef __aarch64__
host = "aarch64";
#endif

auto windowsToolchain = std::make_unique<WindowsToolchain>(
preprocessor, cli.getSingle("-arch").value_or(host));

if (auto paths = cli.get("-vctoolsdir"); !paths.empty()) {
windowsToolchain->setVctoolsdir(paths.back());
Expand Down
37 changes: 24 additions & 13 deletions src/lsp/cxx/lsp/cxx_document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct CxxDocument::Private {
};

void CxxDocument::Private::configure() {
auto preprocesor = unit.preprocessor();
auto preprocessor = unit.preprocessor();

auto toolchainId = cli.getSingle("-toolchain");

Expand All @@ -102,9 +102,15 @@ void CxxDocument::Private::configure() {
}

if (toolchainId == "darwin" || toolchainId == "macos") {
toolchain = std::make_unique<MacOSToolchain>(preprocesor);
std::string host = "aarch64";
#ifdef __x86_64__
host = "x86_64";
#endif

toolchain = std::make_unique<MacOSToolchain>(
preprocessor, cli.getSingle("-arch").value_or(host));
} else if (toolchainId == "wasm32") {
auto wasmToolchain = std::make_unique<Wasm32WasiToolchain>(preprocesor);
auto wasmToolchain = std::make_unique<Wasm32WasiToolchain>(preprocessor);

fs::path app_dir;

Expand All @@ -130,17 +136,22 @@ void CxxDocument::Private::configure() {

toolchain = std::move(wasmToolchain);
} else if (toolchainId == "linux") {
std::string host;
std::string host = "x86_64";
#ifdef __aarch64__
host = "aarch64";
#elif __x86_64__
host = "x86_64";
#endif

std::string arch = cli.getSingle("-arch").value_or(host);
toolchain = std::make_unique<GCCLinuxToolchain>(preprocesor, arch);
toolchain = std::make_unique<GCCLinuxToolchain>(
preprocessor, cli.getSingle("-arch").value_or(host));

} else if (toolchainId == "windows") {
auto windowsToolchain = std::make_unique<WindowsToolchain>(preprocesor);
std::string host = "x86_64";
#ifdef __aarch64__
host = "aarch64";
#endif

auto windowsToolchain = std::make_unique<WindowsToolchain>(
preprocessor, cli.getSingle("-arch").value_or(host));

if (auto paths = cli.get("-vctoolsdir"); !paths.empty()) {
windowsToolchain->setVctoolsdir(paths.back());
Expand Down Expand Up @@ -168,21 +179,21 @@ void CxxDocument::Private::configure() {
}

for (const auto& path : cli.get("-I")) {
preprocesor->addSystemIncludePath(path);
preprocessor->addSystemIncludePath(path);
}

for (const auto& macro : cli.get("-D")) {
auto sep = macro.find_first_of("=");

if (sep == std::string::npos) {
preprocesor->defineMacro(macro, "1");
preprocessor->defineMacro(macro, "1");
} else {
preprocesor->defineMacro(macro.substr(0, sep), macro.substr(sep + 1));
preprocessor->defineMacro(macro.substr(0, sep), macro.substr(sep + 1));
}
}

for (const auto& macro : cli.get("-U")) {
preprocesor->undefMacro(macro);
preprocessor->undefMacro(macro);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/parser/cxx/gcc_linux_toolchain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// SOFTWARE.

#include <cxx/gcc_linux_toolchain.h>
#include <cxx/memory_layout.h>
#include <cxx/preprocessor.h>
#include <cxx/private/path.h>

Expand All @@ -29,6 +30,14 @@ namespace cxx {
GCCLinuxToolchain::GCCLinuxToolchain(Preprocessor* preprocessor,
std::string arch)
: Toolchain(preprocessor), arch_(std::move(arch)) {
if (arch_ == "aarch64") {
memoryLayout()->setSizeOfLongDouble(8);
} else if (arch_ == "x86_64") {
memoryLayout()->setSizeOfLongDouble(16);
} else {
cxx_runtime_error(std::format("Unsupported architecture: {}", arch_));
}

for (int version : {15, 14, 13, 12, 11, 10, 9}) {
const auto path = fs::path(
std::format("/usr/lib/gcc/{}-linux-gnu/{}/include", arch_, version));
Expand Down
13 changes: 11 additions & 2 deletions src/parser/cxx/macos_toolchain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
// SOFTWARE.

#include <cxx/macos_toolchain.h>
#include <cxx/memory_layout.h>
#include <cxx/preprocessor.h>
#include <cxx/private/path.h>

#include <format>

namespace cxx {

MacOSToolchain::MacOSToolchain(Preprocessor* preprocessor)
: Toolchain(preprocessor) {
MacOSToolchain::MacOSToolchain(Preprocessor* preprocessor, std::string arch)
: Toolchain(preprocessor), arch_(std::move(arch)) {
std::string xcodeContentsBasePath = "/Applications/Xcode.app/Contents";

platformPath_ = std::format(
Expand All @@ -38,6 +39,14 @@ MacOSToolchain::MacOSToolchain(Preprocessor* preprocessor)
toolchainPath_ =
std::format("{}/Developer/Toolchains/XcodeDefault.xctoolchain",
xcodeContentsBasePath);

if (arch_ == "aarch64") {
memoryLayout()->setSizeOfLongDouble(8);
} else if (arch_ == "x86_64") {
memoryLayout()->setSizeOfLongDouble(16);
} else {
cxx_runtime_error(std::format("Unsupported architecture: {}", arch_));
}
}

void MacOSToolchain::addSystemIncludePaths() {
Expand Down
8 changes: 3 additions & 5 deletions src/parser/cxx/macos_toolchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ namespace cxx {

class MacOSToolchain final : public Toolchain {
public:
using Toolchain::Toolchain;

explicit MacOSToolchain(Preprocessor* preprocessor);
explicit MacOSToolchain(Preprocessor* preprocessor,
std::string arch = "aarch64");

[[nodiscard]] auto arch() const -> std::string { return arch_; }
void setArch(std::string arch) { arch_ = std::move(arch); }

void addSystemIncludePaths() override;
void addSystemCppIncludePaths() override;
Expand All @@ -42,7 +40,7 @@ class MacOSToolchain final : public Toolchain {
private:
std::string platformPath_;
std::string toolchainPath_;
std::string arch_{"aarch64"};
std::string arch_;
};

} // namespace cxx
51 changes: 28 additions & 23 deletions src/parser/cxx/windows_toolchain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <cxx/windows_toolchain.h>

// cxx
#include <cxx/memory_layout.h>
#include <cxx/preprocessor.h>
#include <cxx/private/path.h>

Expand All @@ -29,6 +30,15 @@

namespace cxx {

WindowsToolchain::WindowsToolchain(Preprocessor *preprocessor,
std::string arch = "x86_64")
: Toolchain(preprocessor), arch_(std::move(arch)) {
memoryLayout()->setSizeOfLong(4);
memoryLayout()->setSizeOfLongLong(8);
memoryLayout()->setSizeOfLongDouble(8);
memoryLayout()->setSizeOfPointer(8);
}

void WindowsToolchain::setVctoolsdir(std::string path) {
vctoolsdir_ = std::move(path);
}
Expand Down Expand Up @@ -72,28 +82,6 @@ void WindowsToolchain::addSystemIncludePaths() {
void WindowsToolchain::addSystemCppIncludePaths() {}

void WindowsToolchain::addPredefinedMacros() {
// clang-format off
defineMacro("__cplusplus", "202101L");
defineMacro("_WIN32", "1");
defineMacro("_WIN64", "1");
defineMacro("_MT", "1");
defineMacro("_M_AMD64", "100");
defineMacro("_M_X64", "100");
defineMacro("_MSC_BUILD", "1");
defineMacro("_MSC_EXTENSIONS", "1");
defineMacro("_MSC_FULL_VER", "193000000");
defineMacro("_MSC_VER", "1930");
defineMacro("_MSVC_LANG", "201705L");
defineMacro("_CPPRTTI", "1");
defineMacro("_CPPUNWIND", "1");
defineMacro("_WCHAR_T_DEFINED", "1");
defineMacro("__BOOL_DEFINED", "1");

defineMacro("__int8", "char");
defineMacro("__int16", "short");
defineMacro("__int32", "int");
defineMacro("__int64", "long long");

defineMacro("__pragma(a)", "");
defineMacro("__declspec(a)", "");
defineMacro("__cdecl", "");
Expand All @@ -105,7 +93,24 @@ void WindowsToolchain::addPredefinedMacros() {
defineMacro("__unaligned", "");
defineMacro("_Pragma(a)", "");

// clang-format on
addCommonMacros();
addCommonWindowsMacros();

if (language() == LanguageKind::kCXX) {
addCommonCxx26Macros();
addWindowsCxx26Macros();
} else {
addCommonC23Macros();
addWindowsC23Macros();
}

if (arch_ == "aarch64") {
addWindowsAArch64Macros();
} else if (arch_ == "x86_64") {
addWindowsX86_64Macros();
} else {
cxx_runtime_error(std::format("Unsupported architecture: {}", arch_));
}
}

} // namespace cxx
3 changes: 2 additions & 1 deletion src/parser/cxx/windows_toolchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace cxx {

class WindowsToolchain final : public Toolchain {
public:
using Toolchain::Toolchain;
explicit WindowsToolchain(Preprocessor* preprocessor, std::string arch);

void setVctoolsdir(std::string path);
void setWinsdkdir(std::string path);
Expand All @@ -40,6 +40,7 @@ class WindowsToolchain final : public Toolchain {
void addPredefinedMacros() override;

private:
std::string arch_;
std::string vctoolsdir_;
std::string winsdkdir_;
std::string winsdkversion_;
Expand Down