Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Compiler/include/Luau/Compiler.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/ParseOptions.h"
5
#include "Luau/Location.h"
6
#include "Luau/StringUtils.h"
7
#include "Luau/Common.h"
8
9
namespace Luau
10
{
11
class AstNameTable;
12
struct ParseResult;
13
class BytecodeBuilder;
14
class BytecodeEncoder;
15
16
using CompileConstant = void*;
17
18
// return a type identifier for a global library member
19
// values are defined by 'enum LuauBytecodeType' in Bytecode.h
20
using LibraryMemberTypeCallback = int (*)(const char* library, const char* member);
21
22
// setup a value of a constant for a global library member
23
// use setCompileConstant*** set of functions for values
24
using LibraryMemberConstantCallback = void (*)(const char* library, const char* member, CompileConstant* constant);
25
26
// Note: this structure is duplicated in luacode.h, don't forget to change these in sync!
27
struct CompileOptions
28
{
29
// 0 - no optimization
30
// 1 - baseline optimization level that doesn't prevent debuggability
31
// 2 - includes optimizations that harm debuggability such as inlining
32
int optimizationLevel = 1;
33
34
// 0 - no debugging support
35
// 1 - line info & function names only; sufficient for backtraces
36
// 2 - full debug info with local & upvalue names; necessary for debugger
37
int debugLevel = 1;
38
39
// type information is used to guide native code generation decisions
40
// information includes testable typeArguments for function arguments, locals, upvalues and some temporaries
41
// 0 - generate for native modules
42
// 1 - generate for all modules
43
int typeInfoLevel = 0;
44
45
// 0 - no code coverage support
46
// 1 - statement coverage
47
// 2 - statement and expression coverage (verbose)
48
int coverageLevel = 0;
49
50
// alternative global builtin to construct vectors, in addition to default builtin 'vector.create'
51
const char* vectorLib = nullptr;
52
const char* vectorCtor = nullptr;
53
54
// alternative vector type name for type tables, in addition to default type 'vector'
55
const char* vectorType = nullptr;
56
57
// null-terminated array of globals that are mutable; disables the import optimization for fields accessed through these
58
const char* const* mutableGlobals = nullptr;
59
60
// null-terminated array of userdata typeArguments that will be included in the type information
61
const char* const* userdataTypes = nullptr;
62
63
// null-terminated array of globals which act as libraries and have members with known type and/or constant value
64
// when an import of one of these libraries is accessed, callbacks below will be called to receive that information
65
const char* const* librariesWithKnownMembers = nullptr;
66
LibraryMemberTypeCallback libraryMemberTypeCb = nullptr;
67
LibraryMemberConstantCallback libraryMemberConstantCb = nullptr;
68
69
// null-terminated array of library functions that should not be compiled into a built-in fastcall ("name" "lib.name")
70
const char* const* disabledBuiltins = nullptr;
71
};
72
73
class CompileError : public std::exception
74
{
75
public:
76
CompileError(const Location& location, std::string message);
77
78
~CompileError() throw() override;
79
80
const char* what() const throw() override;
81
82
const Location& getLocation() const;
83
84
static LUAU_NORETURN void raise(const Location& location, const char* format, ...) LUAU_PRINTF_ATTR(2, 3);
85
86
private:
87
Location location;
88
std::string message;
89
};
90
91
// compiles bytecode into bytecode builder using either a pre-parsed AST or parsing it from source; throws on errors
92
void compileOrThrow(BytecodeBuilder& bytecode, const ParseResult& parseResult, AstNameTable& names, const CompileOptions& options = {});
93
void compileOrThrow(BytecodeBuilder& bytecode, const std::string& source, const CompileOptions& options = {}, const ParseOptions& parseOptions = {});
94
95
// compiles bytecode into a bytecode blob, that either contains the valid bytecode or an encoded error that luau_load can decode
96
std::string compile(
97
const std::string& source,
98
const CompileOptions& options = {},
99
const ParseOptions& parseOptions = {},
100
BytecodeEncoder* encoder = nullptr
101
);
102
103
void setCompileConstantNil(CompileConstant* constant);
104
void setCompileConstantBoolean(CompileConstant* constant, bool b);
105
void setCompileConstantNumber(CompileConstant* constant, double n);
106
void setCompileConstantInteger64(CompileConstant* constant, int64_t l);
107
void setCompileConstantVector(CompileConstant* constant, float x, float y, float z, float w);
108
void setCompileConstantString(CompileConstant* constant, const char* s, size_t l);
109
110
} // namespace Luau
111
112