tan  0.0.1
intrinsic.cpp
1 #include "ast/intrinsic.h"
2 #include "ast/decl.h"
3 #include "ast/type.h"
4 
5 namespace tanlang {
6 
7 umap<str, IntrinsicType> Intrinsic::INTRINSIC_NAME_TO_TYPES{
8  {"asm", IntrinsicType::ASM },
9  {"swap", IntrinsicType::SWAP },
10  {"memset", IntrinsicType::MEMSET },
11  {"memcpy", IntrinsicType::MEMCPY },
12  {"range", IntrinsicType::RANGE },
13  {COMP_PRINT_NAME, IntrinsicType::COMP_PRINT },
14  {"file", IntrinsicType::FILENAME },
15  {"line", IntrinsicType::LINENO },
16  {"define", IntrinsicType::DEFINE },
17  {"sizeof", IntrinsicType::SIZE_OF },
18  {"offsetof", IntrinsicType::OFFSET_OF },
19  {"isa", IntrinsicType::ISA },
20  {"alignof", IntrinsicType::ALIGN_OF },
21  {"min_of", IntrinsicType::MIN_OF },
22  {"max_of", IntrinsicType::MAX_OF },
23  {"is_unsigned", IntrinsicType::IS_UNSIGNED },
24  {"unlikely", IntrinsicType::UNLIKELY },
25  {"likely", IntrinsicType::LIKELY },
26  {"expect", IntrinsicType::EXPECT },
27  {"noop", IntrinsicType::NOOP },
28  {"get_decl", IntrinsicType::GET_DECL },
29  {TEST_COMP_ERROR_NAME, IntrinsicType::TEST_COMP_ERROR},
30  {ABORT_NAME, IntrinsicType::ABORT },
31  {"stack_trace", IntrinsicType::STACK_TRACE },
32  {STACK_TRACE_FUNCTION_REAL_NAME, IntrinsicType::STACK_TRACE }
33 };
34 
36  vector<FunctionDecl *> ret{};
37  auto *src = new TokenizedSourceFile("__intrinsics__", {});
38 
39  // compprint
40  ret.push_back(FunctionDecl::Create(src,
41  COMP_PRINT_NAME,
42  Type::GetFunctionType(Type::GetVoidType(), {Type::GetStringType()}),
43  true,
44  false,
45  nullptr,
46  true));
47 
48  // abort
49  ret.push_back(FunctionDecl::Create(
50  src, ABORT_NAME, Type::GetFunctionType(Type::GetVoidType(), {}), true, false, nullptr, true));
51 
52  return ret;
53 }
54 
55 Intrinsic *Intrinsic::Create(TokenizedSourceFile *src) { return new Intrinsic(src); }
56 
57 Intrinsic::Intrinsic(TokenizedSourceFile *src) : Expr(ASTNodeType::INTRINSIC, src, 0) {}
58 
59 IntrinsicType Intrinsic::get_intrinsic_type() const { return _intrinsic_type; }
60 
61 void Intrinsic::set_intrinsic_type(IntrinsicType intrinsic_type) { _intrinsic_type = intrinsic_type; }
62 
63 ASTBase *Intrinsic::get_sub() const { return _sub; }
64 
65 void Intrinsic::set_sub(ASTBase *sub) { _sub = sub; }
66 
67 vector<ASTBase *> Intrinsic::get_children() const {
68  if (_sub) {
69  return _sub->get_children();
70  }
71  return {};
72 }
73 
75  if (_intrinsic_type == IntrinsicType::TEST_COMP_ERROR) {
76  return "}";
77  }
78 
79  return ";";
80 }
81 
82 } // namespace tanlang
virtual vector< ASTBase * > get_children() const
Get a ordered list of child nodes.
Definition: ast_base.cpp:51
A generic representation of Intrinsic variables/functions.
Definition: intrinsic.h:55
static umap< str, IntrinsicType > INTRINSIC_NAME_TO_TYPES
A mapping from intrinsic names to IntrinsicType.
Definition: intrinsic.h:65
static vector< FunctionDecl * > GetIntrinsicFunctionDeclarations()
Generate a list of intrinsics function prototypes/declarations, such as @abort
Definition: intrinsic.cpp:35
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: intrinsic.cpp:67
str terminal_token() const override
Which terminal token is expected immediately after this node.
Definition: intrinsic.cpp:74
Different from SourceFile, TokenizedSourceFile manages the tokenized text of a source file.