compiler/libec: Fixed missing warnings within ( ) and conditional expressions
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50       bool backOutputLineNumbers = outputLineNumbers;
51       outputLineNumbers = false;
52
53       if(exp)
54          OutputExpression(exp, f);
55       f.Seek(0, start);
56       count = strlen(string);
57       count += f.Read(string + count, 1, 1023);
58       string[count] = '\0';
59       delete f;
60
61       outputLineNumbers = backOutputLineNumbers;
62    }
63 }
64
65 Type ProcessTemplateParameterType(TemplateParameter param)
66 {
67    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
68    {
69       // TOFIX: Will need to free this Type
70       if(!param.baseType)
71       {
72          if(param.dataTypeString)
73             param.baseType = ProcessTypeString(param.dataTypeString, false);
74          else
75             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
76       }
77       return param.baseType;
78    }
79    return null;
80 }
81
82 bool NeedCast(Type type1, Type type2)
83 {
84    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
85
86    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
87    {
88       return false;
89    }
90
91    if(type1.kind == type2.kind)
92    {
93       switch(type1.kind)
94       {
95          case _BoolType:
96          case charType:
97          case shortType:
98          case intType:
99          case int64Type:
100          case intPtrType:
101          case intSizeType:
102             if(type1.passAsTemplate && !type2.passAsTemplate)
103                return true;
104             return type1.isSigned != type2.isSigned;
105          case classType:
106             return type1._class != type2._class;
107          case pointerType:
108             return (type1.type && type2.type && type1.type.constant != type2.type.constant) || NeedCast(type1.type, type2.type);
109          default:
110             return true; //false; ????
111       }
112    }
113    return true;
114 }
115
116 static void ReplaceClassMembers(Expression exp, Class _class)
117 {
118    if(exp.type == identifierExp && exp.identifier)
119    {
120       Identifier id = exp.identifier;
121       Context ctx;
122       Symbol symbol = null;
123       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
124       {
125          // First, check if the identifier is declared inside the function
126          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
127          {
128             symbol = (Symbol)ctx.symbols.FindString(id.string);
129             if(symbol) break;
130          }
131       }
132
133       // If it is not, check if it is a member of the _class
134       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
135       {
136          Property prop = eClass_FindProperty(_class, id.string, privateModule);
137          Method method = null;
138          DataMember member = null;
139          ClassProperty classProp = null;
140          if(!prop)
141          {
142             method = eClass_FindMethod(_class, id.string, privateModule);
143          }
144          if(!prop && !method)
145             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
146          if(!prop && !method && !member)
147          {
148             classProp = eClass_FindClassProperty(_class, id.string);
149          }
150          if(prop || method || member || classProp)
151          {
152             // Replace by this.[member]
153             exp.type = memberExp;
154             exp.member.member = id;
155             exp.member.memberType = unresolvedMember;
156             exp.member.exp = QMkExpId("this");
157             //exp.member.exp.loc = exp.loc;
158             exp.addedThis = true;
159          }
160          else if(_class && _class.templateParams.first)
161          {
162             Class sClass;
163             for(sClass = _class; sClass; sClass = sClass.base)
164             {
165                if(sClass.templateParams.first)
166                {
167                   ClassTemplateParameter param;
168                   for(param = sClass.templateParams.first; param; param = param.next)
169                   {
170                      if(param.type == expression && !strcmp(param.name, id.string))
171                      {
172                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
173
174                         if(argExp)
175                         {
176                            Declarator decl;
177                            OldList * specs = MkList();
178
179                            FreeIdentifier(exp.member.member);
180
181                            ProcessExpressionType(argExp);
182
183                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
184
185                            exp.expType = ProcessType(specs, decl);
186
187                            // *[expType] *[argExp]
188                            exp.type = bracketsExp;
189                            exp.list = MkListOne(MkExpOp(null, '*',
190                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
191                         }
192                      }
193                   }
194                }
195             }
196          }
197       }
198    }
199 }
200
201 ////////////////////////////////////////////////////////////////////////
202 // PRINTING ////////////////////////////////////////////////////////////
203 ////////////////////////////////////////////////////////////////////////
204
205 public char * PrintInt(int64 result)
206 {
207    char temp[100];
208    if(result > MAXINT)
209       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
210    else
211       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
212    if(result > MAXINT || result < MININT)
213       strcat(temp, "LL");
214    return CopyString(temp);
215 }
216
217 public char * PrintUInt(uint64 result)
218 {
219    char temp[100];
220    if(result > MAXDWORD)
221       sprintf(temp, FORMAT64HEXLL /*"0x%I64X"*/, result);
222    else if(result > MAXINT)
223       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
224    else
225       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char *  PrintInt64(int64 result)
230 {
231    char temp[100];
232    if(result > MAXINT || result < MININT)
233       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
234    else
235       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
236    return CopyString(temp);
237 }
238
239 public char * PrintUInt64(uint64 result)
240 {
241    char temp[100];
242    if(result > MAXDWORD)
243       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
244    else if(result > MAXINT)
245       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
246    else
247       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
248    return CopyString(temp);
249 }
250
251 public char * PrintHexUInt(uint64 result)
252 {
253    char temp[100];
254    if(result > MAXDWORD)
255       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
256    else
257       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
258    if(result > MAXDWORD)
259       strcat(temp, "LL");
260    return CopyString(temp);
261 }
262
263 public char * PrintHexUInt64(uint64 result)
264 {
265    char temp[100];
266    if(result > MAXDWORD)
267       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
268    else
269       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
270    return CopyString(temp);
271 }
272
273 public char * PrintShort(short result)
274 {
275    char temp[100];
276    sprintf(temp, "%d", (unsigned short)result);
277    return CopyString(temp);
278 }
279
280 public char * PrintUShort(unsigned short result)
281 {
282    char temp[100];
283    if(result > 32767)
284       sprintf(temp, "0x%X", (int)result);
285    else
286       sprintf(temp, "%d", (int)result);
287    return CopyString(temp);
288 }
289
290 public char * PrintChar(char result)
291 {
292    char temp[100];
293    if(result > 0 && isprint(result))
294       sprintf(temp, "'%c'", result);
295    else if(result < 0)
296       sprintf(temp, "%d", (int)result);
297    else
298       //sprintf(temp, "%#X", result);
299       sprintf(temp, "0x%X", (unsigned char)result);
300    return CopyString(temp);
301 }
302
303 public char * PrintUChar(unsigned char result)
304 {
305    char temp[100];
306    sprintf(temp, "0x%X", result);
307    return CopyString(temp);
308 }
309
310 public char * PrintFloat(float result)
311 {
312    char temp[350];
313    if(result.isInf)
314    {
315       if(result.signBit)
316          strcpy(temp, "-inf");
317       else
318          strcpy(temp, "inf");
319    }
320    else if(result.isNan)
321    {
322       if(result.signBit)
323          strcpy(temp, "-nan");
324       else
325          strcpy(temp, "nan");
326    }
327    else
328       sprintf(temp, "%.16ff", result);
329    return CopyString(temp);
330 }
331
332 public char * PrintDouble(double result)
333 {
334    char temp[350];
335    if(result.isInf)
336    {
337       if(result.signBit)
338          strcpy(temp, "-inf");
339       else
340          strcpy(temp, "inf");
341    }
342    else if(result.isNan)
343    {
344       if(result.signBit)
345          strcpy(temp, "-nan");
346       else
347          strcpy(temp, "nan");
348    }
349    else
350       sprintf(temp, "%.16f", result);
351    return CopyString(temp);
352 }
353
354 ////////////////////////////////////////////////////////////////////////
355 ////////////////////////////////////////////////////////////////////////
356
357 //public Operand GetOperand(Expression exp);
358
359 #define GETVALUE(name, t) \
360    public bool GetOp##name(Operand op2, t * value2) \
361    {                                                        \
362       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
363       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
364       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
365       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
366       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
367       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
368       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
369       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
370       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
371       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
372       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
373       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
374       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
375       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
376       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
377       else                                                                          \
378          return false;                                                              \
379       return true;                                                                  \
380    } \
381    public bool Get##name(Expression exp, t * value2) \
382    {                                                        \
383       Operand op2 = GetOperand(exp);                        \
384       return GetOp##name(op2, value2); \
385    }
386
387 // To help the debugger currently not preprocessing...
388 #define HELP(x) x
389
390 GETVALUE(Int, HELP(int));
391 GETVALUE(UInt, HELP(unsigned int));
392 GETVALUE(Int64, HELP(int64));
393 GETVALUE(UInt64, HELP(uint64));
394 GETVALUE(IntPtr, HELP(intptr));
395 GETVALUE(UIntPtr, HELP(uintptr));
396 GETVALUE(IntSize, HELP(intsize));
397 GETVALUE(UIntSize, HELP(uintsize));
398 GETVALUE(Short, HELP(short));
399 GETVALUE(UShort, HELP(unsigned short));
400 GETVALUE(Char, HELP(char));
401 GETVALUE(UChar, HELP(unsigned char));
402 GETVALUE(Float, HELP(float));
403 GETVALUE(Double, HELP(double));
404
405 void ComputeExpression(Expression exp);
406
407 void ComputeClassMembers(Class _class, bool isMember)
408 {
409    DataMember member = isMember ? (DataMember) _class : null;
410    Context context = isMember ? null : SetupTemplatesContext(_class);
411    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
412                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
413    {
414       int unionMemberOffset = 0;
415       int bitFields = 0;
416
417       /*
418       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
419          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
420       */
421
422       if(member)
423       {
424          member.memberOffset = 0;
425          if(targetBits < sizeof(void *) * 8)
426             member.structAlignment = 0;
427       }
428       else if(targetBits < sizeof(void *) * 8)
429          _class.structAlignment = 0;
430
431       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
432       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
433          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
434
435       if(!member && _class.destructionWatchOffset)
436          _class.memberOffset += sizeof(OldList);
437
438       // To avoid reentrancy...
439       //_class.structSize = -1;
440
441       {
442          DataMember dataMember;
443          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
444          {
445             if(!dataMember.isProperty)
446             {
447                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
448                {
449                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
450                   /*if(!dataMember.dataType)
451                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
452                      */
453                }
454             }
455          }
456       }
457
458       {
459          DataMember dataMember;
460          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
461          {
462             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
463             {
464                if(!isMember && _class.type == bitClass && dataMember.dataType)
465                {
466                   BitMember bitMember = (BitMember) dataMember;
467                   uint64 mask = 0;
468                   int d;
469
470                   ComputeTypeSize(dataMember.dataType);
471
472                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
473                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
474
475                   _class.memberOffset = bitMember.pos + bitMember.size;
476                   for(d = 0; d<bitMember.size; d++)
477                   {
478                      if(d)
479                         mask <<= 1;
480                      mask |= 1;
481                   }
482                   bitMember.mask = mask << bitMember.pos;
483                }
484                else if(dataMember.type == normalMember && dataMember.dataType)
485                {
486                   int size;
487                   int alignment = 0;
488
489                   // Prevent infinite recursion
490                   if(dataMember.dataType.kind != classType ||
491                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
492                      _class.type != structClass)))
493                      ComputeTypeSize(dataMember.dataType);
494
495                   if(dataMember.dataType.bitFieldCount)
496                   {
497                      bitFields += dataMember.dataType.bitFieldCount;
498                      size = 0;
499                   }
500                   else
501                   {
502                      if(bitFields)
503                      {
504                         int size = (bitFields + 7) / 8;
505
506                         if(isMember)
507                         {
508                            // TESTING THIS PADDING CODE
509                            if(alignment)
510                            {
511                               member.structAlignment = Max(member.structAlignment, alignment);
512
513                               if(member.memberOffset % alignment)
514                                  member.memberOffset += alignment - (member.memberOffset % alignment);
515                            }
516
517                            dataMember.offset = member.memberOffset;
518                            if(member.type == unionMember)
519                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
520                            else
521                            {
522                               member.memberOffset += size;
523                            }
524                         }
525                         else
526                         {
527                            // TESTING THIS PADDING CODE
528                            if(alignment)
529                            {
530                               _class.structAlignment = Max(_class.structAlignment, alignment);
531
532                               if(_class.memberOffset % alignment)
533                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
534                            }
535
536                            dataMember.offset = _class.memberOffset;
537                            _class.memberOffset += size;
538                         }
539                         bitFields = 0;
540                      }
541                      size = dataMember.dataType.size;
542                      alignment = dataMember.dataType.alignment;
543                   }
544
545                   if(isMember)
546                   {
547                      // TESTING THIS PADDING CODE
548                      if(alignment)
549                      {
550                         member.structAlignment = Max(member.structAlignment, alignment);
551
552                         if(member.memberOffset % alignment)
553                            member.memberOffset += alignment - (member.memberOffset % alignment);
554                      }
555
556                      dataMember.offset = member.memberOffset;
557                      if(member.type == unionMember)
558                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
559                      else
560                      {
561                         member.memberOffset += size;
562                      }
563                   }
564                   else
565                   {
566                      // TESTING THIS PADDING CODE
567                      if(alignment)
568                      {
569                         _class.structAlignment = Max(_class.structAlignment, alignment);
570
571                         if(_class.memberOffset % alignment)
572                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
573                      }
574
575                      dataMember.offset = _class.memberOffset;
576                      _class.memberOffset += size;
577                   }
578                }
579                else
580                {
581                   int alignment;
582
583                   ComputeClassMembers((Class)dataMember, true);
584                   alignment = dataMember.structAlignment;
585
586                   if(isMember)
587                   {
588                      if(alignment)
589                      {
590                         if(member.memberOffset % alignment)
591                            member.memberOffset += alignment - (member.memberOffset % alignment);
592
593                         member.structAlignment = Max(member.structAlignment, alignment);
594                      }
595                      dataMember.offset = member.memberOffset;
596                      if(member.type == unionMember)
597                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
598                      else
599                         member.memberOffset += dataMember.memberOffset;
600                   }
601                   else
602                   {
603                      if(alignment)
604                      {
605                         if(_class.memberOffset % alignment)
606                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
607                         _class.structAlignment = Max(_class.structAlignment, alignment);
608                      }
609                      dataMember.offset = _class.memberOffset;
610                      _class.memberOffset += dataMember.memberOffset;
611                   }
612                }
613             }
614          }
615          if(bitFields)
616          {
617             int alignment = 0;
618             int size = (bitFields + 7) / 8;
619
620             if(isMember)
621             {
622                // TESTING THIS PADDING CODE
623                if(alignment)
624                {
625                   member.structAlignment = Max(member.structAlignment, alignment);
626
627                   if(member.memberOffset % alignment)
628                      member.memberOffset += alignment - (member.memberOffset % alignment);
629                }
630
631                if(member.type == unionMember)
632                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
633                else
634                {
635                   member.memberOffset += size;
636                }
637             }
638             else
639             {
640                // TESTING THIS PADDING CODE
641                if(alignment)
642                {
643                   _class.structAlignment = Max(_class.structAlignment, alignment);
644
645                   if(_class.memberOffset % alignment)
646                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
647                }
648                _class.memberOffset += size;
649             }
650             bitFields = 0;
651          }
652       }
653       if(member && member.type == unionMember)
654       {
655          member.memberOffset = unionMemberOffset;
656       }
657
658       if(!isMember)
659       {
660          /*if(_class.type == structClass)
661             _class.size = _class.memberOffset;
662          else
663          */
664
665          if(_class.type != bitClass)
666          {
667             int extra = 0;
668             if(_class.structAlignment)
669             {
670                if(_class.memberOffset % _class.structAlignment)
671                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
672             }
673             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
674             if(!member)
675             {
676                Property prop;
677                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
678                {
679                   if(prop.isProperty && prop.isWatchable)
680                   {
681                      prop.watcherOffset = _class.structSize;
682                      _class.structSize += sizeof(OldList);
683                   }
684                }
685             }
686
687             // Fix Derivatives
688             {
689                OldLink derivative;
690                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
691                {
692                   Class deriv = derivative.data;
693
694                   if(deriv.computeSize)
695                   {
696                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
697                      deriv.offset = /*_class.offset + */_class.structSize;
698                      deriv.memberOffset = 0;
699                      // ----------------------
700
701                      deriv.structSize = deriv.offset;
702
703                      ComputeClassMembers(deriv, false);
704                   }
705                }
706             }
707          }
708       }
709    }
710    if(context)
711       FinishTemplatesContext(context);
712 }
713
714 public void ComputeModuleClasses(Module module)
715 {
716    Class _class;
717    OldLink subModule;
718
719    for(subModule = module.modules.first; subModule; subModule = subModule.next)
720       ComputeModuleClasses(subModule.data);
721    for(_class = module.classes.first; _class; _class = _class.next)
722       ComputeClassMembers(_class, false);
723 }
724
725
726 public int ComputeTypeSize(Type type)
727 {
728    uint size = type ? type.size : 0;
729    if(!size && type && !type.computing)
730    {
731       type.computing = true;
732       switch(type.kind)
733       {
734          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
735          case charType: type.alignment = size = sizeof(char); break;
736          case intType: type.alignment = size = sizeof(int); break;
737          case int64Type: type.alignment = size = sizeof(int64); break;
738          case intPtrType: type.alignment = size = targetBits / 8; break;
739          case intSizeType: type.alignment = size = targetBits / 8; break;
740          case longType: type.alignment = size = sizeof(long); break;
741          case shortType: type.alignment = size = sizeof(short); break;
742          case floatType: type.alignment = size = sizeof(float); break;
743          case doubleType: type.alignment = size = sizeof(double); break;
744          case classType:
745          {
746             Class _class = type._class ? type._class.registered : null;
747
748             if(_class && _class.type == structClass)
749             {
750                // Ensure all members are properly registered
751                ComputeClassMembers(_class, false);
752                type.alignment = _class.structAlignment;
753                size = _class.structSize;
754                if(type.alignment && size % type.alignment)
755                   size += type.alignment - (size % type.alignment);
756
757             }
758             else if(_class && (_class.type == unitClass ||
759                    _class.type == enumClass ||
760                    _class.type == bitClass))
761             {
762                if(!_class.dataType)
763                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
764                size = type.alignment = ComputeTypeSize(_class.dataType);
765             }
766             else
767                size = type.alignment = targetBits / 8; // sizeof(Instance *);
768             break;
769          }
770          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
771          case arrayType:
772             if(type.arraySizeExp)
773             {
774                ProcessExpressionType(type.arraySizeExp);
775                ComputeExpression(type.arraySizeExp);
776                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
777                   type.arraySizeExp.expType.kind != shortType &&
778                   type.arraySizeExp.expType.kind != charType &&
779                   type.arraySizeExp.expType.kind != longType &&
780                   type.arraySizeExp.expType.kind != int64Type &&
781                   type.arraySizeExp.expType.kind != intSizeType &&
782                   type.arraySizeExp.expType.kind != intPtrType &&
783                   type.arraySizeExp.expType.kind != enumType &&
784                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
785                {
786                   Location oldLoc = yylloc;
787                   // bool isConstant = type.arraySizeExp.isConstant;
788                   char expression[10240];
789                   expression[0] = '\0';
790                   type.arraySizeExp.expType = null;
791                   yylloc = type.arraySizeExp.loc;
792                   if(inCompiler)
793                      PrintExpression(type.arraySizeExp, expression);
794                   Compiler_Error($"Array size not constant int (%s)\n", expression);
795                   yylloc = oldLoc;
796                }
797                GetInt(type.arraySizeExp, &type.arraySize);
798             }
799             else if(type.enumClass)
800             {
801                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
802                {
803                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
804                }
805                else
806                   type.arraySize = 0;
807             }
808             else
809             {
810                // Unimplemented auto size
811                type.arraySize = 0;
812             }
813
814             size = ComputeTypeSize(type.type) * type.arraySize;
815             if(type.type)
816                type.alignment = type.type.alignment;
817
818             break;
819          case structType:
820          {
821             if(!type.members.first && type.enumName)
822             {
823                Symbol symbol = FindStruct(curContext, type.enumName);
824                if(symbol && symbol.type)
825                {
826                   ComputeTypeSize(symbol.type);
827                   size = symbol.type.size;
828                }
829             }
830             else
831             {
832                Type member;
833                for(member = type.members.first; member; member = member.next)
834                {
835                   uint addSize = ComputeTypeSize(member);
836
837                   member.offset = size;
838                   if(member.alignment && size % member.alignment)
839                      member.offset += member.alignment - (size % member.alignment);
840                   size = member.offset;
841
842                   type.alignment = Max(type.alignment, member.alignment);
843                   size += addSize;
844                }
845                if(type.alignment && size % type.alignment)
846                   size += type.alignment - (size % type.alignment);
847             }
848             break;
849          }
850          case unionType:
851          {
852             if(!type.members.first && type.enumName)
853             {
854                Symbol symbol = FindStruct(curContext, type.enumName);
855                if(symbol && symbol.type)
856                {
857                   ComputeTypeSize(symbol.type);
858                   size = symbol.type.size;
859                }
860             }
861             else
862             {
863                Type member;
864                for(member = type.members.first; member; member = member.next)
865                {
866                   uint addSize = ComputeTypeSize(member);
867
868                   member.offset = size;
869                   if(member.alignment && size % member.alignment)
870                      member.offset += member.alignment - (size % member.alignment);
871                   size = member.offset;
872
873                   type.alignment = Max(type.alignment, member.alignment);
874                   size = Max(size, addSize);
875                }
876                if(type.alignment && size % type.alignment)
877                   size += type.alignment - (size % type.alignment);
878             }
879             break;
880          }
881          case templateType:
882          {
883             TemplateParameter param = type.templateParameter;
884             Type baseType = ProcessTemplateParameterType(param);
885             if(baseType)
886             {
887                size = ComputeTypeSize(baseType);
888                type.alignment = baseType.alignment;
889             }
890             else
891                type.alignment = size = sizeof(uint64);
892             break;
893          }
894          case enumType:
895          {
896             type.alignment = size = sizeof(enum { test });
897             break;
898          }
899          case thisClassType:
900          {
901             type.alignment = size = targetBits / 8; //sizeof(void *);
902             break;
903          }
904       }
905       type.size = size;
906       type.computing = false;
907    }
908    return size;
909 }
910
911
912 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
913 {
914    // This function is in need of a major review when implementing private members etc.
915    DataMember topMember = isMember ? (DataMember) _class : null;
916    uint totalSize = 0;
917    uint maxSize = 0;
918    int alignment;
919    uint size;
920    DataMember member;
921    int anonID = 1;
922    Context context = isMember ? null : SetupTemplatesContext(_class);
923    if(addedPadding)
924       *addedPadding = false;
925
926    if(!isMember && _class.base)
927    {
928       maxSize = _class.structSize;
929       //if(_class.base.type != systemClass) // Commented out with new Instance _class
930       {
931          // DANGER: Testing this noHeadClass here...
932          if(_class.type == structClass || _class.type == noHeadClass)
933             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
934          else
935          {
936             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
937             if(maxSize > baseSize)
938                maxSize -= baseSize;
939             else
940                maxSize = 0;
941          }
942       }
943    }
944
945    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
946    {
947       if(!member.isProperty)
948       {
949          switch(member.type)
950          {
951             case normalMember:
952             {
953                if(member.dataTypeString)
954                {
955                   OldList * specs = MkList(), * decls = MkList();
956                   Declarator decl;
957
958                   decl = SpecDeclFromString(member.dataTypeString, specs,
959                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
960                   ListAdd(decls, MkStructDeclarator(decl, null));
961                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
962
963                   if(!member.dataType)
964                      member.dataType = ProcessType(specs, decl);
965
966                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
967
968                   {
969                      Type type = ProcessType(specs, decl);
970                      DeclareType(member.dataType, false, false);
971                      FreeType(type);
972                   }
973                   /*
974                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
975                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
976                      DeclareStruct(member.dataType._class.string, false);
977                   */
978
979                   ComputeTypeSize(member.dataType);
980                   size = member.dataType.size;
981                   alignment = member.dataType.alignment;
982
983                   if(alignment)
984                   {
985                      if(totalSize % alignment)
986                         totalSize += alignment - (totalSize % alignment);
987                   }
988                   totalSize += size;
989                }
990                break;
991             }
992             case unionMember:
993             case structMember:
994             {
995                OldList * specs = MkList(), * list = MkList();
996                char id[100];
997                sprintf(id, "__anon%d", anonID++);
998
999                size = 0;
1000                AddMembers(list, (Class)member, true, &size, topClass, null);
1001                ListAdd(specs,
1002                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
1003                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, MkListOne(MkDeclaratorIdentifier(MkIdentifier(id))),null)));
1004                alignment = member.structAlignment;
1005
1006                if(alignment)
1007                {
1008                   if(totalSize % alignment)
1009                      totalSize += alignment - (totalSize % alignment);
1010                }
1011                totalSize += size;
1012                break;
1013             }
1014          }
1015       }
1016    }
1017    if(retSize)
1018    {
1019       if(topMember && topMember.type == unionMember)
1020          *retSize = Max(*retSize, totalSize);
1021       else
1022          *retSize += totalSize;
1023    }
1024    else if(totalSize < maxSize && _class.type != systemClass)
1025    {
1026       int autoPadding = 0;
1027       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
1028          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
1029       if(totalSize + autoPadding < maxSize)
1030       {
1031          char sizeString[50];
1032          sprintf(sizeString, "%d", maxSize - totalSize);
1033          ListAdd(declarations,
1034             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
1035             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1036          if(addedPadding)
1037             *addedPadding = true;
1038       }
1039    }
1040    if(context)
1041       FinishTemplatesContext(context);
1042    return topMember ? topMember.memberID : _class.memberID;
1043 }
1044
1045 static int DeclareMembers(Class _class, bool isMember)
1046 {
1047    DataMember topMember = isMember ? (DataMember) _class : null;
1048    DataMember member;
1049    Context context = isMember ? null : SetupTemplatesContext(_class);
1050
1051    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1052       DeclareMembers(_class.base, false);
1053
1054    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1055    {
1056       if(!member.isProperty)
1057       {
1058          switch(member.type)
1059          {
1060             case normalMember:
1061             {
1062                /*
1063                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1064                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1065                   DeclareStruct(member.dataType._class.string, false);
1066                   */
1067                if(!member.dataType && member.dataTypeString)
1068                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1069                if(member.dataType)
1070                   DeclareType(member.dataType, false, false);
1071                break;
1072             }
1073             case unionMember:
1074             case structMember:
1075             {
1076                DeclareMembers((Class)member, true);
1077                break;
1078             }
1079          }
1080       }
1081    }
1082    if(context)
1083       FinishTemplatesContext(context);
1084
1085    return topMember ? topMember.memberID : _class.memberID;
1086 }
1087
1088 static void IdentifyAnonStructs(OldList/*<ClassDef>*/ *  definitions)
1089 {
1090    ClassDef def;
1091    int anonID = 1;
1092    for(def = definitions->first; def; def = def.next)
1093    {
1094       if(def.type == declarationClassDef)
1095       {
1096          Declaration decl = def.decl;
1097          if(decl && decl.specifiers)
1098          {
1099             Specifier spec;
1100             bool isStruct = false;
1101             for(spec = decl.specifiers->first; spec; spec = spec.next)
1102             {
1103                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1104                {
1105                   if(spec.definitions)
1106                      IdentifyAnonStructs(spec.definitions);
1107                   isStruct = true;
1108                }
1109             }
1110             if(isStruct)
1111             {
1112                Declarator d = null;
1113                if(decl.declarators)
1114                {
1115                   for(d = decl.declarators->first; d; d = d.next)
1116                   {
1117                      Identifier idDecl = GetDeclId(d);
1118                      if(idDecl)
1119                         break;
1120                   }
1121                }
1122                if(!d)
1123                {
1124                   char id[100];
1125                   sprintf(id, "__anon%d", anonID++);
1126                   if(!decl.declarators)
1127                      decl.declarators = MkList();
1128                   ListAdd(decl.declarators, MkDeclaratorIdentifier(MkIdentifier(id)));
1129                }
1130             }
1131          }
1132       }
1133    }
1134 }
1135
1136 void DeclareStruct(const char * name, bool skipNoHead)
1137 {
1138    External external = null;
1139    Symbol classSym = FindClass(name);
1140
1141    if(!inCompiler || !classSym) return;
1142
1143    // We don't need any declaration for bit classes...
1144    if(classSym.registered &&
1145       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1146       return;
1147
1148    /*if(classSym.registered.templateClass)
1149       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1150    */
1151
1152    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1153    {
1154       // Add typedef struct
1155       Declaration decl;
1156       OldList * specifiers, * declarators;
1157       OldList * declarations = null;
1158       char structName[1024];
1159       Specifier spec = null;
1160       external = (classSym.registered && classSym.registered.type == structClass) ?
1161          classSym.pointerExternal : classSym.structExternal;
1162
1163       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1164       // Moved this one up because DeclareClass done later will need it
1165
1166       classSym.declaring++;
1167
1168       if(strchr(classSym.string, '<'))
1169       {
1170          if(classSym.registered.templateClass)
1171          {
1172             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1173             classSym.declaring--;
1174          }
1175          return;
1176       }
1177
1178       //if(!skipNoHead)
1179          DeclareMembers(classSym.registered, false);
1180
1181       structName[0] = 0;
1182       FullClassNameCat(structName, name, false);
1183
1184       if(external && external.declaration && external.declaration.specifiers)
1185       {
1186          for(spec = external.declaration.specifiers->first; spec; spec = spec.next)
1187          {
1188             if(spec.type == structSpecifier || spec.type == unionSpecifier)
1189                break;
1190          }
1191       }
1192
1193       /*if(!external)
1194          external = MkExternalDeclaration(null);*/
1195
1196       if(!skipNoHead && (!spec || !spec.definitions))
1197       {
1198          bool addedPadding = false;
1199          classSym.declaredStructSym = true;
1200
1201          declarations = MkList();
1202
1203          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1204
1205          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1206          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1207
1208          if(!declarations->count || (declarations->count == 1 && addedPadding))
1209          {
1210             FreeList(declarations, FreeClassDef);
1211             declarations = null;
1212          }
1213       }
1214       if(skipNoHead || declarations)
1215       {
1216          if(spec)
1217          {
1218             if(declarations)
1219                spec.definitions = declarations;
1220
1221             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1222             {
1223                // TODO: Fix this
1224                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1225
1226                // DANGER
1227                if(classSym.structExternal)
1228                   ast->Move(classSym.structExternal, curExternal.prev);
1229                ast->Move(classSym.pointerExternal, curExternal.prev);
1230
1231                classSym.id = curExternal.symbol.idCode;
1232                classSym.idCode = curExternal.symbol.idCode;
1233                // external = classSym.pointerExternal;
1234                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1235             }
1236          }
1237          else
1238          {
1239             if(!external)
1240                external = MkExternalDeclaration(null);
1241
1242             specifiers = MkList();
1243             declarators = MkList();
1244             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1245
1246             /*
1247             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1248             ListAdd(declarators, MkInitDeclarator(d, null));
1249             */
1250             external.declaration = decl = MkDeclaration(specifiers, declarators);
1251             if(decl.symbol && !decl.symbol.pointerExternal)
1252                decl.symbol.pointerExternal = external;
1253
1254             // For simple classes, keep the declaration as the external to move around
1255             if(classSym.registered && classSym.registered.type == structClass)
1256             {
1257                char className[1024];
1258                strcpy(className, "__ecereClass_");
1259                FullClassNameCat(className, classSym.string, true);
1260                //MangleClassName(className);
1261
1262                // Testing This
1263                DeclareClass(classSym, className);
1264
1265                external.symbol = classSym;
1266                classSym.pointerExternal = external;
1267                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1268                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1269             }
1270             else
1271             {
1272                char className[1024];
1273                strcpy(className, "__ecereClass_");
1274                FullClassNameCat(className, classSym.string, true);
1275                //MangleClassName(className);
1276
1277                // TOFIX: TESTING THIS...
1278                classSym.structExternal = external;
1279                DeclareClass(classSym, className);
1280                external.symbol = classSym;
1281             }
1282
1283             //if(curExternal)
1284                ast->Insert(curExternal ? curExternal.prev : null, external);
1285          }
1286       }
1287
1288       classSym.declaring--;
1289    }
1290    else
1291    {
1292       if(classSym.structExternal && classSym.structExternal.declaration && classSym.structExternal.declaration.specifiers)
1293       {
1294          Specifier spec;
1295          for(spec = classSym.structExternal.declaration.specifiers->first; spec; spec = spec.next)
1296          {
1297             IdentifyAnonStructs(spec.definitions);
1298          }
1299       }
1300
1301       if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1302       {
1303          // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1304          // Moved this one up because DeclareClass done later will need it
1305
1306          // TESTING THIS:
1307          classSym.declaring++;
1308
1309          //if(!skipNoHead)
1310          {
1311             if(classSym.registered)
1312                DeclareMembers(classSym.registered, false);
1313          }
1314
1315          if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1316          {
1317             // TODO: Fix this
1318             //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1319
1320             // DANGER
1321             if(classSym.structExternal)
1322                ast->Move(classSym.structExternal, curExternal.prev);
1323             ast->Move(classSym.pointerExternal, curExternal.prev);
1324
1325             classSym.id = curExternal.symbol.idCode;
1326             classSym.idCode = curExternal.symbol.idCode;
1327             // external = classSym.pointerExternal;
1328             // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1329          }
1330
1331          classSym.declaring--;
1332       }
1333    }
1334    //return external;
1335 }
1336
1337 void DeclareProperty(Property prop, char * setName, char * getName)
1338 {
1339    Symbol symbol = prop.symbol;
1340
1341    strcpy(setName, "__ecereProp_");
1342    FullClassNameCat(setName, prop._class.fullName, false);
1343    strcat(setName, "_Set_");
1344    // strcat(setName, prop.name);
1345    FullClassNameCat(setName, prop.name, true);
1346    //MangleClassName(setName);
1347
1348    strcpy(getName, "__ecereProp_");
1349    FullClassNameCat(getName, prop._class.fullName, false);
1350    strcat(getName, "_Get_");
1351    FullClassNameCat(getName, prop.name, true);
1352    // strcat(getName, prop.name);
1353
1354    // To support "char *" property
1355    //MangleClassName(getName);
1356
1357    if(prop._class.type == structClass)
1358       DeclareStruct(prop._class.fullName, false);
1359
1360    if(!symbol || curExternal.symbol.idCode < symbol.id)
1361    {
1362       bool imported = false;
1363       bool dllImport = false;
1364
1365       if(!symbol || symbol._import)
1366       {
1367          if(!symbol)
1368          {
1369             Symbol classSym;
1370             if(!prop._class.symbol)
1371                prop._class.symbol = FindClass(prop._class.fullName);
1372             classSym = prop._class.symbol;
1373             if(classSym && !classSym._import)
1374             {
1375                ModuleImport module;
1376
1377                if(prop._class.module)
1378                   module = FindModule(prop._class.module);
1379                else
1380                   module = mainModule;
1381
1382                classSym._import = ClassImport
1383                {
1384                   name = CopyString(prop._class.fullName);
1385                   isRemote = prop._class.isRemote;
1386                };
1387                module.classes.Add(classSym._import);
1388             }
1389             symbol = prop.symbol = Symbol { };
1390             symbol._import = (ClassImport)PropertyImport
1391             {
1392                name = CopyString(prop.name);
1393                isVirtual = false; //prop.isVirtual;
1394                hasSet = prop.Set ? true : false;
1395                hasGet = prop.Get ? true : false;
1396             };
1397             if(classSym)
1398                classSym._import.properties.Add(symbol._import);
1399          }
1400          imported = true;
1401          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1402          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1403             prop._class.module.importType != staticImport)
1404             dllImport = true;
1405       }
1406
1407       if(!symbol.type)
1408       {
1409          Context context = SetupTemplatesContext(prop._class);
1410          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1411          FinishTemplatesContext(context);
1412       }
1413
1414       // Get
1415       if(prop.Get)
1416       {
1417          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1418          {
1419             Declaration decl;
1420             OldList * specifiers, * declarators;
1421             Declarator d;
1422             OldList * params;
1423             Specifier spec;
1424             External external;
1425             Declarator typeDecl;
1426             bool simple = false;
1427
1428             specifiers = MkList();
1429             declarators = MkList();
1430             params = MkList();
1431
1432             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1433                MkDeclaratorIdentifier(MkIdentifier("this"))));
1434
1435             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1436             //if(imported)
1437             if(dllImport)
1438                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1439
1440             {
1441                Context context = SetupTemplatesContext(prop._class);
1442                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1443                FinishTemplatesContext(context);
1444             }
1445
1446             // Make sure the simple _class's type is declared
1447             for(spec = specifiers->first; spec; spec = spec.next)
1448             {
1449                if(spec.type == nameSpecifier /*SpecifierClass*/)
1450                {
1451                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1452                   {
1453                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1454                      symbol._class = classSym.registered;
1455                      if(classSym.registered && classSym.registered.type == structClass)
1456                      {
1457                         DeclareStruct(spec.name, false);
1458                         simple = true;
1459                      }
1460                   }
1461                }
1462             }
1463
1464             if(!simple)
1465                d = PlugDeclarator(typeDecl, d);
1466             else
1467             {
1468                ListAdd(params, MkTypeName(specifiers,
1469                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1470                specifiers = MkList();
1471             }
1472
1473             d = MkDeclaratorFunction(d, params);
1474
1475             //if(imported)
1476             if(dllImport)
1477                specifiers->Insert(null, MkSpecifier(EXTERN));
1478             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1479                specifiers->Insert(null, MkSpecifier(STATIC));
1480             if(simple)
1481                ListAdd(specifiers, MkSpecifier(VOID));
1482
1483             ListAdd(declarators, MkInitDeclarator(d, null));
1484
1485             decl = MkDeclaration(specifiers, declarators);
1486
1487             external = MkExternalDeclaration(decl);
1488             ast->Insert(curExternal.prev, external);
1489             external.symbol = symbol;
1490             symbol.externalGet = external;
1491
1492             ReplaceThisClassSpecifiers(specifiers, prop._class);
1493
1494             if(typeDecl)
1495                FreeDeclarator(typeDecl);
1496          }
1497          else
1498          {
1499             // Move declaration higher...
1500             ast->Move(symbol.externalGet, curExternal.prev);
1501          }
1502       }
1503
1504       // Set
1505       if(prop.Set)
1506       {
1507          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1508          {
1509             Declaration decl;
1510             OldList * specifiers, * declarators;
1511             Declarator d;
1512             OldList * params;
1513             Specifier spec;
1514             External external;
1515             Declarator typeDecl;
1516
1517             declarators = MkList();
1518             params = MkList();
1519
1520             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1521             if(!prop.conversion || prop._class.type == structClass)
1522             {
1523                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1524                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1525             }
1526
1527             specifiers = MkList();
1528
1529             {
1530                Context context = SetupTemplatesContext(prop._class);
1531                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1532                   MkDeclaratorIdentifier(MkIdentifier("value")));
1533                FinishTemplatesContext(context);
1534             }
1535             if(!strcmp(prop._class.base.fullName, "eda::Row") || !strcmp(prop._class.base.fullName, "eda::Id"))
1536                specifiers->Insert(null, MkSpecifier(CONST));
1537
1538             ListAdd(params, MkTypeName(specifiers, d));
1539
1540             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1541             //if(imported)
1542             if(dllImport)
1543                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1544             d = MkDeclaratorFunction(d, params);
1545
1546             // Make sure the simple _class's type is declared
1547             for(spec = specifiers->first; spec; spec = spec.next)
1548             {
1549                if(spec.type == nameSpecifier /*SpecifierClass*/)
1550                {
1551                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1552                   {
1553                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1554                      symbol._class = classSym.registered;
1555                      if(classSym.registered && classSym.registered.type == structClass)
1556                         DeclareStruct(spec.name, false);
1557                   }
1558                }
1559             }
1560
1561             ListAdd(declarators, MkInitDeclarator(d, null));
1562
1563             specifiers = MkList();
1564             //if(imported)
1565             if(dllImport)
1566                specifiers->Insert(null, MkSpecifier(EXTERN));
1567             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1568                specifiers->Insert(null, MkSpecifier(STATIC));
1569
1570             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1571             if(!prop.conversion || prop._class.type == structClass)
1572                ListAdd(specifiers, MkSpecifier(VOID));
1573             else
1574                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1575
1576             decl = MkDeclaration(specifiers, declarators);
1577
1578             external = MkExternalDeclaration(decl);
1579             ast->Insert(curExternal.prev, external);
1580             external.symbol = symbol;
1581             symbol.externalSet = external;
1582
1583             ReplaceThisClassSpecifiers(specifiers, prop._class);
1584          }
1585          else
1586          {
1587             // Move declaration higher...
1588             ast->Move(symbol.externalSet, curExternal.prev);
1589          }
1590       }
1591
1592       // Property (for Watchers)
1593       if(!symbol.externalPtr)
1594       {
1595          Declaration decl;
1596          External external;
1597          OldList * specifiers = MkList();
1598          char propName[1024];
1599
1600          if(imported)
1601             specifiers->Insert(null, MkSpecifier(EXTERN));
1602          else
1603          {
1604             specifiers->Insert(null, MkSpecifier(STATIC));
1605             specifiers->Add(MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
1606          }
1607
1608          ListAdd(specifiers, MkSpecifierName("Property"));
1609
1610          strcpy(propName, "__ecereProp_");
1611          FullClassNameCat(propName, prop._class.fullName, false);
1612          strcat(propName, "_");
1613          FullClassNameCat(propName, prop.name, true);
1614          // strcat(propName, prop.name);
1615          //MangleClassName(propName);
1616
1617          {
1618             OldList * list = MkList();
1619             ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1620
1621             if(!imported)
1622             {
1623                strcpy(propName, "__ecerePropM_");
1624                FullClassNameCat(propName, prop._class.fullName, false);
1625                strcat(propName, "_");
1626                // strcat(propName, prop.name);
1627                FullClassNameCat(propName, prop.name, true);
1628
1629                //MangleClassName(propName);
1630
1631                ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1632             }
1633             decl = MkDeclaration(specifiers, list);
1634          }
1635
1636          external = MkExternalDeclaration(decl);
1637          ast->Insert(curExternal.prev, external);
1638          external.symbol = symbol;
1639          symbol.externalPtr = external;
1640       }
1641       else
1642       {
1643          // Move declaration higher...
1644          ast->Move(symbol.externalPtr, curExternal.prev);
1645       }
1646
1647       symbol.id = curExternal.symbol.idCode;
1648    }
1649 }
1650
1651 // ***************** EXPRESSION PROCESSING ***************************
1652 public Type Dereference(Type source)
1653 {
1654    Type type = null;
1655    if(source)
1656    {
1657       if(source.kind == pointerType || source.kind == arrayType)
1658       {
1659          type = source.type;
1660          source.type.refCount++;
1661       }
1662       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1663       {
1664          type = Type
1665          {
1666             kind = charType;
1667             refCount = 1;
1668          };
1669       }
1670       // Support dereferencing of no head classes for now...
1671       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1672       {
1673          type = source;
1674          source.refCount++;
1675       }
1676       else
1677          Compiler_Error($"cannot dereference type\n");
1678    }
1679    return type;
1680 }
1681
1682 static Type Reference(Type source)
1683 {
1684    Type type = null;
1685    if(source)
1686    {
1687       type = Type
1688       {
1689          kind = pointerType;
1690          type = source;
1691          refCount = 1;
1692       };
1693       source.refCount++;
1694    }
1695    return type;
1696 }
1697
1698 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1699 {
1700    Identifier ident = member.identifiers ? member.identifiers->first : null;
1701    bool found = false;
1702    DataMember dataMember = null;
1703    Method method = null;
1704    bool freeType = false;
1705
1706    yylloc = member.loc;
1707
1708    if(!ident)
1709    {
1710       if(curMember)
1711       {
1712          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1713          if(*curMember)
1714          {
1715             found = true;
1716             dataMember = *curMember;
1717          }
1718       }
1719    }
1720    else
1721    {
1722       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1723       DataMember _subMemberStack[256];
1724       int _subMemberStackPos = 0;
1725
1726       // FILL MEMBER STACK
1727       if(!thisMember)
1728          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1729       if(thisMember)
1730       {
1731          dataMember = thisMember;
1732          if(curMember && thisMember.memberAccess == publicAccess)
1733          {
1734             *curMember = thisMember;
1735             *curClass = thisMember._class;
1736             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1737             *subMemberStackPos = _subMemberStackPos;
1738          }
1739          found = true;
1740       }
1741       else
1742       {
1743          // Setting a method
1744          method = eClass_FindMethod(_class, ident.string, privateModule);
1745          if(method && method.type == virtualMethod)
1746             found = true;
1747          else
1748             method = null;
1749       }
1750    }
1751
1752    if(found)
1753    {
1754       Type type = null;
1755       if(dataMember)
1756       {
1757          if(!dataMember.dataType && dataMember.dataTypeString)
1758          {
1759             //Context context = SetupTemplatesContext(dataMember._class);
1760             Context context = SetupTemplatesContext(_class);
1761             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1762             FinishTemplatesContext(context);
1763          }
1764          type = dataMember.dataType;
1765       }
1766       else if(method)
1767       {
1768          // This is for destination type...
1769          if(!method.dataType)
1770             ProcessMethodType(method);
1771          //DeclareMethod(method);
1772          // method.dataType = ((Symbol)method.symbol)->type;
1773          type = method.dataType;
1774       }
1775
1776       if(ident && ident.next)
1777       {
1778          for(ident = ident.next; ident && type; ident = ident.next)
1779          {
1780             if(type.kind == classType)
1781             {
1782                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1783                if(!dataMember)
1784                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1785                if(dataMember)
1786                   type = dataMember.dataType;
1787             }
1788             else if(type.kind == structType || type.kind == unionType)
1789             {
1790                Type memberType;
1791                for(memberType = type.members.first; memberType; memberType = memberType.next)
1792                {
1793                   if(!strcmp(memberType.name, ident.string))
1794                   {
1795                      type = memberType;
1796                      break;
1797                   }
1798                }
1799             }
1800          }
1801       }
1802
1803       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1804       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1805       {
1806          int id = 0;
1807          ClassTemplateParameter curParam = null;
1808          Class sClass;
1809          for(sClass = _class; sClass; sClass = sClass.base)
1810          {
1811             id = 0;
1812             if(sClass.templateClass) sClass = sClass.templateClass;
1813             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1814             {
1815                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1816                {
1817                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1818                   {
1819                      if(sClass.templateClass) sClass = sClass.templateClass;
1820                      id += sClass.templateParams.count;
1821                   }
1822                   break;
1823                }
1824                id++;
1825             }
1826             if(curParam) break;
1827          }
1828
1829          if(curParam)
1830          {
1831             ClassTemplateArgument arg = _class.templateArgs[id];
1832             if(arg.dataTypeString)
1833             {
1834                bool constant = type.constant;
1835                // FreeType(type);
1836                type = ProcessTypeString(arg.dataTypeString, false);
1837                if(type.kind == classType && constant) type.constant = true;
1838                else if(type.kind == pointerType)
1839                {
1840                   Type t = type.type;
1841                   while(t.kind == pointerType) t = t.type;
1842                   if(constant) t.constant = constant;
1843                }
1844                freeType = true;
1845                if(type && _class.templateClass)
1846                   type.passAsTemplate = true;
1847                if(type)
1848                {
1849                   // type.refCount++;
1850                   /*if(!exp.destType)
1851                   {
1852                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1853                      exp.destType.refCount++;
1854                   }*/
1855                }
1856             }
1857          }
1858       }
1859       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1860       {
1861          Class expClass = type._class.registered;
1862          Class cClass = null;
1863          int paramCount = 0;
1864          int lastParam = -1;
1865
1866          char templateString[1024];
1867          ClassTemplateParameter param;
1868          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1869          for(cClass = expClass; cClass; cClass = cClass.base)
1870          {
1871             int p = 0;
1872             if(cClass.templateClass) cClass = cClass.templateClass;
1873             for(param = cClass.templateParams.first; param; param = param.next)
1874             {
1875                int id = p;
1876                Class sClass;
1877                ClassTemplateArgument arg;
1878                for(sClass = cClass.base; sClass; sClass = sClass.base)
1879                {
1880                   if(sClass.templateClass) sClass = sClass.templateClass;
1881                   id += sClass.templateParams.count;
1882                }
1883                arg = expClass.templateArgs[id];
1884
1885                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1886                {
1887                   ClassTemplateParameter cParam;
1888                   //int p = numParams - sClass.templateParams.count;
1889                   int p = 0;
1890                   Class nextClass;
1891                   if(sClass.templateClass) sClass = sClass.templateClass;
1892
1893                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1894                   {
1895                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1896                      p += nextClass.templateParams.count;
1897                   }
1898
1899                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1900                   {
1901                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1902                      {
1903                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1904                         {
1905                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1906                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1907                            break;
1908                         }
1909                      }
1910                   }
1911                }
1912
1913                {
1914                   char argument[256];
1915                   argument[0] = '\0';
1916                   /*if(arg.name)
1917                   {
1918                      strcat(argument, arg.name.string);
1919                      strcat(argument, " = ");
1920                   }*/
1921                   switch(param.type)
1922                   {
1923                      case expression:
1924                      {
1925                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1926                         char expString[1024];
1927                         OldList * specs = MkList();
1928                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1929                         Expression exp;
1930                         char * string = PrintHexUInt64(arg.expression.ui64);
1931                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1932                         delete string;
1933
1934                         ProcessExpressionType(exp);
1935                         ComputeExpression(exp);
1936                         expString[0] = '\0';
1937                         PrintExpression(exp, expString);
1938                         strcat(argument, expString);
1939                         //delete exp;
1940                         FreeExpression(exp);
1941                         break;
1942                      }
1943                      case identifier:
1944                      {
1945                         strcat(argument, arg.member.name);
1946                         break;
1947                      }
1948                      case TemplateParameterType::type:
1949                      {
1950                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1951                            strcat(argument, arg.dataTypeString);
1952                         break;
1953                      }
1954                   }
1955                   if(argument[0])
1956                   {
1957                      if(paramCount) strcat(templateString, ", ");
1958                      if(lastParam != p - 1)
1959                      {
1960                         strcat(templateString, param.name);
1961                         strcat(templateString, " = ");
1962                      }
1963                      strcat(templateString, argument);
1964                      paramCount++;
1965                      lastParam = p;
1966                   }
1967                   p++;
1968                }
1969             }
1970          }
1971          {
1972             int len = strlen(templateString);
1973             if(templateString[len-1] == '<')
1974                len--;
1975             else
1976             {
1977                if(templateString[len-1] == '>')
1978                   templateString[len++] = ' ';
1979                templateString[len++] = '>';
1980             }
1981             templateString[len++] = '\0';
1982          }
1983          {
1984             Context context = SetupTemplatesContext(_class);
1985             if(freeType) FreeType(type);
1986             type = ProcessTypeString(templateString, false);
1987             freeType = true;
1988             FinishTemplatesContext(context);
1989          }
1990       }
1991
1992       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1993       {
1994          ProcessExpressionType(member.initializer.exp);
1995          if(!member.initializer.exp.expType)
1996          {
1997             if(inCompiler)
1998             {
1999                char expString[10240];
2000                expString[0] = '\0';
2001                PrintExpression(member.initializer.exp, expString);
2002                ChangeCh(expString, '\n', ' ');
2003                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
2004             }
2005          }
2006          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
2007          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false, true))
2008          {
2009             Compiler_Error($"incompatible instance method %s\n", ident.string);
2010          }
2011       }
2012       else if(member.initializer)
2013       {
2014          /*
2015          FreeType(member.exp.destType);
2016          member.exp.destType = type;
2017          if(member.exp.destType)
2018             member.exp.destType.refCount++;
2019          ProcessExpressionType(member.exp);
2020          */
2021
2022          ProcessInitializer(member.initializer, type);
2023       }
2024       if(freeType) FreeType(type);
2025    }
2026    else
2027    {
2028       if(_class && _class.type == unitClass)
2029       {
2030          if(member.initializer)
2031          {
2032             /*
2033             FreeType(member.exp.destType);
2034             member.exp.destType = MkClassType(_class.fullName);
2035             ProcessExpressionType(member.initializer, type);
2036             */
2037             Type type = MkClassType(_class.fullName);
2038             ProcessInitializer(member.initializer, type);
2039             FreeType(type);
2040          }
2041       }
2042       else
2043       {
2044          if(member.initializer)
2045          {
2046             //ProcessExpressionType(member.exp);
2047             ProcessInitializer(member.initializer, null);
2048          }
2049          if(ident)
2050          {
2051             if(method)
2052             {
2053                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
2054             }
2055             else if(_class)
2056             {
2057                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
2058                if(inCompiler)
2059                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
2060             }
2061          }
2062          else if(_class)
2063             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
2064       }
2065    }
2066 }
2067
2068 void ProcessInstantiationType(Instantiation inst)
2069 {
2070    yylloc = inst.loc;
2071    if(inst._class)
2072    {
2073       MembersInit members;
2074       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
2075       Class _class;
2076
2077       /*if(!inst._class.symbol)
2078          inst._class.symbol = FindClass(inst._class.name);*/
2079       classSym = inst._class.symbol;
2080       _class = classSym ? classSym.registered : null;
2081
2082       // DANGER: Patch for mutex not declaring its struct when not needed
2083       if(!_class || _class.type != noHeadClass)
2084          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
2085
2086       afterExternal = afterExternal ? afterExternal : curExternal;
2087
2088       if(inst.exp)
2089          ProcessExpressionType(inst.exp);
2090
2091       inst.isConstant = true;
2092       if(inst.members)
2093       {
2094          DataMember curMember = null;
2095          Class curClass = null;
2096          DataMember subMemberStack[256];
2097          int subMemberStackPos = 0;
2098
2099          for(members = inst.members->first; members; members = members.next)
2100          {
2101             switch(members.type)
2102             {
2103                case methodMembersInit:
2104                {
2105                   char name[1024];
2106                   static uint instMethodID = 0;
2107                   External external = curExternal;
2108                   Context context = curContext;
2109                   Declarator declarator = members.function.declarator;
2110                   Identifier nameID = GetDeclId(declarator);
2111                   char * unmangled = nameID ? nameID.string : null;
2112                   Expression exp;
2113                   External createdExternal = null;
2114
2115                   if(inCompiler)
2116                   {
2117                      char number[16];
2118                      //members.function.dontMangle = true;
2119                      strcpy(name, "__ecereInstMeth_");
2120                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2121                      strcat(name, "_");
2122                      strcat(name, nameID.string);
2123                      strcat(name, "_");
2124                      sprintf(number, "_%08d", instMethodID++);
2125                      strcat(name, number);
2126                      nameID.string = CopyString(name);
2127                   }
2128
2129                   // Do modifications here...
2130                   if(declarator)
2131                   {
2132                      Symbol symbol = declarator.symbol;
2133                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2134
2135                      if(method && method.type == virtualMethod)
2136                      {
2137                         symbol.method = method;
2138                         ProcessMethodType(method);
2139
2140                         if(!symbol.type.thisClass)
2141                         {
2142                            if(method.dataType.thisClass && currentClass &&
2143                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2144                            {
2145                               if(!currentClass.symbol)
2146                                  currentClass.symbol = FindClass(currentClass.fullName);
2147                               symbol.type.thisClass = currentClass.symbol;
2148                            }
2149                            else
2150                            {
2151                               if(!_class.symbol)
2152                                  _class.symbol = FindClass(_class.fullName);
2153                               symbol.type.thisClass = _class.symbol;
2154                            }
2155                         }
2156                         // TESTING THIS HERE:
2157                         DeclareType(symbol.type, true, true);
2158
2159                      }
2160                      else if(classSym)
2161                      {
2162                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2163                            unmangled, classSym.string);
2164                      }
2165                   }
2166
2167                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2168                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2169
2170                   if(nameID)
2171                   {
2172                      FreeSpecifier(nameID._class);
2173                      nameID._class = null;
2174                   }
2175
2176                   if(inCompiler)
2177                   {
2178                      //Type type = declarator.symbol.type;
2179                      External oldExternal = curExternal;
2180
2181                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2182                      // *** It was commented out for problems such as
2183                      /*
2184                            class VirtualDesktop : Window
2185                            {
2186                               clientSize = Size { };
2187                               Timer timer
2188                               {
2189                                  bool DelayExpired()
2190                                  {
2191                                     clientSize.w;
2192                                     return true;
2193                                  }
2194                               };
2195                            }
2196                      */
2197                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2198
2199                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2200
2201                      /*
2202                      if(strcmp(declarator.symbol.string, name))
2203                      {
2204                         printf("TOCHECK: Look out for this\n");
2205                         delete declarator.symbol.string;
2206                         declarator.symbol.string = CopyString(name);
2207                      }
2208
2209                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2210                      {
2211                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2212                         excludedSymbols->Remove(declarator.symbol);
2213                         globalContext.symbols.Add((BTNode)declarator.symbol);
2214                         if(strstr(declarator.symbol.string), "::")
2215                            globalContext.hasNameSpace = true;
2216
2217                      }
2218                      */
2219
2220                      //curExternal = curExternal.prev;
2221                      //afterExternal = afterExternal->next;
2222
2223                      //ProcessFunction(afterExternal->function);
2224
2225                      //curExternal = afterExternal;
2226                      {
2227                         External externalDecl;
2228                         externalDecl = MkExternalDeclaration(null);
2229                         ast->Insert(oldExternal.prev, externalDecl);
2230
2231                         // Which function does this process?
2232                         if(createdExternal.function)
2233                         {
2234                            ProcessFunction(createdExternal.function);
2235
2236                            //curExternal = oldExternal;
2237
2238                            {
2239                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2240
2241                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2242                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2243
2244                               //externalDecl = MkExternalDeclaration(decl);
2245
2246                               //***** ast->Insert(external.prev, externalDecl);
2247                               //ast->Insert(curExternal.prev, externalDecl);
2248                               externalDecl.declaration = decl;
2249                               if(decl.symbol && !decl.symbol.pointerExternal)
2250                                  decl.symbol.pointerExternal = externalDecl;
2251
2252                               // Trying this out...
2253                               declarator.symbol.pointerExternal = externalDecl;
2254                            }
2255                         }
2256                      }
2257                   }
2258                   else if(declarator)
2259                   {
2260                      curExternal = declarator.symbol.pointerExternal;
2261                      ProcessFunction((FunctionDefinition)members.function);
2262                   }
2263                   curExternal = external;
2264                   curContext = context;
2265
2266                   if(inCompiler)
2267                   {
2268                      FreeClassFunction(members.function);
2269
2270                      // In this pass, turn this into a MemberInitData
2271                      exp = QMkExpId(name);
2272                      members.type = dataMembersInit;
2273                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2274
2275                      delete unmangled;
2276                   }
2277                   break;
2278                }
2279                case dataMembersInit:
2280                {
2281                   if(members.dataMembers && classSym)
2282                   {
2283                      MemberInit member;
2284                      Location oldyyloc = yylloc;
2285                      for(member = members.dataMembers->first; member; member = member.next)
2286                      {
2287                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2288                         if(member.initializer && !member.initializer.isConstant)
2289                            inst.isConstant = false;
2290                      }
2291                      yylloc = oldyyloc;
2292                   }
2293                   break;
2294                }
2295             }
2296          }
2297       }
2298    }
2299 }
2300
2301 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2302 {
2303    // OPTIMIZATIONS: TESTING THIS...
2304    if(inCompiler)
2305    {
2306       if(type.kind == functionType)
2307       {
2308          Type param;
2309          if(declareParams)
2310          {
2311             for(param = type.params.first; param; param = param.next)
2312                DeclareType(param, declarePointers, true);
2313          }
2314          DeclareType(type.returnType, declarePointers, true);
2315       }
2316       else if(type.kind == pointerType && declarePointers)
2317          DeclareType(type.type, declarePointers, false);
2318       else if(type.kind == classType)
2319       {
2320          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2321             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2322       }
2323       else if(type.kind == structType || type.kind == unionType)
2324       {
2325          Type member;
2326          for(member = type.members.first; member; member = member.next)
2327             DeclareType(member, false, false);
2328       }
2329       else if(type.kind == arrayType)
2330          DeclareType(type.arrayType, declarePointers, false);
2331    }
2332 }
2333
2334 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2335 {
2336    ClassTemplateArgument * arg = null;
2337    int id = 0;
2338    ClassTemplateParameter curParam = null;
2339    Class sClass;
2340    for(sClass = _class; sClass; sClass = sClass.base)
2341    {
2342       id = 0;
2343       if(sClass.templateClass) sClass = sClass.templateClass;
2344       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2345       {
2346          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2347          {
2348             for(sClass = sClass.base; sClass; sClass = sClass.base)
2349             {
2350                if(sClass.templateClass) sClass = sClass.templateClass;
2351                id += sClass.templateParams.count;
2352             }
2353             break;
2354          }
2355          id++;
2356       }
2357       if(curParam) break;
2358    }
2359    if(curParam)
2360    {
2361       arg = &_class.templateArgs[id];
2362       if(arg && param.type == type)
2363          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2364    }
2365    return arg;
2366 }
2367
2368 public Context SetupTemplatesContext(Class _class)
2369 {
2370    Context context = PushContext();
2371    context.templateTypesOnly = true;
2372    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2373    {
2374       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2375       for(; param; param = param.next)
2376       {
2377          if(param.type == type && param.identifier)
2378          {
2379             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2380             curContext.templateTypes.Add((BTNode)type);
2381          }
2382       }
2383    }
2384    else if(_class)
2385    {
2386       Class sClass;
2387       for(sClass = _class; sClass; sClass = sClass.base)
2388       {
2389          ClassTemplateParameter p;
2390          for(p = sClass.templateParams.first; p; p = p.next)
2391          {
2392             //OldList * specs = MkList();
2393             //Declarator decl = null;
2394             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2395             if(p.type == type)
2396             {
2397                TemplateParameter param = p.param;
2398                TemplatedType type;
2399                if(!param)
2400                {
2401                   // ADD DATA TYPE HERE...
2402                   p.param = param = TemplateParameter
2403                   {
2404                      identifier = MkIdentifier(p.name), type = p.type,
2405                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2406                   };
2407                }
2408                type = TemplatedType { key = (uintptr)p.name, param = param };
2409                curContext.templateTypes.Add((BTNode)type);
2410             }
2411          }
2412       }
2413    }
2414    return context;
2415 }
2416
2417 public void FinishTemplatesContext(Context context)
2418 {
2419    PopContext(context);
2420    FreeContext(context);
2421    delete context;
2422 }
2423
2424 public void ProcessMethodType(Method method)
2425 {
2426    if(!method.dataType)
2427    {
2428       Context context = SetupTemplatesContext(method._class);
2429
2430       method.dataType = ProcessTypeString(method.dataTypeString, false);
2431
2432       FinishTemplatesContext(context);
2433
2434       if(method.type != virtualMethod && method.dataType)
2435       {
2436          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2437          {
2438             if(!method._class.symbol)
2439                method._class.symbol = FindClass(method._class.fullName);
2440             method.dataType.thisClass = method._class.symbol;
2441          }
2442       }
2443
2444       // Why was this commented out? Working fine without now...
2445
2446       /*
2447       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2448          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2449          */
2450    }
2451
2452    /*
2453    if(type)
2454    {
2455       char * par = strstr(type, "(");
2456       char * classOp = null;
2457       int classOpLen = 0;
2458       if(par)
2459       {
2460          int c;
2461          for(c = par-type-1; c >= 0; c++)
2462          {
2463             if(type[c] == ':' && type[c+1] == ':')
2464             {
2465                classOp = type + c - 1;
2466                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2467                {
2468                   classOp--;
2469                   classOpLen++;
2470                }
2471                break;
2472             }
2473             else if(!isspace(type[c]))
2474                break;
2475          }
2476       }
2477       if(classOp)
2478       {
2479          char temp[1024];
2480          int typeLen = strlen(type);
2481          memcpy(temp, classOp, classOpLen);
2482          temp[classOpLen] = '\0';
2483          if(temp[0])
2484             _class = eSystem_FindClass(module, temp);
2485          else
2486             _class = null;
2487          method.dataTypeString = new char[typeLen - classOpLen + 1];
2488          memcpy(method.dataTypeString, type, classOp - type);
2489          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2490       }
2491       else
2492          method.dataTypeString = type;
2493    }
2494    */
2495 }
2496
2497
2498 public void ProcessPropertyType(Property prop)
2499 {
2500    if(!prop.dataType)
2501    {
2502       Context context = SetupTemplatesContext(prop._class);
2503       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2504       FinishTemplatesContext(context);
2505    }
2506 }
2507
2508 public void DeclareMethod(Method method, const char * name)
2509 {
2510    Symbol symbol = method.symbol;
2511    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2512    {
2513       //bool imported = false;
2514       bool dllImport = false;
2515
2516       if(!method.dataType)
2517          method.dataType = ProcessTypeString(method.dataTypeString, false);
2518
2519       if(!symbol || symbol._import || method.type == virtualMethod)
2520       {
2521          if(!symbol || method.type == virtualMethod)
2522          {
2523             Symbol classSym;
2524             if(!method._class.symbol)
2525                method._class.symbol = FindClass(method._class.fullName);
2526             classSym = method._class.symbol;
2527             if(!classSym._import)
2528             {
2529                ModuleImport module;
2530
2531                if(method._class.module && method._class.module.name)
2532                   module = FindModule(method._class.module);
2533                else
2534                   module = mainModule;
2535                classSym._import = ClassImport
2536                {
2537                   name = CopyString(method._class.fullName);
2538                   isRemote = method._class.isRemote;
2539                };
2540                module.classes.Add(classSym._import);
2541             }
2542             if(!symbol)
2543             {
2544                symbol = method.symbol = Symbol { };
2545             }
2546             if(!symbol._import)
2547             {
2548                symbol._import = (ClassImport)MethodImport
2549                {
2550                   name = CopyString(method.name);
2551                   isVirtual = method.type == virtualMethod;
2552                };
2553                classSym._import.methods.Add(symbol._import);
2554             }
2555             if(!symbol)
2556             {
2557                // Set the symbol type
2558                /*
2559                if(!type.thisClass)
2560                {
2561                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2562                }
2563                else if(type.thisClass == (void *)-1)
2564                {
2565                   type.thisClass = null;
2566                }
2567                */
2568                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2569                symbol.type = method.dataType;
2570                if(symbol.type) symbol.type.refCount++;
2571             }
2572             /*
2573             if(!method.thisClass || strcmp(method.thisClass, "void"))
2574                symbol.type.params.Insert(null,
2575                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2576             */
2577          }
2578          if(!method.dataType.dllExport)
2579          {
2580             //imported = true;
2581             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2582                dllImport = true;
2583          }
2584       }
2585
2586       /* MOVING THIS UP
2587       if(!method.dataType)
2588          method.dataType = ((Symbol)method.symbol).type;
2589          //ProcessMethodType(method);
2590       */
2591
2592       if(method.type != virtualMethod && method.dataType)
2593          DeclareType(method.dataType, true, true);
2594
2595       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2596       {
2597          // We need a declaration here :)
2598          Declaration decl;
2599          OldList * specifiers, * declarators;
2600          Declarator d;
2601          Declarator funcDecl;
2602          External external;
2603
2604          specifiers = MkList();
2605          declarators = MkList();
2606
2607          //if(imported)
2608          if(dllImport)
2609             ListAdd(specifiers, MkSpecifier(EXTERN));
2610          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2611             ListAdd(specifiers, MkSpecifier(STATIC));
2612
2613          if(method.type == virtualMethod)
2614          {
2615             ListAdd(specifiers, MkSpecifier(INT));
2616             d = MkDeclaratorIdentifier(MkIdentifier(name));
2617          }
2618          else
2619          {
2620             d = MkDeclaratorIdentifier(MkIdentifier(name));
2621             //if(imported)
2622             if(dllImport)
2623                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2624             {
2625                Context context = SetupTemplatesContext(method._class);
2626                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2627                FinishTemplatesContext(context);
2628             }
2629             funcDecl = GetFuncDecl(d);
2630
2631             if(dllImport)
2632             {
2633                Specifier spec, next;
2634                for(spec = specifiers->first; spec; spec = next)
2635                {
2636                   next = spec.next;
2637                   if(spec.type == extendedSpecifier)
2638                   {
2639                      specifiers->Remove(spec);
2640                      FreeSpecifier(spec);
2641                   }
2642                }
2643             }
2644
2645             // Add this parameter if not a static method
2646             if(method.dataType && !method.dataType.staticMethod)
2647             {
2648                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2649                {
2650                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2651                   TypeName thisParam = MkTypeName(MkListOne(
2652                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2653                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2654                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2655                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2656
2657                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2658                   {
2659                      TypeName param = funcDecl.function.parameters->first;
2660                      funcDecl.function.parameters->Remove(param);
2661                      FreeTypeName(param);
2662                   }
2663
2664                   if(!funcDecl.function.parameters)
2665                      funcDecl.function.parameters = MkList();
2666                   funcDecl.function.parameters->Insert(null, thisParam);
2667                }
2668             }
2669             // Make sure we don't have empty parameter declarations for static methods...
2670             /*
2671             else if(!funcDecl.function.parameters)
2672             {
2673                funcDecl.function.parameters = MkList();
2674                funcDecl.function.parameters->Insert(null,
2675                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2676             }*/
2677          }
2678          // TESTING THIS:
2679          ProcessDeclarator(d);
2680
2681          ListAdd(declarators, MkInitDeclarator(d, null));
2682
2683          decl = MkDeclaration(specifiers, declarators);
2684
2685          ReplaceThisClassSpecifiers(specifiers, method._class);
2686
2687          // Keep a different symbol for the function definition than the declaration...
2688          if(symbol.pointerExternal)
2689          {
2690             Symbol functionSymbol { };
2691
2692             // Copy symbol
2693             {
2694                *functionSymbol = *symbol;
2695                functionSymbol.string = CopyString(symbol.string);
2696                if(functionSymbol.type)
2697                   functionSymbol.type.refCount++;
2698             }
2699
2700             excludedSymbols->Add(functionSymbol);
2701             symbol.pointerExternal.symbol = functionSymbol;
2702          }
2703          external = MkExternalDeclaration(decl);
2704          if(curExternal)
2705             ast->Insert(curExternal ? curExternal.prev : null, external);
2706          external.symbol = symbol;
2707          symbol.pointerExternal = external;
2708       }
2709       else if(ast)
2710       {
2711          // Move declaration higher...
2712          ast->Move(symbol.pointerExternal, curExternal.prev);
2713       }
2714
2715       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2716    }
2717 }
2718
2719 char * ReplaceThisClass(Class _class)
2720 {
2721    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2722    {
2723       bool first = true;
2724       int p = 0;
2725       ClassTemplateParameter param;
2726       int lastParam = -1;
2727
2728       char className[1024];
2729       strcpy(className, _class.fullName);
2730       for(param = _class.templateParams.first; param; param = param.next)
2731       {
2732          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2733          {
2734             if(first) strcat(className, "<");
2735             if(!first) strcat(className, ", ");
2736             if(lastParam + 1 != p)
2737             {
2738                strcat(className, param.name);
2739                strcat(className, " = ");
2740             }
2741             strcat(className, param.name);
2742             first = false;
2743             lastParam = p;
2744          }
2745          p++;
2746       }
2747       if(!first)
2748       {
2749          int len = strlen(className);
2750          if(className[len-1] == '>') className[len++] = ' ';
2751          className[len++] = '>';
2752          className[len++] = '\0';
2753       }
2754       return CopyString(className);
2755    }
2756    else
2757       return CopyString(_class.fullName);
2758 }
2759
2760 Type ReplaceThisClassType(Class _class)
2761 {
2762    Type type;
2763    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2764    {
2765       bool first = true;
2766       int p = 0;
2767       ClassTemplateParameter param;
2768       int lastParam = -1;
2769       char className[1024];
2770       strcpy(className, _class.fullName);
2771
2772       for(param = _class.templateParams.first; param; param = param.next)
2773       {
2774          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2775          {
2776             if(first) strcat(className, "<");
2777             if(!first) strcat(className, ", ");
2778             if(lastParam + 1 != p)
2779             {
2780                strcat(className, param.name);
2781                strcat(className, " = ");
2782             }
2783             strcat(className, param.name);
2784             first = false;
2785             lastParam = p;
2786          }
2787          p++;
2788       }
2789       if(!first)
2790       {
2791          int len = strlen(className);
2792          if(className[len-1] == '>') className[len++] = ' ';
2793          className[len++] = '>';
2794          className[len++] = '\0';
2795       }
2796       type = MkClassType(className);
2797       //type = ProcessTypeString(className, false);
2798    }
2799    else
2800    {
2801       type = MkClassType(_class.fullName);
2802       //type = ProcessTypeString(_class.fullName, false);
2803    }
2804    //type.wasThisClass = true;
2805    return type;
2806 }
2807
2808 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2809 {
2810    if(specs != null && _class)
2811    {
2812       Specifier spec;
2813       for(spec = specs.first; spec; spec = spec.next)
2814       {
2815          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2816          {
2817             spec.type = nameSpecifier;
2818             spec.name = ReplaceThisClass(_class);
2819             spec.symbol = FindClass(spec.name); //_class.symbol;
2820          }
2821       }
2822    }
2823 }
2824
2825 // Returns imported or not
2826 bool DeclareFunction(GlobalFunction function, char * name)
2827 {
2828    Symbol symbol = function.symbol;
2829    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2830    {
2831       bool imported = false;
2832       bool dllImport = false;
2833
2834       if(!function.dataType)
2835       {
2836          function.dataType = ProcessTypeString(function.dataTypeString, false);
2837          if(!function.dataType.thisClass)
2838             function.dataType.staticMethod = true;
2839       }
2840
2841       if(inCompiler)
2842       {
2843          if(!symbol)
2844          {
2845             ModuleImport module = FindModule(function.module);
2846             // WARNING: This is not added anywhere...
2847             symbol = function.symbol = Symbol {  };
2848
2849             if(module.name)
2850             {
2851                if(!function.dataType.dllExport)
2852                {
2853                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2854                   module.functions.Add(symbol._import);
2855                }
2856             }
2857             // Set the symbol type
2858             {
2859                symbol.type = ProcessTypeString(function.dataTypeString, false);
2860                if(!symbol.type.thisClass)
2861                   symbol.type.staticMethod = true;
2862             }
2863          }
2864          imported = symbol._import ? true : false;
2865          if(imported && function.module != privateModule && function.module.importType != staticImport)
2866             dllImport = true;
2867       }
2868
2869       DeclareType(function.dataType, true, true);
2870
2871       if(inCompiler)
2872       {
2873          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2874          {
2875             // We need a declaration here :)
2876             Declaration decl;
2877             OldList * specifiers, * declarators;
2878             Declarator d;
2879             Declarator funcDecl;
2880             External external;
2881
2882             specifiers = MkList();
2883             declarators = MkList();
2884
2885             //if(imported)
2886                ListAdd(specifiers, MkSpecifier(EXTERN));
2887             /*
2888             else
2889                ListAdd(specifiers, MkSpecifier(STATIC));
2890             */
2891
2892             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2893             //if(imported)
2894             if(dllImport)
2895                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2896
2897             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2898             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2899             if(function.module.importType == staticImport)
2900             {
2901                Specifier spec;
2902                for(spec = specifiers->first; spec; spec = spec.next)
2903                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2904                   {
2905                      specifiers->Remove(spec);
2906                      FreeSpecifier(spec);
2907                      break;
2908                   }
2909             }
2910
2911             funcDecl = GetFuncDecl(d);
2912
2913             // Make sure we don't have empty parameter declarations for static methods...
2914             if(funcDecl && !funcDecl.function.parameters)
2915             {
2916                funcDecl.function.parameters = MkList();
2917                funcDecl.function.parameters->Insert(null,
2918                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2919             }
2920
2921             ListAdd(declarators, MkInitDeclarator(d, null));
2922
2923             {
2924                Context oldCtx = curContext;
2925                curContext = globalContext;
2926                decl = MkDeclaration(specifiers, declarators);
2927                curContext = oldCtx;
2928             }
2929
2930             // Keep a different symbol for the function definition than the declaration...
2931             if(symbol.pointerExternal)
2932             {
2933                Symbol functionSymbol { };
2934                // Copy symbol
2935                {
2936                   *functionSymbol = *symbol;
2937                   functionSymbol.string = CopyString(symbol.string);
2938                   if(functionSymbol.type)
2939                      functionSymbol.type.refCount++;
2940                }
2941
2942                excludedSymbols->Add(functionSymbol);
2943
2944                symbol.pointerExternal.symbol = functionSymbol;
2945             }
2946             external = MkExternalDeclaration(decl);
2947             if(curExternal)
2948                ast->Insert(curExternal.prev, external);
2949             external.symbol = symbol;
2950             symbol.pointerExternal = external;
2951          }
2952          else
2953          {
2954             // Move declaration higher...
2955             ast->Move(symbol.pointerExternal, curExternal.prev);
2956          }
2957
2958          if(curExternal)
2959             symbol.id = curExternal.symbol.idCode;
2960       }
2961    }
2962    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2963 }
2964
2965 void DeclareGlobalData(GlobalData data)
2966 {
2967    Symbol symbol = data.symbol;
2968    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2969    {
2970       if(inCompiler)
2971       {
2972          if(!symbol)
2973             symbol = data.symbol = Symbol { };
2974       }
2975       if(!data.dataType)
2976          data.dataType = ProcessTypeString(data.dataTypeString, false);
2977       DeclareType(data.dataType, true, true);
2978       if(inCompiler)
2979       {
2980          if(!symbol.pointerExternal)
2981          {
2982             // We need a declaration here :)
2983             Declaration decl;
2984             OldList * specifiers, * declarators;
2985             Declarator d;
2986             External external;
2987
2988             specifiers = MkList();
2989             declarators = MkList();
2990
2991             ListAdd(specifiers, MkSpecifier(EXTERN));
2992             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2993             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2994
2995             ListAdd(declarators, MkInitDeclarator(d, null));
2996
2997             decl = MkDeclaration(specifiers, declarators);
2998             external = MkExternalDeclaration(decl);
2999             if(curExternal)
3000                ast->Insert(curExternal.prev, external);
3001             external.symbol = symbol;
3002             symbol.pointerExternal = external;
3003          }
3004          else
3005          {
3006             // Move declaration higher...
3007             ast->Move(symbol.pointerExternal, curExternal.prev);
3008          }
3009
3010          if(curExternal)
3011             symbol.id = curExternal.symbol.idCode;
3012       }
3013    }
3014 }
3015
3016 class Conversion : struct
3017 {
3018    Conversion prev, next;
3019    Property convert;
3020    bool isGet;
3021    Type resultType;
3022 };
3023
3024 static bool CheckConstCompatibility(Type source, Type dest, bool warn)
3025 {
3026    bool status = true;
3027    if(((source.kind == classType && source._class && source._class.registered) || source.kind == arrayType || source.kind == pointerType) &&
3028       ((dest.kind == classType && dest._class && dest._class.registered) || /*dest.kind == arrayType || */dest.kind == pointerType))
3029    {
3030       Class sourceClass = source.kind == classType ? source._class.registered : null;
3031       Class destClass = dest.kind == classType ? dest._class.registered : null;
3032       if((!sourceClass || (sourceClass && sourceClass.type == normalClass && !sourceClass.structSize)) &&
3033          (!destClass || (destClass && destClass.type == normalClass && !destClass.structSize)))
3034       {
3035          Type sourceType = source, destType = dest;
3036          while((sourceType.kind == pointerType || sourceType.kind == arrayType) && sourceType.type) sourceType = sourceType.type;
3037          while((destType.kind == pointerType || destType.kind == arrayType) && destType.type) destType = destType.type;
3038          if(!destType.constant && sourceType.constant)
3039          {
3040             status = false;
3041             if(warn)
3042                Compiler_Warning($"discarding const qualifier\n");
3043          }
3044       }
3045    }
3046    return status;
3047 }
3048
3049 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams,
3050                        bool isConversionExploration, bool warnConst)
3051 {
3052    if(source && dest)
3053    {
3054       if(warnConst)
3055          CheckConstCompatibility(source, dest, true);
3056       // Property convert;
3057
3058       if(source.kind == templateType && dest.kind != templateType)
3059       {
3060          Type type = ProcessTemplateParameterType(source.templateParameter);
3061          if(type) source = type;
3062       }
3063
3064       if(dest.kind == templateType && source.kind != templateType)
3065       {
3066          Type type = ProcessTemplateParameterType(dest.templateParameter);
3067          if(type) dest = type;
3068       }
3069
3070       if(dest.classObjectType == typedObject && dest.kind != functionType)
3071       {
3072          if(source.classObjectType != anyObject)
3073             return true;
3074          else
3075          {
3076             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
3077             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
3078             {
3079                return true;
3080             }
3081          }
3082       }
3083       else
3084       {
3085          if(source.kind != functionType && source.classObjectType == anyObject)
3086             return true;
3087          if(dest.kind != functionType && dest.classObjectType == anyObject && source.classObjectType != typedObject)
3088             return true;
3089       }
3090
3091       if((dest.kind == structType && source.kind == structType) ||
3092          (dest.kind == unionType && source.kind == unionType))
3093       {
3094          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
3095              (source.members.first && source.members.first == dest.members.first))
3096             return true;
3097       }
3098
3099       if(dest.kind == ellipsisType && source.kind != voidType)
3100          return true;
3101
3102       if(dest.kind == pointerType && dest.type.kind == voidType &&
3103          ((source.kind == classType && (!source._class || !source._class.registered || source._class.registered.type == structClass || source._class.registered.type == normalClass || source._class.registered.type == noHeadClass || source._class.registered.type == systemClass))
3104          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
3105
3106          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
3107
3108          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
3109          return true;
3110       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
3111          ((dest.kind == classType && (!dest._class || !dest._class.registered || dest._class.registered.type == structClass || dest._class.registered.type == normalClass || dest._class.registered.type == noHeadClass || dest._class.registered.type == systemClass))
3112          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
3113          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
3114
3115          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
3116          return true;
3117
3118       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
3119       {
3120          if(source._class.registered && source._class.registered.type == unitClass)
3121          {
3122             if(conversions != null)
3123             {
3124                if(source._class.registered == dest._class.registered)
3125                   return true;
3126             }
3127             else
3128             {
3129                Class sourceBase, destBase;
3130                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
3131                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
3132                if(sourceBase == destBase)
3133                   return true;
3134             }
3135          }
3136          // Don't match enum inheriting from other enum if resolving enumeration values
3137          // TESTING: !dest.classObjectType
3138          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3139             (enumBaseType ||
3140                (!source._class.registered || source._class.registered.type != enumClass) ||
3141                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3142             return true;
3143          else
3144          {
3145             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3146             if(enumBaseType &&
3147                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3148                ((source._class && source._class.registered && source._class.registered.type != enumClass) || source.kind == classType)) // Added this here for a base enum to be acceptable for a derived enum (#139)
3149             {
3150                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3151                {
3152                   return true;
3153                }
3154             }
3155          }
3156       }
3157
3158       // JUST ADDED THIS...
3159       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3160          return true;
3161
3162       if(doConversion)
3163       {
3164          // Just added this for Straight conversion of ColorAlpha => Color
3165          if(source.kind == classType)
3166          {
3167             Class _class;
3168             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3169             {
3170                Property convert;
3171                for(convert = _class.conversions.first; convert; convert = convert.next)
3172                {
3173                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3174                   {
3175                      Conversion after = (conversions != null) ? conversions.last : null;
3176
3177                      if(!convert.dataType)
3178                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3179                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3180                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3181                         MatchTypes(convert.dataType, dest, conversions, null, null,
3182                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3183                               convert.dataType.kind == classType, false, true, warnConst))
3184                      {
3185                         if(!conversions && !convert.Get)
3186                            return true;
3187                         else if(conversions != null)
3188                         {
3189                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3190                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3191                               (dest.kind != classType || dest._class.registered != _class.base))
3192                               return true;
3193                            else
3194                            {
3195                               Conversion conv { convert = convert, isGet = true };
3196                               // conversions.Add(conv);
3197                               conversions.Insert(after, conv);
3198
3199                               return true;
3200                            }
3201                         }
3202                      }
3203                   }
3204                }
3205             }
3206          }
3207
3208          // MOVING THIS??
3209
3210          if(dest.kind == classType)
3211          {
3212             Class _class;
3213             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3214             {
3215                Property convert;
3216                for(convert = _class.conversions.first; convert; convert = convert.next)
3217                {
3218                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3219                   {
3220                      Type constType = null;
3221                      bool success = false;
3222                      // Conversion after = (conversions != null) ? conversions.last : null;
3223
3224                      if(!convert.dataType)
3225                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3226
3227                      if(warnConst && convert.dataType.kind == pointerType && convert.dataType.type && dest.constant)
3228                      {
3229                         Type ptrType { };
3230                         constType = { kind = pointerType, refCount = 1, type = ptrType };
3231                         CopyTypeInto(ptrType, convert.dataType.type);
3232                         ptrType.constant = true;
3233                      }
3234
3235                      // Just added this equality check to prevent recursion.... Make it safer?
3236                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3237                      if((constType || convert.dataType != dest) && MatchTypes(source, constType ? constType : convert.dataType, conversions, null, null, true, false /*true*/, false, true, warnConst))
3238                      {
3239                         if(!conversions && !convert.Set)
3240                            success = true;
3241                         else if(conversions != null)
3242                         {
3243                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3244                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3245                               (source.kind != classType || source._class.registered != _class.base))
3246                               success = true;
3247                            else
3248                            {
3249                               // *** Testing this! ***
3250                               Conversion conv { convert = convert };
3251                               conversions.Add(conv);
3252                               //conversions.Insert(after, conv);
3253                               success = true;
3254                            }
3255                         }
3256                      }
3257                      if(constType)
3258                         FreeType(constType);
3259                      if(success)
3260                         return true;
3261                   }
3262                }
3263             }
3264             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3265             {
3266                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3267                   (source.kind != classType || source._class.registered.type != structClass))
3268                   return true;
3269             }*/
3270
3271             // TESTING THIS... IS THIS OK??
3272             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3273             {
3274                if(!dest._class.registered.dataType)
3275                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3276                // Only support this for classes...
3277                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3278                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3279                {
3280                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false, warnConst))
3281                   {
3282                      return true;
3283                   }
3284                }
3285             }
3286          }
3287
3288          // Moved this lower
3289          if(source.kind == classType)
3290          {
3291             Class _class;
3292             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3293             {
3294                Property convert;
3295                for(convert = _class.conversions.first; convert; convert = convert.next)
3296                {
3297                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3298                   {
3299                      Conversion after = (conversions != null) ? conversions.last : null;
3300
3301                      if(!convert.dataType)
3302                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3303                      if(convert.dataType != source &&
3304                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3305                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true, warnConst))
3306                      {
3307                         if(!conversions && !convert.Get)
3308                            return true;
3309                         else if(conversions != null)
3310                         {
3311                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3312                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3313                               (dest.kind != classType || dest._class.registered != _class.base))
3314                               return true;
3315                            else
3316                            {
3317                               Conversion conv { convert = convert, isGet = true };
3318
3319                               // conversions.Add(conv);
3320                               conversions.Insert(after, conv);
3321                               return true;
3322                            }
3323                         }
3324                      }
3325                   }
3326                }
3327             }
3328
3329             // TESTING THIS... IS THIS OK??
3330             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3331             {
3332                if(!source._class.registered.dataType)
3333                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3334                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3335                {
3336                   if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, source._class.registered.dataType.kind == classType, source._class.registered.dataType.kind == classType, false, false, warnConst))
3337                      return true;
3338                   // For bool to be accepted by byte, short, etc.
3339                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false, warnConst))
3340                      return true;
3341                }
3342             }
3343          }
3344       }
3345
3346       if(source.kind == classType || source.kind == subClassType)
3347          ;
3348       else if(dest.kind == source.kind &&
3349          (dest.kind != structType && dest.kind != unionType &&
3350           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3351           return true;
3352       // RECENTLY ADDED THESE
3353       else if(dest.kind == doubleType && source.kind == floatType)
3354          return true;
3355       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3356          return true;
3357       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3358          return true;
3359       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3360          return true;
3361       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3362          return true;
3363       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3364          return true;
3365       else if(source.kind == enumType &&
3366          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3367           return true;
3368       else if(dest.kind == enumType && !isConversionExploration &&
3369          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3370           return true;
3371       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3372               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3373       {
3374          Type paramSource, paramDest;
3375
3376          if(dest.kind == methodType)
3377             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3378          if(source.kind == methodType)
3379             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3380
3381          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3382          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3383          if(dest.kind == methodType)
3384             dest = dest.method.dataType;
3385          if(source.kind == methodType)
3386             source = source.method.dataType;
3387
3388          paramSource = source.params.first;
3389          if(paramSource && paramSource.kind == voidType) paramSource = null;
3390          paramDest = dest.params.first;
3391          if(paramDest && paramDest.kind == voidType) paramDest = null;
3392
3393
3394          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3395             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3396          {
3397             // Source thisClass must be derived from destination thisClass
3398             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3399                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3400             {
3401                if(paramDest && paramDest.kind == classType)
3402                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3403                else
3404                   Compiler_Error($"method class should not take an object\n");
3405                return false;
3406             }
3407             paramDest = paramDest.next;
3408          }
3409          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3410          {
3411             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3412             {
3413                if(dest.thisClass)
3414                {
3415                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3416                   {
3417                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3418                      return false;
3419                   }
3420                }
3421                else
3422                {
3423                   // THIS WAS BACKWARDS:
3424                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3425                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3426                   {
3427                      if(owningClassDest)
3428                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3429                      else
3430                         Compiler_Error($"overriding class expected to be derived from method class\n");
3431                      return false;
3432                   }
3433                }
3434                paramSource = paramSource.next;
3435             }
3436             else
3437             {
3438                if(dest.thisClass)
3439                {
3440                   // Source thisClass must be derived from destination thisClass
3441                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3442                   {
3443                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3444                      return false;
3445                   }
3446                }
3447                else
3448                {
3449                   // THIS WAS BACKWARDS TOO??
3450                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3451                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3452                   {
3453                      //if(owningClass)
3454                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3455                      //else
3456                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3457                      return false;
3458                   }
3459                }
3460             }
3461          }
3462
3463
3464          // Source return type must be derived from destination return type
3465          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false, warnConst))
3466          {
3467             Compiler_Warning($"incompatible return type for function\n");
3468             return false;
3469          }
3470          // The const check is backwards from the MatchTypes above (for derivative classes checks)
3471          else
3472             CheckConstCompatibility(dest.returnType, source.returnType, true);
3473
3474          // Check parameters
3475
3476          for(; paramDest; paramDest = paramDest.next)
3477          {
3478             if(!paramSource)
3479             {
3480                //Compiler_Warning($"not enough parameters\n");
3481                Compiler_Error($"not enough parameters\n");
3482                return false;
3483             }
3484             {
3485                Type paramDestType = paramDest;
3486                Type paramSourceType = paramSource;
3487                Type type = paramDestType;
3488
3489                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3490                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3491                   paramSource.kind != templateType)
3492                {
3493                   int id = 0;
3494                   ClassTemplateParameter curParam = null;
3495                   Class sClass;
3496                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3497                   {
3498                      id = 0;
3499                      if(sClass.templateClass) sClass = sClass.templateClass;
3500                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3501                      {
3502                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3503                         {
3504                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3505                            {
3506                               if(sClass.templateClass) sClass = sClass.templateClass;
3507                               id += sClass.templateParams.count;
3508                            }
3509                            break;
3510                         }
3511                         id++;
3512                      }
3513                      if(curParam) break;
3514                   }
3515
3516                   if(curParam)
3517                   {
3518                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3519                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3520                   }
3521                }
3522
3523                // paramDest must be derived from paramSource
3524                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false, warnConst) &&
3525                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false, warnConst)))
3526                {
3527                   char type[1024];
3528                   type[0] = 0;
3529                   PrintType(paramDest, type, false, true);
3530                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3531
3532                   if(paramDestType != paramDest)
3533                      FreeType(paramDestType);
3534                   return false;
3535                }
3536                if(paramDestType != paramDest)
3537                   FreeType(paramDestType);
3538             }
3539
3540             paramSource = paramSource.next;
3541          }
3542          if(paramSource)
3543          {
3544             Compiler_Error($"too many parameters\n");
3545             return false;
3546          }
3547          return true;
3548       }
3549       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3550       {
3551          return true;
3552       }
3553       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3554          (source.kind == arrayType || source.kind == pointerType))
3555       {
3556          // Pointers to pointer is incompatible with non normal/nohead classes
3557          if(!(dest.type && dest.type.kind == pointerType && source.type.kind == classType && source.type._class &&
3558             source.type._class.registered && (source.type._class.registered.type != normalClass && source.type._class.registered.type != noHeadClass) && !source.type.byReference))
3559          {
3560             ComputeTypeSize(source.type);
3561             ComputeTypeSize(dest.type);
3562             if(source.type.size == dest.type.size && MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3563                return true;
3564          }
3565       }
3566    }
3567    return false;
3568 }
3569
3570 static void FreeConvert(Conversion convert)
3571 {
3572    if(convert.resultType)
3573       FreeType(convert.resultType);
3574 }
3575
3576 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3577                               char * string, OldList conversions)
3578 {
3579    BTNamedLink link;
3580
3581    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3582    {
3583       Class _class = link.data;
3584       if(_class.type == enumClass)
3585       {
3586          OldList converts { };
3587          Type type { };
3588          type.kind = classType;
3589
3590          if(!_class.symbol)
3591             _class.symbol = FindClass(_class.fullName);
3592          type._class = _class.symbol;
3593
3594          if(MatchTypes(type, dest, &converts, null, null, dest.kind != classType || !dest._class || strcmp(dest._class.string, "bool"),
3595                false, false, false, false))
3596          {
3597             NamedLink64 value;
3598             Class enumClass = eSystem_FindClass(privateModule, "enum");
3599             if(enumClass)
3600             {
3601                Class baseClass;
3602                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3603                {
3604                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3605                   for(value = e.values.first; value; value = value.next)
3606                   {
3607                      if(!strcmp(value.name, string))
3608                         break;
3609                   }
3610                   if(value)
3611                   {
3612                      FreeType(sourceExp.expType);
3613
3614                      sourceExp.isConstant = true;
3615                      sourceExp.expType = MkClassType(baseClass.fullName);
3616                      if(inCompiler || inPreCompiler || inDebugger)
3617                      {
3618                         char constant[256];
3619                         FreeExpContents(sourceExp);
3620
3621                         sourceExp.type = constantExp;
3622                         if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "short") || !strcmp(baseClass.dataTypeString, "char"))
3623                            sprintf(constant, FORMAT64D, value.data);
3624                         else
3625                            sprintf(constant, FORMAT64HEXLL, value.data);
3626                         sourceExp.constant = CopyString(constant);
3627                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3628                      }
3629
3630                      while(converts.first)
3631                      {
3632                         Conversion convert = converts.first;
3633                         converts.Remove(convert);
3634                         conversions.Add(convert);
3635                      }
3636                      delete type;
3637                      return true;
3638                   }
3639                }
3640             }
3641          }
3642          if(converts.first)
3643             converts.Free(FreeConvert);
3644          delete type;
3645       }
3646    }
3647    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3648       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3649          return true;
3650    return false;
3651 }
3652
3653 public bool ModuleVisibility(Module searchIn, Module searchFor)
3654 {
3655    SubModule subModule;
3656
3657    if(searchFor == searchIn)
3658       return true;
3659
3660    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3661    {
3662       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3663       {
3664          if(ModuleVisibility(subModule.module, searchFor))
3665             return true;
3666       }
3667    }
3668    return false;
3669 }
3670
3671 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3672 {
3673    Module module;
3674
3675    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3676       return true;
3677    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3678       return true;
3679    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3680       return true;
3681
3682    for(module = mainModule.application.allModules.first; module; module = module.next)
3683    {
3684       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3685          return true;
3686    }
3687    return false;
3688 }
3689
3690 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3691 {
3692    Type source;
3693    Type realDest = dest;
3694    Type backupSourceExpType = null;
3695    Expression computedExp = sourceExp;
3696    dest.refCount++;
3697
3698    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3699       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3700    {
3701       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3702       ComputeExpression(computedExp /*sourceExp*/);
3703    }
3704
3705    source = sourceExp.expType;
3706
3707    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3708    {
3709       if(computedExp != sourceExp)
3710       {
3711          FreeExpression(computedExp);
3712          computedExp = sourceExp;
3713       }
3714       FreeType(dest);
3715       return true;
3716    }
3717
3718    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3719    {
3720        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3721        {
3722           Class sourceBase, destBase;
3723           for(sourceBase = source._class.registered;
3724               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3725               sourceBase = sourceBase.base);
3726           for(destBase = dest._class.registered;
3727               destBase && destBase.base && destBase.base.type != systemClass;
3728               destBase = destBase.base);
3729           //if(source._class.registered == dest._class.registered)
3730           if(sourceBase == destBase)
3731           {
3732             if(computedExp != sourceExp)
3733             {
3734                FreeExpression(computedExp);
3735                computedExp = sourceExp;
3736             }
3737             FreeType(dest);
3738             return true;
3739          }
3740       }
3741    }
3742
3743    if(source)
3744    {
3745       OldList * specs;
3746       bool flag = false;
3747       int64 value = MAXINT;
3748
3749       source.refCount++;
3750
3751       if(computedExp.type == constantExp)
3752       {
3753          if(source.isSigned)
3754             value = strtoll(computedExp.constant, null, 0);
3755          else
3756             value = strtoull(computedExp.constant, null, 0);
3757       }
3758       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3759       {
3760          if(source.isSigned)
3761             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3762          else
3763             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3764       }
3765       if(computedExp != sourceExp)
3766       {
3767          FreeExpression(computedExp);
3768          computedExp = sourceExp;
3769       }
3770
3771       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3772          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3773       {
3774          FreeType(source);
3775          source = Type { kind = intType, isSigned = false, refCount = 1 };
3776       }
3777
3778       if(dest.kind == classType)
3779       {
3780          Class _class = dest._class ? dest._class.registered : null;
3781
3782          if(_class && _class.type == unitClass)
3783          {
3784             if(source.kind != classType)
3785             {
3786                Type tempType { };
3787                Type tempDest, tempSource;
3788
3789                for(; _class.base.type != systemClass; _class = _class.base);
3790                tempSource = dest;
3791                tempDest = tempType;
3792
3793                tempType.kind = classType;
3794                if(!_class.symbol)
3795                   _class.symbol = FindClass(_class.fullName);
3796
3797                tempType._class = _class.symbol;
3798                tempType.truth = dest.truth;
3799                if(tempType._class)
3800                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3801
3802                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3803                backupSourceExpType = sourceExp.expType;
3804                if(dest.passAsTemplate)
3805                {
3806                   // Don't carry passAsTemplate
3807                   sourceExp.expType = { };
3808                   CopyTypeInto(sourceExp.expType, dest);
3809                   sourceExp.expType.passAsTemplate = false;
3810                }
3811                else
3812                {
3813                   sourceExp.expType = dest;
3814                   dest.refCount++;
3815                }
3816                //sourceExp.expType = MkClassType(_class.fullName);
3817                flag = true;
3818
3819                delete tempType;
3820             }
3821          }
3822
3823
3824          // Why wasn't there something like this?
3825          if(_class && _class.type == bitClass && source.kind != classType)
3826          {
3827             if(!dest._class.registered.dataType)
3828                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3829             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3830             {
3831                FreeType(source);
3832                FreeType(sourceExp.expType);
3833                source = sourceExp.expType = MkClassType(dest._class.string);
3834                source.refCount++;
3835
3836                //source.kind = classType;
3837                //source._class = dest._class;
3838             }
3839          }
3840
3841          // Adding two enumerations
3842          /*
3843          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3844          {
3845             if(!source._class.registered.dataType)
3846                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3847             if(!dest._class.registered.dataType)
3848                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3849
3850             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3851             {
3852                FreeType(source);
3853                source = sourceExp.expType = MkClassType(dest._class.string);
3854                source.refCount++;
3855
3856                //source.kind = classType;
3857                //source._class = dest._class;
3858             }
3859          }*/
3860
3861          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3862          {
3863             OldList * specs = MkList();
3864             Declarator decl;
3865             char string[1024];
3866
3867             ReadString(string, sourceExp.string);
3868             decl = SpecDeclFromString(string, specs, null);
3869
3870             FreeExpContents(sourceExp);
3871             FreeType(sourceExp.expType);
3872
3873             sourceExp.type = classExp;
3874             sourceExp._classExp.specifiers = specs;
3875             sourceExp._classExp.decl = decl;
3876             sourceExp.expType = dest;
3877             dest.refCount++;
3878
3879             FreeType(source);
3880             FreeType(dest);
3881             if(backupSourceExpType) FreeType(backupSourceExpType);
3882             return true;
3883          }
3884       }
3885       else if(source.kind == classType)
3886       {
3887          Class _class = source._class ? source._class.registered : null;
3888
3889          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3890          {
3891             /*
3892             if(dest.kind != classType)
3893             {
3894                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3895                if(!source._class.registered.dataType)
3896                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3897
3898                FreeType(dest);
3899                dest = MkClassType(source._class.string);
3900                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3901                //   dest = MkClassType(source._class.string);
3902             }
3903             */
3904
3905             if(dest.kind != classType)
3906             {
3907                Type tempType { };
3908                Type tempDest, tempSource;
3909
3910                if(!source._class.registered.dataType)
3911                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3912
3913                for(; _class.base.type != systemClass; _class = _class.base);
3914                tempDest = source;
3915                tempSource = tempType;
3916                tempType.kind = classType;
3917                tempType._class = FindClass(_class.fullName);
3918                tempType.truth = source.truth;
3919                tempType.classObjectType = source.classObjectType;
3920
3921                if(tempType._class)
3922                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3923
3924                // PUT THIS BACK TESTING UNITS?
3925                if(conversions && conversions.last)
3926                {
3927                   ((Conversion)(conversions.last)).resultType = dest;
3928                   dest.refCount++;
3929                }
3930
3931                FreeType(sourceExp.expType);
3932                sourceExp.expType = MkClassType(_class.fullName);
3933                sourceExp.expType.truth = source.truth;
3934                sourceExp.expType.classObjectType = source.classObjectType;
3935
3936                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3937
3938                if(!sourceExp.destType)
3939                {
3940                   FreeType(sourceExp.destType);
3941                   sourceExp.destType = sourceExp.expType;
3942                   if(sourceExp.expType)
3943                      sourceExp.expType.refCount++;
3944                }
3945                //flag = true;
3946                //source = _class.dataType;
3947
3948
3949                // TOCHECK: TESTING THIS NEW CODE
3950                if(!_class.dataType)
3951                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3952                FreeType(dest);
3953                dest = MkClassType(source._class.string);
3954                dest.truth = source.truth;
3955                dest.classObjectType = source.classObjectType;
3956
3957                FreeType(source);
3958                source = _class.dataType;
3959                source.refCount++;
3960
3961                delete tempType;
3962             }
3963          }
3964       }
3965
3966       if(!flag)
3967       {
3968          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3969          {
3970             FreeType(source);
3971             FreeType(dest);
3972             return true;
3973          }
3974       }
3975
3976       // Implicit Casts
3977       /*
3978       if(source.kind == classType)
3979       {
3980          Class _class = source._class.registered;
3981          if(_class.type == unitClass)
3982          {
3983             if(!_class.dataType)
3984                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3985             source = _class.dataType;
3986          }
3987       }*/
3988
3989       if(dest.kind == classType)
3990       {
3991          Class _class = dest._class ? dest._class.registered : null;
3992          bool fittingValue = false;
3993          if(_class && _class.type == enumClass)
3994          {
3995             Class enumClass = eSystem_FindClass(privateModule, "enum");
3996             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3997             if(c && value >= 0 && value <= c.largest)
3998                fittingValue = true;
3999          }
4000
4001          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
4002             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
4003          {
4004             if(_class.type == normalClass || _class.type == noHeadClass)
4005             {
4006                Expression newExp { };
4007                *newExp = *sourceExp;
4008                if(sourceExp.destType) sourceExp.destType.refCount++;
4009                if(sourceExp.expType)  sourceExp.expType.refCount++;
4010                sourceExp.type = castExp;
4011                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
4012                sourceExp.cast.exp = newExp;
4013                FreeType(sourceExp.expType);
4014                sourceExp.expType = null;
4015                ProcessExpressionType(sourceExp);
4016
4017                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
4018                if(!inCompiler)
4019                {
4020                   FreeType(sourceExp.expType);
4021                   sourceExp.expType = dest;
4022                }
4023
4024                FreeType(source);
4025                if(inCompiler) FreeType(dest);
4026
4027                if(backupSourceExpType) FreeType(backupSourceExpType);
4028                return true;
4029             }
4030
4031             if(!_class.dataType)
4032                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4033             FreeType(dest);
4034             dest = _class.dataType;
4035             dest.refCount++;
4036          }
4037
4038          // Accept lower precision types for units, since we want to keep the unit type
4039          if(dest.kind == doubleType &&
4040             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
4041              source.kind == charType || source.kind == _BoolType))
4042          {
4043             specs = MkListOne(MkSpecifier(DOUBLE));
4044          }
4045          else if(dest.kind == floatType &&
4046             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
4047             source.kind == _BoolType || source.kind == doubleType))
4048          {
4049             specs = MkListOne(MkSpecifier(FLOAT));
4050          }
4051          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
4052             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4053          {
4054             specs = MkList();
4055             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4056             ListAdd(specs, MkSpecifier(INT64));
4057          }
4058          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
4059             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4060          {
4061             specs = MkList();
4062             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4063             ListAdd(specs, MkSpecifier(INT));
4064          }
4065          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
4066             source.kind == floatType || source.kind == doubleType))
4067          {
4068             specs = MkList();
4069             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4070             ListAdd(specs, MkSpecifier(SHORT));
4071          }
4072          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
4073             source.kind == floatType || source.kind == doubleType))
4074          {
4075             specs = MkList();
4076             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4077             ListAdd(specs, MkSpecifier(CHAR));
4078          }
4079          else
4080          {
4081             FreeType(source);
4082             FreeType(dest);
4083             if(backupSourceExpType)
4084             {
4085                // Failed to convert: revert previous exp type
4086                if(sourceExp.expType) FreeType(sourceExp.expType);
4087                sourceExp.expType = backupSourceExpType;
4088             }
4089             return false;
4090          }
4091       }
4092       else if(dest.kind == doubleType &&
4093          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
4094           source.kind == _BoolType || source.kind == charType))
4095       {
4096          specs = MkListOne(MkSpecifier(DOUBLE));
4097       }
4098       else if(dest.kind == floatType &&
4099          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4100       {
4101          specs = MkListOne(MkSpecifier(FLOAT));
4102       }
4103       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4104          (value == 1 || value == 0))
4105       {
4106          specs = MkList();
4107          ListAdd(specs, MkSpecifier(BOOL));
4108       }
4109       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4110          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
4111       {
4112          specs = MkList();
4113          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4114          ListAdd(specs, MkSpecifier(CHAR));
4115       }
4116       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
4117          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
4118       {
4119          specs = MkList();
4120          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4121          ListAdd(specs, MkSpecifier(SHORT));
4122       }
4123       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
4124       {
4125          specs = MkList();
4126          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4127          ListAdd(specs, MkSpecifier(INT));
4128       }
4129       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
4130       {
4131          specs = MkList();
4132          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4133          ListAdd(specs, MkSpecifier(INT64));
4134       }
4135       else if(dest.kind == enumType &&
4136          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4137       {
4138          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
4139       }
4140       else
4141       {
4142          FreeType(source);
4143          FreeType(dest);
4144          if(backupSourceExpType)
4145          {
4146             // Failed to convert: revert previous exp type
4147             if(sourceExp.expType) FreeType(sourceExp.expType);
4148             sourceExp.expType = backupSourceExpType;
4149          }
4150          return false;
4151       }
4152
4153       if(!flag && !sourceExp.opDestType)
4154       {
4155          Expression newExp { };
4156          *newExp = *sourceExp;
4157          newExp.prev = null;
4158          newExp.next = null;
4159          if(sourceExp.destType) sourceExp.destType.refCount++;
4160          if(sourceExp.expType)  sourceExp.expType.refCount++;
4161
4162          sourceExp.type = castExp;
4163          if(realDest.kind == classType)
4164          {
4165             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
4166             FreeList(specs, FreeSpecifier);
4167          }
4168          else
4169             sourceExp.cast.typeName = MkTypeName(specs, null);
4170          if(newExp.type == opExp)
4171          {
4172             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4173          }
4174          else
4175             sourceExp.cast.exp = newExp;
4176
4177          FreeType(sourceExp.expType);
4178          sourceExp.expType = null;
4179          ProcessExpressionType(sourceExp);
4180       }
4181       else
4182          FreeList(specs, FreeSpecifier);
4183
4184       FreeType(dest);
4185       FreeType(source);
4186       if(backupSourceExpType) FreeType(backupSourceExpType);
4187
4188       return true;
4189    }
4190    else
4191    {
4192       if(computedExp != sourceExp)
4193       {
4194          FreeExpression(computedExp);
4195          computedExp = sourceExp;
4196       }
4197
4198       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4199       if(sourceExp.type == identifierExp)
4200       {
4201          Identifier id = sourceExp.identifier;
4202          if(dest.kind == classType)
4203          {
4204             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4205             {
4206                Class _class = dest._class.registered;
4207                Class enumClass = eSystem_FindClass(privateModule, "enum");
4208                if(enumClass)
4209                {
4210                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4211                   {
4212                      NamedLink64 value;
4213                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4214                      for(value = e.values.first; value; value = value.next)
4215                      {
4216                         if(!strcmp(value.name, id.string))
4217                            break;
4218                      }
4219                      if(value)
4220                      {
4221                         FreeType(sourceExp.expType);
4222
4223                         sourceExp.isConstant = true;
4224                         sourceExp.expType = MkClassType(_class.fullName);
4225                         if(inCompiler || inPreCompiler || inDebugger)
4226                         {
4227                            FreeExpContents(sourceExp);
4228
4229                            sourceExp.type = constantExp;
4230                            if(_class.dataTypeString && (!strcmp(_class.dataTypeString, "int") || !strcmp(_class.dataTypeString, "int64") || !strcmp(_class.dataTypeString, "short") || !strcmp(_class.dataTypeString, "char"))) // _class cannot be null here!
4231                               sourceExp.constant = PrintInt64(value.data);
4232                            else
4233                               sourceExp.constant = PrintUInt64(value.data);
4234                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4235                         }
4236                         FreeType(dest);
4237                         return true;
4238                      }
4239                   }
4240                }
4241             }
4242          }
4243
4244          // Loop through all enum classes
4245          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4246          {
4247             FreeType(dest);
4248             return true;
4249          }
4250       }
4251       FreeType(dest);
4252    }
4253    return false;
4254 }
4255
4256 #define TERTIARY(o, name, m, t, p) \
4257    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4258    {                                                              \
4259       exp.type = constantExp;                                    \
4260       exp.string = p(op1.m ? op2.m : op3.m);                     \
4261       if(!exp.expType) \
4262          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4263       return true;                                                \
4264    }
4265
4266 #define BINARY(o, name, m, t, p) \
4267    static bool name(Expression exp, Operand op1, Operand op2)   \
4268    {                                                              \
4269       t value2 = op2.m;                                           \
4270       exp.type = constantExp;                                    \
4271       exp.string = p((t)(op1.m o value2));                     \
4272       if(!exp.expType) \
4273          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4274       return true;                                                \
4275    }
4276
4277 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4278    static bool name(Expression exp, Operand op1, Operand op2)   \
4279    {                                                              \
4280       t value2 = op2.m;                                           \
4281       exp.type = constantExp;                                    \
4282       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4283       if(!exp.expType) \
4284          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4285       return true;                                                \
4286    }
4287
4288 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4289    static bool name(Expression exp, Operand op1, Operand op2)   \
4290    {                                                              \
4291       t value2 = op2.m;                                           \
4292       exp.type = constantExp;                                    \
4293       exp.string = p(op1.m o value2);             \
4294       if(!exp.expType) \
4295          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4296       return true;                                                \
4297    }
4298
4299 #define UNARY(o, name, m, t, p) \
4300    static bool name(Expression exp, Operand op1)                \
4301    {                                                              \
4302       exp.type = constantExp;                                    \
4303       exp.string = p((t)(o op1.m));                                   \
4304       if(!exp.expType) \
4305          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4306       return true;                                                \
4307    }
4308
4309 #define OPERATOR_ALL(macro, o, name) \
4310    macro(o, Int##name, i, int, PrintInt) \
4311    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4312    macro(o, Int64##name, i64, int64, PrintInt64) \
4313    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4314    macro(o, Short##name, s, short, PrintShort) \
4315    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4316    macro(o, Char##name, c, char, PrintChar) \
4317    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4318    macro(o, Float##name, f, float, PrintFloat) \
4319    macro(o, Double##name, d, double, PrintDouble)
4320
4321 #define OPERATOR_INTTYPES(macro, o, name) \
4322    macro(o, Int##name, i, int, PrintInt) \
4323    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4324    macro(o, Int64##name, i64, int64, PrintInt64) \
4325    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4326    macro(o, Short##name, s, short, PrintShort) \
4327    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4328    macro(o, Char##name, c, char, PrintChar) \
4329    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4330
4331 #define OPERATOR_REALTYPES(macro, o, name) \
4332    macro(o, Float##name, f, float, PrintFloat) \
4333    macro(o, Double##name, d, double, PrintDouble)
4334
4335 // binary arithmetic
4336 OPERATOR_ALL(BINARY, +, Add)
4337 OPERATOR_ALL(BINARY, -, Sub)
4338 OPERATOR_ALL(BINARY, *, Mul)
4339 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4340 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4341 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4342
4343 // unary arithmetic
4344 OPERATOR_ALL(UNARY, -, Neg)
4345
4346 // unary arithmetic increment and decrement
4347 OPERATOR_ALL(UNARY, ++, Inc)
4348 OPERATOR_ALL(UNARY, --, Dec)
4349
4350 // binary arithmetic assignment
4351 OPERATOR_ALL(BINARY, =, Asign)
4352 OPERATOR_ALL(BINARY, +=, AddAsign)
4353 OPERATOR_ALL(BINARY, -=, SubAsign)
4354 OPERATOR_ALL(BINARY, *=, MulAsign)
4355 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4356 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4357 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4358
4359 // binary bitwise
4360 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4361 OPERATOR_INTTYPES(BINARY, |, BitOr)
4362 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4363 OPERATOR_INTTYPES(BINARY, <<, LShift)
4364 OPERATOR_INTTYPES(BINARY, >>, RShift)
4365
4366 // unary bitwise
4367 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4368
4369 // binary bitwise assignment
4370 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4371 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4372 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4373 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4374 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4375
4376 // unary logical negation
4377 OPERATOR_INTTYPES(UNARY, !, Not)
4378
4379 // binary logical equality
4380 OPERATOR_ALL(BINARY, ==, Equ)
4381 OPERATOR_ALL(BINARY, !=, Nqu)
4382
4383 // binary logical
4384 OPERATOR_ALL(BINARY, &&, And)
4385 OPERATOR_ALL(BINARY, ||, Or)
4386
4387 // binary logical relational
4388 OPERATOR_ALL(BINARY, >, Grt)
4389 OPERATOR_ALL(BINARY, <, Sma)
4390 OPERATOR_ALL(BINARY, >=, GrtEqu)
4391 OPERATOR_ALL(BINARY, <=, SmaEqu)
4392
4393 // tertiary condition operator
4394 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4395
4396 //Add, Sub, Mul, Div, Mod,     , Neg,     Inc, Dec,    Asign, AddAsign, SubAsign, MulAsign, DivAsign, ModAsign,     BitAnd, BitOr, BitXor, LShift, RShift, BitNot,     AndAsign, OrAsign, XorAsign, LShiftAsign, RShiftAsign,     Not,     Equ, Nqu,     And, Or,     Grt, Sma, GrtEqu, SmaEqu
4397 #define OPERATOR_TABLE_ALL(name, type) \
4398     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4399                           type##Neg, \
4400                           type##Inc, type##Dec, \
4401                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4402                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4403                           type##BitNot, \
4404                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4405                           type##Not, \
4406                           type##Equ, type##Nqu, \
4407                           type##And, type##Or, \
4408                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4409                         }; \
4410
4411 #define OPERATOR_TABLE_INTTYPES(name, type) \
4412     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4413                           type##Neg, \
4414                           type##Inc, type##Dec, \
4415                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4416                           null, null, null, null, null, \
4417                           null, \
4418                           null, null, null, null, null, \
4419                           null, \
4420                           type##Equ, type##Nqu, \
4421                           type##And, type##Or, \
4422                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4423                         }; \
4424
4425 OPERATOR_TABLE_ALL(int, Int)
4426 OPERATOR_TABLE_ALL(uint, UInt)
4427 OPERATOR_TABLE_ALL(int64, Int64)
4428 OPERATOR_TABLE_ALL(uint64, UInt64)
4429 OPERATOR_TABLE_ALL(short, Short)
4430 OPERATOR_TABLE_ALL(ushort, UShort)
4431 OPERATOR_TABLE_INTTYPES(float, Float)
4432 OPERATOR_TABLE_INTTYPES(double, Double)
4433 OPERATOR_TABLE_ALL(char, Char)
4434 OPERATOR_TABLE_ALL(uchar, UChar)
4435
4436 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4437 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4438 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4439 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4440 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4441 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4442 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4443 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4444
4445 public void ReadString(char * output,  char * string)
4446 {
4447    int len = strlen(string);
4448    int c,d = 0;
4449    bool quoted = false, escaped = false;
4450    for(c = 0; c<len; c++)
4451    {
4452       char ch = string[c];
4453       if(escaped)
4454       {
4455          switch(ch)
4456          {
4457             case 'n': output[d] = '\n'; break;
4458             case 't': output[d] = '\t'; break;
4459             case 'a': output[d] = '\a'; break;
4460             case 'b': output[d] = '\b'; break;
4461             case 'f': output[d] = '\f'; break;
4462             case 'r': output[d] = '\r'; break;
4463             case 'v': output[d] = '\v'; break;
4464             case '\\': output[d] = '\\'; break;
4465             case '\"': output[d] = '\"'; break;
4466             case '\'': output[d] = '\''; break;
4467             default: output[d] = ch;
4468          }
4469          d++;
4470          escaped = false;
4471       }
4472       else
4473       {
4474          if(ch == '\"')
4475             quoted ^= true;
4476          else if(quoted)
4477          {
4478             if(ch == '\\')
4479                escaped = true;
4480             else
4481                output[d++] = ch;
4482          }
4483       }
4484    }
4485    output[d] = '\0';
4486 }
4487
4488 // String Unescape Copy
4489
4490 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4491 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4492 public int UnescapeString(char * d, char * s, int len)
4493 {
4494    int j = 0, k = 0;
4495    char ch;
4496    while(j < len && (ch = s[j]))
4497    {
4498       switch(ch)
4499       {
4500          case '\\':
4501             switch((ch = s[++j]))
4502             {
4503                case 'n': d[k] = '\n'; break;
4504                case 't': d[k] = '\t'; break;
4505                case 'a': d[k] = '\a'; break;
4506                case 'b': d[k] = '\b'; break;
4507                case 'f': d[k] = '\f'; break;
4508                case 'r': d[k] = '\r'; break;
4509                case 'v': d[k] = '\v'; break;
4510                case '\\': d[k] = '\\'; break;
4511                case '\"': d[k] = '\"'; break;
4512                case '\'': d[k] = '\''; break;
4513                default: d[k] = '\\'; d[k] = ch;
4514             }
4515             break;
4516          default:
4517             d[k] = ch;
4518       }
4519       j++, k++;
4520    }
4521    d[k] = '\0';
4522    return k;
4523 }
4524
4525 public char * OffsetEscapedString(char * s, int len, int offset)
4526 {
4527    char ch;
4528    int j = 0, k = 0;
4529    while(j < len && k < offset && (ch = s[j]))
4530    {
4531       if(ch == '\\') ++j;
4532       j++, k++;
4533    }
4534    return (k == offset) ? s + j : null;
4535 }
4536
4537 public Operand GetOperand(Expression exp)
4538 {
4539    Operand op { };
4540    Type type = exp.expType;
4541    if(type)
4542    {
4543       while(type.kind == classType &&
4544          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4545       {
4546          if(!type._class.registered.dataType)
4547             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4548          type = type._class.registered.dataType;
4549
4550       }
4551       if(exp.type == stringExp && op.kind == pointerType)
4552       {
4553          op.ui64 = (uint64)(uintptr)exp.string;
4554          op.kind = pointerType;
4555          op.ops = uint64Ops;
4556       }
4557       else if(exp.isConstant && exp.type == constantExp)
4558       {
4559          op.kind = type.kind;
4560          op.type = type;
4561
4562          switch(op.kind)
4563          {
4564             case _BoolType:
4565             case charType:
4566             {
4567                if(exp.constant[0] == '\'')
4568                {
4569                   op.c = exp.constant[1];
4570                   op.ops = charOps;
4571                }
4572                else if(type.isSigned)
4573                {
4574                   op.c = (char)strtol(exp.constant, null, 0);
4575                   op.ops = charOps;
4576                }
4577                else
4578                {
4579                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4580                   op.ops = ucharOps;
4581                }
4582                break;
4583             }
4584             case shortType:
4585                if(type.isSigned)
4586                {
4587                   op.s = (short)strtol(exp.constant, null, 0);
4588                   op.ops = shortOps;
4589                }
4590                else
4591                {
4592                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4593                   op.ops = ushortOps;
4594                }
4595                break;
4596             case intType:
4597             case longType:
4598                if(type.isSigned)
4599                {
4600                   op.i = (int)strtol(exp.constant, null, 0);
4601                   op.ops = intOps;
4602                }
4603                else
4604                {
4605                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4606                   op.ops = uintOps;
4607                }
4608                op.kind = intType;
4609                break;
4610             case int64Type:
4611                if(type.isSigned)
4612                {
4613                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4614                   op.ops = int64Ops;
4615                }
4616                else
4617                {
4618                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4619                   op.ops = uint64Ops;
4620                }
4621                op.kind = int64Type;
4622                break;
4623             case intPtrType:
4624                if(type.isSigned)
4625                {
4626                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4627                   op.ops = int64Ops;
4628                }
4629                else
4630                {
4631                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4632                   op.ops = uint64Ops;
4633                }
4634                op.kind = int64Type;
4635                break;
4636             case intSizeType:
4637                if(type.isSigned)
4638                {
4639                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4640                   op.ops = int64Ops;
4641                }
4642                else
4643                {
4644                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4645                   op.ops = uint64Ops;
4646                }
4647                op.kind = int64Type;
4648                break;
4649             case floatType:
4650                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4651                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4652                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4653                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4654                else
4655                   op.f = (float)strtod(exp.constant, null);
4656                op.ops = floatOps;
4657                break;
4658             case doubleType:
4659                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4660                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4661                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4662                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4663                else
4664                   op.d = (double)strtod(exp.constant, null);
4665                op.ops = doubleOps;
4666                break;
4667             //case classType:    For when we have operator overloading...
4668             // Pointer additions
4669             //case functionType:
4670             case arrayType:
4671             case pointerType:
4672             case classType:
4673                op.ui64 = _strtoui64(exp.constant, null, 0);
4674                op.kind = pointerType;
4675                op.ops = uint64Ops;
4676                // op.ptrSize =
4677                break;
4678          }
4679       }
4680    }
4681    return op;
4682 }
4683
4684 static int64 GetEnumValue(Class _class, void * ptr)
4685 {
4686    int64 v = 0;
4687    switch(_class.typeSize)
4688    {
4689       case 8:
4690          if(!strcmp(_class.dataTypeString, "uint64"))
4691             v = (int64)*(uint64 *)ptr;
4692          else
4693             v = (int64)*(int64 *)ptr;
4694          break;
4695       case 4:
4696          if(!strcmp(_class.dataTypeString, "uint"))
4697             v = (int64)*(uint *)ptr;
4698          else
4699             v = (int64)*(int *)ptr;
4700          break;
4701       case 2:
4702          if(!strcmp(_class.dataTypeString, "uint16"))
4703             v = (int64)*(uint16 *)ptr;
4704          else
4705             v = (int64)*(short *)ptr;
4706          break;
4707       case 1:
4708          if(!strcmp(_class.dataTypeString, "byte"))
4709             v = (int64)*(byte *)ptr;
4710          else
4711             v = (int64)*(char *)ptr;
4712          break;
4713    }
4714    return v;
4715 }
4716
4717 static __attribute__((unused)) void UnusedFunction()
4718 {
4719    int a;
4720    a.OnGetString(0,0,0);
4721 }
4722 default:
4723 extern int __ecereVMethodID_class_OnGetString;
4724 public:
4725
4726 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4727 {
4728    DataMember dataMember;
4729    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4730    {
4731       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4732          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4733       else
4734       {
4735          Expression exp { };
4736          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4737          Type type;
4738          void * ptr = inst.data + dataMember.offset + offset;
4739          char * result = null;
4740          exp.loc = member.loc = inst.loc;
4741          ((Identifier)member.identifiers->first).loc = inst.loc;
4742
4743          if(!dataMember.dataType)
4744             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4745          type = dataMember.dataType;
4746          if(type.kind == classType)
4747          {
4748             Class _class = type._class.registered;
4749             if(_class.type == enumClass)
4750             {
4751                Class enumClass = eSystem_FindClass(privateModule, "enum");
4752                if(enumClass)
4753                {
4754                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4755                   NamedLink64 item;
4756                   for(item = e.values.first; item; item = item.next)
4757                   {
4758                      if(item.data == GetEnumValue(_class, ptr))
4759                      {
4760                         result = item.name;
4761                         break;
4762                      }
4763                   }
4764                   if(result)
4765                   {
4766                      exp.identifier = MkIdentifier(result);
4767                      exp.type = identifierExp;
4768                      exp.destType = MkClassType(_class.fullName);
4769                      ProcessExpressionType(exp);
4770                   }
4771                }
4772             }
4773             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4774             {
4775                if(!_class.dataType)
4776                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4777                type = _class.dataType;
4778             }
4779          }
4780          if(!result)
4781          {
4782             switch(type.kind)
4783             {
4784                case floatType:
4785                {
4786                   FreeExpContents(exp);
4787
4788                   exp.constant = PrintFloat(*(float*)ptr);
4789                   exp.type = constantExp;
4790                   break;
4791                }
4792                case doubleType:
4793                {
4794                   FreeExpContents(exp);
4795
4796                   exp.constant = PrintDouble(*(double*)ptr);
4797                   exp.type = constantExp;
4798                   break;
4799                }
4800                case intType:
4801                {
4802                   FreeExpContents(exp);
4803
4804                   exp.constant = PrintInt(*(int*)ptr);
4805                   exp.type = constantExp;
4806                   break;
4807                }
4808                case int64Type:
4809                {
4810                   FreeExpContents(exp);
4811
4812                   exp.constant = PrintInt64(*(int64*)ptr);
4813                   exp.type = constantExp;
4814                   break;
4815                }
4816                case intPtrType:
4817                {
4818                   FreeExpContents(exp);
4819                   // TODO: This should probably use proper type
4820                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4821                   exp.type = constantExp;
4822                   break;
4823                }
4824                case intSizeType:
4825                {
4826                   FreeExpContents(exp);
4827                   // TODO: This should probably use proper type
4828                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4829                   exp.type = constantExp;
4830                   break;
4831                }
4832                default:
4833                   Compiler_Error($"Unhandled type populating instance\n");
4834             }
4835          }
4836          ListAdd(memberList, member);
4837       }
4838
4839       if(parentDataMember.type == unionMember)
4840          break;
4841    }
4842 }
4843
4844 void PopulateInstance(Instantiation inst)
4845 {
4846    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4847    Class _class = classSym.registered;
4848    DataMember dataMember;
4849    OldList * memberList = MkList();
4850    // Added this check and ->Add to prevent memory leaks on bad code
4851    if(!inst.members)
4852       inst.members = MkListOne(MkMembersInitList(memberList));
4853    else
4854       inst.members->Add(MkMembersInitList(memberList));
4855    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4856    {
4857       if(!dataMember.isProperty)
4858       {
4859          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4860             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4861          else
4862          {
4863             Expression exp { };
4864             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4865             Type type;
4866             void * ptr = inst.data + dataMember.offset;
4867             char * result = null;
4868
4869             exp.loc = member.loc = inst.loc;
4870             ((Identifier)member.identifiers->first).loc = inst.loc;
4871
4872             if(!dataMember.dataType)
4873                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4874             type = dataMember.dataType;
4875             if(type.kind == classType)
4876             {
4877                Class _class = type._class.registered;
4878                if(_class.type == enumClass)
4879                {
4880                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4881                   if(enumClass)
4882                   {
4883                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4884                      NamedLink64 item;
4885                      for(item = e.values.first; item; item = item.next)
4886                      {
4887                         if(item.data == GetEnumValue(_class, ptr))
4888                         {
4889                            result = item.name;
4890                            break;
4891                         }
4892                      }
4893                   }
4894                   if(result)
4895                   {
4896                      exp.identifier = MkIdentifier(result);
4897                      exp.type = identifierExp;
4898                      exp.destType = MkClassType(_class.fullName);
4899                      ProcessExpressionType(exp);
4900                   }
4901                }
4902                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4903                {
4904                   if(!_class.dataType)
4905                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4906                   type = _class.dataType;
4907                }
4908             }
4909             if(!result)
4910             {
4911                switch(type.kind)
4912                {
4913                   case floatType:
4914                   {
4915                      exp.constant = PrintFloat(*(float*)ptr);
4916                      exp.type = constantExp;
4917                      break;
4918                   }
4919                   case doubleType:
4920                   {
4921                      exp.constant = PrintDouble(*(double*)ptr);
4922                      exp.type = constantExp;
4923                      break;
4924                   }
4925                   case intType:
4926                   {
4927                      exp.constant = PrintInt(*(int*)ptr);
4928                      exp.type = constantExp;
4929                      break;
4930                   }
4931                   case int64Type:
4932                   {
4933                      exp.constant = PrintInt64(*(int64*)ptr);
4934                      exp.type = constantExp;
4935                      break;
4936                   }
4937                   case intPtrType:
4938                   {
4939                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4940                      exp.type = constantExp;
4941                      break;
4942                   }
4943                   default:
4944                      Compiler_Error($"Unhandled type populating instance\n");
4945                }
4946             }
4947             ListAdd(memberList, member);
4948          }
4949       }
4950    }
4951 }
4952
4953 void ComputeInstantiation(Expression exp)
4954 {
4955    Instantiation inst = exp.instance;
4956    MembersInit members;
4957    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4958    Class _class = classSym ? classSym.registered : null;
4959    DataMember curMember = null;
4960    Class curClass = null;
4961    DataMember subMemberStack[256];
4962    int subMemberStackPos = 0;
4963    uint64 bits = 0;
4964
4965    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4966    {
4967       // Don't recompute the instantiation...
4968       // Non Simple classes will have become constants by now
4969       if(inst.data)
4970          return;
4971
4972       if(_class.type == normalClass || _class.type == noHeadClass)
4973       {
4974          inst.data = (byte *)eInstance_New(_class);
4975          if(_class.type == normalClass)
4976             ((Instance)inst.data)._refCount++;
4977       }
4978       else
4979          inst.data = new0 byte[_class.structSize];
4980    }
4981
4982    if(inst.members)
4983    {
4984       for(members = inst.members->first; members; members = members.next)
4985       {
4986          switch(members.type)
4987          {
4988             case dataMembersInit:
4989             {
4990                if(members.dataMembers)
4991                {
4992                   MemberInit member;
4993                   for(member = members.dataMembers->first; member; member = member.next)
4994                   {
4995                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4996                      bool found = false;
4997
4998                      Property prop = null;
4999                      DataMember dataMember = null;
5000                      uint dataMemberOffset;
5001
5002                      if(!ident)
5003                      {
5004                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
5005                         if(curMember)
5006                         {
5007                            if(curMember.isProperty)
5008                               prop = (Property)curMember;
5009                            else
5010                            {
5011                               dataMember = curMember;
5012
5013                               // CHANGED THIS HERE
5014                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
5015
5016                               // 2013/17/29 -- It seems that this was missing here!
5017                               if(_class.type == normalClass)
5018                                  dataMemberOffset += _class.base.structSize;
5019                               // dataMemberOffset = dataMember.offset;
5020                            }
5021                            found = true;
5022                         }
5023                      }
5024                      else
5025                      {
5026                         prop = eClass_FindProperty(_class, ident.string, privateModule);
5027                         if(prop)
5028                         {
5029                            found = true;
5030                            if(prop.memberAccess == publicAccess)
5031                            {
5032                               curMember = (DataMember)prop;
5033                               curClass = prop._class;
5034                            }
5035                         }
5036                         else
5037                         {
5038                            DataMember _subMemberStack[256];
5039                            int _subMemberStackPos = 0;
5040
5041                            // FILL MEMBER STACK
5042                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
5043
5044                            if(dataMember)
5045                            {
5046                               found = true;
5047                               if(dataMember.memberAccess == publicAccess)
5048                               {
5049                                  curMember = dataMember;
5050                                  curClass = dataMember._class;
5051                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
5052                                  subMemberStackPos = _subMemberStackPos;
5053                               }
5054                            }
5055                         }
5056                      }
5057
5058                      if(found && member.initializer && member.initializer.type == expInitializer)
5059                      {
5060                         Expression value = member.initializer.exp;
5061                         Type type = null;
5062                         bool deepMember = false;
5063                         if(prop)
5064                         {
5065                            type = prop.dataType;
5066                         }
5067                         else if(dataMember)
5068                         {
5069                            if(!dataMember.dataType)
5070                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
5071
5072                            type = dataMember.dataType;
5073                         }
5074
5075                         if(ident && ident.next)
5076                         {
5077                            deepMember = true;
5078
5079                            // for(; ident && type; ident = ident.next)
5080                            for(ident = ident.next; ident && type; ident = ident.next)
5081                            {
5082                               if(type.kind == classType)
5083                               {
5084                                  prop = eClass_FindProperty(type._class.registered,
5085                                     ident.string, privateModule);
5086                                  if(prop)
5087                                     type = prop.dataType;
5088                                  else
5089                                  {
5090                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
5091                                        ident.string, &dataMemberOffset, privateModule, null, null);
5092                                     if(dataMember)
5093                                        type = dataMember.dataType;
5094                                  }
5095                               }
5096                               else if(type.kind == structType || type.kind == unionType)
5097                               {
5098                                  Type memberType;
5099                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
5100                                  {
5101                                     if(!strcmp(memberType.name, ident.string))
5102                                     {
5103                                        type = memberType;
5104                                        break;
5105                                     }
5106                                  }
5107                               }
5108                            }
5109                         }
5110                         if(value)
5111                         {
5112                            FreeType(value.destType);
5113                            value.destType = type;
5114                            if(type) type.refCount++;
5115                            ComputeExpression(value);
5116                         }
5117                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
5118                         {
5119                            if(type.kind == classType)
5120                            {
5121                               Class _class = type._class.registered;
5122                               if(_class && (_class.type == bitClass || _class.type == unitClass || _class.type == enumClass))
5123                               {
5124                                  if(!_class.dataType)
5125                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5126                                  type = _class.dataType;
5127                               }
5128                            }
5129
5130                            if(dataMember)
5131                            {
5132                               void * ptr = inst.data + dataMemberOffset;
5133
5134                               if(value.type == constantExp)
5135                               {
5136                                  switch(type.kind)
5137                                  {
5138                                     case intType:
5139                                     {
5140                                        GetInt(value, (int*)ptr);
5141                                        break;
5142                                     }
5143                                     case int64Type:
5144                                     {
5145                                        GetInt64(value, (int64*)ptr);
5146                                        break;
5147                                     }
5148                                     case intPtrType:
5149                                     {
5150                                        GetIntPtr(value, (intptr*)ptr);
5151                                        break;
5152                                     }
5153                                     case intSizeType:
5154                                     {
5155                                        GetIntSize(value, (intsize*)ptr);
5156                                        break;
5157                                     }
5158                                     case floatType:
5159                                     {
5160                                        GetFloat(value, (float*)ptr);
5161                                        break;
5162                                     }
5163                                     case doubleType:
5164                                     {
5165                                        GetDouble(value, (double *)ptr);
5166                                        break;
5167                                     }
5168                                  }
5169                               }
5170                               else if(value.type == instanceExp)
5171                               {
5172                                  if(type.kind == classType)
5173                                  {
5174                                     Class _class = type._class.registered;
5175                                     if(_class.type == structClass)
5176                                     {
5177                                        ComputeTypeSize(type);
5178                                        if(value.instance.data)
5179                                           memcpy(ptr, value.instance.data, type.size);
5180                                     }
5181                                  }
5182                               }
5183                            }
5184                            else if(prop && prop.Set != (void *)(intptr)1)
5185                            {
5186                               if(value.type == instanceExp && value.instance.data)
5187                               {
5188                                  if(type.kind == classType)
5189                                  {
5190                                     Class _class = type._class.registered;
5191                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5192                                     {
5193                                        void (*Set)(void *, void *) = (void *)prop.Set;
5194                                        Set(inst.data, value.instance.data);
5195                                        PopulateInstance(inst);
5196                                     }
5197                                  }
5198                               }
5199                               else if(value.type == constantExp)
5200                               {
5201                                  switch(type.kind)
5202                                  {
5203                                     case doubleType:
5204                                     {
5205                                        void (*Set)(void *, double) = (void *)prop.Set;
5206                                        Set(inst.data, strtod(value.constant, null) );
5207                                        break;
5208                                     }
5209                                     case floatType:
5210                                     {
5211                                        void (*Set)(void *, float) = (void *)prop.Set;
5212                                        Set(inst.data, (float)(strtod(value.constant, null)));
5213                                        break;
5214                                     }
5215                                     case intType:
5216                                     {
5217                                        void (*Set)(void *, int) = (void *)prop.Set;
5218                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5219                                        break;
5220                                     }
5221                                     case int64Type:
5222                                     {
5223                                        void (*Set)(void *, int64) = (void *)prop.Set;
5224                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5225                                        break;
5226                                     }
5227                                     case intPtrType:
5228                                     {
5229                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5230                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5231                                        break;
5232                                     }
5233                                     case intSizeType:
5234                                     {
5235                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5236                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5237                                        break;
5238                                     }
5239                                  }
5240                               }
5241                               else if(value.type == stringExp)
5242                               {
5243                                  char temp[1024];
5244                                  ReadString(temp, value.string);
5245                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5246                               }
5247                            }
5248                         }
5249                         else if(!deepMember && type && _class.type == unitClass)
5250                         {
5251                            if(prop)
5252                            {
5253                               // Only support converting units to units for now...
5254                               if(value.type == constantExp)
5255                               {
5256                                  if(type.kind == classType)
5257                                  {
5258                                     Class _class = type._class.registered;
5259                                     if(_class.type == unitClass)
5260                                     {
5261                                        if(!_class.dataType)
5262                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5263                                        type = _class.dataType;
5264                                     }
5265                                  }
5266                                  // TODO: Assuming same base type for units...
5267                                  switch(type.kind)
5268                                  {
5269                                     case floatType:
5270                                     {
5271                                        float fValue;
5272                                        float (*Set)(float) = (void *)prop.Set;
5273                                        GetFloat(member.initializer.exp, &fValue);
5274                                        exp.constant = PrintFloat(Set(fValue));
5275                                        exp.type = constantExp;
5276                                        break;
5277                                     }
5278                                     case doubleType:
5279                                     {
5280                                        double dValue;
5281                                        double (*Set)(double) = (void *)prop.Set;
5282                                        GetDouble(member.initializer.exp, &dValue);
5283                                        exp.constant = PrintDouble(Set(dValue));
5284                                        exp.type = constantExp;
5285                                        break;
5286                                     }
5287                                  }
5288                               }
5289                            }
5290                         }
5291                         else if(!deepMember && type && _class.type == bitClass)
5292                         {
5293                            if(prop)
5294                            {
5295                               if(value.type == instanceExp && value.instance.data)
5296                               {
5297                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5298                                  bits = Set(value.instance.data);
5299                               }
5300                               else if(value.type == constantExp)
5301                               {
5302                               }
5303                            }
5304                            else if(dataMember)
5305                            {
5306                               BitMember bitMember = (BitMember) dataMember;
5307                               Type type;
5308                               uint64 part = 0;
5309                               bits = (bits & ~bitMember.mask);
5310                               if(!bitMember.dataType)
5311                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5312                               type = bitMember.dataType;
5313                               if(type.kind == classType && type._class && type._class.registered)
5314                               {
5315                                  if(!type._class.registered.dataType)
5316                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5317                                  type = type._class.registered.dataType;
5318                               }
5319                               switch(type.kind)
5320                               {
5321                                  case _BoolType:
5322                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5323                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (short *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5324                                  case intType:
5325                                  case longType:       { uint v; type.isSigned ? GetInt(value, (int *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5326                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (int64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5327                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (intptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5328                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (intsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5329                               }
5330                               bits |= part << bitMember.pos;
5331                            }
5332                         }
5333                      }
5334                      else
5335                      {
5336                         if(_class && _class.type == unitClass)
5337                         {
5338                            ComputeExpression(member.initializer.exp);
5339                            exp.constant = member.initializer.exp.constant;
5340                            exp.type = constantExp;
5341
5342                            member.initializer.exp.constant = null;
5343                         }
5344                      }
5345                   }
5346                }
5347                break;
5348             }
5349          }
5350       }
5351    }
5352    if(_class && _class.type == bitClass)
5353    {
5354       exp.constant = PrintHexUInt(bits);
5355       exp.type = constantExp;
5356    }
5357    if(exp.type != instanceExp)
5358    {
5359       FreeInstance(inst);
5360    }
5361 }
5362
5363 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5364 {
5365    bool result = false;
5366    switch(kind)
5367    {
5368       case shortType:
5369          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5370             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5371          break;
5372       case intType:
5373       case longType:
5374          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5375             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5376          break;
5377       case int64Type:
5378          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5379             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5380             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5381          break;
5382       case floatType:
5383          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5384             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5385             result = GetOpFloat(op, &op.f);
5386          break;
5387       case doubleType:
5388          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5389             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5390             result = GetOpDouble(op, &op.d);
5391          break;
5392       case pointerType:
5393          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5394             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5395             result = GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5396          break;
5397       case enumType:
5398          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5399             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5400             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5401          break;
5402       case intPtrType:
5403          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5404             result = isSigned ? GetOpInt64 /*GetOpIntPtr*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5405          break;
5406       case intSizeType:
5407          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5408             result = isSigned ? GetOpInt64 /*GetOpIntSize*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntSize*/(op, &op.ui64);
5409          break;
5410    }
5411    return result;
5412 }
5413
5414 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5415 {
5416    if(exp.op.op == SIZEOF)
5417    {
5418       FreeExpContents(exp);
5419       exp.type = constantExp;
5420       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5421    }
5422    else
5423    {
5424       if(!exp.op.exp1)
5425       {
5426          switch(exp.op.op)
5427          {
5428             // unary arithmetic
5429             case '+':
5430             {
5431                // Provide default unary +
5432                Expression exp2 = exp.op.exp2;
5433                exp.op.exp2 = null;
5434                FreeExpContents(exp);
5435                FreeType(exp.expType);
5436                FreeType(exp.destType);
5437                *exp = *exp2;
5438                delete exp2;
5439                break;
5440             }
5441             case '-':
5442                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5443                break;
5444             // unary arithmetic increment and decrement
5445                   //OPERATOR_ALL(UNARY, ++, Inc)
5446                   //OPERATOR_ALL(UNARY, --, Dec)
5447             // unary bitwise
5448             case '~':
5449                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5450                break;
5451             // unary logical negation
5452             case '!':
5453                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5454                break;
5455          }
5456       }
5457       else
5458       {
5459          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5460          {
5461             if(Promote(op2, op1.kind, op1.type.isSigned))
5462                op2.kind = op1.kind, op2.ops = op1.ops;
5463             else if(Promote(op1, op2.kind, op2.type.isSigned))
5464                op1.kind = op2.kind, op1.ops = op2.ops;
5465          }
5466          switch(exp.op.op)
5467          {
5468             // binary arithmetic
5469             case '+':
5470                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5471                break;
5472             case '-':
5473                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5474                break;
5475             case '*':
5476                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5477                break;
5478             case '/':
5479                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5480                break;
5481             case '%':
5482                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5483                break;
5484             // binary arithmetic assignment
5485                   //OPERATOR_ALL(BINARY, =, Asign)
5486                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5487                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5488                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5489                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5490                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5491             // binary bitwise
5492             case '&':
5493                if(exp.op.exp2)
5494                {
5495                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5496                }
5497                break;
5498             case '|':
5499                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5500                break;
5501             case '^':
5502                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5503                break;
5504             case LEFT_OP:
5505                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5506                break;
5507             case RIGHT_OP:
5508                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5509                break;
5510             // binary bitwise assignment
5511                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5512                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5513                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5514                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5515                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5516             // binary logical equality
5517             case EQ_OP:
5518                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5519                break;
5520             case NE_OP:
5521                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5522                break;
5523             // binary logical
5524             case AND_OP:
5525                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5526                break;
5527             case OR_OP:
5528                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5529                break;
5530             // binary logical relational
5531             case '>':
5532                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5533                break;
5534             case '<':
5535                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5536                break;
5537             case GE_OP:
5538                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5539                break;
5540             case LE_OP:
5541                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5542                break;
5543          }
5544       }
5545    }
5546 }
5547
5548 void ComputeExpression(Expression exp)
5549 {
5550 #ifdef _DEBUG
5551    char expString[10240];
5552    expString[0] = '\0';
5553    PrintExpression(exp, expString);
5554 #endif
5555
5556    switch(exp.type)
5557    {
5558       case identifierExp:
5559       {
5560          Identifier id = exp.identifier;
5561          if(id && exp.isConstant && !inCompiler && !inPreCompiler && !inDebugger)
5562          {
5563             Class c = (exp.expType && exp.expType.kind == classType && exp.expType._class) ? exp.expType._class.registered : null;
5564             if(c && c.type == enumClass)
5565             {
5566                Class enumClass = eSystem_FindClass(privateModule, "enum");
5567                if(enumClass)
5568                {
5569                   NamedLink64 value;
5570                   EnumClassData e = ACCESS_CLASSDATA(c, enumClass);
5571                   for(value = e.values.first; value; value = value.next)
5572                   {
5573                      if(!strcmp(value.name, id.string))
5574                         break;
5575                   }
5576                   if(value)
5577                   {
5578                      const String dts = c.dataTypeString;
5579                      FreeExpContents(exp);
5580                      exp.type = constantExp;
5581                      exp.constant = (dts && (!strcmp(dts, "int") || !strcmp(dts, "int64") || !strcmp(dts, "short") || !strcmp(dts, "char"))) ? PrintInt64(value.data) : PrintUInt64(value.data);
5582                   }
5583                }
5584             }
5585          }
5586          break;
5587       }
5588       case instanceExp:
5589       {
5590          ComputeInstantiation(exp);
5591          break;
5592       }
5593       /*
5594       case constantExp:
5595          break;
5596       */
5597       case opExp:
5598       {
5599          Expression exp1, exp2 = null;
5600          Operand op1 { };
5601          Operand op2 { };
5602
5603          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5604          if(exp.op.exp2)
5605          {
5606             Expression e = exp.op.exp2;
5607
5608             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5609             {
5610                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5611                {
5612                   if(e.type == extensionCompoundExp)
5613                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5614                   else
5615                      e = e.list->last;
5616                }
5617             }
5618             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5619             {
5620                if(e.type == stringExp && e.string)
5621                {
5622                   char * string = e.string;
5623                   int len = strlen(string);
5624                   char * tmp = new char[len-2+1];
5625                   len = UnescapeString(tmp, string + 1, len - 2);
5626                   delete tmp;
5627                   FreeExpContents(exp);
5628                   exp.type = constantExp;
5629                   exp.constant = PrintUInt(len + 1);
5630                }
5631                else
5632                {
5633                   Type type = e.expType;
5634                   type.refCount++;
5635                   FreeExpContents(exp);
5636                   exp.type = constantExp;
5637                   exp.constant = PrintUInt(ComputeTypeSize(type));
5638                   FreeType(type);
5639                }
5640                break;
5641             }
5642             else
5643                ComputeExpression(exp.op.exp2);
5644          }
5645          if(exp.op.exp1)
5646          {
5647             ComputeExpression(exp.op.exp1);
5648             exp1 = exp.op.exp1;
5649             exp2 = exp.op.exp2;
5650             op1 = GetOperand(exp1);
5651             if(op1.type) op1.type.refCount++;
5652             if(exp2)
5653             {
5654                op2 = GetOperand(exp2);
5655                if(op2.type) op2.type.refCount++;
5656             }
5657          }
5658          else
5659          {
5660             exp1 = exp.op.exp2;
5661             op1 = GetOperand(exp1);
5662             if(op1.type) op1.type.refCount++;
5663          }
5664
5665          CallOperator(exp, exp1, exp2, op1, op2);
5666          /*
5667          switch(exp.op.op)
5668          {
5669             // Unary operators
5670             case '&':
5671                // Also binary
5672                if(exp.op.exp1 && exp.op.exp2)
5673                {
5674                   // Binary And
5675                   if(op1.ops.BitAnd)
5676                   {
5677                      FreeExpContents(exp);
5678                      op1.ops.BitAnd(exp, op1, op2);
5679                   }
5680                }
5681                break;
5682             case '*':
5683                if(exp.op.exp1)
5684                {
5685                   if(op1.ops.Mul)
5686                   {
5687                      FreeExpContents(exp);
5688                      op1.ops.Mul(exp, op1, op2);
5689                   }
5690                }
5691                break;
5692             case '+':
5693                if(exp.op.exp1)
5694                {
5695                   if(op1.ops.Add)
5696                   {
5697                      FreeExpContents(exp);
5698                      op1.ops.Add(exp, op1, op2);
5699                   }
5700                }
5701                else
5702                {
5703                   // Provide default unary +
5704                   Expression exp2 = exp.op.exp2;
5705                   exp.op.exp2 = null;
5706                   FreeExpContents(exp);
5707                   FreeType(exp.expType);
5708                   FreeType(exp.destType);
5709
5710                   *exp = *exp2;
5711                   delete exp2;
5712                }
5713                break;
5714             case '-':
5715                if(exp.op.exp1)
5716                {
5717                   if(op1.ops.Sub)
5718                   {
5719                      FreeExpContents(exp);
5720                      op1.ops.Sub(exp, op1, op2);
5721                   }
5722                }
5723                else
5724                {
5725                   if(op1.ops.Neg)
5726                   {
5727                      FreeExpContents(exp);
5728                      op1.ops.Neg(exp, op1);
5729                   }
5730                }
5731                break;
5732             case '~':
5733                if(op1.ops.BitNot)
5734                {
5735                   FreeExpContents(exp);
5736                   op1.ops.BitNot(exp, op1);
5737                }
5738                break;
5739             case '!':
5740                if(op1.ops.Not)
5741                {
5742                   FreeExpContents(exp);
5743                   op1.ops.Not(exp, op1);
5744                }
5745                break;
5746             // Binary only operators
5747             case '/':
5748                if(op1.ops.Div)
5749                {
5750                   FreeExpContents(exp);
5751                   op1.ops.Div(exp, op1, op2);
5752                }
5753                break;
5754             case '%':
5755                if(op1.ops.Mod)
5756                {
5757                   FreeExpContents(exp);
5758                   op1.ops.Mod(exp, op1, op2);
5759                }
5760                break;
5761             case LEFT_OP:
5762                break;
5763             case RIGHT_OP:
5764                break;
5765             case '<':
5766                if(exp.op.exp1)
5767                {
5768                   if(op1.ops.Sma)
5769                   {
5770                      FreeExpContents(exp);
5771                      op1.ops.Sma(exp, op1, op2);
5772                   }
5773                }
5774                break;
5775             case '>':
5776                if(exp.op.exp1)
5777                {
5778                   if(op1.ops.Grt)
5779                   {
5780                      FreeExpContents(exp);
5781                      op1.ops.Grt(exp, op1, op2);
5782                   }
5783                }
5784                break;
5785             case LE_OP:
5786                if(exp.op.exp1)
5787                {
5788                   if(op1.ops.SmaEqu)
5789                   {
5790                      FreeExpContents(exp);
5791                      op1.ops.SmaEqu(exp, op1, op2);
5792                   }
5793                }
5794                break;
5795             case GE_OP:
5796                if(exp.op.exp1)
5797                {
5798                   if(op1.ops.GrtEqu)
5799                   {
5800                      FreeExpContents(exp);
5801                      op1.ops.GrtEqu(exp, op1, op2);
5802                   }
5803                }
5804                break;
5805             case EQ_OP:
5806                if(exp.op.exp1)
5807                {
5808                   if(op1.ops.Equ)
5809                   {
5810                      FreeExpContents(exp);
5811                      op1.ops.Equ(exp, op1, op2);
5812                   }
5813                }
5814                break;
5815             case NE_OP:
5816                if(exp.op.exp1)
5817                {
5818                   if(op1.ops.Nqu)
5819                   {
5820                      FreeExpContents(exp);
5821                      op1.ops.Nqu(exp, op1, op2);
5822                   }
5823                }
5824                break;
5825             case '|':
5826                if(op1.ops.BitOr)
5827                {
5828                   FreeExpContents(exp);
5829                   op1.ops.BitOr(exp, op1, op2);
5830                }
5831                break;
5832             case '^':
5833                if(op1.ops.BitXor)
5834                {
5835                   FreeExpContents(exp);
5836                   op1.ops.BitXor(exp, op1, op2);
5837                }
5838                break;
5839             case AND_OP:
5840                break;
5841             case OR_OP:
5842                break;
5843             case SIZEOF:
5844                FreeExpContents(exp);
5845                exp.type = constantExp;
5846                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5847                break;
5848          }
5849          */
5850          if(op1.type) FreeType(op1.type);
5851          if(op2.type) FreeType(op2.type);
5852          break;
5853       }
5854       case bracketsExp:
5855       case extensionExpressionExp:
5856       {
5857          Expression e, n;
5858          for(e = exp.list->first; e; e = n)
5859          {
5860             n = e.next;
5861             if(!n)
5862             {
5863                OldList * list = exp.list;
5864                Expression prev = exp.prev;
5865                Expression next = exp.next;
5866                ComputeExpression(e);
5867                //FreeExpContents(exp);
5868                FreeType(exp.expType);
5869                FreeType(exp.destType);
5870                *exp = *e;
5871                exp.prev = prev;
5872                exp.next = next;
5873                delete e;
5874                delete list;
5875             }
5876             else
5877             {
5878                FreeExpression(e);
5879             }
5880          }
5881          break;
5882       }
5883       /*
5884
5885       case ExpIndex:
5886       {
5887          Expression e;
5888          exp.isConstant = true;
5889
5890          ComputeExpression(exp.index.exp);
5891          if(!exp.index.exp.isConstant)
5892             exp.isConstant = false;
5893
5894          for(e = exp.index.index->first; e; e = e.next)
5895          {
5896             ComputeExpression(e);
5897             if(!e.next)
5898             {
5899                // Check if this type is int
5900             }
5901             if(!e.isConstant)
5902                exp.isConstant = false;
5903          }
5904          exp.expType = Dereference(exp.index.exp.expType);
5905          break;
5906       }
5907       */
5908       case memberExp:
5909       {
5910          Expression memberExp = exp.member.exp;
5911          Identifier memberID = exp.member.member;
5912
5913          Type type;
5914          ComputeExpression(exp.member.exp);
5915          type = exp.member.exp.expType;
5916          if(type)
5917          {
5918             Class _class = (exp.member.member && exp.member.member.classSym) ? exp.member.member.classSym.registered : (((type.kind == classType || type.kind == subClassType) && type._class) ? type._class.registered : null);
5919             Property prop = null;
5920             DataMember member = null;
5921             Class convertTo = null;
5922             if(type.kind == subClassType && exp.member.exp.type == classExp)
5923                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5924
5925             if(!_class)
5926             {
5927                char string[256];
5928                Symbol classSym;
5929                string[0] = '\0';
5930                PrintTypeNoConst(type, string, false, true);
5931                classSym = FindClass(string);
5932                _class = classSym ? classSym.registered : null;
5933             }
5934
5935             if(exp.member.member)
5936             {
5937                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5938                if(!prop)
5939                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5940             }
5941             if(!prop && !member && _class && exp.member.member)
5942             {
5943                Symbol classSym = FindClass(exp.member.member.string);
5944                convertTo = _class;
5945                _class = classSym ? classSym.registered : null;
5946                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5947             }
5948
5949             if(prop)
5950             {
5951                if(prop.compiled)
5952                {
5953                   Type type = prop.dataType;
5954                   // TODO: Assuming same base type for units...
5955                   if(_class.type == unitClass)
5956                   {
5957                      if(type.kind == classType)
5958                      {
5959                         Class _class = type._class.registered;
5960                         if(_class.type == unitClass)
5961                         {
5962                            if(!_class.dataType)
5963                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5964                            type = _class.dataType;
5965                         }
5966                      }
5967                      switch(type.kind)
5968                      {
5969                         case floatType:
5970                         {
5971                            float value;
5972                            float (*Get)(float) = (void *)prop.Get;
5973                            GetFloat(exp.member.exp, &value);
5974                            exp.constant = PrintFloat(Get ? Get(value) : value);
5975                            exp.type = constantExp;
5976                            break;
5977                         }
5978                         case doubleType:
5979                         {
5980                            double value;
5981                            double (*Get)(double);
5982                            GetDouble(exp.member.exp, &value);
5983
5984                            if(convertTo)
5985                               Get = (void *)prop.Set;
5986                            else
5987                               Get = (void *)prop.Get;
5988                            exp.constant = PrintDouble(Get ? Get(value) : value);
5989                            exp.type = constantExp;
5990                            break;
5991                         }
5992                      }
5993                   }
5994                   else
5995                   {
5996                      if(convertTo)
5997                      {
5998                         Expression value = exp.member.exp;
5999                         Type type;
6000                         if(!prop.dataType)
6001                            ProcessPropertyType(prop);
6002
6003                         type = prop.dataType;
6004                         if(!type)
6005                         {
6006                             // printf("Investigate this\n");
6007                         }
6008                         else if(_class.type == structClass)
6009                         {
6010                            switch(type.kind)
6011                            {
6012                               case classType:
6013                               {
6014                                  Class propertyClass = type._class.registered;
6015                                  if(propertyClass.type == structClass && value.type == instanceExp)
6016                                  {
6017                                     void (*Set)(void *, void *) = (void *)prop.Set;
6018                                     exp.instance = Instantiation { };
6019                                     exp.instance.data = new0 byte[_class.structSize];
6020                                     exp.instance._class = MkSpecifierName(_class.fullName);
6021                                     exp.instance.loc = exp.loc;
6022                                     exp.type = instanceExp;
6023                                     Set(exp.instance.data, value.instance.data);
6024                                     PopulateInstance(exp.instance);
6025                                  }
6026                                  break;
6027                               }
6028                               case intType:
6029                               {
6030                                  int intValue;
6031                                  void (*Set)(void *, int) = (void *)prop.Set;
6032
6033                                  exp.instance = Instantiation { };
6034                                  exp.instance.data = new0 byte[_class.structSize];
6035                                  exp.instance._class = MkSpecifierName(_class.fullName);
6036                                  exp.instance.loc = exp.loc;
6037                                  exp.type = instanceExp;
6038
6039                                  GetInt(value, &intValue);
6040
6041                                  Set(exp.instance.data, intValue);
6042                                  PopulateInstance(exp.instance);
6043                                  break;
6044                               }
6045                               case int64Type:
6046                               {
6047                                  int64 intValue;
6048                                  void (*Set)(void *, int64) = (void *)prop.Set;
6049
6050                                  exp.instance = Instantiation { };
6051                                  exp.instance.data = new0 byte[_class.structSize];
6052                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
6053                                  exp.instance.loc = exp.loc;
6054                                  exp.type = instanceExp;
6055
6056                                  GetInt64(value, &intValue);
6057
6058                                  Set(exp.instance.data, intValue);
6059                                  PopulateInstance(exp.instance);
6060                                  break;
6061                               }
6062                               case intPtrType:
6063                               {
6064                                  // TOFIX:
6065                                  intptr intValue;
6066                                  void (*Set)(void *, intptr) = (void *)prop.Set;
6067
6068                                  exp.instance = Instantiation { };
6069                                  exp.instance.data = new0 byte[_class.structSize];
6070                                  exp.instance._class = MkSpecifierName(_class.fullName);
6071                                  exp.instance.loc = exp.loc;
6072                                  exp.type = instanceExp;
6073
6074                                  GetIntPtr(value, &intValue);
6075
6076                                  Set(exp.instance.data, intValue);
6077                                  PopulateInstance(exp.instance);
6078                                  break;
6079                               }
6080                               case intSizeType:
6081                               {
6082                                  // TOFIX:
6083                                  intsize intValue;
6084                                  void (*Set)(void *, intsize) = (void *)prop.Set;
6085
6086                                  exp.instance = Instantiation { };
6087                                  exp.instance.data = new0 byte[_class.structSize];
6088                                  exp.instance._class = MkSpecifierName(_class.fullName);
6089                                  exp.instance.loc = exp.loc;
6090                                  exp.type = instanceExp;
6091
6092                                  GetIntSize(value, &intValue);
6093
6094                                  Set(exp.instance.data, intValue);
6095                                  PopulateInstance(exp.instance);
6096                                  break;
6097                               }
6098                               case floatType:
6099                               {
6100                                  float floatValue;
6101                                  void (*Set)(void *, float) = (void *)prop.Set;
6102
6103                                  exp.instance = Instantiation { };
6104                                  exp.instance.data = new0 byte[_class.structSize];
6105                                  exp.instance._class = MkSpecifierName(_class.fullName);
6106                                  exp.instance.loc = exp.loc;
6107                                  exp.type = instanceExp;
6108
6109                                  GetFloat(value, &floatValue);
6110
6111                                  Set(exp.instance.data, floatValue);
6112                                  PopulateInstance(exp.instance);
6113                                  break;
6114                               }
6115                               case doubleType:
6116                               {
6117                                  double doubleValue;
6118                                  void (*Set)(void *, double) = (void *)prop.Set;
6119
6120                                  exp.instance = Instantiation { };
6121                                  exp.instance.data = new0 byte[_class.structSize];
6122                                  exp.instance._class = MkSpecifierName(_class.fullName);
6123                                  exp.instance.loc = exp.loc;
6124                                  exp.type = instanceExp;
6125
6126                                  GetDouble(value, &doubleValue);
6127
6128                                  Set(exp.instance.data, doubleValue);
6129                                  PopulateInstance(exp.instance);
6130                                  break;
6131                               }
6132                            }
6133                         }
6134                         else if(_class.type == bitClass)
6135                         {
6136                            switch(type.kind)
6137                            {
6138                               case classType:
6139                               {
6140                                  Class propertyClass = type._class.registered;
6141                                  if(propertyClass.type == structClass && value.instance.data)
6142                                  {
6143                                     unsigned int (*Set)(void *) = (void *)prop.Set;
6144                                     unsigned int bits = Set(value.instance.data);
6145                                     exp.constant = PrintHexUInt(bits);
6146                                     exp.type = constantExp;
6147                                     break;
6148                                  }
6149                                  else if(_class.type == bitClass)
6150                                  {
6151                                     unsigned int value;
6152                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
6153                                     unsigned int bits;
6154
6155                                     GetUInt(exp.member.exp, &value);
6156                                     bits = Set(value);
6157                                     exp.constant = PrintHexUInt(bits);
6158                                     exp.type = constantExp;
6159                                  }
6160                               }
6161                            }
6162                         }
6163                      }
6164                      else
6165                      {
6166                         if(_class.type == bitClass)
6167                         {
6168                            unsigned int value;
6169                            GetUInt(exp.member.exp, &value);
6170
6171                            switch(type.kind)
6172                            {
6173                               case classType:
6174                               {
6175                                  Class _class = type._class.registered;
6176                                  if(_class.type == structClass)
6177                                  {
6178                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
6179
6180                                     exp.instance = Instantiation { };
6181                                     exp.instance.data = new0 byte[_class.structSize];
6182                                     exp.instance._class = MkSpecifierName(_class.fullName);
6183                                     exp.instance.loc = exp.loc;
6184                                     //exp.instance.fullSet = true;
6185                                     exp.type = instanceExp;
6186                                     Get(value, exp.instance.data);
6187                                     PopulateInstance(exp.instance);
6188                                  }
6189                                  else if(_class.type == bitClass)
6190                                  {
6191                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6192                                     uint64 bits = Get(value);
6193                                     exp.constant = PrintHexUInt64(bits);
6194                                     exp.type = constantExp;
6195                                  }
6196                                  break;
6197                               }
6198                            }
6199                         }
6200                         else if(_class.type == structClass)
6201                         {
6202                            byte * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6203                            switch(type.kind)
6204                            {
6205                               case classType:
6206                               {
6207                                  Class _class = type._class.registered;
6208                                  if(_class.type == structClass && value)
6209                                  {
6210                                     void (*Get)(void *, void *) = (void *)prop.Get;
6211
6212                                     exp.instance = Instantiation { };
6213                                     exp.instance.data = new0 byte[_class.structSize];
6214                                     exp.instance._class = MkSpecifierName(_class.fullName);
6215                                     exp.instance.loc = exp.loc;
6216                                     //exp.instance.fullSet = true;
6217                                     exp.type = instanceExp;
6218                                     Get(value, exp.instance.data);
6219                                     PopulateInstance(exp.instance);
6220                                  }
6221                                  break;
6222                               }
6223                            }
6224                         }
6225                         /*else
6226                         {
6227                            char * value = exp.member.exp.instance.data;
6228                            switch(type.kind)
6229                            {
6230                               case classType:
6231                               {
6232                                  Class _class = type._class.registered;
6233                                  if(_class.type == normalClass)
6234                                  {
6235                                     void *(*Get)(void *) = (void *)prop.Get;
6236
6237                                     exp.instance = Instantiation { };
6238                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6239                                     exp.type = instanceExp;
6240                                     exp.instance.data = Get(value, exp.instance.data);
6241                                  }
6242                                  break;
6243                               }
6244                            }
6245                         }
6246                         */
6247                      }
6248                   }
6249                }
6250                else
6251                {
6252                   exp.isConstant = false;
6253                }
6254             }
6255             else if(member)
6256             {
6257             }
6258          }
6259
6260          if(exp.type != ExpressionType::memberExp)
6261          {
6262             FreeExpression(memberExp);
6263             FreeIdentifier(memberID);
6264          }
6265          break;
6266       }
6267       case typeSizeExp:
6268       {
6269          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6270          FreeExpContents(exp);
6271          exp.constant = PrintUInt(ComputeTypeSize(type));
6272          exp.type = constantExp;
6273          FreeType(type);
6274          break;
6275       }
6276       case classSizeExp:
6277       {
6278          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6279          if(classSym && classSym.registered)
6280          {
6281             if(classSym.registered.fixed)
6282             {
6283                FreeSpecifier(exp._class);
6284                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6285                exp.type = constantExp;
6286             }
6287             else
6288             {
6289                char className[1024];
6290                strcpy(className, "__ecereClass_");
6291                FullClassNameCat(className, classSym.string, true);
6292                //MangleClassName(className);
6293
6294                DeclareClass(classSym, className);
6295
6296                FreeExpContents(exp);
6297                exp.type = pointerExp;
6298                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6299                exp.member.member = MkIdentifier("structSize");
6300             }
6301          }
6302          break;
6303       }
6304       case castExp:
6305       //case constantExp:
6306       {
6307          Type type;
6308          Expression e = exp;
6309          if(exp.type == castExp)
6310          {
6311             if(exp.cast.exp)
6312                ComputeExpression(exp.cast.exp);
6313             e = exp.cast.exp;
6314          }
6315          if(e && exp.expType)
6316          {
6317             /*if(exp.destType)
6318                type = exp.destType;
6319             else*/
6320                type = exp.expType;
6321             if(type.kind == classType)
6322             {
6323                Class _class = type._class.registered;
6324                if(_class && (_class.type == unitClass || _class.type == bitClass))
6325                {
6326                   if(!_class.dataType)
6327                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6328                   type = _class.dataType;
6329                }
6330             }
6331
6332             switch(type.kind)
6333             {
6334                case _BoolType:
6335                case charType:
6336                   if(type.isSigned)
6337                   {
6338                      char value = 0;
6339                      if(GetChar(e, &value))
6340                      {
6341                         FreeExpContents(exp);
6342                         exp.constant = PrintChar(value);
6343                         exp.type = constantExp;
6344                      }
6345                   }
6346                   else
6347                   {
6348                      unsigned char value = 0;
6349                      if(GetUChar(e, &value))
6350                      {
6351                         FreeExpContents(exp);
6352                         exp.constant = PrintUChar(value);
6353                         exp.type = constantExp;
6354                      }
6355                   }
6356                   break;
6357                case shortType:
6358                   if(type.isSigned)
6359                   {
6360                      short value = 0;
6361                      if(GetShort(e, &value))
6362                      {
6363                         FreeExpContents(exp);
6364                         exp.constant = PrintShort(value);
6365                         exp.type = constantExp;
6366                      }
6367                   }
6368                   else
6369                   {
6370                      unsigned short value = 0;
6371                      if(GetUShort(e, &value))
6372                      {
6373                         FreeExpContents(exp);
6374                         exp.constant = PrintUShort(value);
6375                         exp.type = constantExp;
6376                      }
6377                   }
6378                   break;
6379                case intType:
6380                   if(type.isSigned)
6381                   {
6382                      int value = 0;
6383                      if(GetInt(e, &value))
6384                      {
6385                         FreeExpContents(exp);
6386                         exp.constant = PrintInt(value);
6387                         exp.type = constantExp;
6388                      }
6389                   }
6390                   else
6391                   {
6392                      unsigned int value = 0;
6393                      if(GetUInt(e, &value))
6394                      {
6395                         FreeExpContents(exp);
6396                         exp.constant = PrintUInt(value);
6397                         exp.type = constantExp;
6398                      }
6399                   }
6400                   break;
6401                case int64Type:
6402                   if(type.isSigned)
6403                   {
6404                      int64 value = 0;
6405                      if(GetInt64(e, &value))
6406                      {
6407                         FreeExpContents(exp);
6408                         exp.constant = PrintInt64(value);
6409                         exp.type = constantExp;
6410                      }
6411                   }
6412                   else
6413                   {
6414                      uint64 value = 0;
6415                      if(GetUInt64(e, &value))
6416                      {
6417                         FreeExpContents(exp);
6418                         exp.constant = PrintUInt64(value);
6419                         exp.type = constantExp;
6420                      }
6421                   }
6422                   break;
6423                case intPtrType:
6424                   if(type.isSigned)
6425                   {
6426                      intptr value = 0;
6427                      if(GetIntPtr(e, &value))
6428                      {
6429                         FreeExpContents(exp);
6430                         exp.constant = PrintInt64((int64)value);
6431                         exp.type = constantExp;
6432                      }
6433                   }
6434                   else
6435                   {
6436                      uintptr value = 0;
6437                      if(GetUIntPtr(e, &value))
6438                      {
6439                         FreeExpContents(exp);
6440                         exp.constant = PrintUInt64((uint64)value);
6441                         exp.type = constantExp;
6442                      }
6443                   }
6444                   break;
6445                case intSizeType:
6446                   if(type.isSigned)
6447                   {
6448                      intsize value = 0;
6449                      if(GetIntSize(e, &value))
6450                      {
6451                         FreeExpContents(exp);
6452                         exp.constant = PrintInt64((int64)value);
6453                         exp.type = constantExp;
6454                      }
6455                   }
6456                   else
6457                   {
6458                      uintsize value = 0;
6459                      if(GetUIntSize(e, &value))
6460                      {
6461                         FreeExpContents(exp);
6462                         exp.constant = PrintUInt64((uint64)value);
6463                         exp.type = constantExp;
6464                      }
6465                   }
6466                   break;
6467                case floatType:
6468                {
6469                   float value = 0;
6470                   if(GetFloat(e, &value))
6471                   {
6472                      FreeExpContents(exp);
6473                      exp.constant = PrintFloat(value);
6474                      exp.type = constantExp;
6475                   }
6476                   break;
6477                }
6478                case doubleType:
6479                {
6480                   double value = 0;
6481                   if(GetDouble(e, &value))
6482                   {
6483                      FreeExpContents(exp);
6484                      exp.constant = PrintDouble(value);
6485                      exp.type = constantExp;
6486                   }
6487                   break;
6488                }
6489             }
6490          }
6491          break;
6492       }
6493       case conditionExp:
6494       {
6495          Operand op1 { };
6496          Operand op2 { };
6497          Operand op3 { };
6498
6499          if(exp.cond.exp)
6500             // Caring only about last expression for now...
6501             ComputeExpression(exp.cond.exp->last);
6502          if(exp.cond.elseExp)
6503             ComputeExpression(exp.cond.elseExp);
6504          if(exp.cond.cond)
6505             ComputeExpression(exp.cond.cond);
6506
6507          op1 = GetOperand(exp.cond.cond);
6508          if(op1.type) op1.type.refCount++;
6509          op2 = GetOperand(exp.cond.exp->last);
6510          if(op2.type) op2.type.refCount++;
6511          op3 = GetOperand(exp.cond.elseExp);
6512          if(op3.type) op3.type.refCount++;
6513
6514          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6515          if(op1.type) FreeType(op1.type);
6516          if(op2.type) FreeType(op2.type);
6517          if(op3.type) FreeType(op3.type);
6518          break;
6519       }
6520    }
6521 }
6522
6523 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6524 {
6525    bool result = true;
6526    if(destType)
6527    {
6528       OldList converts { };
6529       Conversion convert;
6530
6531       if(destType.kind == voidType)
6532          return false;
6533
6534       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6535          result = false;
6536       if(converts.count)
6537       {
6538          // for(convert = converts.last; convert; convert = convert.prev)
6539          for(convert = converts.first; convert; convert = convert.next)
6540          {
6541             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6542             if(!empty)
6543             {
6544                Expression newExp { };
6545                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6546
6547                // TODO: Check this...
6548                *newExp = *exp;
6549                newExp.prev = null;
6550                newExp.next = null;
6551                newExp.destType = null;
6552
6553                if(convert.isGet)
6554                {
6555                   // [exp].ColorRGB
6556                   exp.type = memberExp;
6557                   exp.addedThis = true;
6558                   exp.member.exp = newExp;
6559                   FreeType(exp.member.exp.expType);
6560
6561                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6562                   exp.member.exp.expType.classObjectType = objectType;
6563                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6564                   exp.member.memberType = propertyMember;
6565                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6566                   // TESTING THIS... for (int)degrees
6567                   exp.needCast = true;
6568                   if(exp.expType) exp.expType.refCount++;
6569                   ApplyAnyObjectLogic(exp.member.exp);
6570                }
6571                else
6572                {
6573
6574                   /*if(exp.isConstant)
6575                   {
6576                      // Color { ColorRGB = [exp] };
6577                      exp.type = instanceExp;
6578                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6579                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6580                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6581                   }
6582                   else*/
6583                   {
6584                      // If not constant, don't turn it yet into an instantiation
6585                      // (Go through the deep members system first)
6586                      exp.type = memberExp;
6587                      exp.addedThis = true;
6588                      exp.member.exp = newExp;
6589
6590                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6591                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6592                         newExp.expType._class.registered.type == noHeadClass)
6593                      {
6594                         newExp.byReference = true;
6595                      }
6596
6597                      FreeType(exp.member.exp.expType);
6598                      /*exp.member.exp.expType = convert.convert.dataType;
6599                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6600                      exp.member.exp.expType = null;
6601                      if(convert.convert.dataType)
6602                      {
6603                         exp.member.exp.expType = { };
6604                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6605                         exp.member.exp.expType.refCount = 1;
6606                         exp.member.exp.expType.classObjectType = objectType;
6607                         ApplyAnyObjectLogic(exp.member.exp);
6608                      }
6609
6610                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6611                      exp.member.memberType = reverseConversionMember;
6612                      exp.expType = convert.resultType ? convert.resultType :
6613                         MkClassType(convert.convert._class.fullName);
6614                      exp.needCast = true;
6615                      if(convert.resultType) convert.resultType.refCount++;
6616                   }
6617                }
6618             }
6619             else
6620             {
6621                FreeType(exp.expType);
6622                if(convert.isGet)
6623                {
6624                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6625                   if(exp.destType.casted)
6626                      exp.needCast = true;
6627                   if(exp.expType) exp.expType.refCount++;
6628                }
6629                else
6630                {
6631                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6632                   if(exp.destType.casted)
6633                      exp.needCast = true;
6634                   if(convert.resultType)
6635                      convert.resultType.refCount++;
6636                }
6637             }
6638          }
6639          if(exp.isConstant && inCompiler)
6640             ComputeExpression(exp);
6641
6642          converts.Free(FreeConvert);
6643       }
6644
6645       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6646       {
6647          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6648       }
6649       if(!result && exp.expType && exp.destType)
6650       {
6651          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6652              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6653             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6654             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6655             result = true;
6656       }
6657    }
6658    // if(result) CheckTemplateTypes(exp);
6659    return result;
6660 }
6661
6662 void CheckTemplateTypes(Expression exp)
6663 {
6664    /*
6665    bool et = exp.expType ? exp.expType.passAsTemplate : false;
6666    bool dt = exp.destType ? exp.destType.passAsTemplate : false;
6667    */
6668    Expression nbExp = GetNonBracketsExp(exp);
6669    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6670       (nbExp == exp || nbExp.type != castExp))
6671    {
6672       Expression newExp { };
6673       Context context;
6674       TypeKind kind = exp.expType.kind;
6675       *newExp = *exp;
6676       if(exp.destType) exp.destType.refCount++;
6677       if(exp.expType)  exp.expType.refCount++;
6678       newExp.prev = null;
6679       newExp.next = null;
6680
6681       if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered)
6682       {
6683          Class c = exp.expType._class.registered;
6684          if(c.type == bitClass || c.type == enumClass || c.type == unitClass)
6685          {
6686             if(!c.dataType)
6687                c.dataType = ProcessTypeString(c.dataTypeString, false);
6688             kind = c.dataType.kind;
6689          }
6690       }
6691
6692       switch(kind)
6693       {
6694          case doubleType:
6695             if(exp.destType.classObjectType)
6696             {
6697                // We need to pass the address, just pass it along (Undo what was done above)
6698                if(exp.destType) exp.destType.refCount--;
6699                if(exp.expType)  exp.expType.refCount--;
6700                delete newExp;
6701             }
6702             else
6703             {
6704                // If we're looking for value:
6705                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6706                OldList * specs;
6707                OldList * unionDefs = MkList();
6708                OldList * statements = MkList();
6709                context = PushContext();
6710                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6711                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6712                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6713                exp.type = extensionCompoundExp;
6714                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6715                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6716                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6717                exp.compound.compound.context = context;
6718                PopContext(context);
6719             }
6720             break;
6721          default:
6722             exp.type = castExp;
6723             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6724             if((exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass) || exp.expType.isPointerType)
6725                exp.cast.exp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), MkExpBrackets(MkListOne(newExp)));
6726             else
6727                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6728             exp.needCast = true;
6729             break;
6730       }
6731    }
6732    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6733    {
6734       Expression newExp { };
6735       Context context;
6736       TypeKind kind = exp.expType.kind;
6737       *newExp = *exp;
6738       if(exp.destType) exp.destType.refCount++;
6739       if(exp.expType)  exp.expType.refCount++;
6740       newExp.prev = null;
6741       newExp.next = null;
6742
6743       if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered)
6744       {
6745          Class c = exp.expType._class.registered;
6746          if(c.type == bitClass || c.type == enumClass || c.type == unitClass)
6747          {
6748             if(!c.dataType)
6749                c.dataType = ProcessTypeString(c.dataTypeString, false);
6750             kind = c.dataType.kind;
6751          }
6752       }
6753
6754       switch(kind)
6755       {
6756          case doubleType:
6757             if(exp.destType.classObjectType)
6758             {
6759                // We need to pass the address, just pass it along (Undo what was done above)
6760                if(exp.destType) exp.destType.refCount--;
6761                if(exp.expType)  exp.expType.refCount--;
6762                delete newExp;
6763             }
6764             else
6765             {
6766                // If we're looking for value:
6767                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6768                OldList * specs;
6769                OldList * unionDefs = MkList();
6770                OldList * statements = MkList();
6771                context = PushContext();
6772                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6773                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6774                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6775                exp.type = extensionCompoundExp;
6776                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6777                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6778                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6779                exp.compound.compound.context = context;
6780                PopContext(context);
6781             }
6782             break;
6783          case classType:
6784          {
6785             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6786             {
6787                exp.type = bracketsExp;
6788
6789                newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6790                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6791                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6792                ProcessExpressionType(exp.list->first);
6793                break;
6794             }
6795             else
6796             {
6797                exp.type = bracketsExp;
6798                if(exp.expType.isPointerType)
6799                {
6800                   exp.needTemplateCast = 2;
6801                   newExp.needCast = true;
6802                   newExp.needTemplateCast = 2;
6803                   newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6804                }
6805
6806                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6807                exp.needTemplateCast = 2;
6808                newExp.needCast = true;
6809                newExp.needTemplateCast = 2;
6810                ProcessExpressionType(exp.list->first);
6811                break;
6812             }
6813          }
6814          default:
6815          {
6816             if(exp.expType.kind == templateType)
6817             {
6818                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6819                if(type)
6820                {
6821                   FreeType(exp.destType);
6822                   FreeType(exp.expType);
6823                   delete newExp;
6824                   break;
6825                }
6826             }
6827             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6828             {
6829                exp.type = opExp;
6830                exp.op.op = '*';
6831                exp.op.exp1 = null;
6832                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6833                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6834             }
6835             else
6836             {
6837                char typeString[1024];
6838                Declarator decl;
6839                OldList * specs = MkList();
6840                typeString[0] = '\0';
6841                PrintType(exp.expType, typeString, false, false);
6842                decl = SpecDeclFromString(typeString, specs, null);
6843
6844                exp.type = castExp;
6845                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6846                exp.cast.typeName = MkTypeName(specs, decl);
6847                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6848                exp.cast.exp.needCast = true;
6849             }
6850             break;
6851          }
6852       }
6853    }
6854 }
6855 // TODO: The Symbol tree should be reorganized by namespaces
6856 // Name Space:
6857 //    - Tree of all symbols within (stored without namespace)
6858 //    - Tree of sub-namespaces
6859
6860 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6861 {
6862    int nsLen = strlen(nameSpace);
6863    Symbol symbol;
6864    // Start at the name space prefix
6865    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6866    {
6867       char * s = symbol.string;
6868       if(!strncmp(s, nameSpace, nsLen))
6869       {
6870          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6871          int c;
6872          char * namePart;
6873          for(c = strlen(s)-1; c >= 0; c--)
6874             if(s[c] == ':')
6875                break;
6876
6877          namePart = s+c+1;
6878          if(!strcmp(namePart, name))
6879          {
6880             // TODO: Error on ambiguity
6881             return symbol;
6882          }
6883       }
6884       else
6885          break;
6886    }
6887    return null;
6888 }
6889
6890 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6891 {
6892    int c;
6893    char nameSpace[1024];
6894    const char * namePart;
6895    bool gotColon = false;
6896
6897    nameSpace[0] = '\0';
6898    for(c = strlen(name)-1; c >= 0; c--)
6899       if(name[c] == ':')
6900       {
6901          gotColon = true;
6902          break;
6903       }
6904
6905    namePart = name+c+1;
6906    while(c >= 0 && name[c] == ':') c--;
6907    if(c >= 0)
6908    {
6909       // Try an exact match first
6910       Symbol symbol = (Symbol)tree.FindString(name);
6911       if(symbol)
6912          return symbol;
6913
6914       // Namespace specified
6915       memcpy(nameSpace, name, c + 1);
6916       nameSpace[c+1] = 0;
6917
6918       return ScanWithNameSpace(tree, nameSpace, namePart);
6919    }
6920    else if(gotColon)
6921    {
6922       // Looking for a global symbol, e.g. ::Sleep()
6923       Symbol symbol = (Symbol)tree.FindString(namePart);
6924       return symbol;
6925    }
6926    else
6927    {
6928       // Name only (no namespace specified)
6929       Symbol symbol = (Symbol)tree.FindString(namePart);
6930       if(symbol)
6931          return symbol;
6932       return ScanWithNameSpace(tree, "", namePart);
6933    }
6934    return null;
6935 }
6936
6937 static void ProcessDeclaration(Declaration decl);
6938
6939 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6940 {
6941 #ifdef _DEBUG
6942    //Time startTime = GetTime();
6943 #endif
6944    // Optimize this later? Do this before/less?
6945    Context ctx;
6946    Symbol symbol = null;
6947    // First, check if the identifier is declared inside the function
6948    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6949
6950    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6951    {
6952       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6953       {
6954          symbol = null;
6955          if(thisNameSpace)
6956          {
6957             char curName[1024];
6958             strcpy(curName, thisNameSpace);
6959             strcat(curName, "::");
6960             strcat(curName, name);
6961             // Try to resolve in current namespace first
6962             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6963          }
6964          if(!symbol)
6965             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6966       }
6967       else
6968          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6969
6970       if(symbol || ctx == endContext) break;
6971    }
6972    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6973    {
6974       if(symbol.pointerExternal.type == functionExternal)
6975       {
6976          FunctionDefinition function = symbol.pointerExternal.function;
6977
6978          // Modified this recently...
6979          Context tmpContext = curContext;
6980          curContext = null;
6981          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6982          curContext = tmpContext;
6983
6984          symbol.pointerExternal.symbol = symbol;
6985
6986          // TESTING THIS:
6987          DeclareType(symbol.type, true, true);
6988
6989          ast->Insert(curExternal.prev, symbol.pointerExternal);
6990
6991          symbol.id = curExternal.symbol.idCode;
6992
6993       }
6994       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6995       {
6996          ast->Move(symbol.pointerExternal, curExternal.prev);
6997          symbol.id = curExternal.symbol.idCode;
6998       }
6999    }
7000 #ifdef _DEBUG
7001    //findSymbolTotalTime += GetTime() - startTime;
7002 #endif
7003    return symbol;
7004 }
7005
7006 static void GetTypeSpecs(Type type, OldList * specs)
7007 {
7008    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
7009    switch(type.kind)
7010    {
7011       case classType:
7012       {
7013          if(type._class.registered)
7014          {
7015             if(!type._class.registered.dataType)
7016                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
7017             GetTypeSpecs(type._class.registered.dataType, specs);
7018          }
7019          break;
7020       }
7021       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
7022       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
7023       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
7024       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
7025       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
7026       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
7027       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
7028       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
7029       case intType:
7030       default:
7031          ListAdd(specs, MkSpecifier(INT)); break;
7032    }
7033 }
7034
7035 static void PrintArraySize(Type arrayType, char * string)
7036 {
7037    char size[256];
7038    size[0] = '\0';
7039    strcat(size, "[");
7040    if(arrayType.enumClass)
7041       strcat(size, arrayType.enumClass.string);
7042    else if(arrayType.arraySizeExp)
7043       PrintExpression(arrayType.arraySizeExp, size);
7044    strcat(size, "]");
7045    strcat(string, size);
7046 }
7047
7048 // WARNING : This function expects a null terminated string since it recursively concatenate...
7049 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
7050 {
7051    if(type)
7052    {
7053       if(printConst && type.constant)
7054          strcat(string, "const ");
7055       switch(type.kind)
7056       {
7057          case classType:
7058          {
7059             Symbol c = type._class;
7060             bool isObjectBaseClass = !c || !c.string || !strcmp(c.string, "class");
7061             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
7062             //       look into merging with thisclass ?
7063             if(type.classObjectType == typedObject && isObjectBaseClass)
7064                strcat(string, "typed_object");
7065             else if(type.classObjectType == anyObject && isObjectBaseClass)
7066                strcat(string, "any_object");
7067             else
7068             {
7069                if(c && c.string)
7070                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
7071             }
7072             if(type.byReference)
7073                strcat(string, " &");
7074             break;
7075          }
7076          case voidType: strcat(string, "void"); break;
7077          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
7078          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
7079          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
7080          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
7081          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
7082          case _BoolType: strcat(string, "_Bool"); break;
7083          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
7084          case floatType: strcat(string, "float"); break;
7085          case doubleType: strcat(string, "double"); break;
7086          case structType:
7087             if(type.enumName)
7088             {
7089                strcat(string, "struct ");
7090                strcat(string, type.enumName);
7091             }
7092             else if(type.typeName)
7093                strcat(string, type.typeName);
7094             else
7095             {
7096                Type member;
7097                strcat(string, "struct { ");
7098                for(member = type.members.first; member; member = member.next)
7099                {
7100                   PrintType(member, string, true, fullName);
7101                   strcat(string,"; ");
7102                }
7103                strcat(string,"}");
7104             }
7105             break;
7106          case unionType:
7107             if(type.enumName)
7108             {
7109                strcat(string, "union ");
7110                strcat(string, type.enumName);
7111             }
7112             else if(type.typeName)
7113                strcat(string, type.typeName);
7114             else
7115             {
7116                strcat(string, "union ");
7117                strcat(string,"(unnamed)");
7118             }
7119             break;
7120          case enumType:
7121             if(type.enumName)
7122             {
7123                strcat(string, "enum ");
7124                strcat(string, type.enumName);
7125             }
7126             else if(type.typeName)
7127                strcat(string, type.typeName);
7128             else
7129                strcat(string, "int"); // "enum");
7130             break;
7131          case ellipsisType:
7132             strcat(string, "...");
7133             break;
7134          case subClassType:
7135             strcat(string, "subclass(");
7136             strcat(string, type._class ? type._class.string : "int");
7137             strcat(string, ")");
7138             break;
7139          case templateType:
7140             strcat(string, type.templateParameter.identifier.string);
7141             break;
7142          case thisClassType:
7143             strcat(string, "thisclass");
7144             break;
7145          case vaListType:
7146             strcat(string, "__builtin_va_list");
7147             break;
7148       }
7149    }
7150 }
7151
7152 static void PrintName(Type type, char * string, bool fullName)
7153 {
7154    if(type.name && type.name[0])
7155    {
7156       if(fullName)
7157          strcat(string, type.name);
7158       else
7159       {
7160          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
7161          if(name) name += 2; else name = type.name;
7162          strcat(string, name);
7163       }
7164    }
7165 }
7166
7167 static void PrintAttribs(Type type, char * string)
7168 {
7169    if(type)
7170    {
7171       if(type.dllExport)   strcat(string, "dllexport ");
7172       if(type.attrStdcall) strcat(string, "stdcall ");
7173    }
7174 }
7175
7176 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
7177 {
7178    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7179    {
7180       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7181          PrintAttribs(type, string);
7182       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7183          strcat(string, " const");
7184       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7185       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7186          strcat(string, " (");
7187       if(type.kind == pointerType)
7188       {
7189          if(type.type.kind == functionType || type.type.kind == methodType)
7190             PrintAttribs(type.type, string);
7191       }
7192       if(type.kind == pointerType)
7193       {
7194          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7195             strcat(string, "*");
7196          else
7197             strcat(string, " *");
7198       }
7199       if(printConst && type.constant && type.kind == pointerType)
7200          strcat(string, " const");
7201    }
7202    else
7203       PrintTypeSpecs(type, string, fullName, printConst);
7204 }
7205
7206 static void PostPrintType(Type type, char * string, bool fullName)
7207 {
7208    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7209       strcat(string, ")");
7210    if(type.kind == arrayType)
7211       PrintArraySize(type, string);
7212    else if(type.kind == functionType)
7213    {
7214       Type param;
7215       strcat(string, "(");
7216       for(param = type.params.first; param; param = param.next)
7217       {
7218          PrintType(param, string, true, fullName);
7219          if(param.next) strcat(string, ", ");
7220       }
7221       strcat(string, ")");
7222    }
7223    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7224       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7225 }
7226
7227 // *****
7228 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7229 // *****
7230 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7231 {
7232    PrePrintType(type, string, fullName, null, printConst);
7233
7234    if(type.thisClass || (printName && type.name && type.name[0]))
7235       strcat(string, " ");
7236    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7237    {
7238       Symbol _class = type.thisClass;
7239       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7240       {
7241          if(type.classObjectType == classPointer)
7242             strcat(string, "class");
7243          else
7244             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7245       }
7246       else if(_class && _class.string)
7247       {
7248          String s = _class.string;
7249          if(fullName)
7250             strcat(string, s);
7251          else
7252          {
7253             char * name = RSearchString(s, "::", strlen(s), true, false);
7254             if(name) name += 2; else name = s;
7255             strcat(string, name);
7256          }
7257       }
7258       strcat(string, "::");
7259    }
7260
7261    if(printName && type.name)
7262       PrintName(type, string, fullName);
7263    PostPrintType(type, string, fullName);
7264    if(type.bitFieldCount)
7265    {
7266       char count[100];
7267       sprintf(count, ":%d", type.bitFieldCount);
7268       strcat(string, count);
7269    }
7270 }
7271
7272 void PrintType(Type type, char * string, bool printName, bool fullName)
7273 {
7274    _PrintType(type, string, printName, fullName, true);
7275 }
7276
7277 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7278 {
7279    _PrintType(type, string, printName, fullName, false);
7280 }
7281
7282 static Type FindMember(Type type, char * string)
7283 {
7284    Type memberType;
7285    for(memberType = type.members.first; memberType; memberType = memberType.next)
7286    {
7287       if(!memberType.name)
7288       {
7289          Type subType = FindMember(memberType, string);
7290          if(subType)
7291             return subType;
7292       }
7293       else if(!strcmp(memberType.name, string))
7294          return memberType;
7295    }
7296    return null;
7297 }
7298
7299 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7300 {
7301    Type memberType;
7302    for(memberType = type.members.first; memberType; memberType = memberType.next)
7303    {
7304       if(!memberType.name)
7305       {
7306          Type subType = FindMember(memberType, string);
7307          if(subType)
7308          {
7309             *offset += memberType.offset;
7310             return subType;
7311          }
7312       }
7313       else if(!strcmp(memberType.name, string))
7314       {
7315          *offset += memberType.offset;
7316          return memberType;
7317       }
7318    }
7319    return null;
7320 }
7321
7322 public bool GetParseError() { return parseError; }
7323
7324 Expression ParseExpressionString(char * expression)
7325 {
7326    parseError = false;
7327
7328    fileInput = TempFile { };
7329    fileInput.Write(expression, 1, strlen(expression));
7330    fileInput.Seek(0, start);
7331
7332    echoOn = false;
7333    parsedExpression = null;
7334    resetScanner();
7335    expression_yyparse();
7336    delete fileInput;
7337
7338    return parsedExpression;
7339 }
7340
7341 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7342 {
7343    Identifier id = exp.identifier;
7344    Method method = null;
7345    Property prop = null;
7346    DataMember member = null;
7347    ClassProperty classProp = null;
7348
7349    if(_class && _class.type == enumClass)
7350    {
7351       NamedLink64 value = null;
7352       Class enumClass = eSystem_FindClass(privateModule, "enum");
7353       if(enumClass)
7354       {
7355          Class baseClass;
7356          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7357          {
7358             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7359             for(value = e.values.first; value; value = value.next)
7360             {
7361                if(!strcmp(value.name, id.string))
7362                   break;
7363             }
7364             if(value)
7365             {
7366                exp.isConstant = true;
7367                if(inCompiler || inPreCompiler || inDebugger)
7368                {
7369                   char constant[256];
7370                   FreeExpContents(exp);
7371
7372                   exp.type = constantExp;
7373                   if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "char") || !strcmp(baseClass.dataTypeString, "short"))
7374                      sprintf(constant, FORMAT64D, value.data);
7375                   else
7376                      sprintf(constant, FORMAT64HEX, value.data);
7377                   exp.constant = CopyString(constant);
7378                }
7379                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7380                exp.expType = MkClassType(baseClass.fullName);
7381                break;
7382             }
7383          }
7384       }
7385       if(value)
7386          return true;
7387    }
7388    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7389    {
7390       ProcessMethodType(method);
7391       exp.expType = Type
7392       {
7393          refCount = 1;
7394          kind = methodType;
7395          method = method;
7396          // Crash here?
7397          // TOCHECK: Put it back to what it was...
7398          // methodClass = _class;
7399          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7400       };
7401       //id._class = null;
7402       return true;
7403    }
7404    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7405    {
7406       if(!prop.dataType)
7407          ProcessPropertyType(prop);
7408       exp.expType = prop.dataType;
7409       if(prop.dataType) prop.dataType.refCount++;
7410       return true;
7411    }
7412    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7413    {
7414       if(!member.dataType)
7415          member.dataType = ProcessTypeString(member.dataTypeString, false);
7416       exp.expType = member.dataType;
7417       if(member.dataType) member.dataType.refCount++;
7418       return true;
7419    }
7420    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7421    {
7422       if(!classProp.dataType)
7423          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7424
7425       if(classProp.constant)
7426       {
7427          FreeExpContents(exp);
7428
7429          exp.isConstant = true;
7430          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7431          {
7432             //char constant[256];
7433             exp.type = stringExp;
7434             exp.constant = QMkString((char *)(uintptr)classProp.Get(_class));
7435          }
7436          else
7437          {
7438             char constant[256];
7439             exp.type = constantExp;
7440             sprintf(constant, "%d", (int)classProp.Get(_class));
7441             exp.constant = CopyString(constant);
7442          }
7443       }
7444       else
7445       {
7446          // TO IMPLEMENT...
7447       }
7448
7449       exp.expType = classProp.dataType;
7450       if(classProp.dataType) classProp.dataType.refCount++;
7451       return true;
7452    }
7453    return false;
7454 }
7455
7456 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7457 {
7458    BinaryTree * tree = &nameSpace.functions;
7459    GlobalData data = (GlobalData)tree->FindString(name);
7460    NameSpace * child;
7461    if(!data)
7462    {
7463       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7464       {
7465          data = ScanGlobalData(child, name);
7466          if(data)
7467             break;
7468       }
7469    }
7470    return data;
7471 }
7472
7473 static GlobalData FindGlobalData(char * name)
7474 {
7475    int start = 0, c;
7476    NameSpace * nameSpace;
7477    nameSpace = globalData;
7478    for(c = 0; name[c]; c++)
7479    {
7480       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7481       {
7482          NameSpace * newSpace;
7483          char * spaceName = new char[c - start + 1];
7484          strncpy(spaceName, name + start, c - start);
7485          spaceName[c-start] = '\0';
7486          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7487          delete spaceName;
7488          if(!newSpace)
7489             return null;
7490          nameSpace = newSpace;
7491          if(name[c] == ':') c++;
7492          start = c+1;
7493       }
7494    }
7495    if(c - start)
7496    {
7497       return ScanGlobalData(nameSpace, name + start);
7498    }
7499    return null;
7500 }
7501
7502 static int definedExpStackPos;
7503 static void * definedExpStack[512];
7504
7505 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7506 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7507 {
7508    Expression prev = checkedExp.prev, next = checkedExp.next;
7509
7510    FreeExpContents(checkedExp);
7511    FreeType(checkedExp.expType);
7512    FreeType(checkedExp.destType);
7513
7514    *checkedExp = *newExp;
7515
7516    delete newExp;
7517
7518    checkedExp.prev = prev;
7519    checkedExp.next = next;
7520 }
7521
7522 void ApplyAnyObjectLogic(Expression e)
7523 {
7524    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7525 #ifdef _DEBUG
7526    char debugExpString[4096];
7527    debugExpString[0] = '\0';
7528    PrintExpression(e, debugExpString);
7529 #endif
7530
7531    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7532    {
7533       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7534       //ellipsisDestType = destType;
7535       if(e && e.expType)
7536       {
7537          Type type = e.expType;
7538          Class _class = null;
7539          //Type destType = e.destType;
7540
7541          if(type.kind == classType && type._class && type._class.registered)
7542          {
7543             _class = type._class.registered;
7544          }
7545          else if(type.kind == subClassType)
7546          {
7547             _class = FindClass("ecere::com::Class").registered;
7548          }
7549          else
7550          {
7551             char string[1024] = "";
7552             Symbol classSym;
7553
7554             PrintTypeNoConst(type, string, false, true);
7555             classSym = FindClass(string);
7556             if(classSym) _class = classSym.registered;
7557          }
7558
7559          if((_class && (_class.type == enumClass || _class.type == unitClass || _class.type == bitClass || _class.type == systemClass) && strcmp(_class.fullName, "class") && strcmp(_class.fullName, "uintptr") && strcmp(_class.fullName, "intptr")) || // Patched so that class isn't considered SYSTEM...
7560             (!e.expType.classObjectType && (((type.kind != pointerType && type.kind != intPtrType && type.kind != subClassType && (type.kind != classType || !type._class || !type._class.registered || type._class.registered.type == structClass))) ||
7561             destType.byReference)))
7562          {
7563             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7564             {
7565                Expression checkedExp = e, newExp;
7566
7567                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7568                {
7569                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7570                   {
7571                      if(checkedExp.type == extensionCompoundExp)
7572                      {
7573                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7574                      }
7575                      else
7576                         checkedExp = checkedExp.list->last;
7577                   }
7578                   else if(checkedExp.type == castExp)
7579                      checkedExp = checkedExp.cast.exp;
7580                }
7581
7582                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7583                {
7584                   newExp = checkedExp.op.exp2;
7585                   checkedExp.op.exp2 = null;
7586                   FreeExpContents(checkedExp);
7587
7588                   if(e.expType && e.expType.passAsTemplate)
7589                   {
7590                      char size[100];
7591                      ComputeTypeSize(e.expType);
7592                      sprintf(size, "%d", e.expType.size);
7593                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7594                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7595                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7596                   }
7597
7598                   ReplaceExpContents(checkedExp, newExp);
7599                   e.byReference = true;
7600                }
7601                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7602                {
7603                   Expression checkedExp; //, newExp;
7604
7605                   {
7606                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7607                      bool hasAddress =
7608                         e.type == identifierExp ||
7609                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7610                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7611                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7612                         e.type == indexExp;
7613
7614                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7615                      {
7616                         Context context = PushContext();
7617                         Declarator decl;
7618                         OldList * specs = MkList();
7619                         char typeString[1024];
7620                         Expression newExp { };
7621
7622                         typeString[0] = '\0';
7623                         *newExp = *e;
7624
7625                         //if(e.destType) e.destType.refCount++;
7626                         // if(exp.expType) exp.expType.refCount++;
7627                         newExp.prev = null;
7628                         newExp.next = null;
7629                         newExp.expType = null;
7630
7631                         PrintTypeNoConst(e.expType, typeString, false, true);
7632                         decl = SpecDeclFromString(typeString, specs, null);
7633                         newExp.destType = ProcessType(specs, decl);
7634
7635                         curContext = context;
7636
7637                         // We need a current compound for this
7638                         if(curCompound)
7639                         {
7640                            char name[100];
7641                            OldList * stmts = MkList();
7642                            e.type = extensionCompoundExp;
7643                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7644                            if(!curCompound.compound.declarations)
7645                               curCompound.compound.declarations = MkList();
7646                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7647                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7648                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7649                            e.compound = MkCompoundStmt(null, stmts);
7650                         }
7651                         else
7652                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7653
7654                         /*
7655                         e.compound = MkCompoundStmt(
7656                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7657                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7658
7659                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7660                         */
7661
7662                         {
7663                            Type type = e.destType;
7664                            e.destType = { };
7665                            CopyTypeInto(e.destType, type);
7666                            e.destType.refCount = 1;
7667                            e.destType.classObjectType = none;
7668                            FreeType(type);
7669                         }
7670
7671                         e.compound.compound.context = context;
7672                         PopContext(context);
7673                         curContext = context.parent;
7674                      }
7675                   }
7676
7677                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7678                   checkedExp = e;
7679                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7680                   {
7681                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7682                      {
7683                         if(checkedExp.type == extensionCompoundExp)
7684                         {
7685                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7686                         }
7687                         else
7688                            checkedExp = checkedExp.list->last;
7689                      }
7690                      else if(checkedExp.type == castExp)
7691                         checkedExp = checkedExp.cast.exp;
7692                   }
7693                   {
7694                      Expression operand { };
7695                      operand = *checkedExp;
7696                      checkedExp.Clear();
7697                      checkedExp.destType = ProcessTypeString("void *", false);
7698                      checkedExp.expType = checkedExp.destType;
7699                      checkedExp.destType.refCount++;
7700
7701                      checkedExp.type = opExp;
7702                      checkedExp.op.op = '&';
7703                      checkedExp.op.exp1 = null;
7704                      checkedExp.op.exp2 = operand;
7705
7706                      //newExp = MkExpOp(null, '&', checkedExp);
7707                   }
7708                   //ReplaceExpContents(checkedExp, newExp);
7709                }
7710             }
7711          }
7712       }
7713    }
7714    {
7715       // If expression type is a simple class, make it an address
7716       // FixReference(e, true);
7717    }
7718 //#if 0
7719    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7720       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7721          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7722    {
7723       if(e.expType.classObjectType && destType && destType.classObjectType) //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class"))
7724       {
7725          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7726       }
7727       else
7728       {
7729          Expression thisExp { };
7730
7731          *thisExp = *e;
7732          thisExp.prev = null;
7733          thisExp.next = null;
7734          e.Clear();
7735
7736          e.type = bracketsExp;
7737          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7738          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7739             ((Expression)e.list->first).byReference = true;
7740
7741          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7742          {
7743             e.expType = thisExp.expType;
7744             e.expType.refCount++;
7745          }
7746          else*/
7747          {
7748             e.expType = { };
7749             CopyTypeInto(e.expType, thisExp.expType);
7750             e.expType.byReference = false;
7751             e.expType.refCount = 1;
7752
7753             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7754                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7755             {
7756                e.expType.classObjectType = none;
7757             }
7758          }
7759       }
7760    }
7761 // TOFIX: Try this for a nice IDE crash!
7762 //#endif
7763    // The other way around
7764    else
7765 //#endif
7766    if(destType && e.expType &&
7767          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7768          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7769          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7770    {
7771       if(destType.kind == ellipsisType)
7772       {
7773          Compiler_Error($"Unspecified type\n");
7774       }
7775       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7776       {
7777          bool byReference = e.expType.byReference;
7778          Expression thisExp { };
7779          Declarator decl;
7780          OldList * specs = MkList();
7781          char typeString[1024]; // Watch buffer overruns
7782          Type type;
7783          ClassObjectType backupClassObjectType;
7784          bool backupByReference;
7785
7786          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7787             type = e.expType;
7788          else
7789             type = destType;
7790
7791          backupClassObjectType = type.classObjectType;
7792          backupByReference = type.byReference;
7793
7794          type.classObjectType = none;
7795          type.byReference = false;
7796
7797          typeString[0] = '\0';
7798          PrintType(type, typeString, false, true);
7799          decl = SpecDeclFromString(typeString, specs, null);
7800
7801          type.classObjectType = backupClassObjectType;
7802          type.byReference = backupByReference;
7803
7804          *thisExp = *e;
7805          thisExp.prev = null;
7806          thisExp.next = null;
7807          e.Clear();
7808
7809          if( ( type.kind == classType && type._class && type._class.registered &&
7810                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7811                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7812              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7813              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7814          {
7815             bool passAsTemplate = thisExp.destType.passAsTemplate;
7816             Type t;
7817
7818             destType.refCount++;
7819
7820             e.type = opExp;
7821             e.op.op = '*';
7822             e.op.exp1 = null;
7823             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7824
7825             t = { };
7826             CopyTypeInto(t, thisExp.destType);
7827             t.passAsTemplate = false;
7828             FreeType(thisExp.destType);
7829             thisExp.destType = t;
7830
7831             t = { };
7832             CopyTypeInto(t, destType);
7833             t.passAsTemplate = passAsTemplate;
7834             FreeType(destType);
7835             destType = t;
7836             destType.refCount = 0;
7837
7838             e.expType = { };
7839             CopyTypeInto(e.expType, type);
7840             if(type.passAsTemplate)
7841             {
7842                e.expType.classObjectType = none;
7843                e.expType.passAsTemplate = false;
7844             }
7845             e.expType.byReference = false;
7846             e.expType.refCount = 1;
7847          }
7848          else
7849          {
7850             e.type = castExp;
7851             e.cast.typeName = MkTypeName(specs, decl);
7852             e.cast.exp = thisExp;
7853             e.byReference = true;
7854             e.expType = type;
7855             type.refCount++;
7856          }
7857
7858          if(e.destType)
7859             FreeType(e.destType);
7860
7861          e.destType = destType;
7862          destType.refCount++;
7863       }
7864    }
7865 }
7866
7867 void ApplyLocation(Expression exp, Location loc)
7868 {
7869    exp.loc = loc;
7870    switch(exp.type)
7871    {
7872       case opExp:
7873          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7874          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7875          break;
7876       case bracketsExp:
7877          if(exp.list)
7878          {
7879             Expression e;
7880             for(e = exp.list->first; e; e = e.next)
7881                ApplyLocation(e, loc);
7882          }
7883          break;
7884       case indexExp:
7885          if(exp.index.index)
7886          {
7887             Expression e;
7888             for(e = exp.index.index->first; e; e = e.next)
7889                ApplyLocation(e, loc);
7890          }
7891          if(exp.index.exp)
7892             ApplyLocation(exp.index.exp, loc);
7893          break;
7894       case callExp:
7895          if(exp.call.arguments)
7896          {
7897             Expression arg;
7898             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7899                ApplyLocation(arg, loc);
7900          }
7901          if(exp.call.exp)
7902             ApplyLocation(exp.call.exp, loc);
7903          break;
7904       case memberExp:
7905       case pointerExp:
7906          if(exp.member.exp)
7907             ApplyLocation(exp.member.exp, loc);
7908          break;
7909       case castExp:
7910          if(exp.cast.exp)
7911             ApplyLocation(exp.cast.exp, loc);
7912          break;
7913       case conditionExp:
7914          if(exp.cond.exp)
7915          {
7916             Expression e;
7917             for(e = exp.cond.exp->first; e; e = e.next)
7918                ApplyLocation(e, loc);
7919          }
7920          if(exp.cond.cond)
7921             ApplyLocation(exp.cond.cond, loc);
7922          if(exp.cond.elseExp)
7923             ApplyLocation(exp.cond.elseExp, loc);
7924          break;
7925       case vaArgExp:
7926          if(exp.vaArg.exp)
7927             ApplyLocation(exp.vaArg.exp, loc);
7928          break;
7929       default:
7930          break;
7931    }
7932 }
7933
7934 void ProcessExpressionType(Expression exp)
7935 {
7936    bool unresolved = false;
7937    Location oldyylloc = yylloc;
7938    bool notByReference = false;
7939 #ifdef _DEBUG
7940    char debugExpString[4096];
7941    debugExpString[0] = '\0';
7942    PrintExpression(exp, debugExpString);
7943 #endif
7944    if(!exp || exp.expType)
7945       return;
7946
7947    //eSystem_Logf("%s\n", expString);
7948
7949    // Testing this here
7950    yylloc = exp.loc;
7951    switch(exp.type)
7952    {
7953       case identifierExp:
7954       {
7955          Identifier id = exp.identifier;
7956          if(!id || !topContext) return;
7957
7958          // DOING THIS LATER NOW...
7959          if(id._class && id._class.name)
7960          {
7961             id.classSym = id._class.symbol; // FindClass(id._class.name);
7962             /* TODO: Name Space Fix ups
7963             if(!id.classSym)
7964                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7965             */
7966          }
7967
7968          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7969          {
7970             exp.expType = ProcessTypeString("Module", true);
7971             break;
7972          }
7973          else */
7974          if(!strcmp(id.string, "__runtimePlatform"))
7975          {
7976             exp.expType = ProcessTypeString("ecere::com::Platform", true);
7977             break;
7978          }
7979          else if(strstr(id.string, "__ecereClass") == id.string)
7980          {
7981             exp.expType = ProcessTypeString("ecere::com::Class", true);
7982             break;
7983          }
7984          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7985          {
7986             // Added this here as well
7987             ReplaceClassMembers(exp, thisClass);
7988             if(exp.type != identifierExp)
7989             {
7990                ProcessExpressionType(exp);
7991                break;
7992             }
7993
7994             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7995                break;
7996          }
7997          else
7998          {
7999             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
8000             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
8001             if(!symbol/* && exp.destType*/)
8002             {
8003                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
8004                   break;
8005                else
8006                {
8007                   if(thisClass)
8008                   {
8009                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
8010                      if(exp.type != identifierExp)
8011                      {
8012                         ProcessExpressionType(exp);
8013                         break;
8014                      }
8015                   }
8016                   // Static methods called from inside the _class
8017                   else if(currentClass && !id._class)
8018                   {
8019                      if(ResolveIdWithClass(exp, currentClass, true))
8020                         break;
8021                   }
8022                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
8023                }
8024             }
8025
8026             // If we manage to resolve this symbol
8027             if(symbol)
8028             {
8029                Type type = symbol.type;
8030                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
8031
8032                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
8033                {
8034                   Context context = SetupTemplatesContext(_class);
8035                   type = ReplaceThisClassType(_class);
8036                   FinishTemplatesContext(context);
8037                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
8038                }
8039
8040                FreeSpecifier(id._class);
8041                id._class = null;
8042                delete id.string;
8043                id.string = CopyString(symbol.string);
8044
8045                id.classSym = null;
8046                exp.expType = type;
8047                if(type)
8048                   type.refCount++;
8049
8050                                                 // Commented this out, it was making non-constant enum parameters seen as constant
8051                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
8052                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
8053                   // Add missing cases here... enum Classes...
8054                   exp.isConstant = true;
8055
8056                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
8057                if(symbol.isParam || !strcmp(id.string, "this"))
8058                {
8059                   if(_class && _class.type == structClass && !type.declaredWithStruct)
8060                      exp.byReference = true;
8061
8062                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
8063                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
8064                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
8065                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
8066                   {
8067                      Identifier id = exp.identifier;
8068                      exp.type = bracketsExp;
8069                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
8070                   }*/
8071                }
8072
8073                if(symbol.isIterator)
8074                {
8075                   if(symbol.isIterator == 3)
8076                   {
8077                      exp.type = bracketsExp;
8078                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
8079                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
8080                      exp.expType = null;
8081                      ProcessExpressionType(exp);
8082                   }
8083                   else if(symbol.isIterator != 4)
8084                   {
8085                      exp.type = memberExp;
8086                      exp.member.exp = MkExpIdentifier(exp.identifier);
8087                      exp.member.exp.expType = exp.expType;
8088                      /*if(symbol.isIterator == 6)
8089                         exp.member.member = MkIdentifier("key");
8090                      else*/
8091                         exp.member.member = MkIdentifier("data");
8092                      exp.expType = null;
8093                      ProcessExpressionType(exp);
8094                   }
8095                }
8096                break;
8097             }
8098             else
8099             {
8100                DefinedExpression definedExp = null;
8101                if(thisNameSpace && !(id._class && !id._class.name))
8102                {
8103                   char name[1024];
8104                   strcpy(name, thisNameSpace);
8105                   strcat(name, "::");
8106                   strcat(name, id.string);
8107                   definedExp = eSystem_FindDefine(privateModule, name);
8108                }
8109                if(!definedExp)
8110                   definedExp = eSystem_FindDefine(privateModule, id.string);
8111                if(definedExp)
8112                {
8113                   int c;
8114                   for(c = 0; c<definedExpStackPos; c++)
8115                      if(definedExpStack[c] == definedExp)
8116                         break;
8117                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
8118                   {
8119                      Location backupYylloc = yylloc;
8120                      File backInput = fileInput;
8121                      definedExpStack[definedExpStackPos++] = definedExp;
8122
8123                      fileInput = TempFile { };
8124                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
8125                      fileInput.Seek(0, start);
8126
8127                      echoOn = false;
8128                      parsedExpression = null;
8129                      resetScanner();
8130                      expression_yyparse();
8131                      delete fileInput;
8132                      if(backInput)
8133                         fileInput = backInput;
8134
8135                      yylloc = backupYylloc;
8136
8137                      if(parsedExpression)
8138                      {
8139                         FreeIdentifier(id);
8140                         exp.type = bracketsExp;
8141                         exp.list = MkListOne(parsedExpression);
8142                         ApplyLocation(parsedExpression, yylloc);
8143                         ProcessExpressionType(exp);
8144                         definedExpStackPos--;
8145                         return;
8146                      }
8147                      definedExpStackPos--;
8148                   }
8149                   else
8150                   {
8151                      if(inCompiler)
8152                      {
8153                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
8154                      }
8155                   }
8156                }
8157                else
8158                {
8159                   GlobalData data = null;
8160                   if(thisNameSpace && !(id._class && !id._class.name))
8161                   {
8162                      char name[1024];
8163                      strcpy(name, thisNameSpace);
8164                      strcat(name, "::");
8165                      strcat(name, id.string);
8166                      data = FindGlobalData(name);
8167                   }
8168                   if(!data)
8169                      data = FindGlobalData(id.string);
8170                   if(data)
8171                   {
8172                      DeclareGlobalData(data);
8173                      exp.expType = data.dataType;
8174                      if(data.dataType) data.dataType.refCount++;
8175
8176                      delete id.string;
8177                      id.string = CopyString(data.fullName);
8178                      FreeSpecifier(id._class);
8179                      id._class = null;
8180
8181                      break;
8182                   }
8183                   else
8184                   {
8185                      GlobalFunction function = null;
8186                      if(thisNameSpace && !(id._class && !id._class.name))
8187                      {
8188                         char name[1024];
8189                         strcpy(name, thisNameSpace);
8190                         strcat(name, "::");
8191                         strcat(name, id.string);
8192                         function = eSystem_FindFunction(privateModule, name);
8193                      }
8194                      if(!function)
8195                         function = eSystem_FindFunction(privateModule, id.string);
8196                      if(function)
8197                      {
8198                         char name[1024];
8199                         delete id.string;
8200                         id.string = CopyString(function.name);
8201                         name[0] = 0;
8202
8203                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8204                            strcpy(name, "__ecereFunction_");
8205                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8206                         if(DeclareFunction(function, name))
8207                         {
8208                            delete id.string;
8209                            id.string = CopyString(name);
8210                         }
8211                         exp.expType = function.dataType;
8212                         if(function.dataType) function.dataType.refCount++;
8213
8214                         FreeSpecifier(id._class);
8215                         id._class = null;
8216
8217                         break;
8218                      }
8219                   }
8220                }
8221             }
8222          }
8223          unresolved = true;
8224          break;
8225       }
8226       case instanceExp:
8227       {
8228          // Class _class;
8229          // Symbol classSym;
8230
8231          if(!exp.instance._class)
8232          {
8233             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8234             {
8235                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8236             }
8237          }
8238
8239          //classSym = FindClass(exp.instance._class.fullName);
8240          //_class = classSym ? classSym.registered : null;
8241
8242          ProcessInstantiationType(exp.instance);
8243
8244          exp.isConstant = exp.instance.isConstant;
8245
8246          /*
8247          if(_class.type == unitClass && _class.base.type != systemClass)
8248          {
8249             {
8250                Type destType = exp.destType;
8251
8252                exp.destType = MkClassType(_class.base.fullName);
8253                exp.expType = MkClassType(_class.fullName);
8254                CheckExpressionType(exp, exp.destType, true);
8255
8256                exp.destType = destType;
8257             }
8258             exp.expType = MkClassType(_class.fullName);
8259          }
8260          else*/
8261          if(exp.instance._class)
8262          {
8263             exp.expType = MkClassType(exp.instance._class.name);
8264             /*if(exp.expType._class && exp.expType._class.registered &&
8265                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8266                exp.expType.byReference = true;*/
8267          }
8268          break;
8269       }
8270       case constantExp:
8271       {
8272          if(!exp.expType)
8273          {
8274             char * constant = exp.constant;
8275             Type type
8276             {
8277                refCount = 1;
8278                constant = true;
8279             };
8280             exp.expType = type;
8281
8282             if(constant[0] == '\'')
8283             {
8284                if((int)((byte *)constant)[1] > 127)
8285                {
8286                   int nb;
8287                   unichar ch = UTF8GetChar(constant + 1, &nb);
8288                   if(nb < 2) ch = constant[1];
8289                   delete constant;
8290                   exp.constant = PrintUInt(ch);
8291                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8292                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8293                   type._class = FindClass("unichar");
8294
8295                   type.isSigned = false;
8296                }
8297                else
8298                {
8299                   type.kind = charType;
8300                   type.isSigned = true;
8301                }
8302             }
8303             else
8304             {
8305                char * dot = strchr(constant, '.');
8306                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8307                char * exponent;
8308                if(isHex)
8309                {
8310                   exponent = strchr(constant, 'p');
8311                   if(!exponent) exponent = strchr(constant, 'P');
8312                }
8313                else
8314                {
8315                   exponent = strchr(constant, 'e');
8316                   if(!exponent) exponent = strchr(constant, 'E');
8317                }
8318
8319                if(dot || exponent)
8320                {
8321                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8322                      type.kind = floatType;
8323                   else
8324                      type.kind = doubleType;
8325                   type.isSigned = true;
8326                }
8327                else
8328                {
8329                   bool isSigned = constant[0] == '-';
8330                   char * endP = null;
8331                   int64 i64 = strtoll(constant, &endP, 0);
8332                   uint64 ui64 = strtoull(constant, &endP, 0);
8333                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
8334                   if(isSigned)
8335                   {
8336                      if(i64 < MININT)
8337                         is64Bit = true;
8338                   }
8339                   else
8340                   {
8341                      if(ui64 > MAXINT)
8342                      {
8343                         if(ui64 > MAXDWORD)
8344                         {
8345                            is64Bit = true;
8346                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8347                               isSigned = true;
8348                         }
8349                      }
8350                      else if(constant[0] != '0' || !constant[1])
8351                         isSigned = true;
8352                   }
8353                   type.kind = is64Bit ? int64Type : intType;
8354                   type.isSigned = isSigned;
8355                }
8356             }
8357             exp.isConstant = true;
8358             if(exp.destType && exp.destType.kind == doubleType)
8359                type.kind = doubleType;
8360             else if(exp.destType && exp.destType.kind == floatType)
8361                type.kind = floatType;
8362             else if(exp.destType && exp.destType.kind == int64Type)
8363                type.kind = int64Type;
8364          }
8365          break;
8366       }
8367       case stringExp:
8368       {
8369          exp.isConstant = true;      // Why wasn't this constant?
8370          exp.expType = Type
8371          {
8372             refCount = 1;
8373             kind = pointerType;
8374             type = Type
8375             {
8376                refCount = 1;
8377                kind = exp.wideString ? shortType : charType;
8378                constant = true;
8379                isSigned = exp.wideString ? false : true;
8380             }
8381          };
8382          break;
8383       }
8384       case newExp:
8385       case new0Exp:
8386          ProcessExpressionType(exp._new.size);
8387          exp.expType = Type
8388          {
8389             refCount = 1;
8390             kind = pointerType;
8391             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8392          };
8393          DeclareType(exp.expType.type, false, false);
8394          break;
8395       case renewExp:
8396       case renew0Exp:
8397          ProcessExpressionType(exp._renew.size);
8398          ProcessExpressionType(exp._renew.exp);
8399          exp.expType = Type
8400          {
8401             refCount = 1;
8402             kind = pointerType;
8403             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8404          };
8405          DeclareType(exp.expType.type, false, false);
8406          break;
8407       case opExp:
8408       {
8409          bool assign = false, boolResult = false, boolOps = false;
8410          Type type1 = null, type2 = null;
8411          bool useDestType = false, useSideType = false;
8412          Location oldyylloc = yylloc;
8413          bool useSideUnit = false;
8414          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8415
8416          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8417          Type dummy
8418          {
8419             count = 1;
8420             refCount = 1;
8421          };
8422
8423          switch(exp.op.op)
8424          {
8425             // Assignment Operators
8426             case '=':
8427             case MUL_ASSIGN:
8428             case DIV_ASSIGN:
8429             case MOD_ASSIGN:
8430             case ADD_ASSIGN:
8431             case SUB_ASSIGN:
8432             case LEFT_ASSIGN:
8433             case RIGHT_ASSIGN:
8434             case AND_ASSIGN:
8435             case XOR_ASSIGN:
8436             case OR_ASSIGN:
8437                assign = true;
8438                break;
8439             // boolean Operators
8440             case '!':
8441                // Expect boolean operators
8442                //boolOps = true;
8443                //boolResult = true;
8444                break;
8445             case AND_OP:
8446             case OR_OP:
8447                // Expect boolean operands
8448                boolOps = true;
8449                boolResult = true;
8450                break;
8451             // Comparisons
8452             case EQ_OP:
8453             case '<':
8454             case '>':
8455             case LE_OP:
8456             case GE_OP:
8457             case NE_OP:
8458                // Gives boolean result
8459                boolResult = true;
8460                useSideType = true;
8461                break;
8462             case '+':
8463             case '-':
8464                useSideUnit = true;
8465                useSideType = true;
8466                useDestType = true;
8467                break;
8468
8469             case LEFT_OP:
8470             case RIGHT_OP:
8471                useSideType = true;
8472                useDestType = true;
8473                break;
8474
8475             case '|':
8476             case '^':
8477                useSideType = true;
8478                useDestType = true;
8479                break;
8480
8481             case '/':
8482             case '%':
8483                useSideType = true;
8484                useDestType = true;
8485                break;
8486             case '&':
8487             case '*':
8488                if(exp.op.exp1)
8489                {
8490                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8491                   useSideType = true;
8492                   useDestType = true;
8493                }
8494                break;
8495
8496             /*// Implement speed etc.
8497             case '*':
8498             case '/':
8499                break;
8500             */
8501          }
8502          if(exp.op.op == '&')
8503          {
8504             // Added this here earlier for Iterator address as key
8505             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8506             {
8507                Identifier id = exp.op.exp2.identifier;
8508                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8509                if(symbol && symbol.isIterator == 2)
8510                {
8511                   exp.type = memberExp;
8512                   exp.member.exp = exp.op.exp2;
8513                   exp.member.member = MkIdentifier("key");
8514                   exp.expType = null;
8515                   exp.op.exp2.expType = symbol.type;
8516                   symbol.type.refCount++;
8517                   ProcessExpressionType(exp);
8518                   FreeType(dummy);
8519                   break;
8520                }
8521                // exp.op.exp2.usage.usageRef = true;
8522             }
8523          }
8524
8525          //dummy.kind = TypeDummy;
8526          if(exp.op.exp1)
8527          {
8528             // Added this check here to use the dest type only for units derived from the base unit
8529             // So that untyped units will use the side unit as opposed to the untyped destination unit
8530             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8531             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8532                useDestType = false;
8533
8534             if(destClass && useDestType &&
8535               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8536
8537               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8538             {
8539                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8540                exp.op.exp1.destType = exp.destType;
8541                exp.op.exp1.opDestType = true;
8542                if(exp.destType)
8543                   exp.destType.refCount++;
8544             }
8545             else if(!assign)
8546             {
8547                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8548                exp.op.exp1.destType = dummy;
8549                dummy.refCount++;
8550             }
8551
8552             // TESTING THIS HERE...
8553             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8554                ProcessExpressionType(exp.op.exp1);
8555             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8556
8557             exp.op.exp1.opDestType = false;
8558
8559             // Fix for unit and ++ / --
8560             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8561                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8562             {
8563                exp.op.exp2 = MkExpConstant("1");
8564                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8565                assign = true;
8566             }
8567
8568             if(exp.op.exp1.destType == dummy)
8569             {
8570                FreeType(dummy);
8571                exp.op.exp1.destType = null;
8572             }
8573             type1 = exp.op.exp1.expType;
8574          }
8575
8576          if(exp.op.exp2)
8577          {
8578             char expString[10240];
8579             expString[0] = '\0';
8580             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8581             {
8582                if(exp.op.exp1)
8583                {
8584                   exp.op.exp2.destType = exp.op.exp1.expType;
8585                   if(exp.op.exp1.expType)
8586                      exp.op.exp1.expType.refCount++;
8587                }
8588                else
8589                {
8590                   exp.op.exp2.destType = exp.destType;
8591                   if(!exp.op.exp1 || exp.op.op != '&')
8592                      exp.op.exp2.opDestType = true;
8593                   if(exp.destType)
8594                      exp.destType.refCount++;
8595                }
8596
8597                if(type1) type1.refCount++;
8598                exp.expType = type1;
8599             }
8600             else if(assign)
8601             {
8602                if(inCompiler)
8603                   PrintExpression(exp.op.exp2, expString);
8604
8605                if(type1 && type1.kind == pointerType)
8606                {
8607                   if(exp.op.op == MUL_ASSIGN || exp.op.op == DIV_ASSIGN ||exp.op.op == MOD_ASSIGN ||exp.op.op == LEFT_ASSIGN ||exp.op.op == RIGHT_ASSIGN ||
8608                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8609                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8610                   else if(exp.op.op == '=')
8611                   {
8612                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8613                      exp.op.exp2.destType = type1;
8614                      if(type1)
8615                         type1.refCount++;
8616                   }
8617                }
8618                else
8619                {
8620                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8621                   if(exp.op.op == MUL_ASSIGN || exp.op.op == DIV_ASSIGN ||exp.op.op == MOD_ASSIGN ||exp.op.op == LEFT_ASSIGN ||exp.op.op == RIGHT_ASSIGN/* ||
8622                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8623                   else
8624                   {
8625                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8626                      exp.op.exp2.destType = type1;
8627                      if(type1)
8628                         type1.refCount++;
8629                   }
8630                }
8631                if(type1) type1.refCount++;
8632                exp.expType = type1;
8633             }
8634             else if(destClass &&
8635                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8636                   (destClass.type == enumClass && useDestType)))
8637             {
8638                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8639                exp.op.exp2.destType = exp.destType;
8640                if(exp.op.op != '&')
8641                   exp.op.exp2.opDestType = true;
8642                if(exp.destType)
8643                   exp.destType.refCount++;
8644             }
8645             else
8646             {
8647                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8648                exp.op.exp2.destType = dummy;
8649                dummy.refCount++;
8650             }
8651
8652             // TESTING THIS HERE... (DANGEROUS)
8653             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8654                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8655             {
8656                FreeType(exp.op.exp2.destType);
8657                exp.op.exp2.destType = type1;
8658                type1.refCount++;
8659             }
8660             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8661             // Cannot lose the cast on a sizeof
8662             if(exp.op.op == SIZEOF)
8663             {
8664                Expression e = exp.op.exp2;
8665                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8666                {
8667                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8668                   {
8669                      if(e.type == extensionCompoundExp)
8670                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8671                      else
8672                         e = e.list->last;
8673                   }
8674                }
8675                if(e.type == castExp && e.cast.exp)
8676                   e.cast.exp.needCast = true;
8677             }
8678             ProcessExpressionType(exp.op.exp2);
8679             exp.op.exp2.opDestType = false;
8680             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8681
8682             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8683             {
8684                if(exp.op.exp2.expType.kind == intSizeType || exp.op.exp2.expType.kind == intPtrType || exp.op.exp2.expType.kind == int64Type || exp.op.exp2.expType.kind == intType || exp.op.exp2.expType.kind == shortType || exp.op.exp2.expType.kind == charType)
8685                {
8686                   if(exp.op.op != '=' && type1.type.kind == voidType)
8687                      Compiler_Error($"void *: unknown size\n");
8688                }
8689                else if(exp.op.exp2.expType.kind == pointerType || exp.op.exp2.expType.kind == arrayType || exp.op.exp2.expType.kind == functionType || exp.op.exp2.expType.kind == methodType||
8690                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8691                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8692                               exp.op.exp2.expType._class.registered.type == structClass ||
8693                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8694                {
8695                   if(exp.op.op == ADD_ASSIGN)
8696                      Compiler_Error($"cannot add two pointers\n");
8697                }
8698                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8699                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8700                {
8701                   if(exp.op.op == ADD_ASSIGN)
8702                      Compiler_Error($"cannot add two pointers\n");
8703                }
8704                else if(inCompiler)
8705                {
8706                   char type1String[1024];
8707                   char type2String[1024];
8708                   type1String[0] = '\0';
8709                   type2String[0] = '\0';
8710
8711                   PrintType(exp.op.exp2.expType, type1String, false, true);
8712                   PrintType(type1, type2String, false, true);
8713                   ChangeCh(expString, '\n', ' ');
8714                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8715                }
8716             }
8717
8718             if(exp.op.exp2.destType == dummy)
8719             {
8720                FreeType(dummy);
8721                exp.op.exp2.destType = null;
8722             }
8723
8724             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8725             {
8726                type2 = { };
8727                type2.refCount = 1;
8728                CopyTypeInto(type2, exp.op.exp2.expType);
8729                type2.isSigned = true;
8730             }
8731             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8732             {
8733                type2 = { kind = intType };
8734                type2.refCount = 1;
8735                type2.isSigned = true;
8736             }
8737             else
8738             {
8739                type2 = exp.op.exp2.expType;
8740                if(type2) type2.refCount++;
8741             }
8742          }
8743
8744          dummy.kind = voidType;
8745
8746          if(exp.op.op == SIZEOF)
8747          {
8748             exp.expType = Type
8749             {
8750                refCount = 1;
8751                kind = intSizeType;
8752             };
8753             exp.isConstant = true;
8754          }
8755          // Get type of dereferenced pointer
8756          else if(exp.op.op == '*' && !exp.op.exp1)
8757          {
8758             exp.expType = Dereference(type2);
8759             if(type2 && type2.kind == classType)
8760                notByReference = true;
8761          }
8762          else if(exp.op.op == '&' && !exp.op.exp1)
8763             exp.expType = Reference(type2);
8764          else if(!assign)
8765          {
8766             if(boolOps)
8767             {
8768                if(exp.op.exp1)
8769                {
8770                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8771                   exp.op.exp1.destType = MkClassType("bool");
8772                   exp.op.exp1.destType.truth = true;
8773                   if(!exp.op.exp1.expType)
8774                      ProcessExpressionType(exp.op.exp1);
8775                   else
8776                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8777                   FreeType(exp.op.exp1.expType);
8778                   exp.op.exp1.expType = MkClassType("bool");
8779                   exp.op.exp1.expType.truth = true;
8780                }
8781                if(exp.op.exp2)
8782                {
8783                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8784                   exp.op.exp2.destType = MkClassType("bool");
8785                   exp.op.exp2.destType.truth = true;
8786                   if(!exp.op.exp2.expType)
8787                      ProcessExpressionType(exp.op.exp2);
8788                   else
8789                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8790                   FreeType(exp.op.exp2.expType);
8791                   exp.op.exp2.expType = MkClassType("bool");
8792                   exp.op.exp2.expType.truth = true;
8793                }
8794             }
8795             else if(exp.op.exp1 && exp.op.exp2 &&
8796                ((useSideType /*&&
8797                      (useSideUnit ||
8798                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8799                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8800                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8801                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8802             {
8803                if(type1 && type2 &&
8804                   // If either both are class or both are not class
8805                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8806                {
8807                   // Added this check for enum subtraction to result in an int type:
8808                   if(exp.op.op == '-' &&
8809                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8810                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8811                   {
8812                      Type intType;
8813                      if(!type1._class.registered.dataType)
8814                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8815                      if(!type2._class.registered.dataType)
8816                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8817
8818                      intType = ProcessTypeString(
8819                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8820
8821                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8822                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8823                      exp.op.exp1.destType = intType;
8824                      exp.op.exp2.destType = intType;
8825                      intType.refCount++;
8826                   }
8827                   else
8828                   {
8829                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8830                      exp.op.exp2.destType = type1;
8831                      type1.refCount++;
8832                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8833                      exp.op.exp1.destType = type2;
8834                      type2.refCount++;
8835                   }
8836
8837                   // Warning here for adding Radians + Degrees with no destination type
8838                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8839                      type1._class.registered && type1._class.registered.type == unitClass &&
8840                      type2._class.registered && type2._class.registered.type == unitClass &&
8841                      type1._class.registered != type2._class.registered)
8842                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8843                         type1._class.string, type2._class.string, type1._class.string);
8844
8845                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8846                   {
8847                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8848                      if(argExp)
8849                      {
8850                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8851
8852                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8853                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8854                            exp.op.exp1)));
8855
8856                         ProcessExpressionType(exp.op.exp1);
8857
8858                         if(type2.kind != pointerType)
8859                         {
8860                            ProcessExpressionType(classExp);
8861
8862                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8863
8864                            if(!exp.op.exp2.expType)
8865                            {
8866                               if(type2)
8867                                  FreeType(type2);
8868                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8869                               type2.refCount++;
8870                            }
8871
8872                            ProcessExpressionType(exp.op.exp2);
8873                         }
8874                      }
8875                   }
8876
8877                   if(!boolResult && ((type1.kind == pointerType || type1.kind == arrayType || (type1.kind == classType && !strcmp(type1._class.string, "String"))) && (type2.kind == intSizeType || type2.kind == intPtrType || type2.kind == int64Type || type2.kind == intType || type2.kind == shortType || type2.kind == charType)))
8878                   {
8879                      if(type1.kind != classType && type1.type.kind == voidType)
8880                         Compiler_Error($"void *: unknown size\n");
8881                      exp.expType = type1;
8882                      if(type1) type1.refCount++;
8883                   }
8884                   else if(!boolResult && ((type2.kind == pointerType || type2.kind == arrayType || (type2.kind == classType && !strcmp(type2._class.string, "String"))) && (type1.kind == intSizeType || type1.kind == intPtrType || type1.kind == int64Type || type1.kind == intType || type1.kind == shortType || type1.kind == charType)))
8885                   {
8886                      if(type2.kind != classType && type2.type.kind == voidType)
8887                         Compiler_Error($"void *: unknown size\n");
8888                      exp.expType = type2;
8889                      if(type2) type2.refCount++;
8890                   }
8891                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8892                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8893                   {
8894                      Compiler_Warning($"different levels of indirection\n");
8895                   }
8896                   else
8897                   {
8898                      bool success = false;
8899                      if(type1.kind == pointerType && type2.kind == pointerType)
8900                      {
8901                         if(exp.op.op == '+')
8902                            Compiler_Error($"cannot add two pointers\n");
8903                         else if(exp.op.op == '-')
8904                         {
8905                            // Pointer Subtraction gives integer
8906                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8907                            {
8908                               exp.expType = Type
8909                               {
8910                                  kind = intType;
8911                                  refCount = 1;
8912                               };
8913                               success = true;
8914
8915                               if(type1.type.kind == templateType)
8916                               {
8917                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8918                                  if(argExp)
8919                                  {
8920                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8921
8922                                     ProcessExpressionType(classExp);
8923
8924                                     exp.type = bracketsExp;
8925                                     exp.list = MkListOne(MkExpOp(
8926                                        MkExpBrackets(MkListOne(MkExpOp(
8927                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8928                                              , exp.op.op,
8929                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8930                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8931
8932                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8933                                     FreeType(dummy);
8934                                     return;
8935                                  }
8936                               }
8937                            }
8938                         }
8939                      }
8940
8941                      if(!success && exp.op.exp1.type == constantExp)
8942                      {
8943                         // If first expression is constant, try to match that first
8944                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8945                         {
8946                            if(exp.expType) FreeType(exp.expType);
8947                            exp.expType = exp.op.exp1.destType;
8948                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8949                            success = true;
8950                         }
8951                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8952                         {
8953                            if(exp.expType) FreeType(exp.expType);
8954                            exp.expType = exp.op.exp2.destType;
8955                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8956                            success = true;
8957                         }
8958                      }
8959                      else if(!success)
8960                      {
8961                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8962                         {
8963                            if(exp.expType) FreeType(exp.expType);
8964                            exp.expType = exp.op.exp2.destType;
8965                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8966                            success = true;
8967                         }
8968                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8969                         {
8970                            if(exp.expType) FreeType(exp.expType);
8971                            exp.expType = exp.op.exp1.destType;
8972                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8973                            success = true;
8974                         }
8975                      }
8976                      if(!success)
8977                      {
8978                         char expString1[10240];
8979                         char expString2[10240];
8980                         char type1[1024];
8981                         char type2[1024];
8982                         expString1[0] = '\0';
8983                         expString2[0] = '\0';
8984                         type1[0] = '\0';
8985                         type2[0] = '\0';
8986                         if(inCompiler)
8987                         {
8988                            PrintExpression(exp.op.exp1, expString1);
8989                            ChangeCh(expString1, '\n', ' ');
8990                            PrintExpression(exp.op.exp2, expString2);
8991                            ChangeCh(expString2, '\n', ' ');
8992                            PrintType(exp.op.exp1.expType, type1, false, true);
8993                            PrintType(exp.op.exp2.expType, type2, false, true);
8994                         }
8995
8996                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8997                      }
8998                   }
8999                }
9000                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
9001                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9002                {
9003                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9004                   // Convert e.g. / 4 into / 4.0
9005                   exp.op.exp1.destType = type2._class.registered.dataType;
9006                   if(type2._class.registered.dataType)
9007                      type2._class.registered.dataType.refCount++;
9008                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9009                   exp.expType = type2;
9010                   if(type2) type2.refCount++;
9011                }
9012                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9013                {
9014                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9015                   // Convert e.g. / 4 into / 4.0
9016                   exp.op.exp2.destType = type1._class.registered.dataType;
9017                   if(type1._class.registered.dataType)
9018                      type1._class.registered.dataType.refCount++;
9019                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9020                   exp.expType = type1;
9021                   if(type1) type1.refCount++;
9022                }
9023                else if(type1)
9024                {
9025                   bool valid = false;
9026
9027                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9028                   {
9029                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9030
9031                      if(!type1._class.registered.dataType)
9032                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
9033                      exp.op.exp2.destType = type1._class.registered.dataType;
9034                      exp.op.exp2.destType.refCount++;
9035
9036                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9037                      if(type2)
9038                         FreeType(type2);
9039                      type2 = exp.op.exp2.destType;
9040                      if(type2) type2.refCount++;
9041
9042                      exp.expType = type2;
9043                      type2.refCount++;
9044                   }
9045
9046                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9047                   {
9048                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9049
9050                      if(!type2._class.registered.dataType)
9051                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
9052                      exp.op.exp1.destType = type2._class.registered.dataType;
9053                      exp.op.exp1.destType.refCount++;
9054
9055                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9056                      type1 = exp.op.exp1.destType;
9057                      exp.expType = type1;
9058                      type1.refCount++;
9059                   }
9060
9061                   // TESTING THIS NEW CODE
9062                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
9063                   {
9064                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
9065                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
9066                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
9067                      {
9068                         // Convert the enum to an int instead for these operators
9069                         if(op1IsEnum && exp.op.exp2.expType)
9070                         {
9071                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
9072                            {
9073                               if(exp.expType) FreeType(exp.expType);
9074                               exp.expType = exp.op.exp2.expType;
9075                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9076                               valid = true;
9077                            }
9078                         }
9079                         else if(op2IsEnum && exp.op.exp1.expType)
9080                         {
9081                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
9082                            {
9083                               if(exp.expType) FreeType(exp.expType);
9084                               exp.expType = exp.op.exp1.expType;
9085                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9086                               valid = true;
9087                            }
9088                         }
9089                      }
9090                      else
9091                      {
9092                         if(op1IsEnum && exp.op.exp2.expType)
9093                         {
9094                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
9095                            {
9096                               if(exp.expType) FreeType(exp.expType);
9097                               exp.expType = exp.op.exp1.expType;
9098                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9099                               valid = true;
9100                            }
9101                         }
9102                         else if(op2IsEnum && exp.op.exp1.expType)
9103                         {
9104                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
9105                            {
9106                               if(exp.expType) FreeType(exp.expType);
9107                               exp.expType = exp.op.exp2.expType;
9108                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9109                               valid = true;
9110                            }
9111                         }
9112                      }
9113                   }
9114
9115                   if(!valid)
9116                   {
9117                      // Added this first part of the if here to handle  5 + Degrees { 5 } with either a base unit dest or not a unit dest type
9118                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
9119                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
9120                      {
9121                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9122                         exp.op.exp1.destType = type2;
9123                         type2.refCount++;
9124
9125                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9126                         {
9127                            if(exp.expType) FreeType(exp.expType);
9128                            exp.expType = exp.op.exp1.destType;
9129                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9130                         }
9131                      }
9132                      else
9133                      {
9134                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9135                         exp.op.exp2.destType = type1;
9136                         type1.refCount++;
9137
9138                      /*
9139                      // Maybe this was meant to be an enum...
9140                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9141                      {
9142                         Type oldType = exp.op.exp2.expType;
9143                         exp.op.exp2.expType = null;
9144                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
9145                            FreeType(oldType);
9146                         else
9147                            exp.op.exp2.expType = oldType;
9148                      }
9149                      */
9150
9151                      /*
9152                      // TESTING THIS HERE... LATEST ADDITION
9153                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9154                      {
9155                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9156                         exp.op.exp2.destType = type2._class.registered.dataType;
9157                         if(type2._class.registered.dataType)
9158                            type2._class.registered.dataType.refCount++;
9159                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
9160
9161                         //exp.expType = type2._class.registered.dataType; //type2;
9162                         //if(type2) type2.refCount++;
9163                      }
9164
9165                      // TESTING THIS HERE... LATEST ADDITION
9166                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9167                      {
9168                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9169                         exp.op.exp1.destType = type1._class.registered.dataType;
9170                         if(type1._class.registered.dataType)
9171                            type1._class.registered.dataType.refCount++;
9172                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
9173                         exp.expType = type1._class.registered.dataType; //type1;
9174                         if(type1) type1.refCount++;
9175                      }
9176                      */
9177
9178                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
9179                         {
9180                            if(exp.expType) FreeType(exp.expType);
9181                            exp.expType = exp.op.exp2.destType;
9182                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
9183                         }
9184                         else if(type1 && type2)
9185                         {
9186                            char expString1[10240];
9187                            char expString2[10240];
9188                            char type1String[1024];
9189                            char type2String[1024];
9190                            expString1[0] = '\0';
9191                            expString2[0] = '\0';
9192                            type1String[0] = '\0';
9193                            type2String[0] = '\0';
9194                            if(inCompiler)
9195                            {
9196                               PrintExpression(exp.op.exp1, expString1);
9197                               ChangeCh(expString1, '\n', ' ');
9198                               PrintExpression(exp.op.exp2, expString2);
9199                               ChangeCh(expString2, '\n', ' ');
9200                               PrintType(exp.op.exp1.expType, type1String, false, true);
9201                               PrintType(exp.op.exp2.expType, type2String, false, true);
9202                            }
9203
9204                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9205
9206                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9207                            {
9208                               exp.expType = exp.op.exp1.expType;
9209                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9210                            }
9211                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9212                            {
9213                               exp.expType = exp.op.exp2.expType;
9214                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9215                            }
9216                         }
9217                      }
9218                   }
9219                }
9220                else if(type2)
9221                {
9222                   // Maybe this was meant to be an enum...
9223                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9224                   {
9225                      Type oldType = exp.op.exp1.expType;
9226                      exp.op.exp1.expType = null;
9227                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9228                         FreeType(oldType);
9229                      else
9230                         exp.op.exp1.expType = oldType;
9231                   }
9232
9233                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9234                   exp.op.exp1.destType = type2;
9235                   type2.refCount++;
9236                   /*
9237                   // TESTING THIS HERE... LATEST ADDITION
9238                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9239                   {
9240                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9241                      exp.op.exp1.destType = type1._class.registered.dataType;
9242                      if(type1._class.registered.dataType)
9243                         type1._class.registered.dataType.refCount++;
9244                   }
9245
9246                   // TESTING THIS HERE... LATEST ADDITION
9247                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9248                   {
9249                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9250                      exp.op.exp2.destType = type2._class.registered.dataType;
9251                      if(type2._class.registered.dataType)
9252                         type2._class.registered.dataType.refCount++;
9253                   }
9254                   */
9255
9256                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9257                   {
9258                      if(exp.expType) FreeType(exp.expType);
9259                      exp.expType = exp.op.exp1.destType;
9260                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9261                   }
9262                }
9263             }
9264             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9265             {
9266                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9267                {
9268                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9269                   // Convert e.g. / 4 into / 4.0
9270                   exp.op.exp1.destType = type2._class.registered.dataType;
9271                   if(type2._class.registered.dataType)
9272                      type2._class.registered.dataType.refCount++;
9273                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9274                }
9275                if(exp.op.op == '!')
9276                {
9277                   exp.expType = MkClassType("bool");
9278                   exp.expType.truth = true;
9279                }
9280                else
9281                {
9282                   exp.expType = type2;
9283                   if(type2) type2.refCount++;
9284                }
9285             }
9286             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9287             {
9288                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9289                {
9290                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9291                   // Convert e.g. / 4 into / 4.0
9292                   exp.op.exp2.destType = type1._class.registered.dataType;
9293                   if(type1._class.registered.dataType)
9294                      type1._class.registered.dataType.refCount++;
9295                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9296                }
9297                exp.expType = type1;
9298                if(type1) type1.refCount++;
9299             }
9300          }
9301
9302          yylloc = exp.loc;
9303          if(exp.op.exp1 && !exp.op.exp1.expType)
9304          {
9305             char expString[10000];
9306             expString[0] = '\0';
9307             if(inCompiler)
9308             {
9309                PrintExpression(exp.op.exp1, expString);
9310                ChangeCh(expString, '\n', ' ');
9311             }
9312             if(expString[0])
9313                Compiler_Error($"couldn't determine type of %s\n", expString);
9314          }
9315          if(exp.op.exp2 && !exp.op.exp2.expType)
9316          {
9317             char expString[10240];
9318             expString[0] = '\0';
9319             if(inCompiler)
9320             {
9321                PrintExpression(exp.op.exp2, expString);
9322                ChangeCh(expString, '\n', ' ');
9323             }
9324             if(expString[0])
9325                Compiler_Error($"couldn't determine type of %s\n", expString);
9326          }
9327
9328          if(boolResult)
9329          {
9330             FreeType(exp.expType);
9331             exp.expType = MkClassType("bool");
9332             exp.expType.truth = true;
9333          }
9334
9335          if(exp.op.op != SIZEOF)
9336             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9337                (!exp.op.exp2 || exp.op.exp2.isConstant);
9338
9339          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9340          {
9341             DeclareType(exp.op.exp2.expType, false, false);
9342          }
9343
9344          if(exp.op.op == DELETE && exp.op.exp2 && exp.op.exp2.expType && exp.op.exp2.expType.specConst)
9345             Compiler_Warning($"deleting const qualified object\n");
9346
9347          yylloc = oldyylloc;
9348
9349          FreeType(dummy);
9350          if(type2)
9351             FreeType(type2);
9352          break;
9353       }
9354       case bracketsExp:
9355       case extensionExpressionExp:
9356       {
9357          Expression e;
9358          exp.isConstant = true;
9359          for(e = exp.list->first; e; e = e.next)
9360          {
9361             //bool inced = false;
9362             if(!e.next)
9363             {
9364                FreeType(e.destType);
9365                e.opDestType = exp.opDestType;
9366                e.destType = exp.destType;
9367                if(e.destType) { exp.destType.refCount++; /*e.destType.count++; inced = true;*/ }
9368             }
9369             ProcessExpressionType(e);
9370             /*if(inced)
9371                exp.destType.count--;*/
9372             if(!exp.expType && !e.next)
9373             {
9374                exp.expType = e.expType;
9375                if(e.expType) e.expType.refCount++;
9376             }
9377             if(!e.isConstant)
9378                exp.isConstant = false;
9379          }
9380
9381          // In case a cast became a member...
9382          e = exp.list->first;
9383          if(!e.next && e.type == memberExp)
9384          {
9385             // Preserve prev, next
9386             Expression next = exp.next, prev = exp.prev;
9387
9388
9389             FreeType(exp.expType);
9390             FreeType(exp.destType);
9391             delete exp.list;
9392
9393             *exp = *e;
9394
9395             exp.prev = prev;
9396             exp.next = next;
9397
9398             delete e;
9399
9400             ProcessExpressionType(exp);
9401          }
9402          break;
9403       }
9404       case indexExp:
9405       {
9406          Expression e;
9407          exp.isConstant = true;
9408
9409          ProcessExpressionType(exp.index.exp);
9410          if(!exp.index.exp.isConstant)
9411             exp.isConstant = false;
9412
9413          if(exp.index.exp.expType)
9414          {
9415             Type source = exp.index.exp.expType;
9416             if(source.kind == classType && source._class && source._class.registered)
9417             {
9418                Class _class = source._class.registered;
9419                Class c = _class.templateClass ? _class.templateClass : _class;
9420                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9421                {
9422                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9423
9424                   if(exp.index.index && exp.index.index->last)
9425                   {
9426                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9427
9428                      if(type.kind == classType) type.constant = true;
9429                      else if(type.kind == pointerType)
9430                      {
9431                         Type t = type;
9432                         while(t.kind == pointerType) t = t.type;
9433                         t.constant = true;
9434                      }
9435
9436                      ((Expression)exp.index.index->last).destType = type;
9437                   }
9438                }
9439             }
9440          }
9441
9442          for(e = exp.index.index->first; e; e = e.next)
9443          {
9444             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9445             {
9446                if(e.destType) FreeType(e.destType);
9447                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9448             }
9449             ProcessExpressionType(e);
9450             if(!e.next)
9451             {
9452                // Check if this type is int
9453             }
9454             if(!e.isConstant)
9455                exp.isConstant = false;
9456          }
9457
9458          if(!exp.expType)
9459             exp.expType = Dereference(exp.index.exp.expType);
9460          if(exp.expType)
9461             DeclareType(exp.expType, false, false);
9462          break;
9463       }
9464       case callExp:
9465       {
9466          Expression e;
9467          Type functionType;
9468          Type methodType = null;
9469          char name[1024];
9470          name[0] = '\0';
9471
9472          if(inCompiler)
9473          {
9474             PrintExpression(exp.call.exp,  name);
9475             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9476             {
9477                //exp.call.exp.expType = null;
9478                PrintExpression(exp.call.exp,  name);
9479             }
9480          }
9481          if(exp.call.exp.type == identifierExp)
9482          {
9483             Expression idExp = exp.call.exp;
9484             Identifier id = idExp.identifier;
9485             if(!strcmp(id.string, "__builtin_frame_address"))
9486             {
9487                exp.expType = ProcessTypeString("void *", true);
9488                if(exp.call.arguments && exp.call.arguments->first)
9489                   ProcessExpressionType(exp.call.arguments->first);
9490                break;
9491             }
9492             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9493             {
9494                exp.expType = ProcessTypeString("int", true);
9495                if(exp.call.arguments && exp.call.arguments->first)
9496                   ProcessExpressionType(exp.call.arguments->first);
9497                break;
9498             }
9499             else if(!strcmp(id.string, "Max") ||
9500                !strcmp(id.string, "Min") ||
9501                !strcmp(id.string, "Sgn") ||
9502                !strcmp(id.string, "Abs"))
9503             {
9504                Expression a = null;
9505                Expression b = null;
9506                Expression tempExp1 = null, tempExp2 = null;
9507                if((!strcmp(id.string, "Max") ||
9508                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9509                {
9510                   a = exp.call.arguments->first;
9511                   b = exp.call.arguments->last;
9512                   tempExp1 = a;
9513                   tempExp2 = b;
9514                }
9515                else if(exp.call.arguments->count == 1)
9516                {
9517                   a = exp.call.arguments->first;
9518                   tempExp1 = a;
9519                }
9520
9521                if(a)
9522                {
9523                   exp.call.arguments->Clear();
9524                   idExp.identifier = null;
9525
9526                   FreeExpContents(exp);
9527
9528                   ProcessExpressionType(a);
9529                   if(b)
9530                      ProcessExpressionType(b);
9531
9532                   exp.type = bracketsExp;
9533                   exp.list = MkList();
9534
9535                   if(a.expType && (!b || b.expType))
9536                   {
9537                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9538                      {
9539                         // Use the simpleStruct name/ids for now...
9540                         if(inCompiler)
9541                         {
9542                            OldList * specs = MkList();
9543                            OldList * decls = MkList();
9544                            Declaration decl;
9545                            char temp1[1024], temp2[1024];
9546
9547                            GetTypeSpecs(a.expType, specs);
9548
9549                            if(a && !a.isConstant && a.type != identifierExp)
9550                            {
9551                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9552                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9553                               tempExp1 = QMkExpId(temp1);
9554                               tempExp1.expType = a.expType;
9555                               if(a.expType)
9556                                  a.expType.refCount++;
9557                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9558                            }
9559                            if(b && !b.isConstant && b.type != identifierExp)
9560                            {
9561                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9562                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9563                               tempExp2 = QMkExpId(temp2);
9564                               tempExp2.expType = b.expType;
9565                               if(b.expType)
9566                                  b.expType.refCount++;
9567                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9568                            }
9569
9570                            decl = MkDeclaration(specs, decls);
9571                            if(!curCompound.compound.declarations)
9572                               curCompound.compound.declarations = MkList();
9573                            curCompound.compound.declarations->Insert(null, decl);
9574                         }
9575                      }
9576                   }
9577
9578                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9579                   {
9580                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9581                      ListAdd(exp.list,
9582                         MkExpCondition(MkExpBrackets(MkListOne(
9583                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9584                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9585                      exp.expType = a.expType;
9586                      if(a.expType)
9587                         a.expType.refCount++;
9588                   }
9589                   else if(!strcmp(id.string, "Abs"))
9590                   {
9591                      ListAdd(exp.list,
9592                         MkExpCondition(MkExpBrackets(MkListOne(
9593                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9594                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9595                      exp.expType = a.expType;
9596                      if(a.expType)
9597                         a.expType.refCount++;
9598                   }
9599                   else if(!strcmp(id.string, "Sgn"))
9600                   {
9601                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9602                      ListAdd(exp.list,
9603                         MkExpCondition(MkExpBrackets(MkListOne(
9604                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9605                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9606                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9607                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9608                      exp.expType = ProcessTypeString("int", false);
9609                   }
9610
9611                   FreeExpression(tempExp1);
9612                   if(tempExp2) FreeExpression(tempExp2);
9613
9614                   FreeIdentifier(id);
9615                   break;
9616                }
9617             }
9618          }
9619
9620          {
9621             Type dummy
9622             {
9623                count = 1;
9624                refCount = 1;
9625             };
9626             if(!exp.call.exp.destType)
9627             {
9628                exp.call.exp.destType = dummy;
9629                dummy.refCount++;
9630             }
9631             ProcessExpressionType(exp.call.exp);
9632             if(exp.call.exp.destType == dummy)
9633             {
9634                FreeType(dummy);
9635                exp.call.exp.destType = null;
9636             }
9637             FreeType(dummy);
9638          }
9639
9640          // Check argument types against parameter types
9641          functionType = exp.call.exp.expType;
9642
9643          if(functionType && functionType.kind == TypeKind::methodType)
9644          {
9645             methodType = functionType;
9646             functionType = methodType.method.dataType;
9647
9648             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9649             // TOCHECK: Instead of doing this here could this be done per param?
9650             if(exp.call.exp.expType.usedClass)
9651             {
9652                char typeString[1024];
9653                typeString[0] = '\0';
9654                {
9655                   Symbol back = functionType.thisClass;
9656                   // Do not output class specifier here (thisclass was added to this)
9657                   functionType.thisClass = null;
9658                   PrintType(functionType, typeString, true, true);
9659                   functionType.thisClass = back;
9660                }
9661                if(strstr(typeString, "thisclass"))
9662                {
9663                   OldList * specs = MkList();
9664                   Declarator decl;
9665                   {
9666                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9667
9668                      decl = SpecDeclFromString(typeString, specs, null);
9669
9670                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9671                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9672                         exp.call.exp.expType.usedClass))
9673                         thisClassParams = false;
9674
9675                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9676                      {
9677                         Class backupThisClass = thisClass;
9678                         thisClass = exp.call.exp.expType.usedClass;
9679                         ProcessDeclarator(decl);
9680                         thisClass = backupThisClass;
9681                      }
9682
9683                      thisClassParams = true;
9684
9685                      functionType = ProcessType(specs, decl);
9686                      functionType.refCount = 0;
9687                      FinishTemplatesContext(context);
9688
9689                      // Mark parameters that were 'thisclass'
9690                      /*{
9691                         Type p, op;
9692                         for(p = functionType.params.first, op = methodType.method.dataType.params.first; p && op; p = p.next, op = op.next)
9693                            p.wasThisClass = op.kind == thisClassType;
9694                      }*/
9695                   }
9696
9697                   FreeList(specs, FreeSpecifier);
9698                   FreeDeclarator(decl);
9699                 }
9700             }
9701          }
9702          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9703          {
9704             Type type = functionType.type;
9705             if(!functionType.refCount)
9706             {
9707                functionType.type = null;
9708                FreeType(functionType);
9709             }
9710             //methodType = functionType;
9711             functionType = type;
9712          }
9713          if(functionType && functionType.kind != TypeKind::functionType)
9714          {
9715             Compiler_Error($"called object %s is not a function\n", name);
9716          }
9717          else if(functionType)
9718          {
9719             bool emptyParams = false, noParams = false;
9720             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9721             Type type = functionType.params.first;
9722             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9723             int extra = 0;
9724             Location oldyylloc = yylloc;
9725
9726             if(!type) emptyParams = true;
9727
9728             // WORKING ON THIS:
9729             if(functionType.extraParam && e && functionType.thisClass)
9730             {
9731                e.destType = MkClassType(functionType.thisClass.string);
9732                e = e.next;
9733             }
9734
9735             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9736             // Fixed #141 by adding '&& !functionType.extraParam'
9737             if(!functionType.staticMethod && !functionType.extraParam)
9738             {
9739                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9740                   memberExp.member.exp.expType._class)
9741                {
9742                   type = MkClassType(memberExp.member.exp.expType._class.string);
9743                   if(e)
9744                   {
9745                      e.destType = type;
9746                      e = e.next;
9747                      type = functionType.params.first;
9748                   }
9749                   else
9750                      type.refCount = 0;
9751                }
9752                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9753                {
9754                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9755                   type.byReference = functionType.byReference;
9756                   type.typedByReference = functionType.typedByReference;
9757                   if(e)
9758                   {
9759                      // Allow manually passing a class for typed object
9760                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9761                         e = e.next;
9762                      e.destType = type;
9763                      e = e.next;
9764                      type = functionType.params.first;
9765                   }
9766                   else
9767                      type.refCount = 0;
9768                   //extra = 1;
9769                }
9770             }
9771
9772             if(type && type.kind == voidType)
9773             {
9774                noParams = true;
9775                if(!type.refCount) FreeType(type);
9776                type = null;
9777             }
9778
9779             for( ; e; e = e.next)
9780             {
9781                if(!type && !emptyParams)
9782                {
9783                   yylloc = e.loc;
9784                   if(methodType && methodType.methodClass)
9785                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9786                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9787                         noParams ? 0 : functionType.params.count);
9788                   else
9789                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9790                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9791                         noParams ? 0 : functionType.params.count);
9792                   break;
9793                }
9794
9795                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9796                {
9797                   Type templatedType = null;
9798                   Class _class = methodType.usedClass;
9799                   ClassTemplateParameter curParam = null;
9800                   int id = 0;
9801                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9802                   {
9803                      Class sClass;
9804                      for(sClass = _class; sClass; sClass = sClass.base)
9805                      {
9806                         if(sClass.templateClass) sClass = sClass.templateClass;
9807                         id = 0;
9808                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9809                         {
9810                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9811                            {
9812                               Class nextClass;
9813                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9814                               {
9815                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9816                                  id += nextClass.templateParams.count;
9817                               }
9818                               break;
9819                            }
9820                            id++;
9821                         }
9822                         if(curParam) break;
9823                      }
9824                   }
9825                   if(curParam && _class.templateArgs[id].dataTypeString)
9826                   {
9827                      bool constant = type.constant;
9828                      ClassTemplateArgument arg = _class.templateArgs[id];
9829                      {
9830                         Context context = SetupTemplatesContext(_class);
9831
9832                         /*if(!arg.dataType)
9833                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9834                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9835                         FinishTemplatesContext(context);
9836                      }
9837
9838                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9839                      else if(templatedType.kind == pointerType)
9840                      {
9841                         Type t = templatedType.type;
9842                         while(t.kind == pointerType) t = t.type;
9843                         if(constant) t.constant = constant;
9844                      }
9845
9846                      e.destType = templatedType;
9847                      if(templatedType)
9848                      {
9849                         templatedType.passAsTemplate = true;
9850                         // templatedType.refCount++;
9851                      }
9852                   }
9853                   else
9854                   {
9855                      e.destType = type;
9856                      if(type) type.refCount++;
9857                   }
9858                }
9859                else
9860                {
9861                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9862                   {
9863                      e.destType = type.prev;
9864                      e.destType.refCount++;
9865                   }
9866                   else
9867                   {
9868                      e.destType = type;
9869                      if(type) type.refCount++;
9870                   }
9871                }
9872                // Don't reach the end for the ellipsis
9873                if(type && type.kind != ellipsisType)
9874                {
9875                   Type next = type.next;
9876                   if(!type.refCount) FreeType(type);
9877                   type = next;
9878                }
9879             }
9880
9881             if(type && type.kind != ellipsisType)
9882             {
9883                if(methodType && methodType.methodClass)
9884                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9885                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9886                      functionType.params.count + extra);
9887                else
9888                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9889                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9890                      functionType.params.count + extra);
9891             }
9892             yylloc = oldyylloc;
9893             if(type && !type.refCount) FreeType(type);
9894          }
9895          else
9896          {
9897             functionType = Type
9898             {
9899                refCount = 0;
9900                kind = TypeKind::functionType;
9901             };
9902
9903             if(exp.call.exp.type == identifierExp)
9904             {
9905                char * string = exp.call.exp.identifier.string;
9906                if(inCompiler)
9907                {
9908                   Symbol symbol;
9909                   Location oldyylloc = yylloc;
9910
9911                   yylloc = exp.call.exp.identifier.loc;
9912                   if(strstr(string, "__builtin_") == string)
9913                   {
9914                      if(exp.destType)
9915                      {
9916                         functionType.returnType = exp.destType;
9917                         exp.destType.refCount++;
9918                      }
9919                   }
9920                   else
9921                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9922                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9923                   globalContext.symbols.Add((BTNode)symbol);
9924                   if(strstr(symbol.string, "::"))
9925                      globalContext.hasNameSpace = true;
9926
9927                   yylloc = oldyylloc;
9928                }
9929             }
9930             else if(exp.call.exp.type == memberExp)
9931             {
9932                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9933                   exp.call.exp.member.member.string);*/
9934             }
9935             else
9936                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9937
9938             if(!functionType.returnType)
9939             {
9940                functionType.returnType = Type
9941                {
9942                   refCount = 1;
9943                   kind = intType;
9944                };
9945             }
9946          }
9947          if(functionType && functionType.kind == TypeKind::functionType)
9948          {
9949             exp.expType = functionType.returnType;
9950
9951             if(functionType.returnType)
9952                functionType.returnType.refCount++;
9953
9954             if(!functionType.refCount)
9955                FreeType(functionType);
9956          }
9957
9958          if(exp.call.arguments)
9959          {
9960             for(e = exp.call.arguments->first; e; e = e.next)
9961                ProcessExpressionType(e);
9962          }
9963          break;
9964       }
9965       case memberExp:
9966       {
9967          Type type;
9968          Location oldyylloc = yylloc;
9969          bool thisPtr;
9970          Expression checkExp = exp.member.exp;
9971          while(checkExp)
9972          {
9973             if(checkExp.type == castExp)
9974                checkExp = checkExp.cast.exp;
9975             else if(checkExp.type == bracketsExp)
9976                checkExp = checkExp.list ? checkExp.list->first : null;
9977             else
9978                break;
9979          }
9980
9981          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9982          exp.thisPtr = thisPtr;
9983
9984          // DOING THIS LATER NOW...
9985          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9986          {
9987             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9988             /* TODO: Name Space Fix ups
9989             if(!exp.member.member.classSym)
9990                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9991             */
9992          }
9993
9994          ProcessExpressionType(exp.member.exp);
9995          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9996             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9997          {
9998             exp.isConstant = false;
9999          }
10000          else
10001             exp.isConstant = exp.member.exp.isConstant;
10002          type = exp.member.exp.expType;
10003
10004          yylloc = exp.loc;
10005
10006          if(type && (type.kind == templateType))
10007          {
10008             Class _class = thisClass ? thisClass : currentClass;
10009             ClassTemplateParameter param = null;
10010             if(_class)
10011             {
10012                for(param = _class.templateParams.first; param; param = param.next)
10013                {
10014                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
10015                      break;
10016                }
10017             }
10018             if(param && param.defaultArg.member)
10019             {
10020                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
10021                if(argExp)
10022                {
10023                   Expression expMember = exp.member.exp;
10024                   Declarator decl;
10025                   OldList * specs = MkList();
10026                   char thisClassTypeString[1024];
10027
10028                   FreeIdentifier(exp.member.member);
10029
10030                   ProcessExpressionType(argExp);
10031
10032                   {
10033                      char * colon = strstr(param.defaultArg.memberString, "::");
10034                      if(colon)
10035                      {
10036                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
10037                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
10038                      }
10039                      else
10040                         strcpy(thisClassTypeString, _class.fullName);
10041                   }
10042
10043                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
10044
10045                   exp.expType = ProcessType(specs, decl);
10046                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
10047                   {
10048                      Class expClass = exp.expType._class.registered;
10049                      Class cClass = null;
10050                      int paramCount = 0;
10051                      int lastParam = -1;
10052
10053                      char templateString[1024];
10054                      ClassTemplateParameter param;
10055                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
10056                      for(cClass = expClass; cClass; cClass = cClass.base)
10057                      {
10058                         int p = 0;
10059                         for(param = cClass.templateParams.first; param; param = param.next)
10060                         {
10061                            int id = p;
10062                            Class sClass;
10063                            ClassTemplateArgument arg;
10064                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
10065                            arg = expClass.templateArgs[id];
10066
10067                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
10068                            {
10069                               ClassTemplateParameter cParam;
10070                               //int p = numParams - sClass.templateParams.count;
10071                               int p = 0;
10072                               Class nextClass;
10073                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
10074
10075                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
10076                               {
10077                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
10078                                  {
10079                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10080                                     {
10081                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
10082                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
10083                                        break;
10084                                     }
10085                                  }
10086                               }
10087                            }
10088
10089                            {
10090                               char argument[256];
10091                               argument[0] = '\0';
10092                               /*if(arg.name)
10093                               {
10094                                  strcat(argument, arg.name.string);
10095                                  strcat(argument, " = ");
10096                               }*/
10097                               switch(param.type)
10098                               {
10099                                  case expression:
10100                                  {
10101                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10102                                     char expString[1024];
10103                                     OldList * specs = MkList();
10104                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10105                                     Expression exp;
10106                                     char * string = PrintHexUInt64(arg.expression.ui64);
10107                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10108                                     delete string;
10109
10110                                     ProcessExpressionType(exp);
10111                                     ComputeExpression(exp);
10112                                     expString[0] = '\0';
10113                                     PrintExpression(exp, expString);
10114                                     strcat(argument, expString);
10115                                     // delete exp;
10116                                     FreeExpression(exp);
10117                                     break;
10118                                  }
10119                                  case identifier:
10120                                  {
10121                                     strcat(argument, arg.member.name);
10122                                     break;
10123                                  }
10124                                  case TemplateParameterType::type:
10125                                  {
10126                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10127                                     {
10128                                        if(!strcmp(arg.dataTypeString, "thisclass"))
10129                                           strcat(argument, thisClassTypeString);
10130                                        else
10131                                           strcat(argument, arg.dataTypeString);
10132                                     }
10133                                     break;
10134                                  }
10135                               }
10136                               if(argument[0])
10137                               {
10138                                  if(paramCount) strcat(templateString, ", ");
10139                                  if(lastParam != p - 1)
10140                                  {
10141                                     strcat(templateString, param.name);
10142                                     strcat(templateString, " = ");
10143                                  }
10144                                  strcat(templateString, argument);
10145                                  paramCount++;
10146                                  lastParam = p;
10147                               }
10148                               p++;
10149                            }
10150                         }
10151                      }
10152                      {
10153                         int len = strlen(templateString);
10154                         if(templateString[len-1] == '>') templateString[len++] = ' ';
10155                         templateString[len++] = '>';
10156                         templateString[len++] = '\0';
10157                      }
10158                      {
10159                         Context context = SetupTemplatesContext(_class);
10160                         FreeType(exp.expType);
10161                         exp.expType = ProcessTypeString(templateString, false);
10162                         FinishTemplatesContext(context);
10163                      }
10164                   }
10165
10166                   if(!expMember.expType.isPointerType)
10167                      expMember = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), expMember);
10168                   // *([expType] *)(((byte *)(uintptr)[exp.member.exp]) + [argExp].member.offset)
10169                   exp.type = bracketsExp;
10170                   exp.list = MkListOne(MkExpOp(null, '*',
10171                   /*opExp;
10172                   exp.op.op = '*';
10173                   exp.op.exp1 = null;
10174                   exp.op.exp2 = */
10175                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
10176                      MkExpBrackets(MkListOne(
10177                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
10178                            expMember))),
10179                               '+',
10180                               MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
10181                               '+',
10182                               MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
10183
10184                            ));
10185                }
10186             }
10187             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
10188                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
10189             {
10190                type = ProcessTemplateParameterType(type.templateParameter);
10191             }
10192          }
10193          // TODO: *** This seems to be where we should add method support for all basic types ***
10194          if(type && (type.kind == templateType));
10195          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
10196                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
10197                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
10198                           (type.kind == pointerType && type.type.kind == charType)))
10199          {
10200             Identifier id = exp.member.member;
10201             TypeKind typeKind = type.kind;
10202             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10203             if(typeKind == subClassType && exp.member.exp.type == classExp)
10204             {
10205                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10206                typeKind = classType;
10207             }
10208
10209             if(id)
10210             {
10211                if(typeKind == intType || typeKind == enumType)
10212                   _class = eSystem_FindClass(privateModule, "int");
10213                else if(!_class)
10214                {
10215                   if(type.kind == classType && type._class && type._class.registered)
10216                   {
10217                      _class = type._class.registered;
10218                   }
10219                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10220                   {
10221                      _class = FindClass("char *").registered;
10222                   }
10223                   else if(type.kind == pointerType)
10224                   {
10225                      _class = eSystem_FindClass(privateModule, "uintptr");
10226                      FreeType(exp.expType);
10227                      exp.expType = ProcessTypeString("uintptr", false);
10228                      exp.byReference = true;
10229                   }
10230                   else
10231                   {
10232                      char string[1024] = "";
10233                      Symbol classSym;
10234                      PrintTypeNoConst(type, string, false, true);
10235                      classSym = FindClass(string);
10236                      if(classSym) _class = classSym.registered;
10237                   }
10238                }
10239             }
10240
10241             if(_class && id)
10242             {
10243                /*bool thisPtr =
10244                   (exp.member.exp.type == identifierExp &&
10245                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10246                Property prop = null;
10247                Method method = null;
10248                DataMember member = null;
10249                Property revConvert = null;
10250                ClassProperty classProp = null;
10251
10252                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10253                   exp.member.memberType = propertyMember;
10254
10255                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10256                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10257
10258                if(typeKind != subClassType)
10259                {
10260                   // Prioritize data members over properties for "this"
10261                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10262                   {
10263                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10264                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10265                      {
10266                         prop = eClass_FindProperty(_class, id.string, privateModule);
10267                         if(prop)
10268                            member = null;
10269                      }
10270                      if(!member && !prop)
10271                         prop = eClass_FindProperty(_class, id.string, privateModule);
10272                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10273                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10274                         exp.member.thisPtr = true;
10275                   }
10276                   // Prioritize properties over data members otherwise
10277                   else
10278                   {
10279                      bool useMemberForNonConst = false;
10280                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10281                      if(!id.classSym)
10282                      {
10283                         prop = eClass_FindProperty(_class, id.string, null);
10284
10285                         useMemberForNonConst = prop && exp.destType &&
10286                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10287                               !strncmp(prop.dataTypeString, "const ", 6);
10288
10289                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10290                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10291                      }
10292
10293                      if((!prop || useMemberForNonConst) && !member)
10294                      {
10295                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10296                         if(!method)
10297                         {
10298                            prop = eClass_FindProperty(_class, id.string, privateModule);
10299
10300                            useMemberForNonConst |= prop && exp.destType &&
10301                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10302                                  !strncmp(prop.dataTypeString, "const ", 6);
10303
10304                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10305                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10306                         }
10307                      }
10308
10309                      if(member && prop)
10310                      {
10311                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10312                            prop = null;
10313                         else
10314                            member = null;
10315                      }
10316                   }
10317                }
10318                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10319                   method = eClass_FindMethod(_class, id.string, privateModule);
10320                if(!prop && !member && !method)
10321                {
10322                   if(typeKind == subClassType)
10323                   {
10324                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10325                      if(classProp)
10326                      {
10327                         exp.member.memberType = classPropertyMember;
10328                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10329                      }
10330                      else
10331                      {
10332                         // Assume this is a class_data member
10333                         char structName[1024];
10334                         Identifier id = exp.member.member;
10335                         Expression classExp = exp.member.exp;
10336                         type.refCount++;
10337
10338                         FreeType(classExp.expType);
10339                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10340
10341                         strcpy(structName, "__ecereClassData_");
10342                         FullClassNameCat(structName, type._class.string, false);
10343                         exp.type = pointerExp;
10344                         exp.member.member = id;
10345
10346                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10347                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10348                               MkExpBrackets(MkListOne(MkExpOp(
10349                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10350                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10351                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10352                                  )));
10353
10354                         FreeType(type);
10355
10356                         ProcessExpressionType(exp);
10357                         return;
10358                      }
10359                   }
10360                   else
10361                   {
10362                      // Check for reverse conversion
10363                      // (Convert in an instantiation later, so that we can use
10364                      //  deep properties system)
10365                      Symbol classSym = FindClass(id.string);
10366                      if(classSym)
10367                      {
10368                         Class convertClass = classSym.registered;
10369                         if(convertClass)
10370                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10371                      }
10372                   }
10373                }
10374
10375                //if(!exp.member.exp.destType)
10376                if(exp.member.exp.destType)
10377                   FreeType(exp.member.exp.destType);
10378                {
10379                   if(method && !method._class.symbol)
10380                      method._class.symbol = FindClass(method._class.fullName);
10381                   if(prop && !prop._class.symbol)
10382                      prop._class.symbol = FindClass(prop._class.fullName);
10383
10384                   exp.member.exp.destType = Type
10385                   {
10386                      refCount = 1;
10387                      kind = classType;
10388                      _class = prop ? prop._class.symbol : method ? method._class.symbol : _class.symbol;
10389                      // wasThisClass = type ? type.wasThisClass : false;
10390                   };
10391                }
10392
10393                if(prop)
10394                {
10395                   exp.member.memberType = propertyMember;
10396                   if(!prop.dataType)
10397                      ProcessPropertyType(prop);
10398                   exp.expType = prop.dataType;
10399                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10400                   {
10401                      Type type { };
10402                      CopyTypeInto(type, exp.expType);
10403                      type.refCount = 1;
10404                      type.constant = true;
10405                      exp.expType = type;
10406                   }
10407                   else if(prop.dataType)
10408                      prop.dataType.refCount++;
10409                }
10410                else if(member)
10411                {
10412                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10413                   {
10414                      FreeExpContents(exp);
10415                      exp.type = identifierExp;
10416                      exp.identifier = MkIdentifier("class");
10417                      ProcessExpressionType(exp);
10418                      return;
10419                   }
10420
10421                   exp.member.memberType = dataMember;
10422                   DeclareStruct(_class.fullName, false);
10423                   if(!member.dataType)
10424                   {
10425                      Context context = SetupTemplatesContext(_class);
10426                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10427                      FinishTemplatesContext(context);
10428                   }
10429                   exp.expType = member.dataType;
10430                   if(member.dataType) member.dataType.refCount++;
10431                }
10432                else if(revConvert)
10433                {
10434                   exp.member.memberType = reverseConversionMember;
10435                   exp.expType = MkClassType(revConvert._class.fullName);
10436                }
10437                else if(method)
10438                {
10439                   //if(inCompiler)
10440                   {
10441                      /*if(id._class)
10442                      {
10443                         exp.type = identifierExp;
10444                         exp.identifier = exp.member.member;
10445                      }
10446                      else*/
10447                         exp.member.memberType = methodMember;
10448                   }
10449                   if(!method.dataType)
10450                      ProcessMethodType(method);
10451                   exp.expType = Type
10452                   {
10453                      refCount = 1;
10454                      kind = methodType;
10455                      method = method;
10456                   };
10457
10458                   // Tricky spot here... To use instance versus class virtual table
10459                   // Put it back to what it was... What did we break?
10460
10461                   // Had to put it back for overriding Main of Thread global instance
10462
10463                   //exp.expType.methodClass = _class;
10464                   exp.expType.methodClass = (id && id._class) ? _class : null;
10465
10466                   // Need the actual class used for templated classes
10467                   exp.expType.usedClass = _class;
10468                }
10469                else if(!classProp)
10470                {
10471                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10472                   {
10473                      FreeExpContents(exp);
10474                      exp.type = identifierExp;
10475                      exp.identifier = MkIdentifier("class");
10476                      FreeType(exp.expType);
10477                      exp.expType = MkClassType("ecere::com::Class");
10478                      return;
10479                   }
10480                   yylloc = exp.member.member.loc;
10481                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10482                   if(inCompiler)
10483                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10484                }
10485
10486                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10487                {
10488                   Class tClass;
10489
10490                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10491                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10492
10493                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10494                   {
10495                      int id = 0;
10496                      ClassTemplateParameter curParam = null;
10497                      Class sClass;
10498
10499                      for(sClass = tClass; sClass; sClass = sClass.base)
10500                      {
10501                         id = 0;
10502                         if(sClass.templateClass) sClass = sClass.templateClass;
10503                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10504                         {
10505                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10506                            {
10507                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10508                                  id += sClass.templateParams.count;
10509                               break;
10510                            }
10511                            id++;
10512                         }
10513                         if(curParam) break;
10514                      }
10515
10516                      if(curParam && tClass.templateArgs[id].dataTypeString)
10517                      {
10518                         ClassTemplateArgument arg = tClass.templateArgs[id];
10519                         Context context = SetupTemplatesContext(tClass);
10520                         bool constant = exp.expType.constant;
10521                         /*if(!arg.dataType)
10522                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10523                         FreeType(exp.expType);
10524
10525                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10526                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10527                         else if(exp.expType.kind == pointerType)
10528                         {
10529                            Type t = exp.expType.type;
10530                            while(t.kind == pointerType) t = t.type;
10531                            if(constant) t.constant = constant;
10532                         }
10533                         if(exp.expType)
10534                         {
10535                            if(exp.expType.kind == thisClassType)
10536                            {
10537                               FreeType(exp.expType);
10538                               exp.expType = ReplaceThisClassType(_class);
10539                            }
10540
10541                            if(tClass.templateClass && (exp.expType.kind != templateType || (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType))))
10542                               exp.expType.passAsTemplate = true;
10543                            //exp.expType.refCount++;
10544                            if(!exp.destType)
10545                            {
10546                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10547                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10548                               else if(exp.destType.kind == pointerType)
10549                               {
10550                                  Type t = exp.destType.type;
10551                                  while(t.kind == pointerType) t = t.type;
10552                                  if(constant) t.constant = constant;
10553                               }
10554
10555                               //exp.destType.refCount++;
10556
10557                               if(exp.destType.kind == thisClassType)
10558                               {
10559                                  FreeType(exp.destType);
10560                                  exp.destType = ReplaceThisClassType(_class);
10561                               }
10562                            }
10563                         }
10564                         FinishTemplatesContext(context);
10565                      }
10566                   }
10567                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10568                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10569                   {
10570                      int id = 0;
10571                      ClassTemplateParameter curParam = null;
10572                      Class sClass;
10573
10574                      for(sClass = tClass; sClass; sClass = sClass.base)
10575                      {
10576                         id = 0;
10577                         if(sClass.templateClass) sClass = sClass.templateClass;
10578                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10579                         {
10580                            if(curParam.type == TemplateParameterType::type &&
10581                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10582                            {
10583                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10584                                  id += sClass.templateParams.count;
10585                               break;
10586                            }
10587                            id++;
10588                         }
10589                         if(curParam) break;
10590                      }
10591
10592                      if(curParam)
10593                      {
10594                         ClassTemplateArgument arg = tClass.templateArgs[id];
10595                         Context context = SetupTemplatesContext(tClass);
10596                         Type basicType;
10597                         /*if(!arg.dataType)
10598                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10599
10600                         basicType = ProcessTypeString(arg.dataTypeString, false);
10601                         if(basicType)
10602                         {
10603                            if(basicType.kind == thisClassType)
10604                            {
10605                               FreeType(basicType);
10606                               basicType = ReplaceThisClassType(_class);
10607                            }
10608
10609                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10610                            if(tClass.templateClass)
10611                               basicType.passAsTemplate = true;
10612                            */
10613
10614                            FreeType(exp.expType);
10615
10616                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10617                            //exp.expType.refCount++;
10618                            if(!exp.destType)
10619                            {
10620                               exp.destType = exp.expType;
10621                               exp.destType.refCount++;
10622                            }
10623
10624                            {
10625                               Expression newExp { };
10626                               OldList * specs = MkList();
10627                               Declarator decl;
10628                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10629                               *newExp = *exp;
10630                               if(exp.destType) exp.destType.refCount++;
10631                               if(exp.expType)  exp.expType.refCount++;
10632                               exp.type = castExp;
10633                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10634                               exp.cast.exp = newExp;
10635                               //FreeType(exp.expType);
10636                               //exp.expType = null;
10637                               //ProcessExpressionType(sourceExp);
10638                            }
10639                         }
10640                         FinishTemplatesContext(context);
10641                      }
10642                   }
10643                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10644                   {
10645                      Class expClass = exp.expType._class.registered;
10646                      if(expClass)
10647                      {
10648                         Class cClass = null;
10649                         int p = 0;
10650                         int paramCount = 0;
10651                         int lastParam = -1;
10652                         char templateString[1024];
10653                         ClassTemplateParameter param;
10654                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10655                         while(cClass != expClass)
10656                         {
10657                            Class sClass;
10658                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10659                            cClass = sClass;
10660
10661                            for(param = cClass.templateParams.first; param; param = param.next)
10662                            {
10663                               Class cClassCur = null;
10664                               int cp = 0;
10665                               ClassTemplateParameter paramCur = null;
10666                               ClassTemplateArgument arg;
10667                               while(cClassCur != tClass && !paramCur)
10668                               {
10669                                  Class sClassCur;
10670                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10671                                  cClassCur = sClassCur;
10672
10673                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10674                                  {
10675                                     if(!strcmp(paramCur.name, param.name))
10676                                     {
10677
10678                                        break;
10679                                     }
10680                                     cp++;
10681                                  }
10682                               }
10683                               if(paramCur && paramCur.type == TemplateParameterType::type)
10684                                  arg = tClass.templateArgs[cp];
10685                               else
10686                                  arg = expClass.templateArgs[p];
10687
10688                               {
10689                                  char argument[256];
10690                                  argument[0] = '\0';
10691                                  /*if(arg.name)
10692                                  {
10693                                     strcat(argument, arg.name.string);
10694                                     strcat(argument, " = ");
10695                                  }*/
10696                                  switch(param.type)
10697                                  {
10698                                     case expression:
10699                                     {
10700                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10701                                        char expString[1024];
10702                                        OldList * specs = MkList();
10703                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10704                                        Expression exp;
10705                                        char * string = PrintHexUInt64(arg.expression.ui64);
10706                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10707                                        delete string;
10708
10709                                        ProcessExpressionType(exp);
10710                                        ComputeExpression(exp);
10711                                        expString[0] = '\0';
10712                                        PrintExpression(exp, expString);
10713                                        strcat(argument, expString);
10714                                        // delete exp;
10715                                        FreeExpression(exp);
10716                                        break;
10717                                     }
10718                                     case identifier:
10719                                     {
10720                                        strcat(argument, arg.member.name);
10721                                        break;
10722                                     }
10723                                     case TemplateParameterType::type:
10724                                     {
10725                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10726                                           strcat(argument, arg.dataTypeString);
10727                                        break;
10728                                     }
10729                                  }
10730                                  if(argument[0])
10731                                  {
10732                                     if(paramCount) strcat(templateString, ", ");
10733                                     if(lastParam != p - 1)
10734                                     {
10735                                        strcat(templateString, param.name);
10736                                        strcat(templateString, " = ");
10737                                     }
10738                                     strcat(templateString, argument);
10739                                     paramCount++;
10740                                     lastParam = p;
10741                                  }
10742                               }
10743                               p++;
10744                            }
10745                         }
10746                         {
10747                            int len = strlen(templateString);
10748                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10749                            templateString[len++] = '>';
10750                            templateString[len++] = '\0';
10751                         }
10752
10753                         FreeType(exp.expType);
10754                         {
10755                            Context context = SetupTemplatesContext(tClass);
10756                            exp.expType = ProcessTypeString(templateString, false);
10757                            FinishTemplatesContext(context);
10758                         }
10759                      }
10760                   }
10761                }
10762             }
10763             else
10764                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10765          }
10766          else if(type && (type.kind == structType || type.kind == unionType))
10767          {
10768             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10769             if(memberType)
10770             {
10771                exp.expType = memberType;
10772                if(memberType)
10773                   memberType.refCount++;
10774             }
10775          }
10776          else
10777          {
10778             char expString[10240];
10779             expString[0] = '\0';
10780             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10781             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10782          }
10783
10784          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10785          {
10786             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10787             {
10788                Identifier id = exp.member.member;
10789                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10790                if(_class)
10791                {
10792                   FreeType(exp.expType);
10793                   exp.expType = ReplaceThisClassType(_class);
10794                }
10795             }
10796          }
10797          yylloc = oldyylloc;
10798          break;
10799       }
10800       // Convert x->y into (*x).y
10801       case pointerExp:
10802       {
10803          Type destType = exp.destType;
10804
10805          // DOING THIS LATER NOW...
10806          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10807          {
10808             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10809             /* TODO: Name Space Fix ups
10810             if(!exp.member.member.classSym)
10811                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10812             */
10813          }
10814
10815          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10816          exp.type = memberExp;
10817          if(destType)
10818             destType.count++;
10819          ProcessExpressionType(exp);
10820          if(destType)
10821             destType.count--;
10822          break;
10823       }
10824       case classSizeExp:
10825       {
10826          //ComputeExpression(exp);
10827
10828          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10829          if(classSym && classSym.registered)
10830          {
10831             if(classSym.registered.type == noHeadClass)
10832             {
10833                char name[1024];
10834                name[0] = '\0';
10835                DeclareStruct(classSym.string, false);
10836                FreeSpecifier(exp._class);
10837                exp.type = typeSizeExp;
10838                FullClassNameCat(name, classSym.string, false);
10839                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10840             }
10841             else
10842             {
10843                if(classSym.registered.fixed)
10844                {
10845                   FreeSpecifier(exp._class);
10846                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10847                   exp.type = constantExp;
10848                }
10849                else
10850                {
10851                   char className[1024];
10852                   strcpy(className, "__ecereClass_");
10853                   FullClassNameCat(className, classSym.string, true);
10854                   //MangleClassName(className);
10855
10856                   DeclareClass(classSym, className);
10857
10858                   FreeExpContents(exp);
10859                   exp.type = pointerExp;
10860                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10861                   exp.member.member = MkIdentifier("structSize");
10862                }
10863             }
10864          }
10865
10866          exp.expType = Type
10867          {
10868             refCount = 1;
10869             kind = intSizeType;
10870          };
10871          // exp.isConstant = true;
10872          break;
10873       }
10874       case typeSizeExp:
10875       {
10876          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10877
10878          exp.expType = Type
10879          {
10880             refCount = 1;
10881             kind = intSizeType;
10882          };
10883          exp.isConstant = true;
10884
10885          DeclareType(type, false, false);
10886          FreeType(type);
10887          break;
10888       }
10889       case castExp:
10890       {
10891          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10892          type.count = 1;
10893          FreeType(exp.cast.exp.destType);
10894          exp.cast.exp.destType = type;
10895          type.refCount++;
10896          type.casted = true;
10897          ProcessExpressionType(exp.cast.exp);
10898          type.casted = false;
10899          type.count = 0;
10900          exp.expType = type;
10901          //type.refCount++;
10902
10903          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10904          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10905          {
10906             void * prev = exp.prev, * next = exp.next;
10907             Type expType = exp.cast.exp.destType;
10908             Expression castExp = exp.cast.exp;
10909             Type destType = exp.destType;
10910
10911             if(expType) expType.refCount++;
10912
10913             //FreeType(exp.destType);
10914             FreeType(exp.expType);
10915             FreeTypeName(exp.cast.typeName);
10916
10917             *exp = *castExp;
10918             FreeType(exp.expType);
10919             FreeType(exp.destType);
10920
10921             exp.expType = expType;
10922             exp.destType = destType;
10923
10924             delete castExp;
10925
10926             exp.prev = prev;
10927             exp.next = next;
10928
10929          }
10930          else
10931          {
10932             exp.isConstant = exp.cast.exp.isConstant;
10933          }
10934          //FreeType(type);
10935          break;
10936       }
10937       case extensionInitializerExp:
10938       {
10939          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10940          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10941          // ProcessInitializer(exp.initializer.initializer, type);
10942          exp.expType = type;
10943          break;
10944       }
10945       case vaArgExp:
10946       {
10947          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10948          ProcessExpressionType(exp.vaArg.exp);
10949          exp.expType = type;
10950          break;
10951       }
10952       case conditionExp:
10953       {
10954          Expression e;
10955          Type t = exp.destType;
10956          if(t && !exp.destType.casted)
10957          {
10958             t = { };
10959             CopyTypeInto(t, exp.destType);
10960             t.count = 0;
10961          }
10962          else if(t)
10963             t.refCount++;
10964
10965          exp.isConstant = true;
10966
10967          FreeType(exp.cond.cond.destType);
10968          exp.cond.cond.destType = MkClassType("bool");
10969          exp.cond.cond.destType.truth = true;
10970          ProcessExpressionType(exp.cond.cond);
10971          if(!exp.cond.cond.isConstant)
10972             exp.isConstant = false;
10973          for(e = exp.cond.exp->first; e; e = e.next)
10974          {
10975             if(!e.next)
10976             {
10977                FreeType(e.destType);
10978                e.destType = t;
10979                if(e.destType) e.destType.refCount++;
10980             }
10981             ProcessExpressionType(e);
10982             if(!e.next)
10983             {
10984                exp.expType = e.expType;
10985                if(e.expType) e.expType.refCount++;
10986             }
10987             if(!e.isConstant)
10988                exp.isConstant = false;
10989          }
10990
10991          FreeType(exp.cond.elseExp.destType);
10992          // Added this check if we failed to find an expType
10993          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10994
10995          // Reversed it...
10996          exp.cond.elseExp.destType = t ? t : exp.expType;
10997
10998          if(exp.cond.elseExp.destType)
10999             exp.cond.elseExp.destType.refCount++;
11000          ProcessExpressionType(exp.cond.elseExp);
11001
11002          // FIXED THIS: Was done before calling process on elseExp
11003          if(!exp.cond.elseExp.isConstant)
11004             exp.isConstant = false;
11005
11006          FreeType(t);
11007          break;
11008       }
11009       case extensionCompoundExp:
11010       {
11011          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
11012          {
11013             Statement last = exp.compound.compound.statements->last;
11014             if(last.type == expressionStmt && last.expressions && last.expressions->last)
11015             {
11016                ((Expression)last.expressions->last).destType = exp.destType;
11017                if(exp.destType)
11018                   exp.destType.refCount++;
11019             }
11020             ProcessStatement(exp.compound);
11021             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
11022             if(exp.expType)
11023                exp.expType.refCount++;
11024          }
11025          break;
11026       }
11027       case classExp:
11028       {
11029          Specifier spec = exp._classExp.specifiers->first;
11030          if(spec && spec.type == nameSpecifier)
11031          {
11032             exp.expType = MkClassType(spec.name);
11033             exp.expType.kind = subClassType;
11034             exp.byReference = true;
11035          }
11036          else
11037          {
11038             exp.expType = MkClassType("ecere::com::Class");
11039             exp.byReference = true;
11040          }
11041          break;
11042       }
11043       case classDataExp:
11044       {
11045          Class _class = thisClass ? thisClass : currentClass;
11046          if(_class)
11047          {
11048             Identifier id = exp.classData.id;
11049             char structName[1024];
11050             Expression classExp;
11051             strcpy(structName, "__ecereClassData_");
11052             FullClassNameCat(structName, _class.fullName, false);
11053             exp.type = pointerExp;
11054             exp.member.member = id;
11055             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
11056                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
11057             else
11058                classExp = MkExpIdentifier(MkIdentifier("class"));
11059
11060             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
11061                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
11062                   MkExpBrackets(MkListOne(MkExpOp(
11063                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
11064                         MkExpMember(classExp, MkIdentifier("data"))), '+',
11065                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
11066                      )));
11067
11068             ProcessExpressionType(exp);
11069             return;
11070          }
11071          break;
11072       }
11073       case arrayExp:
11074       {
11075          Type type = null;
11076          const char * typeString = null;
11077          char typeStringBuf[1024];
11078          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
11079             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
11080          {
11081             Class templateClass = exp.destType._class.registered;
11082             typeString = templateClass.templateArgs[2].dataTypeString;
11083          }
11084          else if(exp.list)
11085          {
11086             // Guess type from expressions in the array
11087             Expression e;
11088             for(e = exp.list->first; e; e = e.next)
11089             {
11090                ProcessExpressionType(e);
11091                if(e.expType)
11092                {
11093                   if(!type) { type = e.expType; type.refCount++; }
11094                   else
11095                   {
11096                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11097                      if(!MatchTypeExpression(e, type, null, false, true))
11098                      {
11099                         FreeType(type);
11100                         type = e.expType;
11101                         e.expType = null;
11102
11103                         e = exp.list->first;
11104                         ProcessExpressionType(e);
11105                         if(e.expType)
11106                         {
11107                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
11108                            if(!MatchTypeExpression(e, type, null, false, true))
11109                            {
11110                               FreeType(e.expType);
11111                               e.expType = null;
11112                               FreeType(type);
11113                               type = null;
11114                               break;
11115                            }
11116                         }
11117                      }
11118                   }
11119                   if(e.expType)
11120                   {
11121                      FreeType(e.expType);
11122                      e.expType = null;
11123                   }
11124                }
11125             }
11126             if(type)
11127             {
11128                typeStringBuf[0] = '\0';
11129                PrintTypeNoConst(type, typeStringBuf, false, true);
11130                typeString = typeStringBuf;
11131                FreeType(type);
11132                type = null;
11133             }
11134          }
11135          if(typeString)
11136          {
11137             /*
11138             (Container)& (struct BuiltInContainer)
11139             {
11140                ._vTbl = class(BuiltInContainer)._vTbl,
11141                ._class = class(BuiltInContainer),
11142                .refCount = 0,
11143                .data = (int[]){ 1, 7, 3, 4, 5 },
11144                .count = 5,
11145                .type = class(int),
11146             }
11147             */
11148             char templateString[1024];
11149             OldList * initializers = MkList();
11150             OldList * structInitializers = MkList();
11151             OldList * specs = MkList();
11152             Expression expExt;
11153             Declarator decl = SpecDeclFromString(typeString, specs, null);
11154             sprintf(templateString, "Container<%s>", typeString);
11155
11156             if(exp.list)
11157             {
11158                Expression e;
11159                type = ProcessTypeString(typeString, false);
11160                while((e = exp.list->first))
11161                {
11162                   exp.list->Remove(e);
11163                   e.destType = type;
11164                   type.refCount++;
11165                   ProcessExpressionType(e);
11166                   ListAdd(initializers, MkInitializerAssignment(e));
11167                }
11168                FreeType(type);
11169                delete exp.list;
11170             }
11171
11172             DeclareStruct("ecere::com::BuiltInContainer", false);
11173
11174             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
11175                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11176             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
11177                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11178             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
11179                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11180             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
11181                MkTypeName(specs, MkDeclaratorArray(decl, null)),
11182                MkInitializerList(initializers))));
11183                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11184             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
11185                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11186             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
11187                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11188             exp.expType = ProcessTypeString(templateString, false);
11189             exp.type = bracketsExp;
11190             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
11191                MkExpOp(null, '&',
11192                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
11193                   MkInitializerList(structInitializers)))));
11194             ProcessExpressionType(expExt);
11195          }
11196          else
11197          {
11198             exp.expType = ProcessTypeString("Container", false);
11199             Compiler_Error($"Couldn't determine type of array elements\n");
11200          }
11201          break;
11202       }
11203    }
11204
11205    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
11206    {
11207       FreeType(exp.expType);
11208       exp.expType = ReplaceThisClassType(thisClass);
11209    }
11210
11211    // Resolve structures here
11212    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
11213    {
11214       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
11215       // TODO: Fix members reference...
11216       if(symbol)
11217       {
11218          if(exp.expType.kind != enumType)
11219          {
11220             Type member;
11221             String enumName = CopyString(exp.expType.enumName);
11222
11223             // Fixed a memory leak on self-referencing C structs typedefs
11224             // by instantiating a new type rather than simply copying members
11225             // into exp.expType
11226             FreeType(exp.expType);
11227             exp.expType = Type { };
11228             exp.expType.kind = symbol.type.kind;
11229             exp.expType.refCount++;
11230             exp.expType.enumName = enumName;
11231
11232             exp.expType.members = symbol.type.members;
11233             for(member = symbol.type.members.first; member; member = member.next)
11234                member.refCount++;
11235          }
11236          else
11237          {
11238             NamedLink64 member;
11239             for(member = symbol.type.members.first; member; member = member.next)
11240             {
11241                NamedLink64 value { name = CopyString(member.name) };
11242                exp.expType.members.Add(value);
11243             }
11244          }
11245       }
11246    }
11247
11248    yylloc = exp.loc;
11249    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11250    else if(exp.destType && !exp.destType.keepCast)
11251    {
11252       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11253          exp.needTemplateCast = 1;
11254
11255       if(exp.destType.kind == voidType);
11256       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11257       {
11258          // Warn for casting unrelated types to/from struct classes
11259          bool invalidCast = false;
11260          if(inCompiler && exp.destType.count && exp.expType)
11261          {
11262             Class c1 = (exp.expType.kind == classType && exp.expType._class) ? exp.expType._class.registered : null;
11263             Class c2 = (exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
11264             if(c1 && c1.type != structClass) c1 = null;
11265             if(c2 && c2.type != structClass) c2 = null;
11266             if((c1 && !exp.expType.byReference && !c2 && !exp.destType.isPointerType) || (c2 && !exp.destType.byReference && !c1 && !exp.expType.isPointerType))
11267                invalidCast = true;
11268          }
11269          if(!exp.destType.count || unresolved || invalidCast)
11270          {
11271             if(!exp.expType)
11272             {
11273                yylloc = exp.loc;
11274                if(exp.destType.kind != ellipsisType)
11275                {
11276                   char type2[1024];
11277                   type2[0] = '\0';
11278                   if(inCompiler)
11279                   {
11280                      char expString[10240];
11281                      expString[0] = '\0';
11282
11283                      PrintType(exp.destType, type2, false, true);
11284
11285                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11286                      if(unresolved)
11287                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11288                      else if(exp.type != dummyExp)
11289                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11290                   }
11291                }
11292                else
11293                {
11294                   char expString[10240] ;
11295                   expString[0] = '\0';
11296                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11297
11298                   if(unresolved)
11299                      Compiler_Error($"unresolved identifier %s\n", expString);
11300                   else if(exp.type != dummyExp)
11301                      Compiler_Error($"couldn't determine type of %s\n", expString);
11302                }
11303             }
11304             else
11305             {
11306                char type1[1024];
11307                char type2[1024];
11308                type1[0] = '\0';
11309                type2[0] = '\0';
11310                if(inCompiler)
11311                {
11312                   PrintType(exp.expType, type1, false, true);
11313                   PrintType(exp.destType, type2, false, true);
11314                }
11315
11316                //CheckExpressionType(exp, exp.destType, false);
11317
11318                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11319                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11320                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11321                else
11322                {
11323                   char expString[10240];
11324                   expString[0] = '\0';
11325                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11326
11327 #ifdef _DEBUG
11328                   CheckExpressionType(exp, exp.destType, false, true);
11329 #endif
11330                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11331                   if(!sourceFile || (!strstr(sourceFile, "src\\lexer.ec") && !strstr(sourceFile, "src/lexer.ec") &&
11332                                      !strstr(sourceFile, "src\\grammar.ec") && !strstr(sourceFile, "src/grammar.ec") &&
11333                                      !strstr(sourceFile, "src\\type.ec") && !strstr(sourceFile, "src/type.ec") &&
11334                                      !strstr(sourceFile, "src\\expression.ec") && !strstr(sourceFile, "src/expression.ec")))
11335                   {
11336                      if(invalidCast)
11337                         Compiler_Error($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11338                      else
11339                         Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11340                   }
11341
11342                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11343                   if(!inCompiler)
11344                   {
11345                      FreeType(exp.expType);
11346                      exp.destType.refCount++;
11347                      exp.expType = exp.destType;
11348                   }
11349                }
11350             }
11351          }
11352       }
11353       // Cast function pointers to void * as eC already checked compatibility
11354       else if(exp.destType && exp.destType.kind == pointerType && exp.destType.type && exp.destType.type.kind == functionType &&
11355               exp.expType && (exp.expType.kind == functionType || exp.expType.kind == methodType))
11356       {
11357          Expression nbExp = GetNonBracketsExp(exp);
11358          if(nbExp.type != castExp || !IsVoidPtrCast(nbExp.cast.typeName))
11359          {
11360             Expression e = MoveExpContents(exp);
11361             exp.cast.exp = MkExpBrackets(MkListOne(e));
11362             exp.type = castExp;
11363             exp.cast.exp.destType = exp.destType;
11364             if(exp.destType) exp.destType.refCount++;
11365             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
11366          }
11367       }
11368    }
11369    else if(unresolved)
11370    {
11371       if(exp.identifier._class && exp.identifier._class.name)
11372          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11373       else if(exp.identifier.string && exp.identifier.string[0])
11374          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11375    }
11376    else if(!exp.expType && exp.type != dummyExp)
11377    {
11378       char expString[10240];
11379       expString[0] = '\0';
11380       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11381       Compiler_Error($"couldn't determine type of %s\n", expString);
11382    }
11383
11384    // Let's try to support any_object & typed_object here:
11385    if(inCompiler)
11386       ApplyAnyObjectLogic(exp);
11387
11388    // Mark nohead classes as by reference, unless we're casting them to an integral type
11389    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11390       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11391          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11392           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11393    {
11394       exp.byReference = true;
11395    }
11396    yylloc = oldyylloc;
11397 }
11398
11399 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11400 {
11401    // THIS CODE WILL FIND NEXT MEMBER...
11402    if(*curMember)
11403    {
11404       *curMember = (*curMember).next;
11405
11406       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11407       {
11408          *curMember = subMemberStack[--(*subMemberStackPos)];
11409          *curMember = (*curMember).next;
11410       }
11411
11412       // SKIP ALL PROPERTIES HERE...
11413       while((*curMember) && (*curMember).isProperty)
11414          *curMember = (*curMember).next;
11415
11416       if(subMemberStackPos)
11417       {
11418          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11419          {
11420             subMemberStack[(*subMemberStackPos)++] = *curMember;
11421
11422             *curMember = (*curMember).members.first;
11423             while(*curMember && (*curMember).isProperty)
11424                *curMember = (*curMember).next;
11425          }
11426       }
11427    }
11428    while(!*curMember)
11429    {
11430       if(!*curMember)
11431       {
11432          if(subMemberStackPos && *subMemberStackPos)
11433          {
11434             *curMember = subMemberStack[--(*subMemberStackPos)];
11435             *curMember = (*curMember).next;
11436          }
11437          else
11438          {
11439             Class lastCurClass = *curClass;
11440
11441             if(*curClass == _class) break;     // REACHED THE END
11442
11443             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11444             *curMember = (*curClass).membersAndProperties.first;
11445          }
11446
11447          while((*curMember) && (*curMember).isProperty)
11448             *curMember = (*curMember).next;
11449          if(subMemberStackPos)
11450          {
11451             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11452             {
11453                subMemberStack[(*subMemberStackPos)++] = *curMember;
11454
11455                *curMember = (*curMember).members.first;
11456                while(*curMember && (*curMember).isProperty)
11457                   *curMember = (*curMember).next;
11458             }
11459          }
11460       }
11461    }
11462 }
11463
11464
11465 static void ProcessInitializer(Initializer init, Type type)
11466 {
11467    switch(init.type)
11468    {
11469       case expInitializer:
11470          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11471          {
11472             // TESTING THIS FOR SHUTTING = 0 WARNING
11473             if(init.exp && !init.exp.destType)
11474             {
11475                FreeType(init.exp.destType);
11476                init.exp.destType = type;
11477                if(type) type.refCount++;
11478             }
11479             if(init.exp)
11480             {
11481                ProcessExpressionType(init.exp);
11482                init.isConstant = init.exp.isConstant;
11483             }
11484             break;
11485          }
11486          else
11487          {
11488             Expression exp = init.exp;
11489             Instantiation inst = exp.instance;
11490             MembersInit members;
11491
11492             init.type = listInitializer;
11493             init.list = MkList();
11494
11495             if(inst.members)
11496             {
11497                for(members = inst.members->first; members; members = members.next)
11498                {
11499                   if(members.type == dataMembersInit)
11500                   {
11501                      MemberInit member;
11502                      for(member = members.dataMembers->first; member; member = member.next)
11503                      {
11504                         ListAdd(init.list, member.initializer);
11505                         member.initializer = null;
11506                      }
11507                   }
11508                   // Discard all MembersInitMethod
11509                }
11510             }
11511             FreeExpression(exp);
11512          }
11513       case listInitializer:
11514       {
11515          Initializer i;
11516          Type initializerType = null;
11517          Class curClass = null;
11518          DataMember curMember = null;
11519          DataMember subMemberStack[256];
11520          int subMemberStackPos = 0;
11521
11522          if(type && type.kind == arrayType)
11523             initializerType = Dereference(type);
11524          else if(type && (type.kind == structType || type.kind == unionType))
11525             initializerType = type.members.first;
11526
11527          for(i = init.list->first; i; i = i.next)
11528          {
11529             if(type && type.kind == classType && type._class && type._class.registered)
11530             {
11531                // THIS IS FOR A C STYLE INSTANTIATION OF STRUCT CLASSES ONLY... WE ONLY CARE ABOUT DATA MEMBERS, AND ACTUAL MEMORY ORDER (PRIVATE MEMBERS ARE INCLUDED)
11532                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11533                // TODO: Generate error on initializing a private data member this way from another module...
11534                if(curMember)
11535                {
11536                   if(!curMember.dataType)
11537                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11538                   initializerType = curMember.dataType;
11539                }
11540             }
11541             ProcessInitializer(i, initializerType);
11542             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11543                initializerType = initializerType.next;
11544             if(!i.isConstant)
11545                init.isConstant = false;
11546          }
11547
11548          if(type && type.kind == arrayType)
11549             FreeType(initializerType);
11550
11551          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11552          {
11553             Compiler_Error($"Assigning list initializer to non list\n");
11554          }
11555          break;
11556       }
11557    }
11558 }
11559
11560 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11561 {
11562    switch(spec.type)
11563    {
11564       case baseSpecifier:
11565       {
11566          if(spec.specifier == THISCLASS)
11567          {
11568             if(thisClass)
11569             {
11570                spec.type = nameSpecifier;
11571                spec.name = ReplaceThisClass(thisClass);
11572                spec.symbol = FindClass(spec.name);
11573                ProcessSpecifier(spec, declareStruct);
11574             }
11575          }
11576          break;
11577       }
11578       case nameSpecifier:
11579       {
11580          Symbol symbol = FindType(curContext, spec.name);
11581          if(symbol)
11582             DeclareType(symbol.type, true, true);
11583          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11584             DeclareStruct(spec.name, false);
11585          break;
11586       }
11587       case enumSpecifier:
11588       {
11589          Enumerator e;
11590          if(spec.list)
11591          {
11592             for(e = spec.list->first; e; e = e.next)
11593             {
11594                if(e.exp)
11595                   ProcessExpressionType(e.exp);
11596             }
11597          }
11598          // Fall through for IDE type processing
11599          if(inCompiler)
11600             break;
11601       }
11602       case structSpecifier:
11603       case unionSpecifier:
11604       {
11605          if(spec.definitions)
11606          {
11607             //ClassDef def;
11608             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11609             //if(symbol)
11610                ProcessClass(spec.definitions, symbol);
11611             /*else
11612             {
11613                for(def = spec.definitions->first; def; def = def.next)
11614                {
11615                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11616                      ProcessDeclaration(def.decl);
11617                }
11618             }*/
11619          }
11620          break;
11621       }
11622       /*
11623       case classSpecifier:
11624       {
11625          Symbol classSym = FindClass(spec.name);
11626          if(classSym && classSym.registered && classSym.registered.type == structClass)
11627             DeclareStruct(spec.name, false);
11628          break;
11629       }
11630       */
11631    }
11632 }
11633
11634
11635 static void ProcessDeclarator(Declarator decl)
11636 {
11637    switch(decl.type)
11638    {
11639       case identifierDeclarator:
11640          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11641          {
11642             FreeSpecifier(decl.identifier._class);
11643             decl.identifier._class = null;
11644          }
11645          break;
11646       case arrayDeclarator:
11647          if(decl.array.exp)
11648             ProcessExpressionType(decl.array.exp);
11649       case structDeclarator:
11650       case bracketsDeclarator:
11651       case functionDeclarator:
11652       case pointerDeclarator:
11653       case extendedDeclarator:
11654       case extendedDeclaratorEnd:
11655          if(decl.declarator)
11656             ProcessDeclarator(decl.declarator);
11657          if(decl.type == functionDeclarator)
11658          {
11659             Identifier id = GetDeclId(decl);
11660             if(id && id._class)
11661             {
11662                TypeName param
11663                {
11664                   qualifiers = MkListOne(id._class);
11665                   declarator = null;
11666                };
11667                if(!decl.function.parameters)
11668                   decl.function.parameters = MkList();
11669                decl.function.parameters->Insert(null, param);
11670                id._class = null;
11671             }
11672             if(decl.function.parameters)
11673             {
11674                TypeName param;
11675
11676                for(param = decl.function.parameters->first; param; param = param.next)
11677                {
11678                   if(param.qualifiers && param.qualifiers->first)
11679                   {
11680                      Specifier spec = param.qualifiers->first;
11681                      if(spec && spec.specifier == TYPED_OBJECT)
11682                      {
11683                         Declarator d = param.declarator;
11684                         TypeName newParam
11685                         {
11686                            qualifiers = MkListOne(MkSpecifier(VOID));
11687                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11688                         };
11689                         if(d.type != pointerDeclarator)
11690                            newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11691
11692                         FreeList(param.qualifiers, FreeSpecifier);
11693
11694                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11695                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11696
11697                         decl.function.parameters->Insert(param, newParam);
11698                         param = newParam;
11699                      }
11700                      else if(spec && spec.specifier == ANY_OBJECT)
11701                      {
11702                         Declarator d = param.declarator;
11703
11704                         FreeList(param.qualifiers, FreeSpecifier);
11705
11706                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11707                         if(d.type != pointerDeclarator)
11708                            param.qualifiers->Insert(null, MkSpecifier(CONST));
11709                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11710                      }
11711                      else if(spec.specifier == THISCLASS)
11712                      {
11713                         if(thisClass)
11714                         {
11715                            spec.type = nameSpecifier;
11716                            spec.name = ReplaceThisClass(thisClass);
11717                            spec.symbol = FindClass(spec.name);
11718                            ProcessSpecifier(spec, false);
11719                         }
11720                      }
11721                   }
11722
11723                   if(param.declarator)
11724                      ProcessDeclarator(param.declarator);
11725                }
11726             }
11727          }
11728          break;
11729    }
11730 }
11731
11732 static void ProcessDeclaration(Declaration decl)
11733 {
11734    yylloc = decl.loc;
11735    switch(decl.type)
11736    {
11737       case initDeclaration:
11738       {
11739          bool declareStruct = false;
11740          /*
11741          lineNum = decl.pos.line;
11742          column = decl.pos.col;
11743          */
11744
11745          if(decl.declarators)
11746          {
11747             InitDeclarator d;
11748
11749             for(d = decl.declarators->first; d; d = d.next)
11750             {
11751                Type type, subType;
11752                ProcessDeclarator(d.declarator);
11753
11754                type = ProcessType(decl.specifiers, d.declarator);
11755
11756                if(d.initializer)
11757                {
11758                   ProcessInitializer(d.initializer, type);
11759
11760                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11761
11762                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11763                      d.initializer.exp.type == instanceExp)
11764                   {
11765                      if(type.kind == classType && type._class ==
11766                         d.initializer.exp.expType._class)
11767                      {
11768                         Instantiation inst = d.initializer.exp.instance;
11769                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11770
11771                         d.initializer.exp.instance = null;
11772                         if(decl.specifiers)
11773                            FreeList(decl.specifiers, FreeSpecifier);
11774                         FreeList(decl.declarators, FreeInitDeclarator);
11775
11776                         d = null;
11777
11778                         decl.type = instDeclaration;
11779                         decl.inst = inst;
11780                      }
11781                   }
11782                }
11783                for(subType = type; subType;)
11784                {
11785                   if(subType.kind == classType)
11786                   {
11787                      declareStruct = true;
11788                      break;
11789                   }
11790                   else if(subType.kind == pointerType)
11791                      break;
11792                   else if(subType.kind == arrayType)
11793                      subType = subType.arrayType;
11794                   else
11795                      break;
11796                }
11797
11798                FreeType(type);
11799                if(!d) break;
11800             }
11801          }
11802
11803          if(decl.specifiers)
11804          {
11805             Specifier s;
11806             for(s = decl.specifiers->first; s; s = s.next)
11807             {
11808                ProcessSpecifier(s, declareStruct);
11809             }
11810          }
11811          break;
11812       }
11813       case instDeclaration:
11814       {
11815          ProcessInstantiationType(decl.inst);
11816          break;
11817       }
11818       case structDeclaration:
11819       {
11820          Specifier spec;
11821          Declarator d;
11822          bool declareStruct = false;
11823
11824          if(decl.declarators)
11825          {
11826             for(d = decl.declarators->first; d; d = d.next)
11827             {
11828                Type type = ProcessType(decl.specifiers, d.declarator);
11829                Type subType;
11830                ProcessDeclarator(d);
11831                for(subType = type; subType;)
11832                {
11833                   if(subType.kind == classType)
11834                   {
11835                      declareStruct = true;
11836                      break;
11837                   }
11838                   else if(subType.kind == pointerType)
11839                      break;
11840                   else if(subType.kind == arrayType)
11841                      subType = subType.arrayType;
11842                   else
11843                      break;
11844                }
11845                FreeType(type);
11846             }
11847          }
11848          if(decl.specifiers)
11849          {
11850             for(spec = decl.specifiers->first; spec; spec = spec.next)
11851                ProcessSpecifier(spec, declareStruct);
11852          }
11853          break;
11854       }
11855    }
11856 }
11857
11858 static FunctionDefinition curFunction;
11859
11860 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11861 {
11862    char propName[1024], propNameM[1024];
11863    char getName[1024], setName[1024];
11864    OldList * args;
11865
11866    DeclareProperty(prop, setName, getName);
11867
11868    // eInstance_FireWatchers(object, prop);
11869    strcpy(propName, "__ecereProp_");
11870    FullClassNameCat(propName, prop._class.fullName, false);
11871    strcat(propName, "_");
11872    // strcat(propName, prop.name);
11873    FullClassNameCat(propName, prop.name, true);
11874    //MangleClassName(propName);
11875
11876    strcpy(propNameM, "__ecerePropM_");
11877    FullClassNameCat(propNameM, prop._class.fullName, false);
11878    strcat(propNameM, "_");
11879    // strcat(propNameM, prop.name);
11880    FullClassNameCat(propNameM, prop.name, true);
11881    //MangleClassName(propNameM);
11882
11883    if(prop.isWatchable)
11884    {
11885       args = MkList();
11886       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11887       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11888       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11889
11890       args = MkList();
11891       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11892       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11893       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11894    }
11895
11896
11897    {
11898       args = MkList();
11899       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11900       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11901       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11902
11903       args = MkList();
11904       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11905       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11906       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11907    }
11908
11909    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11910       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11911       curFunction.propSet.fireWatchersDone = true;
11912 }
11913
11914 static void ProcessStatement(Statement stmt)
11915 {
11916    yylloc = stmt.loc;
11917    /*
11918    lineNum = stmt.pos.line;
11919    column = stmt.pos.col;
11920    */
11921    switch(stmt.type)
11922    {
11923       case labeledStmt:
11924          ProcessStatement(stmt.labeled.stmt);
11925          break;
11926       case caseStmt:
11927          // This expression should be constant...
11928          if(stmt.caseStmt.exp)
11929          {
11930             FreeType(stmt.caseStmt.exp.destType);
11931             stmt.caseStmt.exp.destType = curSwitchType;
11932             if(curSwitchType) curSwitchType.refCount++;
11933             ProcessExpressionType(stmt.caseStmt.exp);
11934             ComputeExpression(stmt.caseStmt.exp);
11935          }
11936          if(stmt.caseStmt.stmt)
11937             ProcessStatement(stmt.caseStmt.stmt);
11938          break;
11939       case compoundStmt:
11940       {
11941          if(stmt.compound.context)
11942          {
11943             Declaration decl;
11944             Statement s;
11945
11946             Statement prevCompound = curCompound;
11947             Context prevContext = curContext;
11948
11949             if(!stmt.compound.isSwitch)
11950                curCompound = stmt;
11951             curContext = stmt.compound.context;
11952
11953             if(stmt.compound.declarations)
11954             {
11955                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11956                   ProcessDeclaration(decl);
11957             }
11958             if(stmt.compound.statements)
11959             {
11960                for(s = stmt.compound.statements->first; s; s = s.next)
11961                   ProcessStatement(s);
11962             }
11963
11964             curContext = prevContext;
11965             curCompound = prevCompound;
11966          }
11967          break;
11968       }
11969       case expressionStmt:
11970       {
11971          Expression exp;
11972          if(stmt.expressions)
11973          {
11974             for(exp = stmt.expressions->first; exp; exp = exp.next)
11975                ProcessExpressionType(exp);
11976          }
11977          break;
11978       }
11979       case ifStmt:
11980       {
11981          Expression exp;
11982
11983          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11984          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11985          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11986          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11987          {
11988             ProcessExpressionType(exp);
11989          }
11990          if(stmt.ifStmt.stmt)
11991             ProcessStatement(stmt.ifStmt.stmt);
11992          if(stmt.ifStmt.elseStmt)
11993             ProcessStatement(stmt.ifStmt.elseStmt);
11994          break;
11995       }
11996       case switchStmt:
11997       {
11998          Type oldSwitchType = curSwitchType;
11999          if(stmt.switchStmt.exp)
12000          {
12001             Expression exp;
12002             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
12003             {
12004                if(!exp.next)
12005                {
12006                   /*
12007                   Type destType
12008                   {
12009                      kind = intType;
12010                      refCount = 1;
12011                   };
12012                   e.exp.destType = destType;
12013                   */
12014
12015                   ProcessExpressionType(exp);
12016                }
12017                if(!exp.next)
12018                   curSwitchType = exp.expType;
12019             }
12020          }
12021          ProcessStatement(stmt.switchStmt.stmt);
12022          curSwitchType = oldSwitchType;
12023          break;
12024       }
12025       case whileStmt:
12026       {
12027          if(stmt.whileStmt.exp)
12028          {
12029             Expression exp;
12030
12031             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
12032             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
12033             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
12034             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
12035             {
12036                ProcessExpressionType(exp);
12037             }
12038          }
12039          if(stmt.whileStmt.stmt)
12040             ProcessStatement(stmt.whileStmt.stmt);
12041          break;
12042       }
12043       case doWhileStmt:
12044       {
12045          if(stmt.doWhile.exp)
12046          {
12047             Expression exp;
12048
12049             if(stmt.doWhile.exp->last)
12050             {
12051                FreeType(((Expression)stmt.doWhile.exp->last).destType);
12052                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
12053                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
12054             }
12055             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
12056             {
12057                ProcessExpressionType(exp);
12058             }
12059          }
12060          if(stmt.doWhile.stmt)
12061             ProcessStatement(stmt.doWhile.stmt);
12062          break;
12063       }
12064       case forStmt:
12065       {
12066          Expression exp;
12067          if(stmt.forStmt.init)
12068             ProcessStatement(stmt.forStmt.init);
12069
12070          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
12071          {
12072             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
12073             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
12074             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
12075          }
12076
12077          if(stmt.forStmt.check)
12078             ProcessStatement(stmt.forStmt.check);
12079          if(stmt.forStmt.increment)
12080          {
12081             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
12082                ProcessExpressionType(exp);
12083          }
12084
12085          if(stmt.forStmt.stmt)
12086             ProcessStatement(stmt.forStmt.stmt);
12087          break;
12088       }
12089       case forEachStmt:
12090       {
12091          Identifier id = stmt.forEachStmt.id;
12092          OldList * exp = stmt.forEachStmt.exp;
12093          OldList * filter = stmt.forEachStmt.filter;
12094          Statement block = stmt.forEachStmt.stmt;
12095          char iteratorType[1024];
12096          Type source;
12097          Expression e;
12098          bool isBuiltin = exp && exp->last &&
12099             (((Expression)exp->last).type == ExpressionType::arrayExp ||
12100               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
12101          Expression arrayExp;
12102          const char * typeString = null;
12103          int builtinCount = 0;
12104
12105          for(e = exp ? exp->first : null; e; e = e.next)
12106          {
12107             if(!e.next)
12108             {
12109                FreeType(e.destType);
12110                e.destType = ProcessTypeString("Container", false);
12111             }
12112             if(!isBuiltin || e.next)
12113                ProcessExpressionType(e);
12114          }
12115
12116          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
12117          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
12118             eClass_IsDerived(source._class.registered, containerClass)))
12119          {
12120             Class _class = source ? source._class.registered : null;
12121             Symbol symbol;
12122             Expression expIt = null;
12123             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
12124             Class arrayClass = eSystem_FindClass(privateModule, "Array");
12125             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
12126             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
12127
12128             if(inCompiler)
12129             {
12130                stmt.type = compoundStmt;
12131
12132                stmt.compound.context = Context { };
12133                stmt.compound.context.parent = curContext;
12134                curContext = stmt.compound.context;
12135             }
12136
12137             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
12138             {
12139                Class mapClass = eSystem_FindClass(privateModule, "Map");
12140                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
12141                isCustomAVLTree = true;
12142                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
12143                   isAVLTree = true;
12144                else */if(eClass_IsDerived(source._class.registered, mapClass))
12145                   isMap = true;
12146             }
12147             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
12148             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
12149             {
12150                Class listClass = eSystem_FindClass(privateModule, "List");
12151                isLinkList = true;
12152                isList = eClass_IsDerived(source._class.registered, listClass);
12153             }
12154
12155             if(inCompiler && isArray)
12156             {
12157                Declarator decl;
12158                OldList * specs = MkList();
12159                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12160                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12161                stmt.compound.declarations = MkListOne(
12162                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12163                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12164                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
12165                      MkInitializerAssignment(MkExpBrackets(exp))))));
12166             }
12167             else if(isBuiltin)
12168             {
12169                Type type = null;
12170                char typeStringBuf[1024];
12171
12172                // TODO: Merge this code?
12173                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
12174                if(((Expression)exp->last).type == castExp)
12175                {
12176                   TypeName typeName = ((Expression)exp->last).cast.typeName;
12177                   if(typeName)
12178                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
12179                }
12180
12181                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
12182                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
12183                   arrayExp.destType._class.registered.templateArgs)
12184                {
12185                   Class templateClass = arrayExp.destType._class.registered;
12186                   typeString = templateClass.templateArgs[2].dataTypeString;
12187                }
12188                else if(arrayExp.list)
12189                {
12190                   // Guess type from expressions in the array
12191                   Expression e;
12192                   for(e = arrayExp.list->first; e; e = e.next)
12193                   {
12194                      ProcessExpressionType(e);
12195                      if(e.expType)
12196                      {
12197                         if(!type) { type = e.expType; type.refCount++; }
12198                         else
12199                         {
12200                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
12201                            if(!MatchTypeExpression(e, type, null, false, true))
12202                            {
12203                               FreeType(type);
12204                               type = e.expType;
12205                               e.expType = null;
12206
12207                               e = arrayExp.list->first;
12208                               ProcessExpressionType(e);
12209                               if(e.expType)
12210                               {
12211                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
12212                                  if(!MatchTypeExpression(e, type, null, false, true))
12213                                  {
12214                                     FreeType(e.expType);
12215                                     e.expType = null;
12216                                     FreeType(type);
12217                                     type = null;
12218                                     break;
12219                                  }
12220                               }
12221                            }
12222                         }
12223                         if(e.expType)
12224                         {
12225                            FreeType(e.expType);
12226                            e.expType = null;
12227                         }
12228                      }
12229                   }
12230                   if(type)
12231                   {
12232                      typeStringBuf[0] = '\0';
12233                      PrintType(type, typeStringBuf, false, true);
12234                      typeString = typeStringBuf;
12235                      FreeType(type);
12236                   }
12237                }
12238                if(typeString)
12239                {
12240                   if(inCompiler)
12241                   {
12242                      OldList * initializers = MkList();
12243                      Declarator decl;
12244                      OldList * specs = MkList();
12245                      if(arrayExp.list)
12246                      {
12247                         Expression e;
12248
12249                         builtinCount = arrayExp.list->count;
12250                         type = ProcessTypeString(typeString, false);
12251                         while((e = arrayExp.list->first))
12252                         {
12253                            arrayExp.list->Remove(e);
12254                            e.destType = type;
12255                            type.refCount++;
12256                            ProcessExpressionType(e);
12257                            if(inCompiler)
12258                               ListAdd(initializers, MkInitializerAssignment(e));
12259                         }
12260                         FreeType(type);
12261                         delete arrayExp.list;
12262                      }
12263                      decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12264
12265                      stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12266                         MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12267
12268                      ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12269                         PlugDeclarator(
12270                            /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12271                            ), MkInitializerList(initializers)))));
12272                      FreeList(exp, FreeExpression);
12273                   }
12274                   else if(arrayExp.list)
12275                   {
12276                      Expression e;
12277                      type = ProcessTypeString(typeString, false);
12278                      for(e = arrayExp.list->first; e; e = e.next)
12279                      {
12280                         e.destType = type;
12281                         type.refCount++;
12282                         ProcessExpressionType(e);
12283                      }
12284                      FreeType(type);
12285                   }
12286                }
12287                else
12288                {
12289                   arrayExp.expType = ProcessTypeString("Container", false);
12290                   Compiler_Error($"Couldn't determine type of array elements\n");
12291                }
12292
12293                /*
12294                Declarator decl;
12295                OldList * specs = MkList();
12296
12297                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12298                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12299                stmt.compound.declarations = MkListOne(
12300                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12301                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12302                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12303                      MkInitializerAssignment(MkExpBrackets(exp))))));
12304                */
12305             }
12306             else if(inCompiler && isLinkList && !isList)
12307             {
12308                Declarator decl;
12309                OldList * specs = MkList();
12310                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12311                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12312                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12313                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12314                      MkInitializerAssignment(MkExpBrackets(exp))))));
12315             }
12316             /*else if(isCustomAVLTree)
12317             {
12318                Declarator decl;
12319                OldList * specs = MkList();
12320                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12321                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12322                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12323                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12324                      MkInitializerAssignment(MkExpBrackets(exp))))));
12325             }*/
12326             else if(inCompiler && _class.templateArgs)
12327             {
12328                if(isMap)
12329                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12330                else
12331                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12332
12333                stmt.compound.declarations = MkListOne(
12334                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12335                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12336                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12337             }
12338             if(inCompiler)
12339             {
12340                symbol = FindSymbol(id.string, curContext, curContext, false, false);
12341
12342                if(block)
12343                {
12344                   // Reparent sub-contexts in this statement
12345                   switch(block.type)
12346                   {
12347                      case compoundStmt:
12348                         if(block.compound.context)
12349                            block.compound.context.parent = stmt.compound.context;
12350                         break;
12351                      case ifStmt:
12352                         if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12353                            block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12354                         if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12355                            block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12356                         break;
12357                      case switchStmt:
12358                         if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12359                            block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12360                         break;
12361                      case whileStmt:
12362                         if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12363                            block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12364                         break;
12365                      case doWhileStmt:
12366                         if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12367                            block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12368                         break;
12369                      case forStmt:
12370                         if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12371                            block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12372                         break;
12373                      case forEachStmt:
12374                         if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12375                            block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12376                         break;
12377                      /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12378                      case labeledStmt:
12379                      case caseStmt
12380                      case expressionStmt:
12381                      case gotoStmt:
12382                      case continueStmt:
12383                      case breakStmt
12384                      case returnStmt:
12385                      case asmStmt:
12386                      case badDeclarationStmt:
12387                      case fireWatchersStmt:
12388                      case stopWatchingStmt:
12389                      case watchStmt:
12390                      */
12391                   }
12392                }
12393
12394                if(filter)
12395                {
12396                   block = MkIfStmt(filter, block, null);
12397                }
12398                if(isArray)
12399                {
12400                   stmt.compound.statements = MkListOne(MkForStmt(
12401                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12402                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12403                         MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12404                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12405                      block));
12406                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12407                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12408                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12409                }
12410                else if(isBuiltin)
12411                {
12412                   char count[128];
12413                   //OldList * specs = MkList();
12414                   // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12415
12416                   sprintf(count, "%d", builtinCount);
12417
12418                   stmt.compound.statements = MkListOne(MkForStmt(
12419                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12420                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12421                         MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12422                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12423                      block));
12424
12425                   /*
12426                   Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12427                   stmt.compound.statements = MkListOne(MkForStmt(
12428                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12429                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12430                         MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12431                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12432                      block));
12433                  */
12434                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12435                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12436                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12437                }
12438                else if(isLinkList && !isList)
12439                {
12440                   Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12441                   Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12442                   if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12443                      !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12444                   {
12445                      stmt.compound.statements = MkListOne(MkForStmt(
12446                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12447                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12448                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12449                         block));
12450                   }
12451                   else
12452                   {
12453                      OldList * specs = MkList();
12454                      Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12455                      stmt.compound.statements = MkListOne(MkForStmt(
12456                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12457                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12458                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12459                            MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12460                               MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12461                         block));
12462                   }
12463                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12464                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12465                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12466                }
12467                /*else if(isCustomAVLTree)
12468                {
12469                   stmt.compound.statements = MkListOne(MkForStmt(
12470                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12471                         MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12472                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12473                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12474                      block));
12475
12476                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12477                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12478                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12479                }*/
12480                else
12481                {
12482                   stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12483                      MkIdentifier("Next")), null)), block));
12484                }
12485                ProcessExpressionType(expIt);
12486                if(stmt.compound.declarations->first)
12487                   ProcessDeclaration(stmt.compound.declarations->first);
12488
12489                if(symbol)
12490                   symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12491
12492                ProcessStatement(stmt);
12493             }
12494             else
12495                ProcessStatement(stmt.forEachStmt.stmt);
12496             if(inCompiler)
12497                curContext = stmt.compound.context.parent;
12498             break;
12499          }
12500          else
12501          {
12502             Compiler_Error($"Expression is not a container\n");
12503          }
12504          break;
12505       }
12506       case gotoStmt:
12507          break;
12508       case continueStmt:
12509          break;
12510       case breakStmt:
12511          break;
12512       case returnStmt:
12513       {
12514          Expression exp;
12515          if(stmt.expressions)
12516          {
12517             for(exp = stmt.expressions->first; exp; exp = exp.next)
12518             {
12519                if(!exp.next)
12520                {
12521                   if(curFunction && !curFunction.type)
12522                      curFunction.type = ProcessType(
12523                         curFunction.specifiers, curFunction.declarator);
12524                   FreeType(exp.destType);
12525                   // TODO: current property if not compiling
12526                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12527                   if(exp.destType) exp.destType.refCount++;
12528                }
12529                ProcessExpressionType(exp);
12530             }
12531          }
12532          break;
12533       }
12534       case badDeclarationStmt:
12535       {
12536          ProcessDeclaration(stmt.decl);
12537          break;
12538       }
12539       case asmStmt:
12540       {
12541          AsmField field;
12542          if(stmt.asmStmt.inputFields)
12543          {
12544             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12545                if(field.expression)
12546                   ProcessExpressionType(field.expression);
12547          }
12548          if(stmt.asmStmt.outputFields)
12549          {
12550             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12551                if(field.expression)
12552                   ProcessExpressionType(field.expression);
12553          }
12554          if(stmt.asmStmt.clobberedFields)
12555          {
12556             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12557             {
12558                if(field.expression)
12559                   ProcessExpressionType(field.expression);
12560             }
12561          }
12562          break;
12563       }
12564       case watchStmt:
12565       {
12566          PropertyWatch propWatch;
12567          OldList * watches = stmt._watch.watches;
12568          Expression object = stmt._watch.object;
12569          Expression watcher = stmt._watch.watcher;
12570          if(watcher)
12571             ProcessExpressionType(watcher);
12572          if(object)
12573             ProcessExpressionType(object);
12574
12575          if(inCompiler)
12576          {
12577             if(watcher || thisClass)
12578             {
12579                External external = curExternal;
12580                Context context = curContext;
12581
12582                stmt.type = expressionStmt;
12583                stmt.expressions = MkList();
12584
12585                curExternal = external.prev;
12586
12587                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12588                {
12589                   ClassFunction func;
12590                   char watcherName[1024];
12591                   Class watcherClass = watcher ?
12592                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12593                   External createdExternal;
12594
12595                   // Create a declaration above
12596                   External externalDecl = MkExternalDeclaration(null);
12597                   ast->Insert(curExternal.prev, externalDecl);
12598
12599                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12600                   if(propWatch.deleteWatch)
12601                      strcat(watcherName, "_delete");
12602                   else
12603                   {
12604                      Identifier propID;
12605                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12606                      {
12607                         strcat(watcherName, "_");
12608                         strcat(watcherName, propID.string);
12609                      }
12610                   }
12611
12612                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12613                   {
12614                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12615                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12616                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12617                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12618                      ProcessClassFunctionBody(func, propWatch.compound);
12619                      propWatch.compound = null;
12620
12621                      //afterExternal = afterExternal ? afterExternal : curExternal;
12622
12623                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12624                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12625                      // TESTING THIS...
12626                      createdExternal.symbol.idCode = external.symbol.idCode;
12627
12628                      curExternal = createdExternal;
12629                      ProcessFunction(createdExternal.function);
12630
12631
12632                      // Create a declaration above
12633                      {
12634                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12635                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12636                         externalDecl.declaration = decl;
12637                         if(decl.symbol && !decl.symbol.pointerExternal)
12638                            decl.symbol.pointerExternal = externalDecl;
12639                      }
12640
12641                      if(propWatch.deleteWatch)
12642                      {
12643                         OldList * args = MkList();
12644                         ListAdd(args, CopyExpression(object));
12645                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12646                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12647                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12648                      }
12649                      else
12650                      {
12651                         Class _class = object.expType._class.registered;
12652                         Identifier propID;
12653
12654                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12655                         {
12656                            char propName[1024];
12657                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12658                            if(prop)
12659                            {
12660                               char getName[1024], setName[1024];
12661                               OldList * args = MkList();
12662
12663                               DeclareProperty(prop, setName, getName);
12664
12665                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12666                               strcpy(propName, "__ecereProp_");
12667                               FullClassNameCat(propName, prop._class.fullName, false);
12668                               strcat(propName, "_");
12669                               // strcat(propName, prop.name);
12670                               FullClassNameCat(propName, prop.name, true);
12671
12672                               ListAdd(args, CopyExpression(object));
12673                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12674                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12675                               ListAdd(args, MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpIdentifier(MkIdentifier(watcherName))));
12676
12677                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12678                            }
12679                            else
12680                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12681                         }
12682                      }
12683                   }
12684                   else
12685                      Compiler_Error($"Invalid watched object\n");
12686                }
12687
12688                curExternal = external;
12689                curContext = context;
12690
12691                if(watcher)
12692                   FreeExpression(watcher);
12693                if(object)
12694                   FreeExpression(object);
12695                FreeList(watches, FreePropertyWatch);
12696             }
12697             else
12698                Compiler_Error($"No observer specified and not inside a _class\n");
12699          }
12700          else
12701          {
12702             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12703             {
12704                ProcessStatement(propWatch.compound);
12705             }
12706
12707          }
12708          break;
12709       }
12710       case fireWatchersStmt:
12711       {
12712          OldList * watches = stmt._watch.watches;
12713          Expression object = stmt._watch.object;
12714          Class _class;
12715          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12716          // printf("%X\n", watches);
12717          // printf("%X\n", stmt._watch.watches);
12718          if(object)
12719             ProcessExpressionType(object);
12720
12721          if(inCompiler)
12722          {
12723             _class = object ?
12724                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12725
12726             if(_class)
12727             {
12728                Identifier propID;
12729
12730                stmt.type = expressionStmt;
12731                stmt.expressions = MkList();
12732
12733                // Check if we're inside a property set
12734                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12735                {
12736                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12737                }
12738                else if(!watches)
12739                {
12740                   //Compiler_Error($"No property specified and not inside a property set\n");
12741                }
12742                if(watches)
12743                {
12744                   for(propID = watches->first; propID; propID = propID.next)
12745                   {
12746                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12747                      if(prop)
12748                      {
12749                         CreateFireWatcher(prop, object, stmt);
12750                      }
12751                      else
12752                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12753                   }
12754                }
12755                else
12756                {
12757                   // Fire all properties!
12758                   Property prop;
12759                   Class base;
12760                   for(base = _class; base; base = base.base)
12761                   {
12762                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12763                      {
12764                         if(prop.isProperty && prop.isWatchable)
12765                         {
12766                            CreateFireWatcher(prop, object, stmt);
12767                         }
12768                      }
12769                   }
12770                }
12771
12772                if(object)
12773                   FreeExpression(object);
12774                FreeList(watches, FreeIdentifier);
12775             }
12776             else
12777                Compiler_Error($"Invalid object specified and not inside a class\n");
12778          }
12779          break;
12780       }
12781       case stopWatchingStmt:
12782       {
12783          OldList * watches = stmt._watch.watches;
12784          Expression object = stmt._watch.object;
12785          Expression watcher = stmt._watch.watcher;
12786          Class _class;
12787          if(object)
12788             ProcessExpressionType(object);
12789          if(watcher)
12790             ProcessExpressionType(watcher);
12791          if(inCompiler)
12792          {
12793             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12794
12795             if(watcher || thisClass)
12796             {
12797                if(_class)
12798                {
12799                   Identifier propID;
12800
12801                   stmt.type = expressionStmt;
12802                   stmt.expressions = MkList();
12803
12804                   if(!watches)
12805                   {
12806                      OldList * args;
12807                      // eInstance_StopWatching(object, null, watcher);
12808                      args = MkList();
12809                      ListAdd(args, CopyExpression(object));
12810                      ListAdd(args, MkExpConstant("0"));
12811                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12812                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12813                   }
12814                   else
12815                   {
12816                      for(propID = watches->first; propID; propID = propID.next)
12817                      {
12818                         char propName[1024];
12819                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12820                         if(prop)
12821                         {
12822                            char getName[1024], setName[1024];
12823                            OldList * args = MkList();
12824
12825                            DeclareProperty(prop, setName, getName);
12826
12827                            // eInstance_StopWatching(object, prop, watcher);
12828                            strcpy(propName, "__ecereProp_");
12829                            FullClassNameCat(propName, prop._class.fullName, false);
12830                            strcat(propName, "_");
12831                            // strcat(propName, prop.name);
12832                            FullClassNameCat(propName, prop.name, true);
12833                            //MangleClassName(propName);
12834
12835                            ListAdd(args, CopyExpression(object));
12836                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12837                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12838                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12839                         }
12840                         else
12841                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12842                      }
12843                   }
12844
12845                   if(object)
12846                      FreeExpression(object);
12847                   if(watcher)
12848                      FreeExpression(watcher);
12849                   FreeList(watches, FreeIdentifier);
12850                }
12851                else
12852                   Compiler_Error($"Invalid object specified and not inside a class\n");
12853             }
12854             else
12855                Compiler_Error($"No observer specified and not inside a class\n");
12856          }
12857          break;
12858       }
12859    }
12860 }
12861
12862 static void ProcessFunction(FunctionDefinition function)
12863 {
12864    Identifier id = GetDeclId(function.declarator);
12865    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12866    Type type = symbol ? symbol.type : null;
12867    Class oldThisClass = thisClass;
12868    Context oldTopContext = topContext;
12869
12870    yylloc = function.loc;
12871    // Process thisClass
12872
12873    if(type && type.thisClass)
12874    {
12875       Symbol classSym = type.thisClass;
12876       Class _class = type.thisClass.registered;
12877       char className[1024];
12878       char structName[1024];
12879       Declarator funcDecl;
12880       Symbol thisSymbol;
12881
12882       bool typedObject = false;
12883
12884       if(_class && !_class.base)
12885       {
12886          _class = currentClass;
12887          if(_class && !_class.symbol)
12888             _class.symbol = FindClass(_class.fullName);
12889          classSym = _class ? _class.symbol : null;
12890          typedObject = true;
12891       }
12892
12893       thisClass = _class;
12894
12895       if(inCompiler && _class)
12896       {
12897          if(type.kind == functionType)
12898          {
12899             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12900             {
12901                //TypeName param = symbol.type.params.first;
12902                Type param = symbol.type.params.first;
12903                symbol.type.params.Remove(param);
12904                //FreeTypeName(param);
12905                FreeType(param);
12906             }
12907             if(type.classObjectType != classPointer)
12908             {
12909                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12910                symbol.type.staticMethod = true;
12911                symbol.type.thisClass = null;
12912
12913                // HIGH DANGER: VERIFYING THIS...
12914                symbol.type.extraParam = false;
12915             }
12916          }
12917
12918          strcpy(className, "__ecereClass_");
12919          FullClassNameCat(className, _class.fullName, true);
12920
12921          //MangleClassName(className);
12922
12923          structName[0] = 0;
12924          FullClassNameCat(structName, _class.fullName, false);
12925
12926          // [class] this
12927
12928
12929          funcDecl = GetFuncDecl(function.declarator);
12930          if(funcDecl)
12931          {
12932             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12933             {
12934                TypeName param = funcDecl.function.parameters->first;
12935                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12936                {
12937                   funcDecl.function.parameters->Remove(param);
12938                   FreeTypeName(param);
12939                }
12940             }
12941
12942             // DANGER: Watch for this... Check if it's a Conversion?
12943             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12944
12945             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12946             if(!function.propertyNoThis)
12947             {
12948                TypeName thisParam = null;
12949
12950                if(type.classObjectType != classPointer)
12951                {
12952                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12953                   if(!funcDecl.function.parameters)
12954                      funcDecl.function.parameters = MkList();
12955                   funcDecl.function.parameters->Insert(null, thisParam);
12956                }
12957
12958                if(typedObject)
12959                {
12960                   if(type.classObjectType != classPointer)
12961                   {
12962                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12963                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12964                   }
12965
12966                   thisParam = TypeName
12967                   {
12968                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12969                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12970                   };
12971                   funcDecl.function.parameters->Insert(null, thisParam);
12972                }
12973             }
12974          }
12975
12976          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12977          {
12978             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12979             funcDecl = GetFuncDecl(initDecl.declarator);
12980             if(funcDecl)
12981             {
12982                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12983                {
12984                   TypeName param = funcDecl.function.parameters->first;
12985                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12986                   {
12987                      funcDecl.function.parameters->Remove(param);
12988                      FreeTypeName(param);
12989                   }
12990                }
12991
12992                if(type.classObjectType != classPointer)
12993                {
12994                   // DANGER: Watch for this... Check if it's a Conversion?
12995                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12996                   {
12997                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12998
12999                      if(!funcDecl.function.parameters)
13000                         funcDecl.function.parameters = MkList();
13001                      funcDecl.function.parameters->Insert(null, thisParam);
13002                   }
13003                }
13004             }
13005          }
13006       }
13007
13008       // Add this to the context
13009       if(function.body)
13010       {
13011          if(type.classObjectType != classPointer)
13012          {
13013             thisSymbol = Symbol
13014             {
13015                string = CopyString("this");
13016                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
13017             };
13018             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
13019
13020             if(typedObject && thisSymbol.type)
13021             {
13022                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
13023                thisSymbol.type.byReference = type.byReference;
13024                thisSymbol.type.typedByReference = type.byReference;
13025                /*
13026                thisSymbol = Symbol { string = CopyString("class") };
13027                function.body.compound.context.symbols.Add(thisSymbol);
13028                */
13029             }
13030          }
13031       }
13032
13033       // Pointer to class data
13034
13035       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
13036       {
13037          DataMember member = null;
13038          {
13039             Class base;
13040             for(base = _class; base && base.type != systemClass; base = base.next)
13041             {
13042                for(member = base.membersAndProperties.first; member; member = member.next)
13043                   if(!member.isProperty)
13044                      break;
13045                if(member)
13046                   break;
13047             }
13048          }
13049          for(member = _class.membersAndProperties.first; member; member = member.next)
13050             if(!member.isProperty)
13051                break;
13052          if(member)
13053          {
13054             char pointerName[1024];
13055
13056             Declaration decl;
13057             Initializer initializer;
13058             Expression exp, bytePtr;
13059
13060             strcpy(pointerName, "__ecerePointer_");
13061             FullClassNameCat(pointerName, _class.fullName, false);
13062             {
13063                char className[1024];
13064                strcpy(className, "__ecereClass_");
13065                FullClassNameCat(className, classSym.string, true);
13066                //MangleClassName(className);
13067
13068                // Testing This
13069                DeclareClass(classSym, className);
13070             }
13071
13072             // ((byte *) this)
13073             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
13074
13075             if(_class.fixed)
13076             {
13077                char string[256];
13078                sprintf(string, "%d", _class.offset);
13079                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
13080             }
13081             else
13082             {
13083                // ([bytePtr] + [className]->offset)
13084                exp = QBrackets(MkExpOp(bytePtr, '+',
13085                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
13086             }
13087
13088             // (this ? [exp] : 0)
13089             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
13090             exp.expType = Type
13091             {
13092                refCount = 1;
13093                kind = pointerType;
13094                type = Type { refCount = 1, kind = voidType };
13095             };
13096
13097             if(function.body)
13098             {
13099                yylloc = function.body.loc;
13100                // ([structName] *) [exp]
13101                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
13102                initializer = MkInitializerAssignment(
13103                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
13104
13105                // [structName] * [pointerName] = [initializer];
13106                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
13107
13108                {
13109                   Context prevContext = curContext;
13110                   OldList * list;
13111                   curContext = function.body.compound.context;
13112
13113                   decl = MkDeclaration((list = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null))),
13114                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
13115                   list->Insert(null, MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
13116
13117                   curContext = prevContext;
13118                }
13119
13120                // WHY?
13121                decl.symbol = null;
13122
13123                if(!function.body.compound.declarations)
13124                   function.body.compound.declarations = MkList();
13125                function.body.compound.declarations->Insert(null, decl);
13126             }
13127          }
13128       }
13129
13130
13131       // Loop through the function and replace undeclared identifiers
13132       // which are a member of the class (methods, properties or data)
13133       // by "this.[member]"
13134    }
13135    else
13136       thisClass = null;
13137
13138    if(id)
13139    {
13140       FreeSpecifier(id._class);
13141       id._class = null;
13142
13143       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
13144       {
13145          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
13146          id = GetDeclId(initDecl.declarator);
13147
13148          FreeSpecifier(id._class);
13149          id._class = null;
13150       }
13151    }
13152    if(function.body)
13153       topContext = function.body.compound.context;
13154    {
13155       FunctionDefinition oldFunction = curFunction;
13156       curFunction = function;
13157       if(function.body)
13158          ProcessStatement(function.body);
13159
13160       // If this is a property set and no firewatchers has been done yet, add one here
13161       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
13162       {
13163          Statement prevCompound = curCompound;
13164          Context prevContext = curContext;
13165
13166          Statement fireWatchers = MkFireWatchersStmt(null, null);
13167          if(!function.body.compound.statements) function.body.compound.statements = MkList();
13168          ListAdd(function.body.compound.statements, fireWatchers);
13169
13170          curCompound = function.body;
13171          curContext = function.body.compound.context;
13172
13173          ProcessStatement(fireWatchers);
13174
13175          curContext = prevContext;
13176          curCompound = prevCompound;
13177
13178       }
13179
13180       curFunction = oldFunction;
13181    }
13182
13183    if(function.declarator)
13184    {
13185       ProcessDeclarator(function.declarator);
13186    }
13187
13188    topContext = oldTopContext;
13189    thisClass = oldThisClass;
13190 }
13191
13192 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
13193 static void ProcessClass(OldList definitions, Symbol symbol)
13194 {
13195    ClassDef def;
13196    External external = curExternal;
13197    Class regClass = symbol ? symbol.registered : null;
13198
13199    // Process all functions
13200    for(def = definitions.first; def; def = def.next)
13201    {
13202       if(def.type == functionClassDef)
13203       {
13204          if(def.function.declarator)
13205             curExternal = def.function.declarator.symbol.pointerExternal;
13206          else
13207             curExternal = external;
13208
13209          ProcessFunction((FunctionDefinition)def.function);
13210       }
13211       else if(def.type == declarationClassDef)
13212       {
13213          if(def.decl.type == instDeclaration)
13214          {
13215             thisClass = regClass;
13216             ProcessInstantiationType(def.decl.inst);
13217             thisClass = null;
13218          }
13219          // Testing this
13220          else
13221          {
13222             Class backThisClass = thisClass;
13223             if(regClass) thisClass = regClass;
13224             ProcessDeclaration(def.decl);
13225             thisClass = backThisClass;
13226          }
13227       }
13228       else if(def.type == defaultPropertiesClassDef && def.defProperties)
13229       {
13230          MemberInit defProperty;
13231
13232          // Add this to the context
13233          Symbol thisSymbol = Symbol
13234          {
13235             string = CopyString("this");
13236             type = regClass ? MkClassType(regClass.fullName) : null;
13237          };
13238          globalContext.symbols.Add((BTNode)thisSymbol);
13239
13240          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
13241          {
13242             thisClass = regClass;
13243             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
13244             thisClass = null;
13245          }
13246
13247          globalContext.symbols.Remove((BTNode)thisSymbol);
13248          FreeSymbol(thisSymbol);
13249       }
13250       else if(def.type == propertyClassDef && def.propertyDef)
13251       {
13252          PropertyDef prop = def.propertyDef;
13253
13254          // Add this to the context
13255          /*
13256          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
13257          globalContext.symbols.Add(thisSymbol);
13258          */
13259
13260          thisClass = regClass;
13261          if(prop.setStmt)
13262          {
13263             if(regClass)
13264             {
13265                Symbol thisSymbol
13266                {
13267                   string = CopyString("this");
13268                   type = MkClassType(regClass.fullName);
13269                };
13270                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13271             }
13272
13273             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13274             ProcessStatement(prop.setStmt);
13275          }
13276          if(prop.getStmt)
13277          {
13278             if(regClass)
13279             {
13280                Symbol thisSymbol
13281                {
13282                   string = CopyString("this");
13283                   type = MkClassType(regClass.fullName);
13284                };
13285                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13286             }
13287
13288             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13289             ProcessStatement(prop.getStmt);
13290          }
13291          if(prop.issetStmt)
13292          {
13293             if(regClass)
13294             {
13295                Symbol thisSymbol
13296                {
13297                   string = CopyString("this");
13298                   type = MkClassType(regClass.fullName);
13299                };
13300                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13301             }
13302
13303             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13304             ProcessStatement(prop.issetStmt);
13305          }
13306
13307          thisClass = null;
13308
13309          /*
13310          globalContext.symbols.Remove(thisSymbol);
13311          FreeSymbol(thisSymbol);
13312          */
13313       }
13314       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13315       {
13316          PropertyWatch propertyWatch = def.propertyWatch;
13317
13318          thisClass = regClass;
13319          if(propertyWatch.compound)
13320          {
13321             Symbol thisSymbol
13322             {
13323                string = CopyString("this");
13324                type = regClass ? MkClassType(regClass.fullName) : null;
13325             };
13326
13327             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13328
13329             curExternal = null;
13330             ProcessStatement(propertyWatch.compound);
13331          }
13332          thisClass = null;
13333       }
13334    }
13335 }
13336
13337 void DeclareFunctionUtil(const String s)
13338 {
13339    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13340    if(function)
13341    {
13342       char name[1024];
13343       name[0] = 0;
13344       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13345          strcpy(name, "__ecereFunction_");
13346       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13347       DeclareFunction(function, name);
13348    }
13349 }
13350
13351 void ComputeDataTypes()
13352 {
13353    External external;
13354    External temp { };
13355    External after = null;
13356
13357    currentClass = null;
13358
13359    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13360
13361    for(external = ast->first; external; external = external.next)
13362    {
13363       if(external.type == declarationExternal)
13364       {
13365          Declaration decl = external.declaration;
13366          if(decl)
13367          {
13368             OldList * decls = decl.declarators;
13369             if(decls)
13370             {
13371                InitDeclarator initDecl = decls->first;
13372                if(initDecl)
13373                {
13374                   Declarator declarator = initDecl.declarator;
13375                   if(declarator && declarator.type == identifierDeclarator)
13376                   {
13377                      Identifier id = declarator.identifier;
13378                      if(id && id.string)
13379                      {
13380                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
13381                         {
13382                            external.symbol.id = -1001, external.symbol.idCode = -1001;
13383                            after = external;
13384                         }
13385                      }
13386                   }
13387                }
13388             }
13389          }
13390        }
13391    }
13392
13393    {
13394       // Workaround until we have proper toposort for declarations reordering
13395       External e = MkExternalDeclaration(MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Instance"), null)), null));
13396       ast->Insert(after, e);
13397       after = e;
13398    }
13399
13400    temp.symbol = Symbol { id = -1000, idCode = -1000 };
13401    ast->Insert(after, temp);
13402    curExternal = temp;
13403
13404    DeclareFunctionUtil("eSystem_New");
13405    DeclareFunctionUtil("eSystem_New0");
13406    DeclareFunctionUtil("eSystem_Renew");
13407    DeclareFunctionUtil("eSystem_Renew0");
13408    DeclareFunctionUtil("eSystem_Delete");
13409    DeclareFunctionUtil("eClass_GetProperty");
13410    DeclareFunctionUtil("eClass_SetProperty");
13411    DeclareFunctionUtil("eInstance_FireSelfWatchers");
13412    DeclareFunctionUtil("eInstance_SetMethod");
13413    DeclareFunctionUtil("eInstance_IncRef");
13414    DeclareFunctionUtil("eInstance_StopWatching");
13415    DeclareFunctionUtil("eInstance_Watch");
13416    DeclareFunctionUtil("eInstance_FireWatchers");
13417    if(memoryGuard)
13418    {
13419       DeclareFunctionUtil("MemoryGuard_PushLoc");
13420       DeclareFunctionUtil("MemoryGuard_PopLoc");
13421    }
13422
13423    DeclareStruct("ecere::com::Class", false);
13424    DeclareStruct("ecere::com::Instance", false);
13425    DeclareStruct("ecere::com::Property", false);
13426    DeclareStruct("ecere::com::DataMember", false);
13427    DeclareStruct("ecere::com::Method", false);
13428    DeclareStruct("ecere::com::SerialBuffer", false);
13429    DeclareStruct("ecere::com::ClassTemplateArgument", false);
13430
13431    ast->Remove(temp);
13432
13433    for(external = ast->first; external; external = external.next)
13434    {
13435       afterExternal = curExternal = external;
13436       if(external.type == functionExternal)
13437       {
13438          currentClass = external.function._class;
13439          ProcessFunction(external.function);
13440       }
13441       // There shouldn't be any _class member access here anyways...
13442       else if(external.type == declarationExternal)
13443       {
13444          currentClass = null;
13445          if(external.declaration)
13446             ProcessDeclaration(external.declaration);
13447       }
13448       else if(external.type == classExternal)
13449       {
13450          ClassDefinition _class = external._class;
13451          currentClass = external.symbol.registered;
13452          if(_class.definitions)
13453          {
13454             ProcessClass(_class.definitions, _class.symbol);
13455          }
13456          if(inCompiler)
13457          {
13458             // Free class data...
13459             ast->Remove(external);
13460             delete external;
13461          }
13462       }
13463       else if(external.type == nameSpaceExternal)
13464       {
13465          thisNameSpace = external.id.string;
13466       }
13467    }
13468    currentClass = null;
13469    thisNameSpace = null;
13470    curExternal = null;
13471
13472    delete temp.symbol;
13473    delete temp;
13474 }