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;
97 type->ty_Info.in_Refs = PINFO_STATIC;
101 initQualType(Type *type, typelist_t *list, int op, int sqflags)
103 initType(type, list, op);
104 type->ty_SQFlags = sqflags;
108 initPtrType(Type *type, Type *ptrto, int sqflags)
110 initQualType(type, &ptrto->ty_QList, TY_PTRTO, sqflags);
111 type->ty_PtrType.et_Type = ptrto;
112 /*type->ty_Bytes = sizeof(PointerStor);*/
113 type->ty_AlignMask = POINTERSTOR_ALIGN;
117 initCPtrType(Type *type, Type *ptrto, int sqflags)
119 initQualType(type, &ptrto->ty_QList, TY_CPTRTO, sqflags);
120 type->ty_PtrType.et_Type = ptrto;
121 /*type->ty_Bytes = sizeof(void *);*/
122 type->ty_AlignMask = sizeof(void *) - 1;
126 initRefType(Type *type, Type *refto, int sqflags)
128 initQualType(type, &refto->ty_QList, TY_REFTO, sqflags);
129 type->ty_RefType.et_Type = refto;
130 /*type->ty_Bytes = sizeof(PointerStor);*/
131 type->ty_AlignMask = POINTERSTOR_ALIGN;
139 initQualType(&DynamicLValueType, &DynamicTypeList,
140 TY_DYNAMIC, SF_LVALUE);
141 initType(&DynamicRValueType, &DynamicTypeList, TY_DYNAMIC);
142 initType(&NumericType, NULL, TY_UNRESOLVED);
143 initType(&IntegralType, NULL, TY_UNRESOLVED);
144 initType(&SIntegerType, NULL, TY_UNRESOLVED);
145 initType(&UIntegerType, NULL, TY_UNRESOLVED);
146 initType(&FloatType, NULL, TY_UNRESOLVED);
147 initType(&PointerType, NULL, TY_UNRESOLVED);
148 PointerType.ty_AlignMask = POINTERSTOR_ALIGN;
150 initType(&VoidType, NULL, TY_UNRESOLVED);
151 initType(&BoolType, NULL, TY_UNRESOLVED);
152 initType(&Int8Type, NULL, TY_UNRESOLVED);
153 initType(&UInt8Type, NULL, TY_UNRESOLVED);
154 initType(&Int16Type, NULL, TY_UNRESOLVED);
155 initType(&UInt16Type, NULL, TY_UNRESOLVED);
156 initType(&Int32Type, NULL, TY_UNRESOLVED);
157 initType(&UInt32Type, NULL, TY_UNRESOLVED);
158 initType(&Int64Type, NULL, TY_UNRESOLVED);
159 initType(&UInt64Type, NULL, TY_UNRESOLVED);
161 initType(&IntPtrType, NULL, TY_UNRESOLVED);
162 initType(&UIntPtrType, NULL, TY_UNRESOLVED);
163 initType(&OffType, NULL, TY_UNRESOLVED);
164 initType(&SizeType, NULL, TY_UNRESOLVED);
166 initType(&Float32Type, NULL, TY_UNRESOLVED);
167 initType(&Float64Type, NULL, TY_UNRESOLVED);
168 initType(&Float128Type, NULL, TY_UNRESOLVED);
170 initQualType(&CCharType, NULL, TY_UNRESOLVED, SF_CONST);
171 initPtrType(&StrType, &CCharType, 0);
172 initPtrType(&CharPtrType, &UInt8Type, 0);
173 initPtrType(&CharPtrPtrType, &CharPtrType, 0);
174 initPtrType(&VoidPtrType, &VoidType, 0);
175 initRefType(&VoidRefType, &VoidType, 0);
176 initCPtrType(&CVoidPtrType, &VoidType, SF_CONST);
177 initPtrType(&LVoidPtrType, &VoidType, SF_LVALUE);
180 * Mark internal types (not all are bound to classes so it is
181 * easiest to just do it here). This will prevent the collapse
182 * code from trying to collapse our base types.
184 for (i = 0; BaseTypeAry[i]; ++i)
185 BaseTypeAry[i]->ty_Flags |= TF_ISINTERNAL;
187 StrTableAlloc("void", 4, SPECIAL_INTERNAL_VOID);
188 StrTableAlloc("bool", 4, SPECIAL_INTERNAL_BOOL);
189 StrTableAlloc("int8_t", 6, SPECIAL_INTERNAL_INT8);
190 StrTableAlloc("uint8_t", 7, SPECIAL_INTERNAL_UINT8);
191 StrTableAlloc("int16_t", 7, SPECIAL_INTERNAL_INT16);
192 StrTableAlloc("uint16_t", 8, SPECIAL_INTERNAL_UINT16);
193 StrTableAlloc("int32_t", 7, SPECIAL_INTERNAL_INT32);
194 StrTableAlloc("uint32_t", 8, SPECIAL_INTERNAL_UINT32);
195 StrTableAlloc("int64_t", 7, SPECIAL_INTERNAL_INT64);
196 StrTableAlloc("uint64_t", 8, SPECIAL_INTERNAL_UINT64);
197 StrTableAlloc("int128_t", 8, SPECIAL_INTERNAL_INT128);
198 StrTableAlloc("uint128_t", 9, SPECIAL_INTERNAL_UINT128);
200 StrTableAlloc("float32_t", 9, SPECIAL_INTERNAL_FLOAT32);
201 StrTableAlloc("float64_t", 9, SPECIAL_INTERNAL_FLOAT64);
202 StrTableAlloc("float128_t", 10, SPECIAL_INTERNAL_FLOAT128);
204 StrTableAlloc("intptr_t", 8, SPECIAL_INTERNAL_INTPTR);
205 StrTableAlloc("uintptr_t", 9, SPECIAL_INTERNAL_UINTPTR);
206 StrTableAlloc("size_t", 6, SPECIAL_INTERNAL_SIZE);
207 StrTableAlloc("off_t", 5, SPECIAL_INTERNAL_OFF);
209 StrTableAlloc("Float", 5, SPECIAL_INTERNAL_FLOAT);
210 StrTableAlloc("Pointer", 7, SPECIAL_INTERNAL_POINTER);
211 StrTableAlloc("Numeric", 7, SPECIAL_INTERNAL_NUMERIC);
212 StrTableAlloc("Integral", 8, SPECIAL_INTERNAL_INTEGRAL);
213 StrTableAlloc("SInteger", 8, SPECIAL_INTERNAL_SINTEGER);
214 StrTableAlloc("UInteger", 8, SPECIAL_INTERNAL_UINTEGER);
216 StrTableAlloc("LexRef", 6, SPECIAL_INTERNAL_LEXREF);
217 StrTableAlloc("Scope", 5, SPECIAL_INTERNAL_SCOPE);
218 StrTableAlloc("Declaration", 11, SPECIAL_INTERNAL_DECLARATION);
219 StrTableAlloc("SemGroup", 8, SPECIAL_INTERNAL_SEMGROUP);
220 StrTableAlloc("PointerInfo", 11, SPECIAL_INTERNAL_POINTERINFO);
221 StrTableAlloc("Type", 4, SPECIAL_INTERNAL_TYPE);
222 StrTableAlloc("FILLERType", 10, SPECIAL_INTERNAL_FILLERTYPE);
223 StrTableAlloc("FILLERDecl", 10, SPECIAL_INTERNAL_FILLERDECL);
225 StrTableAlloc("__count", 7, SPECIAL_COUNT);
226 StrTableAlloc("__data", 6, SPECIAL_DATA);
227 StrTableAlloc("__varcount", 10, SPECIAL_VAR_COUNT);
228 StrTableAlloc("__vardata", 9, SPECIAL_VAR_DATA);
229 StrTableAlloc("__typeid", 8, SPECIAL_TYPEID);
230 StrTableAlloc("__typestr", 9, SPECIAL_TYPESTR);
231 StrTableAlloc("NULL", 4, SPECIAL_NULL);
235 * Attach an internal class, creating a global summary type for it that
236 * allows our interpreter and code generator to make various assumptions.
239 InternalClassAttach(Parse *p __unused, int t, Declaration *d)
244 dassert_decl(d, d->d_Op == DOP_CLASS);
246 if ((s = StrTableSpecial(d->d_Id)) & SPECIALF_INTERNAL) {
248 case SPECIAL_INTERNAL_VOID:
251 case SPECIAL_INTERNAL_BOOL:
253 * Special flag helper (resolver sets TF_ISBOOL in
257 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISBOOL;
259 case SPECIAL_INTERNAL_INT8:
262 case SPECIAL_INTERNAL_UINT8:
265 case SPECIAL_INTERNAL_INT16:
268 case SPECIAL_INTERNAL_UINT16:
271 case SPECIAL_INTERNAL_INT32:
274 case SPECIAL_INTERNAL_UINT32:
277 case SPECIAL_INTERNAL_INT64:
280 case SPECIAL_INTERNAL_UINT64:
283 case SPECIAL_INTERNAL_INT128:
286 case SPECIAL_INTERNAL_UINT128:
287 itype = &UInt128Type;
290 case SPECIAL_INTERNAL_FLOAT:
292 * Special flag helper (resolver sets TF_ISFLOATING in
296 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISFLOATING;
298 case SPECIAL_INTERNAL_FLOAT32:
299 itype = &Float32Type;
301 case SPECIAL_INTERNAL_FLOAT64:
302 itype = &Float64Type;
304 case SPECIAL_INTERNAL_FLOAT128:
305 itype = &Float128Type;
308 case SPECIAL_INTERNAL_INTPTR:
311 case SPECIAL_INTERNAL_UINTPTR:
312 itype = &UIntPtrType;
314 case SPECIAL_INTERNAL_OFF:
317 case SPECIAL_INTERNAL_SIZE:
320 /* NOTE: There is no ssize_t in rune. size_t is signed */
322 case SPECIAL_INTERNAL_POINTER:
323 itype = &PointerType;
325 case SPECIAL_INTERNAL_NUMERIC:
326 itype = &NumericType;
328 case SPECIAL_INTERNAL_INTEGRAL:
329 itype = &IntegralType;
331 case SPECIAL_INTERNAL_SINTEGER:
333 * Special flag helper (resolver sets TF_ISINTEGER in
336 itype = &SIntegerType;
337 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISINTEGER;
339 case SPECIAL_INTERNAL_UINTEGER:
341 * Special flag helper (resolver sets TF_ISINTEGER
342 * and TF_ISUNSIGNED in the type)
344 itype = &UIntegerType;
345 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISUNSIGNED;
346 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISINTEGER;
349 case SPECIAL_INTERNAL_LEXREF:
352 case SPECIAL_INTERNAL_SCOPE:
355 case SPECIAL_INTERNAL_DECLARATION:
356 itype = &DeclarationType;
358 case SPECIAL_INTERNAL_SEMGROUP:
359 itype = &SemGroupType;
361 case SPECIAL_INTERNAL_POINTERINFO:
362 itype = &PointerInfoType;
364 case SPECIAL_INTERNAL_TYPE:
367 case SPECIAL_INTERNAL_FILLERTYPE:
368 itype = &FILLERTypeType;
370 case SPECIAL_INTERNAL_FILLERDECL:
371 itype = &FILLERDeclType;
374 dpanic("Unknown internal class: %s", d->d_Id);
378 initInternalClassType(itype, d);
381 * Fixup for const int8 pointers... we did not have
382 * a QList to put CCharType on until now. It will
383 * wind up on the SemGroup's sg_ClassList.
385 if (itype == &UInt8Type) {
386 TypeToQualType(itype, &CCharType,
387 itype->ty_SQFlags | SF_CONST, NULL);
394 * This is mostly deprecated except for official type aliases such as
398 InternalTypeAttach(Parse *p, int t, Declaration *d)
403 dassert_decl(d, d->d_Op == DOP_TYPEDEF);
405 if ((s = StrTableSpecial(d->d_Id)) & SPECIALF_INTERNAL) {
407 case SPECIAL_INTERNAL_VOID:
410 case SPECIAL_INTERNAL_BOOL:
413 case SPECIAL_INTERNAL_INT8:
416 case SPECIAL_INTERNAL_UINT8:
419 case SPECIAL_INTERNAL_INT16:
422 case SPECIAL_INTERNAL_UINT16:
425 case SPECIAL_INTERNAL_INT32:
428 case SPECIAL_INTERNAL_UINT32:
431 case SPECIAL_INTERNAL_INT64:
434 case SPECIAL_INTERNAL_UINT64:
438 case SPECIAL_INTERNAL_FLOAT32:
439 itype = &Float32Type;
441 case SPECIAL_INTERNAL_FLOAT64:
442 itype = &Float64Type;
444 case SPECIAL_INTERNAL_FLOAT128:
445 itype = &Float128Type;
448 case SPECIAL_INTERNAL_INTPTR:
451 case SPECIAL_INTERNAL_UINTPTR:
452 itype = &UIntPtrType;
454 case SPECIAL_INTERNAL_OFF:
457 case SPECIAL_INTERNAL_SIZE:
460 /* NOTE: There is no ssize_t in rune. size_t is signed */
462 case SPECIAL_INTERNAL_POINTER:
463 itype = &PointerType;
465 case SPECIAL_INTERNAL_NUMERIC:
466 itype = &NumericType;
468 case SPECIAL_INTERNAL_INTEGRAL:
469 itype = &IntegralType;
471 case SPECIAL_INTERNAL_SINTEGER:
472 itype = &SIntegerType;
474 case SPECIAL_INTERNAL_UINTEGER:
475 itype = &UIntegerType;
478 itype = InternalRegisteredTypeLookup(d->d_Id);
480 dpanic("Unknown internal type: %s", d->d_Id);
485 if (itype->ty_Op != TY_UNRESOLVED) {
486 t = LexError(&p->p_Token, TOK_ERR_DUPLICATE_ATTACH);
488 Type *ntype = d->d_TypedefDecl.ed_Type;
490 TypeToQualType(ntype, itype, ntype->ty_SQFlags, NULL);
493 t = LexError(&p->p_Token, TOK_ERR_UNRECOGNIZED_ATTACH);
499 AllocType(typelist_t *list, int op)
501 Type *type = zalloc(sizeof(Type));
503 initType(type, list, op);
508 * May be used to generate a varargs compound type, in which case the
509 * semgroup may already be resolved.
511 * We do no matching/caching at this time and callers assume that (for
512 * making adjustments) to the underlying sg at parse-time via
516 AllocCompoundType(SemGroup *sg)
520 type = AllocType(&CompoundTypeList, TY_COMPOUND);
521 type->ty_CompType.et_SemGroup = sg;
522 dassert((sg->sg_Flags & SGF_RESOLVED) == 0);
527 * XXX match the compound type(s)
530 AllocArgsType(SemGroup *sg)
534 type = AllocType(&ArgsTypeList, TY_ARGS);
535 type->ty_ArgsType.et_SemGroup = sg;
540 AllocStorageType(runesize_t bytes)
544 RUNE_FOREACH(type, &StorageTypeList, ty_Node) {
545 if (type->ty_Op == TY_STORAGE &&
546 type->ty_StorType.et_Bytes == bytes
551 type = AllocType(&StorageTypeList, TY_STORAGE);
552 type->ty_StorType.et_Bytes = bytes;
557 AllocUnresolvedType(SemGroup *isg, SemGroup *sg, string_t *ary, int eatAry)
561 dassert_semgrp(sg, ary != NULL);
563 RUNE_FOREACH(type, &sg->sg_ClassList, ty_Node) {
566 if (type->ty_Op != TY_UNRESOLVED)
568 if (type->ty_UnresType.et_ImportSemGroup != isg)
571 for (i = 0; ary[i]; ++i) {
572 if (ary[i] != type->ty_UnresType.et_DottedId[i])
575 if (ary[i] == NULL &&
576 type->ty_UnresType.et_DottedId[i] == NULL) {
582 type = AllocType((sg ? &sg->sg_ClassList : NULL), TY_UNRESOLVED);
583 type->ty_UnresType.et_DottedId = ary;
584 type->ty_UnresType.et_SemGroup = sg; /* may be NULL */
585 type->ty_UnresType.et_ImportSemGroup = isg; /* may be NULL */
590 * AllocClassType() - allocate a type representing a the semgroup which
591 * in turn represents (typically) a class.
594 AllocClassType(typelist_t *list, Type *super, SemGroup *sg, int visibility)
599 list = &sg->sg_ClassList;
601 RUNE_FOREACH(type, list, ty_Node) {
602 if (type->ty_Op == TY_CLASS &&
603 type->ty_ClassType.et_SemGroup == sg &&
604 type->ty_ClassType.et_Super == super &&
605 type->ty_Visibility == visibility
611 dassert(&sg->sg_ClassList == list);
612 type = AllocType(list, TY_CLASS);
613 type->ty_ClassType.et_SemGroup = sg;
614 type->ty_ClassType.et_Super = super;
615 type->ty_Visibility = visibility;
621 initInternalClassType(Type *type, Declaration *d)
623 SemGroup *sg = d->d_ClassDecl.ed_SemGroup;
625 initType(type, &sg->sg_ClassList, TY_CLASS);
626 RUNE_REMOVE(&sg->sg_ClassList, type, ty_Node);
627 RUNE_INSERT_HEAD(&sg->sg_ClassList, type, ty_Node);
628 type->ty_ClassType.et_SemGroup = d->d_ClassDecl.ed_SemGroup;
629 type->ty_ClassType.et_Super = d->d_ClassDecl.ed_Super;
630 type->ty_Visibility = d->d_ScopeFlags & SCOPE_ALL_VISIBLE;
631 type->ty_Flags |= TF_ISINTERNAL;
635 AllocImportType(typelist_t *list, SemGroup *sg, int visibility)
637 Type *type = AllocType(list, TY_IMPORT);
639 type->ty_ImportType.et_SemGroup = sg;
640 type->ty_Visibility = visibility;
645 * adjtype must be moved to type's QList because type is being modified
646 * such that that is where it is expected to be.
649 TypeAdjustQList(Type *type, Type *adjtype)
652 if (adjtype->ty_SQList)
653 RUNE_REMOVE(adjtype->ty_SQList, adjtype, ty_Node);
654 adjtype->ty_SQList = &type->ty_QList;
655 RUNE_INSERT_TAIL(adjtype->ty_SQList, adjtype, ty_Node);
661 * Compound types inherit locking modes if not specifically
665 TypeFixupInheritedFlags(Type *type, int rqflags)
670 if (type->ty_Op != TY_COMPOUND)
674 * Assume no matching collapse yet
676 sg = type->ty_CompType.et_SemGroup;
677 RUNE_FOREACH(d, &sg->sg_DeclList, d_Node) {
678 if (d->d_Op != DOP_GROUP_STORAGE)
680 if ((d->d_ScopeFlags & SCOPE_LOCKING_MASK) == 0) {
681 if (rqflags & SF_UNTRACKED)
682 d->d_ScopeFlags |= SCOPE_UNTRACKED;
683 if (rqflags & SF_UNLOCKED)
684 d->d_ScopeFlags |= SCOPE_UNLOCKED;
685 if (rqflags & SF_SOFT)
686 d->d_ScopeFlags |= SCOPE_SOFT;
687 if (rqflags & SF_HARD)
688 d->d_ScopeFlags |= SCOPE_HARD;
695 TypeToQualType(Type *otype, Type *ntype, int sqFlags, Exp *exp)
700 * Combine with existing qualifiers, Shortcut if no changes made.
703 sqFlags == otype->ty_SQFlags &&
704 (exp == NULL || exp == otype->ty_AssExp)
710 * See if we already have a matching qualified type (only if storage
711 * for the new type is not being provided). Note: the provided storage
712 * has already been initType()d
715 RUNE_FOREACH(ntype, otype->ty_SQList, ty_Node) {
716 if (ntype->ty_Op == otype->ty_Op &&
717 ntype->ty_SQFlags == sqFlags &&
718 (exp == NULL || ntype->ty_AssExp == exp)
720 if (SameType(ntype, otype, sqFlags))
727 * Build a new qualified type and set its qualifiers, then duplicate
728 * appropriate sections of the old type.
730 * Default to the same SQList as otype.
733 ntype = AllocType(otype->ty_SQList, otype->ty_Op);
735 if (ntype->ty_SQList)
736 RUNE_REMOVE(ntype->ty_SQList, ntype, ty_Node);
737 ntype->ty_SQList = otype->ty_SQList;
738 RUNE_INSERT_TAIL(ntype->ty_SQList, ntype, ty_Node);
742 * Set the op and the expression. Unlike SQFlags, if exp is passed as
743 * NULL we inherit the old type's default.
745 * The DupExp() call here is special, see DupExp()'s handling of
748 * Normally DupExp() is called during resolution prior to ex_Decl
749 * being set. This is the one case where it may be called with
750 * ex_Decl already set.
752 * WARNING! We do not try to resolve the type here. Various resolve
753 * related flags in ty_Flags will be resolved later. This
754 * includes TF_ISUNSIGNED and other TF_* flags.
756 ntype->ty_Op = otype->ty_Op;
758 ntype->ty_AssExp = exp;
759 else if (otype->ty_AssExp)
760 ntype->ty_AssExp = SetDupExp(NULL, otype->ty_AssExp);
761 ntype->ty_SQFlags = sqFlags;
762 ntype->ty_Visibility = otype->ty_Visibility;
764 switch(otype->ty_Op) {
767 * When updating the class, alternative forms are collapsed
768 * into it's SemGroup->sg_ClassList and not into some
769 * potentially long recursive chain based on ty_QList.
771 sg = otype->ty_ClassType.et_SemGroup;
773 dassert(ntype->ty_SQList == &sg->sg_ClassList);
774 if (ntype->ty_ClassType.et_SemGroup != sg) {
775 ntype->ty_ClassType.et_SemGroup = sg;
777 ntype->ty_ClassType.et_Super = otype->ty_ClassType.et_Super;
780 ntype->ty_Visibility = otype->ty_Visibility;
781 ntype->ty_ImportType.et_SemGroup = otype->ty_ImportType.et_SemGroup;
784 ntype->ty_CPtrType.et_Type = otype->ty_CPtrType.et_Type;
787 ntype->ty_PtrType.et_Type = otype->ty_PtrType.et_Type;
790 ntype->ty_RefType.et_Type = otype->ty_RefType.et_Type;
794 * note: multiple type structures may share the same array size
795 * expression in simple qualified-type cases. YYY XXX bad bad.
797 ntype->ty_AryType.et_Type = otype->ty_AryType.et_Type;
798 ntype->ty_AryType.et_ArySize = otype->ty_AryType.et_ArySize;
799 ntype->ty_AryType.et_SemGroup = otype->ty_AryType.et_SemGroup;
800 ntype->ty_AryType.et_Count = otype->ty_AryType.et_Count;
803 ntype->ty_CompType.et_SemGroup = otype->ty_CompType.et_SemGroup;
806 ntype->ty_VarType.et_Type = otype->ty_VarType.et_Type;
807 ntype->ty_VarType.et_SemGroup = otype->ty_VarType.et_SemGroup;
810 ntype->ty_ArgsType.et_SemGroup = otype->ty_ArgsType.et_SemGroup;
813 ntype->ty_ProcType.et_ArgsType = otype->ty_ProcType.et_ArgsType;
814 ntype->ty_ProcType.et_RetType = otype->ty_ProcType.et_RetType;
815 ntype->ty_ProcType.et_ArgCount = otype->ty_ProcType.et_ArgCount;
816 dassert(ntype->ty_SQList ==
817 &otype->ty_ProcType.et_RetType->ty_QList);
820 ntype->ty_StorType.et_Bytes = otype->ty_StorType.et_Bytes;
824 * It is not legal to qualify a dynamic type other then to
825 * add or remove SF_LVALUE.
827 dpanic("Dynamic type cannot be qualified");
830 ntype->ty_UnresType.et_DottedId =
831 otype->ty_UnresType.et_DottedId;
832 ntype->ty_UnresType.et_SemGroup =
833 otype->ty_UnresType.et_SemGroup;
834 ntype->ty_UnresType.et_ImportSemGroup =
835 otype->ty_UnresType.et_ImportSemGroup;
838 dassert_type(otype, 0);
844 * Convert a return-type + argument-type into a procedure type. If
845 * adjret is non-zero the return-type is converted to locked storage
846 * (which is generally what we want).
848 * Match the procedure type(s) (after adjustment?)
851 TypeToProcType(Type *rtype, Type *atype, int adjret)
854 runesize_t count = 0;
858 dassert_type(atype, atype->ty_Op == TY_ARGS);
861 rtype = TypeFixupInheritedFlags(rtype, rtype->ty_SQFlags);
863 sg = atype->ty_CompType.et_SemGroup;
865 RUNE_FOREACH(d, &sg->sg_DeclList, d_Node) {
868 RUNE_FOREACH(type, &rtype->ty_QList, ty_Node) {
869 if (type->ty_Op == TY_PROC) {
870 if (type->ty_ProcType.et_ArgsType == atype &&
871 type->ty_ProcType.et_RetType == rtype &&
872 type->ty_ProcType.et_ArgCount == count
878 type = AllocType(&rtype->ty_QList, TY_PROC);
879 type->ty_ProcType.et_ArgsType = AllocArgsType(sg);
880 type->ty_ProcType.et_RetType = rtype;
881 type->ty_ProcType.et_ArgCount = count;
886 * Convert type to pointer-to-type
889 TypeToPtrType(Type *otype)
893 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
894 if (type->ty_Op == TY_PTRTO)
897 type = AllocType(&otype->ty_QList, TY_PTRTO);
898 type->ty_PtrType.et_Type = otype;
903 * Convert type to pointer-to-type
906 TypeToCPtrType(Type *otype)
910 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
911 if (type->ty_Op == TY_CPTRTO)
914 type = AllocType(&otype->ty_QList, TY_CPTRTO);
915 type->ty_CPtrType.et_Type = otype;
920 * Convert type to ref-to-type
922 * A reference type is similar to a pointer type except that the
923 * resolver is not able to entirely know what it is pointing to.
924 * The reference type is a superclass, but the actual type is
925 * stored in the run-time structure.
928 TypeToRefType(Type *otype)
932 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
933 if (type->ty_Op == TY_REFTO)
936 type = AllocType(&otype->ty_QList, TY_REFTO);
937 type->ty_RefType.et_Type = otype;
942 TypeToAryType(Type *otype, Exp *exp, SemGroup *sg)
947 * XXX handle constant expression optimization for QList
948 * XXX handle qualifiers
950 type = AllocType(&otype->ty_QList, TY_ARYOF);
951 type->ty_AryType.et_ArySize = exp;
952 type->ty_AryType.et_Type = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
953 type->ty_AryType.et_SemGroup = sg;
954 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
961 TypeToRunTimeAryType(Type *otype, int count)
964 Type *t2 = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
966 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
967 if (type->ty_Op == TY_ARYOF &&
968 type->ty_AryType.et_Type == t2 &&
969 type->ty_AryType.et_Count == count &&
971 (otype->ty_SQFlags & SF_MASK_ARY_INHERIT)
976 type = AllocType(&otype->ty_QList, TY_ARYOF);
977 type->ty_AryType.et_Count = count;
978 type->ty_AryType.et_Type = t2;
979 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
986 TypeToVarType(Type *otype, SemGroup *sg)
990 /*dassert(sg->sg_Flags & SGF_RESOLVED);*/
991 /*dassert(otype->ty_Flags & TF_RESOLVED);*/
993 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
994 if (type->ty_Op == TY_VAR &&
995 type->ty_VarType.et_Type == otype &&
996 type->ty_VarType.et_SemGroup == sg
998 puts("SG2"); /* YYY */
1002 type = AllocType(&otype->ty_QList, TY_VAR);
1003 type->ty_VarType.et_Type = otype;
1004 type->ty_VarType.et_SemGroup = sg;
1006 /* XXX doesn't work for var-args */
1007 if (sg->sg_Flags & SGF_RESOLVED) {
1008 type->ty_Flags |= TF_RESOLVED;
1009 type->ty_Bytes = sg->sg_Bytes;
1010 type->ty_AlignMask = sg->sg_AlignMask;
1017 * ChangeType() - given pointer, C pointer, or array of something,
1018 * return 'op' of something instead.
1021 ChangeType(Type *type, int op)
1023 switch(type->ty_Op) {
1027 type = TypeToCPtrType(type->ty_PtrType.et_Type);
1030 type = TypeToAryType(type->ty_PtrType.et_Type,
1034 dpanic("Illegal type convesion (A)");
1040 type = TypeToPtrType(type->ty_CPtrType.et_Type);
1043 type = TypeToAryType(type->ty_CPtrType.et_Type,
1047 dpanic("Illegal type convesion (B)");
1053 type = TypeToPtrType(type->ty_AryType.et_Type);
1056 type = TypeToCPtrType(type->ty_AryType.et_Type);
1059 dpanic("Illegal type convesion (C)");
1063 dpanic("Illegal type convesion (D)");
1069 * BaseType() - return base type
1071 * Traverse the type to locate the base type. Store the base type
1072 * in *ptype and return the SemGroup, or return NULL if the base type
1073 * does not have a SemGroup.
1076 BaseType(Type **ptype)
1078 Type *type = *ptype;
1081 switch(type->ty_Op) {
1083 type = type->ty_CPtrType.et_Type;
1086 type = type->ty_PtrType.et_Type;
1089 type = type->ty_RefType.et_Type;
1092 type = type->ty_AryType.et_Type;
1100 switch(type->ty_Op) {
1102 return(type->ty_ClassType.et_SemGroup);
1104 return(type->ty_CompType.et_SemGroup);
1106 return(type->ty_ArgsType.et_SemGroup);
1114 dassert_type(type, 0);
1115 return(NULL); /* avoid compiler complaints */
1120 * DupType() - create a duplicate of a type, possibly in a new SemGroup.
1122 * This code is used when duplicating procedures and other elements
1123 * when merging a superclass into a subclass.
1125 * If sg is NULL, stype is simply returned. The case is used when we
1126 * try to duplciate an expression with DupExp()... in that case we
1127 * want to dup the expression tree but use the same types.
1130 DupType(SemGroup *sg, Type *stype)
1138 * XXX type may be resolved if it is part of a varargs dup
1142 (stype->ty_Flags & (TF_RESOLVED|TF_RESOLVING)) == 0);
1145 switch(stype->ty_Op) {
1148 * This only occurs because the resolver has resolved an
1149 * unresolved type on the original SemGroup. We duplicate
1150 * that on the new SemGroup.
1152 type = AllocClassType(&sg->sg_ClassList,
1153 stype->ty_ClassType.et_Super,
1154 stype->ty_ClassType.et_SemGroup,
1155 stype->ty_Visibility);
1162 type = TypeToCPtrType(DupType(sg, stype->ty_CPtrType.et_Type));
1165 type = TypeToPtrType(DupType(sg, stype->ty_PtrType.et_Type));
1168 type = TypeToRefType(DupType(sg, stype->ty_RefType.et_Type));
1171 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1172 SetDupExp(sg, stype->ty_AryType.et_ArySize),
1173 stype->ty_AryType.et_SemGroup);
1174 type->ty_AryType.et_Count = stype->ty_AryType.et_Count;
1177 type = TypeToVarType(DupType(sg, stype->ty_VarType.et_Type),
1178 DupSemGroup(sg, NULL,
1179 stype->ty_VarType.et_SemGroup, 1));
1182 type = AllocCompoundType(
1183 DupSemGroup(sg, NULL,
1184 stype->ty_CompType.et_SemGroup, 1));
1188 * At the moment we always formally duplicate the arguments
1189 * so we can modify them for methods below.
1191 type = AllocArgsType(
1192 DupSemGroup(sg, NULL,
1193 stype->ty_CompType.et_SemGroup, 1));
1196 type = DupType(sg, stype->ty_ProcType.et_RetType);
1197 type = TypeToProcType(type,
1198 DupType(sg, stype->ty_ProcType.et_ArgsType),
1202 * If this is a method procedure, we have to change the
1203 * first argument to point at our new subclass. It was
1204 * previously pointing at our superclass. XXX the
1205 * programmer can override the argument. If it isn't a
1206 * reference we have to assert.
1208 if (sg->sg_Stmt->st_Op != ST_Class) {
1210 * XXX probably an inlined procedure, the type is
1211 * already correct. Need an assertrion here.
1213 } else if (stype->ty_SQFlags & SF_METHOD) {
1214 SemGroup *asg = type->ty_ProcType.et_ArgsType->ty_ArgsType.et_SemGroup;
1215 Declaration *d = RUNE_FIRST(&asg->sg_DeclList);
1216 Type *thisType = d->d_StorDecl.ed_Type;
1218 dassert_decl(d, d->d_Id == String_This &&
1219 d->d_Op == DOP_ARGS_STORAGE);
1220 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1221 if (thisType->ty_Op == TY_CLASS) {
1222 /* XXX sg_ClassList? right sg? */
1223 /* XXX correct visibility? */
1224 if (d->d_Search == NULL) {
1225 d->d_Search = d->d_StorDecl.ed_Type->
1226 ty_ClassType.et_SemGroup;
1228 d->d_StorDecl.ed_Type =
1229 AllocClassType(&sg->sg_ClassList,
1230 sg->sg_Stmt->st_ClassStmt.es_Super,
1231 sg->sg_Stmt->st_MyGroup,
1234 dassert_decl(d, thisType->ty_Op == TY_REFTO);
1236 } else if (stype->ty_SQFlags & SF_GMETHOD) {
1240 asg = type->ty_ProcType.et_ArgsType->
1241 ty_ArgsType.et_SemGroup;
1242 d = RUNE_FIRST(&asg->sg_DeclList);
1244 dassert_decl(d, d->d_Id == String_This &&
1245 d->d_Op == DOP_TYPEDEF);
1246 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1247 dassert_decl(d, d->d_TypedefDecl.ed_Type->ty_Op ==
1249 /* XXX sg_ClassList? right sg? */
1250 /* XXX correct visibility? */
1251 if (d->d_Search == NULL) {
1252 d->d_Search = d->d_TypedefDecl.ed_Type->
1253 ty_ClassType.et_SemGroup;
1255 d->d_TypedefDecl.ed_Type =
1256 AllocClassType(&sg->sg_ClassList,
1257 sg->sg_Stmt->st_ClassStmt.es_Super,
1258 sg->sg_Stmt->st_MyGroup,
1267 * e.g. so elements in a superclass will see refined elements
1268 * in the subclass. Note that the original import semgroup
1269 * is left intact so the semantic search mechanism uses it
1270 * when the base sg (typically a subclass) fails.
1272 type = AllocUnresolvedType(
1273 stype->ty_UnresType.et_ImportSemGroup,
1275 stype->ty_UnresType.et_DottedId,
1279 dassert_type(stype, 0);
1282 if (type != stype) {
1283 type->ty_Flags = stype->ty_Flags &
1284 ~(TF_ISINTERNAL | TF_RESOLVING | TF_RESOLVED |
1285 TF_ALIGNRESOLVED | TF_TMPRESOLVED);
1286 type->ty_Bytes = stype->ty_Bytes;
1287 type->ty_AlignMask = stype->ty_AlignMask;
1288 type->ty_Visibility = stype->ty_Visibility;
1289 type->ty_DynamicVector = stype->ty_DynamicVector;
1291 if (stype->ty_AssExp || stype->ty_SQFlags != type->ty_SQFlags) {
1292 type = TypeToQualType(type, NULL,
1294 SetDupExp(sg, stype->ty_AssExp));
1300 typereglist_t TypeRegList = RUNE_HEAD_INITIALIZER(TypeRegList);
1303 InternalRegisterType(const char *str, const char *linkname, Type *type)
1305 static int Special = SPECIALF_REGISTERED|SPECIALF_INTERNAL|1;
1308 dassert(Special & SPECIALF_MASK);
1309 tr = zalloc(sizeof(TypeRegNode));
1310 tr->tr_Id = StrTableAlloc(str, strlen(str), Special++);
1312 tr->tr_LinkName = linkname;
1313 RUNE_INSERT_TAIL(&TypeRegList, tr, tr_Node);
1317 InternalRegisteredTypeLookup(string_t id)
1321 RUNE_FOREACH(tr, &TypeRegList, tr_Node) {
1322 if (tr->tr_Id == id)
1323 return(tr->tr_Type);