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 * */
52 static List DynamicTypeList = INITLIST(DynamicTypeList);
53 static List CompoundTypeList = INITLIST(CompoundTypeList);
54 static List ArgsTypeList = INITLIST(ArgsTypeList);
55 static List StorageTypeList = INITLIST(StorageTypeList);
57 static void initClassType(Type *type, Declaration *d);
60 initType(Type *type, List *list, int op)
63 initList(&type->ty_QList);
65 addTail(list, &type->ty_Node);
66 type->ty_SQList = list;
70 initQualType(Type *type, List *list, int op, int sqflags)
72 initType(type, list, op);
73 type->ty_SQFlags = sqflags;
77 initPtrType(Type *type, Type *ptrto, int sqflags)
79 initQualType(type, &ptrto->ty_QList, TY_PTRTO, sqflags);
80 type->ty_PtrType.et_Type = ptrto;
84 initRefType(Type *type, Type *refto, int sqflags)
86 initQualType(type, &refto->ty_QList, TY_REFTO, sqflags);
87 type->ty_RefType.et_Type = refto;
93 initQualType(&DynamicLValueType, &DynamicTypeList,
94 TY_DYNAMIC, SF_LVALUE);
95 initType(&DynamicRValueType, &DynamicTypeList, TY_DYNAMIC);
96 initType(&NumericType, NULL, TY_UNRESOLVED);
97 initType(&IntegralType, NULL, TY_UNRESOLVED);
98 initType(&SIntegerType, NULL, TY_UNRESOLVED);
99 initType(&UIntegerType, NULL, TY_UNRESOLVED);
100 initType(&FloatType, NULL, TY_UNRESOLVED);
101 initType(&PointerType, NULL, TY_UNRESOLVED);
103 initType(&VoidType, NULL, TY_UNRESOLVED);
104 initType(&BoolType, NULL, TY_UNRESOLVED);
105 initType(&Int8Type, NULL, TY_UNRESOLVED);
106 initType(&UInt8Type, NULL, TY_UNRESOLVED);
107 initType(&Int16Type, NULL, TY_UNRESOLVED);
108 initType(&UInt16Type, NULL, TY_UNRESOLVED);
109 initType(&Int32Type, NULL, TY_UNRESOLVED);
110 initType(&UInt32Type, NULL, TY_UNRESOLVED);
111 initType(&Int64Type, NULL, TY_UNRESOLVED);
112 initType(&UInt64Type, NULL, TY_UNRESOLVED);
114 initType(&IntPtrType, NULL, TY_UNRESOLVED);
115 initType(&UIntPtrType, NULL, TY_UNRESOLVED);
116 initType(&OffType, NULL, TY_UNRESOLVED);
117 initType(&SizeType, NULL, TY_UNRESOLVED);
119 initType(&Float32Type, NULL, TY_UNRESOLVED);
120 initType(&Float64Type, NULL, TY_UNRESOLVED);
121 initType(&Float128Type, NULL, TY_UNRESOLVED);
123 initQualType(&CCharType, NULL, TY_UNRESOLVED, SF_CONST);
124 initPtrType(&StrType, &CCharType, 0);
125 initPtrType(&CharPtrType, &UInt8Type, 0);
126 initPtrType(&CharPtrPtrType, &CharPtrType, 0);
127 initPtrType(&VoidPtrType, &VoidType, 0);
128 initRefType(&VoidRefType, &VoidType, 0);
129 initPtrType(&CVoidPtrType, &VoidType, SF_CONST);
130 initPtrType(&LVoidPtrType, &VoidType, SF_LVALUE);
132 StrTableAlloc("void", 4, SPECIAL_INTERNAL_VOID);
133 StrTableAlloc("bool", 4, SPECIAL_INTERNAL_BOOL);
134 StrTableAlloc("int8_t", 6, SPECIAL_INTERNAL_INT8);
135 StrTableAlloc("uint8_t", 7, SPECIAL_INTERNAL_UINT8);
136 StrTableAlloc("int16_t", 7, SPECIAL_INTERNAL_INT16);
137 StrTableAlloc("uint16_t", 8, SPECIAL_INTERNAL_UINT16);
138 StrTableAlloc("int32_t", 7, SPECIAL_INTERNAL_INT32);
139 StrTableAlloc("uint32_t", 8, SPECIAL_INTERNAL_UINT32);
140 StrTableAlloc("int64_t", 7, SPECIAL_INTERNAL_INT64);
141 StrTableAlloc("uint64_t", 8, SPECIAL_INTERNAL_UINT64);
142 StrTableAlloc("int128_t", 8, SPECIAL_INTERNAL_INT128);
143 StrTableAlloc("uint128_t", 9, SPECIAL_INTERNAL_UINT128);
145 StrTableAlloc("float32_t", 9, SPECIAL_INTERNAL_FLOAT32);
146 StrTableAlloc("float64_t", 9, SPECIAL_INTERNAL_FLOAT64);
147 StrTableAlloc("float128_t", 10, SPECIAL_INTERNAL_FLOAT128);
149 StrTableAlloc("intptr_t", 8, SPECIAL_INTERNAL_INTPTR);
150 StrTableAlloc("uintptr_t", 9, SPECIAL_INTERNAL_UINTPTR);
151 StrTableAlloc("size_t", 6, SPECIAL_INTERNAL_SIZE);
152 StrTableAlloc("off_t", 5, SPECIAL_INTERNAL_OFF);
154 StrTableAlloc("Float", 5, SPECIAL_INTERNAL_FLOAT);
155 StrTableAlloc("Pointer", 7, SPECIAL_INTERNAL_POINTER);
156 StrTableAlloc("Numeric", 7, SPECIAL_INTERNAL_NUMERIC);
157 StrTableAlloc("Integral", 8, SPECIAL_INTERNAL_INTEGRAL);
158 StrTableAlloc("SInteger", 8, SPECIAL_INTERNAL_SINTEGER);
159 StrTableAlloc("UInteger", 8, SPECIAL_INTERNAL_UINTEGER);
161 StrTableAlloc("__count", 7, SPECIAL_COUNT);
162 StrTableAlloc("__data", 6, SPECIAL_DATA);
163 StrTableAlloc("__varcount", 10, SPECIAL_VAR_COUNT);
164 StrTableAlloc("__vardata", 9, SPECIAL_VAR_DATA);
165 StrTableAlloc("__typeid", 8, SPECIAL_TYPEID);
166 StrTableAlloc("__typestr", 9, SPECIAL_TYPESTR);
167 StrTableAlloc("NULL", 4, SPECIAL_NULL);
171 * Attach an internal class, creating a global summary type for it that
172 * allows our interpreter and code generator to make various assumptions.
175 InternalClassAttach(Parse *p __unused, int t, Declaration *d)
180 dassert_decl(d, d->d_Op == DOP_CLASS);
182 if ((s = StrTableSpecial(d->d_Id)) & SPECIALF_INTERNAL) {
184 case SPECIAL_INTERNAL_VOID:
187 case SPECIAL_INTERNAL_BOOL:
190 case SPECIAL_INTERNAL_INT8:
193 case SPECIAL_INTERNAL_UINT8:
196 case SPECIAL_INTERNAL_INT16:
199 case SPECIAL_INTERNAL_UINT16:
202 case SPECIAL_INTERNAL_INT32:
205 case SPECIAL_INTERNAL_UINT32:
208 case SPECIAL_INTERNAL_INT64:
211 case SPECIAL_INTERNAL_UINT64:
214 case SPECIAL_INTERNAL_INT128:
217 case SPECIAL_INTERNAL_UINT128:
218 itype = &UInt128Type;
221 case SPECIAL_INTERNAL_FLOAT:
223 * Special flag helper (resolver sets TF_ISFLOATING
227 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISFLOATING;
229 case SPECIAL_INTERNAL_FLOAT32:
230 itype = &Float32Type;
232 case SPECIAL_INTERNAL_FLOAT64:
233 itype = &Float64Type;
235 case SPECIAL_INTERNAL_FLOAT128:
236 itype = &Float128Type;
239 case SPECIAL_INTERNAL_INTPTR:
242 case SPECIAL_INTERNAL_UINTPTR:
243 itype = &UIntPtrType;
245 case SPECIAL_INTERNAL_OFF:
248 case SPECIAL_INTERNAL_SIZE:
251 /* NOTE: There is no ssize_t in rune. size_t is signed */
253 case SPECIAL_INTERNAL_POINTER:
254 itype = &PointerType;
256 case SPECIAL_INTERNAL_NUMERIC:
257 itype = &NumericType;
259 case SPECIAL_INTERNAL_INTEGRAL:
260 itype = &IntegralType;
262 case SPECIAL_INTERNAL_SINTEGER:
263 itype = &SIntegerType;
265 case SPECIAL_INTERNAL_UINTEGER:
267 * Special flag helper (resolver sets TF_ISUNSIGNED
268 * in the type in the resolver).
270 itype = &UIntegerType;
271 d->d_ClassDecl.ed_SemGroup->sg_Flags |= SGF_ISUNSIGNED;
278 initClassType(itype, d);
281 * Fixup for const int8 pointers... we did not have
282 * a QList to put CCharType on until now. It will
283 * wind up on the SemGroup's sg_ClassList.
285 if (itype == &UInt8Type) {
286 TypeToQualType(itype, &CCharType,
287 itype->ty_SQFlags | SF_CONST, NULL);
294 * This is mostly deprecated except for official type aliases such as
298 InternalTypeAttach(Parse *p, int t, Declaration *d)
303 dassert_decl(d, d->d_Op == DOP_TYPEDEF);
305 if ((s = StrTableSpecial(d->d_Id)) & SPECIALF_INTERNAL) {
307 case SPECIAL_INTERNAL_VOID:
310 case SPECIAL_INTERNAL_BOOL:
313 case SPECIAL_INTERNAL_INT8:
316 case SPECIAL_INTERNAL_UINT8:
319 case SPECIAL_INTERNAL_INT16:
322 case SPECIAL_INTERNAL_UINT16:
325 case SPECIAL_INTERNAL_INT32:
328 case SPECIAL_INTERNAL_UINT32:
331 case SPECIAL_INTERNAL_INT64:
334 case SPECIAL_INTERNAL_UINT64:
338 case SPECIAL_INTERNAL_FLOAT32:
339 itype = &Float32Type;
341 case SPECIAL_INTERNAL_FLOAT64:
342 itype = &Float64Type;
344 case SPECIAL_INTERNAL_FLOAT128:
345 itype = &Float128Type;
348 case SPECIAL_INTERNAL_INTPTR:
351 case SPECIAL_INTERNAL_UINTPTR:
352 itype = &UIntPtrType;
354 case SPECIAL_INTERNAL_OFF:
357 case SPECIAL_INTERNAL_SIZE:
360 /* NOTE: There is no ssize_t in rune. size_t is signed */
362 case SPECIAL_INTERNAL_POINTER:
363 itype = &PointerType;
365 case SPECIAL_INTERNAL_NUMERIC:
366 itype = &NumericType;
368 case SPECIAL_INTERNAL_INTEGRAL:
369 itype = &IntegralType;
371 case SPECIAL_INTERNAL_SINTEGER:
372 itype = &SIntegerType;
374 case SPECIAL_INTERNAL_UINTEGER:
375 itype = &UIntegerType;
378 itype = InternalRegisteredTypeLookup(d->d_Id);
385 if (itype->ty_Op != TY_UNRESOLVED) {
386 t = LexError(&p->p_Token, TOK_ERR_DUPLICATE_ATTACH);
388 Type *ntype = d->d_TypedefDecl.ed_Type;
390 TypeToQualType(ntype, itype, ntype->ty_SQFlags, NULL);
393 t = LexError(&p->p_Token, TOK_ERR_UNRECOGNIZED_ATTACH);
399 AllocType(List *list, int op)
401 Type *type = zalloc(sizeof(Type));
403 initType(type, list, op);
408 * XXX match the compound type(s)
410 * May be used to generate a varargs compound type, in which case the
411 * semgroup may already be resolved.
414 AllocCompoundType(SemGroup *sg)
418 type = AllocType(&CompoundTypeList, TY_COMPOUND);
419 type->ty_CompType.et_SemGroup = sg;
420 dassert((sg->sg_Flags & SGF_RESOLVED) == 0);
425 * XXX match the compound type(s)
428 AllocArgsType(SemGroup *sg)
432 type = AllocType(&ArgsTypeList, TY_ARGS);
433 type->ty_ArgsType.et_SemGroup = sg;
438 AllocStorageType(runesize_t bytes)
443 type = getHead(&StorageTypeList);
445 type = getSucc(&StorageTypeList, &type->ty_Node)
447 if (type->ty_Op == TY_STORAGE &&
448 type->ty_StorType.et_Bytes == bytes
453 type = AllocType(&StorageTypeList, TY_STORAGE);
454 type->ty_StorType.et_Bytes = bytes;
459 AllocUnresolvedType(SemGroup *isg, SemGroup *sg, string_t *ary, int eatAry)
463 dassert_semgrp(sg, ary != NULL);
466 type = getHead(&sg->sg_ClassList);
468 type = getSucc(&sg->sg_ClassList, &type->ty_Node)
472 if (type->ty_Op != TY_UNRESOLVED)
474 if (type->ty_UnresType.et_ImportSemGroup != isg)
477 for (i = 0; ary[i]; ++i) {
478 if (ary[i] != type->ty_UnresType.et_DottedId[i])
481 if (ary[i] == NULL &&
482 type->ty_UnresType.et_DottedId[i] == NULL) {
488 type = AllocType((sg ? &sg->sg_ClassList : NULL), TY_UNRESOLVED);
489 type->ty_UnresType.et_DottedId = ary;
490 type->ty_UnresType.et_SemGroup = sg; /* may be NULL */
491 type->ty_UnresType.et_ImportSemGroup = isg; /* may be NULL */
496 * AllocClassType() - allocate a type representing a the semgroup which
497 * in turn represents (typically) a class.
500 AllocClassType(List *list, Type *super, SemGroup *sg, int visibility)
504 for (type = getHead(list); type; type = getSucc(list, &type->ty_Node)) {
505 if (type->ty_Op == TY_CLASS &&
506 type->ty_ClassType.et_SemGroup == sg &&
507 type->ty_ClassType.et_Super == super &&
508 type->ty_Visibility == visibility
513 type = AllocType(list, TY_CLASS);
514 type->ty_ClassType.et_SemGroup = sg;
515 type->ty_ClassType.et_Super = super;
516 type->ty_Visibility = visibility;
522 initClassType(Type *type, Declaration *d)
524 initType(type, &d->d_ClassDecl.ed_SemGroup->sg_ClassList, TY_CLASS);
525 type->ty_ClassType.et_SemGroup = d->d_ClassDecl.ed_SemGroup;
526 type->ty_ClassType.et_Super = d->d_ClassDecl.ed_Super;
527 type->ty_Visibility = d->d_ScopeFlags & SCOPE_ALL_VISIBLE;
531 AllocImportType(List *list, SemGroup *sg, int visibility)
533 Type *type = AllocType(list, TY_IMPORT);
535 type->ty_ImportType.et_SemGroup = sg;
536 type->ty_Visibility = visibility;
541 TypeToQualType(Type *otype, Type *ntype, int sqFlags, Exp *exp)
544 * Combine with existing qualifiers, Shortcut if no changes made.
547 sqFlags == otype->ty_SQFlags &&
548 (exp == NULL || exp == otype->ty_AssExp)
554 * See if we already have a matching qualified type (only if storage
555 * for the new type is not being provided). Note: the provided storage
556 * has already been initType()d
559 for (ntype = getHead(otype->ty_SQList);
561 ntype = getSucc(otype->ty_SQList, &ntype->ty_Node)
563 if (ntype->ty_Op == otype->ty_Op &&
564 ntype->ty_SQFlags == sqFlags &&
565 (exp == NULL || ntype->ty_AssExp == exp)
567 if (SameType(ntype, otype, sqFlags))
574 * Build a new qualified type and set its qualifiers, then duplicate
575 * appropriate sections of the old type.
578 ntype = AllocType(otype->ty_SQList, otype->ty_Op);
579 } else if (ntype->ty_SQList == NULL) {
580 ntype->ty_SQList = otype->ty_SQList;
581 addTail(ntype->ty_SQList, &ntype->ty_Node);
585 * Set the op and the expression. Unlike SQFlags, if exp is passed as
586 * NULL we inherit the old type's default.
588 * The DupExp() call here is special, see DupExp()'s handling of
591 * Normally DupExp() is called during resolution prior to ex_Decl
592 * being set. This is the one case where it may be called with
593 * ex_Decl already set.
595 * WARNING! We do not try to resolve the type here. Various resolve
596 * related flags in ty_Flags will be resolved later. This
597 * includes TF_ISUNSIGNED and other TF_* flags.
599 ntype->ty_Op = otype->ty_Op;
601 ntype->ty_AssExp = exp;
602 else if (otype->ty_AssExp)
603 ntype->ty_AssExp = SetDupExp(NULL, otype->ty_AssExp);
604 ntype->ty_SQFlags = sqFlags;
605 ntype->ty_Visibility = otype->ty_Visibility;
607 switch(otype->ty_Op) {
609 ntype->ty_ClassType.et_SemGroup =
610 otype->ty_ClassType.et_SemGroup;
611 ntype->ty_ClassType.et_Super = otype->ty_ClassType.et_Super;
614 ntype->ty_Visibility = otype->ty_Visibility;
615 ntype->ty_ImportType.et_SemGroup = otype->ty_ImportType.et_SemGroup;
618 ntype->ty_CPtrType.et_Type = otype->ty_CPtrType.et_Type;
621 ntype->ty_PtrType.et_Type = otype->ty_PtrType.et_Type;
624 ntype->ty_RefType.et_Type = otype->ty_RefType.et_Type;
628 * note: multiple type structures may share the same array size
629 * expression in simple qualified-type cases. YYY XXX bad bad.
631 ntype->ty_AryType.et_Type = otype->ty_AryType.et_Type;
632 ntype->ty_AryType.et_ArySize = otype->ty_AryType.et_ArySize;
633 ntype->ty_AryType.et_SemGroup = otype->ty_AryType.et_SemGroup;
636 ntype->ty_CompType.et_SemGroup = otype->ty_CompType.et_SemGroup;
639 ntype->ty_VarType.et_Type = otype->ty_VarType.et_Type;
640 ntype->ty_VarType.et_SemGroup = otype->ty_VarType.et_SemGroup;
643 ntype->ty_ArgsType.et_SemGroup = otype->ty_ArgsType.et_SemGroup;
646 ntype->ty_ProcType.et_ArgsType = otype->ty_ProcType.et_ArgsType;
647 ntype->ty_ProcType.et_RetType = otype->ty_ProcType.et_RetType;
648 ntype->ty_ProcType.et_ArgCount = otype->ty_ProcType.et_ArgCount;
651 ntype->ty_StorType.et_Bytes = otype->ty_StorType.et_Bytes;
655 * It is not legal to qualify a dynamic type other then to
656 * add or remove SF_LVALUE.
661 ntype->ty_UnresType.et_DottedId =
662 otype->ty_UnresType.et_DottedId;
663 ntype->ty_UnresType.et_SemGroup =
664 otype->ty_UnresType.et_SemGroup;
667 dassert_type(otype, 0);
673 * XXX match the procedure type(s)
676 TypeToProcType(Type *rtype, Type *atype)
683 dassert_type(atype, atype->ty_Op == TY_ARGS);
684 sg = atype->ty_CompType.et_SemGroup;
687 d = getHead(&sg->sg_DeclList);
689 d = getSucc(&sg->sg_DeclList, &d->d_Node)
694 type = getHead(&rtype->ty_QList);
696 type = getSucc(&rtype->ty_QList, &type->ty_Node)
698 if (type->ty_Op == TY_PROC) {
699 if (type->ty_ProcType.et_ArgsType == atype &&
700 type->ty_ProcType.et_RetType == rtype &&
701 type->ty_ProcType.et_ArgCount == count
703 puts("SG1"); /* YYY */
708 type = AllocType(&rtype->ty_QList, TY_PROC);
709 type->ty_ProcType.et_ArgsType = AllocArgsType(sg);
710 type->ty_ProcType.et_RetType = rtype;
711 type->ty_ProcType.et_ArgCount = count;
716 * Convert type to pointer-to-type
719 TypeToPtrType(Type *otype)
723 for (type = getHead(&otype->ty_QList);
725 type = getSucc(&otype->ty_QList, &type->ty_Node)
727 if (type->ty_Op == TY_PTRTO)
730 type = AllocType(&otype->ty_QList, TY_PTRTO);
731 type->ty_PtrType.et_Type = otype;
736 * Convert type to pointer-to-type
739 TypeToCPtrType(Type *otype)
743 for (type = getHead(&otype->ty_QList);
745 type = getSucc(&otype->ty_QList, &type->ty_Node)
747 if (type->ty_Op == TY_CPTRTO)
750 type = AllocType(&otype->ty_QList, TY_CPTRTO);
751 type->ty_CPtrType.et_Type = otype;
756 * Convert type to ref-to-type
758 * A reference type is similar to a pointer type except that the
759 * resolver is not able to entirely know what it is pointing to.
760 * The reference type is a superclass, but the actual type is
761 * stored in the run-time structure.
764 TypeToRefType(Type *otype)
768 for (type = getHead(&otype->ty_QList);
770 type = getSucc(&otype->ty_QList, &type->ty_Node)
772 if (type->ty_Op == TY_REFTO)
775 type = AllocType(&otype->ty_QList, TY_REFTO);
776 type->ty_RefType.et_Type = otype;
781 TypeToAryType(Type *otype, Exp *exp, SemGroup *sg)
786 * XXX handle constant expression optimization for QList
787 * XXX handle qualifiers
789 type = AllocType(&otype->ty_QList, TY_ARYOF);
790 type->ty_AryType.et_ArySize = exp;
791 type->ty_AryType.et_Type = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
792 type->ty_AryType.et_SemGroup = sg;
793 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
800 TypeToRunTimeAryType(Type *otype, int count)
803 Type *t2 = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
805 for (type = getHead(&otype->ty_QList);
807 type = getSucc(&otype->ty_QList, &type->ty_Node)
809 if (type->ty_Op == TY_ARYOF &&
810 type->ty_AryType.et_Type == t2 &&
811 type->ty_AryType.et_Count == count &&
813 (otype->ty_SQFlags & SF_MASK_ARY_INHERIT)
818 type = AllocType(&otype->ty_QList, TY_ARYOF);
819 type->ty_AryType.et_Count = count;
820 type->ty_AryType.et_Type = t2;
821 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
828 TypeToVarType(Type *otype, SemGroup *sg)
832 dassert(sg->sg_Flags & SGF_RESOLVED);
833 dassert(otype->ty_Flags & TF_RESOLVED);
835 for (type = getHead(&otype->ty_QList);
837 type = getSucc(&otype->ty_QList, &type->ty_Node)
839 if (type->ty_Op == TY_VAR &&
840 type->ty_VarType.et_Type == otype &&
841 type->ty_VarType.et_SemGroup == sg
843 puts("SG2"); /* YYY */
847 type = AllocType(&otype->ty_QList, TY_VAR);
848 type->ty_VarType.et_Type = otype;
849 type->ty_VarType.et_SemGroup = sg;
851 /* XXX doesn't work for var-args */
852 if (sg->sg_Flags & SGF_RESOLVED) {
853 type->ty_Flags |= TF_RESOLVED;
854 type->ty_Bytes = sg->sg_Bytes;
855 type->ty_AlignMask = sg->sg_AlignMask;
862 * ChangeType() - given pointer, C pointer, or array of something,
863 * return 'op' of something instead.
866 ChangeType(Type *type, int op)
868 switch(type->ty_Op) {
872 type = TypeToCPtrType(type->ty_PtrType.et_Type);
875 type = TypeToAryType(type->ty_PtrType.et_Type,
885 type = TypeToPtrType(type->ty_CPtrType.et_Type);
888 type = TypeToAryType(type->ty_CPtrType.et_Type,
898 type = TypeToPtrType(type->ty_AryType.et_Type);
901 type = TypeToCPtrType(type->ty_AryType.et_Type);
914 * BaseType() - return base type
916 * Traverse the type to locate the base type. Store the base type
917 * in *ptype and return the SemGroup, or return NULL if the base type
918 * does not have a SemGroup.
921 BaseType(Type **ptype)
926 switch(type->ty_Op) {
928 type = type->ty_CPtrType.et_Type;
931 type = type->ty_PtrType.et_Type;
934 type = type->ty_RefType.et_Type;
937 type = type->ty_AryType.et_Type;
945 switch(type->ty_Op) {
947 return(type->ty_ClassType.et_SemGroup);
949 return(type->ty_CompType.et_SemGroup);
951 return(type->ty_ArgsType.et_SemGroup);
959 dassert_type(type, 0);
960 return(NULL); /* avoid compiler complaints */
965 * MatchType() - Match two types
967 * Match two types as if we wanted to cast type to super or use
970 * SG_COMPAT_FULL Type is a subclass, methods and storage are
971 * compatible (storage may be extended).
973 * SG_COMPAT_PART Type is a subclass, methods are compatible
974 * but storage is not.
976 * SG_COMPAT_SUBCLASS Type is a subclass, but the methods are
977 * not directly compatible (the methods that
978 * propogate down must be regenerated).
979 * SG_COMPAT_FAIL Type is not even a subclass
981 * XXX we are skipping qualifiers
984 MatchType(Type *super, Type *type)
986 int r = SG_COMPAT_FULL;
988 while (super && type) {
989 SemGroup *sg1 = NULL;
990 SemGroup *sg2 = NULL;
992 if (type->ty_Op != super->ty_Op) {
993 if (super->ty_Op == TY_REFTO &&
994 type->ty_Op == TY_PTRTO) {
995 super = super->ty_RefType.et_Type;
996 type = type->ty_PtrType.et_Type;
997 r = MatchType(super, type);
1005 * Relaxed storage qualifiers. The less refined target type
1006 * (super) can omit certain storage qualifiers and still match.
1008 * NOTE: Actual casts may do more stringent tests.
1010 if (super->ty_SQFlags != type->ty_SQFlags) {
1011 if ((super->ty_SQFlags & ~(SF_LVALUE)) !=
1012 (type->ty_SQFlags & ~SF_RELAXING_MASK)) {
1018 switch(super->ty_Op) {
1021 * type can be a subclass of super
1023 if (type->ty_ClassType.et_SemGroup ==
1024 super->ty_ClassType.et_SemGroup) {
1027 r = type->ty_ClassType.et_SemGroup->sg_Compat;
1029 if (r < SG_COMPAT_PART)
1032 while ((type = type->ty_ClassType.et_Super) != NULL) {
1033 if (type->ty_ClassType.et_SemGroup ==
1034 super->ty_ClassType.et_SemGroup) {
1037 if (r < type->ty_ClassType.et_SemGroup->sg_Compat)
1038 r = type->ty_ClassType.et_SemGroup->sg_Compat;
1040 if (type == NULL) /* not even a subclass */
1045 * type can be a subclass of super
1051 type = type->ty_CPtrType.et_Type;
1052 super = super->ty_CPtrType.et_Type;
1055 type = type->ty_PtrType.et_Type;
1056 super = super->ty_PtrType.et_Type;
1059 type = type->ty_RefType.et_Type;
1060 super = super->ty_RefType.et_Type;
1063 type = type->ty_AryType.et_Type;
1064 super = super->ty_AryType.et_Type;
1068 sg1 = super->ty_CompType.et_SemGroup;
1069 sg2 = type->ty_CompType.et_SemGroup;
1072 sg1 = super->ty_ArgsType.et_SemGroup;
1073 sg2 = type->ty_ArgsType.et_SemGroup;
1076 r = MatchType(super->ty_VarType.et_Type,
1077 type->ty_VarType.et_Type);
1083 r = MatchType(super->ty_ProcType.et_ArgsType,
1084 type->ty_ProcType.et_ArgsType);
1085 v = MatchType(super->ty_ProcType.et_RetType,
1086 type->ty_ProcType.et_RetType);
1094 if (type->ty_StorType.et_Bytes !=
1095 super->ty_StorType.et_Bytes) {
1096 r = SG_COMPAT_SUBCLASS;
1101 dassert_type(super, 0); /* can't happen */
1105 Declaration *sd = getHead(&sg1->sg_DeclList);
1106 Declaration *rd = getHead(&sg2->sg_DeclList);
1108 int v = MatchDeclTypes(sd, rd);
1111 if (r == SG_COMPAT_FAIL)
1113 sd = getSucc(&sg1->sg_DeclList, &sd->d_Node);
1114 rd = getSucc(&sg2->sg_DeclList, &rd->d_Node);
1125 * DupType() - create a duplicate of a type, possibly in a new SemGroup.
1127 * This code is used when duplicating procedures and other elements
1128 * when merging a superclass into a subclass.
1130 * If sg is NULL, stype is simply returned. The case is used when we
1131 * try to duplciate an expression with DupExp()... in that case we
1132 * want to dup the expression tree but use the same types.
1135 DupType(SemGroup *sg, Type *stype)
1143 * XXX type may be resolved if it is part of a varargs dup
1147 (stype->ty_Flags & (TF_RESOLVED|TF_RESOLVING)) == 0);
1150 switch(stype->ty_Op) {
1153 * This only occurs because the resolver has resolved an
1154 * unresolved type on the original SemGroup. We duplicate
1155 * that on the new SemGroup.
1157 type = AllocClassType(&sg->sg_ClassList,
1158 stype->ty_ClassType.et_Super,
1159 stype->ty_ClassType.et_SemGroup,
1160 stype->ty_Visibility);
1167 type = TypeToCPtrType(DupType(sg, stype->ty_CPtrType.et_Type));
1170 type = TypeToPtrType(DupType(sg, stype->ty_PtrType.et_Type));
1173 type = TypeToRefType(DupType(sg, stype->ty_RefType.et_Type));
1176 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1177 SetDupExp(sg, stype->ty_AryType.et_ArySize),
1178 stype->ty_AryType.et_SemGroup);
1181 type = TypeToVarType(DupType(sg, stype->ty_VarType.et_Type),
1182 DupSemGroup(sg, NULL,
1183 stype->ty_VarType.et_SemGroup, 1));
1186 type = AllocCompoundType(
1187 DupSemGroup(sg, NULL,
1188 stype->ty_CompType.et_SemGroup, 1));
1192 * At the moment we always formally duplicate the arguments
1193 * so we can modify them for methods below.
1195 type = AllocArgsType(
1196 DupSemGroup(sg, NULL,
1197 stype->ty_CompType.et_SemGroup, 1));
1200 type = DupType(sg, stype->ty_ProcType.et_RetType);
1201 type = TypeToProcType(type,
1203 stype->ty_ProcType.et_ArgsType));
1206 * If this is a method procedure, we have to change the
1207 * first argument to point at our new subclass. It was
1208 * previously pointing at our superclass. XXX the
1209 * programmer can override the argument. If it isn't a
1210 * reference we have to assert.
1212 if (stype->ty_SQFlags & SF_METHOD) {
1213 SemGroup *asg = type->ty_ProcType.et_ArgsType->ty_ArgsType.et_SemGroup;
1214 Declaration *d = getHead(&asg->sg_DeclList);
1215 Type *thisType = d->d_StorDecl.ed_Type;
1217 dassert_decl(d, d->d_Id == String_This &&
1218 d->d_Op == DOP_ARGS_STORAGE);
1219 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1220 if (thisType->ty_Op == TY_CLASS) {
1221 /* XXX sg_ClassList? right sg? */
1222 /* XXX correct visibility? */
1223 if (d->d_Search == NULL) {
1224 d->d_Search = d->d_StorDecl.ed_Type->
1225 ty_ClassType.et_SemGroup;
1227 d->d_StorDecl.ed_Type =
1228 AllocClassType(&sg->sg_ClassList,
1229 sg->sg_Stmt->st_ClassStmt.es_Super,
1230 sg->sg_Stmt->st_MyGroup,
1233 dassert_decl(d, thisType->ty_Op == TY_REFTO);
1235 } else if (stype->ty_SQFlags & SF_GMETHOD) {
1239 asg = type->ty_ProcType.et_ArgsType->
1240 ty_ArgsType.et_SemGroup;
1241 d = getHead(&asg->sg_DeclList);
1243 dassert_decl(d, d->d_Id == String_This &&
1244 d->d_Op == DOP_TYPEDEF);
1245 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1246 dassert_decl(d, d->d_TypedefDecl.ed_Type->ty_Op ==
1248 /* XXX sg_ClassList? right sg? */
1249 /* XXX correct visibility? */
1250 if (d->d_Search == NULL) {
1251 d->d_Search = d->d_TypedefDecl.ed_Type->
1252 ty_ClassType.et_SemGroup;
1254 d->d_TypedefDecl.ed_Type =
1255 AllocClassType(&sg->sg_ClassList,
1256 sg->sg_Stmt->st_ClassStmt.es_Super,
1257 sg->sg_Stmt->st_MyGroup,
1266 * e.g. so elements in a superclass will see refined elements
1267 * in the subclass. Note that the original import semgroup
1268 * is left intact so the semantic search mechanism uses it
1269 * when the base sg (typically a subclass) fails.
1271 type = AllocUnresolvedType(
1272 stype->ty_UnresType.et_ImportSemGroup,
1274 stype->ty_UnresType.et_DottedId,
1278 dassert_type(stype, 0);
1281 if (type != stype) {
1282 type->ty_Flags = stype->ty_Flags;
1283 type->ty_Bytes = stype->ty_Bytes;
1284 type->ty_AlignMask = stype->ty_AlignMask;
1285 type->ty_Visibility = stype->ty_Visibility;
1287 if (stype->ty_AssExp || stype->ty_SQFlags != type->ty_SQFlags) {
1288 type = TypeToQualType(type, NULL,
1290 SetDupExp(sg, stype->ty_AssExp));
1297 * SameType() - return 1 if the types are equilvalent, 0 if they are not.
1299 * The sqFlags for t2 may be overriden. If you do not wish to override
1300 * the sqFlags for t2, pass t2->ty_SQFlags for sqFlags. The override
1301 * only applies to the top level of the type.
1303 * Types can be aliased - for example, two different type structures
1304 * may point to the same class data.
1306 * XXX this needs a lot of work. We really need to guarentee
1307 * some level of uniqueness for non-qualified type elements.
1310 SameType(Type *t1, Type *t2, int sqFlags2)
1315 if (t1->ty_Op != t2->ty_Op)
1317 if (t1->ty_SQFlags != sqFlags2)
1321 if (t1->ty_ImportType.et_SemGroup ==
1322 t2->ty_ImportType.et_SemGroup)
1328 if (t1->ty_ClassType.et_SemGroup ==
1329 t2->ty_ClassType.et_SemGroup &&
1330 t1->ty_ClassType.et_Super ==
1331 t2->ty_ClassType.et_Super)
1337 t1 = t1->ty_CPtrType.et_Type;
1338 t2 = t2->ty_CPtrType.et_Type;
1341 t1 = t1->ty_PtrType.et_Type;
1342 t2 = t2->ty_PtrType.et_Type;
1345 t1 = t1->ty_RefType.et_Type;
1346 t2 = t2->ty_RefType.et_Type;
1361 dassert_type(t1, 0);
1364 sqFlags2 = t2->ty_SQFlags;
1370 * SimilarType() - like SameType(), but ignores storage qualifiers and
1371 * if t2 is varargs, compares the original version.
1373 * Used when casting t2 (rhs) to t1 (lhs).
1376 SimilarType(Type *t1, Type *t2)
1378 if (t2->ty_Op == TY_VAR)
1379 t2 = t2->ty_VarType.et_Type;
1385 * Normally we fail if the ops do not match, but it is legal to
1386 * cast a pointer (t2) to a reference type (t1) if the ref type
1387 * is its superclass. It is also legal to cast an array to a
1388 * pointer or C pointer.
1390 if (t2->ty_Op != t1->ty_Op) {
1394 if (t2->ty_Op == TY_PTRTO && t1->ty_Op == TY_REFTO) {
1395 t1 = t1->ty_RefType.et_Type;
1396 t2 = t2->ty_PtrType.et_Type;
1397 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1405 if (t2->ty_Op == TY_ARYOF && t1->ty_Op == TY_PTRTO) {
1406 t1 = t1->ty_PtrType.et_Type;
1407 t2 = t2->ty_AryType.et_Type;
1408 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1416 if (t2->ty_Op == TY_ARYOF && t1->ty_Op == TY_CPTRTO) {
1417 t1 = t1->ty_PtrType.et_Type;
1418 t2 = t2->ty_AryType.et_Type;
1419 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1427 if (t2->ty_Op != t1->ty_Op)
1431 if (t1->ty_ImportType.et_SemGroup ==
1432 t2->ty_ImportType.et_SemGroup) {
1437 if (t1->ty_ClassType.et_SemGroup ==
1438 t2->ty_ClassType.et_SemGroup &&
1439 t1->ty_ClassType.et_Super ==
1440 t2->ty_ClassType.et_Super)
1446 t1 = t1->ty_CPtrType.et_Type;
1447 t2 = t2->ty_CPtrType.et_Type;
1450 t1 = t1->ty_PtrType.et_Type;
1451 t2 = t2->ty_PtrType.et_Type;
1455 * Reference types are similar if the lhs is a
1456 * superclass of the rhs and partially compatible
1457 * (only method call changes and extensions).
1459 t1 = t1->ty_RefType.et_Type;
1460 t2 = t2->ty_RefType.et_Type;
1461 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1468 * Compare the elements making up the compound type.
1471 return(SimilarSemGroup(t1->ty_CompType.et_SemGroup,
1472 t2->ty_CompType.et_SemGroup));
1486 dassert_type(t1, 0);
1494 * SimilarSemGroup() - check to see if the storage underlying the two
1495 * semantic groups is compatible.
1498 SimilarSemGroup(SemGroup *s1, SemGroup *s2)
1503 if (s1->sg_Bytes != s2->sg_Bytes)
1505 d1 = getHead(&s1->sg_DeclList);
1506 d2 = getHead(&s2->sg_DeclList);
1509 ((d1->d_Op & DOPF_STORAGE) == 0 ||
1510 d1->d_Op == DOP_GLOBAL_STORAGE)
1512 d1 = getSucc(&s1->sg_DeclList, &d1->d_Node);
1515 ((d2->d_Op & DOPF_STORAGE) == 0 ||
1516 d2->d_Op == DOP_GLOBAL_STORAGE)
1518 d2 = getSucc(&s2->sg_DeclList, &d2->d_Node);
1520 if (d1 == NULL || d2 == NULL)
1522 if (SimilarType(d1->d_StorDecl.ed_Type,
1523 d2->d_StorDecl.ed_Type) == 0) {
1526 d1 = getSucc(&s1->sg_DeclList, &d1->d_Node);
1527 d2 = getSucc(&s2->sg_DeclList, &d2->d_Node);
1530 return(0); /* compare bad */
1531 return(1); /* compare good */
1535 TypeToStr(Type *type, char **pstr)
1544 static char *SaveStr[8];
1545 static int SaveIndex;
1548 safe_asprintf(&str, "(null)");
1550 switch(type->ty_Op) {
1552 st = type->ty_ClassType.et_SemGroup->sg_Stmt;
1553 dassert(st->st_Op == ST_Class);
1555 safe_asprintf(&str, "CLASS(%s",
1556 st->st_ClassStmt.es_Decl->d_Id);
1558 LexPrintRef(&st->st_LexRef, 0);
1559 safe_asprintf(&str, "CLASS(%s from",
1560 /* type->ty_ClassType.et_SemGroup->sg_Parse, */
1561 st->st_ClassStmt.es_Decl->d_Id);
1563 while (st && st->st_Op != ST_Import)
1567 safe_replacef(&str, "%s <%s%s>",
1569 st->st_ImportStmt.es_Path,
1570 st->st_ImportStmt.es_File);
1574 safe_replacef(&str, "%s)", str);
1577 st = type->ty_ImportType.et_SemGroup->sg_Stmt;
1578 dassert(st->st_Op == ST_Module);
1579 safe_asprintf(&str, "IMPORT(%s)",
1580 st->st_LexRef.lr_Lex->l_Path);
1583 TypeToStr(type->ty_CPtrType.et_Type, &s1);
1584 safe_replacef(&str, "(CLANG)*%s", s1);
1587 TypeToStr(type->ty_PtrType.et_Type, &s1);
1588 safe_replacef(&str, "*%s", s1);
1591 TypeToStr(type->ty_RefType.et_Type, &s1);
1592 safe_replacef(&str, "@%s", s1);
1595 TypeToStr(type->ty_AryType.et_Type, &s1);
1596 safe_replacef(&str, "%s[]", s1);
1599 sg = type->ty_CompType.et_SemGroup;
1600 safe_asprintf(&str, "COMPOUND(");
1602 for (d = getHead(&sg->sg_DeclList);
1604 d = getSucc(&sg->sg_DeclList, &d->d_Node)) {
1605 if (d->d_Op & DOPF_STORAGE) {
1606 TypeToStr(d->d_StorDecl.ed_Type, &s1);
1608 safe_replacef(&str, "%s,%s",
1611 safe_replacef(&str, "%s%s",
1617 safe_replacef(&str, "%s)", str);
1620 safe_replacef(&str, "VAR");
1623 sg = type->ty_ArgsType.et_SemGroup;
1624 safe_asprintf(&str, "ARGS(");
1626 for (d = getHead(&sg->sg_DeclList);
1628 d = getSucc(&sg->sg_DeclList, &d->d_Node)) {
1629 if (d->d_Op & DOPF_STORAGE) {
1630 TypeToStr(d->d_StorDecl.ed_Type, &s1);
1632 safe_replacef(&str, "%s,%s",
1635 safe_replacef(&str, "%s%s",
1641 safe_replacef(&str, "%s)", str);
1645 TypeToStr(type->ty_ProcType.et_RetType, &s1);
1646 TypeToStr(type->ty_ProcType.et_ArgsType, &s2);
1647 safe_asprintf(&str, "%s %s", s1, s2);
1651 safe_asprintf(&str, "STORAGE(%d)",
1652 type->ty_StorType.et_Bytes);
1655 safe_asprintf(&str, "DYNAMIC");
1658 safe_asprintf(&str, "UNRES(");
1659 for (count = 0; type->ty_UnresType.et_DottedId[count]; ++count) {
1660 safe_replacef(&str, "%s%s",
1661 str, type->ty_UnresType.et_DottedId[count]);
1663 safe_replacef(&str, "%s,%s",
1664 str, type->ty_UnresType.et_DottedId[count]);
1667 safe_replacef(&str, "%s)", str);
1670 safe_asprintf(&str, "?");
1673 if (type->ty_SQFlags) {
1676 safe_asprintf(&sqstr, "(%s%s%s%s%s%s%s)",
1677 ((type->ty_SQFlags & SF_CONST) ? "const " : ""),
1678 ((type->ty_SQFlags & SF_VOLATILE) ? "volatile " : ""),
1679 ((type->ty_SQFlags & SF_VARARGS) ? "varargs " : ""),
1680 ((type->ty_SQFlags & SF_LVALUE) ? "lvalue " : ""),
1681 ((type->ty_SQFlags & SF_NOZERO) ? "__nozero " : ""),
1682 ((type->ty_SQFlags & SF_METHOD) ? "method " : ""),
1683 ((type->ty_SQFlags & SF_GMETHOD) ? "global method " : "")
1685 safe_replacef(&str, "%s%s", sqstr, str);
1695 safe_free(&SaveStr[SaveIndex]);
1696 SaveStr[SaveIndex] = str;
1697 SaveIndex = (SaveIndex + 1) % arysize(SaveStr);
1702 typedef struct TypeRegNode {
1708 List TypeRegList = INITLIST(TypeRegList);
1711 InternalRegisterType(const char *str, Type *type)
1713 static int Special = SPECIALF_REGISTERED|SPECIALF_INTERNAL|1;
1716 dassert(Special & SPECIALF_MASK);
1717 tr = zalloc(sizeof(TypeRegNode));
1718 tr->tr_Id = StrTableAlloc(str, strlen(str), Special++);
1720 addTail(&TypeRegList, &tr->tr_Node);
1724 InternalRegisteredTypeLookup(string_t id)
1729 tr = getHead(&TypeRegList);
1731 tr = getSucc(&TypeRegList, &tr->tr_Node)
1733 if (tr->tr_Id == id)
1734 return(tr->tr_Type);