MCPcopy Create free account
hub / github.com/WheretIB/nullc / GetArrayType

Method GetArrayType

NULLC/CodeInfo.cpp:38–113  ·  view source on GitHub ↗

Function returns type that is an array for passed type

Source from the content-addressed store, hash-verified

36
37// Function returns type that is an array for passed type
38TypeInfo* CodeInfo::GetArrayType(TypeInfo* type, unsigned int sizeInArgument)
39{
40 int arrSize = -1;
41 bool unFixed = false;
42 // If size wasn't passed through argument, then it can be found in previous node
43 if(sizeInArgument == 0)
44 {
45 // It must be a constant type
46 if(nodeList.back()->nodeType == typeNodeNumber)
47 {
48 TypeInfo *aType = nodeList.back()->typeInfo;
49 NodeZeroOP* zOP = nodeList.back();
50 if(aType->type != TypeInfo::TYPE_COMPLEX && aType->type != TypeInfo::TYPE_VOID)
51 {
52 arrSize = static_cast<NodeNumber*>(zOP)->GetInteger();
53 }else if(aType == typeVoid){ // If number type is void, then array with explicit type must be created
54 arrSize = -1;
55 unFixed = true;
56 }else{
57 ThrowError(lastKnownStartPos, "ERROR: unknown type of constant number node '%s'", aType->name);
58 }
59 nodeList.pop_back();
60 }else{
61 ThrowError(lastKnownStartPos, "ERROR: array size must be a constant expression");
62 }
63 }else{
64 arrSize = sizeInArgument;
65 if(arrSize == -1)
66 unFixed = true;
67 }
68
69 if(!unFixed && arrSize < 1)
70 ThrowError(lastKnownStartPos, "ERROR: array size can't be negative or zero");
71
72 if(unFixed && type->unsizedType)
73 return type->unsizedType;
74
75 if(!unFixed && type->hasFinalizer)
76 ThrowError(lastKnownStartPos, "ERROR: class '%s' implements 'finalize' so only an unsized array type can be created", type->GetFullTypeName());
77 if(!unFixed && !type->hasFinished)
78 ThrowError(lastKnownStartPos, "ERROR: type '%s' is not fully defined. You can use '%s ref' or '%s[]' at this point", type->GetFullTypeName(), type->GetFullTypeName(), type->GetFullTypeName());
79
80 // Search type list for the type that we need
81 TypeInfo *target = type->arrayType;
82 while(target && target->arrSize != (unsigned int)arrSize)
83 target = target->nextArrayType;
84 if(target)
85 return target;
86
87 // If not found, create new type
88 TypeInfo* newInfo = new TypeInfo(typeInfo.size(), NULL, 0, type->arrLevel + 1, arrSize, type, TypeInfo::TYPE_COMPLEX);
89
90 if(unFixed)
91 {
92 newInfo->size = NULLC_PTR_SIZE;
93 newInfo->AddMemberVariable("size", typeInt);
94 type->unsizedType = newInfo;
95 }else{

Callers

nothing calls this directly

Calls 7

ThrowErrorFunction · 0.85
GetIntegerMethod · 0.80
GetFullTypeNameMethod · 0.80
AddMemberVariableMethod · 0.80
pop_backMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected