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,
348 itype->ty_SQFlags | SF_CONST, NULL);
355 * This is mostly deprecated except for official type aliases such as
359 InternalTypeAttach(Parse *p, int t, Declaration *d)
363 dassert_decl(d, d->d_Op == DOP_TYPEDEF);
398 itype = &Float32Type;
401 itype = &Float64Type;
403 case RUNEID_FLOAT128:
404 itype = &Float128Type;
411 itype = &UIntPtrType;
423 itype = &PointerType;
425 case RUNEID_REFERENCE:
426 itype = &ReferenceType;
429 itype = &NumericType;
431 case RUNEID_INTEGRAL:
432 itype = &IntegralType;
434 case RUNEID_SINTEGER:
435 itype = &SIntegerType;
437 case RUNEID_UINTEGER:
438 itype = &UIntegerType;
441 itype = InternalRegisteredTypeLookup(d->d_Id);
443 dpanic("Unknown internal type: %s", d->d_Id);
448 if (itype->ty_Op != TY_UNRESOLVED) {
449 t = LexError(&p->p_Token, TOK_ERR_DUPLICATE_ATTACH);
451 Type *ntype = d->d_TypedefDecl.ed_Type;
453 TypeToQualType(ntype, itype, ntype->ty_SQFlags, NULL);
456 t = LexError(&p->p_Token, TOK_ERR_UNRECOGNIZED_ATTACH);
462 AllocType(typelist_t *list, int op)
464 Type *type = zalloc(sizeof(Type));
466 initType(type, list, op);
471 * May be used to generate a varargs compound type, in which case the
472 * semgroup may already be resolved.
474 * We do no matching/caching at this time and callers assume that (for making
475 * adjustments) to the underlying sg at parse-time via TypeToProcType()
478 AllocCompoundType(SemGroup *sg)
482 type = AllocType(&CompoundTypeList, TY_COMPOUND);
483 type->ty_CompType.et_SemGroup = sg;
484 dassert((sg->sg_Flags & SGF_RESOLVED) == 0);
489 * XXX match the compound type(s)
492 AllocArgsType(SemGroup *sg)
496 type = AllocType(&ArgsTypeList, TY_ARGS);
497 type->ty_ArgsType.et_SemGroup = sg;
502 AllocStorageType(urunesize_t bytes)
506 RUNE_FOREACH(type, &StorageTypeList, ty_Node) {
507 if (type->ty_Op == TY_STORAGE &&
508 type->ty_StorType.et_Bytes == bytes
513 type = AllocType(&StorageTypeList, TY_STORAGE);
514 type->ty_StorType.et_Bytes = bytes;
519 AllocUnresolvedType(SemGroup *isg, SemGroup *sg, runeid_t *ary, int eatAry)
523 dassert_semgrp(sg, ary != NULL);
525 RUNE_FOREACH(type, &sg->sg_ClassList, ty_Node) {
528 if (type->ty_Op != TY_UNRESOLVED)
530 if (type->ty_UnresType.et_ImportSemGroup != isg)
533 for (i = 0; ary[i]; ++i) {
534 if (ary[i] != type->ty_UnresType.et_DottedId[i])
537 if (ary[i] == 0 && type->ty_UnresType.et_DottedId[i] == 0) {
543 type = AllocType((sg ? &sg->sg_ClassList : NULL), TY_UNRESOLVED);
544 type->ty_UnresType.et_DottedId = ary;
545 type->ty_UnresType.et_SemGroup = sg; /* may be NULL */
546 type->ty_UnresType.et_ImportSemGroup = isg; /* may be NULL */
551 * AllocClassType() - Allocate a class representing a the semgroup which in
552 * turn represents (typically) a class.
555 AllocClassType(typelist_t *list, Type *super, SemGroup *sg, int visibility)
560 list = &sg->sg_ClassList;
562 RUNE_FOREACH(type, list, ty_Node) {
563 if (type->ty_Op == TY_CLASS &&
564 type->ty_ClassType.et_SemGroup == sg &&
565 type->ty_ClassType.et_Super == super &&
566 type->ty_Visibility == visibility)
572 dassert(&sg->sg_ClassList == list);
573 type = AllocType(list, TY_CLASS);
574 type->ty_ClassType.et_SemGroup = sg;
575 type->ty_ClassType.et_Super = super;
576 type->ty_Visibility = visibility;
582 initInternalClassType(Type *type, Declaration *d)
584 SemGroup *sg = d->d_ClassDecl.ed_SemGroup;
586 initType(type, &sg->sg_ClassList, TY_CLASS);
587 RUNE_REMOVE(&sg->sg_ClassList, type, ty_Node);
588 RUNE_INSERT_HEAD(&sg->sg_ClassList, type, ty_Node);
589 type->ty_ClassType.et_SemGroup = d->d_ClassDecl.ed_SemGroup;
590 type->ty_ClassType.et_Super = d->d_ClassDecl.ed_Super;
591 type->ty_Visibility = d->d_ScopeFlags & SCOPE_ALL_VISIBLE;
592 type->ty_Flags |= TF_ISINTERNAL;
596 AllocImportType(typelist_t *list, SemGroup *sg, int visibility)
598 Type *type = AllocType(list, TY_IMPORT);
600 type->ty_ImportType.et_SemGroup = sg;
601 type->ty_Visibility = visibility;
606 * adjtype must be moved to type's QList because type is being modified such
607 * that that is where it is expected to be.
610 TypeAdjustQList(Type *type, Type *adjtype)
613 if (adjtype->ty_SQList)
614 RUNE_REMOVE(adjtype->ty_SQList, adjtype, ty_Node);
615 adjtype->ty_SQList = &type->ty_QList;
616 RUNE_INSERT_TAIL(adjtype->ty_SQList, adjtype, ty_Node);
622 * Compound types inherit locking modes if not specifically overridden.
625 TypeFixupInheritedFlags(Type *type, int rqflags)
630 if (type->ty_Op != TY_COMPOUND)
634 * Assume no matching collapse yet
636 sg = type->ty_CompType.et_SemGroup;
637 RUNE_FOREACH(d, &sg->sg_DeclList, d_Node) {
638 if (d->d_Op != DOP_GROUP_STORAGE)
640 if ((d->d_ScopeFlags & SCOPE_LOCKING_MASK) == 0) {
641 if (rqflags & SF_UNTRACKED)
642 d->d_ScopeFlags |= SCOPE_UNTRACKED;
643 if (rqflags & SF_UNLOCKED)
644 d->d_ScopeFlags |= SCOPE_UNLOCKED;
645 if (rqflags & SF_SOFT)
646 d->d_ScopeFlags |= SCOPE_SOFT;
647 if (rqflags & SF_HARD)
648 d->d_ScopeFlags |= SCOPE_HARD;
655 * Convert type to qualified type
657 * NOTE: exp is applied to ty_OrigAssExp, meaning that the new type
658 * must then be resolved.
661 TypeToQualType(Type *otype, Type *ntype, int sqFlags, Exp *exp)
666 * Combine with existing qualifiers, Shortcut if no changes made.
668 if (ntype == NULL && sqFlags == otype->ty_SQFlags &&
669 (exp == NULL || exp == otype->ty_OrigAssExp))
675 * See if we already have a matching qualified type (only if storage for
676 * the new type is not being provided). Note: the provided storage has
677 * already been initType()d
680 RUNE_FOREACH(ntype, otype->ty_SQList, ty_Node) {
681 if (ntype->ty_Op == otype->ty_Op &&
682 ntype->ty_SQFlags == sqFlags &&
683 (exp == NULL || ntype->ty_OrigAssExp == exp))
685 if (SameType(ntype, otype, sqFlags))
692 * Build a new qualified type and set its qualifiers, then duplicate
693 * appropriate sections of the old type.
695 * Default to the same SQList as otype.
698 ntype = AllocType(otype->ty_SQList, otype->ty_Op);
700 if (ntype->ty_SQList)
701 RUNE_REMOVE(ntype->ty_SQList, ntype, ty_Node);
702 ntype->ty_SQList = otype->ty_SQList;
703 RUNE_INSERT_TAIL(ntype->ty_SQList, ntype, ty_Node);
707 * Set the op and the expression. Unlike SQFlags, if exp is passed as
708 * NULL we inherit the old type's default.
710 * The DupExp() call here is special, see DupExp()'s handling of ex_Decl.
712 * Normally DupExp() is called during resolution prior to ex_Decl being
713 * set. This is the one case where it may be called with ex_Decl already
716 * WARNING! We do not try to resolve the type here. Various resolve
717 * related flags in ty_Flags will be resolved later. This includes
718 * TF_ISUNSIGNED and other TF_* flags.
720 ntype->ty_Op = otype->ty_Op;
722 ntype->ty_OrigAssExp = exp;
723 else if (otype->ty_OrigAssExp)
724 ntype->ty_OrigAssExp = otype->ty_OrigAssExp;
725 ntype->ty_SQFlags = sqFlags;
726 ntype->ty_Visibility = otype->ty_Visibility;
728 switch (otype->ty_Op) {
731 * When updating the class, alternative forms are collapsed into it's
732 * SemGroup->sg_ClassList and not into some potentially long
733 * recursive chain based on ty_QList.
735 sg = otype->ty_ClassType.et_SemGroup;
737 dassert(ntype->ty_SQList == &sg->sg_ClassList);
738 if (ntype->ty_ClassType.et_SemGroup != sg) {
739 ntype->ty_ClassType.et_SemGroup = sg;
741 ntype->ty_ClassType.et_Super = otype->ty_ClassType.et_Super;
744 ntype->ty_Visibility = otype->ty_Visibility;
745 ntype->ty_ImportType.et_SemGroup = otype->ty_ImportType.et_SemGroup;
748 ntype->ty_RawPtrType.et_Type = otype->ty_RawPtrType.et_Type;
751 ntype->ty_RefType.et_Type = otype->ty_RefType.et_Type;
755 * note: multiple type structures may share the same array size
756 * expression in simple qualified-type cases. YYY XXX bad bad.
758 ntype->ty_AryType.et_Type = otype->ty_AryType.et_Type;
759 ntype->ty_AryType.et_OrigArySize = otype->ty_AryType.et_OrigArySize;
760 ntype->ty_AryType.et_SemGroup = otype->ty_AryType.et_SemGroup;
761 ntype->ty_AryType.et_Count = otype->ty_AryType.et_Count;
764 ntype->ty_CompType.et_SemGroup = otype->ty_CompType.et_SemGroup;
767 ntype->ty_VarType.et_Type = otype->ty_VarType.et_Type;
768 ntype->ty_VarType.et_SemGroup = otype->ty_VarType.et_SemGroup;
771 ntype->ty_ArgsType.et_SemGroup = otype->ty_ArgsType.et_SemGroup;
774 ntype->ty_ProcType.et_ArgsType = otype->ty_ProcType.et_ArgsType;
775 ntype->ty_ProcType.et_RetType = otype->ty_ProcType.et_RetType;
776 ntype->ty_ProcType.et_ArgCount = otype->ty_ProcType.et_ArgCount;
777 dassert(ntype->ty_SQList ==
778 &otype->ty_ProcType.et_RetType->ty_QList);
781 ntype->ty_StorType.et_Bytes = otype->ty_StorType.et_Bytes;
785 * It is not legal to qualify a dynamic type other then to add or
788 dpanic("Dynamic type cannot be qualified");
791 ntype->ty_UnresType.et_DottedId =
792 otype->ty_UnresType.et_DottedId;
793 ntype->ty_UnresType.et_SemGroup =
794 otype->ty_UnresType.et_SemGroup;
795 ntype->ty_UnresType.et_ImportSemGroup =
796 otype->ty_UnresType.et_ImportSemGroup;
799 dassert_type(otype, 0);
805 * Convert a return-type + argument-type into a procedure type. If adjret is
806 * non-zero the return-type is converted to locked storage (which is
807 * generally what we want).
809 * Match the procedure type(s) (after adjustment?)
812 TypeToProcType(Type *rtype, Type *atype, int adjret)
814 urunesize_t count = 0;
819 dassert_type(atype, atype->ty_Op == TY_ARGS);
822 rtype = TypeFixupInheritedFlags(rtype, rtype->ty_SQFlags);
824 sg = atype->ty_CompType.et_SemGroup;
826 RUNE_FOREACH(d, &sg->sg_DeclList, d_Node) {
829 RUNE_FOREACH(type, &rtype->ty_QList, ty_Node) {
830 if (type->ty_Op == TY_PROC) {
831 if (type->ty_ProcType.et_ArgsType == atype &&
832 type->ty_ProcType.et_RetType == rtype &&
833 type->ty_ProcType.et_ArgCount == count
839 type = AllocType(&rtype->ty_QList, TY_PROC);
840 type->ty_ProcType.et_ArgsType = AllocArgsType(sg);
841 type->ty_ProcType.et_RetType = rtype;
842 type->ty_ProcType.et_ArgCount = count;
847 * Convert type to pointer-to-type
850 TypeToRawPtrType(Type *otype)
854 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
855 if (type->ty_Op == TY_PTRTO)
858 type = AllocType(&otype->ty_QList, TY_PTRTO);
859 type->ty_RawPtrType.et_Type = otype;
865 * Convert type to ref-to-type
867 * A reference type is similar to a pointer type except that the resolver is
868 * not able to entirely know what it is pointing to. The reference type is a
869 * superclass, but the actual type is stored in the run-time structure.
872 TypeToRefType(Type *otype)
876 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
877 if (type->ty_Op == TY_REFTO)
880 type = AllocType(&otype->ty_QList, TY_REFTO);
881 type->ty_RefType.et_Type = otype;
886 TypeToAryType(Type *otype, Exp *exp, SemGroup *sg)
891 * XXX handle constant expression optimization for QList XXX handle
894 type = AllocType(&otype->ty_QList, TY_ARYOF);
895 type->ty_AryType.et_OrigArySize = exp;
896 type->ty_AryType.et_Type = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
897 type->ty_AryType.et_SemGroup = sg;
898 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
906 TypeToRunTimeAryType(Type *otype, int count)
909 Type *t2 = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
911 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
912 if (type->ty_Op == TY_ARYOF &&
913 type->ty_AryType.et_Type == t2 &&
914 type->ty_AryType.et_Count == count &&
916 (otype->ty_SQFlags & SF_MASK_ARY_INHERIT)
921 type = AllocType(&otype->ty_QList, TY_ARYOF);
922 type->ty_AryType.et_Count = count;
923 type->ty_AryType.et_Type = t2;
924 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
931 TypeToVarType(Type *otype, SemGroup *sg)
935 /* dassert(sg->sg_Flags & SGF_RESOLVED); */
936 /* dassert(otype->ty_Flags & TF_RESOLVED); */
938 RUNE_FOREACH(type, &otype->ty_QList, ty_Node) {
939 if (type->ty_Op == TY_VAR &&
940 type->ty_VarType.et_Type == otype &&
941 type->ty_VarType.et_SemGroup == sg
943 puts("SG2"); /* YYY */
947 type = AllocType(&otype->ty_QList, TY_VAR);
948 type->ty_VarType.et_Type = otype;
949 type->ty_VarType.et_SemGroup = sg;
951 /* XXX doesn't work for var-args */
952 if (sg->sg_Flags & SGF_RESOLVED) {
953 type->ty_Flags |= TF_RESOLVED;
954 type->ty_Bytes = sg->sg_Bytes;
955 type->ty_AlignMask = sg->sg_AlignMask;
962 * ChangeType() - given pointer, C pointer, or array of something, return
963 * 'op' of something instead.
966 ChangeType(Type *type, int op)
968 switch (type->ty_Op) {
972 type = TypeToAryType(type->ty_RawPtrType.et_Type,
976 dpanic("Illegal type convesion (B)");
982 type = TypeToRawPtrType(type->ty_AryType.et_Type);
985 dpanic("Illegal type convesion (C)");
989 dpanic("Illegal type convesion (D)");
995 * BaseType() - return base type
997 * Traverse the type to locate the base type. Store the base type in *ptype
998 * and return the SemGroup, or return NULL if the base type does not have a
1002 BaseType(Type **ptype)
1004 Type *type = *ptype;
1007 switch (type->ty_Op) {
1009 type = type->ty_RawPtrType.et_Type;
1012 type = type->ty_RefType.et_Type;
1015 type = type->ty_AryType.et_Type;
1023 switch (type->ty_Op) {
1025 return (type->ty_ClassType.et_SemGroup);
1027 return (type->ty_CompType.et_SemGroup);
1029 return (type->ty_ArgsType.et_SemGroup);
1037 dassert_type(type, 0);
1038 return (NULL); /* avoid compiler complaints */
1043 * DupType() - create a duplicate of a type, possibly in a new SemGroup.
1045 * This code is used when duplicating procedures and other elements when
1046 * merging a superclass into a subclass.
1048 * If sg is NULL, stype is simply returned. The case is used when we try to
1049 * duplciate an expression with DupExp()... in that case we want to dup the
1050 * expression tree but use the same types.
1053 DupType(SemGroup *sg, Type *stype)
1061 * XXX type may be resolved if it is part of a varargs dup
1065 (stype->ty_Flags & (TF_RESOLVED | TF_RESOLVING)) == 0);
1068 switch (stype->ty_Op) {
1071 * This only occurs because the resolver has resolved an unresolved
1072 * type on the original SemGroup. We duplicate that on the new
1075 type = AllocClassType(&sg->sg_ClassList,
1076 stype->ty_ClassType.et_Super,
1077 stype->ty_ClassType.et_SemGroup,
1078 stype->ty_Visibility);
1083 * For TY_IMPORT, leave et_SemGroup alone?
1088 type = TypeToRawPtrType(DupType(sg, stype->ty_RawPtrType.et_Type));
1091 type = TypeToRefType(DupType(sg, stype->ty_RefType.et_Type));
1095 * When redeclaring an array, the semantic context if the
1096 * [exp] needs to be able to see refined elements in subclasses,
1097 * even if the array declaration itself has not been refined.
1099 * XXX however, this may make other elements in the subclass
1100 * visible to the [exp] that we did not refine (? TEST)
1104 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1105 stype->ty_AryType.et_OrigArySize,
1108 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1109 stype->ty_AryType.et_OrigArySize,
1110 stype->ty_AryType.et_SemGroup);
1112 type->ty_AryType.et_Count = stype->ty_AryType.et_Count;
1116 * varargs (whole thing), requires duplicating the semantic group
1117 * they are contained in too.
1119 type = TypeToVarType(DupType(sg, stype->ty_VarType.et_Type),
1120 DupSemGroup(sg, NULL, stype->ty_VarType.et_SemGroup, 1));
1123 type = AllocCompoundType(DupSemGroup(sg, NULL,
1124 stype->ty_CompType.et_SemGroup, 1));
1128 * Arguments (whole thing), requires duplicating the semantic
1129 * group they are contained in too.
1131 * At the moment we always formally duplicate the arguments so we can
1132 * modify them for methods below.
1134 type = AllocArgsType(DupSemGroup(sg, NULL,
1135 stype->ty_CompType.et_SemGroup, 1));
1141 type = DupType(sg, stype->ty_ProcType.et_RetType);
1142 type = TypeToProcType(type,
1143 DupType(sg, stype->ty_ProcType.et_ArgsType),
1151 * e.g. so elements in a superclass will see refined elements in the
1152 * subclass. Note that the original import semgroup is left intact
1153 * so the semantic search mechanism uses it when the base sg
1154 * (typically a subclass) fails.
1156 type = AllocUnresolvedType(
1157 stype->ty_UnresType.et_ImportSemGroup,
1159 stype->ty_UnresType.et_DottedId,
1163 dassert_type(stype, 0);
1166 if (type != stype) {
1167 type->ty_Flags = stype->ty_Flags &
1168 ~(TF_ISINTERNAL | TF_RESOLVING | TF_RESOLVED |
1169 TF_ALIGNRESOLVED | TF_TMPRESOLVED);
1170 type->ty_Bytes = stype->ty_Bytes;
1171 type->ty_AlignMask = stype->ty_AlignMask;
1172 type->ty_Visibility = stype->ty_Visibility;
1173 type->ty_DynamicVector = stype->ty_DynamicVector;
1175 if (stype->ty_OrigAssExp || stype->ty_SQFlags != type->ty_SQFlags) {
1176 type = TypeToQualType(type, NULL,
1177 stype->ty_SQFlags, stype->ty_OrigAssExp);
1183 typereglist_t TypeRegList = RUNE_HEAD_INITIALIZER(TypeRegList);
1186 InternalRegisterType(const char *str, const char *linkname, Type *type)
1190 tr = zalloc(sizeof(TypeRegNode));
1191 tr->tr_Id = runeid_hash(str, strlen(str));
1193 tr->tr_LinkName = linkname;
1194 RUNE_INSERT_TAIL(&TypeRegList, tr, tr_Node);
1198 InternalRegisteredTypeLookup(runeid_t id)
1202 RUNE_FOREACH(tr, &TypeRegList, tr_Node) {
1203 if (tr->tr_Id == id)
1204 return (tr->tr_Type);