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;
30 Type CCharType; /* const char */
31 Type StrType; /* const char * */
32 Type CharPtrType; /* char * */
33 Type CharPtrPtrType; /* char ** */
34 Type VoidPtrType; /* void * */
35 Type VoidRefType; /* void @ */
36 Type CVoidPtrType; /* const void * */
37 Type LVoidPtrType; /* lvalue void * */
39 Type PointerType; /* generic pointer placeholder */
40 Type IntPtrType; /* not int * ... intptr_t */
41 Type UIntPtrType; /* not int * ... intptr_t */
43 static List DynamicTypeList = INITLIST(DynamicTypeList);
44 static List CompoundTypeList = INITLIST(CompoundTypeList);
45 static List ArgsTypeList = INITLIST(ArgsTypeList);
46 static List StorageTypeList = INITLIST(StorageTypeList);
49 initType(Type *type, List *list, int op)
52 initList(&type->ty_QList);
54 addTail(list, &type->ty_Node);
55 type->ty_SQList = list;
59 initQualType(Type *type, List *list, int op, int sqflags)
61 initType(type, list, op);
62 type->ty_SQFlags = sqflags;
66 initPtrType(Type *type, Type *ptrto, int sqflags)
68 initQualType(type, &ptrto->ty_QList, TY_PTRTO, sqflags);
69 type->ty_PtrType.et_Type = ptrto;
70 type->ty_PtrType.et_RefNull.rs_Type = ptrto;
71 type->ty_PtrType.et_RefNull.rs_Refs = 1;
75 initRefType(Type *type, Type *refto, int sqflags)
77 initQualType(type, &refto->ty_QList, TY_REFTO, sqflags);
78 type->ty_RefType.et_Type = refto;
79 type->ty_RefType.et_RefNull.rs_Type = refto;
80 type->ty_RefType.et_RefNull.rs_Refs = 1;
86 initQualType(&DynamicLValueType, &DynamicTypeList,
87 TY_DYNAMIC, SF_LVALUE);
88 initType(&DynamicRValueType, &DynamicTypeList, TY_DYNAMIC);
89 initType(&NumericType, NULL, TY_UNRESOLVED);
90 initType(&IntegerType, NULL, TY_UNRESOLVED);
91 initType(&VoidType, NULL, TY_UNRESOLVED);
92 initType(&BoolType, NULL, TY_UNRESOLVED);
93 initType(&Int8Type, NULL, TY_UNRESOLVED);
94 initType(&UInt8Type, NULL, TY_UNRESOLVED);
95 initType(&Int16Type, NULL, TY_UNRESOLVED);
96 initType(&UInt16Type, NULL, TY_UNRESOLVED);
97 initType(&Int32Type, NULL, TY_UNRESOLVED);
98 initType(&UInt32Type, NULL, TY_UNRESOLVED);
99 initType(&Int64Type, NULL, TY_UNRESOLVED);
100 initType(&UInt64Type, NULL, TY_UNRESOLVED);
102 initType(&Float32Type, NULL, TY_UNRESOLVED);
103 initType(&Float64Type, NULL, TY_UNRESOLVED);
104 initType(&Float128Type, NULL, TY_UNRESOLVED);
106 initQualType(&CCharType, NULL, TY_UNRESOLVED, SF_CONST);
107 initPtrType(&StrType, &CCharType, 0);
108 initPtrType(&CharPtrType, &UInt8Type, 0);
109 initPtrType(&CharPtrPtrType, &CharPtrType, 0);
110 initPtrType(&VoidPtrType, &VoidType, 0);
111 initRefType(&VoidRefType, &VoidType, 0);
112 initPtrType(&CVoidPtrType, &VoidType, SF_CONST);
113 initPtrType(&LVoidPtrType, &VoidType, SF_LVALUE);
115 initType(&PointerType, NULL, TY_UNRESOLVED);
116 initType(&IntPtrType, NULL, TY_UNRESOLVED);
117 initType(&UIntPtrType, NULL, TY_UNRESOLVED);
119 StrTableAlloc("__internal_void", 15, SPECIAL_INTERNAL_VOID);
120 StrTableAlloc("__internal_bool", 15, SPECIAL_INTERNAL_BOOL);
121 StrTableAlloc("__internal_int8", 15, SPECIAL_INTERNAL_INT8);
122 StrTableAlloc("__internal_uint8", 16, SPECIAL_INTERNAL_UINT8);
123 StrTableAlloc("__internal_int16", 16, SPECIAL_INTERNAL_INT16);
124 StrTableAlloc("__internal_uint16", 17, SPECIAL_INTERNAL_UINT16);
125 StrTableAlloc("__internal_int32", 16, SPECIAL_INTERNAL_INT32);
126 StrTableAlloc("__internal_uint32", 17, SPECIAL_INTERNAL_UINT32);
127 StrTableAlloc("__internal_int64", 16, SPECIAL_INTERNAL_INT64);
128 StrTableAlloc("__internal_uint64", 17, SPECIAL_INTERNAL_UINT64);
130 StrTableAlloc("__internal_float32", 18, SPECIAL_INTERNAL_FLOAT32);
131 StrTableAlloc("__internal_float64", 18, SPECIAL_INTERNAL_FLOAT64);
132 StrTableAlloc("__internal_float128", 19, SPECIAL_INTERNAL_FLOAT128);
134 StrTableAlloc("__internal_pointer", 18, SPECIAL_INTERNAL_POINTER);
135 StrTableAlloc("__internal_intptr", 17, SPECIAL_INTERNAL_INTPTR);
136 StrTableAlloc("__internal_uintptr", 18, SPECIAL_INTERNAL_UINTPTR);
138 StrTableAlloc("__internal_numeric", 18, SPECIAL_INTERNAL_NUMERIC);
139 StrTableAlloc("__internal_integer", 18, SPECIAL_INTERNAL_INTEGER);
141 StrTableAlloc("__count", 7, SPECIAL_COUNT);
142 StrTableAlloc("__data", 6, SPECIAL_DATA);
143 StrTableAlloc("__varcount", 10, SPECIAL_VAR_COUNT);
144 StrTableAlloc("__vardata", 9, SPECIAL_VAR_DATA);
145 StrTableAlloc("__typeid", 8, SPECIAL_TYPEID);
146 StrTableAlloc("__typestr", 9, SPECIAL_TYPESTR);
147 StrTableAlloc("NULL", 4, SPECIAL_NULL);
151 InternalTypeAttach(Parse *p, int t, Declaration *d)
156 dassert_decl(d, d->d_Op == DOP_TYPEDEF);
158 if ((s = StrTableSpecial(d->d_Id)) & SPECIALF_INTERNAL) {
160 case SPECIAL_INTERNAL_VOID:
163 case SPECIAL_INTERNAL_BOOL:
166 case SPECIAL_INTERNAL_INT8:
169 case SPECIAL_INTERNAL_UINT8:
172 case SPECIAL_INTERNAL_INT16:
175 case SPECIAL_INTERNAL_UINT16:
178 case SPECIAL_INTERNAL_INT32:
181 case SPECIAL_INTERNAL_UINT32:
184 case SPECIAL_INTERNAL_INT64:
187 case SPECIAL_INTERNAL_UINT64:
190 case SPECIAL_INTERNAL_FLOAT32:
191 itype = &Float32Type;
193 case SPECIAL_INTERNAL_FLOAT64:
194 itype = &Float64Type;
196 case SPECIAL_INTERNAL_FLOAT128:
197 itype = &Float128Type;
199 case SPECIAL_INTERNAL_POINTER:
200 itype = &PointerType;
202 case SPECIAL_INTERNAL_INTPTR:
205 case SPECIAL_INTERNAL_UINTPTR:
206 itype = &UIntPtrType;
208 case SPECIAL_INTERNAL_NUMERIC:
209 itype = &NumericType;
211 case SPECIAL_INTERNAL_INTEGER:
212 itype = &IntegerType;
215 itype = InternalRegisteredTypeLookup(d->d_Id);
222 if (itype->ty_Op != TY_UNRESOLVED) {
223 t = LexError(&p->p_Token, TOK_ERR_DUPLICATE_ATTACH);
225 Type *ntype = d->d_TypedefDecl.ed_Type;
227 TypeToQualType(ntype, itype, ntype->ty_SQFlags, NULL);
230 * fixup for const int8 pointers... we did not have
231 * a QList to put CCharType on until now. It will
232 * wind up on the SemGroup's sg_ClassList.
234 if (itype == &UInt8Type) {
235 TypeToQualType(ntype, &CCharType,
236 ntype->ty_SQFlags | SF_CONST, NULL);
240 t = LexError(&p->p_Token, TOK_ERR_UNRECOGNIZED_ATTACH);
246 AllocType(List *list, int op)
248 Type *type = zalloc(sizeof(Type));
250 initType(type, list, op);
255 * XXX match the compound type(s)
257 * May be used to generate a varargs compound type, in which case the
258 * semgroup may already be resolved.
261 AllocCompoundType(SemGroup *sg)
265 type = AllocType(&CompoundTypeList, TY_COMPOUND);
266 type->ty_CompType.et_SemGroup = sg;
267 dassert((sg->sg_Flags & SGF_RESOLVED) == 0);
272 * XXX match the compound type(s)
275 AllocArgsType(SemGroup *sg)
279 type = AllocType(&ArgsTypeList, TY_ARGS);
280 type->ty_ArgsType.et_SemGroup = sg;
285 AllocStorageType(int bytes)
290 type = getHead(&StorageTypeList);
292 type = getSucc(&StorageTypeList, &type->ty_Node)
294 if (type->ty_Op == TY_STORAGE &&
295 type->ty_StorType.et_Bytes == bytes
300 type = AllocType(&StorageTypeList, TY_STORAGE);
301 type->ty_StorType.et_Bytes = bytes;
306 AllocUnresolvedType(SemGroup *isg, SemGroup *sg, string_t *ary, int eatAry)
310 dassert_semgrp(sg, ary != NULL);
313 type = getHead(&sg->sg_ClassList);
315 type = getSucc(&sg->sg_ClassList, &type->ty_Node)
319 if (type->ty_Op != TY_UNRESOLVED)
321 if (type->ty_UnresType.et_ImportSemGroup != isg)
324 for (i = 0; ary[i]; ++i) {
325 if (ary[i] != type->ty_UnresType.et_DottedId[i])
328 if (ary[i] == NULL &&
329 type->ty_UnresType.et_DottedId[i] == NULL) {
335 type = AllocType((sg ? &sg->sg_ClassList : NULL), TY_UNRESOLVED);
336 type->ty_UnresType.et_DottedId = ary;
337 type->ty_UnresType.et_SemGroup = sg; /* may be NULL */
338 type->ty_UnresType.et_ImportSemGroup = isg; /* may be NULL */
343 * AllocClassType() - allocate a type representing a the semgroup which
344 * in turn represents (typically) a class.
347 AllocClassType(List *list, Type *super, SemGroup *sg, int visibility)
351 for (type = getHead(list); type; type = getSucc(list, &type->ty_Node)) {
352 if (type->ty_Op == TY_CLASS &&
353 type->ty_ClassType.et_SemGroup == sg &&
354 type->ty_ClassType.et_Super == super &&
355 type->ty_Visibility == visibility
360 type = AllocType(list, TY_CLASS);
361 type->ty_ClassType.et_SemGroup = sg;
362 type->ty_ClassType.et_Super = super;
363 type->ty_Visibility = visibility;
368 AllocImportType(List *list, SemGroup *sg, int visibility)
370 Type *type = AllocType(list, TY_IMPORT);
372 type->ty_ImportType.et_SemGroup = sg;
373 type->ty_Visibility = visibility;
378 TypeToQualType(Type *otype, Type *ntype, int sqFlags, Exp *exp)
381 * Combine with existing qualifiers, Shortcut if no changes made.
384 sqFlags == otype->ty_SQFlags &&
385 (exp == NULL || exp == otype->ty_AssExp)
391 * See if we already have a matching qualified type (only if storage
392 * for the new type is not being provided). Note: the provided storage
393 * has already been initType()d
396 for (ntype = getHead(otype->ty_SQList);
398 ntype = getSucc(otype->ty_SQList, &ntype->ty_Node)
400 if (ntype->ty_Op == otype->ty_Op &&
401 ntype->ty_SQFlags == sqFlags &&
402 (exp == NULL || ntype->ty_AssExp == exp)
404 if (SameType(ntype, otype, sqFlags))
411 * Build a new qualified type and set its qualifiers, then duplicate
412 * appropriate sections of the old type.
415 ntype = AllocType(otype->ty_SQList, otype->ty_Op);
416 } else if (ntype->ty_SQList == NULL) {
417 ntype->ty_SQList = otype->ty_SQList;
418 addTail(ntype->ty_SQList, &ntype->ty_Node);
422 * Set the op and the expression. Unlike SQFlags, if exp is passed as
423 * NULL we inherit the old type's default.
425 * The DupExp() call here is special, see DupExp()'s handling of
428 * Normally DupExp() is called during resolution prior to ex_Decl
429 * being set. This is the one case where it may be called with
430 * ex_Decl already set.
432 ntype->ty_Op = otype->ty_Op;
434 ntype->ty_AssExp = exp;
435 else if (otype->ty_AssExp)
436 ntype->ty_AssExp = SetDupExp(NULL, otype->ty_AssExp);
437 ntype->ty_SQFlags = sqFlags;
438 ntype->ty_Visibility = otype->ty_Visibility;
440 switch(otype->ty_Op) {
442 ntype->ty_ClassType.et_SemGroup =
443 otype->ty_ClassType.et_SemGroup;
444 ntype->ty_ClassType.et_Super = otype->ty_ClassType.et_Super;
447 ntype->ty_Visibility = otype->ty_Visibility;
448 ntype->ty_ImportType.et_SemGroup = otype->ty_ImportType.et_SemGroup;
451 ntype->ty_CPtrType.et_Type = otype->ty_CPtrType.et_Type;
452 ntype->ty_CPtrType.et_RefNull.rs_Type =
453 ntype->ty_CPtrType.et_Type;
454 ntype->ty_CPtrType.et_RefNull.rs_Refs = 1;
457 ntype->ty_PtrType.et_Type = otype->ty_PtrType.et_Type;
458 ntype->ty_PtrType.et_RefNull.rs_Type =
459 ntype->ty_PtrType.et_Type;
460 ntype->ty_PtrType.et_RefNull.rs_Refs = 1;
463 ntype->ty_RefType.et_Type = otype->ty_RefType.et_Type;
464 ntype->ty_RefType.et_RefNull.rs_Type =
465 ntype->ty_RefType.et_Type;
466 ntype->ty_RefType.et_RefNull.rs_Refs = 1;
470 * note: multiple type structures may share the same array size
471 * expression in simple qualified-type cases. YYY XXX bad bad.
473 ntype->ty_AryType.et_Type = otype->ty_AryType.et_Type;
474 ntype->ty_AryType.et_ArySize = otype->ty_AryType.et_ArySize;
475 ntype->ty_AryType.et_SemGroup = otype->ty_AryType.et_SemGroup;
478 ntype->ty_CompType.et_SemGroup = otype->ty_CompType.et_SemGroup;
481 ntype->ty_VarType.et_Type = otype->ty_VarType.et_Type;
482 ntype->ty_VarType.et_SemGroup = otype->ty_VarType.et_SemGroup;
485 ntype->ty_ArgsType.et_SemGroup = otype->ty_ArgsType.et_SemGroup;
488 ntype->ty_ProcType.et_ArgsType = otype->ty_ProcType.et_ArgsType;
489 ntype->ty_ProcType.et_RetType = otype->ty_ProcType.et_RetType;
490 ntype->ty_ProcType.et_ArgCount = otype->ty_ProcType.et_ArgCount;
493 ntype->ty_StorType.et_Bytes = otype->ty_StorType.et_Bytes;
497 * It is not legal to qualify a dynamic type other then to
498 * add or remove SF_LVALUE.
503 ntype->ty_UnresType.et_DottedId =
504 otype->ty_UnresType.et_DottedId;
505 ntype->ty_UnresType.et_SemGroup =
506 otype->ty_UnresType.et_SemGroup;
509 dassert_type(otype, 0);
515 * XXX match the procedure type(s)
518 TypeToProcType(Type *rtype, Type *atype)
525 dassert_type(atype, atype->ty_Op == TY_ARGS);
526 sg = atype->ty_CompType.et_SemGroup;
529 d = getHead(&sg->sg_DeclList);
531 d = getSucc(&sg->sg_DeclList, &d->d_Node)
536 type = getHead(&rtype->ty_QList);
538 type = getSucc(&rtype->ty_QList, &type->ty_Node)
540 if (type->ty_Op == TY_PROC) {
541 if (type->ty_ProcType.et_ArgsType == atype &&
542 type->ty_ProcType.et_RetType == rtype &&
543 type->ty_ProcType.et_ArgCount == count
545 puts("SG1"); /* YYY */
550 type = AllocType(&rtype->ty_QList, TY_PROC);
551 type->ty_ProcType.et_ArgsType = AllocArgsType(sg);
552 type->ty_ProcType.et_RetType = rtype;
553 type->ty_ProcType.et_ArgCount = count;
558 * Convert type to pointer-to-type
561 TypeToPtrType(Type *otype)
565 for (type = getHead(&otype->ty_QList);
567 type = getSucc(&otype->ty_QList, &type->ty_Node)
569 if (type->ty_Op == TY_PTRTO)
572 type = AllocType(&otype->ty_QList, TY_PTRTO);
573 type->ty_PtrType.et_Type = otype;
574 type->ty_PtrType.et_RefNull.rs_Type = otype;
575 type->ty_PtrType.et_RefNull.rs_Refs = 1;
580 * Convert type to pointer-to-type
583 TypeToCPtrType(Type *otype)
587 for (type = getHead(&otype->ty_QList);
589 type = getSucc(&otype->ty_QList, &type->ty_Node)
591 if (type->ty_Op == TY_CPTRTO)
594 type = AllocType(&otype->ty_QList, TY_CPTRTO);
595 type->ty_CPtrType.et_Type = otype;
596 type->ty_CPtrType.et_RefNull.rs_Type = otype;
597 type->ty_CPtrType.et_RefNull.rs_Refs = 1;
602 * Convert type to ref-to-type
604 * A reference type is similar to a pointer type except that the
605 * resolver is not able to entirely know what it is pointing to.
606 * The reference type is a superclass, but the actual type is
607 * stored in the run-time structure.
610 TypeToRefType(Type *otype)
614 for (type = getHead(&otype->ty_QList);
616 type = getSucc(&otype->ty_QList, &type->ty_Node)
618 if (type->ty_Op == TY_REFTO)
621 type = AllocType(&otype->ty_QList, TY_REFTO);
622 type->ty_RefType.et_Type = otype;
623 type->ty_RefType.et_RefNull.rs_Type = otype;
624 type->ty_RefType.et_RefNull.rs_Refs = 1;
629 TypeToAryType(Type *otype, Exp *exp, SemGroup *sg)
634 * XXX handle constant expression optimization for QList
635 * XXX handle qualifiers
637 type = AllocType(&otype->ty_QList, TY_ARYOF);
638 type->ty_AryType.et_ArySize = exp;
639 type->ty_AryType.et_Type = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
640 type->ty_AryType.et_SemGroup = sg;
641 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
648 TypeToRunTimeAryType(Type *otype, int count)
651 Type *t2 = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
653 for (type = getHead(&otype->ty_QList);
655 type = getSucc(&otype->ty_QList, &type->ty_Node)
657 if (type->ty_Op == TY_ARYOF &&
658 type->ty_AryType.et_Type == t2 &&
659 type->ty_AryType.et_Count == count &&
661 (otype->ty_SQFlags & SF_MASK_ARY_INHERIT)
666 type = AllocType(&otype->ty_QList, TY_ARYOF);
667 type->ty_AryType.et_Count = count;
668 type->ty_AryType.et_Type = t2;
669 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
676 TypeToVarType(Type *otype, SemGroup *sg)
680 dassert(sg->sg_Flags & SGF_RESOLVED);
681 dassert(otype->ty_Flags & TF_RESOLVED);
683 for (type = getHead(&otype->ty_QList);
685 type = getSucc(&otype->ty_QList, &type->ty_Node)
687 if (type->ty_Op == TY_VAR &&
688 type->ty_VarType.et_Type == otype &&
689 type->ty_VarType.et_SemGroup == sg
691 puts("SG2"); /* YYY */
695 type = AllocType(&otype->ty_QList, TY_VAR);
696 type->ty_VarType.et_Type = otype;
697 type->ty_VarType.et_SemGroup = sg;
698 if (sg->sg_Flags & SGF_RESOLVED) {
699 type->ty_Flags |= TF_RESOLVED;
700 type->ty_Bytes = sg->sg_Bytes;
701 type->ty_AlignMask = sg->sg_AlignMask;
707 * ChangeType() - given pointer, C pointer, or array of something,
708 * return 'op' of something instead.
711 ChangeType(Type *type, int op)
713 switch(type->ty_Op) {
717 type = TypeToCPtrType(type->ty_PtrType.et_Type);
720 type = TypeToAryType(type->ty_PtrType.et_Type,
730 type = TypeToPtrType(type->ty_CPtrType.et_Type);
733 type = TypeToAryType(type->ty_CPtrType.et_Type,
743 type = TypeToPtrType(type->ty_AryType.et_Type);
746 type = TypeToCPtrType(type->ty_AryType.et_Type);
759 * BaseType() - return base type
761 * Traverse the type to locate the base type. Store the base type
762 * in *ptype and return the SemGroup, or return NULL if the base type
763 * does not have a SemGroup.
766 BaseType(Type **ptype)
771 switch(type->ty_Op) {
773 type = type->ty_CPtrType.et_Type;
776 type = type->ty_PtrType.et_Type;
779 type = type->ty_RefType.et_Type;
782 type = type->ty_AryType.et_Type;
790 switch(type->ty_Op) {
792 return(type->ty_ClassType.et_SemGroup);
794 return(type->ty_CompType.et_SemGroup);
796 return(type->ty_ArgsType.et_SemGroup);
804 dassert_type(type, 0);
805 return(NULL); /* avoid compiler complaints */
810 * MatchType() - Match two types
812 * Match two types as if we wanted to cast type to super or use
815 * SG_COMPAT_FULL Type is a subclass, methods and storage are
816 * compatible (storage may be extended).
818 * SG_COMPAT_PART Type is a subclass, methods are compatible
819 * but storage is not.
821 * SG_COMPAT_SUBCLASS Type is a subclass, but the methods are
822 * not directly compatible (the methods that
823 * propogate down must be regenerated).
824 * SG_COMPAT_FAIL Type is not even a subclass
826 * XXX we are skipping qualifiers
829 MatchType(Type *super, Type *type)
831 int r = SG_COMPAT_FULL;
833 while (super && type) {
834 SemGroup *sg1 = NULL;
835 SemGroup *sg2 = NULL;
837 if (type->ty_Op != super->ty_Op) {
838 if (super->ty_Op == TY_REFTO &&
839 type->ty_Op == TY_PTRTO) {
840 super = super->ty_RefType.et_Type;
841 type = type->ty_PtrType.et_Type;
842 r = MatchType(super, type);
848 switch(super->ty_Op) {
851 * type can be a subclass of super
853 if (type->ty_ClassType.et_SemGroup ==
854 super->ty_ClassType.et_SemGroup) {
857 r = type->ty_ClassType.et_SemGroup->sg_Compat;
859 if (r < SG_COMPAT_PART)
862 while ((type = type->ty_ClassType.et_Super) != NULL) {
863 if (type->ty_ClassType.et_SemGroup ==
864 super->ty_ClassType.et_SemGroup) {
867 if (r < type->ty_ClassType.et_SemGroup->sg_Compat)
868 r = type->ty_ClassType.et_SemGroup->sg_Compat;
870 if (type == NULL) /* not even a subclass */
875 * type can be a subclass of super
881 type = type->ty_CPtrType.et_Type;
882 super = super->ty_CPtrType.et_Type;
885 type = type->ty_PtrType.et_Type;
886 super = super->ty_PtrType.et_Type;
889 type = type->ty_RefType.et_Type;
890 super = super->ty_RefType.et_Type;
893 type = type->ty_AryType.et_Type;
894 super = super->ty_AryType.et_Type;
898 sg1 = super->ty_CompType.et_SemGroup;
899 sg2 = type->ty_CompType.et_SemGroup;
902 sg1 = super->ty_ArgsType.et_SemGroup;
903 sg2 = type->ty_ArgsType.et_SemGroup;
906 r = MatchType(super->ty_VarType.et_Type,
907 type->ty_VarType.et_Type);
913 r = MatchType(super->ty_ProcType.et_ArgsType,
914 type->ty_ProcType.et_ArgsType);
915 v = MatchType(super->ty_ProcType.et_RetType,
916 type->ty_ProcType.et_RetType);
924 if (type->ty_StorType.et_Bytes !=
925 super->ty_StorType.et_Bytes) {
926 r = SG_COMPAT_SUBCLASS;
931 dassert_type(super, 0); /* can't happen */
935 Declaration *sd = getHead(&sg1->sg_DeclList);
936 Declaration *rd = getHead(&sg2->sg_DeclList);
938 int v = MatchDeclTypes(sd, rd);
941 if (r == SG_COMPAT_FAIL)
943 sd = getSucc(&sg1->sg_DeclList, &sd->d_Node);
944 rd = getSucc(&sg2->sg_DeclList, &rd->d_Node);
955 * DupType() - create a duplicate of a type, possibly in a new SemGroup.
957 * This code is used when duplicating procedures and other elements
958 * when merging a superclass into a subclass.
960 * If sg is NULL, stype is simply returned. The case is used when we
961 * try to duplciate an expression with DupExp()... in that case we
962 * want to dup the expression tree but use the same types.
965 DupType(SemGroup *sg, Type *stype)
973 * XXX type may be resolved if it is part of a varargs dup
977 (stype->ty_Flags & (TF_RESOLVED|TF_RESOLVING)) == 0);
980 switch(stype->ty_Op) {
983 * This only occurs because the resolver has resolved an
984 * unresolved type on the original SemGroup. We duplicate
985 * that on the new SemGroup.
987 type = AllocClassType(&sg->sg_ClassList,
988 stype->ty_ClassType.et_Super,
989 stype->ty_ClassType.et_SemGroup,
990 stype->ty_Visibility);
997 type = TypeToCPtrType(DupType(sg, stype->ty_CPtrType.et_Type));
1000 type = TypeToPtrType(DupType(sg, stype->ty_PtrType.et_Type));
1003 type = TypeToRefType(DupType(sg, stype->ty_RefType.et_Type));
1006 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1007 SetDupExp(sg, stype->ty_AryType.et_ArySize),
1008 stype->ty_AryType.et_SemGroup);
1011 type = TypeToVarType(DupType(sg, stype->ty_VarType.et_Type),
1012 DupSemGroup(sg, NULL,
1013 stype->ty_VarType.et_SemGroup, 1));
1016 type = AllocCompoundType(
1017 DupSemGroup(sg, NULL,
1018 stype->ty_CompType.et_SemGroup, 1));
1022 * At the moment we always formally duplicate the arguments
1023 * so we can modify them for methods below.
1025 type = AllocArgsType(
1026 DupSemGroup(sg, NULL,
1027 stype->ty_CompType.et_SemGroup, 1));
1030 type = DupType(sg, stype->ty_ProcType.et_RetType);
1031 type = TypeToProcType(type,
1033 stype->ty_ProcType.et_ArgsType));
1036 * If this is a method procedure, we have to change the
1037 * first argument to point at our new subclass. It was
1038 * previously pointing at our superclass. XXX the
1039 * programmer can override the argument. If it isn't a
1040 * reference we have to assert.
1042 if (stype->ty_SQFlags & SF_METHOD) {
1043 SemGroup *asg = type->ty_ProcType.et_ArgsType->ty_ArgsType.et_SemGroup;
1044 Declaration *d = getHead(&asg->sg_DeclList);
1045 Type *thisType = d->d_StorDecl.ed_Type;
1047 dassert_decl(d, d->d_Id == String_This &&
1048 d->d_Op == DOP_ARGS_STORAGE);
1049 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1050 if (thisType->ty_Op == TY_CLASS) {
1051 /* XXX sg_ClassList? right sg? */
1052 /* XXX correct visibility? */
1053 if (d->d_Search == NULL) {
1054 d->d_Search = d->d_StorDecl.ed_Type->
1055 ty_ClassType.et_SemGroup;
1057 d->d_StorDecl.ed_Type =
1058 AllocClassType(&sg->sg_ClassList,
1059 sg->sg_Stmt->st_ClassStmt.es_Super,
1060 sg->sg_Stmt->st_MyGroup,
1063 dassert_decl(d, thisType->ty_Op == TY_REFTO);
1065 } else if (stype->ty_SQFlags & SF_GMETHOD) {
1069 asg = type->ty_ProcType.et_ArgsType->
1070 ty_ArgsType.et_SemGroup;
1071 d = getHead(&asg->sg_DeclList);
1073 dassert_decl(d, d->d_Id == String_This &&
1074 d->d_Op == DOP_TYPEDEF);
1075 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1076 dassert_decl(d, d->d_TypedefDecl.ed_Type->ty_Op ==
1078 /* XXX sg_ClassList? right sg? */
1079 /* XXX correct visibility? */
1080 if (d->d_Search == NULL) {
1081 d->d_Search = d->d_TypedefDecl.ed_Type->
1082 ty_ClassType.et_SemGroup;
1084 d->d_TypedefDecl.ed_Type =
1085 AllocClassType(&sg->sg_ClassList,
1086 sg->sg_Stmt->st_ClassStmt.es_Super,
1087 sg->sg_Stmt->st_MyGroup,
1096 * e.g. so elements in a superclass will see refined elements
1097 * in the subclass. Note that the original import semgroup
1098 * is left intact so the semantic search mechanism uses it
1099 * when the base sg (typically a subclass) fails.
1101 type = AllocUnresolvedType(
1102 stype->ty_UnresType.et_ImportSemGroup,
1104 stype->ty_UnresType.et_DottedId,
1108 dassert_type(stype, 0);
1111 if (type != stype) {
1112 type->ty_Flags = stype->ty_Flags;
1113 type->ty_Bytes = stype->ty_Bytes;
1114 type->ty_AlignMask = stype->ty_AlignMask;
1115 type->ty_Visibility = stype->ty_Visibility;
1117 if (stype->ty_AssExp || stype->ty_SQFlags != type->ty_SQFlags) {
1118 type = TypeToQualType(type, NULL,
1120 SetDupExp(sg, stype->ty_AssExp));
1127 * SameType() - return 1 if the types are equilvalent, 0 if they are not.
1129 * The sqFlags for t2 may be overriden. If you do not wish to override
1130 * the sqFlags for t2, pass t2->ty_SQFlags for sqFlags. The override
1131 * only applies to the top level of the type.
1133 * Types can be aliased - for example, two different type structures
1134 * may point to the same class data.
1136 * XXX this needs a lot of work. We really need to guarentee
1137 * some level of uniqueness for non-qualified type elements.
1140 SameType(Type *t1, Type *t2, int sqFlags2)
1145 if (t1->ty_Op != t2->ty_Op)
1147 if (t1->ty_SQFlags != sqFlags2)
1151 if (t1->ty_ImportType.et_SemGroup ==
1152 t2->ty_ImportType.et_SemGroup)
1158 if (t1->ty_ClassType.et_SemGroup ==
1159 t2->ty_ClassType.et_SemGroup &&
1160 t1->ty_ClassType.et_Super ==
1161 t2->ty_ClassType.et_Super)
1167 t1 = t1->ty_CPtrType.et_Type;
1168 t2 = t2->ty_CPtrType.et_Type;
1171 t1 = t1->ty_PtrType.et_Type;
1172 t2 = t2->ty_PtrType.et_Type;
1175 t1 = t1->ty_RefType.et_Type;
1176 t2 = t2->ty_RefType.et_Type;
1191 dassert_type(t1, 0);
1194 sqFlags2 = t2->ty_SQFlags;
1200 * SimilarType() - like SameType(), but ignores storage qualifiers and
1201 * if t2 is varargs, compares the original version.
1203 * Used when casting t2 (rhs) to t1 (lhs).
1206 SimilarType(Type *t1, Type *t2)
1208 if (t2->ty_Op == TY_VAR)
1209 t2 = t2->ty_VarType.et_Type;
1215 * Normally we fail if the ops do not match, but it is legal to
1216 * cast a pointer (t2) to a reference type (t1) if the ref type
1217 * is its superclass. It is also legal to cast an array to a
1218 * pointer or C pointer.
1220 if (t2->ty_Op != t1->ty_Op) {
1224 if (t2->ty_Op == TY_PTRTO && t1->ty_Op == TY_REFTO) {
1225 t1 = t1->ty_RefType.et_Type;
1226 t2 = t2->ty_PtrType.et_Type;
1227 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1235 if (t2->ty_Op == TY_ARYOF && t1->ty_Op == TY_PTRTO) {
1236 t1 = t1->ty_PtrType.et_Type;
1237 t2 = t2->ty_AryType.et_Type;
1238 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1246 if (t2->ty_Op == TY_ARYOF && t1->ty_Op == TY_CPTRTO) {
1247 t1 = t1->ty_PtrType.et_Type;
1248 t2 = t2->ty_AryType.et_Type;
1249 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1257 if (t2->ty_Op != t1->ty_Op)
1261 if (t1->ty_ImportType.et_SemGroup ==
1262 t2->ty_ImportType.et_SemGroup) {
1267 if (t1->ty_ClassType.et_SemGroup ==
1268 t2->ty_ClassType.et_SemGroup &&
1269 t1->ty_ClassType.et_Super ==
1270 t2->ty_ClassType.et_Super)
1276 t1 = t1->ty_CPtrType.et_Type;
1277 t2 = t2->ty_CPtrType.et_Type;
1280 t1 = t1->ty_PtrType.et_Type;
1281 t2 = t2->ty_PtrType.et_Type;
1285 * Reference types are similar if the lhs is a
1286 * superclass of the rhs and partially compatible
1287 * (only method call changes and extensions).
1289 t1 = t1->ty_RefType.et_Type;
1290 t2 = t2->ty_RefType.et_Type;
1291 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1298 * Compare the elements making up the compound type.
1301 return(SimilarSemGroup(t1->ty_CompType.et_SemGroup,
1302 t2->ty_CompType.et_SemGroup));
1316 dassert_type(t1, 0);
1324 * SimilarSemGroup() - check to see if the storage underlying the two
1325 * semantic groups is compatible.
1328 SimilarSemGroup(SemGroup *s1, SemGroup *s2)
1333 if (s1->sg_Bytes != s2->sg_Bytes)
1335 d1 = getHead(&s1->sg_DeclList);
1336 d2 = getHead(&s2->sg_DeclList);
1339 ((d1->d_Op & DOPF_STORAGE) == 0 ||
1340 d1->d_Op == DOP_GLOBAL_STORAGE)
1342 d1 = getSucc(&s1->sg_DeclList, &d1->d_Node);
1345 ((d2->d_Op & DOPF_STORAGE) == 0 ||
1346 d2->d_Op == DOP_GLOBAL_STORAGE)
1348 d2 = getSucc(&s2->sg_DeclList, &d2->d_Node);
1350 if (d1 == NULL || d2 == NULL)
1352 if (SimilarType(d1->d_StorDecl.ed_Type,
1353 d2->d_StorDecl.ed_Type) == 0) {
1356 d1 = getSucc(&s1->sg_DeclList, &d1->d_Node);
1357 d2 = getSucc(&s2->sg_DeclList, &d2->d_Node);
1360 return(0); /* compare bad */
1361 return(1); /* compare good */
1365 TypeToStr(Type *type, char **pstr)
1374 static char *SaveStr[8];
1375 static int SaveIndex;
1378 safe_asprintf(&str, "(null)");
1380 switch(type->ty_Op) {
1382 st = type->ty_ClassType.et_SemGroup->sg_Stmt;
1383 dassert(st->st_Op == ST_Class);
1385 safe_asprintf(&str, "CLASS(%s",
1386 st->st_ClassStmt.es_Decl->d_Id);
1388 LexPrintRef(&st->st_LexRef, 0);
1389 safe_asprintf(&str, "PARSE(%p) CLASS(%s from",
1390 type->ty_ClassType.et_SemGroup->sg_Parse,
1391 st->st_ClassStmt.es_Decl->d_Id);
1393 while (st && st->st_Op != ST_Import)
1397 safe_replacef(&str, "%s <%s%s>",
1399 st->st_ImportStmt.es_Path,
1400 st->st_ImportStmt.es_File);
1404 safe_replacef(&str, "%s)", str);
1407 st = type->ty_ImportType.et_SemGroup->sg_Stmt;
1408 dassert(st->st_Op == ST_Module);
1409 safe_asprintf(&str, "IMPORT(%s)",
1410 st->st_LexRef.lr_Lex->l_Path);
1413 TypeToStr(type->ty_CPtrType.et_Type, &s1);
1414 safe_replacef(&str, "(CLANG)*%s", s1);
1417 TypeToStr(type->ty_PtrType.et_Type, &s1);
1418 safe_replacef(&str, "*%s", s1);
1421 TypeToStr(type->ty_RefType.et_Type, &s1);
1422 safe_replacef(&str, "@%s", s1);
1425 TypeToStr(type->ty_AryType.et_Type, &s1);
1426 safe_replacef(&str, "%s[]", s1);
1429 sg = type->ty_CompType.et_SemGroup;
1430 safe_asprintf(&str, "COMPOUND(");
1432 for (d = getHead(&sg->sg_DeclList);
1434 d = getSucc(&sg->sg_DeclList, &d->d_Node)) {
1435 if (d->d_Op & DOPF_STORAGE) {
1436 TypeToStr(d->d_StorDecl.ed_Type, &s1);
1438 safe_replacef(&str, "%s,%s",
1441 safe_replacef(&str, "%s%s",
1447 safe_replacef(&str, "%s)", str);
1450 safe_replacef(&str, "VAR");
1453 sg = type->ty_ArgsType.et_SemGroup;
1454 safe_asprintf(&str, "ARGS(");
1456 for (d = getHead(&sg->sg_DeclList);
1458 d = getSucc(&sg->sg_DeclList, &d->d_Node)) {
1459 if (d->d_Op & DOPF_STORAGE) {
1460 TypeToStr(d->d_StorDecl.ed_Type, &s1);
1462 safe_replacef(&str, "%s,%s",
1465 safe_replacef(&str, "%s%s",
1471 safe_replacef(&str, "%s)", str);
1475 TypeToStr(type->ty_ProcType.et_RetType, &s1);
1476 TypeToStr(type->ty_ProcType.et_ArgsType, &s2);
1477 safe_asprintf(&str, "%s %s", s1, s2);
1481 safe_asprintf(&str, "STORAGE(%d)",
1482 type->ty_StorType.et_Bytes);
1485 safe_asprintf(&str, "DYNAMIC");
1488 safe_asprintf(&str, "UNRES(");
1489 for (count = 0; type->ty_UnresType.et_DottedId[count]; ++count) {
1490 safe_replacef(&str, "%s%s",
1491 str, type->ty_UnresType.et_DottedId[count]);
1493 safe_replacef(&str, "%s,%s",
1494 str, type->ty_UnresType.et_DottedId[count]);
1497 safe_replacef(&str, "%s)", str);
1500 safe_asprintf(&str, "?");
1510 safe_free(&SaveStr[SaveIndex]);
1511 SaveStr[SaveIndex] = str;
1512 SaveIndex = (SaveIndex + 1) % arysize(SaveStr);
1517 typedef struct TypeRegNode {
1523 List TypeRegList = INITLIST(TypeRegList);
1526 InternalRegisterType(const char *str, Type *type)
1528 static int Special = SPECIALF_REGISTERED|SPECIALF_INTERNAL|1;
1531 dassert(Special & SPECIALF_MASK);
1532 tr = zalloc(sizeof(TypeRegNode));
1533 tr->tr_Id = StrTableAlloc(str, strlen(str), Special++);
1535 addTail(&TypeRegList, &tr->tr_Node);
1539 InternalRegisteredTypeLookup(string_t id)
1544 tr = getHead(&TypeRegList);
1546 tr = getSucc(&TypeRegList, &tr->tr_Node)
1548 if (tr->tr_Id == id)
1549 return(tr->tr_Type);