4 * (c)Copyright 1993-2014, Matthew Dillon, All Rights Reserved. See the
5 * COPYRIGHT file at the base of the distribution.
10 Type DynamicLValueType;
11 Type DynamicRValueType;
13 Type NumericType; /* generic numeric placeholder */
14 Type IntegralType; /* generic integral placeholder */
15 Type SIntegerType; /* generic signed integer placeholder */
16 Type UIntegerType; /* generic unsigned integer placeholder */
36 Type FloatType; /* generic float placeholder */
41 Type PointerType; /* generic pointer placeholder */
42 Type CCharType; /* const char */
43 Type StrType; /* const char * */
44 Type CharPtrType; /* char * */
45 Type CharPtrPtrType; /* char ** */
46 Type VoidPtrType; /* void * */
47 Type VoidRefType; /* void @ */
48 Type CVoidPtrType; /* const void * */
49 Type LVoidPtrType; /* lvalue void * */
51 Type LexRefType; /* run-time access class tie-ins */
60 typelist_t DynamicTypeList = RUNE_HEAD_INITIALIZER(DynamicTypeList);
61 typelist_t CompoundTypeList = RUNE_HEAD_INITIALIZER(CompoundTypeList);
62 typelist_t ArgsTypeList = RUNE_HEAD_INITIALIZER(ArgsTypeList);
63 typelist_t StorageTypeList = RUNE_HEAD_INITIALIZER(StorageTypeList);
65 static void initInternalClassType(Type *type, Declaration *d);
67 Type *BaseTypeAry[] = {
68 &DynamicLValueType, &DynamicRValueType, &NumericType,
69 &IntegralType, &SIntegerType, &UIntegerType,
70 &VoidType, &BoolType, &Int8Type, &UInt8Type,
71 &Int16Type, &UInt16Type, &Int32Type, &UInt32Type,
72 &Int64Type, &UInt64Type, &Int128Type, &UInt128Type,
74 &IntPtrType, &UIntPtrType, &OffType, &SizeType,
76 &FloatType, &Float32Type, &Float64Type, &Float128Type,
78 &PointerType, &CCharType, &StrType, &CharPtrType,
79 &CharPtrPtrType, &VoidPtrType, &VoidRefType, &CVoidPtrType,
82 &LexRefType, &ScopeType, &DeclarationType, &SemGroupType,
83 &PointerInfoType, &TypeType,
84 &FILLERTypeType, &FILLERDeclType,
89 initType(Type *type, typelist_t *list, int op)
92 RUNE_INIT(&type->ty_QList);
94 RUNE_INSERT_TAIL(list, type, ty_Node);
95 type->ty_SQList = list;
96 type->ty_Info.in_Type = type;
100 initQualType(Type *type, typelist_t *list, int op, int sqflags)
102 initType(type, list, op);
103 type->ty_SQFlags = sqflags;
107 initPtrType(Type *type, Type *ptrto, int sqflags)
109 initQualType(type, &ptrto->ty_QList, TY_PTRTO, sqflags);
110 type->ty_PtrType.et_Type = ptrto;
111 /*type->ty_Bytes = sizeof(PointerStor);*/
112 type->ty_AlignMask = POINTERSTOR_ALIGN;
116 initCPtrType(Type *type, Type *ptrto, int sqflags)
118 initQualType(type, &ptrto->ty_QList, TY_CPTRTO, sqflags);
119 type->ty_PtrType.et_Type = ptrto;
120 /*type->ty_Bytes = sizeof(void *);*/
121 type->ty_AlignMask = sizeof(void *) - 1;
125 initRefType(Type *type, Type *refto, int sqflags)
127 initQualType(type, &refto->ty_QList, TY_REFTO, sqflags);
128 type->ty_RefType.et_Type = refto;
129 /*type->ty_Bytes = sizeof(PointerStor);*/
130 type->ty_AlignMask = POINTERSTOR_ALIGN;
138 initQualType(&DynamicLValueType, &DynamicTypeList,
139 TY_DYNAMIC, SF_LVALUE);
140 initType(&DynamicRValueType, &DynamicTypeList, TY_DYNAMIC);
141 initType(&NumericType, NULL, TY_UNRESOLVED);
142 initType(&IntegralType, NULL, TY_UNRESOLVED);
143 initType(&SIntegerType, NULL, TY_UNRESOLVED);
144 initType(&UIntegerType, NULL, TY_UNRESOLVED);
145 initType(&FloatType, NULL, TY_UNRESOLVED);
146 initType(&PointerType, NULL, TY_UNRESOLVED);
147 PointerType.ty_AlignMask = POINTERSTOR_ALIGN;
149 initType(&VoidType, NULL, TY_UNRESOLVED);
150 initType(&BoolType, NULL, TY_UNRESOLVED);
151 initType(&Int8Type, NULL, TY_UNRESOLVED);
152 initType(&UInt8Type, NULL, TY_UNRESOLVED);
153 initType(&Int16Type, NULL, TY_UNRESOLVED);
154 initType(&UInt16Type, NULL, TY_UNRESOLVED);
155 initType(&Int32Type, NULL, TY_UNRESOLVED);
156 initType(&UInt32Type, NULL, TY_UNRESOLVED);
157 initType(&Int64Type, NULL, TY_UNRESOLVED);
158 initType(&UInt64Type, NULL, TY_UNRESOLVED);
160 initType(&IntPtrType, NULL, TY_UNRESOLVED);
161 initType(&UIntPtrType, NULL, TY_UNRESOLVED);
162 initType(&OffType, NULL, TY_UNRESOLVED);
163 initType(&SizeType, NULL, TY_UNRESOLVED);
165 initType(&Float32Type, NULL, TY_UNRESOLVED);
166 initType(&Float64Type, NULL, TY_UNRESOLVED);
167 initType(&Float128Type, NULL, TY_UNRESOLVED);
169 initQualType(&CCharType, NULL, TY_UNRESOLVED, SF_CONST);
170 initPtrType(&StrType, &CCharType, 0);
171 initPtrType(&CharPtrType, &UInt8Type, 0);
172 initPtrType(&CharPtrPtrType, &CharPtrType, 0);
173 initPtrType(&VoidPtrType, &VoidType, 0);
174 initRefType(&VoidRefType, &VoidType, 0);
175 initCPtrType(&CVoidPtrType, &VoidType, SF_CONST);
176 initPtrType(&LVoidPtrType, &VoidType, SF_LVALUE);
179 * Mark internal types (not all are bound to classes so it is
180 * easiest to just do it here). This will prevent the collapse
181 * code from trying to collapse our base types.
183 for (i = 0; BaseTypeAry[i]; ++i)
184 BaseTypeAry[i]->ty_Flags |= TF_ISINTERNAL;
186 StrTableAlloc("void", 4, SPECIAL_INTERNAL_VOID);
187 StrTableAlloc("bool", 4, SPECIAL_INTERNAL_BOOL);
188 StrTableAlloc("int8_t", 6, SPECIAL_INTERNAL_INT8);
189 StrTableAlloc("uint8_t", 7, SPECIAL_INTERNAL_UINT8);
190 StrTableAlloc("int16_t", 7, SPECIAL_INTERNAL_INT16);
191 StrTableAlloc("uint16_t", 8, SPECIAL_INTERNAL_UINT16);
192 StrTableAlloc("int32_t", 7, SPECIAL_INTERNAL_INT32);
193 StrTableAlloc("uint32_t", 8, SPECIAL_INTERNAL_UINT32);
194 StrTableAlloc("int64_t", 7, SPECIAL_INTERNAL_INT64);
195 StrTableAlloc("uint64_t", 8, SPECIAL_INTERNAL_UINT64);
196 StrTableAlloc("int128_t", 8, SPECIAL_INTERNAL_INT128);
197 StrTableAlloc("uint128_t", 9, SPECIAL_INTERNAL_UINT128);
199 StrTableAlloc("float32_t", 9, SPECIAL_INTERNAL_FLOAT32);
200 StrTableAlloc("float64_t", 9, SPECIAL_INTERNAL_FLOAT64);
201 StrTableAlloc("float128_t", 10, SPECIAL_INTERNAL_FLOAT128);
203 StrTableAlloc("intptr_t", 8, SPECIAL_INTERNAL_INTPTR);
204 StrTableAlloc("uintptr_t", 9, SPECIAL_INTERNAL_UINTPTR);
205 StrTableAlloc("size_t", 6, SPECIAL_INTERNAL_SIZE);
206 StrTableAlloc("off_t", 5, SPECIAL_INTERNAL_OFF);
208 StrTableAlloc("Float", 5, SPECIAL_INTERNAL_FLOAT);
209 StrTableAlloc("Pointer", 7, SPECIAL_INTERNAL_POINTER);
210 StrTableAlloc("Numeric", 7, SPECIAL_INTERNAL_NUMERIC);
211 StrTableAlloc("Integral", 8, SPECIAL_INTERNAL_INTEGRAL);
212 StrTableAlloc("SInteger", 8, SPECIAL_INTERNAL_SINTEGER);
213 StrTableAlloc("UInteger", 8, SPECIAL_INTERNAL_UINTEGER);
215 StrTableAlloc("LexRef", 6, SPECIAL_INTERNAL_LEXREF);
216 StrTableAlloc("Scope", 5, SPECIAL_INTERNAL_SCOPE);
217 StrTableAlloc("Declaration", 11, SPECIAL_INTERNAL_DECLARATION);
218 StrTableAlloc("SemGroup", 8, SPECIAL_INTERNAL_SEMGROUP);
219 StrTableAlloc("PointerInfo", 11, SPECIAL_INTERNAL_POINTERINFO);
220 StrTableAlloc("Type", 4, SPECIAL_INTERNAL_TYPE);
221 StrTableAlloc("FILLERType", 10, SPECIAL_INTERNAL_FILLERTYPE);
222 StrTableAlloc("FILLERDecl", 10, SPECIAL_INTERNAL_FILLERDECL);
224 StrTableAlloc("__count", 7, SPECIAL_COUNT);
225 StrTableAlloc("__data", 6, SPECIAL_DATA);
226 StrTableAlloc("__varcount", 10, SPECIAL_VAR_COUNT);
227 StrTableAlloc("__vardata", 9, SPECIAL_VAR_DATA);
228 StrTableAlloc("__typeid", 8, SPECIAL_TYPEID);
229 StrTableAlloc("__typestr", 9, SPECIAL_TYPESTR);
230 StrTableAlloc("NULL", 4, SPECIAL_NULL);
234 * Attach an internal class, creating a global summary type for it that
235 * allows our interpreter and code generator to make various assumptions.
238 InternalClassAttach(Parse *p __unused, int t, Declaration *d)
243 dassert_decl(d, d->d_Op == DOP_CLASS);
245 if ((s = StrTableSpecial(d->d_Id)) & SPECIALF_INTERNAL) {
247 case SPECIAL_INTERNAL_VOID:
250 case SPECIAL_INTERNAL_BOOL:
252 * Special flag helper (resolver sets TF_ISBOOL in
256 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISBOOL;
258 case SPECIAL_INTERNAL_INT8:
261 case SPECIAL_INTERNAL_UINT8:
264 case SPECIAL_INTERNAL_INT16:
267 case SPECIAL_INTERNAL_UINT16:
270 case SPECIAL_INTERNAL_INT32:
273 case SPECIAL_INTERNAL_UINT32:
276 case SPECIAL_INTERNAL_INT64:
279 case SPECIAL_INTERNAL_UINT64:
282 case SPECIAL_INTERNAL_INT128:
285 case SPECIAL_INTERNAL_UINT128:
286 itype = &UInt128Type;
289 case SPECIAL_INTERNAL_FLOAT:
291 * Special flag helper (resolver sets TF_ISFLOATING in
295 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISFLOATING;
297 case SPECIAL_INTERNAL_FLOAT32:
298 itype = &Float32Type;
300 case SPECIAL_INTERNAL_FLOAT64:
301 itype = &Float64Type;
303 case SPECIAL_INTERNAL_FLOAT128:
304 itype = &Float128Type;
307 case SPECIAL_INTERNAL_INTPTR:
310 case SPECIAL_INTERNAL_UINTPTR:
311 itype = &UIntPtrType;
313 case SPECIAL_INTERNAL_OFF:
316 case SPECIAL_INTERNAL_SIZE:
319 /* NOTE: There is no ssize_t in rune. size_t is signed */
321 case SPECIAL_INTERNAL_POINTER:
322 itype = &PointerType;
324 case SPECIAL_INTERNAL_NUMERIC:
325 itype = &NumericType;
327 case SPECIAL_INTERNAL_INTEGRAL:
328 itype = &IntegralType;
330 case SPECIAL_INTERNAL_SINTEGER:
332 * Special flag helper (resolver sets TF_ISINTEGER in
335 itype = &SIntegerType;
336 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISINTEGER;
338 case SPECIAL_INTERNAL_UINTEGER:
340 * Special flag helper (resolver sets TF_ISINTEGER
341 * and TF_ISUNSIGNED in the type)
343 itype = &UIntegerType;
344 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISUNSIGNED;
345 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISINTEGER;
348 case SPECIAL_INTERNAL_LEXREF:
351 case SPECIAL_INTERNAL_SCOPE:
354 case SPECIAL_INTERNAL_DECLARATION:
355 itype = &DeclarationType;
357 case SPECIAL_INTERNAL_SEMGROUP:
358 itype = &SemGroupType;
360 case SPECIAL_INTERNAL_POINTERINFO:
361 itype = &PointerInfoType;
363 case SPECIAL_INTERNAL_TYPE:
366 case SPECIAL_INTERNAL_FILLERTYPE:
367 itype = &FILLERTypeType;
369 case SPECIAL_INTERNAL_FILLERDECL:
370 itype = &FILLERDeclType;
373 dpanic("Unknown internal class: %s", d->d_Id);
377 initInternalClassType(itype, d);
380 * Fixup for const int8 pointers... we did not have
381 * a QList to put CCharType on until now. It will
382 * wind up on the SemGroup's sg_ClassList.
384 if (itype == &UInt8Type) {
385 TypeToQualType(itype, &CCharType,
386 itype->ty_SQFlags | SF_CONST, NULL);
393 * This is mostly deprecated except for official type aliases such as
397 InternalTypeAttach(Parse *p, int t, Declaration *d)
402 dassert_decl(d, d->d_Op == DOP_TYPEDEF);
404 if ((s = StrTableSpecial(d->d_Id)) & SPECIALF_INTERNAL) {
406 case SPECIAL_INTERNAL_VOID:
409 case SPECIAL_INTERNAL_BOOL:
412 case SPECIAL_INTERNAL_INT8:
415 case SPECIAL_INTERNAL_UINT8:
418 case SPECIAL_INTERNAL_INT16:
421 case SPECIAL_INTERNAL_UINT16:
424 case SPECIAL_INTERNAL_INT32:
427 case SPECIAL_INTERNAL_UINT32:
430 case SPECIAL_INTERNAL_INT64:
433 case SPECIAL_INTERNAL_UINT64:
437 case SPECIAL_INTERNAL_FLOAT32:
438 itype = &Float32Type;
440 case SPECIAL_INTERNAL_FLOAT64:
441 itype = &Float64Type;
443 case SPECIAL_INTERNAL_FLOAT128:
444 itype = &Float128Type;
447 case SPECIAL_INTERNAL_INTPTR:
450 case SPECIAL_INTERNAL_UINTPTR:
451 itype = &UIntPtrType;
453 case SPECIAL_INTERNAL_OFF:
456 case SPECIAL_INTERNAL_SIZE:
459 /* NOTE: There is no ssize_t in rune. size_t is signed */
461 case SPECIAL_INTERNAL_POINTER:
462 itype = &PointerType;
464 case SPECIAL_INTERNAL_NUMERIC:
465 itype = &NumericType;
467 case SPECIAL_INTERNAL_INTEGRAL:
468 itype = &IntegralType;
470 case SPECIAL_INTERNAL_SINTEGER:
471 itype = &SIntegerType;
473 case SPECIAL_INTERNAL_UINTEGER:
474 itype = &UIntegerType;
477 itype = InternalRegisteredTypeLookup(d->d_Id);
479 dpanic("Unknown internal type: %s", d->d_Id);
484 if (itype->ty_Op != TY_UNRESOLVED) {
485 t = LexError(&p->p_Token, TOK_ERR_DUPLICATE_ATTACH);
487 Type *ntype = d->d_TypedefDecl.ed_Type;
489 TypeToQualType(ntype, itype, ntype->ty_SQFlags, NULL);
492 t = LexError(&p->p_Token, TOK_ERR_UNRECOGNIZED_ATTACH);
498 AllocType(typelist_t *list, int op)
500 Type *type = zalloc(sizeof(Type));
502 initType(type, list, op);
507 * May be used to generate a varargs compound type, in which case the
508 * semgroup may already be resolved.
510 * We do no matching/caching at this time and callers assume that (for
511 * making adjustments) to the underlying sg at parse-time via
515 AllocCompoundType(SemGroup *sg)
519 type = AllocType(&CompoundTypeList, TY_COMPOUND);
520 type->ty_CompType.et_SemGroup = sg;
521 dassert((sg->sg_Flags & SGF_RESOLVED) == 0);
526 * XXX match the compound type(s)
529 AllocArgsType(SemGroup *sg)
533 type = AllocType(&ArgsTypeList, TY_ARGS);
534 type->ty_ArgsType.et_SemGroup = sg;
539 AllocStorageType(runesize_t bytes)
543 RUNE_FOREACH(type, &StorageTypeList, ty_Node) {
544 if (type->ty_Op == TY_STORAGE &&
545 type->ty_StorType.et_Bytes == bytes
550 type = AllocType(&StorageTypeList, TY_STORAGE);
551 type->ty_StorType.et_Bytes = bytes;
556 AllocUnresolvedType(SemGroup *isg, SemGroup *sg, string_t *ary, int eatAry)
560 dassert_semgrp(sg, ary != NULL);
562 RUNE_FOREACH(type, &sg->sg_ClassList, ty_Node) {
565 if (type->ty_Op != TY_UNRESOLVED)
567 if (type->ty_UnresType.et_ImportSemGroup != isg)
570 for (i = 0; ary[i]; ++i) {
571 if (ary[i] != type->ty_UnresType.et_DottedId[i])
574 if (ary[i] == NULL &&
575 type->ty_UnresType.et_DottedId[i] == NULL) {
581 type = AllocType((sg ? &sg->sg_ClassList : NULL), TY_UNRESOLVED);
582 type->ty_UnresType.et_DottedId = ary;
583 type->ty_UnresType.et_SemGroup = sg; /* may be NULL */
584 type->ty_UnresType.et_ImportSemGroup = isg; /* may be NULL */
589 * AllocClassType() - allocate a type representing a the semgroup which
590 * in turn represents (typically) a class.
593 AllocClassType(typelist_t *list, Type *super, SemGroup *sg, int visibility)
598 list = &sg->sg_ClassList;
600 RUNE_FOREACH(type, list, ty_Node) {
601 if (type->ty_Op == TY_CLASS &&
602 type->ty_ClassType.et_SemGroup == sg &&
603 type->ty_ClassType.et_Super == super &&
604 type->ty_Visibility == visibility
610 dassert(&sg->sg_ClassList == list);
611 type = AllocType(list, TY_CLASS);
612 type->ty_ClassType.et_SemGroup = sg;
613 type->ty_ClassType.et_Super = super;
614 type->ty_Visibility = visibility;
620 initInternalClassType(Type *type, Declaration *d)
622 SemGroup *sg = d->d_ClassDecl.ed_SemGroup;
624 initType(type, &sg->sg_ClassList, TY_CLASS);
625 RUNE_REMOVE(&sg->sg_ClassList, type, ty_Node);
626 RUNE_INSERT_HEAD(&sg->sg_ClassList, type, ty_Node);
627 type->ty_ClassType.et_SemGroup = d->d_ClassDecl.ed_SemGroup;
628 type->ty_ClassType.et_Super = d->d_ClassDecl.ed_Super;
629 type->ty_Visibility = d->d_ScopeFlags & SCOPE_ALL_VISIBLE;
630 type->ty_Flags |= TF_ISINTERNAL;
634 AllocImportType(typelist_t *list, SemGroup *sg, int visibility)
636 Type *type = AllocType(list, TY_IMPORT);
638 type->ty_ImportType.et_SemGroup = sg;
639 type->ty_Visibility = visibility;
644 * adjtype must be moved to type's QList because type is being modified
645 * such that that is where it is expected to be.
648 TypeAdjustQList(Type *type, Type *adjtype)
651 if (adjtype->ty_SQList)
652 RUNE_REMOVE(adjtype->ty_SQList, adjtype, ty_Node);
653 adjtype->ty_SQList = &type->ty_QList;
654 RUNE_INSERT_TAIL(adjtype->ty_SQList, adjtype, ty_Node);
660 * Adjust the type to be locked storage. Assumes that compound types
661 * have not been cached/collapsed (parse-time only).
664 TypeAdjustLockedStorage(Type *type)
669 if (type->ty_Op != TY_COMPOUND)
673 * Assume no matching collapse yet
675 sg = type->ty_CompType.et_SemGroup;
676 RUNE_FOREACH(d, &sg->sg_DeclList, d_Node) {
677 if (d->d_Op != DOP_GROUP_STORAGE)
679 d->d_ScopeFlags &= ~SCOPE_UNLOCKED;
685 TypeToQualType(Type *otype, Type *ntype, int sqFlags, Exp *exp)
690 * Combine with existing qualifiers, Shortcut if no changes made.
693 sqFlags == otype->ty_SQFlags &&
694 (exp == NULL || exp == otype->ty_AssExp)
700 * See if we already have a matching qualified type (only if storage
701 * for the new type is not being provided). Note: the provided storage
702 * has already been initType()d
705 RUNE_FOREACH(ntype, otype->ty_SQList, ty_Node) {
706 if (ntype->ty_Op == otype->ty_Op &&
707 ntype->ty_SQFlags == sqFlags &&
708 (exp == NULL || ntype->ty_AssExp == exp)
710 if (SameType(ntype, otype, sqFlags))
717 * Build a new qualified type and set its qualifiers, then duplicate
718 * appropriate sections of the old type.
720 * Default to the same SQList as otype.
723 ntype = AllocType(otype->ty_SQList, otype->ty_Op);
725 if (ntype->ty_SQList)
726 RUNE_REMOVE(ntype->ty_SQList, ntype, ty_Node);
727 ntype->ty_SQList = otype->ty_SQList;
728 RUNE_INSERT_TAIL(ntype->ty_SQList, ntype, ty_Node);
732 * Set the op and the expression. Unlike SQFlags, if exp is passed as
733 * NULL we inherit the old type's default.
735 * The DupExp() call here is special, see DupExp()'s handling of
738 * Normally DupExp() is called during resolution prior to ex_Decl
739 * being set. This is the one case where it may be called with
740 * ex_Decl already set.
742 * WARNING! We do not try to resolve the type here. Various resolve
743 * related flags in ty_Flags will be resolved later. This
744 * includes TF_ISUNSIGNED and other TF_* flags.
746 ntype->ty_Op = otype->ty_Op;
748 ntype->ty_AssExp = exp;
749 else if (otype->ty_AssExp)
750 ntype->ty_AssExp = SetDupExp(NULL, otype->ty_AssExp);
751 ntype->ty_SQFlags = sqFlags;
752 ntype->ty_Visibility = otype->ty_Visibility;
754 switch(otype->ty_Op) {
757 * When updating the class, alternative forms are collapsed
758 * into it's SemGroup->sg_ClassList and not into some
759 * potentially long recursive chain based on ty_QList.
761 sg = otype->ty_ClassType.et_SemGroup;
763 dassert(ntype->ty_SQList == &sg->sg_ClassList);
764 if (ntype->ty_ClassType.et_SemGroup != sg) {
765 ntype->ty_ClassType.et_SemGroup = sg;
767 ntype->ty_ClassType.et_Super = otype->ty_ClassType.et_Super;
770 ntype->ty_Visibility = otype->ty_Visibility;
771 ntype->ty_ImportType.et_SemGroup = otype->ty_ImportType.et_SemGroup;
774 ntype->ty_CPtrType.et_Type = otype->ty_CPtrType.et_Type;
777 ntype->ty_PtrType.et_Type = otype->ty_PtrType.et_Type;
780 ntype->ty_RefType.et_Type = otype->ty_RefType.et_Type;
784 * note: multiple type structures may share the same array size
785 * expression in simple qualified-type cases. YYY XXX bad bad.
787 ntype->ty_AryType.et_Type = otype->ty_AryType.et_Type;
788 ntype->ty_AryType.et_ArySize = otype->ty_AryType.et_ArySize;
789 ntype->ty_AryType.et_SemGroup = otype->ty_AryType.et_SemGroup;
790 ntype->ty_AryType.et_Count = otype->ty_AryType.et_Count;
793 ntype->ty_CompType.et_SemGroup = otype->ty_CompType.et_SemGroup;
796 ntype->ty_VarType.et_Type = otype->ty_VarType.et_Type;
797 ntype->ty_VarType.et_SemGroup = otype->ty_VarType.et_SemGroup;
800 ntype->ty_ArgsType.et_SemGroup = otype->ty_ArgsType.et_SemGroup;
803 ntype->ty_ProcType.et_ArgsType = otype->ty_ProcType.et_ArgsType;
804 ntype->ty_ProcType.et_RetType = otype->ty_ProcType.et_RetType;
805 ntype->ty_ProcType.et_ArgCount = otype->ty_ProcType.et_ArgCount;
806 dassert(ntype->ty_SQList ==
807 &otype->ty_ProcType.et_RetType->ty_QList);
810 ntype->ty_StorType.et_Bytes = otype->ty_StorType.et_Bytes;
814 * It is not legal to qualify a dynamic type other then to
815 * add or remove SF_LVALUE.
817 dpanic("Dynamic type cannot be qualified");
820 ntype->ty_UnresType.et_DottedId =
821 otype->ty_UnresType.et_DottedId;
822 ntype->ty_UnresType.et_SemGroup =
823 otype->ty_UnresType.et_SemGroup;
824 ntype->ty_UnresType.et_ImportSemGroup =
825 otype->ty_UnresType.et_ImportSemGroup;
828 dassert_type(otype, 0);
834 * Convert a return-type + argument-type into a procedure type. If
835 * adjret is non-zero the return-type is converted to locked storage
836 * (which is generally what we want).
838 * Match the procedure type(s) (after adjustment?)
841 TypeToProcType(Type *rtype, Type *atype, int adjret)
844 runesize_t count = 0;
848 dassert_type(atype, atype->ty_Op == TY_ARGS);
851 rtype = TypeAdjustLockedStorage(rtype);
853 sg = atype->ty_CompType.et_SemGroup;
855 RUNE_FOREACH(d, &sg->sg_DeclList, d_Node) {
858 RUNE_FOREACH(type, &rtype->ty_QList, ty_Node) {
859 if (type->ty_Op == TY_PROC) {
860 if (type->ty_ProcType.et_ArgsType == atype &&
861 type->ty_ProcType.et_RetType == rtype &&
862 type->ty_ProcType.et_ArgCount == count
868 type = AllocType(&rtype->ty_QList, TY_PROC);
869 type->ty_ProcType.et_ArgsType = AllocArgsType(sg);
870 type->ty_ProcType.et_RetType = rtype;
871 type->ty_ProcType.et_ArgCount = count;
876 * Convert type to pointer-to-type
879 TypeToPtrType(Type *otype)
883 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
884 if (type->ty_Op == TY_PTRTO)
887 type = AllocType(&otype->ty_QList, TY_PTRTO);
888 type->ty_PtrType.et_Type = otype;
893 * Convert type to pointer-to-type
896 TypeToCPtrType(Type *otype)
900 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
901 if (type->ty_Op == TY_CPTRTO)
904 type = AllocType(&otype->ty_QList, TY_CPTRTO);
905 type->ty_CPtrType.et_Type = otype;
910 * Convert type to ref-to-type
912 * A reference type is similar to a pointer type except that the
913 * resolver is not able to entirely know what it is pointing to.
914 * The reference type is a superclass, but the actual type is
915 * stored in the run-time structure.
918 TypeToRefType(Type *otype)
922 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
923 if (type->ty_Op == TY_REFTO)
926 type = AllocType(&otype->ty_QList, TY_REFTO);
927 type->ty_RefType.et_Type = otype;
932 TypeToAryType(Type *otype, Exp *exp, SemGroup *sg)
937 * XXX handle constant expression optimization for QList
938 * XXX handle qualifiers
940 type = AllocType(&otype->ty_QList, TY_ARYOF);
941 type->ty_AryType.et_ArySize = exp;
942 type->ty_AryType.et_Type = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
943 type->ty_AryType.et_SemGroup = sg;
944 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
951 TypeToRunTimeAryType(Type *otype, int count)
954 Type *t2 = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
956 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
957 if (type->ty_Op == TY_ARYOF &&
958 type->ty_AryType.et_Type == t2 &&
959 type->ty_AryType.et_Count == count &&
961 (otype->ty_SQFlags & SF_MASK_ARY_INHERIT)
966 type = AllocType(&otype->ty_QList, TY_ARYOF);
967 type->ty_AryType.et_Count = count;
968 type->ty_AryType.et_Type = t2;
969 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
976 TypeToVarType(Type *otype, SemGroup *sg)
980 /*dassert(sg->sg_Flags & SGF_RESOLVED);*/
981 /*dassert(otype->ty_Flags & TF_RESOLVED);*/
983 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
984 if (type->ty_Op == TY_VAR &&
985 type->ty_VarType.et_Type == otype &&
986 type->ty_VarType.et_SemGroup == sg
988 puts("SG2"); /* YYY */
992 type = AllocType(&otype->ty_QList, TY_VAR);
993 type->ty_VarType.et_Type = otype;
994 type->ty_VarType.et_SemGroup = sg;
996 /* XXX doesn't work for var-args */
997 if (sg->sg_Flags & SGF_RESOLVED) {
998 type->ty_Flags |= TF_RESOLVED;
999 type->ty_Bytes = sg->sg_Bytes;
1000 type->ty_AlignMask = sg->sg_AlignMask;
1007 * ChangeType() - given pointer, C pointer, or array of something,
1008 * return 'op' of something instead.
1011 ChangeType(Type *type, int op)
1013 switch(type->ty_Op) {
1017 type = TypeToCPtrType(type->ty_PtrType.et_Type);
1020 type = TypeToAryType(type->ty_PtrType.et_Type,
1024 dpanic("Illegal type convesion (A)");
1030 type = TypeToPtrType(type->ty_CPtrType.et_Type);
1033 type = TypeToAryType(type->ty_CPtrType.et_Type,
1037 dpanic("Illegal type convesion (B)");
1043 type = TypeToPtrType(type->ty_AryType.et_Type);
1046 type = TypeToCPtrType(type->ty_AryType.et_Type);
1049 dpanic("Illegal type convesion (C)");
1053 dpanic("Illegal type convesion (D)");
1059 * BaseType() - return base type
1061 * Traverse the type to locate the base type. Store the base type
1062 * in *ptype and return the SemGroup, or return NULL if the base type
1063 * does not have a SemGroup.
1066 BaseType(Type **ptype)
1068 Type *type = *ptype;
1071 switch(type->ty_Op) {
1073 type = type->ty_CPtrType.et_Type;
1076 type = type->ty_PtrType.et_Type;
1079 type = type->ty_RefType.et_Type;
1082 type = type->ty_AryType.et_Type;
1090 switch(type->ty_Op) {
1092 return(type->ty_ClassType.et_SemGroup);
1094 return(type->ty_CompType.et_SemGroup);
1096 return(type->ty_ArgsType.et_SemGroup);
1104 dassert_type(type, 0);
1105 return(NULL); /* avoid compiler complaints */
1110 * DupType() - create a duplicate of a type, possibly in a new SemGroup.
1112 * This code is used when duplicating procedures and other elements
1113 * when merging a superclass into a subclass.
1115 * If sg is NULL, stype is simply returned. The case is used when we
1116 * try to duplciate an expression with DupExp()... in that case we
1117 * want to dup the expression tree but use the same types.
1120 DupType(SemGroup *sg, Type *stype)
1128 * XXX type may be resolved if it is part of a varargs dup
1132 (stype->ty_Flags & (TF_RESOLVED|TF_RESOLVING)) == 0);
1135 switch(stype->ty_Op) {
1138 * This only occurs because the resolver has resolved an
1139 * unresolved type on the original SemGroup. We duplicate
1140 * that on the new SemGroup.
1142 type = AllocClassType(&sg->sg_ClassList,
1143 stype->ty_ClassType.et_Super,
1144 stype->ty_ClassType.et_SemGroup,
1145 stype->ty_Visibility);
1152 type = TypeToCPtrType(DupType(sg, stype->ty_CPtrType.et_Type));
1155 type = TypeToPtrType(DupType(sg, stype->ty_PtrType.et_Type));
1158 type = TypeToRefType(DupType(sg, stype->ty_RefType.et_Type));
1161 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1162 SetDupExp(sg, stype->ty_AryType.et_ArySize),
1163 stype->ty_AryType.et_SemGroup);
1164 type->ty_AryType.et_Count = stype->ty_AryType.et_Count;
1167 type = TypeToVarType(DupType(sg, stype->ty_VarType.et_Type),
1168 DupSemGroup(sg, NULL,
1169 stype->ty_VarType.et_SemGroup, 1));
1172 type = AllocCompoundType(
1173 DupSemGroup(sg, NULL,
1174 stype->ty_CompType.et_SemGroup, 1));
1178 * At the moment we always formally duplicate the arguments
1179 * so we can modify them for methods below.
1181 type = AllocArgsType(
1182 DupSemGroup(sg, NULL,
1183 stype->ty_CompType.et_SemGroup, 1));
1186 type = DupType(sg, stype->ty_ProcType.et_RetType);
1187 type = TypeToProcType(type,
1188 DupType(sg, stype->ty_ProcType.et_ArgsType),
1192 * If this is a method procedure, we have to change the
1193 * first argument to point at our new subclass. It was
1194 * previously pointing at our superclass. XXX the
1195 * programmer can override the argument. If it isn't a
1196 * reference we have to assert.
1198 if (sg->sg_Stmt->st_Op != ST_Class) {
1200 * XXX probably an inlined procedure, the type is
1201 * already correct. Need an assertrion here.
1203 } else if (stype->ty_SQFlags & SF_METHOD) {
1204 SemGroup *asg = type->ty_ProcType.et_ArgsType->ty_ArgsType.et_SemGroup;
1205 Declaration *d = RUNE_FIRST(&asg->sg_DeclList);
1206 Type *thisType = d->d_StorDecl.ed_Type;
1208 dassert_decl(d, d->d_Id == String_This &&
1209 d->d_Op == DOP_ARGS_STORAGE);
1210 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1211 if (thisType->ty_Op == TY_CLASS) {
1212 /* XXX sg_ClassList? right sg? */
1213 /* XXX correct visibility? */
1214 if (d->d_Search == NULL) {
1215 d->d_Search = d->d_StorDecl.ed_Type->
1216 ty_ClassType.et_SemGroup;
1218 d->d_StorDecl.ed_Type =
1219 AllocClassType(&sg->sg_ClassList,
1220 sg->sg_Stmt->st_ClassStmt.es_Super,
1221 sg->sg_Stmt->st_MyGroup,
1224 dassert_decl(d, thisType->ty_Op == TY_REFTO);
1226 } else if (stype->ty_SQFlags & SF_GMETHOD) {
1230 asg = type->ty_ProcType.et_ArgsType->
1231 ty_ArgsType.et_SemGroup;
1232 d = RUNE_FIRST(&asg->sg_DeclList);
1234 dassert_decl(d, d->d_Id == String_This &&
1235 d->d_Op == DOP_TYPEDEF);
1236 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1237 dassert_decl(d, d->d_TypedefDecl.ed_Type->ty_Op ==
1239 /* XXX sg_ClassList? right sg? */
1240 /* XXX correct visibility? */
1241 if (d->d_Search == NULL) {
1242 d->d_Search = d->d_TypedefDecl.ed_Type->
1243 ty_ClassType.et_SemGroup;
1245 d->d_TypedefDecl.ed_Type =
1246 AllocClassType(&sg->sg_ClassList,
1247 sg->sg_Stmt->st_ClassStmt.es_Super,
1248 sg->sg_Stmt->st_MyGroup,
1257 * e.g. so elements in a superclass will see refined elements
1258 * in the subclass. Note that the original import semgroup
1259 * is left intact so the semantic search mechanism uses it
1260 * when the base sg (typically a subclass) fails.
1262 type = AllocUnresolvedType(
1263 stype->ty_UnresType.et_ImportSemGroup,
1265 stype->ty_UnresType.et_DottedId,
1269 dassert_type(stype, 0);
1272 if (type != stype) {
1273 type->ty_Flags = stype->ty_Flags &
1274 ~(TF_ISINTERNAL | TF_RESOLVING | TF_RESOLVED |
1275 TF_ALIGNRESOLVED | TF_TMPRESOLVED);
1276 type->ty_Bytes = stype->ty_Bytes;
1277 type->ty_AlignMask = stype->ty_AlignMask;
1278 type->ty_Visibility = stype->ty_Visibility;
1279 type->ty_DynamicVector = stype->ty_DynamicVector;
1281 if (stype->ty_AssExp || stype->ty_SQFlags != type->ty_SQFlags) {
1282 type = TypeToQualType(type, NULL,
1284 SetDupExp(sg, stype->ty_AssExp));
1290 typereglist_t TypeRegList = RUNE_HEAD_INITIALIZER(TypeRegList);
1293 InternalRegisterType(const char *str, const char *linkname, Type *type)
1295 static int Special = SPECIALF_REGISTERED|SPECIALF_INTERNAL|1;
1298 dassert(Special & SPECIALF_MASK);
1299 tr = zalloc(sizeof(TypeRegNode));
1300 tr->tr_Id = StrTableAlloc(str, strlen(str), Special++);
1302 tr->tr_LinkName = linkname;
1303 RUNE_INSERT_TAIL(&TypeRegList, tr, tr_Node);
1307 InternalRegisteredTypeLookup(string_t id)
1311 RUNE_FOREACH(tr, &TypeRegList, tr_Node) {
1312 if (tr->tr_Id == id)
1313 return(tr->tr_Type);