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;
36 Type CCharType; /* const char */
37 Type StrType; /* const char * */
38 Type CharPtrType; /* char * */
39 Type CharPtrPtrType; /* char ** */
40 Type VoidPtrType; /* void * */
41 Type VoidRefType; /* void @ */
42 Type CVoidPtrType; /* const void * */
43 Type LVoidPtrType; /* lvalue void * */
45 Type PointerType; /* generic pointer placeholder */
47 static List DynamicTypeList = INITLIST(DynamicTypeList);
48 static List CompoundTypeList = INITLIST(CompoundTypeList);
49 static List ArgsTypeList = INITLIST(ArgsTypeList);
50 static List StorageTypeList = INITLIST(StorageTypeList);
53 initType(Type *type, List *list, int op)
56 initList(&type->ty_QList);
58 addTail(list, &type->ty_Node);
59 type->ty_SQList = list;
63 initQualType(Type *type, List *list, int op, int sqflags)
65 initType(type, list, op);
66 type->ty_SQFlags = sqflags;
70 initPtrType(Type *type, Type *ptrto, int sqflags)
72 initQualType(type, &ptrto->ty_QList, TY_PTRTO, sqflags);
73 type->ty_PtrType.et_Type = ptrto;
74 type->ty_PtrType.et_RefNull.rs_Type = ptrto;
75 type->ty_PtrType.et_RefNull.rs_Refs = 1;
79 initRefType(Type *type, Type *refto, int sqflags)
81 initQualType(type, &refto->ty_QList, TY_REFTO, sqflags);
82 type->ty_RefType.et_Type = refto;
83 type->ty_RefType.et_RefNull.rs_Type = refto;
84 type->ty_RefType.et_RefNull.rs_Refs = 1;
90 initQualType(&DynamicLValueType, &DynamicTypeList,
91 TY_DYNAMIC, SF_LVALUE);
92 initType(&DynamicRValueType, &DynamicTypeList, TY_DYNAMIC);
93 initType(&NumericType, NULL, TY_UNRESOLVED);
94 initType(&IntegerType, NULL, TY_UNRESOLVED);
95 initType(&VoidType, NULL, TY_UNRESOLVED);
96 initType(&BoolType, NULL, TY_UNRESOLVED);
97 initType(&Int8Type, NULL, TY_UNRESOLVED);
98 initType(&UInt8Type, NULL, TY_UNRESOLVED);
99 initType(&Int16Type, NULL, TY_UNRESOLVED);
100 initType(&UInt16Type, NULL, TY_UNRESOLVED);
101 initType(&Int32Type, NULL, TY_UNRESOLVED);
102 initType(&UInt32Type, NULL, TY_UNRESOLVED);
103 initType(&Int64Type, NULL, TY_UNRESOLVED);
104 initType(&UInt64Type, NULL, TY_UNRESOLVED);
106 initType(&IntPtrType, NULL, TY_UNRESOLVED);
107 initType(&UIntPtrType, NULL, TY_UNRESOLVED);
108 initType(&OffType, NULL, TY_UNRESOLVED);
109 initType(&SizeType, NULL, TY_UNRESOLVED);
110 initType(&SSizeType, NULL, TY_UNRESOLVED);
112 initType(&Float32Type, NULL, TY_UNRESOLVED);
113 initType(&Float64Type, NULL, TY_UNRESOLVED);
114 initType(&Float128Type, NULL, TY_UNRESOLVED);
116 initQualType(&CCharType, NULL, TY_UNRESOLVED, SF_CONST);
117 initPtrType(&StrType, &CCharType, 0);
118 initPtrType(&CharPtrType, &UInt8Type, 0);
119 initPtrType(&CharPtrPtrType, &CharPtrType, 0);
120 initPtrType(&VoidPtrType, &VoidType, 0);
121 initRefType(&VoidRefType, &VoidType, 0);
122 initPtrType(&CVoidPtrType, &VoidType, SF_CONST);
123 initPtrType(&LVoidPtrType, &VoidType, SF_LVALUE);
125 initType(&PointerType, NULL, TY_UNRESOLVED);
127 StrTableAlloc("__internal_void", 15, SPECIAL_INTERNAL_VOID);
128 StrTableAlloc("__internal_bool", 15, SPECIAL_INTERNAL_BOOL);
129 StrTableAlloc("__internal_int8", 15, SPECIAL_INTERNAL_INT8);
130 StrTableAlloc("__internal_uint8", 16, SPECIAL_INTERNAL_UINT8);
131 StrTableAlloc("__internal_int16", 16, SPECIAL_INTERNAL_INT16);
132 StrTableAlloc("__internal_uint16", 17, SPECIAL_INTERNAL_UINT16);
133 StrTableAlloc("__internal_int32", 16, SPECIAL_INTERNAL_INT32);
134 StrTableAlloc("__internal_uint32", 17, SPECIAL_INTERNAL_UINT32);
135 StrTableAlloc("__internal_int64", 16, SPECIAL_INTERNAL_INT64);
136 StrTableAlloc("__internal_uint64", 17, SPECIAL_INTERNAL_UINT64);
138 StrTableAlloc("__internal_float32", 18, SPECIAL_INTERNAL_FLOAT32);
139 StrTableAlloc("__internal_float64", 18, SPECIAL_INTERNAL_FLOAT64);
140 StrTableAlloc("__internal_float128", 19, SPECIAL_INTERNAL_FLOAT128);
142 StrTableAlloc("__internal_pointer", 18, SPECIAL_INTERNAL_POINTER);
143 StrTableAlloc("__internal_intptr", 17, SPECIAL_INTERNAL_INTPTR);
144 StrTableAlloc("__internal_uintptr", 18, SPECIAL_INTERNAL_UINTPTR);
145 StrTableAlloc("__internal_size", 15, SPECIAL_INTERNAL_SIZE);
146 StrTableAlloc("__internal_ssize", 16, SPECIAL_INTERNAL_SSIZE);
147 StrTableAlloc("__internal_off", 14, SPECIAL_INTERNAL_OFF);
149 StrTableAlloc("__internal_numeric", 18, SPECIAL_INTERNAL_NUMERIC);
150 StrTableAlloc("__internal_integer", 18, SPECIAL_INTERNAL_INTEGER);
152 StrTableAlloc("__count", 7, SPECIAL_COUNT);
153 StrTableAlloc("__data", 6, SPECIAL_DATA);
154 StrTableAlloc("__varcount", 10, SPECIAL_VAR_COUNT);
155 StrTableAlloc("__vardata", 9, SPECIAL_VAR_DATA);
156 StrTableAlloc("__typeid", 8, SPECIAL_TYPEID);
157 StrTableAlloc("__typestr", 9, SPECIAL_TYPESTR);
158 StrTableAlloc("NULL", 4, SPECIAL_NULL);
162 InternalTypeAttach(Parse *p, int t, Declaration *d)
167 dassert_decl(d, d->d_Op == DOP_TYPEDEF);
169 if ((s = StrTableSpecial(d->d_Id)) & SPECIALF_INTERNAL) {
171 case SPECIAL_INTERNAL_VOID:
174 case SPECIAL_INTERNAL_BOOL:
177 case SPECIAL_INTERNAL_INT8:
180 case SPECIAL_INTERNAL_UINT8:
183 case SPECIAL_INTERNAL_INT16:
186 case SPECIAL_INTERNAL_UINT16:
189 case SPECIAL_INTERNAL_INT32:
192 case SPECIAL_INTERNAL_UINT32:
195 case SPECIAL_INTERNAL_INT64:
198 case SPECIAL_INTERNAL_UINT64:
202 case SPECIAL_INTERNAL_FLOAT32:
203 itype = &Float32Type;
205 case SPECIAL_INTERNAL_FLOAT64:
206 itype = &Float64Type;
208 case SPECIAL_INTERNAL_FLOAT128:
209 itype = &Float128Type;
212 case SPECIAL_INTERNAL_INTPTR:
215 case SPECIAL_INTERNAL_UINTPTR:
216 itype = &UIntPtrType;
218 case SPECIAL_INTERNAL_OFF:
221 case SPECIAL_INTERNAL_SIZE:
224 case SPECIAL_INTERNAL_SSIZE:
228 case SPECIAL_INTERNAL_POINTER:
229 itype = &PointerType;
231 case SPECIAL_INTERNAL_NUMERIC:
232 itype = &NumericType;
234 case SPECIAL_INTERNAL_INTEGER:
235 itype = &IntegerType;
238 itype = InternalRegisteredTypeLookup(d->d_Id);
245 if (itype->ty_Op != TY_UNRESOLVED) {
246 t = LexError(&p->p_Token, TOK_ERR_DUPLICATE_ATTACH);
248 Type *ntype = d->d_TypedefDecl.ed_Type;
250 TypeToQualType(ntype, itype, ntype->ty_SQFlags, NULL);
253 * fixup for const int8 pointers... we did not have
254 * a QList to put CCharType on until now. It will
255 * wind up on the SemGroup's sg_ClassList.
257 if (itype == &UInt8Type) {
258 TypeToQualType(ntype, &CCharType,
259 ntype->ty_SQFlags | SF_CONST, NULL);
263 t = LexError(&p->p_Token, TOK_ERR_UNRECOGNIZED_ATTACH);
269 AllocType(List *list, int op)
271 Type *type = zalloc(sizeof(Type));
273 initType(type, list, op);
278 * XXX match the compound type(s)
280 * May be used to generate a varargs compound type, in which case the
281 * semgroup may already be resolved.
284 AllocCompoundType(SemGroup *sg)
288 type = AllocType(&CompoundTypeList, TY_COMPOUND);
289 type->ty_CompType.et_SemGroup = sg;
290 dassert((sg->sg_Flags & SGF_RESOLVED) == 0);
295 * XXX match the compound type(s)
298 AllocArgsType(SemGroup *sg)
302 type = AllocType(&ArgsTypeList, TY_ARGS);
303 type->ty_ArgsType.et_SemGroup = sg;
308 AllocStorageType(runesize_t bytes)
313 type = getHead(&StorageTypeList);
315 type = getSucc(&StorageTypeList, &type->ty_Node)
317 if (type->ty_Op == TY_STORAGE &&
318 type->ty_StorType.et_Bytes == bytes
323 type = AllocType(&StorageTypeList, TY_STORAGE);
324 type->ty_StorType.et_Bytes = bytes;
329 AllocUnresolvedType(SemGroup *isg, SemGroup *sg, string_t *ary, int eatAry)
333 dassert_semgrp(sg, ary != NULL);
336 type = getHead(&sg->sg_ClassList);
338 type = getSucc(&sg->sg_ClassList, &type->ty_Node)
342 if (type->ty_Op != TY_UNRESOLVED)
344 if (type->ty_UnresType.et_ImportSemGroup != isg)
347 for (i = 0; ary[i]; ++i) {
348 if (ary[i] != type->ty_UnresType.et_DottedId[i])
351 if (ary[i] == NULL &&
352 type->ty_UnresType.et_DottedId[i] == NULL) {
358 type = AllocType((sg ? &sg->sg_ClassList : NULL), TY_UNRESOLVED);
359 type->ty_UnresType.et_DottedId = ary;
360 type->ty_UnresType.et_SemGroup = sg; /* may be NULL */
361 type->ty_UnresType.et_ImportSemGroup = isg; /* may be NULL */
366 * AllocClassType() - allocate a type representing a the semgroup which
367 * in turn represents (typically) a class.
370 AllocClassType(List *list, Type *super, SemGroup *sg, int visibility)
374 for (type = getHead(list); type; type = getSucc(list, &type->ty_Node)) {
375 if (type->ty_Op == TY_CLASS &&
376 type->ty_ClassType.et_SemGroup == sg &&
377 type->ty_ClassType.et_Super == super &&
378 type->ty_Visibility == visibility
383 type = AllocType(list, TY_CLASS);
384 type->ty_ClassType.et_SemGroup = sg;
385 type->ty_ClassType.et_Super = super;
386 type->ty_Visibility = visibility;
391 AllocImportType(List *list, SemGroup *sg, int visibility)
393 Type *type = AllocType(list, TY_IMPORT);
395 type->ty_ImportType.et_SemGroup = sg;
396 type->ty_Visibility = visibility;
401 TypeToQualType(Type *otype, Type *ntype, int sqFlags, Exp *exp)
404 * Combine with existing qualifiers, Shortcut if no changes made.
407 sqFlags == otype->ty_SQFlags &&
408 (exp == NULL || exp == otype->ty_AssExp)
414 * See if we already have a matching qualified type (only if storage
415 * for the new type is not being provided). Note: the provided storage
416 * has already been initType()d
419 for (ntype = getHead(otype->ty_SQList);
421 ntype = getSucc(otype->ty_SQList, &ntype->ty_Node)
423 if (ntype->ty_Op == otype->ty_Op &&
424 ntype->ty_SQFlags == sqFlags &&
425 (exp == NULL || ntype->ty_AssExp == exp)
427 if (SameType(ntype, otype, sqFlags))
434 * Build a new qualified type and set its qualifiers, then duplicate
435 * appropriate sections of the old type.
438 ntype = AllocType(otype->ty_SQList, otype->ty_Op);
439 } else if (ntype->ty_SQList == NULL) {
440 ntype->ty_SQList = otype->ty_SQList;
441 addTail(ntype->ty_SQList, &ntype->ty_Node);
445 * Set the op and the expression. Unlike SQFlags, if exp is passed as
446 * NULL we inherit the old type's default.
448 * The DupExp() call here is special, see DupExp()'s handling of
451 * Normally DupExp() is called during resolution prior to ex_Decl
452 * being set. This is the one case where it may be called with
453 * ex_Decl already set.
455 ntype->ty_Op = otype->ty_Op;
457 ntype->ty_AssExp = exp;
458 else if (otype->ty_AssExp)
459 ntype->ty_AssExp = SetDupExp(NULL, otype->ty_AssExp);
460 ntype->ty_SQFlags = sqFlags;
461 ntype->ty_Visibility = otype->ty_Visibility;
463 switch(otype->ty_Op) {
465 ntype->ty_ClassType.et_SemGroup =
466 otype->ty_ClassType.et_SemGroup;
467 ntype->ty_ClassType.et_Super = otype->ty_ClassType.et_Super;
470 ntype->ty_Visibility = otype->ty_Visibility;
471 ntype->ty_ImportType.et_SemGroup = otype->ty_ImportType.et_SemGroup;
474 ntype->ty_CPtrType.et_Type = otype->ty_CPtrType.et_Type;
475 ntype->ty_CPtrType.et_RefNull.rs_Type =
476 ntype->ty_CPtrType.et_Type;
477 ntype->ty_CPtrType.et_RefNull.rs_Refs = 1;
480 ntype->ty_PtrType.et_Type = otype->ty_PtrType.et_Type;
481 ntype->ty_PtrType.et_RefNull.rs_Type =
482 ntype->ty_PtrType.et_Type;
483 ntype->ty_PtrType.et_RefNull.rs_Refs = 1;
486 ntype->ty_RefType.et_Type = otype->ty_RefType.et_Type;
487 ntype->ty_RefType.et_RefNull.rs_Type =
488 ntype->ty_RefType.et_Type;
489 ntype->ty_RefType.et_RefNull.rs_Refs = 1;
493 * note: multiple type structures may share the same array size
494 * expression in simple qualified-type cases. YYY XXX bad bad.
496 ntype->ty_AryType.et_Type = otype->ty_AryType.et_Type;
497 ntype->ty_AryType.et_ArySize = otype->ty_AryType.et_ArySize;
498 ntype->ty_AryType.et_SemGroup = otype->ty_AryType.et_SemGroup;
501 ntype->ty_CompType.et_SemGroup = otype->ty_CompType.et_SemGroup;
504 ntype->ty_VarType.et_Type = otype->ty_VarType.et_Type;
505 ntype->ty_VarType.et_SemGroup = otype->ty_VarType.et_SemGroup;
508 ntype->ty_ArgsType.et_SemGroup = otype->ty_ArgsType.et_SemGroup;
511 ntype->ty_ProcType.et_ArgsType = otype->ty_ProcType.et_ArgsType;
512 ntype->ty_ProcType.et_RetType = otype->ty_ProcType.et_RetType;
513 ntype->ty_ProcType.et_ArgCount = otype->ty_ProcType.et_ArgCount;
516 ntype->ty_StorType.et_Bytes = otype->ty_StorType.et_Bytes;
520 * It is not legal to qualify a dynamic type other then to
521 * add or remove SF_LVALUE.
526 ntype->ty_UnresType.et_DottedId =
527 otype->ty_UnresType.et_DottedId;
528 ntype->ty_UnresType.et_SemGroup =
529 otype->ty_UnresType.et_SemGroup;
532 dassert_type(otype, 0);
538 * XXX match the procedure type(s)
541 TypeToProcType(Type *rtype, Type *atype)
548 dassert_type(atype, atype->ty_Op == TY_ARGS);
549 sg = atype->ty_CompType.et_SemGroup;
552 d = getHead(&sg->sg_DeclList);
554 d = getSucc(&sg->sg_DeclList, &d->d_Node)
559 type = getHead(&rtype->ty_QList);
561 type = getSucc(&rtype->ty_QList, &type->ty_Node)
563 if (type->ty_Op == TY_PROC) {
564 if (type->ty_ProcType.et_ArgsType == atype &&
565 type->ty_ProcType.et_RetType == rtype &&
566 type->ty_ProcType.et_ArgCount == count
568 puts("SG1"); /* YYY */
573 type = AllocType(&rtype->ty_QList, TY_PROC);
574 type->ty_ProcType.et_ArgsType = AllocArgsType(sg);
575 type->ty_ProcType.et_RetType = rtype;
576 type->ty_ProcType.et_ArgCount = count;
581 * Convert type to pointer-to-type
584 TypeToPtrType(Type *otype)
588 for (type = getHead(&otype->ty_QList);
590 type = getSucc(&otype->ty_QList, &type->ty_Node)
592 if (type->ty_Op == TY_PTRTO)
595 type = AllocType(&otype->ty_QList, TY_PTRTO);
596 type->ty_PtrType.et_Type = otype;
597 type->ty_PtrType.et_RefNull.rs_Type = otype;
598 type->ty_PtrType.et_RefNull.rs_Refs = 1;
603 * Convert type to pointer-to-type
606 TypeToCPtrType(Type *otype)
610 for (type = getHead(&otype->ty_QList);
612 type = getSucc(&otype->ty_QList, &type->ty_Node)
614 if (type->ty_Op == TY_CPTRTO)
617 type = AllocType(&otype->ty_QList, TY_CPTRTO);
618 type->ty_CPtrType.et_Type = otype;
619 type->ty_CPtrType.et_RefNull.rs_Type = otype;
620 type->ty_CPtrType.et_RefNull.rs_Refs = 1;
625 * Convert type to ref-to-type
627 * A reference type is similar to a pointer type except that the
628 * resolver is not able to entirely know what it is pointing to.
629 * The reference type is a superclass, but the actual type is
630 * stored in the run-time structure.
633 TypeToRefType(Type *otype)
637 for (type = getHead(&otype->ty_QList);
639 type = getSucc(&otype->ty_QList, &type->ty_Node)
641 if (type->ty_Op == TY_REFTO)
644 type = AllocType(&otype->ty_QList, TY_REFTO);
645 type->ty_RefType.et_Type = otype;
646 type->ty_RefType.et_RefNull.rs_Type = otype;
647 type->ty_RefType.et_RefNull.rs_Refs = 1;
652 TypeToAryType(Type *otype, Exp *exp, SemGroup *sg)
657 * XXX handle constant expression optimization for QList
658 * XXX handle qualifiers
660 type = AllocType(&otype->ty_QList, TY_ARYOF);
661 type->ty_AryType.et_ArySize = exp;
662 type->ty_AryType.et_Type = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
663 type->ty_AryType.et_SemGroup = sg;
664 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
671 TypeToRunTimeAryType(Type *otype, int count)
674 Type *t2 = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
676 for (type = getHead(&otype->ty_QList);
678 type = getSucc(&otype->ty_QList, &type->ty_Node)
680 if (type->ty_Op == TY_ARYOF &&
681 type->ty_AryType.et_Type == t2 &&
682 type->ty_AryType.et_Count == count &&
684 (otype->ty_SQFlags & SF_MASK_ARY_INHERIT)
689 type = AllocType(&otype->ty_QList, TY_ARYOF);
690 type->ty_AryType.et_Count = count;
691 type->ty_AryType.et_Type = t2;
692 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
699 TypeToVarType(Type *otype, SemGroup *sg)
703 dassert(sg->sg_Flags & SGF_RESOLVED);
704 dassert(otype->ty_Flags & TF_RESOLVED);
706 for (type = getHead(&otype->ty_QList);
708 type = getSucc(&otype->ty_QList, &type->ty_Node)
710 if (type->ty_Op == TY_VAR &&
711 type->ty_VarType.et_Type == otype &&
712 type->ty_VarType.et_SemGroup == sg
714 puts("SG2"); /* YYY */
718 type = AllocType(&otype->ty_QList, TY_VAR);
719 type->ty_VarType.et_Type = otype;
720 type->ty_VarType.et_SemGroup = sg;
722 /* XXX doesn't work for var-args */
723 if (sg->sg_Flags & SGF_RESOLVED) {
724 type->ty_Flags |= TF_RESOLVED;
725 type->ty_Bytes = sg->sg_Bytes;
726 type->ty_AlignMask = sg->sg_AlignMask;
733 * ChangeType() - given pointer, C pointer, or array of something,
734 * return 'op' of something instead.
737 ChangeType(Type *type, int op)
739 switch(type->ty_Op) {
743 type = TypeToCPtrType(type->ty_PtrType.et_Type);
746 type = TypeToAryType(type->ty_PtrType.et_Type,
756 type = TypeToPtrType(type->ty_CPtrType.et_Type);
759 type = TypeToAryType(type->ty_CPtrType.et_Type,
769 type = TypeToPtrType(type->ty_AryType.et_Type);
772 type = TypeToCPtrType(type->ty_AryType.et_Type);
785 * BaseType() - return base type
787 * Traverse the type to locate the base type. Store the base type
788 * in *ptype and return the SemGroup, or return NULL if the base type
789 * does not have a SemGroup.
792 BaseType(Type **ptype)
797 switch(type->ty_Op) {
799 type = type->ty_CPtrType.et_Type;
802 type = type->ty_PtrType.et_Type;
805 type = type->ty_RefType.et_Type;
808 type = type->ty_AryType.et_Type;
816 switch(type->ty_Op) {
818 return(type->ty_ClassType.et_SemGroup);
820 return(type->ty_CompType.et_SemGroup);
822 return(type->ty_ArgsType.et_SemGroup);
830 dassert_type(type, 0);
831 return(NULL); /* avoid compiler complaints */
836 * MatchType() - Match two types
838 * Match two types as if we wanted to cast type to super or use
841 * SG_COMPAT_FULL Type is a subclass, methods and storage are
842 * compatible (storage may be extended).
844 * SG_COMPAT_PART Type is a subclass, methods are compatible
845 * but storage is not.
847 * SG_COMPAT_SUBCLASS Type is a subclass, but the methods are
848 * not directly compatible (the methods that
849 * propogate down must be regenerated).
850 * SG_COMPAT_FAIL Type is not even a subclass
852 * XXX we are skipping qualifiers
855 MatchType(Type *super, Type *type)
857 int r = SG_COMPAT_FULL;
859 while (super && type) {
860 SemGroup *sg1 = NULL;
861 SemGroup *sg2 = NULL;
863 if (type->ty_Op != super->ty_Op) {
864 if (super->ty_Op == TY_REFTO &&
865 type->ty_Op == TY_PTRTO) {
866 super = super->ty_RefType.et_Type;
867 type = type->ty_PtrType.et_Type;
868 r = MatchType(super, type);
876 * Relaxed storage qualifiers. The less refined target type
877 * (super) can omit certain storage qualifiers and still match.
879 * NOTE: Actual casts may do more stringent tests.
881 if (super->ty_SQFlags != type->ty_SQFlags) {
882 if ((super->ty_SQFlags & ~(SF_LVALUE)) !=
883 (type->ty_SQFlags & ~SF_RELAXING_MASK)) {
889 switch(super->ty_Op) {
892 * type can be a subclass of super
894 if (type->ty_ClassType.et_SemGroup ==
895 super->ty_ClassType.et_SemGroup) {
898 r = type->ty_ClassType.et_SemGroup->sg_Compat;
900 if (r < SG_COMPAT_PART)
903 while ((type = type->ty_ClassType.et_Super) != NULL) {
904 if (type->ty_ClassType.et_SemGroup ==
905 super->ty_ClassType.et_SemGroup) {
908 if (r < type->ty_ClassType.et_SemGroup->sg_Compat)
909 r = type->ty_ClassType.et_SemGroup->sg_Compat;
911 if (type == NULL) /* not even a subclass */
916 * type can be a subclass of super
922 type = type->ty_CPtrType.et_Type;
923 super = super->ty_CPtrType.et_Type;
926 type = type->ty_PtrType.et_Type;
927 super = super->ty_PtrType.et_Type;
930 type = type->ty_RefType.et_Type;
931 super = super->ty_RefType.et_Type;
934 type = type->ty_AryType.et_Type;
935 super = super->ty_AryType.et_Type;
939 sg1 = super->ty_CompType.et_SemGroup;
940 sg2 = type->ty_CompType.et_SemGroup;
943 sg1 = super->ty_ArgsType.et_SemGroup;
944 sg2 = type->ty_ArgsType.et_SemGroup;
947 r = MatchType(super->ty_VarType.et_Type,
948 type->ty_VarType.et_Type);
954 r = MatchType(super->ty_ProcType.et_ArgsType,
955 type->ty_ProcType.et_ArgsType);
956 v = MatchType(super->ty_ProcType.et_RetType,
957 type->ty_ProcType.et_RetType);
965 if (type->ty_StorType.et_Bytes !=
966 super->ty_StorType.et_Bytes) {
967 r = SG_COMPAT_SUBCLASS;
972 dassert_type(super, 0); /* can't happen */
976 Declaration *sd = getHead(&sg1->sg_DeclList);
977 Declaration *rd = getHead(&sg2->sg_DeclList);
979 int v = MatchDeclTypes(sd, rd);
982 if (r == SG_COMPAT_FAIL)
984 sd = getSucc(&sg1->sg_DeclList, &sd->d_Node);
985 rd = getSucc(&sg2->sg_DeclList, &rd->d_Node);
996 * DupType() - create a duplicate of a type, possibly in a new SemGroup.
998 * This code is used when duplicating procedures and other elements
999 * when merging a superclass into a subclass.
1001 * If sg is NULL, stype is simply returned. The case is used when we
1002 * try to duplciate an expression with DupExp()... in that case we
1003 * want to dup the expression tree but use the same types.
1006 DupType(SemGroup *sg, Type *stype)
1014 * XXX type may be resolved if it is part of a varargs dup
1018 (stype->ty_Flags & (TF_RESOLVED|TF_RESOLVING)) == 0);
1021 switch(stype->ty_Op) {
1024 * This only occurs because the resolver has resolved an
1025 * unresolved type on the original SemGroup. We duplicate
1026 * that on the new SemGroup.
1028 type = AllocClassType(&sg->sg_ClassList,
1029 stype->ty_ClassType.et_Super,
1030 stype->ty_ClassType.et_SemGroup,
1031 stype->ty_Visibility);
1038 type = TypeToCPtrType(DupType(sg, stype->ty_CPtrType.et_Type));
1041 type = TypeToPtrType(DupType(sg, stype->ty_PtrType.et_Type));
1044 type = TypeToRefType(DupType(sg, stype->ty_RefType.et_Type));
1047 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1048 SetDupExp(sg, stype->ty_AryType.et_ArySize),
1049 stype->ty_AryType.et_SemGroup);
1052 type = TypeToVarType(DupType(sg, stype->ty_VarType.et_Type),
1053 DupSemGroup(sg, NULL,
1054 stype->ty_VarType.et_SemGroup, 1));
1057 type = AllocCompoundType(
1058 DupSemGroup(sg, NULL,
1059 stype->ty_CompType.et_SemGroup, 1));
1063 * At the moment we always formally duplicate the arguments
1064 * so we can modify them for methods below.
1066 type = AllocArgsType(
1067 DupSemGroup(sg, NULL,
1068 stype->ty_CompType.et_SemGroup, 1));
1071 type = DupType(sg, stype->ty_ProcType.et_RetType);
1072 type = TypeToProcType(type,
1074 stype->ty_ProcType.et_ArgsType));
1077 * If this is a method procedure, we have to change the
1078 * first argument to point at our new subclass. It was
1079 * previously pointing at our superclass. XXX the
1080 * programmer can override the argument. If it isn't a
1081 * reference we have to assert.
1083 if (stype->ty_SQFlags & SF_METHOD) {
1084 SemGroup *asg = type->ty_ProcType.et_ArgsType->ty_ArgsType.et_SemGroup;
1085 Declaration *d = getHead(&asg->sg_DeclList);
1086 Type *thisType = d->d_StorDecl.ed_Type;
1088 dassert_decl(d, d->d_Id == String_This &&
1089 d->d_Op == DOP_ARGS_STORAGE);
1090 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1091 if (thisType->ty_Op == TY_CLASS) {
1092 /* XXX sg_ClassList? right sg? */
1093 /* XXX correct visibility? */
1094 if (d->d_Search == NULL) {
1095 d->d_Search = d->d_StorDecl.ed_Type->
1096 ty_ClassType.et_SemGroup;
1098 d->d_StorDecl.ed_Type =
1099 AllocClassType(&sg->sg_ClassList,
1100 sg->sg_Stmt->st_ClassStmt.es_Super,
1101 sg->sg_Stmt->st_MyGroup,
1104 dassert_decl(d, thisType->ty_Op == TY_REFTO);
1106 } else if (stype->ty_SQFlags & SF_GMETHOD) {
1110 asg = type->ty_ProcType.et_ArgsType->
1111 ty_ArgsType.et_SemGroup;
1112 d = getHead(&asg->sg_DeclList);
1114 dassert_decl(d, d->d_Id == String_This &&
1115 d->d_Op == DOP_TYPEDEF);
1116 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1117 dassert_decl(d, d->d_TypedefDecl.ed_Type->ty_Op ==
1119 /* XXX sg_ClassList? right sg? */
1120 /* XXX correct visibility? */
1121 if (d->d_Search == NULL) {
1122 d->d_Search = d->d_TypedefDecl.ed_Type->
1123 ty_ClassType.et_SemGroup;
1125 d->d_TypedefDecl.ed_Type =
1126 AllocClassType(&sg->sg_ClassList,
1127 sg->sg_Stmt->st_ClassStmt.es_Super,
1128 sg->sg_Stmt->st_MyGroup,
1137 * e.g. so elements in a superclass will see refined elements
1138 * in the subclass. Note that the original import semgroup
1139 * is left intact so the semantic search mechanism uses it
1140 * when the base sg (typically a subclass) fails.
1142 type = AllocUnresolvedType(
1143 stype->ty_UnresType.et_ImportSemGroup,
1145 stype->ty_UnresType.et_DottedId,
1149 dassert_type(stype, 0);
1152 if (type != stype) {
1153 type->ty_Flags = stype->ty_Flags;
1154 type->ty_Bytes = stype->ty_Bytes;
1155 type->ty_AlignMask = stype->ty_AlignMask;
1156 type->ty_Visibility = stype->ty_Visibility;
1158 if (stype->ty_AssExp || stype->ty_SQFlags != type->ty_SQFlags) {
1159 type = TypeToQualType(type, NULL,
1161 SetDupExp(sg, stype->ty_AssExp));
1168 * SameType() - return 1 if the types are equilvalent, 0 if they are not.
1170 * The sqFlags for t2 may be overriden. If you do not wish to override
1171 * the sqFlags for t2, pass t2->ty_SQFlags for sqFlags. The override
1172 * only applies to the top level of the type.
1174 * Types can be aliased - for example, two different type structures
1175 * may point to the same class data.
1177 * XXX this needs a lot of work. We really need to guarentee
1178 * some level of uniqueness for non-qualified type elements.
1181 SameType(Type *t1, Type *t2, int sqFlags2)
1186 if (t1->ty_Op != t2->ty_Op)
1188 if (t1->ty_SQFlags != sqFlags2)
1192 if (t1->ty_ImportType.et_SemGroup ==
1193 t2->ty_ImportType.et_SemGroup)
1199 if (t1->ty_ClassType.et_SemGroup ==
1200 t2->ty_ClassType.et_SemGroup &&
1201 t1->ty_ClassType.et_Super ==
1202 t2->ty_ClassType.et_Super)
1208 t1 = t1->ty_CPtrType.et_Type;
1209 t2 = t2->ty_CPtrType.et_Type;
1212 t1 = t1->ty_PtrType.et_Type;
1213 t2 = t2->ty_PtrType.et_Type;
1216 t1 = t1->ty_RefType.et_Type;
1217 t2 = t2->ty_RefType.et_Type;
1232 dassert_type(t1, 0);
1235 sqFlags2 = t2->ty_SQFlags;
1241 * SimilarType() - like SameType(), but ignores storage qualifiers and
1242 * if t2 is varargs, compares the original version.
1244 * Used when casting t2 (rhs) to t1 (lhs).
1247 SimilarType(Type *t1, Type *t2)
1249 if (t2->ty_Op == TY_VAR)
1250 t2 = t2->ty_VarType.et_Type;
1256 * Normally we fail if the ops do not match, but it is legal to
1257 * cast a pointer (t2) to a reference type (t1) if the ref type
1258 * is its superclass. It is also legal to cast an array to a
1259 * pointer or C pointer.
1261 if (t2->ty_Op != t1->ty_Op) {
1265 if (t2->ty_Op == TY_PTRTO && t1->ty_Op == TY_REFTO) {
1266 t1 = t1->ty_RefType.et_Type;
1267 t2 = t2->ty_PtrType.et_Type;
1268 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1276 if (t2->ty_Op == TY_ARYOF && t1->ty_Op == TY_PTRTO) {
1277 t1 = t1->ty_PtrType.et_Type;
1278 t2 = t2->ty_AryType.et_Type;
1279 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1287 if (t2->ty_Op == TY_ARYOF && t1->ty_Op == TY_CPTRTO) {
1288 t1 = t1->ty_PtrType.et_Type;
1289 t2 = t2->ty_AryType.et_Type;
1290 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1298 if (t2->ty_Op != t1->ty_Op)
1302 if (t1->ty_ImportType.et_SemGroup ==
1303 t2->ty_ImportType.et_SemGroup) {
1308 if (t1->ty_ClassType.et_SemGroup ==
1309 t2->ty_ClassType.et_SemGroup &&
1310 t1->ty_ClassType.et_Super ==
1311 t2->ty_ClassType.et_Super)
1317 t1 = t1->ty_CPtrType.et_Type;
1318 t2 = t2->ty_CPtrType.et_Type;
1321 t1 = t1->ty_PtrType.et_Type;
1322 t2 = t2->ty_PtrType.et_Type;
1326 * Reference types are similar if the lhs is a
1327 * superclass of the rhs and partially compatible
1328 * (only method call changes and extensions).
1330 t1 = t1->ty_RefType.et_Type;
1331 t2 = t2->ty_RefType.et_Type;
1332 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1339 * Compare the elements making up the compound type.
1342 return(SimilarSemGroup(t1->ty_CompType.et_SemGroup,
1343 t2->ty_CompType.et_SemGroup));
1357 dassert_type(t1, 0);
1365 * SimilarSemGroup() - check to see if the storage underlying the two
1366 * semantic groups is compatible.
1369 SimilarSemGroup(SemGroup *s1, SemGroup *s2)
1374 if (s1->sg_Bytes != s2->sg_Bytes)
1376 d1 = getHead(&s1->sg_DeclList);
1377 d2 = getHead(&s2->sg_DeclList);
1380 ((d1->d_Op & DOPF_STORAGE) == 0 ||
1381 d1->d_Op == DOP_GLOBAL_STORAGE)
1383 d1 = getSucc(&s1->sg_DeclList, &d1->d_Node);
1386 ((d2->d_Op & DOPF_STORAGE) == 0 ||
1387 d2->d_Op == DOP_GLOBAL_STORAGE)
1389 d2 = getSucc(&s2->sg_DeclList, &d2->d_Node);
1391 if (d1 == NULL || d2 == NULL)
1393 if (SimilarType(d1->d_StorDecl.ed_Type,
1394 d2->d_StorDecl.ed_Type) == 0) {
1397 d1 = getSucc(&s1->sg_DeclList, &d1->d_Node);
1398 d2 = getSucc(&s2->sg_DeclList, &d2->d_Node);
1401 return(0); /* compare bad */
1402 return(1); /* compare good */
1406 TypeToStr(Type *type, char **pstr)
1415 static char *SaveStr[8];
1416 static int SaveIndex;
1419 safe_asprintf(&str, "(null)");
1421 switch(type->ty_Op) {
1423 st = type->ty_ClassType.et_SemGroup->sg_Stmt;
1424 dassert(st->st_Op == ST_Class);
1426 safe_asprintf(&str, "CLASS(%s",
1427 st->st_ClassStmt.es_Decl->d_Id);
1429 LexPrintRef(&st->st_LexRef, 0);
1430 safe_asprintf(&str, "CLASS(%s from",
1431 /* type->ty_ClassType.et_SemGroup->sg_Parse, */
1432 st->st_ClassStmt.es_Decl->d_Id);
1434 while (st && st->st_Op != ST_Import)
1438 safe_replacef(&str, "%s <%s%s>",
1440 st->st_ImportStmt.es_Path,
1441 st->st_ImportStmt.es_File);
1445 safe_replacef(&str, "%s)", str);
1448 st = type->ty_ImportType.et_SemGroup->sg_Stmt;
1449 dassert(st->st_Op == ST_Module);
1450 safe_asprintf(&str, "IMPORT(%s)",
1451 st->st_LexRef.lr_Lex->l_Path);
1454 TypeToStr(type->ty_CPtrType.et_Type, &s1);
1455 safe_replacef(&str, "(CLANG)*%s", s1);
1458 TypeToStr(type->ty_PtrType.et_Type, &s1);
1459 safe_replacef(&str, "*%s", s1);
1462 TypeToStr(type->ty_RefType.et_Type, &s1);
1463 safe_replacef(&str, "@%s", s1);
1466 TypeToStr(type->ty_AryType.et_Type, &s1);
1467 safe_replacef(&str, "%s[]", s1);
1470 sg = type->ty_CompType.et_SemGroup;
1471 safe_asprintf(&str, "COMPOUND(");
1473 for (d = getHead(&sg->sg_DeclList);
1475 d = getSucc(&sg->sg_DeclList, &d->d_Node)) {
1476 if (d->d_Op & DOPF_STORAGE) {
1477 TypeToStr(d->d_StorDecl.ed_Type, &s1);
1479 safe_replacef(&str, "%s,%s",
1482 safe_replacef(&str, "%s%s",
1488 safe_replacef(&str, "%s)", str);
1491 safe_replacef(&str, "VAR");
1494 sg = type->ty_ArgsType.et_SemGroup;
1495 safe_asprintf(&str, "ARGS(");
1497 for (d = getHead(&sg->sg_DeclList);
1499 d = getSucc(&sg->sg_DeclList, &d->d_Node)) {
1500 if (d->d_Op & DOPF_STORAGE) {
1501 TypeToStr(d->d_StorDecl.ed_Type, &s1);
1503 safe_replacef(&str, "%s,%s",
1506 safe_replacef(&str, "%s%s",
1512 safe_replacef(&str, "%s)", str);
1516 TypeToStr(type->ty_ProcType.et_RetType, &s1);
1517 TypeToStr(type->ty_ProcType.et_ArgsType, &s2);
1518 safe_asprintf(&str, "%s %s", s1, s2);
1522 safe_asprintf(&str, "STORAGE(%d)",
1523 type->ty_StorType.et_Bytes);
1526 safe_asprintf(&str, "DYNAMIC");
1529 safe_asprintf(&str, "UNRES(");
1530 for (count = 0; type->ty_UnresType.et_DottedId[count]; ++count) {
1531 safe_replacef(&str, "%s%s",
1532 str, type->ty_UnresType.et_DottedId[count]);
1534 safe_replacef(&str, "%s,%s",
1535 str, type->ty_UnresType.et_DottedId[count]);
1538 safe_replacef(&str, "%s)", str);
1541 safe_asprintf(&str, "?");
1544 if (type->ty_SQFlags) {
1547 safe_asprintf(&sqstr, "(%s%s%s%s%s%s%s)",
1548 ((type->ty_SQFlags & SF_CONST) ? "const " : ""),
1549 ((type->ty_SQFlags & SF_VOLATILE) ? "volatile " : ""),
1550 ((type->ty_SQFlags & SF_VARARGS) ? "varargs " : ""),
1551 ((type->ty_SQFlags & SF_LVALUE) ? "lvalue " : ""),
1552 ((type->ty_SQFlags & SF_NOZERO) ? "__nozero " : ""),
1553 ((type->ty_SQFlags & SF_METHOD) ? "method " : ""),
1554 ((type->ty_SQFlags & SF_GMETHOD) ? "global method " : "")
1556 safe_replacef(&str, "%s%s", sqstr, str);
1566 safe_free(&SaveStr[SaveIndex]);
1567 SaveStr[SaveIndex] = str;
1568 SaveIndex = (SaveIndex + 1) % arysize(SaveStr);
1573 typedef struct TypeRegNode {
1579 List TypeRegList = INITLIST(TypeRegList);
1582 InternalRegisterType(const char *str, Type *type)
1584 static int Special = SPECIALF_REGISTERED|SPECIALF_INTERNAL|1;
1587 dassert(Special & SPECIALF_MASK);
1588 tr = zalloc(sizeof(TypeRegNode));
1589 tr->tr_Id = StrTableAlloc(str, strlen(str), Special++);
1591 addTail(&TypeRegList, &tr->tr_Node);
1595 InternalRegisteredTypeLookup(string_t id)
1600 tr = getHead(&TypeRegList);
1602 tr = getSucc(&TypeRegList, &tr->tr_Node)
1604 if (tr->tr_Id == id)
1605 return(tr->tr_Type);