tan  0.0.1
default_value.cpp
1 #include "ast/default_value.h"
2 #include "ast/type.h"
3 #include "ast/decl.h"
4 
5 using namespace tanlang;
6 
7 Literal *DefaultValue::CreateTypeDefaultValueLiteral(TokenizedSourceFile *src, Type *type) {
8  Literal *ret = nullptr;
9 
10  if (type->is_pointer()) {
11  ret = Literal::CreateNullPointerLiteral(src, pcast<PointerType>(type)->get_pointee());
12 
13  } else if (type->is_array()) {
14  ArrayType *at = pcast<ArrayType>(type);
15  Type *element_type = at->get_element_type();
16  size_t size = (size_t)at->array_size();
17 
18  vector<Literal *> elements(size);
19  for (uint32_t i = 0; i < size; ++i) {
20  elements[i] = DefaultValue::CreateTypeDefaultValueLiteral(src, element_type);
21  }
22 
23  ret = Literal::CreateArrayLiteral(src, element_type, elements);
24 
25  } else if (type->is_string()) {
26  ret = Literal::CreateStringLiteral(src, "");
27 
28  } else if (type->is_struct()) {
29  // TODO: IMPLEMENT THIS
30 
31  } else if (type->is_function()) {
32  // TODO: IMPLEMENT THIS
33 
34  } else if (type->is_float()) {
35  ret = Literal::CreateFloatLiteral(src, 0, (size_t)type->get_size_bits());
36 
37  } else if (type->is_int()) {
38  ret = Literal::CreateIntegerLiteral(src, 0, (size_t)type->get_size_bits(), type->is_unsigned());
39 
40  } else if (type->is_bool()) {
41  ret = Literal::CreateBoolLiteral(src, false);
42 
43  } else if (type->is_char()) {
44  ret = Literal::CreateCharLiteral(src, '\0');
45  }
46 
47  TAN_ASSERT(ret);
48  return ret;
49 }
Different from SourceFile, TokenizedSourceFile manages the tokenized text of a source file.
Type is immutable once created. The exception is StructType. Its information is updated in multiple s...
Definition: type.h:22