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 */
37 Type FloatType; /* generic float placeholder */
42 Type PointerType; /* generic pointer placeholder */
43 Type ReferenceType; /* generic pointer placeholder */
44 Type CCharType; /* const uchar */
45 Type StrType; /* const uchar * */
46 Type UCharPtrType; /* uchar * */
47 Type UCharPtrPtrType; /* uchar ** */
48 Type CUCharPtrPtrType; /* const uchar ** */
49 Type VoidPtrType; /* void * */
50 Type VoidRefType; /* void @ */
51 Type CVoidPtrType; /* const void * */
52 Type LVoidPtrType; /* lvalue void * */
54 Type LexRefType; /* run-time access class tie-ins */
64 typelist_t DynamicTypeList = RUNE_HEAD_INITIALIZER(DynamicTypeList);
65 typelist_t CompoundTypeList = RUNE_HEAD_INITIALIZER(CompoundTypeList);
66 typelist_t ArgsTypeList = RUNE_HEAD_INITIALIZER(ArgsTypeList);
67 typelist_t StorageTypeList = RUNE_HEAD_INITIALIZER(StorageTypeList);
69 static void initInternalClassType(Type *type, Declaration *d);
71 Type *BaseTypeAry[] = {
72 &DynamicLValueType, &DynamicRValueType, &NumericType,
73 &IntegralType, &SIntegerType, &UIntegerType,
74 &VoidType, &BoolType, &Int8Type, &UInt8Type,
75 &Int16Type, &UInt16Type, &Int32Type, &UInt32Type,
76 &Int64Type, &UInt64Type, &Int128Type, &UInt128Type,
78 &IntPtrType, &UIntPtrType, &OffType, &USizeType, &SSizeType,
80 &FloatType, &Float32Type, &Float64Type, &Float128Type,
82 &PointerType, &ReferenceType,
84 &CCharType, &StrType, &UCharPtrType,
85 &UCharPtrPtrType, &CUCharPtrPtrType,
86 &VoidPtrType, &VoidRefType, &CVoidPtrType,
89 &LexRefType, &ScopeType, &DeclarationType, &SemGroupType,
90 &ObjectInfoType, &RuneLockType, &TypeType,
91 &FILLERTypeType, &FILLERDeclType,
96 initType(Type *type, typelist_t *list, int op)
99 RUNE_INIT(&type->ty_QList);
101 RUNE_INSERT_TAIL(list, type, ty_Node);
102 type->ty_SQList = list;
106 initQualType(Type *type, typelist_t *list, int op, int sqflags)
108 initType(type, list, op);
109 type->ty_SQFlags = sqflags;
113 initRawPtrType(Type *type, Type *ptrto, int sqflags)
115 initQualType(type, &ptrto->ty_QList, TY_PTRTO, sqflags);
116 type->ty_RawPtrType.et_Type = ptrto;
117 /* type->ty_Bytes = sizeof(void *); */
118 type->ty_AlignMask = sizeof(void *) - 1;
122 initRefType(Type *type, Type *refto, int sqflags)
124 initQualType(type, &refto->ty_QList, TY_REFTO, sqflags);
125 type->ty_RefType.et_Type = refto;
126 /* type->ty_Bytes = sizeof(ReferenceStor); */
127 type->ty_AlignMask = REFERENCESTOR_ALIGNMASK;
135 initQualType(&DynamicLValueType, &DynamicTypeList,
136 TY_DYNAMIC, SF_LVALUE);
137 initType(&DynamicRValueType, &DynamicTypeList, TY_DYNAMIC);
138 initType(&NumericType, NULL, TY_UNRESOLVED);
139 initType(&IntegralType, NULL, TY_UNRESOLVED);
140 initType(&SIntegerType, NULL, TY_UNRESOLVED);
141 initType(&UIntegerType, NULL, TY_UNRESOLVED);
142 initType(&FloatType, NULL, TY_UNRESOLVED);
143 initType(&PointerType, NULL, TY_UNRESOLVED);
144 initType(&ReferenceType, NULL, TY_UNRESOLVED);
145 PointerType.ty_AlignMask = RAWPTR_ALIGN;
146 ReferenceType.ty_AlignMask = REFERENCESTOR_ALIGNMASK;
148 initType(&VoidType, NULL, TY_UNRESOLVED);
149 initType(&BoolType, NULL, TY_UNRESOLVED);
150 initType(&Int8Type, NULL, TY_UNRESOLVED);
151 initType(&UInt8Type, NULL, TY_UNRESOLVED);
152 initType(&Int16Type, NULL, TY_UNRESOLVED);
153 initType(&UInt16Type, NULL, TY_UNRESOLVED);
154 initType(&Int32Type, NULL, TY_UNRESOLVED);
155 initType(&UInt32Type, NULL, TY_UNRESOLVED);
156 initType(&Int64Type, NULL, TY_UNRESOLVED);
157 initType(&UInt64Type, NULL, TY_UNRESOLVED);
159 initType(&IntPtrType, NULL, TY_UNRESOLVED);
160 initType(&UIntPtrType, NULL, TY_UNRESOLVED);
161 initType(&OffType, NULL, TY_UNRESOLVED);
162 initType(&USizeType, NULL, TY_UNRESOLVED);
163 initType(&SSizeType, 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 initRawPtrType(&StrType, &CCharType, 0);
171 initRawPtrType(&UCharPtrType, &UInt8Type, 0);
172 initRawPtrType(&UCharPtrPtrType, &UCharPtrType, 0);
173 initRawPtrType(&CUCharPtrPtrType, &StrType, 0);
174 initRawPtrType(&VoidPtrType, &VoidType, 0);
175 initRefType(&VoidRefType, &VoidType, 0);
176 initRawPtrType(&CVoidPtrType, &VoidType, SF_CONST);
177 initRawPtrType(&LVoidPtrType, &VoidType, SF_LVALUE);
180 * Mark internal types (not all are bound to classes so it is easiest to
181 * just do it here). This will prevent the collapse code from trying to
182 * collapse our base types.
184 for (i = 0; BaseTypeAry[i]; ++i)
185 BaseTypeAry[i]->ty_Flags |= TF_ISINTERNAL;
189 * Attach an internal class, creating a global summary type for it that
190 * allows our interpreter and code generator to make various assumptions.
193 InternalClassAttach(Parse *p __unused, int t, Declaration *d)
197 dassert_decl(d, d->d_Op == DOP_CLASS);
205 * Special flag helper (resolver sets TF_ISBOOL in in the type)
208 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISBOOL;
238 itype = &UInt128Type;
243 * Special flag helper (resolver sets TF_ISFLOATING in in the
247 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISFLOATING;
250 itype = &Float32Type;
253 itype = &Float64Type;
255 case RUNEID_FLOAT128:
256 itype = &Float128Type;
263 itype = &UIntPtrType;
275 itype = &PointerType;
277 case RUNEID_REFERENCE:
278 itype = &ReferenceType;
281 itype = &NumericType;
283 case RUNEID_INTEGRAL:
284 itype = &IntegralType;
286 case RUNEID_SINTEGER:
288 * Special flag helper (resolver sets TF_ISINTEGER in in the
291 itype = &SIntegerType;
292 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISINTEGER;
294 case RUNEID_UINTEGER:
296 * Special flag helper (resolver sets TF_ISINTEGER and
297 * TF_ISUNSIGNED in the type)
299 itype = &UIntegerType;
300 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISUNSIGNED;
301 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISINTEGER;
309 case RUNEID_DECLARATION:
310 itype = &DeclarationType;
312 case RUNEID_SEMGROUP:
313 itype = &SemGroupType;
315 case RUNEID_OBJECTINFO:
316 itype = &ObjectInfoType;
318 case RUNEID_RUNELOCK:
319 itype = &RuneLockType;
324 case RUNEID_FILLERTYPE:
325 itype = &FILLERTypeType;
327 case RUNEID_FILLERDECL:
328 itype = &FILLERDeclType;
332 char buf[RUNE_IDTOSTR_LEN];
334 dpanic("Unknown internal class: %s", runeid_text(d->d_Id, buf));
339 initInternalClassType(itype, d);
342 * Fixup for const int8 pointers... we did not have a QList to put
343 * CCharType on until now. It will wind up on the SemGroup's
346 if (itype == &UInt8Type) {
347 TypeToQualType(itype, &CCharType, NULL,
348 itype->ty_SQFlags | SF_CONST,
349 itype->ty_Visibility);
356 * This is mostly deprecated except for official type aliases such as
360 InternalTypeAttach(Parse *p, int t, Declaration *d)
364 dassert_decl(d, d->d_Op == DOP_TYPEDEF);
399 itype = &Float32Type;
402 itype = &Float64Type;
404 case RUNEID_FLOAT128:
405 itype = &Float128Type;
412 itype = &UIntPtrType;
424 itype = &PointerType;
426 case RUNEID_REFERENCE:
427 itype = &ReferenceType;
430 itype = &NumericType;
432 case RUNEID_INTEGRAL:
433 itype = &IntegralType;
435 case RUNEID_SINTEGER:
436 itype = &SIntegerType;
438 case RUNEID_UINTEGER:
439 itype = &UIntegerType;
442 itype = InternalRegisteredTypeLookup(d->d_Id);
444 dpanic("Unknown internal type: %s", d->d_Id);
449 if (itype->ty_Op != TY_UNRESOLVED) {
450 t = LexError(&p->p_Token, TOK_ERR_DUPLICATE_ATTACH);
452 Type *ntype = d->d_TypedefDecl.ed_Type;
454 TypeToQualType(ntype, itype, NULL,
456 ntype->ty_Visibility);
459 t = LexError(&p->p_Token, TOK_ERR_UNRECOGNIZED_ATTACH);
465 AllocType(typelist_t *list, int op)
467 Type *type = zalloc(sizeof(Type));
469 initType(type, list, op);
474 * May be used to generate a varargs compound type, in which case the
475 * semgroup may already be resolved.
477 * We do no matching/caching at this time and callers assume that (for making
478 * adjustments) to the underlying sg at parse-time via TypeToProcType()
481 AllocCompoundType(SemGroup *sg)
485 type = AllocType(&CompoundTypeList, TY_COMPOUND);
486 type->ty_CompType.et_SemGroup = sg;
487 dassert((sg->sg_Flags & SGF_RESOLVED) == 0);
492 * XXX match the compound type(s)
495 AllocArgsType(SemGroup *sg)
499 type = AllocType(&ArgsTypeList, TY_ARGS);
500 type->ty_ArgsType.et_SemGroup = sg;
505 AllocStorageType(urunesize_t bytes)
509 RUNE_FOREACH(type, &StorageTypeList, ty_Node) {
510 if (type->ty_Op == TY_STORAGE &&
511 type->ty_StorType.et_Bytes == bytes
516 type = AllocType(&StorageTypeList, TY_STORAGE);
517 type->ty_StorType.et_Bytes = bytes;
522 AllocUnresolvedType(SemGroup *isg, SemGroup *sg, runeid_t *ary, int eatAry)
526 dassert_sg(sg, ary != NULL);
528 RUNE_FOREACH(type, &sg->sg_ClassList, ty_Node) {
531 if (type->ty_Op != TY_UNRESOLVED)
533 if (type->ty_UnresType.et_ImportSemGroup != isg)
536 for (i = 0; ary[i]; ++i) {
537 if (ary[i] != type->ty_UnresType.et_DottedId[i])
540 if (ary[i] == 0 && type->ty_UnresType.et_DottedId[i] == 0) {
546 type = AllocType((sg ? &sg->sg_ClassList : NULL), TY_UNRESOLVED);
547 type->ty_UnresType.et_DottedId = ary;
548 type->ty_UnresType.et_SemGroup = sg; /* may be NULL */
549 type->ty_UnresType.et_ImportSemGroup = isg; /* may be NULL */
554 * AllocClassType() - Allocate a class representing a the semgroup which in
555 * turn represents (typically) a class.
557 * (This function is only used by parse1.c when parsing class statements)
560 AllocClassType(typelist_t *list, Type *super, SemGroup *sg, int visibility)
565 list = &sg->sg_ClassList;
567 RUNE_FOREACH(type, list, ty_Node) {
568 if (type->ty_Op == TY_CLASS &&
569 type->ty_ClassType.et_SemGroup == sg &&
570 type->ty_ClassType.et_Super == super &&
571 type->ty_Visibility == visibility)
577 dassert(&sg->sg_ClassList == list);
578 type = AllocType(list, TY_CLASS);
579 type->ty_ClassType.et_SemGroup = sg;
580 type->ty_ClassType.et_Super = super;
581 type->ty_Visibility = visibility;
587 initInternalClassType(Type *type, Declaration *d)
589 SemGroup *sg = d->d_ClassDecl.ed_SemGroup;
591 dassert(d->d_Op == DOP_CLASS);
593 initType(type, &sg->sg_ClassList, TY_CLASS);
594 RUNE_REMOVE(&sg->sg_ClassList, type, ty_Node);
595 RUNE_INSERT_HEAD(&sg->sg_ClassList, type, ty_Node);
596 type->ty_ClassType.et_SemGroup = sg;
597 type->ty_ClassType.et_Super = d->d_ClassDecl.ed_Super;
598 if (sg->sg_ClassType)
599 type->ty_SQFlags = sg->sg_ClassType->ty_SQFlags;
600 type->ty_Visibility = d->d_ScopeFlags & SCOPE_ALL_VISIBLE;
601 type->ty_Flags |= TF_ISINTERNAL;
605 AllocImportType(typelist_t *list, SemGroup *sg, int visibility)
607 Type *type = AllocType(list, TY_IMPORT);
609 type->ty_ImportType.et_SemGroup = sg;
610 type->ty_Visibility = visibility;
615 * adjtype must be moved to type's QList because type is being modified such
616 * that that is where it is expected to be.
619 TypeAdjustQList(Type *type, Type *adjtype)
622 if (adjtype->ty_SQList)
623 RUNE_REMOVE(adjtype->ty_SQList, adjtype, ty_Node);
624 adjtype->ty_SQList = &type->ty_QList;
625 RUNE_INSERT_TAIL(adjtype->ty_SQList, adjtype, ty_Node);
631 * Compound types inherit locking modes if not specifically overridden.
634 TypeFixupInheritedFlags(Type *type, int rqflags)
639 if (type->ty_Op != TY_COMPOUND)
643 * Assume no matching collapse yet
645 sg = type->ty_CompType.et_SemGroup;
646 RUNE_FOREACH(d, &sg->sg_DeclList, d_Node) {
647 if (d->d_Op != DOP_GROUP_STORAGE)
649 if ((d->d_ScopeFlags & SCOPE_LOCKING_MASK) == 0) {
650 if (rqflags & SF_UNTRACKED)
651 d->d_ScopeFlags |= SCOPE_UNTRACKED;
652 if (rqflags & SF_UNLOCKED)
653 d->d_ScopeFlags |= SCOPE_UNLOCKED;
654 if (rqflags & SF_SOFT)
655 d->d_ScopeFlags |= SCOPE_SOFT;
656 if (rqflags & SF_HARD)
657 d->d_ScopeFlags |= SCOPE_HARD;
664 * Convert type to qualified type
666 * NOTE: exp is applied to ty_OrigAssExp, meaning that the new type
667 * must then be resolved.
670 TypeToQualType(Type *otype, Type *ntype, Exp *exp,
671 int sqflags, int visibility)
676 * Combine with existing qualifiers, Shortcut if no changes made.
678 if (ntype == NULL && sqflags == otype->ty_SQFlags &&
679 (exp == NULL || exp == otype->ty_OrigAssExp))
685 * See if we already have a matching qualified type (only if storage for
686 * the new type is not being provided). Note: the provided storage has
687 * already been initType()d
690 RUNE_FOREACH(ntype, otype->ty_SQList, ty_Node) {
691 if (ntype->ty_Op == otype->ty_Op &&
692 ntype->ty_SQFlags == sqflags &&
693 ntype->ty_Visibility == visibility &&
694 (exp == NULL || ntype->ty_OrigAssExp == exp))
696 if (SameType(ntype, otype, sqflags))
703 * Build a new qualified type and set its qualifiers, then duplicate
704 * appropriate sections of the old type.
706 * Default to the same SQList as otype.
709 ntype = AllocType(otype->ty_SQList, otype->ty_Op);
711 if (ntype->ty_SQList)
712 RUNE_REMOVE(ntype->ty_SQList, ntype, ty_Node);
713 ntype->ty_SQList = otype->ty_SQList;
714 RUNE_INSERT_TAIL(ntype->ty_SQList, ntype, ty_Node);
718 * Set the op and the expression. Unlike SQFlags, if exp is passed as
719 * NULL we inherit the old type's default.
721 * The DupExp() call here is special, see DupExp()'s handling of ex_Decl.
723 * Normally DupExp() is called during resolution prior to ex_Decl being
724 * set. This is the one case where it may be called with ex_Decl already
727 * WARNING! We do not try to resolve the type here. Various resolve
728 * related flags in ty_Flags will be resolved later. This includes
729 * TF_ISUNSIGNED and other TF_* flags.
731 ntype->ty_Op = otype->ty_Op;
733 ntype->ty_OrigAssExp = exp;
734 else if (otype->ty_OrigAssExp)
735 ntype->ty_OrigAssExp = otype->ty_OrigAssExp;
736 ntype->ty_SQFlags = sqflags;
737 ntype->ty_Visibility = visibility;
739 switch (otype->ty_Op) {
742 * When updating the class, alternative forms are collapsed into it's
743 * SemGroup->sg_ClassList and not into some potentially long
744 * recursive chain based on ty_QList.
746 sg = otype->ty_ClassType.et_SemGroup;
748 dassert(ntype->ty_SQList == &sg->sg_ClassList);
749 if (ntype->ty_ClassType.et_SemGroup != sg) {
750 ntype->ty_ClassType.et_SemGroup = sg;
752 ntype->ty_ClassType.et_Super = otype->ty_ClassType.et_Super;
755 ntype->ty_Visibility = otype->ty_Visibility;
756 ntype->ty_ImportType.et_SemGroup = otype->ty_ImportType.et_SemGroup;
759 ntype->ty_RawPtrType.et_Type = otype->ty_RawPtrType.et_Type;
762 ntype->ty_RefType.et_Type = otype->ty_RefType.et_Type;
766 * note: multiple type structures may share the same array size
767 * expression in simple qualified-type cases. YYY XXX bad bad.
769 ntype->ty_AryType.et_Type = otype->ty_AryType.et_Type;
770 ntype->ty_AryType.et_OrigArySize = otype->ty_AryType.et_OrigArySize;
771 ntype->ty_AryType.et_SemGroup = otype->ty_AryType.et_SemGroup;
772 ntype->ty_AryType.et_Count = otype->ty_AryType.et_Count;
775 ntype->ty_CompType.et_SemGroup = otype->ty_CompType.et_SemGroup;
778 ntype->ty_VarType.et_Type = otype->ty_VarType.et_Type;
779 ntype->ty_VarType.et_SemGroup = otype->ty_VarType.et_SemGroup;
782 ntype->ty_ArgsType.et_SemGroup = otype->ty_ArgsType.et_SemGroup;
785 ntype->ty_ProcType.et_ArgsType = otype->ty_ProcType.et_ArgsType;
786 ntype->ty_ProcType.et_RetType = otype->ty_ProcType.et_RetType;
787 ntype->ty_ProcType.et_ArgCount = otype->ty_ProcType.et_ArgCount;
788 dassert(ntype->ty_SQList ==
789 &otype->ty_ProcType.et_RetType->ty_QList);
792 ntype->ty_StorType.et_Bytes = otype->ty_StorType.et_Bytes;
796 * It is not legal to qualify a dynamic type other then to add or
799 dpanic("Dynamic type cannot be qualified");
802 ntype->ty_UnresType.et_DottedId =
803 otype->ty_UnresType.et_DottedId;
804 ntype->ty_UnresType.et_SemGroup =
805 otype->ty_UnresType.et_SemGroup;
806 ntype->ty_UnresType.et_ImportSemGroup =
807 otype->ty_UnresType.et_ImportSemGroup;
810 dassert_type(otype, 0);
816 TypeToVisibilityType(Type *type, int visibility)
818 if (type->ty_Visibility != visibility) {
819 type = TypeToQualType(type, NULL, type->ty_OrigAssExp,
820 type->ty_SQFlags, visibility);
826 * Convert a return-type + argument-type into a procedure type. If adjret is
827 * non-zero the return-type is converted to locked storage (which is
828 * generally what we want).
830 * Match the procedure type(s) (after adjustment?)
833 TypeToProcType(Type *rtype, Type *atype, int adjret)
835 urunesize_t count = 0;
840 dassert_type(atype, atype->ty_Op == TY_ARGS);
843 rtype = TypeFixupInheritedFlags(rtype, rtype->ty_SQFlags);
845 sg = atype->ty_CompType.et_SemGroup;
847 RUNE_FOREACH(d, &sg->sg_DeclList, d_Node) {
850 RUNE_FOREACH(type, &rtype->ty_QList, ty_Node) {
851 if (type->ty_Op == TY_PROC) {
852 if (type->ty_ProcType.et_ArgsType == atype &&
853 type->ty_ProcType.et_RetType == rtype &&
854 type->ty_ProcType.et_ArgCount == count
860 type = AllocType(&rtype->ty_QList, TY_PROC);
861 type->ty_ProcType.et_ArgsType = AllocArgsType(sg);
862 type->ty_ProcType.et_RetType = rtype;
863 type->ty_ProcType.et_ArgCount = count;
868 * Convert type to pointer-to-type
871 TypeToRawPtrType(Type *otype)
875 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
876 if (type->ty_Op == TY_PTRTO)
879 type = AllocType(&otype->ty_QList, TY_PTRTO);
880 type->ty_RawPtrType.et_Type = otype;
886 * Convert type to ref-to-type
888 * A reference type is similar to a pointer type except that the resolver is
889 * not able to entirely know what it is pointing to. The reference type is a
890 * superclass, but the actual type is stored in the run-time structure.
893 TypeToRefType(Type *otype)
897 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
898 if (type->ty_Op == TY_REFTO)
901 type = AllocType(&otype->ty_QList, TY_REFTO);
902 type->ty_RefType.et_Type = otype;
907 TypeToAryType(Type *otype, Exp *exp, SemGroup *sg)
912 * XXX handle constant expression optimization for QList XXX handle
915 type = AllocType(&otype->ty_QList, TY_ARYOF);
916 type->ty_AryType.et_OrigArySize = exp;
917 type->ty_AryType.et_Type = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
918 type->ty_AryType.et_SemGroup = sg;
919 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
927 TypeToRunTimeAryType(Type *otype, int count)
930 Type *t2 = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
932 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
933 if (type->ty_Op == TY_ARYOF &&
934 type->ty_AryType.et_Type == t2 &&
935 type->ty_AryType.et_Count == count &&
937 (otype->ty_SQFlags & SF_MASK_ARY_INHERIT)
942 type = AllocType(&otype->ty_QList, TY_ARYOF);
943 type->ty_AryType.et_Count = count;
944 type->ty_AryType.et_Type = t2;
945 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
952 TypeToVarType(Type *otype, SemGroup *sg)
956 /* dassert(sg->sg_Flags & SGF_RESOLVED); */
957 /* dassert(otype->ty_Flags & TF_RESOLVED); */
959 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
960 if (type->ty_Op == TY_VAR &&
961 type->ty_VarType.et_Type == otype &&
962 type->ty_VarType.et_SemGroup == sg
964 puts("SG2"); /* YYY */
968 type = AllocType(&otype->ty_QList, TY_VAR);
969 type->ty_VarType.et_Type = otype;
970 type->ty_VarType.et_SemGroup = sg;
972 /* XXX doesn't work for var-args */
973 if (sg->sg_Flags & SGF_RESOLVED) {
974 type->ty_Flags |= TF_RESOLVED;
975 type->ty_Bytes = sg->sg_Bytes;
976 type->ty_AlignMask = sg->sg_AlignMask;
983 * ChangeType() - given pointer, C pointer, or array of something, return
984 * 'op' of something instead.
987 ChangeType(Type *type, int op)
989 switch (type->ty_Op) {
993 type = TypeToAryType(type->ty_RawPtrType.et_Type,
997 dpanic("Illegal type convesion (B)");
1003 type = TypeToRawPtrType(type->ty_AryType.et_Type);
1006 dpanic("Illegal type convesion (C)");
1010 dpanic("Illegal type convesion (D)");
1016 * BaseType() - return base type
1018 * Traverse the type to locate the base type. Store the base type in *ptype
1019 * and return the SemGroup, or return NULL if the base type does not have a
1023 BaseType(Type **ptype)
1025 Type *type = *ptype;
1028 switch (type->ty_Op) {
1030 type = type->ty_RawPtrType.et_Type;
1033 type = type->ty_RefType.et_Type;
1036 type = type->ty_AryType.et_Type;
1044 switch (type->ty_Op) {
1046 return (type->ty_ClassType.et_SemGroup);
1048 return (type->ty_CompType.et_SemGroup);
1050 return (type->ty_ArgsType.et_SemGroup);
1058 dassert_type(type, 0);
1059 return (NULL); /* avoid compiler complaints */
1064 * DupType() - create a duplicate of a type, possibly in a new SemGroup.
1066 * This code is used when duplicating procedures and other elements when
1067 * merging a superclass into a subclass.
1069 * If sg is NULL, stype is simply returned. The case is used when we try to
1070 * duplciate an expression with DupExp()... in that case we want to dup the
1071 * expression tree but use the same types.
1074 DupType(SemGroup *sg, Type *stype)
1082 * XXX type may be resolved if it is part of a varargs dup
1086 (stype->ty_Flags & (TF_RESOLVED | TF_RESOLVING)) == 0);
1089 switch (stype->ty_Op) {
1092 * This only occurs because the resolver has resolved an unresolved
1093 * type on the original SemGroup. We duplicate that on the new
1094 * SemGroup. XXX remove
1097 type = AllocClassType(&sg->sg_ClassList,
1098 stype->ty_ClassType.et_Super,
1099 stype->ty_ClassType.et_SemGroup,
1100 stype->ty_Visibility);
1103 type = TypeToQualType(sg->sg_ClassType, NULL, stype->ty_OrigAssExp,
1105 stype->ty_Visibility);
1112 * For TY_IMPORT, leave et_SemGroup alone?
1117 type = TypeToRawPtrType(DupType(sg, stype->ty_RawPtrType.et_Type));
1120 type = TypeToRefType(DupType(sg, stype->ty_RefType.et_Type));
1124 * When redeclaring an array, the semantic context if the
1125 * [exp] needs to be able to see refined elements in subclasses,
1126 * even if the array declaration itself has not been refined.
1128 * XXX however, this may make other elements in the subclass
1129 * visible to the [exp] that we did not refine (? TEST)
1133 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1134 stype->ty_AryType.et_OrigArySize,
1137 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1138 stype->ty_AryType.et_OrigArySize,
1139 stype->ty_AryType.et_SemGroup);
1141 type->ty_AryType.et_Count = stype->ty_AryType.et_Count;
1145 * varargs (whole thing), requires duplicating the semantic group
1146 * they are contained in too.
1148 type = TypeToVarType(DupType(sg, stype->ty_VarType.et_Type),
1149 DupSemGroup(sg, NULL, stype->ty_VarType.et_SemGroup, 1));
1152 type = AllocCompoundType(DupSemGroup(sg, NULL,
1153 stype->ty_CompType.et_SemGroup, 1));
1157 * Arguments (whole thing), requires duplicating the semantic
1158 * group they are contained in too.
1160 * At the moment we always formally duplicate the arguments so we can
1161 * modify them for methods below.
1163 type = AllocArgsType(DupSemGroup(sg, NULL,
1164 stype->ty_CompType.et_SemGroup, 1));
1170 type = DupType(sg, stype->ty_ProcType.et_RetType);
1171 type = TypeToProcType(type,
1172 DupType(sg, stype->ty_ProcType.et_ArgsType),
1180 * e.g. so elements in a superclass will see refined elements in the
1181 * subclass. Note that the original import semgroup is left intact
1182 * so the semantic search mechanism uses it when the base sg
1183 * (typically a subclass) fails.
1185 type = AllocUnresolvedType(stype->ty_UnresType.et_ImportSemGroup,
1187 stype->ty_UnresType.et_DottedId,
1191 dassert_type(stype, 0);
1194 if (type != stype) {
1195 type->ty_Flags = stype->ty_Flags &
1196 ~(TF_ISINTERNAL | TF_RESOLVING | TF_RESOLVED |
1197 TF_ALIGNRESOLVED | TF_TMPRESOLVED);
1198 type->ty_Bytes = stype->ty_Bytes;
1199 type->ty_AlignMask = stype->ty_AlignMask;
1200 type->ty_Visibility = stype->ty_Visibility;
1201 type->ty_DynamicVector = stype->ty_DynamicVector;
1203 if (stype->ty_OrigAssExp ||
1204 stype->ty_SQFlags != type->ty_SQFlags ||
1205 stype->ty_Visibility != type->ty_Visibility)
1207 type = TypeToQualType(type, NULL, stype->ty_OrigAssExp,
1208 stype->ty_SQFlags, stype->ty_Visibility);
1214 typereglist_t TypeRegList = RUNE_HEAD_INITIALIZER(TypeRegList);
1217 InternalRegisterType(const char *str, const char *linkname, Type *type)
1221 tr = zalloc(sizeof(TypeRegNode));
1222 tr->tr_Id = runeid_hash(str, strlen(str));
1224 tr->tr_LinkName = linkname;
1225 RUNE_INSERT_TAIL(&TypeRegList, tr, tr_Node);
1229 InternalRegisteredTypeLookup(runeid_t id)
1233 RUNE_FOREACH(tr, &TypeRegList, tr_Node) {
1234 if (tr->tr_Id == id)
1235 return (tr->tr_Type);