2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2007 by Digital Mars
5 // written by Walter Bright
6 // http://www.digitalmars.com
7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
9 // See the included readme.txt for details.
11 /* NOTE: This file has been patched from the original DMD distribution to
12 work with the GDC compiler.
14 Modified by David Friedman, January 2007
27 #include "stringtable.h"
29 #include "arraytypes.h"
30 #include "expression.h"
35 struct StructDeclaration
;
36 struct ClassDeclaration
;
37 struct VarDeclaration
;
38 struct EnumDeclaration
;
39 struct TypedefDeclaration
;
40 struct TypeInfoDeclaration
;
42 struct TemplateInstance
;
50 union tree_node
; typedef union tree_node TYPE
;
53 typedef struct TYPE type
;
61 Tarray
, // dynamic array
62 Tsarray
, // static array
63 Taarray
, // associative array
118 extern int Tptrdiff_t
;
124 unsigned char mod
; // modifiers (MODconst, MODinvariant)
125 #define MODconst 1 // type is const
126 #define MODinvariant 2 // type is invariant
129 Type
*pto
; // merged pointer to this type
130 Type
*mbe
; // maybe null of this pointer or reference
131 Type
*rto
; // reference to this type
132 Type
*arrayof
; // array of this type
133 TypeInfoDeclaration
*vtinfo
; // TypeInfo object for this Type
135 type
*ctype
; // for back end
137 #define tvoid basic[Tvoid]
138 #define tint8 basic[Tint8]
139 #define tuns8 basic[Tuns8]
140 #define tint16 basic[Tint16]
141 #define tuns16 basic[Tuns16]
142 #define tint32 basic[Tint32]
143 #define tuns32 basic[Tuns32]
144 #define tint64 basic[Tint64]
145 #define tuns64 basic[Tuns64]
146 #define tfloat32 basic[Tfloat32]
147 #define tfloat64 basic[Tfloat64]
148 #define tfloat80 basic[Tfloat80]
150 #define timaginary32 basic[Timaginary32]
151 #define timaginary64 basic[Timaginary64]
152 #define timaginary80 basic[Timaginary80]
154 #define tcomplex32 basic[Tcomplex32]
155 #define tcomplex64 basic[Tcomplex64]
156 #define tcomplex80 basic[Tcomplex80]
158 #define tbit basic[Tbit]
159 #define tbool basic[Tbool]
160 #define tchar basic[Tchar]
161 #define twchar basic[Twchar]
162 #define tdchar basic[Tdchar]
164 // Some special types
165 #define tshiftcnt tint32 // right side of shift expression
166 // #define tboolean tint32 // result of boolean expression
167 #define tboolean tbool // result of boolean expression
168 #define tindex basic[Tindex] // array/ptr index
169 static Type
*tvoidptr
; // void*
170 #define terror basic[Terror] // for error recovery
172 #define tsize_t basic[Tsize_t] // matches size_t alias
173 #define tptrdiff_t basic[Tptrdiff_t] // matches ptrdiff_t alias
174 #define thash_t tsize_t // matches hash_t alias
176 static ClassDeclaration
*typeinfo
;
177 static ClassDeclaration
*typeinfoclass
;
178 static ClassDeclaration
*typeinfointerface
;
179 static ClassDeclaration
*typeinfostruct
;
180 static ClassDeclaration
*typeinfotypedef
;
181 static ClassDeclaration
*typeinfopointer
;
182 static ClassDeclaration
*typeinfomaybe
;
183 static ClassDeclaration
*typeinfoarray
;
184 static ClassDeclaration
*typeinfostaticarray
;
185 static ClassDeclaration
*typeinfoassociativearray
;
186 static ClassDeclaration
*typeinfoenum
;
187 static ClassDeclaration
*typeinfofunction
;
188 static ClassDeclaration
*typeinfodelegate
;
189 static ClassDeclaration
*typeinfotypelist
;
191 static Type
*basic
[TMAX
];
192 static unsigned char mangleChar
[TMAX
];
193 static StringTable stringtable
;
195 // These tables are for implicit conversion of binary ops;
196 // the indices are the type of operand one, followed by operand two.
197 static unsigned char impcnvResult
[TMAX
][TMAX
];
198 static unsigned char impcnvType1
[TMAX
][TMAX
];
199 static unsigned char impcnvType2
[TMAX
][TMAX
];
201 // If !=0, give warning on implicit conversion
202 static unsigned char impcnvWarn
[TMAX
][TMAX
];
204 Type(TY ty
, Type
*next
);
205 virtual Type
*syntaxCopy();
206 int equals(Object
*o
);
207 int dyncast() { return DYNCAST_TYPE
; } // kludge for template.isType()
208 int covariant(Type
*t
);
210 static char needThisPrefix();
213 virtual d_uns64
size(Loc loc
);
214 virtual unsigned alignsize();
215 virtual Type
*semantic(Loc loc
, Scope
*sc
);
216 virtual void toDecoBuffer(OutBuffer
*buf
);
218 virtual void toCBuffer(OutBuffer
*buf
, Identifier
*ident
, HdrGenState
*hgs
);
219 virtual void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
220 void toCBuffer3(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
222 virtual int isintegral();
223 virtual int isfloating(); // real, imaginary, or complex
224 virtual int isreal();
225 virtual int isimaginary();
226 virtual int iscomplex();
227 virtual int isscalar();
228 virtual int isunsigned();
229 virtual int isauto();
230 virtual int isString();
231 virtual int checkBoolean(); // if can be converted to boolean value
232 void checkDeprecated(Loc loc
, Scope
*sc
);
237 virtual Dsymbol
*toDsymbol(Scope
*sc
);
238 virtual Type
*toBasetype();
239 virtual int isBaseOf(Type
*t
, target_ptrdiff_t
*poffset
);
240 virtual MATCH
implicitConvTo(Type
*to
);
241 virtual ClassDeclaration
*isClassHandle();
242 virtual Expression
*getProperty(Loc loc
, Identifier
*ident
);
243 virtual Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
244 virtual unsigned memalign(unsigned salign
);
245 virtual Expression
*defaultInit(Loc loc
= 0);
246 virtual int isZeroInit(); // if initializer is 0
247 virtual dt_t
**toDt(dt_t
**pdt
);
248 Identifier
*getTypeInfoIdent(int internal
);
249 virtual MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
250 virtual void resolve(Loc loc
, Scope
*sc
, Expression
**pe
, Type
**pt
, Dsymbol
**ps
);
251 Expression
*getInternalTypeInfo(Scope
*sc
);
252 Expression
*getTypeInfo(Scope
*sc
);
253 virtual TypeInfoDeclaration
*getTypeInfoDeclaration();
254 virtual int builtinTypeInfo();
255 virtual Type
*reliesOnTident();
256 virtual Expression
*toExpression();
257 virtual int hasPointers();
258 Type
*nextOf() { return next
; }
260 static void error(Loc loc
, const char *format
, ...);
263 virtual unsigned totym();
264 virtual type
*toCtype();
265 virtual type
*toCParamtype();
266 virtual Symbol
*toSymbol();
268 // For eliminating dynamic_cast
269 virtual TypeBasic
*isTypeBasic();
272 struct TypeBasic
: Type
280 d_uns64
size(Loc loc
);
281 unsigned alignsize();
282 Expression
*getProperty(Loc loc
, Identifier
*ident
);
283 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
285 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
294 MATCH
implicitConvTo(Type
*to
);
295 Expression
*defaultInit(Loc loc
);
297 int builtinTypeInfo();
299 // For eliminating dynamic_cast
300 TypeBasic
*isTypeBasic();
303 struct TypeArray
: Type
305 TypeArray(TY ty
, Type
*next
);
306 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
309 // Static array, one with a fixed dimension
310 struct TypeSArray
: TypeArray
314 TypeSArray(Type
*t
, Expression
*dim
);
316 d_uns64
size(Loc loc
);
317 unsigned alignsize();
318 Type
*semantic(Loc loc
, Scope
*sc
);
319 void resolve(Loc loc
, Scope
*sc
, Expression
**pe
, Type
**pt
, Dsymbol
**ps
);
320 void toDecoBuffer(OutBuffer
*buf
);
321 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
322 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
325 unsigned memalign(unsigned salign
);
326 MATCH
implicitConvTo(Type
*to
);
327 Expression
*defaultInit(Loc loc
);
328 dt_t
**toDt(dt_t
**pdt
);
329 dt_t
**toDtElem(dt_t
**pdt
, Expression
*e
);
330 MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
331 TypeInfoDeclaration
*getTypeInfoDeclaration();
332 Expression
*toExpression();
336 type
*toCParamtype();
339 // Dynamic array, no dimension
340 struct TypeDArray
: TypeArray
344 d_uns64
size(Loc loc
);
345 unsigned alignsize();
346 Type
*semantic(Loc loc
, Scope
*sc
);
347 void toDecoBuffer(OutBuffer
*buf
);
348 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
349 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
353 MATCH
implicitConvTo(Type
*to
);
354 Expression
*defaultInit(Loc loc
);
355 int builtinTypeInfo();
356 TypeInfoDeclaration
*getTypeInfoDeclaration();
362 struct TypeAArray
: TypeArray
364 Type
*index
; // key type for type checking
365 Type
*key
; // actual key type
367 TypeAArray(Type
*t
, Type
*index
);
369 d_uns64
size(Loc loc
);
370 Type
*semantic(Loc loc
, Scope
*sc
);
371 void resolve(Loc loc
, Scope
*sc
, Expression
**pe
, Type
**pt
, Dsymbol
**ps
);
372 void toDecoBuffer(OutBuffer
*buf
);
373 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
374 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
375 Expression
*defaultInit(Loc loc
);
376 MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
378 TypeInfoDeclaration
*getTypeInfoDeclaration();
382 Symbol
*aaGetSymbol(char *func
, int flags
);
387 struct TypePointer
: Type
389 TypePointer(Type
*t
);
391 Type
*semantic(Loc loc
, Scope
*sc
);
392 d_uns64
size(Loc loc
);
393 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
394 MATCH
implicitConvTo(Type
*to
);
396 Expression
*defaultInit(Loc loc
);
398 TypeInfoDeclaration
*getTypeInfoDeclaration();
404 struct TypeMaybe
: Type
409 Type
*semantic(Loc loc
, Scope
*sc
);
410 d_uns64
size(Loc loc
);
411 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
412 MATCH
implicitConvTo(Type
*to
);
414 Expression
*defaultInit(Loc loc
);
416 TypeInfoDeclaration
*getTypeInfoDeclaration();
422 struct TypeReference
: Type
424 TypeReference(Type
*t
);
426 d_uns64
size(Loc loc
);
427 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
428 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
429 Expression
*defaultInit(Loc loc
);
435 RETregs
= 1, // returned in registers
436 RETstack
= 2, // returned on stack
439 struct TypeFunction
: Type
441 Arguments
*parameters
; // function parameters
442 int varargs
; // 1: T t, ...) style for variable number of arguments
443 // 2: T t ...) style for variable number of arguments
444 enum LINK linkage
; // calling convention
448 TypeFunction(Arguments
*parameters
, Type
*treturn
, int varargs
, enum LINK linkage
);
450 Type
*semantic(Loc loc
, Scope
*sc
);
451 void toDecoBuffer(OutBuffer
*buf
);
452 void toCBuffer(OutBuffer
*buf
, Identifier
*ident
, HdrGenState
*hgs
);
453 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
454 MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
455 TypeInfoDeclaration
*getTypeInfoDeclaration();
456 Type
*reliesOnTident();
458 int callMatch(Expressions
*toargs
);
465 struct TypeDelegate
: Type
467 TypeDelegate(Type
*t
);
469 Type
*semantic(Loc loc
, Scope
*sc
);
470 d_uns64
size(Loc loc
);
471 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
472 Expression
*defaultInit(Loc loc
);
475 TypeInfoDeclaration
*getTypeInfoDeclaration();
476 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
482 struct TypeQualified
: Type
485 Array idents
; // array of Identifier's representing ident.ident.ident etc.
487 TypeQualified(TY ty
, Loc loc
);
488 void syntaxCopyHelper(TypeQualified
*t
);
489 void addIdent(Identifier
*ident
);
490 void toCBuffer2Helper(OutBuffer
*buf
, HdrGenState
*hgs
);
491 d_uns64
size(Loc loc
);
492 void resolveHelper(Loc loc
, Scope
*sc
, Dsymbol
*s
, Dsymbol
*scopesym
,
493 Expression
**pe
, Type
**pt
, Dsymbol
**ps
);
496 struct TypeIdentifier
: TypeQualified
500 TypeIdentifier(Loc loc
, Identifier
*ident
);
503 void toDecoBuffer(OutBuffer
*buf
);
504 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
505 void resolve(Loc loc
, Scope
*sc
, Expression
**pe
, Type
**pt
, Dsymbol
**ps
);
506 Dsymbol
*toDsymbol(Scope
*sc
);
507 Type
*semantic(Loc loc
, Scope
*sc
);
508 MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
509 Type
*reliesOnTident();
510 Expression
*toExpression();
513 /* Similar to TypeIdentifier, but with a TemplateInstance as the root
515 struct TypeInstance
: TypeQualified
517 TemplateInstance
*tempinst
;
519 TypeInstance(Loc loc
, TemplateInstance
*tempinst
);
522 //void toDecoBuffer(OutBuffer *buf);
523 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
524 void resolve(Loc loc
, Scope
*sc
, Expression
**pe
, Type
**pt
, Dsymbol
**ps
);
525 Type
*semantic(Loc loc
, Scope
*sc
);
526 MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
529 struct TypeTypeof
: TypeQualified
533 TypeTypeof(Loc loc
, Expression
*exp
);
535 Dsymbol
*toDsymbol(Scope
*sc
);
536 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
537 Type
*semantic(Loc loc
, Scope
*sc
);
538 d_uns64
size(Loc loc
);
541 struct TypeStruct
: Type
543 StructDeclaration
*sym
;
545 TypeStruct(StructDeclaration
*sym
);
546 d_uns64
size(Loc loc
);
547 unsigned alignsize();
550 Type
*semantic(Loc loc
, Scope
*sc
);
551 Dsymbol
*toDsymbol(Scope
*sc
);
552 void toDecoBuffer(OutBuffer
*buf
);
553 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
554 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
555 unsigned memalign(unsigned salign
);
556 Expression
*defaultInit(Loc loc
);
559 dt_t
**toDt(dt_t
**pdt
);
560 MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
561 TypeInfoDeclaration
*getTypeInfoDeclaration();
567 struct TypeEnum
: Type
569 EnumDeclaration
*sym
;
571 TypeEnum(EnumDeclaration
*sym
);
572 d_uns64
size(Loc loc
);
573 unsigned alignsize();
575 Type
*semantic(Loc loc
, Scope
*sc
);
576 Dsymbol
*toDsymbol(Scope
*sc
);
577 void toDecoBuffer(OutBuffer
*buf
);
578 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
579 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
580 Expression
*getProperty(Loc loc
, Identifier
*ident
);
585 MATCH
implicitConvTo(Type
*to
);
587 Expression
*defaultInit(Loc loc
);
589 MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
590 TypeInfoDeclaration
*getTypeInfoDeclaration();
596 struct TypeTypedef
: Type
598 TypedefDeclaration
*sym
;
600 TypeTypedef(TypedefDeclaration
*sym
);
602 d_uns64
size(Loc loc
);
603 unsigned alignsize();
605 Type
*semantic(Loc loc
, Scope
*sc
);
606 Dsymbol
*toDsymbol(Scope
*sc
);
607 void toDecoBuffer(OutBuffer
*buf
);
608 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
609 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
610 Expression
*getProperty(Loc loc
, Identifier
*ident
);
621 MATCH
implicitConvTo(Type
*to
);
622 Expression
*defaultInit(Loc loc
);
624 dt_t
**toDt(dt_t
**pdt
);
625 MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
626 TypeInfoDeclaration
*getTypeInfoDeclaration();
630 type
*toCParamtype();
633 struct TypeClass
: Type
635 ClassDeclaration
*sym
;
637 TypeClass(ClassDeclaration
*sym
);
638 d_uns64
size(Loc loc
);
641 Type
*semantic(Loc loc
, Scope
*sc
);
642 Dsymbol
*toDsymbol(Scope
*sc
);
643 void toDecoBuffer(OutBuffer
*buf
);
644 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
645 Expression
*dotExp(Scope
*sc
, Expression
*e
, Identifier
*ident
);
646 ClassDeclaration
*isClassHandle();
647 int isBaseOf(Type
*t
, target_ptrdiff_t
*poffset
);
648 MATCH
implicitConvTo(Type
*to
);
649 Expression
*defaultInit(Loc loc
);
651 MATCH
deduceType(Scope
*sc
, Type
*tparam
, TemplateParameters
*parameters
, Objects
*dedtypes
);
654 TypeInfoDeclaration
*getTypeInfoDeclaration();
662 struct TypeTuple
: Type
664 Arguments
*arguments
; // types making up the tuple
666 TypeTuple(Arguments
*arguments
);
667 TypeTuple(Expressions
*exps
);
669 Type
*semantic(Loc loc
, Scope
*sc
);
670 int equals(Object
*o
);
671 Type
*reliesOnTident();
672 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
673 void toDecoBuffer(OutBuffer
*buf
);
674 Expression
*getProperty(Loc loc
, Identifier
*ident
);
675 TypeInfoDeclaration
*getTypeInfoDeclaration();
678 struct TypeSlice
: Type
683 TypeSlice(Type
*next
, Expression
*lwr
, Expression
*upr
);
685 Type
*semantic(Loc loc
, Scope
*sc
);
686 void resolve(Loc loc
, Scope
*sc
, Expression
**pe
, Type
**pt
, Dsymbol
**ps
);
687 void toCBuffer2(OutBuffer
*buf
, HdrGenState
*hgs
, int mod
);
690 /**************************************************************/
692 //enum InOut { None, In, Out, InOut, Lazy };
694 struct Argument
: Object
697 unsigned storageClass
;
700 Expression
*defaultArg
;
702 Argument(unsigned storageClass
, Type
*type
, Identifier
*ident
, Expression
*defaultArg
);
703 Argument
*syntaxCopy();
705 void toDecoBuffer(OutBuffer
*buf
);
706 static Arguments
*arraySyntaxCopy(Arguments
*args
);
707 static char *argsTypesToChars(Arguments
*args
, int varargs
);
708 static void argsToCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
, Arguments
*arguments
, int varargs
);
709 static void argsToDecoBuffer(OutBuffer
*buf
, Arguments
*arguments
);
710 static size_t dim(Arguments
*arguments
);
711 static Argument
*getNth(Arguments
*arguments
, size_t nth
, size_t *pn
= NULL
);
718 extern int Tptrdiff_t
;
722 #endif /* DMD_MTYPE_H */