compiler/libec: Improved detection of incompatible pointer types
[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          ComputeTypeSize(source.type);
3557          ComputeTypeSize(dest.type);
3558          if(source.type.size == dest.type.size && MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3559             return true;
3560       }
3561    }
3562    return false;
3563 }
3564
3565 static void FreeConvert(Conversion convert)
3566 {
3567    if(convert.resultType)
3568       FreeType(convert.resultType);
3569 }
3570
3571 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3572                               char * string, OldList conversions)
3573 {
3574    BTNamedLink link;
3575
3576    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3577    {
3578       Class _class = link.data;
3579       if(_class.type == enumClass)
3580       {
3581          OldList converts { };
3582          Type type { };
3583          type.kind = classType;
3584
3585          if(!_class.symbol)
3586             _class.symbol = FindClass(_class.fullName);
3587          type._class = _class.symbol;
3588
3589          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false, false))
3590          {
3591             NamedLink64 value;
3592             Class enumClass = eSystem_FindClass(privateModule, "enum");
3593             if(enumClass)
3594             {
3595                Class baseClass;
3596                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3597                {
3598                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3599                   for(value = e.values.first; value; value = value.next)
3600                   {
3601                      if(!strcmp(value.name, string))
3602                         break;
3603                   }
3604                   if(value)
3605                   {
3606                      FreeExpContents(sourceExp);
3607                      FreeType(sourceExp.expType);
3608
3609                      sourceExp.isConstant = true;
3610                      sourceExp.expType = MkClassType(baseClass.fullName);
3611                      //if(inCompiler)
3612                      {
3613                         char constant[256];
3614                         sourceExp.type = constantExp;
3615                         if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "short") || !strcmp(baseClass.dataTypeString, "char"))
3616                            sprintf(constant, FORMAT64D, value.data);
3617                         else
3618                            sprintf(constant, FORMAT64HEXLL, value.data);
3619                         sourceExp.constant = CopyString(constant);
3620                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3621                      }
3622
3623                      while(converts.first)
3624                      {
3625                         Conversion convert = converts.first;
3626                         converts.Remove(convert);
3627                         conversions.Add(convert);
3628                      }
3629                      delete type;
3630                      return true;
3631                   }
3632                }
3633             }
3634          }
3635          if(converts.first)
3636             converts.Free(FreeConvert);
3637          delete type;
3638       }
3639    }
3640    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3641       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3642          return true;
3643    return false;
3644 }
3645
3646 public bool ModuleVisibility(Module searchIn, Module searchFor)
3647 {
3648    SubModule subModule;
3649
3650    if(searchFor == searchIn)
3651       return true;
3652
3653    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3654    {
3655       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3656       {
3657          if(ModuleVisibility(subModule.module, searchFor))
3658             return true;
3659       }
3660    }
3661    return false;
3662 }
3663
3664 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3665 {
3666    Module module;
3667
3668    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3669       return true;
3670    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3671       return true;
3672    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3673       return true;
3674
3675    for(module = mainModule.application.allModules.first; module; module = module.next)
3676    {
3677       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3678          return true;
3679    }
3680    return false;
3681 }
3682
3683 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3684 {
3685    Type source;
3686    Type realDest = dest;
3687    Type backupSourceExpType = null;
3688    Expression computedExp = sourceExp;
3689    dest.refCount++;
3690
3691    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3692       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3693    {
3694       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3695       ComputeExpression(computedExp /*sourceExp*/);
3696    }
3697
3698    source = sourceExp.expType;
3699
3700    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3701    {
3702       if(computedExp != sourceExp)
3703       {
3704          FreeExpression(computedExp);
3705          computedExp = sourceExp;
3706       }
3707       FreeType(dest);
3708       return true;
3709    }
3710
3711    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3712    {
3713        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3714        {
3715           Class sourceBase, destBase;
3716           for(sourceBase = source._class.registered;
3717               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3718               sourceBase = sourceBase.base);
3719           for(destBase = dest._class.registered;
3720               destBase && destBase.base && destBase.base.type != systemClass;
3721               destBase = destBase.base);
3722           //if(source._class.registered == dest._class.registered)
3723           if(sourceBase == destBase)
3724           {
3725             if(computedExp != sourceExp)
3726             {
3727                FreeExpression(computedExp);
3728                computedExp = sourceExp;
3729             }
3730             FreeType(dest);
3731             return true;
3732          }
3733       }
3734    }
3735
3736    if(source)
3737    {
3738       OldList * specs;
3739       bool flag = false;
3740       int64 value = MAXINT;
3741
3742       source.refCount++;
3743
3744       if(computedExp.type == constantExp)
3745       {
3746          if(source.isSigned)
3747             value = strtoll(computedExp.constant, null, 0);
3748          else
3749             value = strtoull(computedExp.constant, null, 0);
3750       }
3751       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3752       {
3753          if(source.isSigned)
3754             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3755          else
3756             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3757       }
3758       if(computedExp != sourceExp)
3759       {
3760          FreeExpression(computedExp);
3761          computedExp = sourceExp;
3762       }
3763
3764       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3765          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3766       {
3767          FreeType(source);
3768          source = Type { kind = intType, isSigned = false, refCount = 1 };
3769       }
3770
3771       if(dest.kind == classType)
3772       {
3773          Class _class = dest._class ? dest._class.registered : null;
3774
3775          if(_class && _class.type == unitClass)
3776          {
3777             if(source.kind != classType)
3778             {
3779                Type tempType { };
3780                Type tempDest, tempSource;
3781
3782                for(; _class.base.type != systemClass; _class = _class.base);
3783                tempSource = dest;
3784                tempDest = tempType;
3785
3786                tempType.kind = classType;
3787                if(!_class.symbol)
3788                   _class.symbol = FindClass(_class.fullName);
3789
3790                tempType._class = _class.symbol;
3791                tempType.truth = dest.truth;
3792                if(tempType._class)
3793                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3794
3795                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3796                backupSourceExpType = sourceExp.expType;
3797                sourceExp.expType = dest; dest.refCount++;
3798                //sourceExp.expType = MkClassType(_class.fullName);
3799                flag = true;
3800
3801                delete tempType;
3802             }
3803          }
3804
3805
3806          // Why wasn't there something like this?
3807          if(_class && _class.type == bitClass && source.kind != classType)
3808          {
3809             if(!dest._class.registered.dataType)
3810                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3811             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3812             {
3813                FreeType(source);
3814                FreeType(sourceExp.expType);
3815                source = sourceExp.expType = MkClassType(dest._class.string);
3816                source.refCount++;
3817
3818                //source.kind = classType;
3819                //source._class = dest._class;
3820             }
3821          }
3822
3823          // Adding two enumerations
3824          /*
3825          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3826          {
3827             if(!source._class.registered.dataType)
3828                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3829             if(!dest._class.registered.dataType)
3830                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3831
3832             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3833             {
3834                FreeType(source);
3835                source = sourceExp.expType = MkClassType(dest._class.string);
3836                source.refCount++;
3837
3838                //source.kind = classType;
3839                //source._class = dest._class;
3840             }
3841          }*/
3842
3843          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3844          {
3845             OldList * specs = MkList();
3846             Declarator decl;
3847             char string[1024];
3848
3849             ReadString(string, sourceExp.string);
3850             decl = SpecDeclFromString(string, specs, null);
3851
3852             FreeExpContents(sourceExp);
3853             FreeType(sourceExp.expType);
3854
3855             sourceExp.type = classExp;
3856             sourceExp._classExp.specifiers = specs;
3857             sourceExp._classExp.decl = decl;
3858             sourceExp.expType = dest;
3859             dest.refCount++;
3860
3861             FreeType(source);
3862             FreeType(dest);
3863             if(backupSourceExpType) FreeType(backupSourceExpType);
3864             return true;
3865          }
3866       }
3867       else if(source.kind == classType)
3868       {
3869          Class _class = source._class ? source._class.registered : null;
3870
3871          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3872          {
3873             /*
3874             if(dest.kind != classType)
3875             {
3876                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3877                if(!source._class.registered.dataType)
3878                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3879
3880                FreeType(dest);
3881                dest = MkClassType(source._class.string);
3882                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3883                //   dest = MkClassType(source._class.string);
3884             }
3885             */
3886
3887             if(dest.kind != classType)
3888             {
3889                Type tempType { };
3890                Type tempDest, tempSource;
3891
3892                if(!source._class.registered.dataType)
3893                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3894
3895                for(; _class.base.type != systemClass; _class = _class.base);
3896                tempDest = source;
3897                tempSource = tempType;
3898                tempType.kind = classType;
3899                tempType._class = FindClass(_class.fullName);
3900                tempType.truth = source.truth;
3901                tempType.classObjectType = source.classObjectType;
3902
3903                if(tempType._class)
3904                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3905
3906                // PUT THIS BACK TESTING UNITS?
3907                if(conversions.last)
3908                {
3909                   ((Conversion)(conversions.last)).resultType = dest;
3910                   dest.refCount++;
3911                }
3912
3913                FreeType(sourceExp.expType);
3914                sourceExp.expType = MkClassType(_class.fullName);
3915                sourceExp.expType.truth = source.truth;
3916                sourceExp.expType.classObjectType = source.classObjectType;
3917
3918                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3919
3920                if(!sourceExp.destType)
3921                {
3922                   FreeType(sourceExp.destType);
3923                   sourceExp.destType = sourceExp.expType;
3924                   if(sourceExp.expType)
3925                      sourceExp.expType.refCount++;
3926                }
3927                //flag = true;
3928                //source = _class.dataType;
3929
3930
3931                // TOCHECK: TESTING THIS NEW CODE
3932                if(!_class.dataType)
3933                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3934                FreeType(dest);
3935                dest = MkClassType(source._class.string);
3936                dest.truth = source.truth;
3937                dest.classObjectType = source.classObjectType;
3938
3939                FreeType(source);
3940                source = _class.dataType;
3941                source.refCount++;
3942
3943                delete tempType;
3944             }
3945          }
3946       }
3947
3948       if(!flag)
3949       {
3950          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3951          {
3952             FreeType(source);
3953             FreeType(dest);
3954             return true;
3955          }
3956       }
3957
3958       // Implicit Casts
3959       /*
3960       if(source.kind == classType)
3961       {
3962          Class _class = source._class.registered;
3963          if(_class.type == unitClass)
3964          {
3965             if(!_class.dataType)
3966                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3967             source = _class.dataType;
3968          }
3969       }*/
3970
3971       if(dest.kind == classType)
3972       {
3973          Class _class = dest._class ? dest._class.registered : null;
3974          bool fittingValue = false;
3975          if(_class && _class.type == enumClass)
3976          {
3977             Class enumClass = eSystem_FindClass(privateModule, "enum");
3978             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3979             if(c && value >= 0 && value <= c.largest)
3980                fittingValue = true;
3981          }
3982
3983          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3984             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3985          {
3986             if(_class.type == normalClass || _class.type == noHeadClass)
3987             {
3988                Expression newExp { };
3989                *newExp = *sourceExp;
3990                if(sourceExp.destType) sourceExp.destType.refCount++;
3991                if(sourceExp.expType)  sourceExp.expType.refCount++;
3992                sourceExp.type = castExp;
3993                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3994                sourceExp.cast.exp = newExp;
3995                FreeType(sourceExp.expType);
3996                sourceExp.expType = null;
3997                ProcessExpressionType(sourceExp);
3998
3999                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
4000                if(!inCompiler)
4001                {
4002                   FreeType(sourceExp.expType);
4003                   sourceExp.expType = dest;
4004                }
4005
4006                FreeType(source);
4007                if(inCompiler) FreeType(dest);
4008
4009                if(backupSourceExpType) FreeType(backupSourceExpType);
4010                return true;
4011             }
4012
4013             if(!_class.dataType)
4014                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4015             FreeType(dest);
4016             dest = _class.dataType;
4017             dest.refCount++;
4018          }
4019
4020          // Accept lower precision types for units, since we want to keep the unit type
4021          if(dest.kind == doubleType &&
4022             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
4023              source.kind == charType || source.kind == _BoolType))
4024          {
4025             specs = MkListOne(MkSpecifier(DOUBLE));
4026          }
4027          else if(dest.kind == floatType &&
4028             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
4029             source.kind == _BoolType || source.kind == doubleType))
4030          {
4031             specs = MkListOne(MkSpecifier(FLOAT));
4032          }
4033          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
4034             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4035          {
4036             specs = MkList();
4037             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4038             ListAdd(specs, MkSpecifier(INT64));
4039          }
4040          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
4041             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4042          {
4043             specs = MkList();
4044             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4045             ListAdd(specs, MkSpecifier(INT));
4046          }
4047          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
4048             source.kind == floatType || source.kind == doubleType))
4049          {
4050             specs = MkList();
4051             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4052             ListAdd(specs, MkSpecifier(SHORT));
4053          }
4054          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
4055             source.kind == floatType || source.kind == doubleType))
4056          {
4057             specs = MkList();
4058             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4059             ListAdd(specs, MkSpecifier(CHAR));
4060          }
4061          else
4062          {
4063             FreeType(source);
4064             FreeType(dest);
4065             if(backupSourceExpType)
4066             {
4067                // Failed to convert: revert previous exp type
4068                if(sourceExp.expType) FreeType(sourceExp.expType);
4069                sourceExp.expType = backupSourceExpType;
4070             }
4071             return false;
4072          }
4073       }
4074       else if(dest.kind == doubleType &&
4075          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
4076           source.kind == _BoolType || source.kind == charType))
4077       {
4078          specs = MkListOne(MkSpecifier(DOUBLE));
4079       }
4080       else if(dest.kind == floatType &&
4081          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4082       {
4083          specs = MkListOne(MkSpecifier(FLOAT));
4084       }
4085       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4086          (value == 1 || value == 0))
4087       {
4088          specs = MkList();
4089          ListAdd(specs, MkSpecifier(BOOL));
4090       }
4091       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4092          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
4093       {
4094          specs = MkList();
4095          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4096          ListAdd(specs, MkSpecifier(CHAR));
4097       }
4098       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
4099          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
4100       {
4101          specs = MkList();
4102          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4103          ListAdd(specs, MkSpecifier(SHORT));
4104       }
4105       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
4106       {
4107          specs = MkList();
4108          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4109          ListAdd(specs, MkSpecifier(INT));
4110       }
4111       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
4112       {
4113          specs = MkList();
4114          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4115          ListAdd(specs, MkSpecifier(INT64));
4116       }
4117       else if(dest.kind == enumType &&
4118          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4119       {
4120          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
4121       }
4122       else
4123       {
4124          FreeType(source);
4125          FreeType(dest);
4126          if(backupSourceExpType)
4127          {
4128             // Failed to convert: revert previous exp type
4129             if(sourceExp.expType) FreeType(sourceExp.expType);
4130             sourceExp.expType = backupSourceExpType;
4131          }
4132          return false;
4133       }
4134
4135       if(!flag && !sourceExp.opDestType)
4136       {
4137          Expression newExp { };
4138          *newExp = *sourceExp;
4139          newExp.prev = null;
4140          newExp.next = null;
4141          if(sourceExp.destType) sourceExp.destType.refCount++;
4142          if(sourceExp.expType)  sourceExp.expType.refCount++;
4143
4144          sourceExp.type = castExp;
4145          if(realDest.kind == classType)
4146          {
4147             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
4148             FreeList(specs, FreeSpecifier);
4149          }
4150          else
4151             sourceExp.cast.typeName = MkTypeName(specs, null);
4152          if(newExp.type == opExp)
4153          {
4154             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4155          }
4156          else
4157             sourceExp.cast.exp = newExp;
4158
4159          FreeType(sourceExp.expType);
4160          sourceExp.expType = null;
4161          ProcessExpressionType(sourceExp);
4162       }
4163       else
4164          FreeList(specs, FreeSpecifier);
4165
4166       FreeType(dest);
4167       FreeType(source);
4168       if(backupSourceExpType) FreeType(backupSourceExpType);
4169
4170       return true;
4171    }
4172    else
4173    {
4174       if(computedExp != sourceExp)
4175       {
4176          FreeExpression(computedExp);
4177          computedExp = sourceExp;
4178       }
4179
4180       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4181       if(sourceExp.type == identifierExp)
4182       {
4183          Identifier id = sourceExp.identifier;
4184          if(dest.kind == classType)
4185          {
4186             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4187             {
4188                Class _class = dest._class.registered;
4189                Class enumClass = eSystem_FindClass(privateModule, "enum");
4190                if(enumClass)
4191                {
4192                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4193                   {
4194                      NamedLink64 value;
4195                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4196                      for(value = e.values.first; value; value = value.next)
4197                      {
4198                         if(!strcmp(value.name, id.string))
4199                            break;
4200                      }
4201                      if(value)
4202                      {
4203                         FreeExpContents(sourceExp);
4204                         FreeType(sourceExp.expType);
4205
4206                         sourceExp.isConstant = true;
4207                         sourceExp.expType = MkClassType(_class.fullName);
4208                         //if(inCompiler)
4209                         {
4210                            sourceExp.type = constantExp;
4211                            if(_class.dataTypeString && (!strcmp(_class.dataTypeString, "int") || !strcmp(_class.dataTypeString, "int64") || !strcmp(_class.dataTypeString, "short") || !strcmp(_class.dataTypeString, "char"))) // _class cannot be null here!
4212                               sourceExp.constant = PrintInt64(value.data);
4213                            else
4214                               sourceExp.constant = PrintUInt64(value.data);
4215                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4216                         }
4217                         FreeType(dest);
4218                         return true;
4219                      }
4220                   }
4221                }
4222             }
4223          }
4224
4225          // Loop through all enum classes
4226          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4227          {
4228             FreeType(dest);
4229             return true;
4230          }
4231       }
4232       FreeType(dest);
4233    }
4234    return false;
4235 }
4236
4237 #define TERTIARY(o, name, m, t, p) \
4238    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4239    {                                                              \
4240       exp.type = constantExp;                                    \
4241       exp.string = p(op1.m ? op2.m : op3.m);                     \
4242       if(!exp.expType) \
4243          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4244       return true;                                                \
4245    }
4246
4247 #define BINARY(o, name, m, t, p) \
4248    static bool name(Expression exp, Operand op1, Operand op2)   \
4249    {                                                              \
4250       t value2 = op2.m;                                           \
4251       exp.type = constantExp;                                    \
4252       exp.string = p((t)(op1.m o value2));                     \
4253       if(!exp.expType) \
4254          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4255       return true;                                                \
4256    }
4257
4258 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4259    static bool name(Expression exp, Operand op1, Operand op2)   \
4260    {                                                              \
4261       t value2 = op2.m;                                           \
4262       exp.type = constantExp;                                    \
4263       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4264       if(!exp.expType) \
4265          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4266       return true;                                                \
4267    }
4268
4269 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4270    static bool name(Expression exp, Operand op1, Operand op2)   \
4271    {                                                              \
4272       t value2 = op2.m;                                           \
4273       exp.type = constantExp;                                    \
4274       exp.string = p(op1.m o value2);             \
4275       if(!exp.expType) \
4276          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4277       return true;                                                \
4278    }
4279
4280 #define UNARY(o, name, m, t, p) \
4281    static bool name(Expression exp, Operand op1)                \
4282    {                                                              \
4283       exp.type = constantExp;                                    \
4284       exp.string = p((t)(o op1.m));                                   \
4285       if(!exp.expType) \
4286          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4287       return true;                                                \
4288    }
4289
4290 #define OPERATOR_ALL(macro, o, name) \
4291    macro(o, Int##name, i, int, PrintInt) \
4292    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4293    macro(o, Int64##name, i64, int64, PrintInt64) \
4294    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4295    macro(o, Short##name, s, short, PrintShort) \
4296    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4297    macro(o, Char##name, c, char, PrintChar) \
4298    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4299    macro(o, Float##name, f, float, PrintFloat) \
4300    macro(o, Double##name, d, double, PrintDouble)
4301
4302 #define OPERATOR_INTTYPES(macro, o, name) \
4303    macro(o, Int##name, i, int, PrintInt) \
4304    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4305    macro(o, Int64##name, i64, int64, PrintInt64) \
4306    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4307    macro(o, Short##name, s, short, PrintShort) \
4308    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4309    macro(o, Char##name, c, char, PrintChar) \
4310    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4311
4312 #define OPERATOR_REALTYPES(macro, o, name) \
4313    macro(o, Float##name, f, float, PrintFloat) \
4314    macro(o, Double##name, d, double, PrintDouble)
4315
4316 // binary arithmetic
4317 OPERATOR_ALL(BINARY, +, Add)
4318 OPERATOR_ALL(BINARY, -, Sub)
4319 OPERATOR_ALL(BINARY, *, Mul)
4320 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4321 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4322 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4323
4324 // unary arithmetic
4325 OPERATOR_ALL(UNARY, -, Neg)
4326
4327 // unary arithmetic increment and decrement
4328 OPERATOR_ALL(UNARY, ++, Inc)
4329 OPERATOR_ALL(UNARY, --, Dec)
4330
4331 // binary arithmetic assignment
4332 OPERATOR_ALL(BINARY, =, Asign)
4333 OPERATOR_ALL(BINARY, +=, AddAsign)
4334 OPERATOR_ALL(BINARY, -=, SubAsign)
4335 OPERATOR_ALL(BINARY, *=, MulAsign)
4336 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4337 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4338 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4339
4340 // binary bitwise
4341 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4342 OPERATOR_INTTYPES(BINARY, |, BitOr)
4343 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4344 OPERATOR_INTTYPES(BINARY, <<, LShift)
4345 OPERATOR_INTTYPES(BINARY, >>, RShift)
4346
4347 // unary bitwise
4348 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4349
4350 // binary bitwise assignment
4351 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4352 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4353 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4354 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4355 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4356
4357 // unary logical negation
4358 OPERATOR_INTTYPES(UNARY, !, Not)
4359
4360 // binary logical equality
4361 OPERATOR_ALL(BINARY, ==, Equ)
4362 OPERATOR_ALL(BINARY, !=, Nqu)
4363
4364 // binary logical
4365 OPERATOR_ALL(BINARY, &&, And)
4366 OPERATOR_ALL(BINARY, ||, Or)
4367
4368 // binary logical relational
4369 OPERATOR_ALL(BINARY, >, Grt)
4370 OPERATOR_ALL(BINARY, <, Sma)
4371 OPERATOR_ALL(BINARY, >=, GrtEqu)
4372 OPERATOR_ALL(BINARY, <=, SmaEqu)
4373
4374 // tertiary condition operator
4375 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4376
4377 //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
4378 #define OPERATOR_TABLE_ALL(name, type) \
4379     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4380                           type##Neg, \
4381                           type##Inc, type##Dec, \
4382                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4383                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4384                           type##BitNot, \
4385                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4386                           type##Not, \
4387                           type##Equ, type##Nqu, \
4388                           type##And, type##Or, \
4389                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4390                         }; \
4391
4392 #define OPERATOR_TABLE_INTTYPES(name, type) \
4393     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4394                           type##Neg, \
4395                           type##Inc, type##Dec, \
4396                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4397                           null, null, null, null, null, \
4398                           null, \
4399                           null, null, null, null, null, \
4400                           null, \
4401                           type##Equ, type##Nqu, \
4402                           type##And, type##Or, \
4403                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4404                         }; \
4405
4406 OPERATOR_TABLE_ALL(int, Int)
4407 OPERATOR_TABLE_ALL(uint, UInt)
4408 OPERATOR_TABLE_ALL(int64, Int64)
4409 OPERATOR_TABLE_ALL(uint64, UInt64)
4410 OPERATOR_TABLE_ALL(short, Short)
4411 OPERATOR_TABLE_ALL(ushort, UShort)
4412 OPERATOR_TABLE_INTTYPES(float, Float)
4413 OPERATOR_TABLE_INTTYPES(double, Double)
4414 OPERATOR_TABLE_ALL(char, Char)
4415 OPERATOR_TABLE_ALL(uchar, UChar)
4416
4417 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4418 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4419 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4420 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4421 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4422 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4423 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4424 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4425
4426 public void ReadString(char * output,  char * string)
4427 {
4428    int len = strlen(string);
4429    int c,d = 0;
4430    bool quoted = false, escaped = false;
4431    for(c = 0; c<len; c++)
4432    {
4433       char ch = string[c];
4434       if(escaped)
4435       {
4436          switch(ch)
4437          {
4438             case 'n': output[d] = '\n'; break;
4439             case 't': output[d] = '\t'; break;
4440             case 'a': output[d] = '\a'; break;
4441             case 'b': output[d] = '\b'; break;
4442             case 'f': output[d] = '\f'; break;
4443             case 'r': output[d] = '\r'; break;
4444             case 'v': output[d] = '\v'; break;
4445             case '\\': output[d] = '\\'; break;
4446             case '\"': output[d] = '\"'; break;
4447             case '\'': output[d] = '\''; break;
4448             default: output[d] = ch;
4449          }
4450          d++;
4451          escaped = false;
4452       }
4453       else
4454       {
4455          if(ch == '\"')
4456             quoted ^= true;
4457          else if(quoted)
4458          {
4459             if(ch == '\\')
4460                escaped = true;
4461             else
4462                output[d++] = ch;
4463          }
4464       }
4465    }
4466    output[d] = '\0';
4467 }
4468
4469 // String Unescape Copy
4470
4471 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4472 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4473 public int UnescapeString(char * d, char * s, int len)
4474 {
4475    int j = 0, k = 0;
4476    char ch;
4477    while(j < len && (ch = s[j]))
4478    {
4479       switch(ch)
4480       {
4481          case '\\':
4482             switch((ch = s[++j]))
4483             {
4484                case 'n': d[k] = '\n'; break;
4485                case 't': d[k] = '\t'; break;
4486                case 'a': d[k] = '\a'; break;
4487                case 'b': d[k] = '\b'; break;
4488                case 'f': d[k] = '\f'; break;
4489                case 'r': d[k] = '\r'; break;
4490                case 'v': d[k] = '\v'; break;
4491                case '\\': d[k] = '\\'; break;
4492                case '\"': d[k] = '\"'; break;
4493                case '\'': d[k] = '\''; break;
4494                default: d[k] = '\\'; d[k] = ch;
4495             }
4496             break;
4497          default:
4498             d[k] = ch;
4499       }
4500       j++, k++;
4501    }
4502    d[k] = '\0';
4503    return k;
4504 }
4505
4506 public char * OffsetEscapedString(char * s, int len, int offset)
4507 {
4508    char ch;
4509    int j = 0, k = 0;
4510    while(j < len && k < offset && (ch = s[j]))
4511    {
4512       if(ch == '\\') ++j;
4513       j++, k++;
4514    }
4515    return (k == offset) ? s + j : null;
4516 }
4517
4518 public Operand GetOperand(Expression exp)
4519 {
4520    Operand op { };
4521    Type type = exp.expType;
4522    if(type)
4523    {
4524       while(type.kind == classType &&
4525          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4526       {
4527          if(!type._class.registered.dataType)
4528             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4529          type = type._class.registered.dataType;
4530
4531       }
4532       if(exp.type == stringExp && op.kind == pointerType)
4533       {
4534          op.ui64 = (uint64)exp.string;
4535          op.kind = pointerType;
4536          op.ops = uint64Ops;
4537       }
4538       else if(exp.isConstant && exp.type == constantExp)
4539       {
4540          op.kind = type.kind;
4541          op.type = type;
4542
4543          switch(op.kind)
4544          {
4545             case _BoolType:
4546             case charType:
4547             {
4548                if(exp.constant[0] == '\'')
4549                {
4550                   op.c = exp.constant[1];
4551                   op.ops = charOps;
4552                }
4553                else if(type.isSigned)
4554                {
4555                   op.c = (char)strtol(exp.constant, null, 0);
4556                   op.ops = charOps;
4557                }
4558                else
4559                {
4560                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4561                   op.ops = ucharOps;
4562                }
4563                break;
4564             }
4565             case shortType:
4566                if(type.isSigned)
4567                {
4568                   op.s = (short)strtol(exp.constant, null, 0);
4569                   op.ops = shortOps;
4570                }
4571                else
4572                {
4573                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4574                   op.ops = ushortOps;
4575                }
4576                break;
4577             case intType:
4578             case longType:
4579                if(type.isSigned)
4580                {
4581                   op.i = (int)strtol(exp.constant, null, 0);
4582                   op.ops = intOps;
4583                }
4584                else
4585                {
4586                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4587                   op.ops = uintOps;
4588                }
4589                op.kind = intType;
4590                break;
4591             case int64Type:
4592                if(type.isSigned)
4593                {
4594                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4595                   op.ops = int64Ops;
4596                }
4597                else
4598                {
4599                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4600                   op.ops = uint64Ops;
4601                }
4602                op.kind = int64Type;
4603                break;
4604             case intPtrType:
4605                if(type.isSigned)
4606                {
4607                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4608                   op.ops = int64Ops;
4609                }
4610                else
4611                {
4612                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4613                   op.ops = uint64Ops;
4614                }
4615                op.kind = int64Type;
4616                break;
4617             case intSizeType:
4618                if(type.isSigned)
4619                {
4620                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4621                   op.ops = int64Ops;
4622                }
4623                else
4624                {
4625                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4626                   op.ops = uint64Ops;
4627                }
4628                op.kind = int64Type;
4629                break;
4630             case floatType:
4631                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4632                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4633                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4634                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4635                else
4636                   op.f = (float)strtod(exp.constant, null);
4637                op.ops = floatOps;
4638                break;
4639             case doubleType:
4640                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4641                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4642                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4643                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4644                else
4645                   op.d = (double)strtod(exp.constant, null);
4646                op.ops = doubleOps;
4647                break;
4648             //case classType:    For when we have operator overloading...
4649             // Pointer additions
4650             //case functionType:
4651             case arrayType:
4652             case pointerType:
4653             case classType:
4654                op.ui64 = _strtoui64(exp.constant, null, 0);
4655                op.kind = pointerType;
4656                op.ops = uint64Ops;
4657                // op.ptrSize =
4658                break;
4659          }
4660       }
4661    }
4662    return op;
4663 }
4664
4665 static int64 GetEnumValue(Class _class, void * ptr)
4666 {
4667    int64 v = 0;
4668    switch(_class.typeSize)
4669    {
4670       case 8:
4671          if(!strcmp(_class.dataTypeString, "uint64"))
4672             v = (int64)*(uint64 *)ptr;
4673          else
4674             v = (int64)*(int64 *)ptr;
4675          break;
4676       case 4:
4677          if(!strcmp(_class.dataTypeString, "uint"))
4678             v = (int64)*(uint *)ptr;
4679          else
4680             v = (int64)*(int *)ptr;
4681          break;
4682       case 2:
4683          if(!strcmp(_class.dataTypeString, "uint16"))
4684             v = (int64)*(uint16 *)ptr;
4685          else
4686             v = (int64)*(short *)ptr;
4687          break;
4688       case 1:
4689          if(!strcmp(_class.dataTypeString, "byte"))
4690             v = (int64)*(byte *)ptr;
4691          else
4692             v = (int64)*(char *)ptr;
4693          break;
4694    }
4695    return v;
4696 }
4697
4698 static __attribute__((unused)) void UnusedFunction()
4699 {
4700    int a;
4701    a.OnGetString(0,0,0);
4702 }
4703 default:
4704 extern int __ecereVMethodID_class_OnGetString;
4705 public:
4706
4707 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4708 {
4709    DataMember dataMember;
4710    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4711    {
4712       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4713          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4714       else
4715       {
4716          Expression exp { };
4717          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4718          Type type;
4719          void * ptr = inst.data + dataMember.offset + offset;
4720          char * result = null;
4721          exp.loc = member.loc = inst.loc;
4722          ((Identifier)member.identifiers->first).loc = inst.loc;
4723
4724          if(!dataMember.dataType)
4725             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4726          type = dataMember.dataType;
4727          if(type.kind == classType)
4728          {
4729             Class _class = type._class.registered;
4730             if(_class.type == enumClass)
4731             {
4732                Class enumClass = eSystem_FindClass(privateModule, "enum");
4733                if(enumClass)
4734                {
4735                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4736                   NamedLink64 item;
4737                   for(item = e.values.first; item; item = item.next)
4738                   {
4739                      if(item.data == GetEnumValue(_class, ptr))
4740                      {
4741                         result = item.name;
4742                         break;
4743                      }
4744                   }
4745                   if(result)
4746                   {
4747                      exp.identifier = MkIdentifier(result);
4748                      exp.type = identifierExp;
4749                      exp.destType = MkClassType(_class.fullName);
4750                      ProcessExpressionType(exp);
4751                   }
4752                }
4753             }
4754             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4755             {
4756                if(!_class.dataType)
4757                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4758                type = _class.dataType;
4759             }
4760          }
4761          if(!result)
4762          {
4763             switch(type.kind)
4764             {
4765                case floatType:
4766                {
4767                   FreeExpContents(exp);
4768
4769                   exp.constant = PrintFloat(*(float*)ptr);
4770                   exp.type = constantExp;
4771                   break;
4772                }
4773                case doubleType:
4774                {
4775                   FreeExpContents(exp);
4776
4777                   exp.constant = PrintDouble(*(double*)ptr);
4778                   exp.type = constantExp;
4779                   break;
4780                }
4781                case intType:
4782                {
4783                   FreeExpContents(exp);
4784
4785                   exp.constant = PrintInt(*(int*)ptr);
4786                   exp.type = constantExp;
4787                   break;
4788                }
4789                case int64Type:
4790                {
4791                   FreeExpContents(exp);
4792
4793                   exp.constant = PrintInt64(*(int64*)ptr);
4794                   exp.type = constantExp;
4795                   break;
4796                }
4797                case intPtrType:
4798                {
4799                   FreeExpContents(exp);
4800                   // TODO: This should probably use proper type
4801                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4802                   exp.type = constantExp;
4803                   break;
4804                }
4805                case intSizeType:
4806                {
4807                   FreeExpContents(exp);
4808                   // TODO: This should probably use proper type
4809                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4810                   exp.type = constantExp;
4811                   break;
4812                }
4813                default:
4814                   Compiler_Error($"Unhandled type populating instance\n");
4815             }
4816          }
4817          ListAdd(memberList, member);
4818       }
4819
4820       if(parentDataMember.type == unionMember)
4821          break;
4822    }
4823 }
4824
4825 void PopulateInstance(Instantiation inst)
4826 {
4827    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4828    Class _class = classSym.registered;
4829    DataMember dataMember;
4830    OldList * memberList = MkList();
4831    // Added this check and ->Add to prevent memory leaks on bad code
4832    if(!inst.members)
4833       inst.members = MkListOne(MkMembersInitList(memberList));
4834    else
4835       inst.members->Add(MkMembersInitList(memberList));
4836    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4837    {
4838       if(!dataMember.isProperty)
4839       {
4840          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4841             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4842          else
4843          {
4844             Expression exp { };
4845             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4846             Type type;
4847             void * ptr = inst.data + dataMember.offset;
4848             char * result = null;
4849
4850             exp.loc = member.loc = inst.loc;
4851             ((Identifier)member.identifiers->first).loc = inst.loc;
4852
4853             if(!dataMember.dataType)
4854                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4855             type = dataMember.dataType;
4856             if(type.kind == classType)
4857             {
4858                Class _class = type._class.registered;
4859                if(_class.type == enumClass)
4860                {
4861                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4862                   if(enumClass)
4863                   {
4864                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4865                      NamedLink64 item;
4866                      for(item = e.values.first; item; item = item.next)
4867                      {
4868                         if(item.data == GetEnumValue(_class, ptr))
4869                         {
4870                            result = item.name;
4871                            break;
4872                         }
4873                      }
4874                   }
4875                   if(result)
4876                   {
4877                      exp.identifier = MkIdentifier(result);
4878                      exp.type = identifierExp;
4879                      exp.destType = MkClassType(_class.fullName);
4880                      ProcessExpressionType(exp);
4881                   }
4882                }
4883                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4884                {
4885                   if(!_class.dataType)
4886                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4887                   type = _class.dataType;
4888                }
4889             }
4890             if(!result)
4891             {
4892                switch(type.kind)
4893                {
4894                   case floatType:
4895                   {
4896                      exp.constant = PrintFloat(*(float*)ptr);
4897                      exp.type = constantExp;
4898                      break;
4899                   }
4900                   case doubleType:
4901                   {
4902                      exp.constant = PrintDouble(*(double*)ptr);
4903                      exp.type = constantExp;
4904                      break;
4905                   }
4906                   case intType:
4907                   {
4908                      exp.constant = PrintInt(*(int*)ptr);
4909                      exp.type = constantExp;
4910                      break;
4911                   }
4912                   case int64Type:
4913                   {
4914                      exp.constant = PrintInt64(*(int64*)ptr);
4915                      exp.type = constantExp;
4916                      break;
4917                   }
4918                   case intPtrType:
4919                   {
4920                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4921                      exp.type = constantExp;
4922                      break;
4923                   }
4924                   default:
4925                      Compiler_Error($"Unhandled type populating instance\n");
4926                }
4927             }
4928             ListAdd(memberList, member);
4929          }
4930       }
4931    }
4932 }
4933
4934 void ComputeInstantiation(Expression exp)
4935 {
4936    Instantiation inst = exp.instance;
4937    MembersInit members;
4938    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4939    Class _class = classSym ? classSym.registered : null;
4940    DataMember curMember = null;
4941    Class curClass = null;
4942    DataMember subMemberStack[256];
4943    int subMemberStackPos = 0;
4944    uint64 bits = 0;
4945
4946    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4947    {
4948       // Don't recompute the instantiation...
4949       // Non Simple classes will have become constants by now
4950       if(inst.data)
4951          return;
4952
4953       if(_class.type == normalClass || _class.type == noHeadClass)
4954       {
4955          inst.data = (byte *)eInstance_New(_class);
4956          if(_class.type == normalClass)
4957             ((Instance)inst.data)._refCount++;
4958       }
4959       else
4960          inst.data = new0 byte[_class.structSize];
4961    }
4962
4963    if(inst.members)
4964    {
4965       for(members = inst.members->first; members; members = members.next)
4966       {
4967          switch(members.type)
4968          {
4969             case dataMembersInit:
4970             {
4971                if(members.dataMembers)
4972                {
4973                   MemberInit member;
4974                   for(member = members.dataMembers->first; member; member = member.next)
4975                   {
4976                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4977                      bool found = false;
4978
4979                      Property prop = null;
4980                      DataMember dataMember = null;
4981                      uint dataMemberOffset;
4982
4983                      if(!ident)
4984                      {
4985                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4986                         if(curMember)
4987                         {
4988                            if(curMember.isProperty)
4989                               prop = (Property)curMember;
4990                            else
4991                            {
4992                               dataMember = curMember;
4993
4994                               // CHANGED THIS HERE
4995                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4996
4997                               // 2013/17/29 -- It seems that this was missing here!
4998                               if(_class.type == normalClass)
4999                                  dataMemberOffset += _class.base.structSize;
5000                               // dataMemberOffset = dataMember.offset;
5001                            }
5002                            found = true;
5003                         }
5004                      }
5005                      else
5006                      {
5007                         prop = eClass_FindProperty(_class, ident.string, privateModule);
5008                         if(prop)
5009                         {
5010                            found = true;
5011                            if(prop.memberAccess == publicAccess)
5012                            {
5013                               curMember = (DataMember)prop;
5014                               curClass = prop._class;
5015                            }
5016                         }
5017                         else
5018                         {
5019                            DataMember _subMemberStack[256];
5020                            int _subMemberStackPos = 0;
5021
5022                            // FILL MEMBER STACK
5023                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
5024
5025                            if(dataMember)
5026                            {
5027                               found = true;
5028                               if(dataMember.memberAccess == publicAccess)
5029                               {
5030                                  curMember = dataMember;
5031                                  curClass = dataMember._class;
5032                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
5033                                  subMemberStackPos = _subMemberStackPos;
5034                               }
5035                            }
5036                         }
5037                      }
5038
5039                      if(found && member.initializer && member.initializer.type == expInitializer)
5040                      {
5041                         Expression value = member.initializer.exp;
5042                         Type type = null;
5043                         bool deepMember = false;
5044                         if(prop)
5045                         {
5046                            type = prop.dataType;
5047                         }
5048                         else if(dataMember)
5049                         {
5050                            if(!dataMember.dataType)
5051                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
5052
5053                            type = dataMember.dataType;
5054                         }
5055
5056                         if(ident && ident.next)
5057                         {
5058                            deepMember = true;
5059
5060                            // for(; ident && type; ident = ident.next)
5061                            for(ident = ident.next; ident && type; ident = ident.next)
5062                            {
5063                               if(type.kind == classType)
5064                               {
5065                                  prop = eClass_FindProperty(type._class.registered,
5066                                     ident.string, privateModule);
5067                                  if(prop)
5068                                     type = prop.dataType;
5069                                  else
5070                                  {
5071                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
5072                                        ident.string, &dataMemberOffset, privateModule, null, null);
5073                                     if(dataMember)
5074                                        type = dataMember.dataType;
5075                                  }
5076                               }
5077                               else if(type.kind == structType || type.kind == unionType)
5078                               {
5079                                  Type memberType;
5080                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
5081                                  {
5082                                     if(!strcmp(memberType.name, ident.string))
5083                                     {
5084                                        type = memberType;
5085                                        break;
5086                                     }
5087                                  }
5088                               }
5089                            }
5090                         }
5091                         if(value)
5092                         {
5093                            FreeType(value.destType);
5094                            value.destType = type;
5095                            if(type) type.refCount++;
5096                            ComputeExpression(value);
5097                         }
5098                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
5099                         {
5100                            if(type.kind == classType)
5101                            {
5102                               Class _class = type._class.registered;
5103                               if(_class.type == bitClass || _class.type == unitClass ||
5104                                  _class.type == enumClass)
5105                               {
5106                                  if(!_class.dataType)
5107                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5108                                  type = _class.dataType;
5109                               }
5110                            }
5111
5112                            if(dataMember)
5113                            {
5114                               void * ptr = inst.data + dataMemberOffset;
5115
5116                               if(value.type == constantExp)
5117                               {
5118                                  switch(type.kind)
5119                                  {
5120                                     case intType:
5121                                     {
5122                                        GetInt(value, (int*)ptr);
5123                                        break;
5124                                     }
5125                                     case int64Type:
5126                                     {
5127                                        GetInt64(value, (int64*)ptr);
5128                                        break;
5129                                     }
5130                                     case intPtrType:
5131                                     {
5132                                        GetIntPtr(value, (intptr*)ptr);
5133                                        break;
5134                                     }
5135                                     case intSizeType:
5136                                     {
5137                                        GetIntSize(value, (intsize*)ptr);
5138                                        break;
5139                                     }
5140                                     case floatType:
5141                                     {
5142                                        GetFloat(value, (float*)ptr);
5143                                        break;
5144                                     }
5145                                     case doubleType:
5146                                     {
5147                                        GetDouble(value, (double *)ptr);
5148                                        break;
5149                                     }
5150                                  }
5151                               }
5152                               else if(value.type == instanceExp)
5153                               {
5154                                  if(type.kind == classType)
5155                                  {
5156                                     Class _class = type._class.registered;
5157                                     if(_class.type == structClass)
5158                                     {
5159                                        ComputeTypeSize(type);
5160                                        if(value.instance.data)
5161                                           memcpy(ptr, value.instance.data, type.size);
5162                                     }
5163                                  }
5164                               }
5165                            }
5166                            else if(prop)
5167                            {
5168                               if(value.type == instanceExp && value.instance.data)
5169                               {
5170                                  if(type.kind == classType)
5171                                  {
5172                                     Class _class = type._class.registered;
5173                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5174                                     {
5175                                        void (*Set)(void *, void *) = (void *)prop.Set;
5176                                        Set(inst.data, value.instance.data);
5177                                        PopulateInstance(inst);
5178                                     }
5179                                  }
5180                               }
5181                               else if(value.type == constantExp)
5182                               {
5183                                  switch(type.kind)
5184                                  {
5185                                     case doubleType:
5186                                     {
5187                                        void (*Set)(void *, double) = (void *)prop.Set;
5188                                        Set(inst.data, strtod(value.constant, null) );
5189                                        break;
5190                                     }
5191                                     case floatType:
5192                                     {
5193                                        void (*Set)(void *, float) = (void *)prop.Set;
5194                                        Set(inst.data, (float)(strtod(value.constant, null)));
5195                                        break;
5196                                     }
5197                                     case intType:
5198                                     {
5199                                        void (*Set)(void *, int) = (void *)prop.Set;
5200                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5201                                        break;
5202                                     }
5203                                     case int64Type:
5204                                     {
5205                                        void (*Set)(void *, int64) = (void *)prop.Set;
5206                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5207                                        break;
5208                                     }
5209                                     case intPtrType:
5210                                     {
5211                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5212                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5213                                        break;
5214                                     }
5215                                     case intSizeType:
5216                                     {
5217                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5218                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5219                                        break;
5220                                     }
5221                                  }
5222                               }
5223                               else if(value.type == stringExp)
5224                               {
5225                                  char temp[1024];
5226                                  ReadString(temp, value.string);
5227                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5228                               }
5229                            }
5230                         }
5231                         else if(!deepMember && type && _class.type == unitClass)
5232                         {
5233                            if(prop)
5234                            {
5235                               // Only support converting units to units for now...
5236                               if(value.type == constantExp)
5237                               {
5238                                  if(type.kind == classType)
5239                                  {
5240                                     Class _class = type._class.registered;
5241                                     if(_class.type == unitClass)
5242                                     {
5243                                        if(!_class.dataType)
5244                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5245                                        type = _class.dataType;
5246                                     }
5247                                  }
5248                                  // TODO: Assuming same base type for units...
5249                                  switch(type.kind)
5250                                  {
5251                                     case floatType:
5252                                     {
5253                                        float fValue;
5254                                        float (*Set)(float) = (void *)prop.Set;
5255                                        GetFloat(member.initializer.exp, &fValue);
5256                                        exp.constant = PrintFloat(Set(fValue));
5257                                        exp.type = constantExp;
5258                                        break;
5259                                     }
5260                                     case doubleType:
5261                                     {
5262                                        double dValue;
5263                                        double (*Set)(double) = (void *)prop.Set;
5264                                        GetDouble(member.initializer.exp, &dValue);
5265                                        exp.constant = PrintDouble(Set(dValue));
5266                                        exp.type = constantExp;
5267                                        break;
5268                                     }
5269                                  }
5270                               }
5271                            }
5272                         }
5273                         else if(!deepMember && type && _class.type == bitClass)
5274                         {
5275                            if(prop)
5276                            {
5277                               if(value.type == instanceExp && value.instance.data)
5278                               {
5279                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5280                                  bits = Set(value.instance.data);
5281                               }
5282                               else if(value.type == constantExp)
5283                               {
5284                               }
5285                            }
5286                            else if(dataMember)
5287                            {
5288                               BitMember bitMember = (BitMember) dataMember;
5289                               Type type;
5290                               uint64 part = 0;
5291                               bits = (bits & ~bitMember.mask);
5292                               if(!bitMember.dataType)
5293                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5294                               type = bitMember.dataType;
5295                               if(type.kind == classType && type._class && type._class.registered)
5296                               {
5297                                  if(!type._class.registered.dataType)
5298                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5299                                  type = type._class.registered.dataType;
5300                               }
5301                               switch(type.kind)
5302                               {
5303                                  case _BoolType:
5304                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5305                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (short *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5306                                  case intType:
5307                                  case longType:       { uint v; type.isSigned ? GetInt(value, (int *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5308                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (int64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5309                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (intptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5310                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (intsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5311                               }
5312                               bits |= part << bitMember.pos;
5313                            }
5314                         }
5315                      }
5316                      else
5317                      {
5318                         if(_class && _class.type == unitClass)
5319                         {
5320                            ComputeExpression(member.initializer.exp);
5321                            exp.constant = member.initializer.exp.constant;
5322                            exp.type = constantExp;
5323
5324                            member.initializer.exp.constant = null;
5325                         }
5326                      }
5327                   }
5328                }
5329                break;
5330             }
5331          }
5332       }
5333    }
5334    if(_class && _class.type == bitClass)
5335    {
5336       exp.constant = PrintHexUInt(bits);
5337       exp.type = constantExp;
5338    }
5339    if(exp.type != instanceExp)
5340    {
5341       FreeInstance(inst);
5342    }
5343 }
5344
5345 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5346 {
5347    bool result = false;
5348    switch(kind)
5349    {
5350       case shortType:
5351          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5352             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5353          break;
5354       case intType:
5355       case longType:
5356          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5357             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5358          break;
5359       case int64Type:
5360          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5361             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5362             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5363          break;
5364       case floatType:
5365          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5366             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5367             result = GetOpFloat(op, &op.f);
5368          break;
5369       case doubleType:
5370          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5371             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5372             result = GetOpDouble(op, &op.d);
5373          break;
5374       case pointerType:
5375          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5376             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5377             result = GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5378          break;
5379       case enumType:
5380          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5381             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5382             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5383          break;
5384       case intPtrType:
5385          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5386             result = isSigned ? GetOpInt64 /*GetOpIntPtr*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5387          break;
5388       case intSizeType:
5389          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5390             result = isSigned ? GetOpInt64 /*GetOpIntSize*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntSize*/(op, &op.ui64);
5391          break;
5392    }
5393    return result;
5394 }
5395
5396 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5397 {
5398    if(exp.op.op == SIZEOF)
5399    {
5400       FreeExpContents(exp);
5401       exp.type = constantExp;
5402       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5403    }
5404    else
5405    {
5406       if(!exp.op.exp1)
5407       {
5408          switch(exp.op.op)
5409          {
5410             // unary arithmetic
5411             case '+':
5412             {
5413                // Provide default unary +
5414                Expression exp2 = exp.op.exp2;
5415                exp.op.exp2 = null;
5416                FreeExpContents(exp);
5417                FreeType(exp.expType);
5418                FreeType(exp.destType);
5419                *exp = *exp2;
5420                delete exp2;
5421                break;
5422             }
5423             case '-':
5424                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5425                break;
5426             // unary arithmetic increment and decrement
5427                   //OPERATOR_ALL(UNARY, ++, Inc)
5428                   //OPERATOR_ALL(UNARY, --, Dec)
5429             // unary bitwise
5430             case '~':
5431                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5432                break;
5433             // unary logical negation
5434             case '!':
5435                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5436                break;
5437          }
5438       }
5439       else
5440       {
5441          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5442          {
5443             if(Promote(op2, op1.kind, op1.type.isSigned))
5444                op2.kind = op1.kind, op2.ops = op1.ops;
5445             else if(Promote(op1, op2.kind, op2.type.isSigned))
5446                op1.kind = op2.kind, op1.ops = op2.ops;
5447          }
5448          switch(exp.op.op)
5449          {
5450             // binary arithmetic
5451             case '+':
5452                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5453                break;
5454             case '-':
5455                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5456                break;
5457             case '*':
5458                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5459                break;
5460             case '/':
5461                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5462                break;
5463             case '%':
5464                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5465                break;
5466             // binary arithmetic assignment
5467                   //OPERATOR_ALL(BINARY, =, Asign)
5468                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5469                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5470                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5471                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5472                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5473             // binary bitwise
5474             case '&':
5475                if(exp.op.exp2)
5476                {
5477                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5478                }
5479                break;
5480             case '|':
5481                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5482                break;
5483             case '^':
5484                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5485                break;
5486             case LEFT_OP:
5487                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5488                break;
5489             case RIGHT_OP:
5490                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5491                break;
5492             // binary bitwise assignment
5493                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5494                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5495                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5496                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5497                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5498             // binary logical equality
5499             case EQ_OP:
5500                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5501                break;
5502             case NE_OP:
5503                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5504                break;
5505             // binary logical
5506             case AND_OP:
5507                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5508                break;
5509             case OR_OP:
5510                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5511                break;
5512             // binary logical relational
5513             case '>':
5514                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5515                break;
5516             case '<':
5517                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5518                break;
5519             case GE_OP:
5520                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5521                break;
5522             case LE_OP:
5523                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5524                break;
5525          }
5526       }
5527    }
5528 }
5529
5530 void ComputeExpression(Expression exp)
5531 {
5532 #ifdef _DEBUG
5533    char expString[10240];
5534    expString[0] = '\0';
5535    PrintExpression(exp, expString);
5536 #endif
5537
5538    switch(exp.type)
5539    {
5540       case instanceExp:
5541       {
5542          ComputeInstantiation(exp);
5543          break;
5544       }
5545       /*
5546       case constantExp:
5547          break;
5548       */
5549       case opExp:
5550       {
5551          Expression exp1, exp2 = null;
5552          Operand op1 { };
5553          Operand op2 { };
5554
5555          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5556          if(exp.op.exp2)
5557          {
5558             Expression e = exp.op.exp2;
5559
5560             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5561             {
5562                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5563                {
5564                   if(e.type == extensionCompoundExp)
5565                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5566                   else
5567                      e = e.list->last;
5568                }
5569             }
5570             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5571             {
5572                if(e.type == stringExp && e.string)
5573                {
5574                   char * string = e.string;
5575                   int len = strlen(string);
5576                   char * tmp = new char[len-2+1];
5577                   len = UnescapeString(tmp, string + 1, len - 2);
5578                   delete tmp;
5579                   FreeExpContents(exp);
5580                   exp.type = constantExp;
5581                   exp.constant = PrintUInt(len + 1);
5582                }
5583                else
5584                {
5585                   Type type = e.expType;
5586                   type.refCount++;
5587                   FreeExpContents(exp);
5588                   exp.type = constantExp;
5589                   exp.constant = PrintUInt(ComputeTypeSize(type));
5590                   FreeType(type);
5591                }
5592                break;
5593             }
5594             else
5595                ComputeExpression(exp.op.exp2);
5596          }
5597          if(exp.op.exp1)
5598          {
5599             ComputeExpression(exp.op.exp1);
5600             exp1 = exp.op.exp1;
5601             exp2 = exp.op.exp2;
5602             op1 = GetOperand(exp1);
5603             if(op1.type) op1.type.refCount++;
5604             if(exp2)
5605             {
5606                op2 = GetOperand(exp2);
5607                if(op2.type) op2.type.refCount++;
5608             }
5609          }
5610          else
5611          {
5612             exp1 = exp.op.exp2;
5613             op1 = GetOperand(exp1);
5614             if(op1.type) op1.type.refCount++;
5615          }
5616
5617          CallOperator(exp, exp1, exp2, op1, op2);
5618          /*
5619          switch(exp.op.op)
5620          {
5621             // Unary operators
5622             case '&':
5623                // Also binary
5624                if(exp.op.exp1 && exp.op.exp2)
5625                {
5626                   // Binary And
5627                   if(op1.ops.BitAnd)
5628                   {
5629                      FreeExpContents(exp);
5630                      op1.ops.BitAnd(exp, op1, op2);
5631                   }
5632                }
5633                break;
5634             case '*':
5635                if(exp.op.exp1)
5636                {
5637                   if(op1.ops.Mul)
5638                   {
5639                      FreeExpContents(exp);
5640                      op1.ops.Mul(exp, op1, op2);
5641                   }
5642                }
5643                break;
5644             case '+':
5645                if(exp.op.exp1)
5646                {
5647                   if(op1.ops.Add)
5648                   {
5649                      FreeExpContents(exp);
5650                      op1.ops.Add(exp, op1, op2);
5651                   }
5652                }
5653                else
5654                {
5655                   // Provide default unary +
5656                   Expression exp2 = exp.op.exp2;
5657                   exp.op.exp2 = null;
5658                   FreeExpContents(exp);
5659                   FreeType(exp.expType);
5660                   FreeType(exp.destType);
5661
5662                   *exp = *exp2;
5663                   delete exp2;
5664                }
5665                break;
5666             case '-':
5667                if(exp.op.exp1)
5668                {
5669                   if(op1.ops.Sub)
5670                   {
5671                      FreeExpContents(exp);
5672                      op1.ops.Sub(exp, op1, op2);
5673                   }
5674                }
5675                else
5676                {
5677                   if(op1.ops.Neg)
5678                   {
5679                      FreeExpContents(exp);
5680                      op1.ops.Neg(exp, op1);
5681                   }
5682                }
5683                break;
5684             case '~':
5685                if(op1.ops.BitNot)
5686                {
5687                   FreeExpContents(exp);
5688                   op1.ops.BitNot(exp, op1);
5689                }
5690                break;
5691             case '!':
5692                if(op1.ops.Not)
5693                {
5694                   FreeExpContents(exp);
5695                   op1.ops.Not(exp, op1);
5696                }
5697                break;
5698             // Binary only operators
5699             case '/':
5700                if(op1.ops.Div)
5701                {
5702                   FreeExpContents(exp);
5703                   op1.ops.Div(exp, op1, op2);
5704                }
5705                break;
5706             case '%':
5707                if(op1.ops.Mod)
5708                {
5709                   FreeExpContents(exp);
5710                   op1.ops.Mod(exp, op1, op2);
5711                }
5712                break;
5713             case LEFT_OP:
5714                break;
5715             case RIGHT_OP:
5716                break;
5717             case '<':
5718                if(exp.op.exp1)
5719                {
5720                   if(op1.ops.Sma)
5721                   {
5722                      FreeExpContents(exp);
5723                      op1.ops.Sma(exp, op1, op2);
5724                   }
5725                }
5726                break;
5727             case '>':
5728                if(exp.op.exp1)
5729                {
5730                   if(op1.ops.Grt)
5731                   {
5732                      FreeExpContents(exp);
5733                      op1.ops.Grt(exp, op1, op2);
5734                   }
5735                }
5736                break;
5737             case LE_OP:
5738                if(exp.op.exp1)
5739                {
5740                   if(op1.ops.SmaEqu)
5741                   {
5742                      FreeExpContents(exp);
5743                      op1.ops.SmaEqu(exp, op1, op2);
5744                   }
5745                }
5746                break;
5747             case GE_OP:
5748                if(exp.op.exp1)
5749                {
5750                   if(op1.ops.GrtEqu)
5751                   {
5752                      FreeExpContents(exp);
5753                      op1.ops.GrtEqu(exp, op1, op2);
5754                   }
5755                }
5756                break;
5757             case EQ_OP:
5758                if(exp.op.exp1)
5759                {
5760                   if(op1.ops.Equ)
5761                   {
5762                      FreeExpContents(exp);
5763                      op1.ops.Equ(exp, op1, op2);
5764                   }
5765                }
5766                break;
5767             case NE_OP:
5768                if(exp.op.exp1)
5769                {
5770                   if(op1.ops.Nqu)
5771                   {
5772                      FreeExpContents(exp);
5773                      op1.ops.Nqu(exp, op1, op2);
5774                   }
5775                }
5776                break;
5777             case '|':
5778                if(op1.ops.BitOr)
5779                {
5780                   FreeExpContents(exp);
5781                   op1.ops.BitOr(exp, op1, op2);
5782                }
5783                break;
5784             case '^':
5785                if(op1.ops.BitXor)
5786                {
5787                   FreeExpContents(exp);
5788                   op1.ops.BitXor(exp, op1, op2);
5789                }
5790                break;
5791             case AND_OP:
5792                break;
5793             case OR_OP:
5794                break;
5795             case SIZEOF:
5796                FreeExpContents(exp);
5797                exp.type = constantExp;
5798                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5799                break;
5800          }
5801          */
5802          if(op1.type) FreeType(op1.type);
5803          if(op2.type) FreeType(op2.type);
5804          break;
5805       }
5806       case bracketsExp:
5807       case extensionExpressionExp:
5808       {
5809          Expression e, n;
5810          for(e = exp.list->first; e; e = n)
5811          {
5812             n = e.next;
5813             if(!n)
5814             {
5815                OldList * list = exp.list;
5816                Expression prev = exp.prev;
5817                Expression next = exp.next;
5818                ComputeExpression(e);
5819                //FreeExpContents(exp);
5820                FreeType(exp.expType);
5821                FreeType(exp.destType);
5822                *exp = *e;
5823                exp.prev = prev;
5824                exp.next = next;
5825                delete e;
5826                delete list;
5827             }
5828             else
5829             {
5830                FreeExpression(e);
5831             }
5832          }
5833          break;
5834       }
5835       /*
5836
5837       case ExpIndex:
5838       {
5839          Expression e;
5840          exp.isConstant = true;
5841
5842          ComputeExpression(exp.index.exp);
5843          if(!exp.index.exp.isConstant)
5844             exp.isConstant = false;
5845
5846          for(e = exp.index.index->first; e; e = e.next)
5847          {
5848             ComputeExpression(e);
5849             if(!e.next)
5850             {
5851                // Check if this type is int
5852             }
5853             if(!e.isConstant)
5854                exp.isConstant = false;
5855          }
5856          exp.expType = Dereference(exp.index.exp.expType);
5857          break;
5858       }
5859       */
5860       case memberExp:
5861       {
5862          Expression memberExp = exp.member.exp;
5863          Identifier memberID = exp.member.member;
5864
5865          Type type;
5866          ComputeExpression(exp.member.exp);
5867          type = exp.member.exp.expType;
5868          if(type)
5869          {
5870             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);
5871             Property prop = null;
5872             DataMember member = null;
5873             Class convertTo = null;
5874             if(type.kind == subClassType && exp.member.exp.type == classExp)
5875                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5876
5877             if(!_class)
5878             {
5879                char string[256];
5880                Symbol classSym;
5881                string[0] = '\0';
5882                PrintTypeNoConst(type, string, false, true);
5883                classSym = FindClass(string);
5884                _class = classSym ? classSym.registered : null;
5885             }
5886
5887             if(exp.member.member)
5888             {
5889                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5890                if(!prop)
5891                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5892             }
5893             if(!prop && !member && _class && exp.member.member)
5894             {
5895                Symbol classSym = FindClass(exp.member.member.string);
5896                convertTo = _class;
5897                _class = classSym ? classSym.registered : null;
5898                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5899             }
5900
5901             if(prop)
5902             {
5903                if(prop.compiled)
5904                {
5905                   Type type = prop.dataType;
5906                   // TODO: Assuming same base type for units...
5907                   if(_class.type == unitClass)
5908                   {
5909                      if(type.kind == classType)
5910                      {
5911                         Class _class = type._class.registered;
5912                         if(_class.type == unitClass)
5913                         {
5914                            if(!_class.dataType)
5915                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5916                            type = _class.dataType;
5917                         }
5918                      }
5919                      switch(type.kind)
5920                      {
5921                         case floatType:
5922                         {
5923                            float value;
5924                            float (*Get)(float) = (void *)prop.Get;
5925                            GetFloat(exp.member.exp, &value);
5926                            exp.constant = PrintFloat(Get ? Get(value) : value);
5927                            exp.type = constantExp;
5928                            break;
5929                         }
5930                         case doubleType:
5931                         {
5932                            double value;
5933                            double (*Get)(double);
5934                            GetDouble(exp.member.exp, &value);
5935
5936                            if(convertTo)
5937                               Get = (void *)prop.Set;
5938                            else
5939                               Get = (void *)prop.Get;
5940                            exp.constant = PrintDouble(Get ? Get(value) : value);
5941                            exp.type = constantExp;
5942                            break;
5943                         }
5944                      }
5945                   }
5946                   else
5947                   {
5948                      if(convertTo)
5949                      {
5950                         Expression value = exp.member.exp;
5951                         Type type;
5952                         if(!prop.dataType)
5953                            ProcessPropertyType(prop);
5954
5955                         type = prop.dataType;
5956                         if(!type)
5957                         {
5958                             // printf("Investigate this\n");
5959                         }
5960                         else if(_class.type == structClass)
5961                         {
5962                            switch(type.kind)
5963                            {
5964                               case classType:
5965                               {
5966                                  Class propertyClass = type._class.registered;
5967                                  if(propertyClass.type == structClass && value.type == instanceExp)
5968                                  {
5969                                     void (*Set)(void *, void *) = (void *)prop.Set;
5970                                     exp.instance = Instantiation { };
5971                                     exp.instance.data = new0 byte[_class.structSize];
5972                                     exp.instance._class = MkSpecifierName(_class.fullName);
5973                                     exp.instance.loc = exp.loc;
5974                                     exp.type = instanceExp;
5975                                     Set(exp.instance.data, value.instance.data);
5976                                     PopulateInstance(exp.instance);
5977                                  }
5978                                  break;
5979                               }
5980                               case intType:
5981                               {
5982                                  int intValue;
5983                                  void (*Set)(void *, int) = (void *)prop.Set;
5984
5985                                  exp.instance = Instantiation { };
5986                                  exp.instance.data = new0 byte[_class.structSize];
5987                                  exp.instance._class = MkSpecifierName(_class.fullName);
5988                                  exp.instance.loc = exp.loc;
5989                                  exp.type = instanceExp;
5990
5991                                  GetInt(value, &intValue);
5992
5993                                  Set(exp.instance.data, intValue);
5994                                  PopulateInstance(exp.instance);
5995                                  break;
5996                               }
5997                               case int64Type:
5998                               {
5999                                  int64 intValue;
6000                                  void (*Set)(void *, int64) = (void *)prop.Set;
6001
6002                                  exp.instance = Instantiation { };
6003                                  exp.instance.data = new0 byte[_class.structSize];
6004                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
6005                                  exp.instance.loc = exp.loc;
6006                                  exp.type = instanceExp;
6007
6008                                  GetInt64(value, &intValue);
6009
6010                                  Set(exp.instance.data, intValue);
6011                                  PopulateInstance(exp.instance);
6012                                  break;
6013                               }
6014                               case intPtrType:
6015                               {
6016                                  // TOFIX:
6017                                  intptr intValue;
6018                                  void (*Set)(void *, intptr) = (void *)prop.Set;
6019
6020                                  exp.instance = Instantiation { };
6021                                  exp.instance.data = new0 byte[_class.structSize];
6022                                  exp.instance._class = MkSpecifierName(_class.fullName);
6023                                  exp.instance.loc = exp.loc;
6024                                  exp.type = instanceExp;
6025
6026                                  GetIntPtr(value, &intValue);
6027
6028                                  Set(exp.instance.data, intValue);
6029                                  PopulateInstance(exp.instance);
6030                                  break;
6031                               }
6032                               case intSizeType:
6033                               {
6034                                  // TOFIX:
6035                                  intsize intValue;
6036                                  void (*Set)(void *, intsize) = (void *)prop.Set;
6037
6038                                  exp.instance = Instantiation { };
6039                                  exp.instance.data = new0 byte[_class.structSize];
6040                                  exp.instance._class = MkSpecifierName(_class.fullName);
6041                                  exp.instance.loc = exp.loc;
6042                                  exp.type = instanceExp;
6043
6044                                  GetIntSize(value, &intValue);
6045
6046                                  Set(exp.instance.data, intValue);
6047                                  PopulateInstance(exp.instance);
6048                                  break;
6049                               }
6050                               case floatType:
6051                               {
6052                                  float floatValue;
6053                                  void (*Set)(void *, float) = (void *)prop.Set;
6054
6055                                  exp.instance = Instantiation { };
6056                                  exp.instance.data = new0 byte[_class.structSize];
6057                                  exp.instance._class = MkSpecifierName(_class.fullName);
6058                                  exp.instance.loc = exp.loc;
6059                                  exp.type = instanceExp;
6060
6061                                  GetFloat(value, &floatValue);
6062
6063                                  Set(exp.instance.data, floatValue);
6064                                  PopulateInstance(exp.instance);
6065                                  break;
6066                               }
6067                               case doubleType:
6068                               {
6069                                  double doubleValue;
6070                                  void (*Set)(void *, double) = (void *)prop.Set;
6071
6072                                  exp.instance = Instantiation { };
6073                                  exp.instance.data = new0 byte[_class.structSize];
6074                                  exp.instance._class = MkSpecifierName(_class.fullName);
6075                                  exp.instance.loc = exp.loc;
6076                                  exp.type = instanceExp;
6077
6078                                  GetDouble(value, &doubleValue);
6079
6080                                  Set(exp.instance.data, doubleValue);
6081                                  PopulateInstance(exp.instance);
6082                                  break;
6083                               }
6084                            }
6085                         }
6086                         else if(_class.type == bitClass)
6087                         {
6088                            switch(type.kind)
6089                            {
6090                               case classType:
6091                               {
6092                                  Class propertyClass = type._class.registered;
6093                                  if(propertyClass.type == structClass && value.instance.data)
6094                                  {
6095                                     unsigned int (*Set)(void *) = (void *)prop.Set;
6096                                     unsigned int bits = Set(value.instance.data);
6097                                     exp.constant = PrintHexUInt(bits);
6098                                     exp.type = constantExp;
6099                                     break;
6100                                  }
6101                                  else if(_class.type == bitClass)
6102                                  {
6103                                     unsigned int value;
6104                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
6105                                     unsigned int bits;
6106
6107                                     GetUInt(exp.member.exp, &value);
6108                                     bits = Set(value);
6109                                     exp.constant = PrintHexUInt(bits);
6110                                     exp.type = constantExp;
6111                                  }
6112                               }
6113                            }
6114                         }
6115                      }
6116                      else
6117                      {
6118                         if(_class.type == bitClass)
6119                         {
6120                            unsigned int value;
6121                            GetUInt(exp.member.exp, &value);
6122
6123                            switch(type.kind)
6124                            {
6125                               case classType:
6126                               {
6127                                  Class _class = type._class.registered;
6128                                  if(_class.type == structClass)
6129                                  {
6130                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
6131
6132                                     exp.instance = Instantiation { };
6133                                     exp.instance.data = new0 byte[_class.structSize];
6134                                     exp.instance._class = MkSpecifierName(_class.fullName);
6135                                     exp.instance.loc = exp.loc;
6136                                     //exp.instance.fullSet = true;
6137                                     exp.type = instanceExp;
6138                                     Get(value, exp.instance.data);
6139                                     PopulateInstance(exp.instance);
6140                                  }
6141                                  else if(_class.type == bitClass)
6142                                  {
6143                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6144                                     uint64 bits = Get(value);
6145                                     exp.constant = PrintHexUInt64(bits);
6146                                     exp.type = constantExp;
6147                                  }
6148                                  break;
6149                               }
6150                            }
6151                         }
6152                         else if(_class.type == structClass)
6153                         {
6154                            byte * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6155                            switch(type.kind)
6156                            {
6157                               case classType:
6158                               {
6159                                  Class _class = type._class.registered;
6160                                  if(_class.type == structClass && value)
6161                                  {
6162                                     void (*Get)(void *, void *) = (void *)prop.Get;
6163
6164                                     exp.instance = Instantiation { };
6165                                     exp.instance.data = new0 byte[_class.structSize];
6166                                     exp.instance._class = MkSpecifierName(_class.fullName);
6167                                     exp.instance.loc = exp.loc;
6168                                     //exp.instance.fullSet = true;
6169                                     exp.type = instanceExp;
6170                                     Get(value, exp.instance.data);
6171                                     PopulateInstance(exp.instance);
6172                                  }
6173                                  break;
6174                               }
6175                            }
6176                         }
6177                         /*else
6178                         {
6179                            char * value = exp.member.exp.instance.data;
6180                            switch(type.kind)
6181                            {
6182                               case classType:
6183                               {
6184                                  Class _class = type._class.registered;
6185                                  if(_class.type == normalClass)
6186                                  {
6187                                     void *(*Get)(void *) = (void *)prop.Get;
6188
6189                                     exp.instance = Instantiation { };
6190                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6191                                     exp.type = instanceExp;
6192                                     exp.instance.data = Get(value, exp.instance.data);
6193                                  }
6194                                  break;
6195                               }
6196                            }
6197                         }
6198                         */
6199                      }
6200                   }
6201                }
6202                else
6203                {
6204                   exp.isConstant = false;
6205                }
6206             }
6207             else if(member)
6208             {
6209             }
6210          }
6211
6212          if(exp.type != ExpressionType::memberExp)
6213          {
6214             FreeExpression(memberExp);
6215             FreeIdentifier(memberID);
6216          }
6217          break;
6218       }
6219       case typeSizeExp:
6220       {
6221          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6222          FreeExpContents(exp);
6223          exp.constant = PrintUInt(ComputeTypeSize(type));
6224          exp.type = constantExp;
6225          FreeType(type);
6226          break;
6227       }
6228       case classSizeExp:
6229       {
6230          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6231          if(classSym && classSym.registered)
6232          {
6233             if(classSym.registered.fixed)
6234             {
6235                FreeSpecifier(exp._class);
6236                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6237                exp.type = constantExp;
6238             }
6239             else
6240             {
6241                char className[1024];
6242                strcpy(className, "__ecereClass_");
6243                FullClassNameCat(className, classSym.string, true);
6244                //MangleClassName(className);
6245
6246                DeclareClass(classSym, className);
6247
6248                FreeExpContents(exp);
6249                exp.type = pointerExp;
6250                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6251                exp.member.member = MkIdentifier("structSize");
6252             }
6253          }
6254          break;
6255       }
6256       case castExp:
6257       //case constantExp:
6258       {
6259          Type type;
6260          Expression e = exp;
6261          if(exp.type == castExp)
6262          {
6263             if(exp.cast.exp)
6264                ComputeExpression(exp.cast.exp);
6265             e = exp.cast.exp;
6266          }
6267          if(e && exp.expType)
6268          {
6269             /*if(exp.destType)
6270                type = exp.destType;
6271             else*/
6272                type = exp.expType;
6273             if(type.kind == classType)
6274             {
6275                Class _class = type._class.registered;
6276                if(_class && (_class.type == unitClass || _class.type == bitClass))
6277                {
6278                   if(!_class.dataType)
6279                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6280                   type = _class.dataType;
6281                }
6282             }
6283
6284             switch(type.kind)
6285             {
6286                case _BoolType:
6287                case charType:
6288                   if(type.isSigned)
6289                   {
6290                      char value = 0;
6291                      if(GetChar(e, &value))
6292                      {
6293                         FreeExpContents(exp);
6294                         exp.constant = PrintChar(value);
6295                         exp.type = constantExp;
6296                      }
6297                   }
6298                   else
6299                   {
6300                      unsigned char value = 0;
6301                      if(GetUChar(e, &value))
6302                      {
6303                         FreeExpContents(exp);
6304                         exp.constant = PrintUChar(value);
6305                         exp.type = constantExp;
6306                      }
6307                   }
6308                   break;
6309                case shortType:
6310                   if(type.isSigned)
6311                   {
6312                      short value = 0;
6313                      if(GetShort(e, &value))
6314                      {
6315                         FreeExpContents(exp);
6316                         exp.constant = PrintShort(value);
6317                         exp.type = constantExp;
6318                      }
6319                   }
6320                   else
6321                   {
6322                      unsigned short value = 0;
6323                      if(GetUShort(e, &value))
6324                      {
6325                         FreeExpContents(exp);
6326                         exp.constant = PrintUShort(value);
6327                         exp.type = constantExp;
6328                      }
6329                   }
6330                   break;
6331                case intType:
6332                   if(type.isSigned)
6333                   {
6334                      int value = 0;
6335                      if(GetInt(e, &value))
6336                      {
6337                         FreeExpContents(exp);
6338                         exp.constant = PrintInt(value);
6339                         exp.type = constantExp;
6340                      }
6341                   }
6342                   else
6343                   {
6344                      unsigned int value = 0;
6345                      if(GetUInt(e, &value))
6346                      {
6347                         FreeExpContents(exp);
6348                         exp.constant = PrintUInt(value);
6349                         exp.type = constantExp;
6350                      }
6351                   }
6352                   break;
6353                case int64Type:
6354                   if(type.isSigned)
6355                   {
6356                      int64 value = 0;
6357                      if(GetInt64(e, &value))
6358                      {
6359                         FreeExpContents(exp);
6360                         exp.constant = PrintInt64(value);
6361                         exp.type = constantExp;
6362                      }
6363                   }
6364                   else
6365                   {
6366                      uint64 value = 0;
6367                      if(GetUInt64(e, &value))
6368                      {
6369                         FreeExpContents(exp);
6370                         exp.constant = PrintUInt64(value);
6371                         exp.type = constantExp;
6372                      }
6373                   }
6374                   break;
6375                case intPtrType:
6376                   if(type.isSigned)
6377                   {
6378                      intptr value = 0;
6379                      if(GetIntPtr(e, &value))
6380                      {
6381                         FreeExpContents(exp);
6382                         exp.constant = PrintInt64((int64)value);
6383                         exp.type = constantExp;
6384                      }
6385                   }
6386                   else
6387                   {
6388                      uintptr value = 0;
6389                      if(GetUIntPtr(e, &value))
6390                      {
6391                         FreeExpContents(exp);
6392                         exp.constant = PrintUInt64((uint64)value);
6393                         exp.type = constantExp;
6394                      }
6395                   }
6396                   break;
6397                case intSizeType:
6398                   if(type.isSigned)
6399                   {
6400                      intsize value = 0;
6401                      if(GetIntSize(e, &value))
6402                      {
6403                         FreeExpContents(exp);
6404                         exp.constant = PrintInt64((int64)value);
6405                         exp.type = constantExp;
6406                      }
6407                   }
6408                   else
6409                   {
6410                      uintsize value = 0;
6411                      if(GetUIntSize(e, &value))
6412                      {
6413                         FreeExpContents(exp);
6414                         exp.constant = PrintUInt64((uint64)value);
6415                         exp.type = constantExp;
6416                      }
6417                   }
6418                   break;
6419                case floatType:
6420                {
6421                   float value = 0;
6422                   if(GetFloat(e, &value))
6423                   {
6424                      FreeExpContents(exp);
6425                      exp.constant = PrintFloat(value);
6426                      exp.type = constantExp;
6427                   }
6428                   break;
6429                }
6430                case doubleType:
6431                {
6432                   double value = 0;
6433                   if(GetDouble(e, &value))
6434                   {
6435                      FreeExpContents(exp);
6436                      exp.constant = PrintDouble(value);
6437                      exp.type = constantExp;
6438                   }
6439                   break;
6440                }
6441             }
6442          }
6443          break;
6444       }
6445       case conditionExp:
6446       {
6447          Operand op1 { };
6448          Operand op2 { };
6449          Operand op3 { };
6450
6451          if(exp.cond.exp)
6452             // Caring only about last expression for now...
6453             ComputeExpression(exp.cond.exp->last);
6454          if(exp.cond.elseExp)
6455             ComputeExpression(exp.cond.elseExp);
6456          if(exp.cond.cond)
6457             ComputeExpression(exp.cond.cond);
6458
6459          op1 = GetOperand(exp.cond.cond);
6460          if(op1.type) op1.type.refCount++;
6461          op2 = GetOperand(exp.cond.exp->last);
6462          if(op2.type) op2.type.refCount++;
6463          op3 = GetOperand(exp.cond.elseExp);
6464          if(op3.type) op3.type.refCount++;
6465
6466          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6467          if(op1.type) FreeType(op1.type);
6468          if(op2.type) FreeType(op2.type);
6469          if(op3.type) FreeType(op3.type);
6470          break;
6471       }
6472    }
6473 }
6474
6475 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6476 {
6477    bool result = true;
6478    if(destType)
6479    {
6480       OldList converts { };
6481       Conversion convert;
6482
6483       if(destType.kind == voidType)
6484          return false;
6485
6486       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6487          result = false;
6488       if(converts.count)
6489       {
6490          // for(convert = converts.last; convert; convert = convert.prev)
6491          for(convert = converts.first; convert; convert = convert.next)
6492          {
6493             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6494             if(!empty)
6495             {
6496                Expression newExp { };
6497                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6498
6499                // TODO: Check this...
6500                *newExp = *exp;
6501                newExp.prev = null;
6502                newExp.next = null;
6503                newExp.destType = null;
6504
6505                if(convert.isGet)
6506                {
6507                   // [exp].ColorRGB
6508                   exp.type = memberExp;
6509                   exp.addedThis = true;
6510                   exp.member.exp = newExp;
6511                   FreeType(exp.member.exp.expType);
6512
6513                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6514                   exp.member.exp.expType.classObjectType = objectType;
6515                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6516                   exp.member.memberType = propertyMember;
6517                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6518                   // TESTING THIS... for (int)degrees
6519                   exp.needCast = true;
6520                   if(exp.expType) exp.expType.refCount++;
6521                   ApplyAnyObjectLogic(exp.member.exp);
6522                }
6523                else
6524                {
6525
6526                   /*if(exp.isConstant)
6527                   {
6528                      // Color { ColorRGB = [exp] };
6529                      exp.type = instanceExp;
6530                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6531                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6532                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6533                   }
6534                   else*/
6535                   {
6536                      // If not constant, don't turn it yet into an instantiation
6537                      // (Go through the deep members system first)
6538                      exp.type = memberExp;
6539                      exp.addedThis = true;
6540                      exp.member.exp = newExp;
6541
6542                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6543                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6544                         newExp.expType._class.registered.type == noHeadClass)
6545                      {
6546                         newExp.byReference = true;
6547                      }
6548
6549                      FreeType(exp.member.exp.expType);
6550                      /*exp.member.exp.expType = convert.convert.dataType;
6551                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6552                      exp.member.exp.expType = null;
6553                      if(convert.convert.dataType)
6554                      {
6555                         exp.member.exp.expType = { };
6556                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6557                         exp.member.exp.expType.refCount = 1;
6558                         exp.member.exp.expType.classObjectType = objectType;
6559                         ApplyAnyObjectLogic(exp.member.exp);
6560                      }
6561
6562                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6563                      exp.member.memberType = reverseConversionMember;
6564                      exp.expType = convert.resultType ? convert.resultType :
6565                         MkClassType(convert.convert._class.fullName);
6566                      exp.needCast = true;
6567                      if(convert.resultType) convert.resultType.refCount++;
6568                   }
6569                }
6570             }
6571             else
6572             {
6573                FreeType(exp.expType);
6574                if(convert.isGet)
6575                {
6576                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6577                   if(exp.destType.casted)
6578                      exp.needCast = true;
6579                   if(exp.expType) exp.expType.refCount++;
6580                }
6581                else
6582                {
6583                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6584                   if(exp.destType.casted)
6585                      exp.needCast = true;
6586                   if(convert.resultType)
6587                      convert.resultType.refCount++;
6588                }
6589             }
6590          }
6591          if(exp.isConstant && inCompiler)
6592             ComputeExpression(exp);
6593
6594          converts.Free(FreeConvert);
6595       }
6596
6597       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6598       {
6599          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6600       }
6601       if(!result && exp.expType && exp.destType)
6602       {
6603          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6604              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6605             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6606             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6607             result = true;
6608       }
6609    }
6610    // if(result) CheckTemplateTypes(exp);
6611    return result;
6612 }
6613
6614 void CheckTemplateTypes(Expression exp)
6615 {
6616    Expression nbExp = GetNonBracketsExp(exp);
6617    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6618       (nbExp == exp || nbExp.type != castExp))
6619    {
6620       Expression newExp { };
6621       Context context;
6622       *newExp = *exp;
6623       if(exp.destType) exp.destType.refCount++;
6624       if(exp.expType)  exp.expType.refCount++;
6625       newExp.prev = null;
6626       newExp.next = null;
6627
6628       switch(exp.expType.kind)
6629       {
6630          case doubleType:
6631             if(exp.destType.classObjectType)
6632             {
6633                // We need to pass the address, just pass it along (Undo what was done above)
6634                if(exp.destType) exp.destType.refCount--;
6635                if(exp.expType)  exp.expType.refCount--;
6636                delete newExp;
6637             }
6638             else
6639             {
6640                // If we're looking for value:
6641                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6642                OldList * specs;
6643                OldList * unionDefs = MkList();
6644                OldList * statements = MkList();
6645                context = PushContext();
6646                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6647                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6648                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6649                exp.type = extensionCompoundExp;
6650                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6651                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6652                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6653                exp.compound.compound.context = context;
6654                PopContext(context);
6655             }
6656             break;
6657          default:
6658             exp.type = castExp;
6659             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6660             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6661             exp.needCast = true;
6662             break;
6663       }
6664    }
6665    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6666    {
6667       Expression newExp { };
6668       Context context;
6669       *newExp = *exp;
6670       if(exp.destType) exp.destType.refCount++;
6671       if(exp.expType)  exp.expType.refCount++;
6672       newExp.prev = null;
6673       newExp.next = null;
6674
6675       switch(exp.expType.kind)
6676       {
6677          case doubleType:
6678             if(exp.destType.classObjectType)
6679             {
6680                // We need to pass the address, just pass it along (Undo what was done above)
6681                if(exp.destType) exp.destType.refCount--;
6682                if(exp.expType)  exp.expType.refCount--;
6683                delete newExp;
6684             }
6685             else
6686             {
6687                // If we're looking for value:
6688                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6689                OldList * specs;
6690                OldList * unionDefs = MkList();
6691                OldList * statements = MkList();
6692                context = PushContext();
6693                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6694                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6695                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6696                exp.type = extensionCompoundExp;
6697                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6698                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6699                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6700                exp.compound.compound.context = context;
6701                PopContext(context);
6702             }
6703             break;
6704          case classType:
6705          {
6706             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6707             {
6708                exp.type = bracketsExp;
6709                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6710                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6711                ProcessExpressionType(exp.list->first);
6712                break;
6713             }
6714             else
6715             {
6716                exp.type = bracketsExp;
6717                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6718                exp.needTemplateCast = 2;
6719                newExp.needCast = true;
6720                newExp.needTemplateCast = 2;
6721                ProcessExpressionType(exp.list->first);
6722                break;
6723             }
6724          }
6725          default:
6726          {
6727             if(exp.expType.kind == templateType)
6728             {
6729                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6730                if(type)
6731                {
6732                   FreeType(exp.destType);
6733                   FreeType(exp.expType);
6734                   delete newExp;
6735                   break;
6736                }
6737             }
6738             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6739             {
6740                exp.type = opExp;
6741                exp.op.op = '*';
6742                exp.op.exp1 = null;
6743                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6744                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6745             }
6746             else
6747             {
6748                char typeString[1024];
6749                Declarator decl;
6750                OldList * specs = MkList();
6751                typeString[0] = '\0';
6752                PrintType(exp.expType, typeString, false, false);
6753                decl = SpecDeclFromString(typeString, specs, null);
6754
6755                exp.type = castExp;
6756                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6757                exp.cast.typeName = MkTypeName(specs, decl);
6758                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6759                exp.cast.exp.needCast = true;
6760             }
6761             break;
6762          }
6763       }
6764    }
6765 }
6766 // TODO: The Symbol tree should be reorganized by namespaces
6767 // Name Space:
6768 //    - Tree of all symbols within (stored without namespace)
6769 //    - Tree of sub-namespaces
6770
6771 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6772 {
6773    int nsLen = strlen(nameSpace);
6774    Symbol symbol;
6775    // Start at the name space prefix
6776    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6777    {
6778       char * s = symbol.string;
6779       if(!strncmp(s, nameSpace, nsLen))
6780       {
6781          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6782          int c;
6783          char * namePart;
6784          for(c = strlen(s)-1; c >= 0; c--)
6785             if(s[c] == ':')
6786                break;
6787
6788          namePart = s+c+1;
6789          if(!strcmp(namePart, name))
6790          {
6791             // TODO: Error on ambiguity
6792             return symbol;
6793          }
6794       }
6795       else
6796          break;
6797    }
6798    return null;
6799 }
6800
6801 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6802 {
6803    int c;
6804    char nameSpace[1024];
6805    const char * namePart;
6806    bool gotColon = false;
6807
6808    nameSpace[0] = '\0';
6809    for(c = strlen(name)-1; c >= 0; c--)
6810       if(name[c] == ':')
6811       {
6812          gotColon = true;
6813          break;
6814       }
6815
6816    namePart = name+c+1;
6817    while(c >= 0 && name[c] == ':') c--;
6818    if(c >= 0)
6819    {
6820       // Try an exact match first
6821       Symbol symbol = (Symbol)tree.FindString(name);
6822       if(symbol)
6823          return symbol;
6824
6825       // Namespace specified
6826       memcpy(nameSpace, name, c + 1);
6827       nameSpace[c+1] = 0;
6828
6829       return ScanWithNameSpace(tree, nameSpace, namePart);
6830    }
6831    else if(gotColon)
6832    {
6833       // Looking for a global symbol, e.g. ::Sleep()
6834       Symbol symbol = (Symbol)tree.FindString(namePart);
6835       return symbol;
6836    }
6837    else
6838    {
6839       // Name only (no namespace specified)
6840       Symbol symbol = (Symbol)tree.FindString(namePart);
6841       if(symbol)
6842          return symbol;
6843       return ScanWithNameSpace(tree, "", namePart);
6844    }
6845    return null;
6846 }
6847
6848 static void ProcessDeclaration(Declaration decl);
6849
6850 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6851 {
6852 #ifdef _DEBUG
6853    //Time startTime = GetTime();
6854 #endif
6855    // Optimize this later? Do this before/less?
6856    Context ctx;
6857    Symbol symbol = null;
6858    // First, check if the identifier is declared inside the function
6859    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6860
6861    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6862    {
6863       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6864       {
6865          symbol = null;
6866          if(thisNameSpace)
6867          {
6868             char curName[1024];
6869             strcpy(curName, thisNameSpace);
6870             strcat(curName, "::");
6871             strcat(curName, name);
6872             // Try to resolve in current namespace first
6873             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6874          }
6875          if(!symbol)
6876             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6877       }
6878       else
6879          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6880
6881       if(symbol || ctx == endContext) break;
6882    }
6883    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6884    {
6885       if(symbol.pointerExternal.type == functionExternal)
6886       {
6887          FunctionDefinition function = symbol.pointerExternal.function;
6888
6889          // Modified this recently...
6890          Context tmpContext = curContext;
6891          curContext = null;
6892          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6893          curContext = tmpContext;
6894
6895          symbol.pointerExternal.symbol = symbol;
6896
6897          // TESTING THIS:
6898          DeclareType(symbol.type, true, true);
6899
6900          ast->Insert(curExternal.prev, symbol.pointerExternal);
6901
6902          symbol.id = curExternal.symbol.idCode;
6903
6904       }
6905       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6906       {
6907          ast->Move(symbol.pointerExternal, curExternal.prev);
6908          symbol.id = curExternal.symbol.idCode;
6909       }
6910    }
6911 #ifdef _DEBUG
6912    //findSymbolTotalTime += GetTime() - startTime;
6913 #endif
6914    return symbol;
6915 }
6916
6917 static void GetTypeSpecs(Type type, OldList * specs)
6918 {
6919    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6920    switch(type.kind)
6921    {
6922       case classType:
6923       {
6924          if(type._class.registered)
6925          {
6926             if(!type._class.registered.dataType)
6927                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6928             GetTypeSpecs(type._class.registered.dataType, specs);
6929          }
6930          break;
6931       }
6932       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6933       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6934       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6935       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6936       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6937       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6938       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6939       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6940       case intType:
6941       default:
6942          ListAdd(specs, MkSpecifier(INT)); break;
6943    }
6944 }
6945
6946 static void PrintArraySize(Type arrayType, char * string)
6947 {
6948    char size[256];
6949    size[0] = '\0';
6950    strcat(size, "[");
6951    if(arrayType.enumClass)
6952       strcat(size, arrayType.enumClass.string);
6953    else if(arrayType.arraySizeExp)
6954       PrintExpression(arrayType.arraySizeExp, size);
6955    strcat(size, "]");
6956    strcat(string, size);
6957 }
6958
6959 // WARNING : This function expects a null terminated string since it recursively concatenate...
6960 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6961 {
6962    if(type)
6963    {
6964       if(printConst && type.constant)
6965          strcat(string, "const ");
6966       switch(type.kind)
6967       {
6968          case classType:
6969          {
6970             Symbol c = type._class;
6971             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6972             //       look into merging with thisclass ?
6973             if(type.classObjectType == typedObject)
6974                strcat(string, "typed_object");
6975             else if(type.classObjectType == anyObject)
6976                strcat(string, "any_object");
6977             else
6978             {
6979                if(c && c.string)
6980                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6981             }
6982             if(type.byReference)
6983                strcat(string, " &");
6984             break;
6985          }
6986          case voidType: strcat(string, "void"); break;
6987          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6988          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6989          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6990          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6991          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6992          case _BoolType: strcat(string, "_Bool"); break;
6993          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6994          case floatType: strcat(string, "float"); break;
6995          case doubleType: strcat(string, "double"); break;
6996          case structType:
6997             if(type.enumName)
6998             {
6999                strcat(string, "struct ");
7000                strcat(string, type.enumName);
7001             }
7002             else if(type.typeName)
7003                strcat(string, type.typeName);
7004             else
7005             {
7006                Type member;
7007                strcat(string, "struct { ");
7008                for(member = type.members.first; member; member = member.next)
7009                {
7010                   PrintType(member, string, true, fullName);
7011                   strcat(string,"; ");
7012                }
7013                strcat(string,"}");
7014             }
7015             break;
7016          case unionType:
7017             if(type.enumName)
7018             {
7019                strcat(string, "union ");
7020                strcat(string, type.enumName);
7021             }
7022             else if(type.typeName)
7023                strcat(string, type.typeName);
7024             else
7025             {
7026                strcat(string, "union ");
7027                strcat(string,"(unnamed)");
7028             }
7029             break;
7030          case enumType:
7031             if(type.enumName)
7032             {
7033                strcat(string, "enum ");
7034                strcat(string, type.enumName);
7035             }
7036             else if(type.typeName)
7037                strcat(string, type.typeName);
7038             else
7039                strcat(string, "int"); // "enum");
7040             break;
7041          case ellipsisType:
7042             strcat(string, "...");
7043             break;
7044          case subClassType:
7045             strcat(string, "subclass(");
7046             strcat(string, type._class ? type._class.string : "int");
7047             strcat(string, ")");
7048             break;
7049          case templateType:
7050             strcat(string, type.templateParameter.identifier.string);
7051             break;
7052          case thisClassType:
7053             strcat(string, "thisclass");
7054             break;
7055          case vaListType:
7056             strcat(string, "__builtin_va_list");
7057             break;
7058       }
7059    }
7060 }
7061
7062 static void PrintName(Type type, char * string, bool fullName)
7063 {
7064    if(type.name && type.name[0])
7065    {
7066       if(fullName)
7067          strcat(string, type.name);
7068       else
7069       {
7070          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
7071          if(name) name += 2; else name = type.name;
7072          strcat(string, name);
7073       }
7074    }
7075 }
7076
7077 static void PrintAttribs(Type type, char * string)
7078 {
7079    if(type)
7080    {
7081       if(type.dllExport)   strcat(string, "dllexport ");
7082       if(type.attrStdcall) strcat(string, "stdcall ");
7083    }
7084 }
7085
7086 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
7087 {
7088    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7089    {
7090       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7091          PrintAttribs(type, string);
7092       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7093          strcat(string, " const");
7094       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7095       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7096          strcat(string, " (");
7097       if(type.kind == pointerType)
7098       {
7099          if(type.type.kind == functionType || type.type.kind == methodType)
7100             PrintAttribs(type.type, string);
7101       }
7102       if(type.kind == pointerType)
7103       {
7104          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7105             strcat(string, "*");
7106          else
7107             strcat(string, " *");
7108       }
7109       if(printConst && type.constant && type.kind == pointerType)
7110          strcat(string, " const");
7111    }
7112    else
7113       PrintTypeSpecs(type, string, fullName, printConst);
7114 }
7115
7116 static void PostPrintType(Type type, char * string, bool fullName)
7117 {
7118    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7119       strcat(string, ")");
7120    if(type.kind == arrayType)
7121       PrintArraySize(type, string);
7122    else if(type.kind == functionType)
7123    {
7124       Type param;
7125       strcat(string, "(");
7126       for(param = type.params.first; param; param = param.next)
7127       {
7128          PrintType(param, string, true, fullName);
7129          if(param.next) strcat(string, ", ");
7130       }
7131       strcat(string, ")");
7132    }
7133    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7134       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7135 }
7136
7137 // *****
7138 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7139 // *****
7140 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7141 {
7142    PrePrintType(type, string, fullName, null, printConst);
7143
7144    if(type.thisClass || (printName && type.name && type.name[0]))
7145       strcat(string, " ");
7146    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7147    {
7148       Symbol _class = type.thisClass;
7149       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7150       {
7151          if(type.classObjectType == classPointer)
7152             strcat(string, "class");
7153          else
7154             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7155       }
7156       else if(_class && _class.string)
7157       {
7158          String s = _class.string;
7159          if(fullName)
7160             strcat(string, s);
7161          else
7162          {
7163             char * name = RSearchString(s, "::", strlen(s), true, false);
7164             if(name) name += 2; else name = s;
7165             strcat(string, name);
7166          }
7167       }
7168       strcat(string, "::");
7169    }
7170
7171    if(printName && type.name)
7172       PrintName(type, string, fullName);
7173    PostPrintType(type, string, fullName);
7174    if(type.bitFieldCount)
7175    {
7176       char count[100];
7177       sprintf(count, ":%d", type.bitFieldCount);
7178       strcat(string, count);
7179    }
7180 }
7181
7182 void PrintType(Type type, char * string, bool printName, bool fullName)
7183 {
7184    _PrintType(type, string, printName, fullName, true);
7185 }
7186
7187 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7188 {
7189    _PrintType(type, string, printName, fullName, false);
7190 }
7191
7192 static Type FindMember(Type type, char * string)
7193 {
7194    Type memberType;
7195    for(memberType = type.members.first; memberType; memberType = memberType.next)
7196    {
7197       if(!memberType.name)
7198       {
7199          Type subType = FindMember(memberType, string);
7200          if(subType)
7201             return subType;
7202       }
7203       else if(!strcmp(memberType.name, string))
7204          return memberType;
7205    }
7206    return null;
7207 }
7208
7209 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7210 {
7211    Type memberType;
7212    for(memberType = type.members.first; memberType; memberType = memberType.next)
7213    {
7214       if(!memberType.name)
7215       {
7216          Type subType = FindMember(memberType, string);
7217          if(subType)
7218          {
7219             *offset += memberType.offset;
7220             return subType;
7221          }
7222       }
7223       else if(!strcmp(memberType.name, string))
7224       {
7225          *offset += memberType.offset;
7226          return memberType;
7227       }
7228    }
7229    return null;
7230 }
7231
7232 public bool GetParseError() { return parseError; }
7233
7234 Expression ParseExpressionString(char * expression)
7235 {
7236    parseError = false;
7237
7238    fileInput = TempFile { };
7239    fileInput.Write(expression, 1, strlen(expression));
7240    fileInput.Seek(0, start);
7241
7242    echoOn = false;
7243    parsedExpression = null;
7244    resetScanner();
7245    expression_yyparse();
7246    delete fileInput;
7247
7248    return parsedExpression;
7249 }
7250
7251 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7252 {
7253    Identifier id = exp.identifier;
7254    Method method = null;
7255    Property prop = null;
7256    DataMember member = null;
7257    ClassProperty classProp = null;
7258
7259    if(_class && _class.type == enumClass)
7260    {
7261       NamedLink64 value = null;
7262       Class enumClass = eSystem_FindClass(privateModule, "enum");
7263       if(enumClass)
7264       {
7265          Class baseClass;
7266          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7267          {
7268             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7269             for(value = e.values.first; value; value = value.next)
7270             {
7271                if(!strcmp(value.name, id.string))
7272                   break;
7273             }
7274             if(value)
7275             {
7276                char constant[256];
7277
7278                FreeExpContents(exp);
7279
7280                exp.type = constantExp;
7281                exp.isConstant = true;
7282                if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "char") || !strcmp(baseClass.dataTypeString, "short"))
7283                   sprintf(constant, FORMAT64D, value.data);
7284                else
7285                   sprintf(constant, FORMAT64HEX, value.data);
7286                exp.constant = CopyString(constant);
7287                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7288                exp.expType = MkClassType(baseClass.fullName);
7289                break;
7290             }
7291          }
7292       }
7293       if(value)
7294          return true;
7295    }
7296    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7297    {
7298       ProcessMethodType(method);
7299       exp.expType = Type
7300       {
7301          refCount = 1;
7302          kind = methodType;
7303          method = method;
7304          // Crash here?
7305          // TOCHECK: Put it back to what it was...
7306          // methodClass = _class;
7307          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7308       };
7309       //id._class = null;
7310       return true;
7311    }
7312    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7313    {
7314       if(!prop.dataType)
7315          ProcessPropertyType(prop);
7316       exp.expType = prop.dataType;
7317       if(prop.dataType) prop.dataType.refCount++;
7318       return true;
7319    }
7320    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7321    {
7322       if(!member.dataType)
7323          member.dataType = ProcessTypeString(member.dataTypeString, false);
7324       exp.expType = member.dataType;
7325       if(member.dataType) member.dataType.refCount++;
7326       return true;
7327    }
7328    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7329    {
7330       if(!classProp.dataType)
7331          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7332
7333       if(classProp.constant)
7334       {
7335          FreeExpContents(exp);
7336
7337          exp.isConstant = true;
7338          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7339          {
7340             //char constant[256];
7341             exp.type = stringExp;
7342             exp.constant = QMkString((char *)classProp.Get(_class));
7343          }
7344          else
7345          {
7346             char constant[256];
7347             exp.type = constantExp;
7348             sprintf(constant, "%d", (int)classProp.Get(_class));
7349             exp.constant = CopyString(constant);
7350          }
7351       }
7352       else
7353       {
7354          // TO IMPLEMENT...
7355       }
7356
7357       exp.expType = classProp.dataType;
7358       if(classProp.dataType) classProp.dataType.refCount++;
7359       return true;
7360    }
7361    return false;
7362 }
7363
7364 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7365 {
7366    BinaryTree * tree = &nameSpace.functions;
7367    GlobalData data = (GlobalData)tree->FindString(name);
7368    NameSpace * child;
7369    if(!data)
7370    {
7371       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7372       {
7373          data = ScanGlobalData(child, name);
7374          if(data)
7375             break;
7376       }
7377    }
7378    return data;
7379 }
7380
7381 static GlobalData FindGlobalData(char * name)
7382 {
7383    int start = 0, c;
7384    NameSpace * nameSpace;
7385    nameSpace = globalData;
7386    for(c = 0; name[c]; c++)
7387    {
7388       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7389       {
7390          NameSpace * newSpace;
7391          char * spaceName = new char[c - start + 1];
7392          strncpy(spaceName, name + start, c - start);
7393          spaceName[c-start] = '\0';
7394          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7395          delete spaceName;
7396          if(!newSpace)
7397             return null;
7398          nameSpace = newSpace;
7399          if(name[c] == ':') c++;
7400          start = c+1;
7401       }
7402    }
7403    if(c - start)
7404    {
7405       return ScanGlobalData(nameSpace, name + start);
7406    }
7407    return null;
7408 }
7409
7410 static int definedExpStackPos;
7411 static void * definedExpStack[512];
7412
7413 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7414 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7415 {
7416    Expression prev = checkedExp.prev, next = checkedExp.next;
7417
7418    FreeExpContents(checkedExp);
7419    FreeType(checkedExp.expType);
7420    FreeType(checkedExp.destType);
7421
7422    *checkedExp = *newExp;
7423
7424    delete newExp;
7425
7426    checkedExp.prev = prev;
7427    checkedExp.next = next;
7428 }
7429
7430 void ApplyAnyObjectLogic(Expression e)
7431 {
7432    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7433 #ifdef _DEBUG
7434    char debugExpString[4096];
7435    debugExpString[0] = '\0';
7436    PrintExpression(e, debugExpString);
7437 #endif
7438
7439    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7440    {
7441       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7442       //ellipsisDestType = destType;
7443       if(e && e.expType)
7444       {
7445          Type type = e.expType;
7446          Class _class = null;
7447          //Type destType = e.destType;
7448
7449          if(type.kind == classType && type._class && type._class.registered)
7450          {
7451             _class = type._class.registered;
7452          }
7453          else if(type.kind == subClassType)
7454          {
7455             _class = FindClass("ecere::com::Class").registered;
7456          }
7457          else
7458          {
7459             char string[1024] = "";
7460             Symbol classSym;
7461
7462             PrintTypeNoConst(type, string, false, true);
7463             classSym = FindClass(string);
7464             if(classSym) _class = classSym.registered;
7465          }
7466
7467          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...
7468             (!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))) ||
7469             destType.byReference)))
7470          {
7471             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7472             {
7473                Expression checkedExp = e, newExp;
7474
7475                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7476                {
7477                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7478                   {
7479                      if(checkedExp.type == extensionCompoundExp)
7480                      {
7481                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7482                      }
7483                      else
7484                         checkedExp = checkedExp.list->last;
7485                   }
7486                   else if(checkedExp.type == castExp)
7487                      checkedExp = checkedExp.cast.exp;
7488                }
7489
7490                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7491                {
7492                   newExp = checkedExp.op.exp2;
7493                   checkedExp.op.exp2 = null;
7494                   FreeExpContents(checkedExp);
7495
7496                   if(e.expType && e.expType.passAsTemplate)
7497                   {
7498                      char size[100];
7499                      ComputeTypeSize(e.expType);
7500                      sprintf(size, "%d", e.expType.size);
7501                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7502                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7503                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7504                   }
7505
7506                   ReplaceExpContents(checkedExp, newExp);
7507                   e.byReference = true;
7508                }
7509                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7510                {
7511                   Expression checkedExp; //, newExp;
7512
7513                   {
7514                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7515                      bool hasAddress =
7516                         e.type == identifierExp ||
7517                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7518                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7519                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7520                         e.type == indexExp;
7521
7522                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7523                      {
7524                         Context context = PushContext();
7525                         Declarator decl;
7526                         OldList * specs = MkList();
7527                         char typeString[1024];
7528                         Expression newExp { };
7529
7530                         typeString[0] = '\0';
7531                         *newExp = *e;
7532
7533                         //if(e.destType) e.destType.refCount++;
7534                         // if(exp.expType) exp.expType.refCount++;
7535                         newExp.prev = null;
7536                         newExp.next = null;
7537                         newExp.expType = null;
7538
7539                         PrintTypeNoConst(e.expType, typeString, false, true);
7540                         decl = SpecDeclFromString(typeString, specs, null);
7541                         newExp.destType = ProcessType(specs, decl);
7542
7543                         curContext = context;
7544
7545                         // We need a current compound for this
7546                         if(curCompound)
7547                         {
7548                            char name[100];
7549                            OldList * stmts = MkList();
7550                            e.type = extensionCompoundExp;
7551                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7552                            if(!curCompound.compound.declarations)
7553                               curCompound.compound.declarations = MkList();
7554                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7555                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7556                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7557                            e.compound = MkCompoundStmt(null, stmts);
7558                         }
7559                         else
7560                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7561
7562                         /*
7563                         e.compound = MkCompoundStmt(
7564                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7565                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7566
7567                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7568                         */
7569
7570                         {
7571                            Type type = e.destType;
7572                            e.destType = { };
7573                            CopyTypeInto(e.destType, type);
7574                            e.destType.refCount = 1;
7575                            e.destType.classObjectType = none;
7576                            FreeType(type);
7577                         }
7578
7579                         e.compound.compound.context = context;
7580                         PopContext(context);
7581                         curContext = context.parent;
7582                      }
7583                   }
7584
7585                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7586                   checkedExp = e;
7587                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7588                   {
7589                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7590                      {
7591                         if(checkedExp.type == extensionCompoundExp)
7592                         {
7593                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7594                         }
7595                         else
7596                            checkedExp = checkedExp.list->last;
7597                      }
7598                      else if(checkedExp.type == castExp)
7599                         checkedExp = checkedExp.cast.exp;
7600                   }
7601                   {
7602                      Expression operand { };
7603                      operand = *checkedExp;
7604                      checkedExp.Clear();
7605                      checkedExp.destType = ProcessTypeString("void *", false);
7606                      checkedExp.expType = checkedExp.destType;
7607                      checkedExp.destType.refCount++;
7608
7609                      checkedExp.type = opExp;
7610                      checkedExp.op.op = '&';
7611                      checkedExp.op.exp1 = null;
7612                      checkedExp.op.exp2 = operand;
7613
7614                      //newExp = MkExpOp(null, '&', checkedExp);
7615                   }
7616                   //ReplaceExpContents(checkedExp, newExp);
7617                }
7618             }
7619          }
7620       }
7621    }
7622    {
7623       // If expression type is a simple class, make it an address
7624       // FixReference(e, true);
7625    }
7626 //#if 0
7627    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7628       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7629          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7630    {
7631       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"))
7632       {
7633          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7634       }
7635       else
7636       {
7637          Expression thisExp { };
7638
7639          *thisExp = *e;
7640          thisExp.prev = null;
7641          thisExp.next = null;
7642          e.Clear();
7643
7644          e.type = bracketsExp;
7645          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7646          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7647             ((Expression)e.list->first).byReference = true;
7648
7649          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7650          {
7651             e.expType = thisExp.expType;
7652             e.expType.refCount++;
7653          }
7654          else*/
7655          {
7656             e.expType = { };
7657             CopyTypeInto(e.expType, thisExp.expType);
7658             e.expType.byReference = false;
7659             e.expType.refCount = 1;
7660
7661             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7662                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7663             {
7664                e.expType.classObjectType = none;
7665             }
7666          }
7667       }
7668    }
7669 // TOFIX: Try this for a nice IDE crash!
7670 //#endif
7671    // The other way around
7672    else
7673 //#endif
7674    if(destType && e.expType &&
7675          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7676          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7677          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7678    {
7679       if(destType.kind == ellipsisType)
7680       {
7681          Compiler_Error($"Unspecified type\n");
7682       }
7683       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7684       {
7685          bool byReference = e.expType.byReference;
7686          Expression thisExp { };
7687          Declarator decl;
7688          OldList * specs = MkList();
7689          char typeString[1024]; // Watch buffer overruns
7690          Type type;
7691          ClassObjectType backupClassObjectType;
7692          bool backupByReference;
7693
7694          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7695             type = e.expType;
7696          else
7697             type = destType;
7698
7699          backupClassObjectType = type.classObjectType;
7700          backupByReference = type.byReference;
7701
7702          type.classObjectType = none;
7703          type.byReference = false;
7704
7705          typeString[0] = '\0';
7706          PrintType(type, typeString, false, true);
7707          decl = SpecDeclFromString(typeString, specs, null);
7708
7709          type.classObjectType = backupClassObjectType;
7710          type.byReference = backupByReference;
7711
7712          *thisExp = *e;
7713          thisExp.prev = null;
7714          thisExp.next = null;
7715          e.Clear();
7716
7717          if( ( type.kind == classType && type._class && type._class.registered &&
7718                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7719                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7720              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7721              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7722          {
7723             e.type = opExp;
7724             e.op.op = '*';
7725             e.op.exp1 = null;
7726             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7727
7728             e.expType = { };
7729             CopyTypeInto(e.expType, type);
7730             e.expType.byReference = false;
7731             e.expType.refCount = 1;
7732          }
7733          else
7734          {
7735             e.type = castExp;
7736             e.cast.typeName = MkTypeName(specs, decl);
7737             e.cast.exp = thisExp;
7738             e.byReference = true;
7739             e.expType = type;
7740             type.refCount++;
7741          }
7742          e.destType = destType;
7743          destType.refCount++;
7744       }
7745    }
7746 }
7747
7748 void ApplyLocation(Expression exp, Location loc)
7749 {
7750    exp.loc = loc;
7751    switch(exp.type)
7752    {
7753       case opExp:
7754          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7755          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7756          break;
7757       case bracketsExp:
7758          if(exp.list)
7759          {
7760             Expression e;
7761             for(e = exp.list->first; e; e = e.next)
7762                ApplyLocation(e, loc);
7763          }
7764          break;
7765       case indexExp:
7766          if(exp.index.index)
7767          {
7768             Expression e;
7769             for(e = exp.index.index->first; e; e = e.next)
7770                ApplyLocation(e, loc);
7771          }
7772          if(exp.index.exp)
7773             ApplyLocation(exp.index.exp, loc);
7774          break;
7775       case callExp:
7776          if(exp.call.arguments)
7777          {
7778             Expression arg;
7779             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7780                ApplyLocation(arg, loc);
7781          }
7782          if(exp.call.exp)
7783             ApplyLocation(exp.call.exp, loc);
7784          break;
7785       case memberExp:
7786       case pointerExp:
7787          if(exp.member.exp)
7788             ApplyLocation(exp.member.exp, loc);
7789          break;
7790       case castExp:
7791          if(exp.cast.exp)
7792             ApplyLocation(exp.cast.exp, loc);
7793          break;
7794       case conditionExp:
7795          if(exp.cond.exp)
7796          {
7797             Expression e;
7798             for(e = exp.cond.exp->first; e; e = e.next)
7799                ApplyLocation(e, loc);
7800          }
7801          if(exp.cond.cond)
7802             ApplyLocation(exp.cond.cond, loc);
7803          if(exp.cond.elseExp)
7804             ApplyLocation(exp.cond.elseExp, loc);
7805          break;
7806       case vaArgExp:
7807          if(exp.vaArg.exp)
7808             ApplyLocation(exp.vaArg.exp, loc);
7809          break;
7810       default:
7811          break;
7812    }
7813 }
7814
7815 void ProcessExpressionType(Expression exp)
7816 {
7817    bool unresolved = false;
7818    Location oldyylloc = yylloc;
7819    bool notByReference = false;
7820 #ifdef _DEBUG
7821    char debugExpString[4096];
7822    debugExpString[0] = '\0';
7823    PrintExpression(exp, debugExpString);
7824 #endif
7825    if(!exp || exp.expType)
7826       return;
7827
7828    //eSystem_Logf("%s\n", expString);
7829
7830    // Testing this here
7831    yylloc = exp.loc;
7832    switch(exp.type)
7833    {
7834       case identifierExp:
7835       {
7836          Identifier id = exp.identifier;
7837          if(!id || !topContext) return;
7838
7839          // DOING THIS LATER NOW...
7840          if(id._class && id._class.name)
7841          {
7842             id.classSym = id._class.symbol; // FindClass(id._class.name);
7843             /* TODO: Name Space Fix ups
7844             if(!id.classSym)
7845                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7846             */
7847          }
7848
7849          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7850          {
7851             exp.expType = ProcessTypeString("Module", true);
7852             break;
7853          }
7854          else */
7855          if(!strcmp(id.string, "__runtimePlatform"))
7856          {
7857             exp.expType = ProcessTypeString("ecere::com::Platform", true);
7858             break;
7859          }
7860          else if(strstr(id.string, "__ecereClass") == id.string)
7861          {
7862             exp.expType = ProcessTypeString("ecere::com::Class", true);
7863             break;
7864          }
7865          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7866          {
7867             // Added this here as well
7868             ReplaceClassMembers(exp, thisClass);
7869             if(exp.type != identifierExp)
7870             {
7871                ProcessExpressionType(exp);
7872                break;
7873             }
7874
7875             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7876                break;
7877          }
7878          else
7879          {
7880             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7881             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7882             if(!symbol/* && exp.destType*/)
7883             {
7884                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7885                   break;
7886                else
7887                {
7888                   if(thisClass)
7889                   {
7890                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7891                      if(exp.type != identifierExp)
7892                      {
7893                         ProcessExpressionType(exp);
7894                         break;
7895                      }
7896                   }
7897                   // Static methods called from inside the _class
7898                   else if(currentClass && !id._class)
7899                   {
7900                      if(ResolveIdWithClass(exp, currentClass, true))
7901                         break;
7902                   }
7903                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7904                }
7905             }
7906
7907             // If we manage to resolve this symbol
7908             if(symbol)
7909             {
7910                Type type = symbol.type;
7911                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7912
7913                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7914                {
7915                   Context context = SetupTemplatesContext(_class);
7916                   type = ReplaceThisClassType(_class);
7917                   FinishTemplatesContext(context);
7918                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7919                }
7920
7921                FreeSpecifier(id._class);
7922                id._class = null;
7923                delete id.string;
7924                id.string = CopyString(symbol.string);
7925
7926                id.classSym = null;
7927                exp.expType = type;
7928                if(type)
7929                   type.refCount++;
7930
7931                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7932                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7933                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7934                   // Add missing cases here... enum Classes...
7935                   exp.isConstant = true;
7936
7937                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7938                if(symbol.isParam || !strcmp(id.string, "this"))
7939                {
7940                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7941                      exp.byReference = true;
7942
7943                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7944                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7945                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7946                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7947                   {
7948                      Identifier id = exp.identifier;
7949                      exp.type = bracketsExp;
7950                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7951                   }*/
7952                }
7953
7954                if(symbol.isIterator)
7955                {
7956                   if(symbol.isIterator == 3)
7957                   {
7958                      exp.type = bracketsExp;
7959                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7960                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7961                      exp.expType = null;
7962                      ProcessExpressionType(exp);
7963                   }
7964                   else if(symbol.isIterator != 4)
7965                   {
7966                      exp.type = memberExp;
7967                      exp.member.exp = MkExpIdentifier(exp.identifier);
7968                      exp.member.exp.expType = exp.expType;
7969                      /*if(symbol.isIterator == 6)
7970                         exp.member.member = MkIdentifier("key");
7971                      else*/
7972                         exp.member.member = MkIdentifier("data");
7973                      exp.expType = null;
7974                      ProcessExpressionType(exp);
7975                   }
7976                }
7977                break;
7978             }
7979             else
7980             {
7981                DefinedExpression definedExp = null;
7982                if(thisNameSpace && !(id._class && !id._class.name))
7983                {
7984                   char name[1024];
7985                   strcpy(name, thisNameSpace);
7986                   strcat(name, "::");
7987                   strcat(name, id.string);
7988                   definedExp = eSystem_FindDefine(privateModule, name);
7989                }
7990                if(!definedExp)
7991                   definedExp = eSystem_FindDefine(privateModule, id.string);
7992                if(definedExp)
7993                {
7994                   int c;
7995                   for(c = 0; c<definedExpStackPos; c++)
7996                      if(definedExpStack[c] == definedExp)
7997                         break;
7998                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7999                   {
8000                      Location backupYylloc = yylloc;
8001                      File backInput = fileInput;
8002                      definedExpStack[definedExpStackPos++] = definedExp;
8003
8004                      fileInput = TempFile { };
8005                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
8006                      fileInput.Seek(0, start);
8007
8008                      echoOn = false;
8009                      parsedExpression = null;
8010                      resetScanner();
8011                      expression_yyparse();
8012                      delete fileInput;
8013                      if(backInput)
8014                         fileInput = backInput;
8015
8016                      yylloc = backupYylloc;
8017
8018                      if(parsedExpression)
8019                      {
8020                         FreeIdentifier(id);
8021                         exp.type = bracketsExp;
8022                         exp.list = MkListOne(parsedExpression);
8023                         ApplyLocation(parsedExpression, yylloc);
8024                         ProcessExpressionType(exp);
8025                         definedExpStackPos--;
8026                         return;
8027                      }
8028                      definedExpStackPos--;
8029                   }
8030                   else
8031                   {
8032                      if(inCompiler)
8033                      {
8034                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
8035                      }
8036                   }
8037                }
8038                else
8039                {
8040                   GlobalData data = null;
8041                   if(thisNameSpace && !(id._class && !id._class.name))
8042                   {
8043                      char name[1024];
8044                      strcpy(name, thisNameSpace);
8045                      strcat(name, "::");
8046                      strcat(name, id.string);
8047                      data = FindGlobalData(name);
8048                   }
8049                   if(!data)
8050                      data = FindGlobalData(id.string);
8051                   if(data)
8052                   {
8053                      DeclareGlobalData(data);
8054                      exp.expType = data.dataType;
8055                      if(data.dataType) data.dataType.refCount++;
8056
8057                      delete id.string;
8058                      id.string = CopyString(data.fullName);
8059                      FreeSpecifier(id._class);
8060                      id._class = null;
8061
8062                      break;
8063                   }
8064                   else
8065                   {
8066                      GlobalFunction function = null;
8067                      if(thisNameSpace && !(id._class && !id._class.name))
8068                      {
8069                         char name[1024];
8070                         strcpy(name, thisNameSpace);
8071                         strcat(name, "::");
8072                         strcat(name, id.string);
8073                         function = eSystem_FindFunction(privateModule, name);
8074                      }
8075                      if(!function)
8076                         function = eSystem_FindFunction(privateModule, id.string);
8077                      if(function)
8078                      {
8079                         char name[1024];
8080                         delete id.string;
8081                         id.string = CopyString(function.name);
8082                         name[0] = 0;
8083
8084                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8085                            strcpy(name, "__ecereFunction_");
8086                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8087                         if(DeclareFunction(function, name))
8088                         {
8089                            delete id.string;
8090                            id.string = CopyString(name);
8091                         }
8092                         exp.expType = function.dataType;
8093                         if(function.dataType) function.dataType.refCount++;
8094
8095                         FreeSpecifier(id._class);
8096                         id._class = null;
8097
8098                         break;
8099                      }
8100                   }
8101                }
8102             }
8103          }
8104          unresolved = true;
8105          break;
8106       }
8107       case instanceExp:
8108       {
8109          // Class _class;
8110          // Symbol classSym;
8111
8112          if(!exp.instance._class)
8113          {
8114             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8115             {
8116                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8117             }
8118          }
8119
8120          //classSym = FindClass(exp.instance._class.fullName);
8121          //_class = classSym ? classSym.registered : null;
8122
8123          ProcessInstantiationType(exp.instance);
8124
8125          exp.isConstant = exp.instance.isConstant;
8126
8127          /*
8128          if(_class.type == unitClass && _class.base.type != systemClass)
8129          {
8130             {
8131                Type destType = exp.destType;
8132
8133                exp.destType = MkClassType(_class.base.fullName);
8134                exp.expType = MkClassType(_class.fullName);
8135                CheckExpressionType(exp, exp.destType, true);
8136
8137                exp.destType = destType;
8138             }
8139             exp.expType = MkClassType(_class.fullName);
8140          }
8141          else*/
8142          if(exp.instance._class)
8143          {
8144             exp.expType = MkClassType(exp.instance._class.name);
8145             /*if(exp.expType._class && exp.expType._class.registered &&
8146                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8147                exp.expType.byReference = true;*/
8148          }
8149          break;
8150       }
8151       case constantExp:
8152       {
8153          if(!exp.expType)
8154          {
8155             char * constant = exp.constant;
8156             Type type
8157             {
8158                refCount = 1;
8159                constant = true;
8160             };
8161             exp.expType = type;
8162
8163             if(constant[0] == '\'')
8164             {
8165                if((int)((byte *)constant)[1] > 127)
8166                {
8167                   int nb;
8168                   unichar ch = UTF8GetChar(constant + 1, &nb);
8169                   if(nb < 2) ch = constant[1];
8170                   delete constant;
8171                   exp.constant = PrintUInt(ch);
8172                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8173                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8174                   type._class = FindClass("unichar");
8175
8176                   type.isSigned = false;
8177                }
8178                else
8179                {
8180                   type.kind = charType;
8181                   type.isSigned = true;
8182                }
8183             }
8184             else
8185             {
8186                char * dot = strchr(constant, '.');
8187                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8188                char * exponent;
8189                if(isHex)
8190                {
8191                   exponent = strchr(constant, 'p');
8192                   if(!exponent) exponent = strchr(constant, 'P');
8193                }
8194                else
8195                {
8196                   exponent = strchr(constant, 'e');
8197                   if(!exponent) exponent = strchr(constant, 'E');
8198                }
8199
8200                if(dot || exponent)
8201                {
8202                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8203                      type.kind = floatType;
8204                   else
8205                      type.kind = doubleType;
8206                   type.isSigned = true;
8207                }
8208                else
8209                {
8210                   bool isSigned = constant[0] == '-';
8211                   char * endP = null;
8212                   int64 i64 = strtoll(constant, &endP, 0);
8213                   uint64 ui64 = strtoull(constant, &endP, 0);
8214                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
8215                   if(isSigned)
8216                   {
8217                      if(i64 < MININT)
8218                         is64Bit = true;
8219                   }
8220                   else
8221                   {
8222                      if(ui64 > MAXINT)
8223                      {
8224                         if(ui64 > MAXDWORD)
8225                         {
8226                            is64Bit = true;
8227                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8228                               isSigned = true;
8229                         }
8230                      }
8231                      else if(constant[0] != '0' || !constant[1])
8232                         isSigned = true;
8233                   }
8234                   type.kind = is64Bit ? int64Type : intType;
8235                   type.isSigned = isSigned;
8236                }
8237             }
8238             exp.isConstant = true;
8239             if(exp.destType && exp.destType.kind == doubleType)
8240                type.kind = doubleType;
8241             else if(exp.destType && exp.destType.kind == floatType)
8242                type.kind = floatType;
8243             else if(exp.destType && exp.destType.kind == int64Type)
8244                type.kind = int64Type;
8245          }
8246          break;
8247       }
8248       case stringExp:
8249       {
8250          exp.isConstant = true;      // Why wasn't this constant?
8251          exp.expType = Type
8252          {
8253             refCount = 1;
8254             kind = pointerType;
8255             type = Type
8256             {
8257                refCount = 1;
8258                kind = exp.wideString ? shortType : charType;
8259                constant = true;
8260                isSigned = exp.wideString ? false : true;
8261             }
8262          };
8263          break;
8264       }
8265       case newExp:
8266       case new0Exp:
8267          ProcessExpressionType(exp._new.size);
8268          exp.expType = Type
8269          {
8270             refCount = 1;
8271             kind = pointerType;
8272             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8273          };
8274          DeclareType(exp.expType.type, false, false);
8275          break;
8276       case renewExp:
8277       case renew0Exp:
8278          ProcessExpressionType(exp._renew.size);
8279          ProcessExpressionType(exp._renew.exp);
8280          exp.expType = Type
8281          {
8282             refCount = 1;
8283             kind = pointerType;
8284             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8285          };
8286          DeclareType(exp.expType.type, false, false);
8287          break;
8288       case opExp:
8289       {
8290          bool assign = false, boolResult = false, boolOps = false;
8291          Type type1 = null, type2 = null;
8292          bool useDestType = false, useSideType = false;
8293          Location oldyylloc = yylloc;
8294          bool useSideUnit = false;
8295          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8296
8297          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8298          Type dummy
8299          {
8300             count = 1;
8301             refCount = 1;
8302          };
8303
8304          switch(exp.op.op)
8305          {
8306             // Assignment Operators
8307             case '=':
8308             case MUL_ASSIGN:
8309             case DIV_ASSIGN:
8310             case MOD_ASSIGN:
8311             case ADD_ASSIGN:
8312             case SUB_ASSIGN:
8313             case LEFT_ASSIGN:
8314             case RIGHT_ASSIGN:
8315             case AND_ASSIGN:
8316             case XOR_ASSIGN:
8317             case OR_ASSIGN:
8318                assign = true;
8319                break;
8320             // boolean Operators
8321             case '!':
8322                // Expect boolean operators
8323                //boolOps = true;
8324                //boolResult = true;
8325                break;
8326             case AND_OP:
8327             case OR_OP:
8328                // Expect boolean operands
8329                boolOps = true;
8330                boolResult = true;
8331                break;
8332             // Comparisons
8333             case EQ_OP:
8334             case '<':
8335             case '>':
8336             case LE_OP:
8337             case GE_OP:
8338             case NE_OP:
8339                // Gives boolean result
8340                boolResult = true;
8341                useSideType = true;
8342                break;
8343             case '+':
8344             case '-':
8345                useSideUnit = true;
8346                useSideType = true;
8347                useDestType = true;
8348                break;
8349
8350             case LEFT_OP:
8351             case RIGHT_OP:
8352                useSideType = true;
8353                useDestType = true;
8354                break;
8355
8356             case '|':
8357             case '^':
8358                useSideType = true;
8359                useDestType = true;
8360                break;
8361
8362             case '/':
8363             case '%':
8364                useSideType = true;
8365                useDestType = true;
8366                break;
8367             case '&':
8368             case '*':
8369                if(exp.op.exp1)
8370                {
8371                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8372                   useSideType = true;
8373                   useDestType = true;
8374                }
8375                break;
8376
8377             /*// Implement speed etc.
8378             case '*':
8379             case '/':
8380                break;
8381             */
8382          }
8383          if(exp.op.op == '&')
8384          {
8385             // Added this here earlier for Iterator address as key
8386             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8387             {
8388                Identifier id = exp.op.exp2.identifier;
8389                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8390                if(symbol && symbol.isIterator == 2)
8391                {
8392                   exp.type = memberExp;
8393                   exp.member.exp = exp.op.exp2;
8394                   exp.member.member = MkIdentifier("key");
8395                   exp.expType = null;
8396                   exp.op.exp2.expType = symbol.type;
8397                   symbol.type.refCount++;
8398                   ProcessExpressionType(exp);
8399                   FreeType(dummy);
8400                   break;
8401                }
8402                // exp.op.exp2.usage.usageRef = true;
8403             }
8404          }
8405
8406          //dummy.kind = TypeDummy;
8407          if(exp.op.exp1)
8408          {
8409             // Added this check here to use the dest type only for units derived from the base unit
8410             // So that untyped units will use the side unit as opposed to the untyped destination unit
8411             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8412             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8413                useDestType = false;
8414
8415             if(destClass && useDestType &&
8416               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8417
8418               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8419             {
8420                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8421                exp.op.exp1.destType = exp.destType;
8422                exp.op.exp1.opDestType = true;
8423                if(exp.destType)
8424                   exp.destType.refCount++;
8425             }
8426             else if(!assign)
8427             {
8428                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8429                exp.op.exp1.destType = dummy;
8430                dummy.refCount++;
8431             }
8432
8433             // TESTING THIS HERE...
8434             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8435                ProcessExpressionType(exp.op.exp1);
8436             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8437
8438             exp.op.exp1.opDestType = false;
8439
8440             // Fix for unit and ++ / --
8441             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8442                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8443             {
8444                exp.op.exp2 = MkExpConstant("1");
8445                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8446                assign = true;
8447             }
8448
8449             if(exp.op.exp1.destType == dummy)
8450             {
8451                FreeType(dummy);
8452                exp.op.exp1.destType = null;
8453             }
8454             type1 = exp.op.exp1.expType;
8455          }
8456
8457          if(exp.op.exp2)
8458          {
8459             char expString[10240];
8460             expString[0] = '\0';
8461             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8462             {
8463                if(exp.op.exp1)
8464                {
8465                   exp.op.exp2.destType = exp.op.exp1.expType;
8466                   if(exp.op.exp1.expType)
8467                      exp.op.exp1.expType.refCount++;
8468                }
8469                else
8470                {
8471                   exp.op.exp2.destType = exp.destType;
8472                   if(!exp.op.exp1 || exp.op.op != '&')
8473                      exp.op.exp2.opDestType = true;
8474                   if(exp.destType)
8475                      exp.destType.refCount++;
8476                }
8477
8478                if(type1) type1.refCount++;
8479                exp.expType = type1;
8480             }
8481             else if(assign)
8482             {
8483                if(inCompiler)
8484                   PrintExpression(exp.op.exp2, expString);
8485
8486                if(type1 && type1.kind == pointerType)
8487                {
8488                   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 ||
8489                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8490                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8491                   else if(exp.op.op == '=')
8492                   {
8493                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8494                      exp.op.exp2.destType = type1;
8495                      if(type1)
8496                         type1.refCount++;
8497                   }
8498                }
8499                else
8500                {
8501                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8502                   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/* ||
8503                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8504                   else
8505                   {
8506                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8507                      exp.op.exp2.destType = type1;
8508                      if(type1)
8509                         type1.refCount++;
8510                   }
8511                }
8512                if(type1) type1.refCount++;
8513                exp.expType = type1;
8514             }
8515             else if(destClass &&
8516                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8517                   (destClass.type == enumClass && useDestType)))
8518             {
8519                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8520                exp.op.exp2.destType = exp.destType;
8521                if(exp.op.op != '&')
8522                   exp.op.exp2.opDestType = true;
8523                if(exp.destType)
8524                   exp.destType.refCount++;
8525             }
8526             else
8527             {
8528                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8529                exp.op.exp2.destType = dummy;
8530                dummy.refCount++;
8531             }
8532
8533             // TESTING THIS HERE... (DANGEROUS)
8534             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8535                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8536             {
8537                FreeType(exp.op.exp2.destType);
8538                exp.op.exp2.destType = type1;
8539                type1.refCount++;
8540             }
8541             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8542             // Cannot lose the cast on a sizeof
8543             if(exp.op.op == SIZEOF)
8544             {
8545                Expression e = exp.op.exp2;
8546                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8547                {
8548                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8549                   {
8550                      if(e.type == extensionCompoundExp)
8551                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8552                      else
8553                         e = e.list->last;
8554                   }
8555                }
8556                if(e.type == castExp && e.cast.exp)
8557                   e.cast.exp.needCast = true;
8558             }
8559             ProcessExpressionType(exp.op.exp2);
8560             exp.op.exp2.opDestType = false;
8561             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8562
8563             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8564             {
8565                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)
8566                {
8567                   if(exp.op.op != '=' && type1.type.kind == voidType)
8568                      Compiler_Error($"void *: unknown size\n");
8569                }
8570                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||
8571                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8572                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8573                               exp.op.exp2.expType._class.registered.type == structClass ||
8574                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8575                {
8576                   if(exp.op.op == ADD_ASSIGN)
8577                      Compiler_Error($"cannot add two pointers\n");
8578                }
8579                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8580                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8581                {
8582                   if(exp.op.op == ADD_ASSIGN)
8583                      Compiler_Error($"cannot add two pointers\n");
8584                }
8585                else if(inCompiler)
8586                {
8587                   char type1String[1024];
8588                   char type2String[1024];
8589                   type1String[0] = '\0';
8590                   type2String[0] = '\0';
8591
8592                   PrintType(exp.op.exp2.expType, type1String, false, true);
8593                   PrintType(type1, type2String, false, true);
8594                   ChangeCh(expString, '\n', ' ');
8595                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8596                }
8597             }
8598
8599             if(exp.op.exp2.destType == dummy)
8600             {
8601                FreeType(dummy);
8602                exp.op.exp2.destType = null;
8603             }
8604
8605             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8606             {
8607                type2 = { };
8608                type2.refCount = 1;
8609                CopyTypeInto(type2, exp.op.exp2.expType);
8610                type2.isSigned = true;
8611             }
8612             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8613             {
8614                type2 = { kind = intType };
8615                type2.refCount = 1;
8616                type2.isSigned = true;
8617             }
8618             else
8619             {
8620                type2 = exp.op.exp2.expType;
8621                if(type2) type2.refCount++;
8622             }
8623          }
8624
8625          dummy.kind = voidType;
8626
8627          if(exp.op.op == SIZEOF)
8628          {
8629             exp.expType = Type
8630             {
8631                refCount = 1;
8632                kind = intSizeType;
8633             };
8634             exp.isConstant = true;
8635          }
8636          // Get type of dereferenced pointer
8637          else if(exp.op.op == '*' && !exp.op.exp1)
8638          {
8639             exp.expType = Dereference(type2);
8640             if(type2 && type2.kind == classType)
8641                notByReference = true;
8642          }
8643          else if(exp.op.op == '&' && !exp.op.exp1)
8644             exp.expType = Reference(type2);
8645          else if(!assign)
8646          {
8647             if(boolOps)
8648             {
8649                if(exp.op.exp1)
8650                {
8651                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8652                   exp.op.exp1.destType = MkClassType("bool");
8653                   exp.op.exp1.destType.truth = true;
8654                   if(!exp.op.exp1.expType)
8655                      ProcessExpressionType(exp.op.exp1);
8656                   else
8657                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8658                   FreeType(exp.op.exp1.expType);
8659                   exp.op.exp1.expType = MkClassType("bool");
8660                   exp.op.exp1.expType.truth = true;
8661                }
8662                if(exp.op.exp2)
8663                {
8664                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8665                   exp.op.exp2.destType = MkClassType("bool");
8666                   exp.op.exp2.destType.truth = true;
8667                   if(!exp.op.exp2.expType)
8668                      ProcessExpressionType(exp.op.exp2);
8669                   else
8670                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8671                   FreeType(exp.op.exp2.expType);
8672                   exp.op.exp2.expType = MkClassType("bool");
8673                   exp.op.exp2.expType.truth = true;
8674                }
8675             }
8676             else if(exp.op.exp1 && exp.op.exp2 &&
8677                ((useSideType /*&&
8678                      (useSideUnit ||
8679                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8680                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8681                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8682                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8683             {
8684                if(type1 && type2 &&
8685                   // If either both are class or both are not class
8686                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8687                {
8688                   // Added this check for enum subtraction to result in an int type:
8689                   if(exp.op.op == '-' &&
8690                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8691                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8692                   {
8693                      Type intType;
8694                      if(!type1._class.registered.dataType)
8695                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8696                      if(!type2._class.registered.dataType)
8697                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8698
8699                      intType = ProcessTypeString(
8700                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8701
8702                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8703                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8704                      exp.op.exp1.destType = intType;
8705                      exp.op.exp2.destType = intType;
8706                      intType.refCount++;
8707                   }
8708                   else
8709                   {
8710                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8711                      exp.op.exp2.destType = type1;
8712                      type1.refCount++;
8713                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8714                      exp.op.exp1.destType = type2;
8715                      type2.refCount++;
8716                   }
8717
8718                   // Warning here for adding Radians + Degrees with no destination type
8719                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8720                      type1._class.registered && type1._class.registered.type == unitClass &&
8721                      type2._class.registered && type2._class.registered.type == unitClass &&
8722                      type1._class.registered != type2._class.registered)
8723                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8724                         type1._class.string, type2._class.string, type1._class.string);
8725
8726                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8727                   {
8728                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8729                      if(argExp)
8730                      {
8731                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8732
8733                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8734                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8735                            exp.op.exp1)));
8736
8737                         ProcessExpressionType(exp.op.exp1);
8738
8739                         if(type2.kind != pointerType)
8740                         {
8741                            ProcessExpressionType(classExp);
8742
8743                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8744
8745                            if(!exp.op.exp2.expType)
8746                            {
8747                               if(type2)
8748                                  FreeType(type2);
8749                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8750                               type2.refCount++;
8751                            }
8752
8753                            ProcessExpressionType(exp.op.exp2);
8754                         }
8755                      }
8756                   }
8757
8758                   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)))
8759                   {
8760                      if(type1.kind != classType && type1.type.kind == voidType)
8761                         Compiler_Error($"void *: unknown size\n");
8762                      exp.expType = type1;
8763                      if(type1) type1.refCount++;
8764                   }
8765                   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)))
8766                   {
8767                      if(type2.kind != classType && type2.type.kind == voidType)
8768                         Compiler_Error($"void *: unknown size\n");
8769                      exp.expType = type2;
8770                      if(type2) type2.refCount++;
8771                   }
8772                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8773                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8774                   {
8775                      Compiler_Warning($"different levels of indirection\n");
8776                   }
8777                   else
8778                   {
8779                      bool success = false;
8780                      if(type1.kind == pointerType && type2.kind == pointerType)
8781                      {
8782                         if(exp.op.op == '+')
8783                            Compiler_Error($"cannot add two pointers\n");
8784                         else if(exp.op.op == '-')
8785                         {
8786                            // Pointer Subtraction gives integer
8787                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8788                            {
8789                               exp.expType = Type
8790                               {
8791                                  kind = intType;
8792                                  refCount = 1;
8793                               };
8794                               success = true;
8795
8796                               if(type1.type.kind == templateType)
8797                               {
8798                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8799                                  if(argExp)
8800                                  {
8801                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8802
8803                                     ProcessExpressionType(classExp);
8804
8805                                     exp.type = bracketsExp;
8806                                     exp.list = MkListOne(MkExpOp(
8807                                        MkExpBrackets(MkListOne(MkExpOp(
8808                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8809                                              , exp.op.op,
8810                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8811                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8812
8813                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8814                                     FreeType(dummy);
8815                                     return;
8816                                  }
8817                               }
8818                            }
8819                         }
8820                      }
8821
8822                      if(!success && exp.op.exp1.type == constantExp)
8823                      {
8824                         // If first expression is constant, try to match that first
8825                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8826                         {
8827                            if(exp.expType) FreeType(exp.expType);
8828                            exp.expType = exp.op.exp1.destType;
8829                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8830                            success = true;
8831                         }
8832                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8833                         {
8834                            if(exp.expType) FreeType(exp.expType);
8835                            exp.expType = exp.op.exp2.destType;
8836                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8837                            success = true;
8838                         }
8839                      }
8840                      else if(!success)
8841                      {
8842                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8843                         {
8844                            if(exp.expType) FreeType(exp.expType);
8845                            exp.expType = exp.op.exp2.destType;
8846                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8847                            success = true;
8848                         }
8849                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8850                         {
8851                            if(exp.expType) FreeType(exp.expType);
8852                            exp.expType = exp.op.exp1.destType;
8853                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8854                            success = true;
8855                         }
8856                      }
8857                      if(!success)
8858                      {
8859                         char expString1[10240];
8860                         char expString2[10240];
8861                         char type1[1024];
8862                         char type2[1024];
8863                         expString1[0] = '\0';
8864                         expString2[0] = '\0';
8865                         type1[0] = '\0';
8866                         type2[0] = '\0';
8867                         if(inCompiler)
8868                         {
8869                            PrintExpression(exp.op.exp1, expString1);
8870                            ChangeCh(expString1, '\n', ' ');
8871                            PrintExpression(exp.op.exp2, expString2);
8872                            ChangeCh(expString2, '\n', ' ');
8873                            PrintType(exp.op.exp1.expType, type1, false, true);
8874                            PrintType(exp.op.exp2.expType, type2, false, true);
8875                         }
8876
8877                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8878                      }
8879                   }
8880                }
8881                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8882                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8883                {
8884                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8885                   // Convert e.g. / 4 into / 4.0
8886                   exp.op.exp1.destType = type2._class.registered.dataType;
8887                   if(type2._class.registered.dataType)
8888                      type2._class.registered.dataType.refCount++;
8889                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8890                   exp.expType = type2;
8891                   if(type2) type2.refCount++;
8892                }
8893                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8894                {
8895                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8896                   // Convert e.g. / 4 into / 4.0
8897                   exp.op.exp2.destType = type1._class.registered.dataType;
8898                   if(type1._class.registered.dataType)
8899                      type1._class.registered.dataType.refCount++;
8900                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8901                   exp.expType = type1;
8902                   if(type1) type1.refCount++;
8903                }
8904                else if(type1)
8905                {
8906                   bool valid = false;
8907
8908                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8909                   {
8910                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8911
8912                      if(!type1._class.registered.dataType)
8913                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8914                      exp.op.exp2.destType = type1._class.registered.dataType;
8915                      exp.op.exp2.destType.refCount++;
8916
8917                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8918                      if(type2)
8919                         FreeType(type2);
8920                      type2 = exp.op.exp2.destType;
8921                      if(type2) type2.refCount++;
8922
8923                      exp.expType = type2;
8924                      type2.refCount++;
8925                   }
8926
8927                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8928                   {
8929                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8930
8931                      if(!type2._class.registered.dataType)
8932                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8933                      exp.op.exp1.destType = type2._class.registered.dataType;
8934                      exp.op.exp1.destType.refCount++;
8935
8936                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8937                      type1 = exp.op.exp1.destType;
8938                      exp.expType = type1;
8939                      type1.refCount++;
8940                   }
8941
8942                   // TESTING THIS NEW CODE
8943                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8944                   {
8945                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8946                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8947                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8948                      {
8949                         // Convert the enum to an int instead for these operators
8950                         if(op1IsEnum && exp.op.exp2.expType)
8951                         {
8952                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8953                            {
8954                               if(exp.expType) FreeType(exp.expType);
8955                               exp.expType = exp.op.exp2.expType;
8956                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8957                               valid = true;
8958                            }
8959                         }
8960                         else if(op2IsEnum && exp.op.exp1.expType)
8961                         {
8962                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8963                            {
8964                               if(exp.expType) FreeType(exp.expType);
8965                               exp.expType = exp.op.exp1.expType;
8966                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8967                               valid = true;
8968                            }
8969                         }
8970                      }
8971                      else
8972                      {
8973                         if(op1IsEnum && exp.op.exp2.expType)
8974                         {
8975                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8976                            {
8977                               if(exp.expType) FreeType(exp.expType);
8978                               exp.expType = exp.op.exp1.expType;
8979                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8980                               valid = true;
8981                            }
8982                         }
8983                         else if(op2IsEnum && exp.op.exp1.expType)
8984                         {
8985                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8986                            {
8987                               if(exp.expType) FreeType(exp.expType);
8988                               exp.expType = exp.op.exp2.expType;
8989                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8990                               valid = true;
8991                            }
8992                         }
8993                      }
8994                   }
8995
8996                   if(!valid)
8997                   {
8998                      // 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
8999                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
9000                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
9001                      {
9002                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9003                         exp.op.exp1.destType = type2;
9004                         type2.refCount++;
9005
9006                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9007                         {
9008                            if(exp.expType) FreeType(exp.expType);
9009                            exp.expType = exp.op.exp1.destType;
9010                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9011                         }
9012                      }
9013                      else
9014                      {
9015                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9016                         exp.op.exp2.destType = type1;
9017                         type1.refCount++;
9018
9019                      /*
9020                      // Maybe this was meant to be an enum...
9021                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9022                      {
9023                         Type oldType = exp.op.exp2.expType;
9024                         exp.op.exp2.expType = null;
9025                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
9026                            FreeType(oldType);
9027                         else
9028                            exp.op.exp2.expType = oldType;
9029                      }
9030                      */
9031
9032                      /*
9033                      // TESTING THIS HERE... LATEST ADDITION
9034                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9035                      {
9036                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9037                         exp.op.exp2.destType = type2._class.registered.dataType;
9038                         if(type2._class.registered.dataType)
9039                            type2._class.registered.dataType.refCount++;
9040                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
9041
9042                         //exp.expType = type2._class.registered.dataType; //type2;
9043                         //if(type2) type2.refCount++;
9044                      }
9045
9046                      // TESTING THIS HERE... LATEST ADDITION
9047                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9048                      {
9049                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9050                         exp.op.exp1.destType = type1._class.registered.dataType;
9051                         if(type1._class.registered.dataType)
9052                            type1._class.registered.dataType.refCount++;
9053                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
9054                         exp.expType = type1._class.registered.dataType; //type1;
9055                         if(type1) type1.refCount++;
9056                      }
9057                      */
9058
9059                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
9060                         {
9061                            if(exp.expType) FreeType(exp.expType);
9062                            exp.expType = exp.op.exp2.destType;
9063                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
9064                         }
9065                         else if(type1 && type2)
9066                         {
9067                            char expString1[10240];
9068                            char expString2[10240];
9069                            char type1String[1024];
9070                            char type2String[1024];
9071                            expString1[0] = '\0';
9072                            expString2[0] = '\0';
9073                            type1String[0] = '\0';
9074                            type2String[0] = '\0';
9075                            if(inCompiler)
9076                            {
9077                               PrintExpression(exp.op.exp1, expString1);
9078                               ChangeCh(expString1, '\n', ' ');
9079                               PrintExpression(exp.op.exp2, expString2);
9080                               ChangeCh(expString2, '\n', ' ');
9081                               PrintType(exp.op.exp1.expType, type1String, false, true);
9082                               PrintType(exp.op.exp2.expType, type2String, false, true);
9083                            }
9084
9085                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9086
9087                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9088                            {
9089                               exp.expType = exp.op.exp1.expType;
9090                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9091                            }
9092                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9093                            {
9094                               exp.expType = exp.op.exp2.expType;
9095                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9096                            }
9097                         }
9098                      }
9099                   }
9100                }
9101                else if(type2)
9102                {
9103                   // Maybe this was meant to be an enum...
9104                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9105                   {
9106                      Type oldType = exp.op.exp1.expType;
9107                      exp.op.exp1.expType = null;
9108                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9109                         FreeType(oldType);
9110                      else
9111                         exp.op.exp1.expType = oldType;
9112                   }
9113
9114                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9115                   exp.op.exp1.destType = type2;
9116                   type2.refCount++;
9117                   /*
9118                   // TESTING THIS HERE... LATEST ADDITION
9119                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9120                   {
9121                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9122                      exp.op.exp1.destType = type1._class.registered.dataType;
9123                      if(type1._class.registered.dataType)
9124                         type1._class.registered.dataType.refCount++;
9125                   }
9126
9127                   // TESTING THIS HERE... LATEST ADDITION
9128                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9129                   {
9130                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9131                      exp.op.exp2.destType = type2._class.registered.dataType;
9132                      if(type2._class.registered.dataType)
9133                         type2._class.registered.dataType.refCount++;
9134                   }
9135                   */
9136
9137                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9138                   {
9139                      if(exp.expType) FreeType(exp.expType);
9140                      exp.expType = exp.op.exp1.destType;
9141                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9142                   }
9143                }
9144             }
9145             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9146             {
9147                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9148                {
9149                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9150                   // Convert e.g. / 4 into / 4.0
9151                   exp.op.exp1.destType = type2._class.registered.dataType;
9152                   if(type2._class.registered.dataType)
9153                      type2._class.registered.dataType.refCount++;
9154                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9155                }
9156                if(exp.op.op == '!')
9157                {
9158                   exp.expType = MkClassType("bool");
9159                   exp.expType.truth = true;
9160                }
9161                else
9162                {
9163                   exp.expType = type2;
9164                   if(type2) type2.refCount++;
9165                }
9166             }
9167             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9168             {
9169                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9170                {
9171                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9172                   // Convert e.g. / 4 into / 4.0
9173                   exp.op.exp2.destType = type1._class.registered.dataType;
9174                   if(type1._class.registered.dataType)
9175                      type1._class.registered.dataType.refCount++;
9176                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9177                }
9178                exp.expType = type1;
9179                if(type1) type1.refCount++;
9180             }
9181          }
9182
9183          yylloc = exp.loc;
9184          if(exp.op.exp1 && !exp.op.exp1.expType)
9185          {
9186             char expString[10000];
9187             expString[0] = '\0';
9188             if(inCompiler)
9189             {
9190                PrintExpression(exp.op.exp1, expString);
9191                ChangeCh(expString, '\n', ' ');
9192             }
9193             if(expString[0])
9194                Compiler_Error($"couldn't determine type of %s\n", expString);
9195          }
9196          if(exp.op.exp2 && !exp.op.exp2.expType)
9197          {
9198             char expString[10240];
9199             expString[0] = '\0';
9200             if(inCompiler)
9201             {
9202                PrintExpression(exp.op.exp2, expString);
9203                ChangeCh(expString, '\n', ' ');
9204             }
9205             if(expString[0])
9206                Compiler_Error($"couldn't determine type of %s\n", expString);
9207          }
9208
9209          if(boolResult)
9210          {
9211             FreeType(exp.expType);
9212             exp.expType = MkClassType("bool");
9213             exp.expType.truth = true;
9214          }
9215
9216          if(exp.op.op != SIZEOF)
9217             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9218                (!exp.op.exp2 || exp.op.exp2.isConstant);
9219
9220          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9221          {
9222             DeclareType(exp.op.exp2.expType, false, false);
9223          }
9224
9225          if(exp.op.op == DELETE && exp.op.exp2 && exp.op.exp2.expType && exp.op.exp2.expType.specConst)
9226             Compiler_Warning($"deleting const qualified object\n");
9227
9228          yylloc = oldyylloc;
9229
9230          FreeType(dummy);
9231          if(type2)
9232             FreeType(type2);
9233          break;
9234       }
9235       case bracketsExp:
9236       case extensionExpressionExp:
9237       {
9238          Expression e;
9239          exp.isConstant = true;
9240          for(e = exp.list->first; e; e = e.next)
9241          {
9242             bool inced = false;
9243             if(!e.next)
9244             {
9245                FreeType(e.destType);
9246                e.opDestType = exp.opDestType;
9247                e.destType = exp.destType;
9248                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9249             }
9250             ProcessExpressionType(e);
9251             if(inced)
9252                exp.destType.count--;
9253             if(!exp.expType && !e.next)
9254             {
9255                exp.expType = e.expType;
9256                if(e.expType) e.expType.refCount++;
9257             }
9258             if(!e.isConstant)
9259                exp.isConstant = false;
9260          }
9261
9262          // In case a cast became a member...
9263          e = exp.list->first;
9264          if(!e.next && e.type == memberExp)
9265          {
9266             // Preserve prev, next
9267             Expression next = exp.next, prev = exp.prev;
9268
9269
9270             FreeType(exp.expType);
9271             FreeType(exp.destType);
9272             delete exp.list;
9273
9274             *exp = *e;
9275
9276             exp.prev = prev;
9277             exp.next = next;
9278
9279             delete e;
9280
9281             ProcessExpressionType(exp);
9282          }
9283          break;
9284       }
9285       case indexExp:
9286       {
9287          Expression e;
9288          exp.isConstant = true;
9289
9290          ProcessExpressionType(exp.index.exp);
9291          if(!exp.index.exp.isConstant)
9292             exp.isConstant = false;
9293
9294          if(exp.index.exp.expType)
9295          {
9296             Type source = exp.index.exp.expType;
9297             if(source.kind == classType && source._class && source._class.registered)
9298             {
9299                Class _class = source._class.registered;
9300                Class c = _class.templateClass ? _class.templateClass : _class;
9301                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9302                {
9303                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9304
9305                   if(exp.index.index && exp.index.index->last)
9306                   {
9307                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9308
9309                      if(type.kind == classType) type.constant = true;
9310                      else if(type.kind == pointerType)
9311                      {
9312                         Type t = type;
9313                         while(t.kind == pointerType) t = t.type;
9314                         t.constant = true;
9315                      }
9316
9317                      ((Expression)exp.index.index->last).destType = type;
9318                   }
9319                }
9320             }
9321          }
9322
9323          for(e = exp.index.index->first; e; e = e.next)
9324          {
9325             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9326             {
9327                if(e.destType) FreeType(e.destType);
9328                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9329             }
9330             ProcessExpressionType(e);
9331             if(!e.next)
9332             {
9333                // Check if this type is int
9334             }
9335             if(!e.isConstant)
9336                exp.isConstant = false;
9337          }
9338
9339          if(!exp.expType)
9340             exp.expType = Dereference(exp.index.exp.expType);
9341          if(exp.expType)
9342             DeclareType(exp.expType, false, false);
9343          break;
9344       }
9345       case callExp:
9346       {
9347          Expression e;
9348          Type functionType;
9349          Type methodType = null;
9350          char name[1024];
9351          name[0] = '\0';
9352
9353          if(inCompiler)
9354          {
9355             PrintExpression(exp.call.exp,  name);
9356             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9357             {
9358                //exp.call.exp.expType = null;
9359                PrintExpression(exp.call.exp,  name);
9360             }
9361          }
9362          if(exp.call.exp.type == identifierExp)
9363          {
9364             Expression idExp = exp.call.exp;
9365             Identifier id = idExp.identifier;
9366             if(!strcmp(id.string, "__builtin_frame_address"))
9367             {
9368                exp.expType = ProcessTypeString("void *", true);
9369                if(exp.call.arguments && exp.call.arguments->first)
9370                   ProcessExpressionType(exp.call.arguments->first);
9371                break;
9372             }
9373             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9374             {
9375                exp.expType = ProcessTypeString("int", true);
9376                if(exp.call.arguments && exp.call.arguments->first)
9377                   ProcessExpressionType(exp.call.arguments->first);
9378                break;
9379             }
9380             else if(!strcmp(id.string, "Max") ||
9381                !strcmp(id.string, "Min") ||
9382                !strcmp(id.string, "Sgn") ||
9383                !strcmp(id.string, "Abs"))
9384             {
9385                Expression a = null;
9386                Expression b = null;
9387                Expression tempExp1 = null, tempExp2 = null;
9388                if((!strcmp(id.string, "Max") ||
9389                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9390                {
9391                   a = exp.call.arguments->first;
9392                   b = exp.call.arguments->last;
9393                   tempExp1 = a;
9394                   tempExp2 = b;
9395                }
9396                else if(exp.call.arguments->count == 1)
9397                {
9398                   a = exp.call.arguments->first;
9399                   tempExp1 = a;
9400                }
9401
9402                if(a)
9403                {
9404                   exp.call.arguments->Clear();
9405                   idExp.identifier = null;
9406
9407                   FreeExpContents(exp);
9408
9409                   ProcessExpressionType(a);
9410                   if(b)
9411                      ProcessExpressionType(b);
9412
9413                   exp.type = bracketsExp;
9414                   exp.list = MkList();
9415
9416                   if(a.expType && (!b || b.expType))
9417                   {
9418                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9419                      {
9420                         // Use the simpleStruct name/ids for now...
9421                         if(inCompiler)
9422                         {
9423                            OldList * specs = MkList();
9424                            OldList * decls = MkList();
9425                            Declaration decl;
9426                            char temp1[1024], temp2[1024];
9427
9428                            GetTypeSpecs(a.expType, specs);
9429
9430                            if(a && !a.isConstant && a.type != identifierExp)
9431                            {
9432                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9433                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9434                               tempExp1 = QMkExpId(temp1);
9435                               tempExp1.expType = a.expType;
9436                               if(a.expType)
9437                                  a.expType.refCount++;
9438                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9439                            }
9440                            if(b && !b.isConstant && b.type != identifierExp)
9441                            {
9442                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9443                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9444                               tempExp2 = QMkExpId(temp2);
9445                               tempExp2.expType = b.expType;
9446                               if(b.expType)
9447                                  b.expType.refCount++;
9448                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9449                            }
9450
9451                            decl = MkDeclaration(specs, decls);
9452                            if(!curCompound.compound.declarations)
9453                               curCompound.compound.declarations = MkList();
9454                            curCompound.compound.declarations->Insert(null, decl);
9455                         }
9456                      }
9457                   }
9458
9459                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9460                   {
9461                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9462                      ListAdd(exp.list,
9463                         MkExpCondition(MkExpBrackets(MkListOne(
9464                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9465                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9466                      exp.expType = a.expType;
9467                      if(a.expType)
9468                         a.expType.refCount++;
9469                   }
9470                   else if(!strcmp(id.string, "Abs"))
9471                   {
9472                      ListAdd(exp.list,
9473                         MkExpCondition(MkExpBrackets(MkListOne(
9474                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9475                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9476                      exp.expType = a.expType;
9477                      if(a.expType)
9478                         a.expType.refCount++;
9479                   }
9480                   else if(!strcmp(id.string, "Sgn"))
9481                   {
9482                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9483                      ListAdd(exp.list,
9484                         MkExpCondition(MkExpBrackets(MkListOne(
9485                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9486                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9487                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9488                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9489                      exp.expType = ProcessTypeString("int", false);
9490                   }
9491
9492                   FreeExpression(tempExp1);
9493                   if(tempExp2) FreeExpression(tempExp2);
9494
9495                   FreeIdentifier(id);
9496                   break;
9497                }
9498             }
9499          }
9500
9501          {
9502             Type dummy
9503             {
9504                count = 1;
9505                refCount = 1;
9506             };
9507             if(!exp.call.exp.destType)
9508             {
9509                exp.call.exp.destType = dummy;
9510                dummy.refCount++;
9511             }
9512             ProcessExpressionType(exp.call.exp);
9513             if(exp.call.exp.destType == dummy)
9514             {
9515                FreeType(dummy);
9516                exp.call.exp.destType = null;
9517             }
9518             FreeType(dummy);
9519          }
9520
9521          // Check argument types against parameter types
9522          functionType = exp.call.exp.expType;
9523
9524          if(functionType && functionType.kind == TypeKind::methodType)
9525          {
9526             methodType = functionType;
9527             functionType = methodType.method.dataType;
9528
9529             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9530             // TOCHECK: Instead of doing this here could this be done per param?
9531             if(exp.call.exp.expType.usedClass)
9532             {
9533                char typeString[1024];
9534                typeString[0] = '\0';
9535                {
9536                   Symbol back = functionType.thisClass;
9537                   // Do not output class specifier here (thisclass was added to this)
9538                   functionType.thisClass = null;
9539                   PrintType(functionType, typeString, true, true);
9540                   functionType.thisClass = back;
9541                }
9542                if(strstr(typeString, "thisclass"))
9543                {
9544                   OldList * specs = MkList();
9545                   Declarator decl;
9546                   {
9547                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9548
9549                      decl = SpecDeclFromString(typeString, specs, null);
9550
9551                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9552                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9553                         exp.call.exp.expType.usedClass))
9554                         thisClassParams = false;
9555
9556                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9557                      {
9558                         Class backupThisClass = thisClass;
9559                         thisClass = exp.call.exp.expType.usedClass;
9560                         ProcessDeclarator(decl);
9561                         thisClass = backupThisClass;
9562                      }
9563
9564                      thisClassParams = true;
9565
9566                      functionType = ProcessType(specs, decl);
9567                      functionType.refCount = 0;
9568                      FinishTemplatesContext(context);
9569
9570                      // Mark parameters that were 'thisclass'
9571                      /*{
9572                         Type p, op;
9573                         for(p = functionType.params.first, op = methodType.method.dataType.params.first; p && op; p = p.next, op = op.next)
9574                            p.wasThisClass = op.kind == thisClassType;
9575                      }*/
9576                   }
9577
9578                   FreeList(specs, FreeSpecifier);
9579                   FreeDeclarator(decl);
9580                 }
9581             }
9582          }
9583          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9584          {
9585             Type type = functionType.type;
9586             if(!functionType.refCount)
9587             {
9588                functionType.type = null;
9589                FreeType(functionType);
9590             }
9591             //methodType = functionType;
9592             functionType = type;
9593          }
9594          if(functionType && functionType.kind != TypeKind::functionType)
9595          {
9596             Compiler_Error($"called object %s is not a function\n", name);
9597          }
9598          else if(functionType)
9599          {
9600             bool emptyParams = false, noParams = false;
9601             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9602             Type type = functionType.params.first;
9603             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9604             int extra = 0;
9605             Location oldyylloc = yylloc;
9606
9607             if(!type) emptyParams = true;
9608
9609             // WORKING ON THIS:
9610             if(functionType.extraParam && e && functionType.thisClass)
9611             {
9612                e.destType = MkClassType(functionType.thisClass.string);
9613                e = e.next;
9614             }
9615
9616             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9617             // Fixed #141 by adding '&& !functionType.extraParam'
9618             if(!functionType.staticMethod && !functionType.extraParam)
9619             {
9620                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9621                   memberExp.member.exp.expType._class)
9622                {
9623                   type = MkClassType(memberExp.member.exp.expType._class.string);
9624                   if(e)
9625                   {
9626                      e.destType = type;
9627                      e = e.next;
9628                      type = functionType.params.first;
9629                   }
9630                   else
9631                      type.refCount = 0;
9632                }
9633                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9634                {
9635                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9636                   type.byReference = functionType.byReference;
9637                   type.typedByReference = functionType.typedByReference;
9638                   if(e)
9639                   {
9640                      // Allow manually passing a class for typed object
9641                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9642                         e = e.next;
9643                      e.destType = type;
9644                      e = e.next;
9645                      type = functionType.params.first;
9646                   }
9647                   else
9648                      type.refCount = 0;
9649                   //extra = 1;
9650                }
9651             }
9652
9653             if(type && type.kind == voidType)
9654             {
9655                noParams = true;
9656                if(!type.refCount) FreeType(type);
9657                type = null;
9658             }
9659
9660             for( ; e; e = e.next)
9661             {
9662                if(!type && !emptyParams)
9663                {
9664                   yylloc = e.loc;
9665                   if(methodType && methodType.methodClass)
9666                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9667                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9668                         noParams ? 0 : functionType.params.count);
9669                   else
9670                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9671                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9672                         noParams ? 0 : functionType.params.count);
9673                   break;
9674                }
9675
9676                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9677                {
9678                   Type templatedType = null;
9679                   Class _class = methodType.usedClass;
9680                   ClassTemplateParameter curParam = null;
9681                   int id = 0;
9682                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9683                   {
9684                      Class sClass;
9685                      for(sClass = _class; sClass; sClass = sClass.base)
9686                      {
9687                         if(sClass.templateClass) sClass = sClass.templateClass;
9688                         id = 0;
9689                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9690                         {
9691                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9692                            {
9693                               Class nextClass;
9694                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9695                               {
9696                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9697                                  id += nextClass.templateParams.count;
9698                               }
9699                               break;
9700                            }
9701                            id++;
9702                         }
9703                         if(curParam) break;
9704                      }
9705                   }
9706                   if(curParam && _class.templateArgs[id].dataTypeString)
9707                   {
9708                      bool constant = type.constant;
9709                      ClassTemplateArgument arg = _class.templateArgs[id];
9710                      {
9711                         Context context = SetupTemplatesContext(_class);
9712
9713                         /*if(!arg.dataType)
9714                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9715                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9716                         FinishTemplatesContext(context);
9717                      }
9718
9719                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9720                      else if(templatedType.kind == pointerType)
9721                      {
9722                         Type t = templatedType.type;
9723                         while(t.kind == pointerType) t = t.type;
9724                         if(constant) t.constant = constant;
9725                      }
9726
9727                      e.destType = templatedType;
9728                      if(templatedType)
9729                      {
9730                         templatedType.passAsTemplate = true;
9731                         // templatedType.refCount++;
9732                      }
9733                   }
9734                   else
9735                   {
9736                      e.destType = type;
9737                      if(type) type.refCount++;
9738                   }
9739                }
9740                else
9741                {
9742                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9743                   {
9744                      e.destType = type.prev;
9745                      e.destType.refCount++;
9746                   }
9747                   else
9748                   {
9749                      e.destType = type;
9750                      if(type) type.refCount++;
9751                   }
9752                }
9753                // Don't reach the end for the ellipsis
9754                if(type && type.kind != ellipsisType)
9755                {
9756                   Type next = type.next;
9757                   if(!type.refCount) FreeType(type);
9758                   type = next;
9759                }
9760             }
9761
9762             if(type && type.kind != ellipsisType)
9763             {
9764                if(methodType && methodType.methodClass)
9765                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9766                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9767                      functionType.params.count + extra);
9768                else
9769                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9770                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9771                      functionType.params.count + extra);
9772             }
9773             yylloc = oldyylloc;
9774             if(type && !type.refCount) FreeType(type);
9775          }
9776          else
9777          {
9778             functionType = Type
9779             {
9780                refCount = 0;
9781                kind = TypeKind::functionType;
9782             };
9783
9784             if(exp.call.exp.type == identifierExp)
9785             {
9786                char * string = exp.call.exp.identifier.string;
9787                if(inCompiler)
9788                {
9789                   Symbol symbol;
9790                   Location oldyylloc = yylloc;
9791
9792                   yylloc = exp.call.exp.identifier.loc;
9793                   if(strstr(string, "__builtin_") == string)
9794                   {
9795                      if(exp.destType)
9796                      {
9797                         functionType.returnType = exp.destType;
9798                         exp.destType.refCount++;
9799                      }
9800                   }
9801                   else
9802                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9803                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9804                   globalContext.symbols.Add((BTNode)symbol);
9805                   if(strstr(symbol.string, "::"))
9806                      globalContext.hasNameSpace = true;
9807
9808                   yylloc = oldyylloc;
9809                }
9810             }
9811             else if(exp.call.exp.type == memberExp)
9812             {
9813                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9814                   exp.call.exp.member.member.string);*/
9815             }
9816             else
9817                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9818
9819             if(!functionType.returnType)
9820             {
9821                functionType.returnType = Type
9822                {
9823                   refCount = 1;
9824                   kind = intType;
9825                };
9826             }
9827          }
9828          if(functionType && functionType.kind == TypeKind::functionType)
9829          {
9830             exp.expType = functionType.returnType;
9831
9832             if(functionType.returnType)
9833                functionType.returnType.refCount++;
9834
9835             if(!functionType.refCount)
9836                FreeType(functionType);
9837          }
9838
9839          if(exp.call.arguments)
9840          {
9841             for(e = exp.call.arguments->first; e; e = e.next)
9842                ProcessExpressionType(e);
9843          }
9844          break;
9845       }
9846       case memberExp:
9847       {
9848          Type type;
9849          Location oldyylloc = yylloc;
9850          bool thisPtr;
9851          Expression checkExp = exp.member.exp;
9852          while(checkExp)
9853          {
9854             if(checkExp.type == castExp)
9855                checkExp = checkExp.cast.exp;
9856             else if(checkExp.type == bracketsExp)
9857                checkExp = checkExp.list ? checkExp.list->first : null;
9858             else
9859                break;
9860          }
9861
9862          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9863          exp.thisPtr = thisPtr;
9864
9865          // DOING THIS LATER NOW...
9866          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9867          {
9868             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9869             /* TODO: Name Space Fix ups
9870             if(!exp.member.member.classSym)
9871                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9872             */
9873          }
9874
9875          ProcessExpressionType(exp.member.exp);
9876          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9877             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9878          {
9879             exp.isConstant = false;
9880          }
9881          else
9882             exp.isConstant = exp.member.exp.isConstant;
9883          type = exp.member.exp.expType;
9884
9885          yylloc = exp.loc;
9886
9887          if(type && (type.kind == templateType))
9888          {
9889             Class _class = thisClass ? thisClass : currentClass;
9890             ClassTemplateParameter param = null;
9891             if(_class)
9892             {
9893                for(param = _class.templateParams.first; param; param = param.next)
9894                {
9895                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9896                      break;
9897                }
9898             }
9899             if(param && param.defaultArg.member)
9900             {
9901                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9902                if(argExp)
9903                {
9904                   Expression expMember = exp.member.exp;
9905                   Declarator decl;
9906                   OldList * specs = MkList();
9907                   char thisClassTypeString[1024];
9908
9909                   FreeIdentifier(exp.member.member);
9910
9911                   ProcessExpressionType(argExp);
9912
9913                   {
9914                      char * colon = strstr(param.defaultArg.memberString, "::");
9915                      if(colon)
9916                      {
9917                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9918                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9919                      }
9920                      else
9921                         strcpy(thisClassTypeString, _class.fullName);
9922                   }
9923
9924                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9925
9926                   exp.expType = ProcessType(specs, decl);
9927                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9928                   {
9929                      Class expClass = exp.expType._class.registered;
9930                      Class cClass = null;
9931                      int paramCount = 0;
9932                      int lastParam = -1;
9933
9934                      char templateString[1024];
9935                      ClassTemplateParameter param;
9936                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9937                      for(cClass = expClass; cClass; cClass = cClass.base)
9938                      {
9939                         int p = 0;
9940                         for(param = cClass.templateParams.first; param; param = param.next)
9941                         {
9942                            int id = p;
9943                            Class sClass;
9944                            ClassTemplateArgument arg;
9945                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9946                            arg = expClass.templateArgs[id];
9947
9948                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9949                            {
9950                               ClassTemplateParameter cParam;
9951                               //int p = numParams - sClass.templateParams.count;
9952                               int p = 0;
9953                               Class nextClass;
9954                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9955
9956                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9957                               {
9958                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9959                                  {
9960                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9961                                     {
9962                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9963                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9964                                        break;
9965                                     }
9966                                  }
9967                               }
9968                            }
9969
9970                            {
9971                               char argument[256];
9972                               argument[0] = '\0';
9973                               /*if(arg.name)
9974                               {
9975                                  strcat(argument, arg.name.string);
9976                                  strcat(argument, " = ");
9977                               }*/
9978                               switch(param.type)
9979                               {
9980                                  case expression:
9981                                  {
9982                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9983                                     char expString[1024];
9984                                     OldList * specs = MkList();
9985                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9986                                     Expression exp;
9987                                     char * string = PrintHexUInt64(arg.expression.ui64);
9988                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9989                                     delete string;
9990
9991                                     ProcessExpressionType(exp);
9992                                     ComputeExpression(exp);
9993                                     expString[0] = '\0';
9994                                     PrintExpression(exp, expString);
9995                                     strcat(argument, expString);
9996                                     // delete exp;
9997                                     FreeExpression(exp);
9998                                     break;
9999                                  }
10000                                  case identifier:
10001                                  {
10002                                     strcat(argument, arg.member.name);
10003                                     break;
10004                                  }
10005                                  case TemplateParameterType::type:
10006                                  {
10007                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10008                                     {
10009                                        if(!strcmp(arg.dataTypeString, "thisclass"))
10010                                           strcat(argument, thisClassTypeString);
10011                                        else
10012                                           strcat(argument, arg.dataTypeString);
10013                                     }
10014                                     break;
10015                                  }
10016                               }
10017                               if(argument[0])
10018                               {
10019                                  if(paramCount) strcat(templateString, ", ");
10020                                  if(lastParam != p - 1)
10021                                  {
10022                                     strcat(templateString, param.name);
10023                                     strcat(templateString, " = ");
10024                                  }
10025                                  strcat(templateString, argument);
10026                                  paramCount++;
10027                                  lastParam = p;
10028                               }
10029                               p++;
10030                            }
10031                         }
10032                      }
10033                      {
10034                         int len = strlen(templateString);
10035                         if(templateString[len-1] == '>') templateString[len++] = ' ';
10036                         templateString[len++] = '>';
10037                         templateString[len++] = '\0';
10038                      }
10039                      {
10040                         Context context = SetupTemplatesContext(_class);
10041                         FreeType(exp.expType);
10042                         exp.expType = ProcessTypeString(templateString, false);
10043                         FinishTemplatesContext(context);
10044                      }
10045                   }
10046
10047                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
10048                   exp.type = bracketsExp;
10049                   exp.list = MkListOne(MkExpOp(null, '*',
10050                   /*opExp;
10051                   exp.op.op = '*';
10052                   exp.op.exp1 = null;
10053                   exp.op.exp2 = */
10054                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
10055                      MkExpBrackets(MkListOne(
10056                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
10057                            '+',
10058                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
10059                            '+',
10060                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
10061
10062                            ));
10063                }
10064             }
10065             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
10066                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
10067             {
10068                type = ProcessTemplateParameterType(type.templateParameter);
10069             }
10070          }
10071          // TODO: *** This seems to be where we should add method support for all basic types ***
10072          if(type && (type.kind == templateType));
10073          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
10074                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
10075                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
10076                           (type.kind == pointerType && type.type.kind == charType)))
10077          {
10078             Identifier id = exp.member.member;
10079             TypeKind typeKind = type.kind;
10080             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10081             if(typeKind == subClassType && exp.member.exp.type == classExp)
10082             {
10083                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10084                typeKind = classType;
10085             }
10086
10087             if(id)
10088             {
10089                if(typeKind == intType || typeKind == enumType)
10090                   _class = eSystem_FindClass(privateModule, "int");
10091                else if(!_class)
10092                {
10093                   if(type.kind == classType && type._class && type._class.registered)
10094                   {
10095                      _class = type._class.registered;
10096                   }
10097                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10098                   {
10099                      _class = FindClass("char *").registered;
10100                   }
10101                   else if(type.kind == pointerType)
10102                   {
10103                      _class = eSystem_FindClass(privateModule, "uintptr");
10104                      FreeType(exp.expType);
10105                      exp.expType = ProcessTypeString("uintptr", false);
10106                      exp.byReference = true;
10107                   }
10108                   else
10109                   {
10110                      char string[1024] = "";
10111                      Symbol classSym;
10112                      PrintTypeNoConst(type, string, false, true);
10113                      classSym = FindClass(string);
10114                      if(classSym) _class = classSym.registered;
10115                   }
10116                }
10117             }
10118
10119             if(_class && id)
10120             {
10121                /*bool thisPtr =
10122                   (exp.member.exp.type == identifierExp &&
10123                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10124                Property prop = null;
10125                Method method = null;
10126                DataMember member = null;
10127                Property revConvert = null;
10128                ClassProperty classProp = null;
10129
10130                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10131                   exp.member.memberType = propertyMember;
10132
10133                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10134                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10135
10136                if(typeKind != subClassType)
10137                {
10138                   // Prioritize data members over properties for "this"
10139                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10140                   {
10141                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10142                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10143                      {
10144                         prop = eClass_FindProperty(_class, id.string, privateModule);
10145                         if(prop)
10146                            member = null;
10147                      }
10148                      if(!member && !prop)
10149                         prop = eClass_FindProperty(_class, id.string, privateModule);
10150                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10151                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10152                         exp.member.thisPtr = true;
10153                   }
10154                   // Prioritize properties over data members otherwise
10155                   else
10156                   {
10157                      bool useMemberForNonConst = false;
10158                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10159                      if(!id.classSym)
10160                      {
10161                         prop = eClass_FindProperty(_class, id.string, null);
10162
10163                         useMemberForNonConst = prop && exp.destType &&
10164                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10165                               !strncmp(prop.dataTypeString, "const ", 6);
10166
10167                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10168                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10169                      }
10170
10171                      if((!prop || useMemberForNonConst) && !member)
10172                      {
10173                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10174                         if(!method)
10175                         {
10176                            prop = eClass_FindProperty(_class, id.string, privateModule);
10177
10178                            useMemberForNonConst |= prop && exp.destType &&
10179                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10180                                  !strncmp(prop.dataTypeString, "const ", 6);
10181
10182                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10183                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10184                         }
10185                      }
10186
10187                      if(member && prop)
10188                      {
10189                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10190                            prop = null;
10191                         else
10192                            member = null;
10193                      }
10194                   }
10195                }
10196                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10197                   method = eClass_FindMethod(_class, id.string, privateModule);
10198                if(!prop && !member && !method)
10199                {
10200                   if(typeKind == subClassType)
10201                   {
10202                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10203                      if(classProp)
10204                      {
10205                         exp.member.memberType = classPropertyMember;
10206                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10207                      }
10208                      else
10209                      {
10210                         // Assume this is a class_data member
10211                         char structName[1024];
10212                         Identifier id = exp.member.member;
10213                         Expression classExp = exp.member.exp;
10214                         type.refCount++;
10215
10216                         FreeType(classExp.expType);
10217                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10218
10219                         strcpy(structName, "__ecereClassData_");
10220                         FullClassNameCat(structName, type._class.string, false);
10221                         exp.type = pointerExp;
10222                         exp.member.member = id;
10223
10224                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10225                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10226                               MkExpBrackets(MkListOne(MkExpOp(
10227                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10228                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10229                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10230                                  )));
10231
10232                         FreeType(type);
10233
10234                         ProcessExpressionType(exp);
10235                         return;
10236                      }
10237                   }
10238                   else
10239                   {
10240                      // Check for reverse conversion
10241                      // (Convert in an instantiation later, so that we can use
10242                      //  deep properties system)
10243                      Symbol classSym = FindClass(id.string);
10244                      if(classSym)
10245                      {
10246                         Class convertClass = classSym.registered;
10247                         if(convertClass)
10248                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10249                      }
10250                   }
10251                }
10252
10253                //if(!exp.member.exp.destType)
10254                if(exp.member.exp.destType)
10255                   FreeType(exp.member.exp.destType);
10256                {
10257                   if(method && !method._class.symbol)
10258                      method._class.symbol = FindClass(method._class.fullName);
10259                   if(prop && !prop._class.symbol)
10260                      prop._class.symbol = FindClass(prop._class.fullName);
10261
10262                   exp.member.exp.destType = Type
10263                   {
10264                      refCount = 1;
10265                      kind = classType;
10266                      _class = prop ? prop._class.symbol : method ? method._class.symbol : _class.symbol;
10267                      // wasThisClass = type ? type.wasThisClass : false;
10268                   };
10269                }
10270
10271                if(prop)
10272                {
10273                   exp.member.memberType = propertyMember;
10274                   if(!prop.dataType)
10275                      ProcessPropertyType(prop);
10276                   exp.expType = prop.dataType;
10277                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10278                   {
10279                      Type type { };
10280                      CopyTypeInto(type, exp.expType);
10281                      type.refCount = 1;
10282                      type.constant = true;
10283                      exp.expType = type;
10284                   }
10285                   else if(prop.dataType)
10286                      prop.dataType.refCount++;
10287                }
10288                else if(member)
10289                {
10290                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10291                   {
10292                      FreeExpContents(exp);
10293                      exp.type = identifierExp;
10294                      exp.identifier = MkIdentifier("class");
10295                      ProcessExpressionType(exp);
10296                      return;
10297                   }
10298
10299                   exp.member.memberType = dataMember;
10300                   DeclareStruct(_class.fullName, false);
10301                   if(!member.dataType)
10302                   {
10303                      Context context = SetupTemplatesContext(_class);
10304                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10305                      FinishTemplatesContext(context);
10306                   }
10307                   exp.expType = member.dataType;
10308                   if(member.dataType) member.dataType.refCount++;
10309                }
10310                else if(revConvert)
10311                {
10312                   exp.member.memberType = reverseConversionMember;
10313                   exp.expType = MkClassType(revConvert._class.fullName);
10314                }
10315                else if(method)
10316                {
10317                   //if(inCompiler)
10318                   {
10319                      /*if(id._class)
10320                      {
10321                         exp.type = identifierExp;
10322                         exp.identifier = exp.member.member;
10323                      }
10324                      else*/
10325                         exp.member.memberType = methodMember;
10326                   }
10327                   if(!method.dataType)
10328                      ProcessMethodType(method);
10329                   exp.expType = Type
10330                   {
10331                      refCount = 1;
10332                      kind = methodType;
10333                      method = method;
10334                   };
10335
10336                   // Tricky spot here... To use instance versus class virtual table
10337                   // Put it back to what it was... What did we break?
10338
10339                   // Had to put it back for overriding Main of Thread global instance
10340
10341                   //exp.expType.methodClass = _class;
10342                   exp.expType.methodClass = (id && id._class) ? _class : null;
10343
10344                   // Need the actual class used for templated classes
10345                   exp.expType.usedClass = _class;
10346                }
10347                else if(!classProp)
10348                {
10349                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10350                   {
10351                      FreeExpContents(exp);
10352                      exp.type = identifierExp;
10353                      exp.identifier = MkIdentifier("class");
10354                      FreeType(exp.expType);
10355                      exp.expType = MkClassType("ecere::com::Class");
10356                      return;
10357                   }
10358                   yylloc = exp.member.member.loc;
10359                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10360                   if(inCompiler)
10361                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10362                }
10363
10364                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10365                {
10366                   Class tClass;
10367
10368                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10369                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10370
10371                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10372                   {
10373                      int id = 0;
10374                      ClassTemplateParameter curParam = null;
10375                      Class sClass;
10376
10377                      for(sClass = tClass; sClass; sClass = sClass.base)
10378                      {
10379                         id = 0;
10380                         if(sClass.templateClass) sClass = sClass.templateClass;
10381                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10382                         {
10383                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10384                            {
10385                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10386                                  id += sClass.templateParams.count;
10387                               break;
10388                            }
10389                            id++;
10390                         }
10391                         if(curParam) break;
10392                      }
10393
10394                      if(curParam && tClass.templateArgs[id].dataTypeString)
10395                      {
10396                         ClassTemplateArgument arg = tClass.templateArgs[id];
10397                         Context context = SetupTemplatesContext(tClass);
10398                         bool constant = exp.expType.constant;
10399                         /*if(!arg.dataType)
10400                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10401                         FreeType(exp.expType);
10402
10403                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10404                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10405                         else if(exp.expType.kind == pointerType)
10406                         {
10407                            Type t = exp.expType.type;
10408                            while(t.kind == pointerType) t = t.type;
10409                            if(constant) t.constant = constant;
10410                         }
10411                         if(exp.expType)
10412                         {
10413                            if(exp.expType.kind == thisClassType)
10414                            {
10415                               FreeType(exp.expType);
10416                               exp.expType = ReplaceThisClassType(_class);
10417                            }
10418
10419                            if(tClass.templateClass && (exp.expType.kind != templateType || (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType))))
10420                               exp.expType.passAsTemplate = true;
10421                            //exp.expType.refCount++;
10422                            if(!exp.destType)
10423                            {
10424                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10425                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10426                               else if(exp.destType.kind == pointerType)
10427                               {
10428                                  Type t = exp.destType.type;
10429                                  while(t.kind == pointerType) t = t.type;
10430                                  if(constant) t.constant = constant;
10431                               }
10432
10433                               //exp.destType.refCount++;
10434
10435                               if(exp.destType.kind == thisClassType)
10436                               {
10437                                  FreeType(exp.destType);
10438                                  exp.destType = ReplaceThisClassType(_class);
10439                               }
10440                            }
10441                         }
10442                         FinishTemplatesContext(context);
10443                      }
10444                   }
10445                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10446                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10447                   {
10448                      int id = 0;
10449                      ClassTemplateParameter curParam = null;
10450                      Class sClass;
10451
10452                      for(sClass = tClass; sClass; sClass = sClass.base)
10453                      {
10454                         id = 0;
10455                         if(sClass.templateClass) sClass = sClass.templateClass;
10456                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10457                         {
10458                            if(curParam.type == TemplateParameterType::type &&
10459                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10460                            {
10461                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10462                                  id += sClass.templateParams.count;
10463                               break;
10464                            }
10465                            id++;
10466                         }
10467                         if(curParam) break;
10468                      }
10469
10470                      if(curParam)
10471                      {
10472                         ClassTemplateArgument arg = tClass.templateArgs[id];
10473                         Context context = SetupTemplatesContext(tClass);
10474                         Type basicType;
10475                         /*if(!arg.dataType)
10476                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10477
10478                         basicType = ProcessTypeString(arg.dataTypeString, false);
10479                         if(basicType)
10480                         {
10481                            if(basicType.kind == thisClassType)
10482                            {
10483                               FreeType(basicType);
10484                               basicType = ReplaceThisClassType(_class);
10485                            }
10486
10487                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10488                            if(tClass.templateClass)
10489                               basicType.passAsTemplate = true;
10490                            */
10491
10492                            FreeType(exp.expType);
10493
10494                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10495                            //exp.expType.refCount++;
10496                            if(!exp.destType)
10497                            {
10498                               exp.destType = exp.expType;
10499                               exp.destType.refCount++;
10500                            }
10501
10502                            {
10503                               Expression newExp { };
10504                               OldList * specs = MkList();
10505                               Declarator decl;
10506                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10507                               *newExp = *exp;
10508                               if(exp.destType) exp.destType.refCount++;
10509                               if(exp.expType)  exp.expType.refCount++;
10510                               exp.type = castExp;
10511                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10512                               exp.cast.exp = newExp;
10513                               //FreeType(exp.expType);
10514                               //exp.expType = null;
10515                               //ProcessExpressionType(sourceExp);
10516                            }
10517                         }
10518                         FinishTemplatesContext(context);
10519                      }
10520                   }
10521                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10522                   {
10523                      Class expClass = exp.expType._class.registered;
10524                      if(expClass)
10525                      {
10526                         Class cClass = null;
10527                         int p = 0;
10528                         int paramCount = 0;
10529                         int lastParam = -1;
10530                         char templateString[1024];
10531                         ClassTemplateParameter param;
10532                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10533                         while(cClass != expClass)
10534                         {
10535                            Class sClass;
10536                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10537                            cClass = sClass;
10538
10539                            for(param = cClass.templateParams.first; param; param = param.next)
10540                            {
10541                               Class cClassCur = null;
10542                               int cp = 0;
10543                               ClassTemplateParameter paramCur = null;
10544                               ClassTemplateArgument arg;
10545                               while(cClassCur != tClass && !paramCur)
10546                               {
10547                                  Class sClassCur;
10548                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10549                                  cClassCur = sClassCur;
10550
10551                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10552                                  {
10553                                     if(!strcmp(paramCur.name, param.name))
10554                                     {
10555
10556                                        break;
10557                                     }
10558                                     cp++;
10559                                  }
10560                               }
10561                               if(paramCur && paramCur.type == TemplateParameterType::type)
10562                                  arg = tClass.templateArgs[cp];
10563                               else
10564                                  arg = expClass.templateArgs[p];
10565
10566                               {
10567                                  char argument[256];
10568                                  argument[0] = '\0';
10569                                  /*if(arg.name)
10570                                  {
10571                                     strcat(argument, arg.name.string);
10572                                     strcat(argument, " = ");
10573                                  }*/
10574                                  switch(param.type)
10575                                  {
10576                                     case expression:
10577                                     {
10578                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10579                                        char expString[1024];
10580                                        OldList * specs = MkList();
10581                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10582                                        Expression exp;
10583                                        char * string = PrintHexUInt64(arg.expression.ui64);
10584                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10585                                        delete string;
10586
10587                                        ProcessExpressionType(exp);
10588                                        ComputeExpression(exp);
10589                                        expString[0] = '\0';
10590                                        PrintExpression(exp, expString);
10591                                        strcat(argument, expString);
10592                                        // delete exp;
10593                                        FreeExpression(exp);
10594                                        break;
10595                                     }
10596                                     case identifier:
10597                                     {
10598                                        strcat(argument, arg.member.name);
10599                                        break;
10600                                     }
10601                                     case TemplateParameterType::type:
10602                                     {
10603                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10604                                           strcat(argument, arg.dataTypeString);
10605                                        break;
10606                                     }
10607                                  }
10608                                  if(argument[0])
10609                                  {
10610                                     if(paramCount) strcat(templateString, ", ");
10611                                     if(lastParam != p - 1)
10612                                     {
10613                                        strcat(templateString, param.name);
10614                                        strcat(templateString, " = ");
10615                                     }
10616                                     strcat(templateString, argument);
10617                                     paramCount++;
10618                                     lastParam = p;
10619                                  }
10620                               }
10621                               p++;
10622                            }
10623                         }
10624                         {
10625                            int len = strlen(templateString);
10626                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10627                            templateString[len++] = '>';
10628                            templateString[len++] = '\0';
10629                         }
10630
10631                         FreeType(exp.expType);
10632                         {
10633                            Context context = SetupTemplatesContext(tClass);
10634                            exp.expType = ProcessTypeString(templateString, false);
10635                            FinishTemplatesContext(context);
10636                         }
10637                      }
10638                   }
10639                }
10640             }
10641             else
10642                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10643          }
10644          else if(type && (type.kind == structType || type.kind == unionType))
10645          {
10646             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10647             if(memberType)
10648             {
10649                exp.expType = memberType;
10650                if(memberType)
10651                   memberType.refCount++;
10652             }
10653          }
10654          else
10655          {
10656             char expString[10240];
10657             expString[0] = '\0';
10658             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10659             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10660          }
10661
10662          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10663          {
10664             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10665             {
10666                Identifier id = exp.member.member;
10667                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10668                if(_class)
10669                {
10670                   FreeType(exp.expType);
10671                   exp.expType = ReplaceThisClassType(_class);
10672                }
10673             }
10674          }
10675          yylloc = oldyylloc;
10676          break;
10677       }
10678       // Convert x->y into (*x).y
10679       case pointerExp:
10680       {
10681          Type destType = exp.destType;
10682
10683          // DOING THIS LATER NOW...
10684          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10685          {
10686             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10687             /* TODO: Name Space Fix ups
10688             if(!exp.member.member.classSym)
10689                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10690             */
10691          }
10692
10693          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10694          exp.type = memberExp;
10695          if(destType)
10696             destType.count++;
10697          ProcessExpressionType(exp);
10698          if(destType)
10699             destType.count--;
10700          break;
10701       }
10702       case classSizeExp:
10703       {
10704          //ComputeExpression(exp);
10705
10706          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10707          if(classSym && classSym.registered)
10708          {
10709             if(classSym.registered.type == noHeadClass)
10710             {
10711                char name[1024];
10712                name[0] = '\0';
10713                DeclareStruct(classSym.string, false);
10714                FreeSpecifier(exp._class);
10715                exp.type = typeSizeExp;
10716                FullClassNameCat(name, classSym.string, false);
10717                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10718             }
10719             else
10720             {
10721                if(classSym.registered.fixed)
10722                {
10723                   FreeSpecifier(exp._class);
10724                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10725                   exp.type = constantExp;
10726                }
10727                else
10728                {
10729                   char className[1024];
10730                   strcpy(className, "__ecereClass_");
10731                   FullClassNameCat(className, classSym.string, true);
10732                   //MangleClassName(className);
10733
10734                   DeclareClass(classSym, className);
10735
10736                   FreeExpContents(exp);
10737                   exp.type = pointerExp;
10738                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10739                   exp.member.member = MkIdentifier("structSize");
10740                }
10741             }
10742          }
10743
10744          exp.expType = Type
10745          {
10746             refCount = 1;
10747             kind = intSizeType;
10748          };
10749          // exp.isConstant = true;
10750          break;
10751       }
10752       case typeSizeExp:
10753       {
10754          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10755
10756          exp.expType = Type
10757          {
10758             refCount = 1;
10759             kind = intSizeType;
10760          };
10761          exp.isConstant = true;
10762
10763          DeclareType(type, false, false);
10764          FreeType(type);
10765          break;
10766       }
10767       case castExp:
10768       {
10769          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10770          type.count = 1;
10771          FreeType(exp.cast.exp.destType);
10772          exp.cast.exp.destType = type;
10773          type.refCount++;
10774          type.casted = true;
10775          ProcessExpressionType(exp.cast.exp);
10776          type.casted = false;
10777          type.count = 0;
10778          exp.expType = type;
10779          //type.refCount++;
10780
10781          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10782          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10783          {
10784             void * prev = exp.prev, * next = exp.next;
10785             Type expType = exp.cast.exp.destType;
10786             Expression castExp = exp.cast.exp;
10787             Type destType = exp.destType;
10788
10789             if(expType) expType.refCount++;
10790
10791             //FreeType(exp.destType);
10792             FreeType(exp.expType);
10793             FreeTypeName(exp.cast.typeName);
10794
10795             *exp = *castExp;
10796             FreeType(exp.expType);
10797             FreeType(exp.destType);
10798
10799             exp.expType = expType;
10800             exp.destType = destType;
10801
10802             delete castExp;
10803
10804             exp.prev = prev;
10805             exp.next = next;
10806
10807          }
10808          else
10809          {
10810             exp.isConstant = exp.cast.exp.isConstant;
10811          }
10812          //FreeType(type);
10813          break;
10814       }
10815       case extensionInitializerExp:
10816       {
10817          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10818          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10819          // ProcessInitializer(exp.initializer.initializer, type);
10820          exp.expType = type;
10821          break;
10822       }
10823       case vaArgExp:
10824       {
10825          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10826          ProcessExpressionType(exp.vaArg.exp);
10827          exp.expType = type;
10828          break;
10829       }
10830       case conditionExp:
10831       {
10832          Expression e;
10833          exp.isConstant = true;
10834
10835          FreeType(exp.cond.cond.destType);
10836          exp.cond.cond.destType = MkClassType("bool");
10837          exp.cond.cond.destType.truth = true;
10838          ProcessExpressionType(exp.cond.cond);
10839          if(!exp.cond.cond.isConstant)
10840             exp.isConstant = false;
10841          for(e = exp.cond.exp->first; e; e = e.next)
10842          {
10843             if(!e.next)
10844             {
10845                FreeType(e.destType);
10846                e.destType = exp.destType;
10847                if(e.destType) e.destType.refCount++;
10848             }
10849             ProcessExpressionType(e);
10850             if(!e.next)
10851             {
10852                exp.expType = e.expType;
10853                if(e.expType) e.expType.refCount++;
10854             }
10855             if(!e.isConstant)
10856                exp.isConstant = false;
10857          }
10858
10859          FreeType(exp.cond.elseExp.destType);
10860          // Added this check if we failed to find an expType
10861          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10862
10863          // Reversed it...
10864          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10865
10866          if(exp.cond.elseExp.destType)
10867             exp.cond.elseExp.destType.refCount++;
10868          ProcessExpressionType(exp.cond.elseExp);
10869
10870          // FIXED THIS: Was done before calling process on elseExp
10871          if(!exp.cond.elseExp.isConstant)
10872             exp.isConstant = false;
10873          break;
10874       }
10875       case extensionCompoundExp:
10876       {
10877          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10878          {
10879             Statement last = exp.compound.compound.statements->last;
10880             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10881             {
10882                ((Expression)last.expressions->last).destType = exp.destType;
10883                if(exp.destType)
10884                   exp.destType.refCount++;
10885             }
10886             ProcessStatement(exp.compound);
10887             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10888             if(exp.expType)
10889                exp.expType.refCount++;
10890          }
10891          break;
10892       }
10893       case classExp:
10894       {
10895          Specifier spec = exp._classExp.specifiers->first;
10896          if(spec && spec.type == nameSpecifier)
10897          {
10898             exp.expType = MkClassType(spec.name);
10899             exp.expType.kind = subClassType;
10900             exp.byReference = true;
10901          }
10902          else
10903          {
10904             exp.expType = MkClassType("ecere::com::Class");
10905             exp.byReference = true;
10906          }
10907          break;
10908       }
10909       case classDataExp:
10910       {
10911          Class _class = thisClass ? thisClass : currentClass;
10912          if(_class)
10913          {
10914             Identifier id = exp.classData.id;
10915             char structName[1024];
10916             Expression classExp;
10917             strcpy(structName, "__ecereClassData_");
10918             FullClassNameCat(structName, _class.fullName, false);
10919             exp.type = pointerExp;
10920             exp.member.member = id;
10921             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10922                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10923             else
10924                classExp = MkExpIdentifier(MkIdentifier("class"));
10925
10926             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10927                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10928                   MkExpBrackets(MkListOne(MkExpOp(
10929                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10930                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10931                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10932                      )));
10933
10934             ProcessExpressionType(exp);
10935             return;
10936          }
10937          break;
10938       }
10939       case arrayExp:
10940       {
10941          Type type = null;
10942          const char * typeString = null;
10943          char typeStringBuf[1024];
10944          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10945             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10946          {
10947             Class templateClass = exp.destType._class.registered;
10948             typeString = templateClass.templateArgs[2].dataTypeString;
10949          }
10950          else if(exp.list)
10951          {
10952             // Guess type from expressions in the array
10953             Expression e;
10954             for(e = exp.list->first; e; e = e.next)
10955             {
10956                ProcessExpressionType(e);
10957                if(e.expType)
10958                {
10959                   if(!type) { type = e.expType; type.refCount++; }
10960                   else
10961                   {
10962                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10963                      if(!MatchTypeExpression(e, type, null, false, true))
10964                      {
10965                         FreeType(type);
10966                         type = e.expType;
10967                         e.expType = null;
10968
10969                         e = exp.list->first;
10970                         ProcessExpressionType(e);
10971                         if(e.expType)
10972                         {
10973                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10974                            if(!MatchTypeExpression(e, type, null, false, true))
10975                            {
10976                               FreeType(e.expType);
10977                               e.expType = null;
10978                               FreeType(type);
10979                               type = null;
10980                               break;
10981                            }
10982                         }
10983                      }
10984                   }
10985                   if(e.expType)
10986                   {
10987                      FreeType(e.expType);
10988                      e.expType = null;
10989                   }
10990                }
10991             }
10992             if(type)
10993             {
10994                typeStringBuf[0] = '\0';
10995                PrintTypeNoConst(type, typeStringBuf, false, true);
10996                typeString = typeStringBuf;
10997                FreeType(type);
10998                type = null;
10999             }
11000          }
11001          if(typeString)
11002          {
11003             /*
11004             (Container)& (struct BuiltInContainer)
11005             {
11006                ._vTbl = class(BuiltInContainer)._vTbl,
11007                ._class = class(BuiltInContainer),
11008                .refCount = 0,
11009                .data = (int[]){ 1, 7, 3, 4, 5 },
11010                .count = 5,
11011                .type = class(int),
11012             }
11013             */
11014             char templateString[1024];
11015             OldList * initializers = MkList();
11016             OldList * structInitializers = MkList();
11017             OldList * specs = MkList();
11018             Expression expExt;
11019             Declarator decl = SpecDeclFromString(typeString, specs, null);
11020             sprintf(templateString, "Container<%s>", typeString);
11021
11022             if(exp.list)
11023             {
11024                Expression e;
11025                type = ProcessTypeString(typeString, false);
11026                while((e = exp.list->first))
11027                {
11028                   exp.list->Remove(e);
11029                   e.destType = type;
11030                   type.refCount++;
11031                   ProcessExpressionType(e);
11032                   ListAdd(initializers, MkInitializerAssignment(e));
11033                }
11034                FreeType(type);
11035                delete exp.list;
11036             }
11037
11038             DeclareStruct("ecere::com::BuiltInContainer", false);
11039
11040             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
11041                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11042             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
11043                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11044             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
11045                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11046             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
11047                MkTypeName(specs, MkDeclaratorArray(decl, null)),
11048                MkInitializerList(initializers))));
11049                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11050             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
11051                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11052             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
11053                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11054             exp.expType = ProcessTypeString(templateString, false);
11055             exp.type = bracketsExp;
11056             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
11057                MkExpOp(null, '&',
11058                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
11059                   MkInitializerList(structInitializers)))));
11060             ProcessExpressionType(expExt);
11061          }
11062          else
11063          {
11064             exp.expType = ProcessTypeString("Container", false);
11065             Compiler_Error($"Couldn't determine type of array elements\n");
11066          }
11067          break;
11068       }
11069    }
11070
11071    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
11072    {
11073       FreeType(exp.expType);
11074       exp.expType = ReplaceThisClassType(thisClass);
11075    }
11076
11077    // Resolve structures here
11078    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
11079    {
11080       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
11081       // TODO: Fix members reference...
11082       if(symbol)
11083       {
11084          if(exp.expType.kind != enumType)
11085          {
11086             Type member;
11087             String enumName = CopyString(exp.expType.enumName);
11088
11089             // Fixed a memory leak on self-referencing C structs typedefs
11090             // by instantiating a new type rather than simply copying members
11091             // into exp.expType
11092             FreeType(exp.expType);
11093             exp.expType = Type { };
11094             exp.expType.kind = symbol.type.kind;
11095             exp.expType.refCount++;
11096             exp.expType.enumName = enumName;
11097
11098             exp.expType.members = symbol.type.members;
11099             for(member = symbol.type.members.first; member; member = member.next)
11100                member.refCount++;
11101          }
11102          else
11103          {
11104             NamedLink64 member;
11105             for(member = symbol.type.members.first; member; member = member.next)
11106             {
11107                NamedLink64 value { name = CopyString(member.name) };
11108                exp.expType.members.Add(value);
11109             }
11110          }
11111       }
11112    }
11113
11114    yylloc = exp.loc;
11115    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11116    else if(exp.destType && !exp.destType.keepCast)
11117    {
11118       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11119          exp.needTemplateCast = 1;
11120
11121       if(exp.destType.kind == voidType);
11122       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11123       {
11124          if(!exp.destType.count || unresolved)
11125          {
11126             if(!exp.expType)
11127             {
11128                yylloc = exp.loc;
11129                if(exp.destType.kind != ellipsisType)
11130                {
11131                   char type2[1024];
11132                   type2[0] = '\0';
11133                   if(inCompiler)
11134                   {
11135                      char expString[10240];
11136                      expString[0] = '\0';
11137
11138                      PrintType(exp.destType, type2, false, true);
11139
11140                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11141                      if(unresolved)
11142                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11143                      else if(exp.type != dummyExp)
11144                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11145                   }
11146                }
11147                else
11148                {
11149                   char expString[10240] ;
11150                   expString[0] = '\0';
11151                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11152
11153                   if(unresolved)
11154                      Compiler_Error($"unresolved identifier %s\n", expString);
11155                   else if(exp.type != dummyExp)
11156                      Compiler_Error($"couldn't determine type of %s\n", expString);
11157                }
11158             }
11159             else
11160             {
11161                char type1[1024];
11162                char type2[1024];
11163                type1[0] = '\0';
11164                type2[0] = '\0';
11165                if(inCompiler)
11166                {
11167                   PrintType(exp.expType, type1, false, true);
11168                   PrintType(exp.destType, type2, false, true);
11169                }
11170
11171                //CheckExpressionType(exp, exp.destType, false);
11172
11173                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11174                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11175                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11176                else
11177                {
11178                   char expString[10240];
11179                   expString[0] = '\0';
11180                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11181
11182 #ifdef _DEBUG
11183                   CheckExpressionType(exp, exp.destType, false, true);
11184 #endif
11185                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11186                   if(!sourceFile || (!strstr(sourceFile, "src\\lexer.ec") && !strstr(sourceFile, "src/lexer.ec") &&
11187                                      !strstr(sourceFile, "src\\grammar.ec") && !strstr(sourceFile, "src/grammar.ec") &&
11188                                      !strstr(sourceFile, "src\\type.ec") && !strstr(sourceFile, "src/type.ec") &&
11189                                      !strstr(sourceFile, "src\\expression.ec") && !strstr(sourceFile, "src/expression.ec")))
11190                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11191
11192                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11193                   FreeType(exp.expType);
11194                   exp.destType.refCount++;
11195                   exp.expType = exp.destType;
11196                }
11197             }
11198          }
11199       }
11200       // Cast function pointers to void * as eC already checked compatibility
11201       else if(exp.destType && exp.destType.kind == pointerType && exp.destType.type && exp.destType.type.kind == functionType &&
11202               exp.expType && (exp.expType.kind == functionType || exp.expType.kind == methodType))
11203       {
11204          Expression nbExp = GetNonBracketsExp(exp);
11205          if(nbExp.type != castExp || !IsVoidPtrCast(nbExp.cast.typeName))
11206          {
11207             Expression e = MoveExpContents(exp);
11208             exp.cast.exp = MkExpBrackets(MkListOne(e));
11209             exp.type = castExp;
11210             exp.cast.exp.destType = exp.destType;
11211             if(exp.destType) exp.destType.refCount++;
11212             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
11213          }
11214       }
11215    }
11216    else if(unresolved)
11217    {
11218       if(exp.identifier._class && exp.identifier._class.name)
11219          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11220       else if(exp.identifier.string && exp.identifier.string[0])
11221          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11222    }
11223    else if(!exp.expType && exp.type != dummyExp)
11224    {
11225       char expString[10240];
11226       expString[0] = '\0';
11227       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11228       Compiler_Error($"couldn't determine type of %s\n", expString);
11229    }
11230
11231    // Let's try to support any_object & typed_object here:
11232    if(inCompiler)
11233       ApplyAnyObjectLogic(exp);
11234
11235    // Mark nohead classes as by reference, unless we're casting them to an integral type
11236    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11237       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11238          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11239           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11240    {
11241       exp.byReference = true;
11242    }
11243    yylloc = oldyylloc;
11244 }
11245
11246 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11247 {
11248    // THIS CODE WILL FIND NEXT MEMBER...
11249    if(*curMember)
11250    {
11251       *curMember = (*curMember).next;
11252
11253       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11254       {
11255          *curMember = subMemberStack[--(*subMemberStackPos)];
11256          *curMember = (*curMember).next;
11257       }
11258
11259       // SKIP ALL PROPERTIES HERE...
11260       while((*curMember) && (*curMember).isProperty)
11261          *curMember = (*curMember).next;
11262
11263       if(subMemberStackPos)
11264       {
11265          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11266          {
11267             subMemberStack[(*subMemberStackPos)++] = *curMember;
11268
11269             *curMember = (*curMember).members.first;
11270             while(*curMember && (*curMember).isProperty)
11271                *curMember = (*curMember).next;
11272          }
11273       }
11274    }
11275    while(!*curMember)
11276    {
11277       if(!*curMember)
11278       {
11279          if(subMemberStackPos && *subMemberStackPos)
11280          {
11281             *curMember = subMemberStack[--(*subMemberStackPos)];
11282             *curMember = (*curMember).next;
11283          }
11284          else
11285          {
11286             Class lastCurClass = *curClass;
11287
11288             if(*curClass == _class) break;     // REACHED THE END
11289
11290             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11291             *curMember = (*curClass).membersAndProperties.first;
11292          }
11293
11294          while((*curMember) && (*curMember).isProperty)
11295             *curMember = (*curMember).next;
11296          if(subMemberStackPos)
11297          {
11298             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11299             {
11300                subMemberStack[(*subMemberStackPos)++] = *curMember;
11301
11302                *curMember = (*curMember).members.first;
11303                while(*curMember && (*curMember).isProperty)
11304                   *curMember = (*curMember).next;
11305             }
11306          }
11307       }
11308    }
11309 }
11310
11311
11312 static void ProcessInitializer(Initializer init, Type type)
11313 {
11314    switch(init.type)
11315    {
11316       case expInitializer:
11317          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11318          {
11319             // TESTING THIS FOR SHUTTING = 0 WARNING
11320             if(init.exp && !init.exp.destType)
11321             {
11322                FreeType(init.exp.destType);
11323                init.exp.destType = type;
11324                if(type) type.refCount++;
11325             }
11326             if(init.exp)
11327             {
11328                ProcessExpressionType(init.exp);
11329                init.isConstant = init.exp.isConstant;
11330             }
11331             break;
11332          }
11333          else
11334          {
11335             Expression exp = init.exp;
11336             Instantiation inst = exp.instance;
11337             MembersInit members;
11338
11339             init.type = listInitializer;
11340             init.list = MkList();
11341
11342             if(inst.members)
11343             {
11344                for(members = inst.members->first; members; members = members.next)
11345                {
11346                   if(members.type == dataMembersInit)
11347                   {
11348                      MemberInit member;
11349                      for(member = members.dataMembers->first; member; member = member.next)
11350                      {
11351                         ListAdd(init.list, member.initializer);
11352                         member.initializer = null;
11353                      }
11354                   }
11355                   // Discard all MembersInitMethod
11356                }
11357             }
11358             FreeExpression(exp);
11359          }
11360       case listInitializer:
11361       {
11362          Initializer i;
11363          Type initializerType = null;
11364          Class curClass = null;
11365          DataMember curMember = null;
11366          DataMember subMemberStack[256];
11367          int subMemberStackPos = 0;
11368
11369          if(type && type.kind == arrayType)
11370             initializerType = Dereference(type);
11371          else if(type && (type.kind == structType || type.kind == unionType))
11372             initializerType = type.members.first;
11373
11374          for(i = init.list->first; i; i = i.next)
11375          {
11376             if(type && type.kind == classType && type._class && type._class.registered)
11377             {
11378                // 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)
11379                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11380                // TODO: Generate error on initializing a private data member this way from another module...
11381                if(curMember)
11382                {
11383                   if(!curMember.dataType)
11384                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11385                   initializerType = curMember.dataType;
11386                }
11387             }
11388             ProcessInitializer(i, initializerType);
11389             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11390                initializerType = initializerType.next;
11391             if(!i.isConstant)
11392                init.isConstant = false;
11393          }
11394
11395          if(type && type.kind == arrayType)
11396             FreeType(initializerType);
11397
11398          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11399          {
11400             Compiler_Error($"Assigning list initializer to non list\n");
11401          }
11402          break;
11403       }
11404    }
11405 }
11406
11407 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11408 {
11409    switch(spec.type)
11410    {
11411       case baseSpecifier:
11412       {
11413          if(spec.specifier == THISCLASS)
11414          {
11415             if(thisClass)
11416             {
11417                spec.type = nameSpecifier;
11418                spec.name = ReplaceThisClass(thisClass);
11419                spec.symbol = FindClass(spec.name);
11420                ProcessSpecifier(spec, declareStruct);
11421             }
11422          }
11423          break;
11424       }
11425       case nameSpecifier:
11426       {
11427          Symbol symbol = FindType(curContext, spec.name);
11428          if(symbol)
11429             DeclareType(symbol.type, true, true);
11430          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11431             DeclareStruct(spec.name, false);
11432          break;
11433       }
11434       case enumSpecifier:
11435       {
11436          Enumerator e;
11437          if(spec.list)
11438          {
11439             for(e = spec.list->first; e; e = e.next)
11440             {
11441                if(e.exp)
11442                   ProcessExpressionType(e.exp);
11443             }
11444          }
11445          break;
11446       }
11447       case structSpecifier:
11448       case unionSpecifier:
11449       {
11450          if(spec.definitions)
11451          {
11452             //ClassDef def;
11453             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11454             //if(symbol)
11455                ProcessClass(spec.definitions, symbol);
11456             /*else
11457             {
11458                for(def = spec.definitions->first; def; def = def.next)
11459                {
11460                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11461                      ProcessDeclaration(def.decl);
11462                }
11463             }*/
11464          }
11465          break;
11466       }
11467       /*
11468       case classSpecifier:
11469       {
11470          Symbol classSym = FindClass(spec.name);
11471          if(classSym && classSym.registered && classSym.registered.type == structClass)
11472             DeclareStruct(spec.name, false);
11473          break;
11474       }
11475       */
11476    }
11477 }
11478
11479
11480 static void ProcessDeclarator(Declarator decl)
11481 {
11482    switch(decl.type)
11483    {
11484       case identifierDeclarator:
11485          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11486          {
11487             FreeSpecifier(decl.identifier._class);
11488             decl.identifier._class = null;
11489          }
11490          break;
11491       case arrayDeclarator:
11492          if(decl.array.exp)
11493             ProcessExpressionType(decl.array.exp);
11494       case structDeclarator:
11495       case bracketsDeclarator:
11496       case functionDeclarator:
11497       case pointerDeclarator:
11498       case extendedDeclarator:
11499       case extendedDeclaratorEnd:
11500          if(decl.declarator)
11501             ProcessDeclarator(decl.declarator);
11502          if(decl.type == functionDeclarator)
11503          {
11504             Identifier id = GetDeclId(decl);
11505             if(id && id._class)
11506             {
11507                TypeName param
11508                {
11509                   qualifiers = MkListOne(id._class);
11510                   declarator = null;
11511                };
11512                if(!decl.function.parameters)
11513                   decl.function.parameters = MkList();
11514                decl.function.parameters->Insert(null, param);
11515                id._class = null;
11516             }
11517             if(decl.function.parameters)
11518             {
11519                TypeName param;
11520
11521                for(param = decl.function.parameters->first; param; param = param.next)
11522                {
11523                   if(param.qualifiers && param.qualifiers->first)
11524                   {
11525                      Specifier spec = param.qualifiers->first;
11526                      if(spec && spec.specifier == TYPED_OBJECT)
11527                      {
11528                         Declarator d = param.declarator;
11529                         TypeName newParam
11530                         {
11531                            qualifiers = MkListOne(MkSpecifier(VOID));
11532                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11533                         };
11534                         if(d.type != pointerDeclarator)
11535                            newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11536
11537                         FreeList(param.qualifiers, FreeSpecifier);
11538
11539                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11540                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11541
11542                         decl.function.parameters->Insert(param, newParam);
11543                         param = newParam;
11544                      }
11545                      else if(spec && spec.specifier == ANY_OBJECT)
11546                      {
11547                         Declarator d = param.declarator;
11548
11549                         FreeList(param.qualifiers, FreeSpecifier);
11550
11551                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11552                         if(d.type != pointerDeclarator)
11553                            param.qualifiers->Insert(null, MkSpecifier(CONST));
11554                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11555                      }
11556                      else if(spec.specifier == THISCLASS)
11557                      {
11558                         if(thisClass)
11559                         {
11560                            spec.type = nameSpecifier;
11561                            spec.name = ReplaceThisClass(thisClass);
11562                            spec.symbol = FindClass(spec.name);
11563                            ProcessSpecifier(spec, false);
11564                         }
11565                      }
11566                   }
11567
11568                   if(param.declarator)
11569                      ProcessDeclarator(param.declarator);
11570                }
11571             }
11572          }
11573          break;
11574    }
11575 }
11576
11577 static void ProcessDeclaration(Declaration decl)
11578 {
11579    yylloc = decl.loc;
11580    switch(decl.type)
11581    {
11582       case initDeclaration:
11583       {
11584          bool declareStruct = false;
11585          /*
11586          lineNum = decl.pos.line;
11587          column = decl.pos.col;
11588          */
11589
11590          if(decl.declarators)
11591          {
11592             InitDeclarator d;
11593
11594             for(d = decl.declarators->first; d; d = d.next)
11595             {
11596                Type type, subType;
11597                ProcessDeclarator(d.declarator);
11598
11599                type = ProcessType(decl.specifiers, d.declarator);
11600
11601                if(d.initializer)
11602                {
11603                   ProcessInitializer(d.initializer, type);
11604
11605                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11606
11607                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11608                      d.initializer.exp.type == instanceExp)
11609                   {
11610                      if(type.kind == classType && type._class ==
11611                         d.initializer.exp.expType._class)
11612                      {
11613                         Instantiation inst = d.initializer.exp.instance;
11614                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11615
11616                         d.initializer.exp.instance = null;
11617                         if(decl.specifiers)
11618                            FreeList(decl.specifiers, FreeSpecifier);
11619                         FreeList(decl.declarators, FreeInitDeclarator);
11620
11621                         d = null;
11622
11623                         decl.type = instDeclaration;
11624                         decl.inst = inst;
11625                      }
11626                   }
11627                }
11628                for(subType = type; subType;)
11629                {
11630                   if(subType.kind == classType)
11631                   {
11632                      declareStruct = true;
11633                      break;
11634                   }
11635                   else if(subType.kind == pointerType)
11636                      break;
11637                   else if(subType.kind == arrayType)
11638                      subType = subType.arrayType;
11639                   else
11640                      break;
11641                }
11642
11643                FreeType(type);
11644                if(!d) break;
11645             }
11646          }
11647
11648          if(decl.specifiers)
11649          {
11650             Specifier s;
11651             for(s = decl.specifiers->first; s; s = s.next)
11652             {
11653                ProcessSpecifier(s, declareStruct);
11654             }
11655          }
11656          break;
11657       }
11658       case instDeclaration:
11659       {
11660          ProcessInstantiationType(decl.inst);
11661          break;
11662       }
11663       case structDeclaration:
11664       {
11665          Specifier spec;
11666          Declarator d;
11667          bool declareStruct = false;
11668
11669          if(decl.declarators)
11670          {
11671             for(d = decl.declarators->first; d; d = d.next)
11672             {
11673                Type type = ProcessType(decl.specifiers, d.declarator);
11674                Type subType;
11675                ProcessDeclarator(d);
11676                for(subType = type; subType;)
11677                {
11678                   if(subType.kind == classType)
11679                   {
11680                      declareStruct = true;
11681                      break;
11682                   }
11683                   else if(subType.kind == pointerType)
11684                      break;
11685                   else if(subType.kind == arrayType)
11686                      subType = subType.arrayType;
11687                   else
11688                      break;
11689                }
11690                FreeType(type);
11691             }
11692          }
11693          if(decl.specifiers)
11694          {
11695             for(spec = decl.specifiers->first; spec; spec = spec.next)
11696                ProcessSpecifier(spec, declareStruct);
11697          }
11698          break;
11699       }
11700    }
11701 }
11702
11703 static FunctionDefinition curFunction;
11704
11705 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11706 {
11707    char propName[1024], propNameM[1024];
11708    char getName[1024], setName[1024];
11709    OldList * args;
11710
11711    DeclareProperty(prop, setName, getName);
11712
11713    // eInstance_FireWatchers(object, prop);
11714    strcpy(propName, "__ecereProp_");
11715    FullClassNameCat(propName, prop._class.fullName, false);
11716    strcat(propName, "_");
11717    // strcat(propName, prop.name);
11718    FullClassNameCat(propName, prop.name, true);
11719    //MangleClassName(propName);
11720
11721    strcpy(propNameM, "__ecerePropM_");
11722    FullClassNameCat(propNameM, prop._class.fullName, false);
11723    strcat(propNameM, "_");
11724    // strcat(propNameM, prop.name);
11725    FullClassNameCat(propNameM, prop.name, true);
11726    //MangleClassName(propNameM);
11727
11728    if(prop.isWatchable)
11729    {
11730       args = MkList();
11731       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11732       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11733       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11734
11735       args = MkList();
11736       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11737       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11738       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11739    }
11740
11741
11742    {
11743       args = MkList();
11744       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11745       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11746       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11747
11748       args = MkList();
11749       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11750       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11751       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11752    }
11753
11754    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11755       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11756       curFunction.propSet.fireWatchersDone = true;
11757 }
11758
11759 static void ProcessStatement(Statement stmt)
11760 {
11761    yylloc = stmt.loc;
11762    /*
11763    lineNum = stmt.pos.line;
11764    column = stmt.pos.col;
11765    */
11766    switch(stmt.type)
11767    {
11768       case labeledStmt:
11769          ProcessStatement(stmt.labeled.stmt);
11770          break;
11771       case caseStmt:
11772          // This expression should be constant...
11773          if(stmt.caseStmt.exp)
11774          {
11775             FreeType(stmt.caseStmt.exp.destType);
11776             stmt.caseStmt.exp.destType = curSwitchType;
11777             if(curSwitchType) curSwitchType.refCount++;
11778             ProcessExpressionType(stmt.caseStmt.exp);
11779             ComputeExpression(stmt.caseStmt.exp);
11780          }
11781          if(stmt.caseStmt.stmt)
11782             ProcessStatement(stmt.caseStmt.stmt);
11783          break;
11784       case compoundStmt:
11785       {
11786          if(stmt.compound.context)
11787          {
11788             Declaration decl;
11789             Statement s;
11790
11791             Statement prevCompound = curCompound;
11792             Context prevContext = curContext;
11793
11794             if(!stmt.compound.isSwitch)
11795                curCompound = stmt;
11796             curContext = stmt.compound.context;
11797
11798             if(stmt.compound.declarations)
11799             {
11800                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11801                   ProcessDeclaration(decl);
11802             }
11803             if(stmt.compound.statements)
11804             {
11805                for(s = stmt.compound.statements->first; s; s = s.next)
11806                   ProcessStatement(s);
11807             }
11808
11809             curContext = prevContext;
11810             curCompound = prevCompound;
11811          }
11812          break;
11813       }
11814       case expressionStmt:
11815       {
11816          Expression exp;
11817          if(stmt.expressions)
11818          {
11819             for(exp = stmt.expressions->first; exp; exp = exp.next)
11820                ProcessExpressionType(exp);
11821          }
11822          break;
11823       }
11824       case ifStmt:
11825       {
11826          Expression exp;
11827
11828          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11829          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11830          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11831          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11832          {
11833             ProcessExpressionType(exp);
11834          }
11835          if(stmt.ifStmt.stmt)
11836             ProcessStatement(stmt.ifStmt.stmt);
11837          if(stmt.ifStmt.elseStmt)
11838             ProcessStatement(stmt.ifStmt.elseStmt);
11839          break;
11840       }
11841       case switchStmt:
11842       {
11843          Type oldSwitchType = curSwitchType;
11844          if(stmt.switchStmt.exp)
11845          {
11846             Expression exp;
11847             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11848             {
11849                if(!exp.next)
11850                {
11851                   /*
11852                   Type destType
11853                   {
11854                      kind = intType;
11855                      refCount = 1;
11856                   };
11857                   e.exp.destType = destType;
11858                   */
11859
11860                   ProcessExpressionType(exp);
11861                }
11862                if(!exp.next)
11863                   curSwitchType = exp.expType;
11864             }
11865          }
11866          ProcessStatement(stmt.switchStmt.stmt);
11867          curSwitchType = oldSwitchType;
11868          break;
11869       }
11870       case whileStmt:
11871       {
11872          if(stmt.whileStmt.exp)
11873          {
11874             Expression exp;
11875
11876             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11877             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11878             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11879             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11880             {
11881                ProcessExpressionType(exp);
11882             }
11883          }
11884          if(stmt.whileStmt.stmt)
11885             ProcessStatement(stmt.whileStmt.stmt);
11886          break;
11887       }
11888       case doWhileStmt:
11889       {
11890          if(stmt.doWhile.exp)
11891          {
11892             Expression exp;
11893
11894             if(stmt.doWhile.exp->last)
11895             {
11896                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11897                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11898                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11899             }
11900             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11901             {
11902                ProcessExpressionType(exp);
11903             }
11904          }
11905          if(stmt.doWhile.stmt)
11906             ProcessStatement(stmt.doWhile.stmt);
11907          break;
11908       }
11909       case forStmt:
11910       {
11911          Expression exp;
11912          if(stmt.forStmt.init)
11913             ProcessStatement(stmt.forStmt.init);
11914
11915          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11916          {
11917             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11918             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11919             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11920          }
11921
11922          if(stmt.forStmt.check)
11923             ProcessStatement(stmt.forStmt.check);
11924          if(stmt.forStmt.increment)
11925          {
11926             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11927                ProcessExpressionType(exp);
11928          }
11929
11930          if(stmt.forStmt.stmt)
11931             ProcessStatement(stmt.forStmt.stmt);
11932          break;
11933       }
11934       case forEachStmt:
11935       {
11936          Identifier id = stmt.forEachStmt.id;
11937          OldList * exp = stmt.forEachStmt.exp;
11938          OldList * filter = stmt.forEachStmt.filter;
11939          Statement block = stmt.forEachStmt.stmt;
11940          char iteratorType[1024];
11941          Type source;
11942          Expression e;
11943          bool isBuiltin = exp && exp->last &&
11944             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11945               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11946          Expression arrayExp;
11947          const char * typeString = null;
11948          int builtinCount = 0;
11949
11950          for(e = exp ? exp->first : null; e; e = e.next)
11951          {
11952             if(!e.next)
11953             {
11954                FreeType(e.destType);
11955                e.destType = ProcessTypeString("Container", false);
11956             }
11957             if(!isBuiltin || e.next)
11958                ProcessExpressionType(e);
11959          }
11960
11961          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11962          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11963             eClass_IsDerived(source._class.registered, containerClass)))
11964          {
11965             Class _class = source ? source._class.registered : null;
11966             Symbol symbol;
11967             Expression expIt = null;
11968             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
11969             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11970             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11971             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11972             stmt.type = compoundStmt;
11973
11974             stmt.compound.context = Context { };
11975             stmt.compound.context.parent = curContext;
11976             curContext = stmt.compound.context;
11977
11978             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11979             {
11980                Class mapClass = eSystem_FindClass(privateModule, "Map");
11981                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11982                isCustomAVLTree = true;
11983                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
11984                   isAVLTree = true;
11985                else */if(eClass_IsDerived(source._class.registered, mapClass))
11986                   isMap = true;
11987             }
11988             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11989             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11990             {
11991                Class listClass = eSystem_FindClass(privateModule, "List");
11992                isLinkList = true;
11993                isList = eClass_IsDerived(source._class.registered, listClass);
11994             }
11995
11996             if(isArray)
11997             {
11998                Declarator decl;
11999                OldList * specs = MkList();
12000                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12001                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12002                stmt.compound.declarations = MkListOne(
12003                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12004                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12005                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
12006                      MkInitializerAssignment(MkExpBrackets(exp))))));
12007             }
12008             else if(isBuiltin)
12009             {
12010                Type type = null;
12011                char typeStringBuf[1024];
12012
12013                // TODO: Merge this code?
12014                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
12015                if(((Expression)exp->last).type == castExp)
12016                {
12017                   TypeName typeName = ((Expression)exp->last).cast.typeName;
12018                   if(typeName)
12019                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
12020                }
12021
12022                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
12023                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
12024                   arrayExp.destType._class.registered.templateArgs)
12025                {
12026                   Class templateClass = arrayExp.destType._class.registered;
12027                   typeString = templateClass.templateArgs[2].dataTypeString;
12028                }
12029                else if(arrayExp.list)
12030                {
12031                   // Guess type from expressions in the array
12032                   Expression e;
12033                   for(e = arrayExp.list->first; e; e = e.next)
12034                   {
12035                      ProcessExpressionType(e);
12036                      if(e.expType)
12037                      {
12038                         if(!type) { type = e.expType; type.refCount++; }
12039                         else
12040                         {
12041                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
12042                            if(!MatchTypeExpression(e, type, null, false, true))
12043                            {
12044                               FreeType(type);
12045                               type = e.expType;
12046                               e.expType = null;
12047
12048                               e = arrayExp.list->first;
12049                               ProcessExpressionType(e);
12050                               if(e.expType)
12051                               {
12052                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
12053                                  if(!MatchTypeExpression(e, type, null, false, true))
12054                                  {
12055                                     FreeType(e.expType);
12056                                     e.expType = null;
12057                                     FreeType(type);
12058                                     type = null;
12059                                     break;
12060                                  }
12061                               }
12062                            }
12063                         }
12064                         if(e.expType)
12065                         {
12066                            FreeType(e.expType);
12067                            e.expType = null;
12068                         }
12069                      }
12070                   }
12071                   if(type)
12072                   {
12073                      typeStringBuf[0] = '\0';
12074                      PrintType(type, typeStringBuf, false, true);
12075                      typeString = typeStringBuf;
12076                      FreeType(type);
12077                   }
12078                }
12079                if(typeString)
12080                {
12081                   OldList * initializers = MkList();
12082                   Declarator decl;
12083                   OldList * specs = MkList();
12084                   if(arrayExp.list)
12085                   {
12086                      Expression e;
12087
12088                      builtinCount = arrayExp.list->count;
12089                      type = ProcessTypeString(typeString, false);
12090                      while((e = arrayExp.list->first))
12091                      {
12092                         arrayExp.list->Remove(e);
12093                         e.destType = type;
12094                         type.refCount++;
12095                         ProcessExpressionType(e);
12096                         ListAdd(initializers, MkInitializerAssignment(e));
12097                      }
12098                      FreeType(type);
12099                      delete arrayExp.list;
12100                   }
12101                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12102                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12103                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12104
12105                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12106                      PlugDeclarator(
12107                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12108                         ), MkInitializerList(initializers)))));
12109                   FreeList(exp, FreeExpression);
12110                }
12111                else
12112                {
12113                   arrayExp.expType = ProcessTypeString("Container", false);
12114                   Compiler_Error($"Couldn't determine type of array elements\n");
12115                }
12116
12117                /*
12118                Declarator decl;
12119                OldList * specs = MkList();
12120
12121                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12122                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12123                stmt.compound.declarations = MkListOne(
12124                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12125                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12126                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12127                      MkInitializerAssignment(MkExpBrackets(exp))))));
12128                */
12129             }
12130             else if(isLinkList && !isList)
12131             {
12132                Declarator decl;
12133                OldList * specs = MkList();
12134                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12135                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12136                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12137                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12138                      MkInitializerAssignment(MkExpBrackets(exp))))));
12139             }
12140             /*else if(isCustomAVLTree)
12141             {
12142                Declarator decl;
12143                OldList * specs = MkList();
12144                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12145                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12146                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12147                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12148                      MkInitializerAssignment(MkExpBrackets(exp))))));
12149             }*/
12150             else if(_class.templateArgs)
12151             {
12152                if(isMap)
12153                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12154                else
12155                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12156
12157                stmt.compound.declarations = MkListOne(
12158                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12159                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12160                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12161             }
12162             symbol = FindSymbol(id.string, curContext, curContext, false, false);
12163
12164             if(block)
12165             {
12166                // Reparent sub-contexts in this statement
12167                switch(block.type)
12168                {
12169                   case compoundStmt:
12170                      if(block.compound.context)
12171                         block.compound.context.parent = stmt.compound.context;
12172                      break;
12173                   case ifStmt:
12174                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12175                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12176                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12177                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12178                      break;
12179                   case switchStmt:
12180                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12181                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12182                      break;
12183                   case whileStmt:
12184                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12185                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12186                      break;
12187                   case doWhileStmt:
12188                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12189                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12190                      break;
12191                   case forStmt:
12192                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12193                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12194                      break;
12195                   case forEachStmt:
12196                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12197                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12198                      break;
12199                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12200                   case labeledStmt:
12201                   case caseStmt
12202                   case expressionStmt:
12203                   case gotoStmt:
12204                   case continueStmt:
12205                   case breakStmt
12206                   case returnStmt:
12207                   case asmStmt:
12208                   case badDeclarationStmt:
12209                   case fireWatchersStmt:
12210                   case stopWatchingStmt:
12211                   case watchStmt:
12212                   */
12213                }
12214             }
12215             if(filter)
12216             {
12217                block = MkIfStmt(filter, block, null);
12218             }
12219             if(isArray)
12220             {
12221                stmt.compound.statements = MkListOne(MkForStmt(
12222                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12223                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12224                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12225                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12226                   block));
12227               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12228               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12229               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12230             }
12231             else if(isBuiltin)
12232             {
12233                char count[128];
12234                //OldList * specs = MkList();
12235                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12236
12237                sprintf(count, "%d", builtinCount);
12238
12239                stmt.compound.statements = MkListOne(MkForStmt(
12240                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12241                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12242                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12243                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12244                   block));
12245
12246                /*
12247                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12248                stmt.compound.statements = MkListOne(MkForStmt(
12249                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12250                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12251                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12252                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12253                   block));
12254               */
12255               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12256               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12257               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12258             }
12259             else if(isLinkList && !isList)
12260             {
12261                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12262                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12263                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12264                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12265                {
12266                   stmt.compound.statements = MkListOne(MkForStmt(
12267                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12268                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12269                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12270                      block));
12271                }
12272                else
12273                {
12274                   OldList * specs = MkList();
12275                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12276                   stmt.compound.statements = MkListOne(MkForStmt(
12277                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12278                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12279                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12280                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12281                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12282                      block));
12283                }
12284                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12285                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12286                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12287             }
12288             /*else if(isCustomAVLTree)
12289             {
12290                stmt.compound.statements = MkListOne(MkForStmt(
12291                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12292                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12293                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12294                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12295                   block));
12296
12297                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12298                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12299                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12300             }*/
12301             else
12302             {
12303                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12304                   MkIdentifier("Next")), null)), block));
12305             }
12306             ProcessExpressionType(expIt);
12307             if(stmt.compound.declarations->first)
12308                ProcessDeclaration(stmt.compound.declarations->first);
12309
12310             if(symbol)
12311                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12312
12313             ProcessStatement(stmt);
12314             curContext = stmt.compound.context.parent;
12315             break;
12316          }
12317          else
12318          {
12319             Compiler_Error($"Expression is not a container\n");
12320          }
12321          break;
12322       }
12323       case gotoStmt:
12324          break;
12325       case continueStmt:
12326          break;
12327       case breakStmt:
12328          break;
12329       case returnStmt:
12330       {
12331          Expression exp;
12332          if(stmt.expressions)
12333          {
12334             for(exp = stmt.expressions->first; exp; exp = exp.next)
12335             {
12336                if(!exp.next)
12337                {
12338                   if(curFunction && !curFunction.type)
12339                      curFunction.type = ProcessType(
12340                         curFunction.specifiers, curFunction.declarator);
12341                   FreeType(exp.destType);
12342                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12343                   if(exp.destType) exp.destType.refCount++;
12344                }
12345                ProcessExpressionType(exp);
12346             }
12347          }
12348          break;
12349       }
12350       case badDeclarationStmt:
12351       {
12352          ProcessDeclaration(stmt.decl);
12353          break;
12354       }
12355       case asmStmt:
12356       {
12357          AsmField field;
12358          if(stmt.asmStmt.inputFields)
12359          {
12360             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12361                if(field.expression)
12362                   ProcessExpressionType(field.expression);
12363          }
12364          if(stmt.asmStmt.outputFields)
12365          {
12366             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12367                if(field.expression)
12368                   ProcessExpressionType(field.expression);
12369          }
12370          if(stmt.asmStmt.clobberedFields)
12371          {
12372             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12373             {
12374                if(field.expression)
12375                   ProcessExpressionType(field.expression);
12376             }
12377          }
12378          break;
12379       }
12380       case watchStmt:
12381       {
12382          PropertyWatch propWatch;
12383          OldList * watches = stmt._watch.watches;
12384          Expression object = stmt._watch.object;
12385          Expression watcher = stmt._watch.watcher;
12386          if(watcher)
12387             ProcessExpressionType(watcher);
12388          if(object)
12389             ProcessExpressionType(object);
12390
12391          if(inCompiler)
12392          {
12393             if(watcher || thisClass)
12394             {
12395                External external = curExternal;
12396                Context context = curContext;
12397
12398                stmt.type = expressionStmt;
12399                stmt.expressions = MkList();
12400
12401                curExternal = external.prev;
12402
12403                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12404                {
12405                   ClassFunction func;
12406                   char watcherName[1024];
12407                   Class watcherClass = watcher ?
12408                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12409                   External createdExternal;
12410
12411                   // Create a declaration above
12412                   External externalDecl = MkExternalDeclaration(null);
12413                   ast->Insert(curExternal.prev, externalDecl);
12414
12415                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12416                   if(propWatch.deleteWatch)
12417                      strcat(watcherName, "_delete");
12418                   else
12419                   {
12420                      Identifier propID;
12421                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12422                      {
12423                         strcat(watcherName, "_");
12424                         strcat(watcherName, propID.string);
12425                      }
12426                   }
12427
12428                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12429                   {
12430                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12431                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12432                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12433                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12434                      ProcessClassFunctionBody(func, propWatch.compound);
12435                      propWatch.compound = null;
12436
12437                      //afterExternal = afterExternal ? afterExternal : curExternal;
12438
12439                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12440                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12441                      // TESTING THIS...
12442                      createdExternal.symbol.idCode = external.symbol.idCode;
12443
12444                      curExternal = createdExternal;
12445                      ProcessFunction(createdExternal.function);
12446
12447
12448                      // Create a declaration above
12449                      {
12450                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12451                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12452                         externalDecl.declaration = decl;
12453                         if(decl.symbol && !decl.symbol.pointerExternal)
12454                            decl.symbol.pointerExternal = externalDecl;
12455                      }
12456
12457                      if(propWatch.deleteWatch)
12458                      {
12459                         OldList * args = MkList();
12460                         ListAdd(args, CopyExpression(object));
12461                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12462                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12463                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12464                      }
12465                      else
12466                      {
12467                         Class _class = object.expType._class.registered;
12468                         Identifier propID;
12469
12470                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12471                         {
12472                            char propName[1024];
12473                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12474                            if(prop)
12475                            {
12476                               char getName[1024], setName[1024];
12477                               OldList * args = MkList();
12478
12479                               DeclareProperty(prop, setName, getName);
12480
12481                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12482                               strcpy(propName, "__ecereProp_");
12483                               FullClassNameCat(propName, prop._class.fullName, false);
12484                               strcat(propName, "_");
12485                               // strcat(propName, prop.name);
12486                               FullClassNameCat(propName, prop.name, true);
12487
12488                               ListAdd(args, CopyExpression(object));
12489                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12490                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12491                               ListAdd(args, MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpIdentifier(MkIdentifier(watcherName))));
12492
12493                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12494                            }
12495                            else
12496                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12497                         }
12498                      }
12499                   }
12500                   else
12501                      Compiler_Error($"Invalid watched object\n");
12502                }
12503
12504                curExternal = external;
12505                curContext = context;
12506
12507                if(watcher)
12508                   FreeExpression(watcher);
12509                if(object)
12510                   FreeExpression(object);
12511                FreeList(watches, FreePropertyWatch);
12512             }
12513             else
12514                Compiler_Error($"No observer specified and not inside a _class\n");
12515          }
12516          else
12517          {
12518             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12519             {
12520                ProcessStatement(propWatch.compound);
12521             }
12522
12523          }
12524          break;
12525       }
12526       case fireWatchersStmt:
12527       {
12528          OldList * watches = stmt._watch.watches;
12529          Expression object = stmt._watch.object;
12530          Class _class;
12531          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12532          // printf("%X\n", watches);
12533          // printf("%X\n", stmt._watch.watches);
12534          if(object)
12535             ProcessExpressionType(object);
12536
12537          if(inCompiler)
12538          {
12539             _class = object ?
12540                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12541
12542             if(_class)
12543             {
12544                Identifier propID;
12545
12546                stmt.type = expressionStmt;
12547                stmt.expressions = MkList();
12548
12549                // Check if we're inside a property set
12550                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12551                {
12552                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12553                }
12554                else if(!watches)
12555                {
12556                   //Compiler_Error($"No property specified and not inside a property set\n");
12557                }
12558                if(watches)
12559                {
12560                   for(propID = watches->first; propID; propID = propID.next)
12561                   {
12562                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12563                      if(prop)
12564                      {
12565                         CreateFireWatcher(prop, object, stmt);
12566                      }
12567                      else
12568                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12569                   }
12570                }
12571                else
12572                {
12573                   // Fire all properties!
12574                   Property prop;
12575                   Class base;
12576                   for(base = _class; base; base = base.base)
12577                   {
12578                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12579                      {
12580                         if(prop.isProperty && prop.isWatchable)
12581                         {
12582                            CreateFireWatcher(prop, object, stmt);
12583                         }
12584                      }
12585                   }
12586                }
12587
12588                if(object)
12589                   FreeExpression(object);
12590                FreeList(watches, FreeIdentifier);
12591             }
12592             else
12593                Compiler_Error($"Invalid object specified and not inside a class\n");
12594          }
12595          break;
12596       }
12597       case stopWatchingStmt:
12598       {
12599          OldList * watches = stmt._watch.watches;
12600          Expression object = stmt._watch.object;
12601          Expression watcher = stmt._watch.watcher;
12602          Class _class;
12603          if(object)
12604             ProcessExpressionType(object);
12605          if(watcher)
12606             ProcessExpressionType(watcher);
12607          if(inCompiler)
12608          {
12609             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12610
12611             if(watcher || thisClass)
12612             {
12613                if(_class)
12614                {
12615                   Identifier propID;
12616
12617                   stmt.type = expressionStmt;
12618                   stmt.expressions = MkList();
12619
12620                   if(!watches)
12621                   {
12622                      OldList * args;
12623                      // eInstance_StopWatching(object, null, watcher);
12624                      args = MkList();
12625                      ListAdd(args, CopyExpression(object));
12626                      ListAdd(args, MkExpConstant("0"));
12627                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12628                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12629                   }
12630                   else
12631                   {
12632                      for(propID = watches->first; propID; propID = propID.next)
12633                      {
12634                         char propName[1024];
12635                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12636                         if(prop)
12637                         {
12638                            char getName[1024], setName[1024];
12639                            OldList * args = MkList();
12640
12641                            DeclareProperty(prop, setName, getName);
12642
12643                            // eInstance_StopWatching(object, prop, watcher);
12644                            strcpy(propName, "__ecereProp_");
12645                            FullClassNameCat(propName, prop._class.fullName, false);
12646                            strcat(propName, "_");
12647                            // strcat(propName, prop.name);
12648                            FullClassNameCat(propName, prop.name, true);
12649                            //MangleClassName(propName);
12650
12651                            ListAdd(args, CopyExpression(object));
12652                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12653                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12654                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12655                         }
12656                         else
12657                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12658                      }
12659                   }
12660
12661                   if(object)
12662                      FreeExpression(object);
12663                   if(watcher)
12664                      FreeExpression(watcher);
12665                   FreeList(watches, FreeIdentifier);
12666                }
12667                else
12668                   Compiler_Error($"Invalid object specified and not inside a class\n");
12669             }
12670             else
12671                Compiler_Error($"No observer specified and not inside a class\n");
12672          }
12673          break;
12674       }
12675    }
12676 }
12677
12678 static void ProcessFunction(FunctionDefinition function)
12679 {
12680    Identifier id = GetDeclId(function.declarator);
12681    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12682    Type type = symbol ? symbol.type : null;
12683    Class oldThisClass = thisClass;
12684    Context oldTopContext = topContext;
12685
12686    yylloc = function.loc;
12687    // Process thisClass
12688
12689    if(type && type.thisClass)
12690    {
12691       Symbol classSym = type.thisClass;
12692       Class _class = type.thisClass.registered;
12693       char className[1024];
12694       char structName[1024];
12695       Declarator funcDecl;
12696       Symbol thisSymbol;
12697
12698       bool typedObject = false;
12699
12700       if(_class && !_class.base)
12701       {
12702          _class = currentClass;
12703          if(_class && !_class.symbol)
12704             _class.symbol = FindClass(_class.fullName);
12705          classSym = _class ? _class.symbol : null;
12706          typedObject = true;
12707       }
12708
12709       thisClass = _class;
12710
12711       if(inCompiler && _class)
12712       {
12713          if(type.kind == functionType)
12714          {
12715             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12716             {
12717                //TypeName param = symbol.type.params.first;
12718                Type param = symbol.type.params.first;
12719                symbol.type.params.Remove(param);
12720                //FreeTypeName(param);
12721                FreeType(param);
12722             }
12723             if(type.classObjectType != classPointer)
12724             {
12725                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12726                symbol.type.staticMethod = true;
12727                symbol.type.thisClass = null;
12728
12729                // HIGH DANGER: VERIFYING THIS...
12730                symbol.type.extraParam = false;
12731             }
12732          }
12733
12734          strcpy(className, "__ecereClass_");
12735          FullClassNameCat(className, _class.fullName, true);
12736
12737          //MangleClassName(className);
12738
12739          structName[0] = 0;
12740          FullClassNameCat(structName, _class.fullName, false);
12741
12742          // [class] this
12743
12744
12745          funcDecl = GetFuncDecl(function.declarator);
12746          if(funcDecl)
12747          {
12748             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12749             {
12750                TypeName param = funcDecl.function.parameters->first;
12751                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12752                {
12753                   funcDecl.function.parameters->Remove(param);
12754                   FreeTypeName(param);
12755                }
12756             }
12757
12758             // DANGER: Watch for this... Check if it's a Conversion?
12759             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12760
12761             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12762             if(!function.propertyNoThis)
12763             {
12764                TypeName thisParam = null;
12765
12766                if(type.classObjectType != classPointer)
12767                {
12768                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12769                   if(!funcDecl.function.parameters)
12770                      funcDecl.function.parameters = MkList();
12771                   funcDecl.function.parameters->Insert(null, thisParam);
12772                }
12773
12774                if(typedObject)
12775                {
12776                   if(type.classObjectType != classPointer)
12777                   {
12778                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12779                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12780                   }
12781
12782                   thisParam = TypeName
12783                   {
12784                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12785                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12786                   };
12787                   funcDecl.function.parameters->Insert(null, thisParam);
12788                }
12789             }
12790          }
12791
12792          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12793          {
12794             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12795             funcDecl = GetFuncDecl(initDecl.declarator);
12796             if(funcDecl)
12797             {
12798                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12799                {
12800                   TypeName param = funcDecl.function.parameters->first;
12801                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12802                   {
12803                      funcDecl.function.parameters->Remove(param);
12804                      FreeTypeName(param);
12805                   }
12806                }
12807
12808                if(type.classObjectType != classPointer)
12809                {
12810                   // DANGER: Watch for this... Check if it's a Conversion?
12811                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12812                   {
12813                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12814
12815                      if(!funcDecl.function.parameters)
12816                         funcDecl.function.parameters = MkList();
12817                      funcDecl.function.parameters->Insert(null, thisParam);
12818                   }
12819                }
12820             }
12821          }
12822       }
12823
12824       // Add this to the context
12825       if(function.body)
12826       {
12827          if(type.classObjectType != classPointer)
12828          {
12829             thisSymbol = Symbol
12830             {
12831                string = CopyString("this");
12832                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12833             };
12834             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12835
12836             if(typedObject && thisSymbol.type)
12837             {
12838                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12839                thisSymbol.type.byReference = type.byReference;
12840                thisSymbol.type.typedByReference = type.byReference;
12841                /*
12842                thisSymbol = Symbol { string = CopyString("class") };
12843                function.body.compound.context.symbols.Add(thisSymbol);
12844                */
12845             }
12846          }
12847       }
12848
12849       // Pointer to class data
12850
12851       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12852       {
12853          DataMember member = null;
12854          {
12855             Class base;
12856             for(base = _class; base && base.type != systemClass; base = base.next)
12857             {
12858                for(member = base.membersAndProperties.first; member; member = member.next)
12859                   if(!member.isProperty)
12860                      break;
12861                if(member)
12862                   break;
12863             }
12864          }
12865          for(member = _class.membersAndProperties.first; member; member = member.next)
12866             if(!member.isProperty)
12867                break;
12868          if(member)
12869          {
12870             char pointerName[1024];
12871
12872             Declaration decl;
12873             Initializer initializer;
12874             Expression exp, bytePtr;
12875
12876             strcpy(pointerName, "__ecerePointer_");
12877             FullClassNameCat(pointerName, _class.fullName, false);
12878             {
12879                char className[1024];
12880                strcpy(className, "__ecereClass_");
12881                FullClassNameCat(className, classSym.string, true);
12882                //MangleClassName(className);
12883
12884                // Testing This
12885                DeclareClass(classSym, className);
12886             }
12887
12888             // ((byte *) this)
12889             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12890
12891             if(_class.fixed)
12892             {
12893                char string[256];
12894                sprintf(string, "%d", _class.offset);
12895                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12896             }
12897             else
12898             {
12899                // ([bytePtr] + [className]->offset)
12900                exp = QBrackets(MkExpOp(bytePtr, '+',
12901                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12902             }
12903
12904             // (this ? [exp] : 0)
12905             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12906             exp.expType = Type
12907             {
12908                refCount = 1;
12909                kind = pointerType;
12910                type = Type { refCount = 1, kind = voidType };
12911             };
12912
12913             if(function.body)
12914             {
12915                yylloc = function.body.loc;
12916                // ([structName] *) [exp]
12917                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12918                initializer = MkInitializerAssignment(
12919                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12920
12921                // [structName] * [pointerName] = [initializer];
12922                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12923
12924                {
12925                   Context prevContext = curContext;
12926                   OldList * list;
12927                   curContext = function.body.compound.context;
12928
12929                   decl = MkDeclaration((list = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null))),
12930                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12931                   list->Insert(null, MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
12932
12933                   curContext = prevContext;
12934                }
12935
12936                // WHY?
12937                decl.symbol = null;
12938
12939                if(!function.body.compound.declarations)
12940                   function.body.compound.declarations = MkList();
12941                function.body.compound.declarations->Insert(null, decl);
12942             }
12943          }
12944       }
12945
12946
12947       // Loop through the function and replace undeclared identifiers
12948       // which are a member of the class (methods, properties or data)
12949       // by "this.[member]"
12950    }
12951    else
12952       thisClass = null;
12953
12954    if(id)
12955    {
12956       FreeSpecifier(id._class);
12957       id._class = null;
12958
12959       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12960       {
12961          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12962          id = GetDeclId(initDecl.declarator);
12963
12964          FreeSpecifier(id._class);
12965          id._class = null;
12966       }
12967    }
12968    if(function.body)
12969       topContext = function.body.compound.context;
12970    {
12971       FunctionDefinition oldFunction = curFunction;
12972       curFunction = function;
12973       if(function.body)
12974          ProcessStatement(function.body);
12975
12976       // If this is a property set and no firewatchers has been done yet, add one here
12977       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12978       {
12979          Statement prevCompound = curCompound;
12980          Context prevContext = curContext;
12981
12982          Statement fireWatchers = MkFireWatchersStmt(null, null);
12983          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12984          ListAdd(function.body.compound.statements, fireWatchers);
12985
12986          curCompound = function.body;
12987          curContext = function.body.compound.context;
12988
12989          ProcessStatement(fireWatchers);
12990
12991          curContext = prevContext;
12992          curCompound = prevCompound;
12993
12994       }
12995
12996       curFunction = oldFunction;
12997    }
12998
12999    if(function.declarator)
13000    {
13001       ProcessDeclarator(function.declarator);
13002    }
13003
13004    topContext = oldTopContext;
13005    thisClass = oldThisClass;
13006 }
13007
13008 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
13009 static void ProcessClass(OldList definitions, Symbol symbol)
13010 {
13011    ClassDef def;
13012    External external = curExternal;
13013    Class regClass = symbol ? symbol.registered : null;
13014
13015    // Process all functions
13016    for(def = definitions.first; def; def = def.next)
13017    {
13018       if(def.type == functionClassDef)
13019       {
13020          if(def.function.declarator)
13021             curExternal = def.function.declarator.symbol.pointerExternal;
13022          else
13023             curExternal = external;
13024
13025          ProcessFunction((FunctionDefinition)def.function);
13026       }
13027       else if(def.type == declarationClassDef)
13028       {
13029          if(def.decl.type == instDeclaration)
13030          {
13031             thisClass = regClass;
13032             ProcessInstantiationType(def.decl.inst);
13033             thisClass = null;
13034          }
13035          // Testing this
13036          else
13037          {
13038             Class backThisClass = thisClass;
13039             if(regClass) thisClass = regClass;
13040             ProcessDeclaration(def.decl);
13041             thisClass = backThisClass;
13042          }
13043       }
13044       else if(def.type == defaultPropertiesClassDef && def.defProperties)
13045       {
13046          MemberInit defProperty;
13047
13048          // Add this to the context
13049          Symbol thisSymbol = Symbol
13050          {
13051             string = CopyString("this");
13052             type = regClass ? MkClassType(regClass.fullName) : null;
13053          };
13054          globalContext.symbols.Add((BTNode)thisSymbol);
13055
13056          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
13057          {
13058             thisClass = regClass;
13059             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
13060             thisClass = null;
13061          }
13062
13063          globalContext.symbols.Remove((BTNode)thisSymbol);
13064          FreeSymbol(thisSymbol);
13065       }
13066       else if(def.type == propertyClassDef && def.propertyDef)
13067       {
13068          PropertyDef prop = def.propertyDef;
13069
13070          // Add this to the context
13071          /*
13072          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
13073          globalContext.symbols.Add(thisSymbol);
13074          */
13075
13076          thisClass = regClass;
13077          if(prop.setStmt)
13078          {
13079             if(regClass)
13080             {
13081                Symbol thisSymbol
13082                {
13083                   string = CopyString("this");
13084                   type = MkClassType(regClass.fullName);
13085                };
13086                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13087             }
13088
13089             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13090             ProcessStatement(prop.setStmt);
13091          }
13092          if(prop.getStmt)
13093          {
13094             if(regClass)
13095             {
13096                Symbol thisSymbol
13097                {
13098                   string = CopyString("this");
13099                   type = MkClassType(regClass.fullName);
13100                };
13101                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13102             }
13103
13104             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13105             ProcessStatement(prop.getStmt);
13106          }
13107          if(prop.issetStmt)
13108          {
13109             if(regClass)
13110             {
13111                Symbol thisSymbol
13112                {
13113                   string = CopyString("this");
13114                   type = MkClassType(regClass.fullName);
13115                };
13116                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13117             }
13118
13119             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13120             ProcessStatement(prop.issetStmt);
13121          }
13122
13123          thisClass = null;
13124
13125          /*
13126          globalContext.symbols.Remove(thisSymbol);
13127          FreeSymbol(thisSymbol);
13128          */
13129       }
13130       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13131       {
13132          PropertyWatch propertyWatch = def.propertyWatch;
13133
13134          thisClass = regClass;
13135          if(propertyWatch.compound)
13136          {
13137             Symbol thisSymbol
13138             {
13139                string = CopyString("this");
13140                type = regClass ? MkClassType(regClass.fullName) : null;
13141             };
13142
13143             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13144
13145             curExternal = null;
13146             ProcessStatement(propertyWatch.compound);
13147          }
13148          thisClass = null;
13149       }
13150    }
13151 }
13152
13153 void DeclareFunctionUtil(const String s)
13154 {
13155    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13156    if(function)
13157    {
13158       char name[1024];
13159       name[0] = 0;
13160       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13161          strcpy(name, "__ecereFunction_");
13162       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13163       DeclareFunction(function, name);
13164    }
13165 }
13166
13167 void ComputeDataTypes()
13168 {
13169    External external;
13170    External temp { };
13171    External after = null;
13172
13173    currentClass = null;
13174
13175    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13176
13177    for(external = ast->first; external; external = external.next)
13178    {
13179       if(external.type == declarationExternal)
13180       {
13181          Declaration decl = external.declaration;
13182          if(decl)
13183          {
13184             OldList * decls = decl.declarators;
13185             if(decls)
13186             {
13187                InitDeclarator initDecl = decls->first;
13188                if(initDecl)
13189                {
13190                   Declarator declarator = initDecl.declarator;
13191                   if(declarator && declarator.type == identifierDeclarator)
13192                   {
13193                      Identifier id = declarator.identifier;
13194                      if(id && id.string)
13195                      {
13196                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
13197                         {
13198                            external.symbol.id = -1001, external.symbol.idCode = -1001;
13199                            after = external;
13200                         }
13201                      }
13202                   }
13203                }
13204             }
13205          }
13206        }
13207    }
13208
13209    {
13210       // Workaround until we have proper toposort for declarations reordering
13211       External e = MkExternalDeclaration(MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Instance"), null)), null));
13212       ast->Insert(after, e);
13213       after = e;
13214    }
13215
13216    temp.symbol = Symbol { id = -1000, idCode = -1000 };
13217    ast->Insert(after, temp);
13218    curExternal = temp;
13219
13220    DeclareFunctionUtil("eSystem_New");
13221    DeclareFunctionUtil("eSystem_New0");
13222    DeclareFunctionUtil("eSystem_Renew");
13223    DeclareFunctionUtil("eSystem_Renew0");
13224    DeclareFunctionUtil("eSystem_Delete");
13225    DeclareFunctionUtil("eClass_GetProperty");
13226    DeclareFunctionUtil("eClass_SetProperty");
13227    DeclareFunctionUtil("eInstance_FireSelfWatchers");
13228    DeclareFunctionUtil("eInstance_SetMethod");
13229    DeclareFunctionUtil("eInstance_IncRef");
13230    DeclareFunctionUtil("eInstance_StopWatching");
13231    DeclareFunctionUtil("eInstance_Watch");
13232    DeclareFunctionUtil("eInstance_FireWatchers");
13233
13234    DeclareStruct("ecere::com::Class", false);
13235    DeclareStruct("ecere::com::Instance", false);
13236    DeclareStruct("ecere::com::Property", false);
13237    DeclareStruct("ecere::com::DataMember", false);
13238    DeclareStruct("ecere::com::Method", false);
13239    DeclareStruct("ecere::com::SerialBuffer", false);
13240    DeclareStruct("ecere::com::ClassTemplateArgument", false);
13241
13242    ast->Remove(temp);
13243
13244    for(external = ast->first; external; external = external.next)
13245    {
13246       afterExternal = curExternal = external;
13247       if(external.type == functionExternal)
13248       {
13249          currentClass = external.function._class;
13250          ProcessFunction(external.function);
13251       }
13252       // There shouldn't be any _class member access here anyways...
13253       else if(external.type == declarationExternal)
13254       {
13255          currentClass = null;
13256          if(external.declaration)
13257             ProcessDeclaration(external.declaration);
13258       }
13259       else if(external.type == classExternal)
13260       {
13261          ClassDefinition _class = external._class;
13262          currentClass = external.symbol.registered;
13263          if(_class.definitions)
13264          {
13265             ProcessClass(_class.definitions, _class.symbol);
13266          }
13267          if(inCompiler)
13268          {
13269             // Free class data...
13270             ast->Remove(external);
13271             delete external;
13272          }
13273       }
13274       else if(external.type == nameSpaceExternal)
13275       {
13276          thisNameSpace = external.id.string;
13277       }
13278    }
13279    currentClass = null;
13280    thisNameSpace = null;
13281    curExternal = null;
13282
13283    delete temp.symbol;
13284    delete temp;
13285 }