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;
77 initRefType(Type *type, Type *refto, int sqflags)
79 initQualType(type, &refto->ty_QList, TY_REFTO, sqflags);
80 type->ty_RefType.et_Type = refto;
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(&IntPtrType, NULL, TY_UNRESOLVED);
103 initType(&UIntPtrType, NULL, TY_UNRESOLVED);
104 initType(&OffType, NULL, TY_UNRESOLVED);
105 initType(&SizeType, NULL, TY_UNRESOLVED);
106 initType(&SSizeType, NULL, TY_UNRESOLVED);
108 initType(&Float32Type, NULL, TY_UNRESOLVED);
109 initType(&Float64Type, NULL, TY_UNRESOLVED);
110 initType(&Float128Type, NULL, TY_UNRESOLVED);
112 initQualType(&CCharType, NULL, TY_UNRESOLVED, SF_CONST);
113 initPtrType(&StrType, &CCharType, 0);
114 initPtrType(&CharPtrType, &UInt8Type, 0);
115 initPtrType(&CharPtrPtrType, &CharPtrType, 0);
116 initPtrType(&VoidPtrType, &VoidType, 0);
117 initRefType(&VoidRefType, &VoidType, 0);
118 initPtrType(&CVoidPtrType, &VoidType, SF_CONST);
119 initPtrType(&LVoidPtrType, &VoidType, SF_LVALUE);
121 initType(&PointerType, NULL, TY_UNRESOLVED);
123 StrTableAlloc("__internal_void", 15, SPECIAL_INTERNAL_VOID);
124 StrTableAlloc("__internal_bool", 15, SPECIAL_INTERNAL_BOOL);
125 StrTableAlloc("__internal_int8", 15, SPECIAL_INTERNAL_INT8);
126 StrTableAlloc("__internal_uint8", 16, SPECIAL_INTERNAL_UINT8);
127 StrTableAlloc("__internal_int16", 16, SPECIAL_INTERNAL_INT16);
128 StrTableAlloc("__internal_uint16", 17, SPECIAL_INTERNAL_UINT16);
129 StrTableAlloc("__internal_int32", 16, SPECIAL_INTERNAL_INT32);
130 StrTableAlloc("__internal_uint32", 17, SPECIAL_INTERNAL_UINT32);
131 StrTableAlloc("__internal_int64", 16, SPECIAL_INTERNAL_INT64);
132 StrTableAlloc("__internal_uint64", 17, SPECIAL_INTERNAL_UINT64);
134 StrTableAlloc("__internal_float32", 18, SPECIAL_INTERNAL_FLOAT32);
135 StrTableAlloc("__internal_float64", 18, SPECIAL_INTERNAL_FLOAT64);
136 StrTableAlloc("__internal_float128", 19, SPECIAL_INTERNAL_FLOAT128);
138 StrTableAlloc("__internal_pointer", 18, SPECIAL_INTERNAL_POINTER);
139 StrTableAlloc("__internal_intptr", 17, SPECIAL_INTERNAL_INTPTR);
140 StrTableAlloc("__internal_uintptr", 18, SPECIAL_INTERNAL_UINTPTR);
141 StrTableAlloc("__internal_size", 15, SPECIAL_INTERNAL_SIZE);
142 StrTableAlloc("__internal_ssize", 16, SPECIAL_INTERNAL_SSIZE);
143 StrTableAlloc("__internal_off", 14, SPECIAL_INTERNAL_OFF);
145 StrTableAlloc("__internal_numeric", 18, SPECIAL_INTERNAL_NUMERIC);
146 StrTableAlloc("__internal_integer", 18, SPECIAL_INTERNAL_INTEGER);
148 StrTableAlloc("__count", 7, SPECIAL_COUNT);
149 StrTableAlloc("__data", 6, SPECIAL_DATA);
150 StrTableAlloc("__varcount", 10, SPECIAL_VAR_COUNT);
151 StrTableAlloc("__vardata", 9, SPECIAL_VAR_DATA);
152 StrTableAlloc("__typeid", 8, SPECIAL_TYPEID);
153 StrTableAlloc("__typestr", 9, SPECIAL_TYPESTR);
154 StrTableAlloc("NULL", 4, SPECIAL_NULL);
158 InternalTypeAttach(Parse *p, int t, Declaration *d)
163 dassert_decl(d, d->d_Op == DOP_TYPEDEF);
165 if ((s = StrTableSpecial(d->d_Id)) & SPECIALF_INTERNAL) {
167 case SPECIAL_INTERNAL_VOID:
170 case SPECIAL_INTERNAL_BOOL:
173 case SPECIAL_INTERNAL_INT8:
176 case SPECIAL_INTERNAL_UINT8:
179 case SPECIAL_INTERNAL_INT16:
182 case SPECIAL_INTERNAL_UINT16:
185 case SPECIAL_INTERNAL_INT32:
188 case SPECIAL_INTERNAL_UINT32:
191 case SPECIAL_INTERNAL_INT64:
194 case SPECIAL_INTERNAL_UINT64:
198 case SPECIAL_INTERNAL_FLOAT32:
199 itype = &Float32Type;
201 case SPECIAL_INTERNAL_FLOAT64:
202 itype = &Float64Type;
204 case SPECIAL_INTERNAL_FLOAT128:
205 itype = &Float128Type;
208 case SPECIAL_INTERNAL_INTPTR:
211 case SPECIAL_INTERNAL_UINTPTR:
212 itype = &UIntPtrType;
214 case SPECIAL_INTERNAL_OFF:
217 case SPECIAL_INTERNAL_SIZE:
220 case SPECIAL_INTERNAL_SSIZE:
224 case SPECIAL_INTERNAL_POINTER:
225 itype = &PointerType;
227 case SPECIAL_INTERNAL_NUMERIC:
228 itype = &NumericType;
230 case SPECIAL_INTERNAL_INTEGER:
231 itype = &IntegerType;
234 itype = InternalRegisteredTypeLookup(d->d_Id);
241 if (itype->ty_Op != TY_UNRESOLVED) {
242 t = LexError(&p->p_Token, TOK_ERR_DUPLICATE_ATTACH);
244 Type *ntype = d->d_TypedefDecl.ed_Type;
246 TypeToQualType(ntype, itype, ntype->ty_SQFlags, NULL);
249 * fixup for const int8 pointers... we did not have
250 * a QList to put CCharType on until now. It will
251 * wind up on the SemGroup's sg_ClassList.
253 if (itype == &UInt8Type) {
254 TypeToQualType(ntype, &CCharType,
255 ntype->ty_SQFlags | SF_CONST, NULL);
259 t = LexError(&p->p_Token, TOK_ERR_UNRECOGNIZED_ATTACH);
265 AllocType(List *list, int op)
267 Type *type = zalloc(sizeof(Type));
269 initType(type, list, op);
274 * XXX match the compound type(s)
276 * May be used to generate a varargs compound type, in which case the
277 * semgroup may already be resolved.
280 AllocCompoundType(SemGroup *sg)
284 type = AllocType(&CompoundTypeList, TY_COMPOUND);
285 type->ty_CompType.et_SemGroup = sg;
286 dassert((sg->sg_Flags & SGF_RESOLVED) == 0);
291 * XXX match the compound type(s)
294 AllocArgsType(SemGroup *sg)
298 type = AllocType(&ArgsTypeList, TY_ARGS);
299 type->ty_ArgsType.et_SemGroup = sg;
304 AllocStorageType(runesize_t bytes)
309 type = getHead(&StorageTypeList);
311 type = getSucc(&StorageTypeList, &type->ty_Node)
313 if (type->ty_Op == TY_STORAGE &&
314 type->ty_StorType.et_Bytes == bytes
319 type = AllocType(&StorageTypeList, TY_STORAGE);
320 type->ty_StorType.et_Bytes = bytes;
325 AllocUnresolvedType(SemGroup *isg, SemGroup *sg, string_t *ary, int eatAry)
329 dassert_semgrp(sg, ary != NULL);
332 type = getHead(&sg->sg_ClassList);
334 type = getSucc(&sg->sg_ClassList, &type->ty_Node)
338 if (type->ty_Op != TY_UNRESOLVED)
340 if (type->ty_UnresType.et_ImportSemGroup != isg)
343 for (i = 0; ary[i]; ++i) {
344 if (ary[i] != type->ty_UnresType.et_DottedId[i])
347 if (ary[i] == NULL &&
348 type->ty_UnresType.et_DottedId[i] == NULL) {
354 type = AllocType((sg ? &sg->sg_ClassList : NULL), TY_UNRESOLVED);
355 type->ty_UnresType.et_DottedId = ary;
356 type->ty_UnresType.et_SemGroup = sg; /* may be NULL */
357 type->ty_UnresType.et_ImportSemGroup = isg; /* may be NULL */
362 * AllocClassType() - allocate a type representing a the semgroup which
363 * in turn represents (typically) a class.
366 AllocClassType(List *list, Type *super, SemGroup *sg, int visibility)
370 for (type = getHead(list); type; type = getSucc(list, &type->ty_Node)) {
371 if (type->ty_Op == TY_CLASS &&
372 type->ty_ClassType.et_SemGroup == sg &&
373 type->ty_ClassType.et_Super == super &&
374 type->ty_Visibility == visibility
379 type = AllocType(list, TY_CLASS);
380 type->ty_ClassType.et_SemGroup = sg;
381 type->ty_ClassType.et_Super = super;
382 type->ty_Visibility = visibility;
387 AllocImportType(List *list, SemGroup *sg, int visibility)
389 Type *type = AllocType(list, TY_IMPORT);
391 type->ty_ImportType.et_SemGroup = sg;
392 type->ty_Visibility = visibility;
397 TypeToQualType(Type *otype, Type *ntype, int sqFlags, Exp *exp)
400 * Combine with existing qualifiers, Shortcut if no changes made.
403 sqFlags == otype->ty_SQFlags &&
404 (exp == NULL || exp == otype->ty_AssExp)
410 * See if we already have a matching qualified type (only if storage
411 * for the new type is not being provided). Note: the provided storage
412 * has already been initType()d
415 for (ntype = getHead(otype->ty_SQList);
417 ntype = getSucc(otype->ty_SQList, &ntype->ty_Node)
419 if (ntype->ty_Op == otype->ty_Op &&
420 ntype->ty_SQFlags == sqFlags &&
421 (exp == NULL || ntype->ty_AssExp == exp)
423 if (SameType(ntype, otype, sqFlags))
430 * Build a new qualified type and set its qualifiers, then duplicate
431 * appropriate sections of the old type.
434 ntype = AllocType(otype->ty_SQList, otype->ty_Op);
435 } else if (ntype->ty_SQList == NULL) {
436 ntype->ty_SQList = otype->ty_SQList;
437 addTail(ntype->ty_SQList, &ntype->ty_Node);
441 * Set the op and the expression. Unlike SQFlags, if exp is passed as
442 * NULL we inherit the old type's default.
444 * The DupExp() call here is special, see DupExp()'s handling of
447 * Normally DupExp() is called during resolution prior to ex_Decl
448 * being set. This is the one case where it may be called with
449 * ex_Decl already set.
451 ntype->ty_Op = otype->ty_Op;
453 ntype->ty_AssExp = exp;
454 else if (otype->ty_AssExp)
455 ntype->ty_AssExp = SetDupExp(NULL, otype->ty_AssExp);
456 ntype->ty_SQFlags = sqFlags;
457 ntype->ty_Visibility = otype->ty_Visibility;
459 switch(otype->ty_Op) {
461 ntype->ty_ClassType.et_SemGroup =
462 otype->ty_ClassType.et_SemGroup;
463 ntype->ty_ClassType.et_Super = otype->ty_ClassType.et_Super;
466 ntype->ty_Visibility = otype->ty_Visibility;
467 ntype->ty_ImportType.et_SemGroup = otype->ty_ImportType.et_SemGroup;
470 ntype->ty_CPtrType.et_Type = otype->ty_CPtrType.et_Type;
473 ntype->ty_PtrType.et_Type = otype->ty_PtrType.et_Type;
476 ntype->ty_RefType.et_Type = otype->ty_RefType.et_Type;
480 * note: multiple type structures may share the same array size
481 * expression in simple qualified-type cases. YYY XXX bad bad.
483 ntype->ty_AryType.et_Type = otype->ty_AryType.et_Type;
484 ntype->ty_AryType.et_ArySize = otype->ty_AryType.et_ArySize;
485 ntype->ty_AryType.et_SemGroup = otype->ty_AryType.et_SemGroup;
488 ntype->ty_CompType.et_SemGroup = otype->ty_CompType.et_SemGroup;
491 ntype->ty_VarType.et_Type = otype->ty_VarType.et_Type;
492 ntype->ty_VarType.et_SemGroup = otype->ty_VarType.et_SemGroup;
495 ntype->ty_ArgsType.et_SemGroup = otype->ty_ArgsType.et_SemGroup;
498 ntype->ty_ProcType.et_ArgsType = otype->ty_ProcType.et_ArgsType;
499 ntype->ty_ProcType.et_RetType = otype->ty_ProcType.et_RetType;
500 ntype->ty_ProcType.et_ArgCount = otype->ty_ProcType.et_ArgCount;
503 ntype->ty_StorType.et_Bytes = otype->ty_StorType.et_Bytes;
507 * It is not legal to qualify a dynamic type other then to
508 * add or remove SF_LVALUE.
513 ntype->ty_UnresType.et_DottedId =
514 otype->ty_UnresType.et_DottedId;
515 ntype->ty_UnresType.et_SemGroup =
516 otype->ty_UnresType.et_SemGroup;
519 dassert_type(otype, 0);
525 * XXX match the procedure type(s)
528 TypeToProcType(Type *rtype, Type *atype)
535 dassert_type(atype, atype->ty_Op == TY_ARGS);
536 sg = atype->ty_CompType.et_SemGroup;
539 d = getHead(&sg->sg_DeclList);
541 d = getSucc(&sg->sg_DeclList, &d->d_Node)
546 type = getHead(&rtype->ty_QList);
548 type = getSucc(&rtype->ty_QList, &type->ty_Node)
550 if (type->ty_Op == TY_PROC) {
551 if (type->ty_ProcType.et_ArgsType == atype &&
552 type->ty_ProcType.et_RetType == rtype &&
553 type->ty_ProcType.et_ArgCount == count
555 puts("SG1"); /* YYY */
560 type = AllocType(&rtype->ty_QList, TY_PROC);
561 type->ty_ProcType.et_ArgsType = AllocArgsType(sg);
562 type->ty_ProcType.et_RetType = rtype;
563 type->ty_ProcType.et_ArgCount = count;
568 * Convert type to pointer-to-type
571 TypeToPtrType(Type *otype)
575 for (type = getHead(&otype->ty_QList);
577 type = getSucc(&otype->ty_QList, &type->ty_Node)
579 if (type->ty_Op == TY_PTRTO)
582 type = AllocType(&otype->ty_QList, TY_PTRTO);
583 type->ty_PtrType.et_Type = otype;
588 * Convert type to pointer-to-type
591 TypeToCPtrType(Type *otype)
595 for (type = getHead(&otype->ty_QList);
597 type = getSucc(&otype->ty_QList, &type->ty_Node)
599 if (type->ty_Op == TY_CPTRTO)
602 type = AllocType(&otype->ty_QList, TY_CPTRTO);
603 type->ty_CPtrType.et_Type = otype;
608 * Convert type to ref-to-type
610 * A reference type is similar to a pointer type except that the
611 * resolver is not able to entirely know what it is pointing to.
612 * The reference type is a superclass, but the actual type is
613 * stored in the run-time structure.
616 TypeToRefType(Type *otype)
620 for (type = getHead(&otype->ty_QList);
622 type = getSucc(&otype->ty_QList, &type->ty_Node)
624 if (type->ty_Op == TY_REFTO)
627 type = AllocType(&otype->ty_QList, TY_REFTO);
628 type->ty_RefType.et_Type = otype;
633 TypeToAryType(Type *otype, Exp *exp, SemGroup *sg)
638 * XXX handle constant expression optimization for QList
639 * XXX handle qualifiers
641 type = AllocType(&otype->ty_QList, TY_ARYOF);
642 type->ty_AryType.et_ArySize = exp;
643 type->ty_AryType.et_Type = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
644 type->ty_AryType.et_SemGroup = sg;
645 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
652 TypeToRunTimeAryType(Type *otype, int count)
655 Type *t2 = DelTypeQual(otype, SF_MASK_ARY_INHERIT);
657 for (type = getHead(&otype->ty_QList);
659 type = getSucc(&otype->ty_QList, &type->ty_Node)
661 if (type->ty_Op == TY_ARYOF &&
662 type->ty_AryType.et_Type == t2 &&
663 type->ty_AryType.et_Count == count &&
665 (otype->ty_SQFlags & SF_MASK_ARY_INHERIT)
670 type = AllocType(&otype->ty_QList, TY_ARYOF);
671 type->ty_AryType.et_Count = count;
672 type->ty_AryType.et_Type = t2;
673 type->ty_SQFlags |= otype->ty_SQFlags & SF_MASK_ARY_INHERIT;
680 TypeToVarType(Type *otype, SemGroup *sg)
684 dassert(sg->sg_Flags & SGF_RESOLVED);
685 dassert(otype->ty_Flags & TF_RESOLVED);
687 for (type = getHead(&otype->ty_QList);
689 type = getSucc(&otype->ty_QList, &type->ty_Node)
691 if (type->ty_Op == TY_VAR &&
692 type->ty_VarType.et_Type == otype &&
693 type->ty_VarType.et_SemGroup == sg
695 puts("SG2"); /* YYY */
699 type = AllocType(&otype->ty_QList, TY_VAR);
700 type->ty_VarType.et_Type = otype;
701 type->ty_VarType.et_SemGroup = sg;
703 /* XXX doesn't work for var-args */
704 if (sg->sg_Flags & SGF_RESOLVED) {
705 type->ty_Flags |= TF_RESOLVED;
706 type->ty_Bytes = sg->sg_Bytes;
707 type->ty_AlignMask = sg->sg_AlignMask;
714 * ChangeType() - given pointer, C pointer, or array of something,
715 * return 'op' of something instead.
718 ChangeType(Type *type, int op)
720 switch(type->ty_Op) {
724 type = TypeToCPtrType(type->ty_PtrType.et_Type);
727 type = TypeToAryType(type->ty_PtrType.et_Type,
737 type = TypeToPtrType(type->ty_CPtrType.et_Type);
740 type = TypeToAryType(type->ty_CPtrType.et_Type,
750 type = TypeToPtrType(type->ty_AryType.et_Type);
753 type = TypeToCPtrType(type->ty_AryType.et_Type);
766 * BaseType() - return base type
768 * Traverse the type to locate the base type. Store the base type
769 * in *ptype and return the SemGroup, or return NULL if the base type
770 * does not have a SemGroup.
773 BaseType(Type **ptype)
778 switch(type->ty_Op) {
780 type = type->ty_CPtrType.et_Type;
783 type = type->ty_PtrType.et_Type;
786 type = type->ty_RefType.et_Type;
789 type = type->ty_AryType.et_Type;
797 switch(type->ty_Op) {
799 return(type->ty_ClassType.et_SemGroup);
801 return(type->ty_CompType.et_SemGroup);
803 return(type->ty_ArgsType.et_SemGroup);
811 dassert_type(type, 0);
812 return(NULL); /* avoid compiler complaints */
817 * MatchType() - Match two types
819 * Match two types as if we wanted to cast type to super or use
822 * SG_COMPAT_FULL Type is a subclass, methods and storage are
823 * compatible (storage may be extended).
825 * SG_COMPAT_PART Type is a subclass, methods are compatible
826 * but storage is not.
828 * SG_COMPAT_SUBCLASS Type is a subclass, but the methods are
829 * not directly compatible (the methods that
830 * propogate down must be regenerated).
831 * SG_COMPAT_FAIL Type is not even a subclass
833 * XXX we are skipping qualifiers
836 MatchType(Type *super, Type *type)
838 int r = SG_COMPAT_FULL;
840 while (super && type) {
841 SemGroup *sg1 = NULL;
842 SemGroup *sg2 = NULL;
844 if (type->ty_Op != super->ty_Op) {
845 if (super->ty_Op == TY_REFTO &&
846 type->ty_Op == TY_PTRTO) {
847 super = super->ty_RefType.et_Type;
848 type = type->ty_PtrType.et_Type;
849 r = MatchType(super, type);
857 * Relaxed storage qualifiers. The less refined target type
858 * (super) can omit certain storage qualifiers and still match.
860 * NOTE: Actual casts may do more stringent tests.
862 if (super->ty_SQFlags != type->ty_SQFlags) {
863 if ((super->ty_SQFlags & ~(SF_LVALUE)) !=
864 (type->ty_SQFlags & ~SF_RELAXING_MASK)) {
870 switch(super->ty_Op) {
873 * type can be a subclass of super
875 if (type->ty_ClassType.et_SemGroup ==
876 super->ty_ClassType.et_SemGroup) {
879 r = type->ty_ClassType.et_SemGroup->sg_Compat;
881 if (r < SG_COMPAT_PART)
884 while ((type = type->ty_ClassType.et_Super) != NULL) {
885 if (type->ty_ClassType.et_SemGroup ==
886 super->ty_ClassType.et_SemGroup) {
889 if (r < type->ty_ClassType.et_SemGroup->sg_Compat)
890 r = type->ty_ClassType.et_SemGroup->sg_Compat;
892 if (type == NULL) /* not even a subclass */
897 * type can be a subclass of super
903 type = type->ty_CPtrType.et_Type;
904 super = super->ty_CPtrType.et_Type;
907 type = type->ty_PtrType.et_Type;
908 super = super->ty_PtrType.et_Type;
911 type = type->ty_RefType.et_Type;
912 super = super->ty_RefType.et_Type;
915 type = type->ty_AryType.et_Type;
916 super = super->ty_AryType.et_Type;
920 sg1 = super->ty_CompType.et_SemGroup;
921 sg2 = type->ty_CompType.et_SemGroup;
924 sg1 = super->ty_ArgsType.et_SemGroup;
925 sg2 = type->ty_ArgsType.et_SemGroup;
928 r = MatchType(super->ty_VarType.et_Type,
929 type->ty_VarType.et_Type);
935 r = MatchType(super->ty_ProcType.et_ArgsType,
936 type->ty_ProcType.et_ArgsType);
937 v = MatchType(super->ty_ProcType.et_RetType,
938 type->ty_ProcType.et_RetType);
946 if (type->ty_StorType.et_Bytes !=
947 super->ty_StorType.et_Bytes) {
948 r = SG_COMPAT_SUBCLASS;
953 dassert_type(super, 0); /* can't happen */
957 Declaration *sd = getHead(&sg1->sg_DeclList);
958 Declaration *rd = getHead(&sg2->sg_DeclList);
960 int v = MatchDeclTypes(sd, rd);
963 if (r == SG_COMPAT_FAIL)
965 sd = getSucc(&sg1->sg_DeclList, &sd->d_Node);
966 rd = getSucc(&sg2->sg_DeclList, &rd->d_Node);
977 * DupType() - create a duplicate of a type, possibly in a new SemGroup.
979 * This code is used when duplicating procedures and other elements
980 * when merging a superclass into a subclass.
982 * If sg is NULL, stype is simply returned. The case is used when we
983 * try to duplciate an expression with DupExp()... in that case we
984 * want to dup the expression tree but use the same types.
987 DupType(SemGroup *sg, Type *stype)
995 * XXX type may be resolved if it is part of a varargs dup
999 (stype->ty_Flags & (TF_RESOLVED|TF_RESOLVING)) == 0);
1002 switch(stype->ty_Op) {
1005 * This only occurs because the resolver has resolved an
1006 * unresolved type on the original SemGroup. We duplicate
1007 * that on the new SemGroup.
1009 type = AllocClassType(&sg->sg_ClassList,
1010 stype->ty_ClassType.et_Super,
1011 stype->ty_ClassType.et_SemGroup,
1012 stype->ty_Visibility);
1019 type = TypeToCPtrType(DupType(sg, stype->ty_CPtrType.et_Type));
1022 type = TypeToPtrType(DupType(sg, stype->ty_PtrType.et_Type));
1025 type = TypeToRefType(DupType(sg, stype->ty_RefType.et_Type));
1028 type = TypeToAryType(DupType(sg, stype->ty_AryType.et_Type),
1029 SetDupExp(sg, stype->ty_AryType.et_ArySize),
1030 stype->ty_AryType.et_SemGroup);
1033 type = TypeToVarType(DupType(sg, stype->ty_VarType.et_Type),
1034 DupSemGroup(sg, NULL,
1035 stype->ty_VarType.et_SemGroup, 1));
1038 type = AllocCompoundType(
1039 DupSemGroup(sg, NULL,
1040 stype->ty_CompType.et_SemGroup, 1));
1044 * At the moment we always formally duplicate the arguments
1045 * so we can modify them for methods below.
1047 type = AllocArgsType(
1048 DupSemGroup(sg, NULL,
1049 stype->ty_CompType.et_SemGroup, 1));
1052 type = DupType(sg, stype->ty_ProcType.et_RetType);
1053 type = TypeToProcType(type,
1055 stype->ty_ProcType.et_ArgsType));
1058 * If this is a method procedure, we have to change the
1059 * first argument to point at our new subclass. It was
1060 * previously pointing at our superclass. XXX the
1061 * programmer can override the argument. If it isn't a
1062 * reference we have to assert.
1064 if (stype->ty_SQFlags & SF_METHOD) {
1065 SemGroup *asg = type->ty_ProcType.et_ArgsType->ty_ArgsType.et_SemGroup;
1066 Declaration *d = getHead(&asg->sg_DeclList);
1067 Type *thisType = d->d_StorDecl.ed_Type;
1069 dassert_decl(d, d->d_Id == String_This &&
1070 d->d_Op == DOP_ARGS_STORAGE);
1071 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1072 if (thisType->ty_Op == TY_CLASS) {
1073 /* XXX sg_ClassList? right sg? */
1074 /* XXX correct visibility? */
1075 if (d->d_Search == NULL) {
1076 d->d_Search = d->d_StorDecl.ed_Type->
1077 ty_ClassType.et_SemGroup;
1079 d->d_StorDecl.ed_Type =
1080 AllocClassType(&sg->sg_ClassList,
1081 sg->sg_Stmt->st_ClassStmt.es_Super,
1082 sg->sg_Stmt->st_MyGroup,
1085 dassert_decl(d, thisType->ty_Op == TY_REFTO);
1087 } else if (stype->ty_SQFlags & SF_GMETHOD) {
1091 asg = type->ty_ProcType.et_ArgsType->
1092 ty_ArgsType.et_SemGroup;
1093 d = getHead(&asg->sg_DeclList);
1095 dassert_decl(d, d->d_Id == String_This &&
1096 d->d_Op == DOP_TYPEDEF);
1097 dassert_decl(d, sg->sg_Stmt->st_Op == ST_Class);
1098 dassert_decl(d, d->d_TypedefDecl.ed_Type->ty_Op ==
1100 /* XXX sg_ClassList? right sg? */
1101 /* XXX correct visibility? */
1102 if (d->d_Search == NULL) {
1103 d->d_Search = d->d_TypedefDecl.ed_Type->
1104 ty_ClassType.et_SemGroup;
1106 d->d_TypedefDecl.ed_Type =
1107 AllocClassType(&sg->sg_ClassList,
1108 sg->sg_Stmt->st_ClassStmt.es_Super,
1109 sg->sg_Stmt->st_MyGroup,
1118 * e.g. so elements in a superclass will see refined elements
1119 * in the subclass. Note that the original import semgroup
1120 * is left intact so the semantic search mechanism uses it
1121 * when the base sg (typically a subclass) fails.
1123 type = AllocUnresolvedType(
1124 stype->ty_UnresType.et_ImportSemGroup,
1126 stype->ty_UnresType.et_DottedId,
1130 dassert_type(stype, 0);
1133 if (type != stype) {
1134 type->ty_Flags = stype->ty_Flags;
1135 type->ty_Bytes = stype->ty_Bytes;
1136 type->ty_AlignMask = stype->ty_AlignMask;
1137 type->ty_Visibility = stype->ty_Visibility;
1139 if (stype->ty_AssExp || stype->ty_SQFlags != type->ty_SQFlags) {
1140 type = TypeToQualType(type, NULL,
1142 SetDupExp(sg, stype->ty_AssExp));
1149 * SameType() - return 1 if the types are equilvalent, 0 if they are not.
1151 * The sqFlags for t2 may be overriden. If you do not wish to override
1152 * the sqFlags for t2, pass t2->ty_SQFlags for sqFlags. The override
1153 * only applies to the top level of the type.
1155 * Types can be aliased - for example, two different type structures
1156 * may point to the same class data.
1158 * XXX this needs a lot of work. We really need to guarentee
1159 * some level of uniqueness for non-qualified type elements.
1162 SameType(Type *t1, Type *t2, int sqFlags2)
1167 if (t1->ty_Op != t2->ty_Op)
1169 if (t1->ty_SQFlags != sqFlags2)
1173 if (t1->ty_ImportType.et_SemGroup ==
1174 t2->ty_ImportType.et_SemGroup)
1180 if (t1->ty_ClassType.et_SemGroup ==
1181 t2->ty_ClassType.et_SemGroup &&
1182 t1->ty_ClassType.et_Super ==
1183 t2->ty_ClassType.et_Super)
1189 t1 = t1->ty_CPtrType.et_Type;
1190 t2 = t2->ty_CPtrType.et_Type;
1193 t1 = t1->ty_PtrType.et_Type;
1194 t2 = t2->ty_PtrType.et_Type;
1197 t1 = t1->ty_RefType.et_Type;
1198 t2 = t2->ty_RefType.et_Type;
1213 dassert_type(t1, 0);
1216 sqFlags2 = t2->ty_SQFlags;
1222 * SimilarType() - like SameType(), but ignores storage qualifiers and
1223 * if t2 is varargs, compares the original version.
1225 * Used when casting t2 (rhs) to t1 (lhs).
1228 SimilarType(Type *t1, Type *t2)
1230 if (t2->ty_Op == TY_VAR)
1231 t2 = t2->ty_VarType.et_Type;
1237 * Normally we fail if the ops do not match, but it is legal to
1238 * cast a pointer (t2) to a reference type (t1) if the ref type
1239 * is its superclass. It is also legal to cast an array to a
1240 * pointer or C pointer.
1242 if (t2->ty_Op != t1->ty_Op) {
1246 if (t2->ty_Op == TY_PTRTO && t1->ty_Op == TY_REFTO) {
1247 t1 = t1->ty_RefType.et_Type;
1248 t2 = t2->ty_PtrType.et_Type;
1249 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1257 if (t2->ty_Op == TY_ARYOF && t1->ty_Op == TY_PTRTO) {
1258 t1 = t1->ty_PtrType.et_Type;
1259 t2 = t2->ty_AryType.et_Type;
1260 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1268 if (t2->ty_Op == TY_ARYOF && t1->ty_Op == TY_CPTRTO) {
1269 t1 = t1->ty_PtrType.et_Type;
1270 t2 = t2->ty_AryType.et_Type;
1271 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1279 if (t2->ty_Op != t1->ty_Op)
1283 if (t1->ty_ImportType.et_SemGroup ==
1284 t2->ty_ImportType.et_SemGroup) {
1289 if (t1->ty_ClassType.et_SemGroup ==
1290 t2->ty_ClassType.et_SemGroup &&
1291 t1->ty_ClassType.et_Super ==
1292 t2->ty_ClassType.et_Super)
1298 t1 = t1->ty_CPtrType.et_Type;
1299 t2 = t2->ty_CPtrType.et_Type;
1302 t1 = t1->ty_PtrType.et_Type;
1303 t2 = t2->ty_PtrType.et_Type;
1307 * Reference types are similar if the lhs is a
1308 * superclass of the rhs and partially compatible
1309 * (only method call changes and extensions).
1311 t1 = t1->ty_RefType.et_Type;
1312 t2 = t2->ty_RefType.et_Type;
1313 if (MatchType(t1, t2) <= SG_COMPAT_PART) {
1320 * Compare the elements making up the compound type.
1323 return(SimilarSemGroup(t1->ty_CompType.et_SemGroup,
1324 t2->ty_CompType.et_SemGroup));
1338 dassert_type(t1, 0);
1346 * SimilarSemGroup() - check to see if the storage underlying the two
1347 * semantic groups is compatible.
1350 SimilarSemGroup(SemGroup *s1, SemGroup *s2)
1355 if (s1->sg_Bytes != s2->sg_Bytes)
1357 d1 = getHead(&s1->sg_DeclList);
1358 d2 = getHead(&s2->sg_DeclList);
1361 ((d1->d_Op & DOPF_STORAGE) == 0 ||
1362 d1->d_Op == DOP_GLOBAL_STORAGE)
1364 d1 = getSucc(&s1->sg_DeclList, &d1->d_Node);
1367 ((d2->d_Op & DOPF_STORAGE) == 0 ||
1368 d2->d_Op == DOP_GLOBAL_STORAGE)
1370 d2 = getSucc(&s2->sg_DeclList, &d2->d_Node);
1372 if (d1 == NULL || d2 == NULL)
1374 if (SimilarType(d1->d_StorDecl.ed_Type,
1375 d2->d_StorDecl.ed_Type) == 0) {
1378 d1 = getSucc(&s1->sg_DeclList, &d1->d_Node);
1379 d2 = getSucc(&s2->sg_DeclList, &d2->d_Node);
1382 return(0); /* compare bad */
1383 return(1); /* compare good */
1387 TypeToStr(Type *type, char **pstr)
1396 static char *SaveStr[8];
1397 static int SaveIndex;
1400 safe_asprintf(&str, "(null)");
1402 switch(type->ty_Op) {
1404 st = type->ty_ClassType.et_SemGroup->sg_Stmt;
1405 dassert(st->st_Op == ST_Class);
1407 safe_asprintf(&str, "CLASS(%s",
1408 st->st_ClassStmt.es_Decl->d_Id);
1410 LexPrintRef(&st->st_LexRef, 0);
1411 safe_asprintf(&str, "CLASS(%s from",
1412 /* type->ty_ClassType.et_SemGroup->sg_Parse, */
1413 st->st_ClassStmt.es_Decl->d_Id);
1415 while (st && st->st_Op != ST_Import)
1419 safe_replacef(&str, "%s <%s%s>",
1421 st->st_ImportStmt.es_Path,
1422 st->st_ImportStmt.es_File);
1426 safe_replacef(&str, "%s)", str);
1429 st = type->ty_ImportType.et_SemGroup->sg_Stmt;
1430 dassert(st->st_Op == ST_Module);
1431 safe_asprintf(&str, "IMPORT(%s)",
1432 st->st_LexRef.lr_Lex->l_Path);
1435 TypeToStr(type->ty_CPtrType.et_Type, &s1);
1436 safe_replacef(&str, "(CLANG)*%s", s1);
1439 TypeToStr(type->ty_PtrType.et_Type, &s1);
1440 safe_replacef(&str, "*%s", s1);
1443 TypeToStr(type->ty_RefType.et_Type, &s1);
1444 safe_replacef(&str, "@%s", s1);
1447 TypeToStr(type->ty_AryType.et_Type, &s1);
1448 safe_replacef(&str, "%s[]", s1);
1451 sg = type->ty_CompType.et_SemGroup;
1452 safe_asprintf(&str, "COMPOUND(");
1454 for (d = getHead(&sg->sg_DeclList);
1456 d = getSucc(&sg->sg_DeclList, &d->d_Node)) {
1457 if (d->d_Op & DOPF_STORAGE) {
1458 TypeToStr(d->d_StorDecl.ed_Type, &s1);
1460 safe_replacef(&str, "%s,%s",
1463 safe_replacef(&str, "%s%s",
1469 safe_replacef(&str, "%s)", str);
1472 safe_replacef(&str, "VAR");
1475 sg = type->ty_ArgsType.et_SemGroup;
1476 safe_asprintf(&str, "ARGS(");
1478 for (d = getHead(&sg->sg_DeclList);
1480 d = getSucc(&sg->sg_DeclList, &d->d_Node)) {
1481 if (d->d_Op & DOPF_STORAGE) {
1482 TypeToStr(d->d_StorDecl.ed_Type, &s1);
1484 safe_replacef(&str, "%s,%s",
1487 safe_replacef(&str, "%s%s",
1493 safe_replacef(&str, "%s)", str);
1497 TypeToStr(type->ty_ProcType.et_RetType, &s1);
1498 TypeToStr(type->ty_ProcType.et_ArgsType, &s2);
1499 safe_asprintf(&str, "%s %s", s1, s2);
1503 safe_asprintf(&str, "STORAGE(%d)",
1504 type->ty_StorType.et_Bytes);
1507 safe_asprintf(&str, "DYNAMIC");
1510 safe_asprintf(&str, "UNRES(");
1511 for (count = 0; type->ty_UnresType.et_DottedId[count]; ++count) {
1512 safe_replacef(&str, "%s%s",
1513 str, type->ty_UnresType.et_DottedId[count]);
1515 safe_replacef(&str, "%s,%s",
1516 str, type->ty_UnresType.et_DottedId[count]);
1519 safe_replacef(&str, "%s)", str);
1522 safe_asprintf(&str, "?");
1525 if (type->ty_SQFlags) {
1528 safe_asprintf(&sqstr, "(%s%s%s%s%s%s%s)",
1529 ((type->ty_SQFlags & SF_CONST) ? "const " : ""),
1530 ((type->ty_SQFlags & SF_VOLATILE) ? "volatile " : ""),
1531 ((type->ty_SQFlags & SF_VARARGS) ? "varargs " : ""),
1532 ((type->ty_SQFlags & SF_LVALUE) ? "lvalue " : ""),
1533 ((type->ty_SQFlags & SF_NOZERO) ? "__nozero " : ""),
1534 ((type->ty_SQFlags & SF_METHOD) ? "method " : ""),
1535 ((type->ty_SQFlags & SF_GMETHOD) ? "global method " : "")
1537 safe_replacef(&str, "%s%s", sqstr, str);
1547 safe_free(&SaveStr[SaveIndex]);
1548 SaveStr[SaveIndex] = str;
1549 SaveIndex = (SaveIndex + 1) % arysize(SaveStr);
1554 typedef struct TypeRegNode {
1560 List TypeRegList = INITLIST(TypeRegList);
1563 InternalRegisterType(const char *str, Type *type)
1565 static int Special = SPECIALF_REGISTERED|SPECIALF_INTERNAL|1;
1568 dassert(Special & SPECIALF_MASK);
1569 tr = zalloc(sizeof(TypeRegNode));
1570 tr->tr_Id = StrTableAlloc(str, strlen(str), Special++);
1572 addTail(&TypeRegList, &tr->tr_Node);
1576 InternalRegisteredTypeLookup(string_t id)
1581 tr = getHead(&TypeRegList);
1583 tr = getSucc(&TypeRegList, &tr->tr_Node)
1585 if (tr->tr_Id == id)
1586 return(tr->tr_Type);