compiler/libec (ide): Fixed crash on sentinel property Set
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50       bool backOutputLineNumbers = outputLineNumbers;
51       outputLineNumbers = false;
52
53       if(exp)
54          OutputExpression(exp, f);
55       f.Seek(0, start);
56       count = strlen(string);
57       count += f.Read(string + count, 1, 1023);
58       string[count] = '\0';
59       delete f;
60
61       outputLineNumbers = backOutputLineNumbers;
62    }
63 }
64
65 Type ProcessTemplateParameterType(TemplateParameter param)
66 {
67    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
68    {
69       // TOFIX: Will need to free this Type
70       if(!param.baseType)
71       {
72          if(param.dataTypeString)
73             param.baseType = ProcessTypeString(param.dataTypeString, false);
74          else
75             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
76       }
77       return param.baseType;
78    }
79    return null;
80 }
81
82 bool NeedCast(Type type1, Type type2)
83 {
84    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
85
86    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
87    {
88       return false;
89    }
90
91    if(type1.kind == type2.kind)
92    {
93       switch(type1.kind)
94       {
95          case _BoolType:
96          case charType:
97          case shortType:
98          case intType:
99          case int64Type:
100          case intPtrType:
101          case intSizeType:
102             if(type1.passAsTemplate && !type2.passAsTemplate)
103                return true;
104             return type1.isSigned != type2.isSigned;
105          case classType:
106             return type1._class != type2._class;
107          case pointerType:
108             return (type1.type && type2.type && type1.type.constant != type2.type.constant) || NeedCast(type1.type, type2.type);
109          default:
110             return true; //false; ????
111       }
112    }
113    return true;
114 }
115
116 static void ReplaceClassMembers(Expression exp, Class _class)
117 {
118    if(exp.type == identifierExp && exp.identifier)
119    {
120       Identifier id = exp.identifier;
121       Context ctx;
122       Symbol symbol = null;
123       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
124       {
125          // First, check if the identifier is declared inside the function
126          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
127          {
128             symbol = (Symbol)ctx.symbols.FindString(id.string);
129             if(symbol) break;
130          }
131       }
132
133       // If it is not, check if it is a member of the _class
134       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
135       {
136          Property prop = eClass_FindProperty(_class, id.string, privateModule);
137          Method method = null;
138          DataMember member = null;
139          ClassProperty classProp = null;
140          if(!prop)
141          {
142             method = eClass_FindMethod(_class, id.string, privateModule);
143          }
144          if(!prop && !method)
145             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
146          if(!prop && !method && !member)
147          {
148             classProp = eClass_FindClassProperty(_class, id.string);
149          }
150          if(prop || method || member || classProp)
151          {
152             // Replace by this.[member]
153             exp.type = memberExp;
154             exp.member.member = id;
155             exp.member.memberType = unresolvedMember;
156             exp.member.exp = QMkExpId("this");
157             //exp.member.exp.loc = exp.loc;
158             exp.addedThis = true;
159          }
160          else if(_class && _class.templateParams.first)
161          {
162             Class sClass;
163             for(sClass = _class; sClass; sClass = sClass.base)
164             {
165                if(sClass.templateParams.first)
166                {
167                   ClassTemplateParameter param;
168                   for(param = sClass.templateParams.first; param; param = param.next)
169                   {
170                      if(param.type == expression && !strcmp(param.name, id.string))
171                      {
172                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
173
174                         if(argExp)
175                         {
176                            Declarator decl;
177                            OldList * specs = MkList();
178
179                            FreeIdentifier(exp.member.member);
180
181                            ProcessExpressionType(argExp);
182
183                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
184
185                            exp.expType = ProcessType(specs, decl);
186
187                            // *[expType] *[argExp]
188                            exp.type = bracketsExp;
189                            exp.list = MkListOne(MkExpOp(null, '*',
190                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
191                         }
192                      }
193                   }
194                }
195             }
196          }
197       }
198    }
199 }
200
201 ////////////////////////////////////////////////////////////////////////
202 // PRINTING ////////////////////////////////////////////////////////////
203 ////////////////////////////////////////////////////////////////////////
204
205 public char * PrintInt(int64 result)
206 {
207    char temp[100];
208    if(result > MAXINT)
209       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
210    else
211       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
212    if(result > MAXINT || result < MININT)
213       strcat(temp, "LL");
214    return CopyString(temp);
215 }
216
217 public char * PrintUInt(uint64 result)
218 {
219    char temp[100];
220    if(result > MAXDWORD)
221       sprintf(temp, FORMAT64HEXLL /*"0x%I64X"*/, result);
222    else if(result > MAXINT)
223       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
224    else
225       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char *  PrintInt64(int64 result)
230 {
231    char temp[100];
232    if(result > MAXINT || result < MININT)
233       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
234    else
235       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
236    return CopyString(temp);
237 }
238
239 public char * PrintUInt64(uint64 result)
240 {
241    char temp[100];
242    if(result > MAXDWORD)
243       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
244    else if(result > MAXINT)
245       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
246    else
247       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
248    return CopyString(temp);
249 }
250
251 public char * PrintHexUInt(uint64 result)
252 {
253    char temp[100];
254    if(result > MAXDWORD)
255       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
256    else
257       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
258    if(result > MAXDWORD)
259       strcat(temp, "LL");
260    return CopyString(temp);
261 }
262
263 public char * PrintHexUInt64(uint64 result)
264 {
265    char temp[100];
266    if(result > MAXDWORD)
267       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
268    else
269       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
270    return CopyString(temp);
271 }
272
273 public char * PrintShort(short result)
274 {
275    char temp[100];
276    sprintf(temp, "%d", (unsigned short)result);
277    return CopyString(temp);
278 }
279
280 public char * PrintUShort(unsigned short result)
281 {
282    char temp[100];
283    if(result > 32767)
284       sprintf(temp, "0x%X", (int)result);
285    else
286       sprintf(temp, "%d", (int)result);
287    return CopyString(temp);
288 }
289
290 public char * PrintChar(char result)
291 {
292    char temp[100];
293    if(result > 0 && isprint(result))
294       sprintf(temp, "'%c'", result);
295    else if(result < 0)
296       sprintf(temp, "%d", (int)result);
297    else
298       //sprintf(temp, "%#X", result);
299       sprintf(temp, "0x%X", (unsigned char)result);
300    return CopyString(temp);
301 }
302
303 public char * PrintUChar(unsigned char result)
304 {
305    char temp[100];
306    sprintf(temp, "0x%X", result);
307    return CopyString(temp);
308 }
309
310 public char * PrintFloat(float result)
311 {
312    char temp[350];
313    if(result.isInf)
314    {
315       if(result.signBit)
316          strcpy(temp, "-inf");
317       else
318          strcpy(temp, "inf");
319    }
320    else if(result.isNan)
321    {
322       if(result.signBit)
323          strcpy(temp, "-nan");
324       else
325          strcpy(temp, "nan");
326    }
327    else
328       sprintf(temp, "%.16ff", result);
329    return CopyString(temp);
330 }
331
332 public char * PrintDouble(double result)
333 {
334    char temp[350];
335    if(result.isInf)
336    {
337       if(result.signBit)
338          strcpy(temp, "-inf");
339       else
340          strcpy(temp, "inf");
341    }
342    else if(result.isNan)
343    {
344       if(result.signBit)
345          strcpy(temp, "-nan");
346       else
347          strcpy(temp, "nan");
348    }
349    else
350       sprintf(temp, "%.16f", result);
351    return CopyString(temp);
352 }
353
354 ////////////////////////////////////////////////////////////////////////
355 ////////////////////////////////////////////////////////////////////////
356
357 //public Operand GetOperand(Expression exp);
358
359 #define GETVALUE(name, t) \
360    public bool GetOp##name(Operand op2, t * value2) \
361    {                                                        \
362       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
363       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
364       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
365       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
366       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
367       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
368       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
369       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
370       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
371       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
372       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
373       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
374       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
375       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
376       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
377       else                                                                          \
378          return false;                                                              \
379       return true;                                                                  \
380    } \
381    public bool Get##name(Expression exp, t * value2) \
382    {                                                        \
383       Operand op2 = GetOperand(exp);                        \
384       return GetOp##name(op2, value2); \
385    }
386
387 // To help the debugger currently not preprocessing...
388 #define HELP(x) x
389
390 GETVALUE(Int, HELP(int));
391 GETVALUE(UInt, HELP(unsigned int));
392 GETVALUE(Int64, HELP(int64));
393 GETVALUE(UInt64, HELP(uint64));
394 GETVALUE(IntPtr, HELP(intptr));
395 GETVALUE(UIntPtr, HELP(uintptr));
396 GETVALUE(IntSize, HELP(intsize));
397 GETVALUE(UIntSize, HELP(uintsize));
398 GETVALUE(Short, HELP(short));
399 GETVALUE(UShort, HELP(unsigned short));
400 GETVALUE(Char, HELP(char));
401 GETVALUE(UChar, HELP(unsigned char));
402 GETVALUE(Float, HELP(float));
403 GETVALUE(Double, HELP(double));
404
405 void ComputeExpression(Expression exp);
406
407 void ComputeClassMembers(Class _class, bool isMember)
408 {
409    DataMember member = isMember ? (DataMember) _class : null;
410    Context context = isMember ? null : SetupTemplatesContext(_class);
411    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
412                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
413    {
414       int unionMemberOffset = 0;
415       int bitFields = 0;
416
417       /*
418       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
419          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
420       */
421
422       if(member)
423       {
424          member.memberOffset = 0;
425          if(targetBits < sizeof(void *) * 8)
426             member.structAlignment = 0;
427       }
428       else if(targetBits < sizeof(void *) * 8)
429          _class.structAlignment = 0;
430
431       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
432       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
433          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
434
435       if(!member && _class.destructionWatchOffset)
436          _class.memberOffset += sizeof(OldList);
437
438       // To avoid reentrancy...
439       //_class.structSize = -1;
440
441       {
442          DataMember dataMember;
443          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
444          {
445             if(!dataMember.isProperty)
446             {
447                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
448                {
449                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
450                   /*if(!dataMember.dataType)
451                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
452                      */
453                }
454             }
455          }
456       }
457
458       {
459          DataMember dataMember;
460          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
461          {
462             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
463             {
464                if(!isMember && _class.type == bitClass && dataMember.dataType)
465                {
466                   BitMember bitMember = (BitMember) dataMember;
467                   uint64 mask = 0;
468                   int d;
469
470                   ComputeTypeSize(dataMember.dataType);
471
472                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
473                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
474
475                   _class.memberOffset = bitMember.pos + bitMember.size;
476                   for(d = 0; d<bitMember.size; d++)
477                   {
478                      if(d)
479                         mask <<= 1;
480                      mask |= 1;
481                   }
482                   bitMember.mask = mask << bitMember.pos;
483                }
484                else if(dataMember.type == normalMember && dataMember.dataType)
485                {
486                   int size;
487                   int alignment = 0;
488
489                   // Prevent infinite recursion
490                   if(dataMember.dataType.kind != classType ||
491                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
492                      _class.type != structClass)))
493                      ComputeTypeSize(dataMember.dataType);
494
495                   if(dataMember.dataType.bitFieldCount)
496                   {
497                      bitFields += dataMember.dataType.bitFieldCount;
498                      size = 0;
499                   }
500                   else
501                   {
502                      if(bitFields)
503                      {
504                         int size = (bitFields + 7) / 8;
505
506                         if(isMember)
507                         {
508                            // TESTING THIS PADDING CODE
509                            if(alignment)
510                            {
511                               member.structAlignment = Max(member.structAlignment, alignment);
512
513                               if(member.memberOffset % alignment)
514                                  member.memberOffset += alignment - (member.memberOffset % alignment);
515                            }
516
517                            dataMember.offset = member.memberOffset;
518                            if(member.type == unionMember)
519                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
520                            else
521                            {
522                               member.memberOffset += size;
523                            }
524                         }
525                         else
526                         {
527                            // TESTING THIS PADDING CODE
528                            if(alignment)
529                            {
530                               _class.structAlignment = Max(_class.structAlignment, alignment);
531
532                               if(_class.memberOffset % alignment)
533                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
534                            }
535
536                            dataMember.offset = _class.memberOffset;
537                            _class.memberOffset += size;
538                         }
539                         bitFields = 0;
540                      }
541                      size = dataMember.dataType.size;
542                      alignment = dataMember.dataType.alignment;
543                   }
544
545                   if(isMember)
546                   {
547                      // TESTING THIS PADDING CODE
548                      if(alignment)
549                      {
550                         member.structAlignment = Max(member.structAlignment, alignment);
551
552                         if(member.memberOffset % alignment)
553                            member.memberOffset += alignment - (member.memberOffset % alignment);
554                      }
555
556                      dataMember.offset = member.memberOffset;
557                      if(member.type == unionMember)
558                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
559                      else
560                      {
561                         member.memberOffset += size;
562                      }
563                   }
564                   else
565                   {
566                      // TESTING THIS PADDING CODE
567                      if(alignment)
568                      {
569                         _class.structAlignment = Max(_class.structAlignment, alignment);
570
571                         if(_class.memberOffset % alignment)
572                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
573                      }
574
575                      dataMember.offset = _class.memberOffset;
576                      _class.memberOffset += size;
577                   }
578                }
579                else
580                {
581                   int alignment;
582
583                   ComputeClassMembers((Class)dataMember, true);
584                   alignment = dataMember.structAlignment;
585
586                   if(isMember)
587                   {
588                      if(alignment)
589                      {
590                         if(member.memberOffset % alignment)
591                            member.memberOffset += alignment - (member.memberOffset % alignment);
592
593                         member.structAlignment = Max(member.structAlignment, alignment);
594                      }
595                      dataMember.offset = member.memberOffset;
596                      if(member.type == unionMember)
597                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
598                      else
599                         member.memberOffset += dataMember.memberOffset;
600                   }
601                   else
602                   {
603                      if(alignment)
604                      {
605                         if(_class.memberOffset % alignment)
606                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
607                         _class.structAlignment = Max(_class.structAlignment, alignment);
608                      }
609                      dataMember.offset = _class.memberOffset;
610                      _class.memberOffset += dataMember.memberOffset;
611                   }
612                }
613             }
614          }
615          if(bitFields)
616          {
617             int alignment = 0;
618             int size = (bitFields + 7) / 8;
619
620             if(isMember)
621             {
622                // TESTING THIS PADDING CODE
623                if(alignment)
624                {
625                   member.structAlignment = Max(member.structAlignment, alignment);
626
627                   if(member.memberOffset % alignment)
628                      member.memberOffset += alignment - (member.memberOffset % alignment);
629                }
630
631                if(member.type == unionMember)
632                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
633                else
634                {
635                   member.memberOffset += size;
636                }
637             }
638             else
639             {
640                // TESTING THIS PADDING CODE
641                if(alignment)
642                {
643                   _class.structAlignment = Max(_class.structAlignment, alignment);
644
645                   if(_class.memberOffset % alignment)
646                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
647                }
648                _class.memberOffset += size;
649             }
650             bitFields = 0;
651          }
652       }
653       if(member && member.type == unionMember)
654       {
655          member.memberOffset = unionMemberOffset;
656       }
657
658       if(!isMember)
659       {
660          /*if(_class.type == structClass)
661             _class.size = _class.memberOffset;
662          else
663          */
664
665          if(_class.type != bitClass)
666          {
667             int extra = 0;
668             if(_class.structAlignment)
669             {
670                if(_class.memberOffset % _class.structAlignment)
671                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
672             }
673             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
674             if(!member)
675             {
676                Property prop;
677                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
678                {
679                   if(prop.isProperty && prop.isWatchable)
680                   {
681                      prop.watcherOffset = _class.structSize;
682                      _class.structSize += sizeof(OldList);
683                   }
684                }
685             }
686
687             // Fix Derivatives
688             {
689                OldLink derivative;
690                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
691                {
692                   Class deriv = derivative.data;
693
694                   if(deriv.computeSize)
695                   {
696                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
697                      deriv.offset = /*_class.offset + */_class.structSize;
698                      deriv.memberOffset = 0;
699                      // ----------------------
700
701                      deriv.structSize = deriv.offset;
702
703                      ComputeClassMembers(deriv, false);
704                   }
705                }
706             }
707          }
708       }
709    }
710    if(context)
711       FinishTemplatesContext(context);
712 }
713
714 public void ComputeModuleClasses(Module module)
715 {
716    Class _class;
717    OldLink subModule;
718
719    for(subModule = module.modules.first; subModule; subModule = subModule.next)
720       ComputeModuleClasses(subModule.data);
721    for(_class = module.classes.first; _class; _class = _class.next)
722       ComputeClassMembers(_class, false);
723 }
724
725
726 public int ComputeTypeSize(Type type)
727 {
728    uint size = type ? type.size : 0;
729    if(!size && type && !type.computing)
730    {
731       type.computing = true;
732       switch(type.kind)
733       {
734          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
735          case charType: type.alignment = size = sizeof(char); break;
736          case intType: type.alignment = size = sizeof(int); break;
737          case int64Type: type.alignment = size = sizeof(int64); break;
738          case intPtrType: type.alignment = size = targetBits / 8; break;
739          case intSizeType: type.alignment = size = targetBits / 8; break;
740          case longType: type.alignment = size = sizeof(long); break;
741          case shortType: type.alignment = size = sizeof(short); break;
742          case floatType: type.alignment = size = sizeof(float); break;
743          case doubleType: type.alignment = size = sizeof(double); break;
744          case classType:
745          {
746             Class _class = type._class ? type._class.registered : null;
747
748             if(_class && _class.type == structClass)
749             {
750                // Ensure all members are properly registered
751                ComputeClassMembers(_class, false);
752                type.alignment = _class.structAlignment;
753                size = _class.structSize;
754                if(type.alignment && size % type.alignment)
755                   size += type.alignment - (size % type.alignment);
756
757             }
758             else if(_class && (_class.type == unitClass ||
759                    _class.type == enumClass ||
760                    _class.type == bitClass))
761             {
762                if(!_class.dataType)
763                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
764                size = type.alignment = ComputeTypeSize(_class.dataType);
765             }
766             else
767                size = type.alignment = targetBits / 8; // sizeof(Instance *);
768             break;
769          }
770          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
771          case arrayType:
772             if(type.arraySizeExp)
773             {
774                ProcessExpressionType(type.arraySizeExp);
775                ComputeExpression(type.arraySizeExp);
776                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
777                   type.arraySizeExp.expType.kind != shortType &&
778                   type.arraySizeExp.expType.kind != charType &&
779                   type.arraySizeExp.expType.kind != longType &&
780                   type.arraySizeExp.expType.kind != int64Type &&
781                   type.arraySizeExp.expType.kind != intSizeType &&
782                   type.arraySizeExp.expType.kind != intPtrType &&
783                   type.arraySizeExp.expType.kind != enumType &&
784                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
785                {
786                   Location oldLoc = yylloc;
787                   // bool isConstant = type.arraySizeExp.isConstant;
788                   char expression[10240];
789                   expression[0] = '\0';
790                   type.arraySizeExp.expType = null;
791                   yylloc = type.arraySizeExp.loc;
792                   if(inCompiler)
793                      PrintExpression(type.arraySizeExp, expression);
794                   Compiler_Error($"Array size not constant int (%s)\n", expression);
795                   yylloc = oldLoc;
796                }
797                GetInt(type.arraySizeExp, &type.arraySize);
798             }
799             else if(type.enumClass)
800             {
801                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
802                {
803                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
804                }
805                else
806                   type.arraySize = 0;
807             }
808             else
809             {
810                // Unimplemented auto size
811                type.arraySize = 0;
812             }
813
814             size = ComputeTypeSize(type.type) * type.arraySize;
815             if(type.type)
816                type.alignment = type.type.alignment;
817
818             break;
819          case structType:
820          {
821             if(!type.members.first && type.enumName)
822             {
823                Symbol symbol = FindStruct(curContext, type.enumName);
824                if(symbol && symbol.type)
825                {
826                   ComputeTypeSize(symbol.type);
827                   size = symbol.type.size;
828                }
829             }
830             else
831             {
832                Type member;
833                for(member = type.members.first; member; member = member.next)
834                {
835                   uint addSize = ComputeTypeSize(member);
836
837                   member.offset = size;
838                   if(member.alignment && size % member.alignment)
839                      member.offset += member.alignment - (size % member.alignment);
840                   size = member.offset;
841
842                   type.alignment = Max(type.alignment, member.alignment);
843                   size += addSize;
844                }
845                if(type.alignment && size % type.alignment)
846                   size += type.alignment - (size % type.alignment);
847             }
848             break;
849          }
850          case unionType:
851          {
852             if(!type.members.first && type.enumName)
853             {
854                Symbol symbol = FindStruct(curContext, type.enumName);
855                if(symbol && symbol.type)
856                {
857                   ComputeTypeSize(symbol.type);
858                   size = symbol.type.size;
859                }
860             }
861             else
862             {
863                Type member;
864                for(member = type.members.first; member; member = member.next)
865                {
866                   uint addSize = ComputeTypeSize(member);
867
868                   member.offset = size;
869                   if(member.alignment && size % member.alignment)
870                      member.offset += member.alignment - (size % member.alignment);
871                   size = member.offset;
872
873                   type.alignment = Max(type.alignment, member.alignment);
874                   size = Max(size, addSize);
875                }
876                if(type.alignment && size % type.alignment)
877                   size += type.alignment - (size % type.alignment);
878             }
879             break;
880          }
881          case templateType:
882          {
883             TemplateParameter param = type.templateParameter;
884             Type baseType = ProcessTemplateParameterType(param);
885             if(baseType)
886             {
887                size = ComputeTypeSize(baseType);
888                type.alignment = baseType.alignment;
889             }
890             else
891                type.alignment = size = sizeof(uint64);
892             break;
893          }
894          case enumType:
895          {
896             type.alignment = size = sizeof(enum { test });
897             break;
898          }
899          case thisClassType:
900          {
901             type.alignment = size = targetBits / 8; //sizeof(void *);
902             break;
903          }
904       }
905       type.size = size;
906       type.computing = false;
907    }
908    return size;
909 }
910
911
912 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
913 {
914    // This function is in need of a major review when implementing private members etc.
915    DataMember topMember = isMember ? (DataMember) _class : null;
916    uint totalSize = 0;
917    uint maxSize = 0;
918    int alignment;
919    uint size;
920    DataMember member;
921    int anonID = 1;
922    Context context = isMember ? null : SetupTemplatesContext(_class);
923    if(addedPadding)
924       *addedPadding = false;
925
926    if(!isMember && _class.base)
927    {
928       maxSize = _class.structSize;
929       //if(_class.base.type != systemClass) // Commented out with new Instance _class
930       {
931          // DANGER: Testing this noHeadClass here...
932          if(_class.type == structClass || _class.type == noHeadClass)
933             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
934          else
935          {
936             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
937             if(maxSize > baseSize)
938                maxSize -= baseSize;
939             else
940                maxSize = 0;
941          }
942       }
943    }
944
945    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
946    {
947       if(!member.isProperty)
948       {
949          switch(member.type)
950          {
951             case normalMember:
952             {
953                if(member.dataTypeString)
954                {
955                   OldList * specs = MkList(), * decls = MkList();
956                   Declarator decl;
957
958                   decl = SpecDeclFromString(member.dataTypeString, specs,
959                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
960                   ListAdd(decls, MkStructDeclarator(decl, null));
961                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
962
963                   if(!member.dataType)
964                      member.dataType = ProcessType(specs, decl);
965
966                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
967
968                   {
969                      Type type = ProcessType(specs, decl);
970                      DeclareType(member.dataType, false, false);
971                      FreeType(type);
972                   }
973                   /*
974                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
975                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
976                      DeclareStruct(member.dataType._class.string, false);
977                   */
978
979                   ComputeTypeSize(member.dataType);
980                   size = member.dataType.size;
981                   alignment = member.dataType.alignment;
982
983                   if(alignment)
984                   {
985                      if(totalSize % alignment)
986                         totalSize += alignment - (totalSize % alignment);
987                   }
988                   totalSize += size;
989                }
990                break;
991             }
992             case unionMember:
993             case structMember:
994             {
995                OldList * specs = MkList(), * list = MkList();
996                char id[100];
997                sprintf(id, "__anon%d", anonID++);
998
999                size = 0;
1000                AddMembers(list, (Class)member, true, &size, topClass, null);
1001                ListAdd(specs,
1002                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
1003                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, MkListOne(MkDeclaratorIdentifier(MkIdentifier(id))),null)));
1004                alignment = member.structAlignment;
1005
1006                if(alignment)
1007                {
1008                   if(totalSize % alignment)
1009                      totalSize += alignment - (totalSize % alignment);
1010                }
1011                totalSize += size;
1012                break;
1013             }
1014          }
1015       }
1016    }
1017    if(retSize)
1018    {
1019       if(topMember && topMember.type == unionMember)
1020          *retSize = Max(*retSize, totalSize);
1021       else
1022          *retSize += totalSize;
1023    }
1024    else if(totalSize < maxSize && _class.type != systemClass)
1025    {
1026       int autoPadding = 0;
1027       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
1028          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
1029       if(totalSize + autoPadding < maxSize)
1030       {
1031          char sizeString[50];
1032          sprintf(sizeString, "%d", maxSize - totalSize);
1033          ListAdd(declarations,
1034             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
1035             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1036          if(addedPadding)
1037             *addedPadding = true;
1038       }
1039    }
1040    if(context)
1041       FinishTemplatesContext(context);
1042    return topMember ? topMember.memberID : _class.memberID;
1043 }
1044
1045 static int DeclareMembers(Class _class, bool isMember)
1046 {
1047    DataMember topMember = isMember ? (DataMember) _class : null;
1048    DataMember member;
1049    Context context = isMember ? null : SetupTemplatesContext(_class);
1050
1051    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1052       DeclareMembers(_class.base, false);
1053
1054    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1055    {
1056       if(!member.isProperty)
1057       {
1058          switch(member.type)
1059          {
1060             case normalMember:
1061             {
1062                /*
1063                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1064                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1065                   DeclareStruct(member.dataType._class.string, false);
1066                   */
1067                if(!member.dataType && member.dataTypeString)
1068                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1069                if(member.dataType)
1070                   DeclareType(member.dataType, false, false);
1071                break;
1072             }
1073             case unionMember:
1074             case structMember:
1075             {
1076                DeclareMembers((Class)member, true);
1077                break;
1078             }
1079          }
1080       }
1081    }
1082    if(context)
1083       FinishTemplatesContext(context);
1084
1085    return topMember ? topMember.memberID : _class.memberID;
1086 }
1087
1088 static void IdentifyAnonStructs(OldList/*<ClassDef>*/ *  definitions)
1089 {
1090    ClassDef def;
1091    int anonID = 1;
1092    for(def = definitions->first; def; def = def.next)
1093    {
1094       if(def.type == declarationClassDef)
1095       {
1096          Declaration decl = def.decl;
1097          if(decl && decl.specifiers)
1098          {
1099             Specifier spec;
1100             bool isStruct = false;
1101             for(spec = decl.specifiers->first; spec; spec = spec.next)
1102             {
1103                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1104                {
1105                   if(spec.definitions)
1106                      IdentifyAnonStructs(spec.definitions);
1107                   isStruct = true;
1108                }
1109             }
1110             if(isStruct)
1111             {
1112                Declarator d = null;
1113                if(decl.declarators)
1114                {
1115                   for(d = decl.declarators->first; d; d = d.next)
1116                   {
1117                      Identifier idDecl = GetDeclId(d);
1118                      if(idDecl)
1119                         break;
1120                   }
1121                }
1122                if(!d)
1123                {
1124                   char id[100];
1125                   sprintf(id, "__anon%d", anonID++);
1126                   if(!decl.declarators)
1127                      decl.declarators = MkList();
1128                   ListAdd(decl.declarators, MkDeclaratorIdentifier(MkIdentifier(id)));
1129                }
1130             }
1131          }
1132       }
1133    }
1134 }
1135
1136 void DeclareStruct(const char * name, bool skipNoHead)
1137 {
1138    External external = null;
1139    Symbol classSym = FindClass(name);
1140
1141    if(!inCompiler || !classSym) return;
1142
1143    // We don't need any declaration for bit classes...
1144    if(classSym.registered &&
1145       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1146       return;
1147
1148    /*if(classSym.registered.templateClass)
1149       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1150    */
1151
1152    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1153    {
1154       // Add typedef struct
1155       Declaration decl;
1156       OldList * specifiers, * declarators;
1157       OldList * declarations = null;
1158       char structName[1024];
1159       Specifier spec = null;
1160       external = (classSym.registered && classSym.registered.type == structClass) ?
1161          classSym.pointerExternal : classSym.structExternal;
1162
1163       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1164       // Moved this one up because DeclareClass done later will need it
1165
1166       classSym.declaring++;
1167
1168       if(strchr(classSym.string, '<'))
1169       {
1170          if(classSym.registered.templateClass)
1171          {
1172             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1173             classSym.declaring--;
1174          }
1175          return;
1176       }
1177
1178       //if(!skipNoHead)
1179          DeclareMembers(classSym.registered, false);
1180
1181       structName[0] = 0;
1182       FullClassNameCat(structName, name, false);
1183
1184       if(external && external.declaration && external.declaration.specifiers)
1185       {
1186          for(spec = external.declaration.specifiers->first; spec; spec = spec.next)
1187          {
1188             if(spec.type == structSpecifier || spec.type == unionSpecifier)
1189                break;
1190          }
1191       }
1192
1193       /*if(!external)
1194          external = MkExternalDeclaration(null);*/
1195
1196       if(!skipNoHead && (!spec || !spec.definitions))
1197       {
1198          bool addedPadding = false;
1199          classSym.declaredStructSym = true;
1200
1201          declarations = MkList();
1202
1203          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1204
1205          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1206          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1207
1208          if(!declarations->count || (declarations->count == 1 && addedPadding))
1209          {
1210             FreeList(declarations, FreeClassDef);
1211             declarations = null;
1212          }
1213       }
1214       if(skipNoHead || declarations)
1215       {
1216          if(spec)
1217          {
1218             if(declarations)
1219                spec.definitions = declarations;
1220
1221             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1222             {
1223                // TODO: Fix this
1224                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1225
1226                // DANGER
1227                if(classSym.structExternal)
1228                   ast->Move(classSym.structExternal, curExternal.prev);
1229                ast->Move(classSym.pointerExternal, curExternal.prev);
1230
1231                classSym.id = curExternal.symbol.idCode;
1232                classSym.idCode = curExternal.symbol.idCode;
1233                // external = classSym.pointerExternal;
1234                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1235             }
1236          }
1237          else
1238          {
1239             if(!external)
1240                external = MkExternalDeclaration(null);
1241
1242             specifiers = MkList();
1243             declarators = MkList();
1244             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1245
1246             /*
1247             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1248             ListAdd(declarators, MkInitDeclarator(d, null));
1249             */
1250             external.declaration = decl = MkDeclaration(specifiers, declarators);
1251             if(decl.symbol && !decl.symbol.pointerExternal)
1252                decl.symbol.pointerExternal = external;
1253
1254             // For simple classes, keep the declaration as the external to move around
1255             if(classSym.registered && classSym.registered.type == structClass)
1256             {
1257                char className[1024];
1258                strcpy(className, "__ecereClass_");
1259                FullClassNameCat(className, classSym.string, true);
1260                //MangleClassName(className);
1261
1262                // Testing This
1263                DeclareClass(classSym, className);
1264
1265                external.symbol = classSym;
1266                classSym.pointerExternal = external;
1267                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1268                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1269             }
1270             else
1271             {
1272                char className[1024];
1273                strcpy(className, "__ecereClass_");
1274                FullClassNameCat(className, classSym.string, true);
1275                //MangleClassName(className);
1276
1277                // TOFIX: TESTING THIS...
1278                classSym.structExternal = external;
1279                DeclareClass(classSym, className);
1280                external.symbol = classSym;
1281             }
1282
1283             //if(curExternal)
1284                ast->Insert(curExternal ? curExternal.prev : null, external);
1285          }
1286       }
1287
1288       classSym.declaring--;
1289    }
1290    else
1291    {
1292       if(classSym.structExternal && classSym.structExternal.declaration && classSym.structExternal.declaration.specifiers)
1293       {
1294          Specifier spec;
1295          for(spec = classSym.structExternal.declaration.specifiers->first; spec; spec = spec.next)
1296          {
1297             IdentifyAnonStructs(spec.definitions);
1298          }
1299       }
1300
1301       if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1302       {
1303          // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1304          // Moved this one up because DeclareClass done later will need it
1305
1306          // TESTING THIS:
1307          classSym.declaring++;
1308
1309          //if(!skipNoHead)
1310          {
1311             if(classSym.registered)
1312                DeclareMembers(classSym.registered, false);
1313          }
1314
1315          if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1316          {
1317             // TODO: Fix this
1318             //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1319
1320             // DANGER
1321             if(classSym.structExternal)
1322                ast->Move(classSym.structExternal, curExternal.prev);
1323             ast->Move(classSym.pointerExternal, curExternal.prev);
1324
1325             classSym.id = curExternal.symbol.idCode;
1326             classSym.idCode = curExternal.symbol.idCode;
1327             // external = classSym.pointerExternal;
1328             // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1329          }
1330
1331          classSym.declaring--;
1332       }
1333    }
1334    //return external;
1335 }
1336
1337 void DeclareProperty(Property prop, char * setName, char * getName)
1338 {
1339    Symbol symbol = prop.symbol;
1340
1341    strcpy(setName, "__ecereProp_");
1342    FullClassNameCat(setName, prop._class.fullName, false);
1343    strcat(setName, "_Set_");
1344    // strcat(setName, prop.name);
1345    FullClassNameCat(setName, prop.name, true);
1346    //MangleClassName(setName);
1347
1348    strcpy(getName, "__ecereProp_");
1349    FullClassNameCat(getName, prop._class.fullName, false);
1350    strcat(getName, "_Get_");
1351    FullClassNameCat(getName, prop.name, true);
1352    // strcat(getName, prop.name);
1353
1354    // To support "char *" property
1355    //MangleClassName(getName);
1356
1357    if(prop._class.type == structClass)
1358       DeclareStruct(prop._class.fullName, false);
1359
1360    if(!symbol || curExternal.symbol.idCode < symbol.id)
1361    {
1362       bool imported = false;
1363       bool dllImport = false;
1364
1365       if(!symbol || symbol._import)
1366       {
1367          if(!symbol)
1368          {
1369             Symbol classSym;
1370             if(!prop._class.symbol)
1371                prop._class.symbol = FindClass(prop._class.fullName);
1372             classSym = prop._class.symbol;
1373             if(classSym && !classSym._import)
1374             {
1375                ModuleImport module;
1376
1377                if(prop._class.module)
1378                   module = FindModule(prop._class.module);
1379                else
1380                   module = mainModule;
1381
1382                classSym._import = ClassImport
1383                {
1384                   name = CopyString(prop._class.fullName);
1385                   isRemote = prop._class.isRemote;
1386                };
1387                module.classes.Add(classSym._import);
1388             }
1389             symbol = prop.symbol = Symbol { };
1390             symbol._import = (ClassImport)PropertyImport
1391             {
1392                name = CopyString(prop.name);
1393                isVirtual = false; //prop.isVirtual;
1394                hasSet = prop.Set ? true : false;
1395                hasGet = prop.Get ? true : false;
1396             };
1397             if(classSym)
1398                classSym._import.properties.Add(symbol._import);
1399          }
1400          imported = true;
1401          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1402          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1403             prop._class.module.importType != staticImport)
1404             dllImport = true;
1405       }
1406
1407       if(!symbol.type)
1408       {
1409          Context context = SetupTemplatesContext(prop._class);
1410          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1411          FinishTemplatesContext(context);
1412       }
1413
1414       // Get
1415       if(prop.Get)
1416       {
1417          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1418          {
1419             Declaration decl;
1420             OldList * specifiers, * declarators;
1421             Declarator d;
1422             OldList * params;
1423             Specifier spec;
1424             External external;
1425             Declarator typeDecl;
1426             bool simple = false;
1427
1428             specifiers = MkList();
1429             declarators = MkList();
1430             params = MkList();
1431
1432             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1433                MkDeclaratorIdentifier(MkIdentifier("this"))));
1434
1435             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1436             //if(imported)
1437             if(dllImport)
1438                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1439
1440             {
1441                Context context = SetupTemplatesContext(prop._class);
1442                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1443                FinishTemplatesContext(context);
1444             }
1445
1446             // Make sure the simple _class's type is declared
1447             for(spec = specifiers->first; spec; spec = spec.next)
1448             {
1449                if(spec.type == nameSpecifier /*SpecifierClass*/)
1450                {
1451                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1452                   {
1453                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1454                      symbol._class = classSym.registered;
1455                      if(classSym.registered && classSym.registered.type == structClass)
1456                      {
1457                         DeclareStruct(spec.name, false);
1458                         simple = true;
1459                      }
1460                   }
1461                }
1462             }
1463
1464             if(!simple)
1465                d = PlugDeclarator(typeDecl, d);
1466             else
1467             {
1468                ListAdd(params, MkTypeName(specifiers,
1469                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1470                specifiers = MkList();
1471             }
1472
1473             d = MkDeclaratorFunction(d, params);
1474
1475             //if(imported)
1476             if(dllImport)
1477                specifiers->Insert(null, MkSpecifier(EXTERN));
1478             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1479                specifiers->Insert(null, MkSpecifier(STATIC));
1480             if(simple)
1481                ListAdd(specifiers, MkSpecifier(VOID));
1482
1483             ListAdd(declarators, MkInitDeclarator(d, null));
1484
1485             decl = MkDeclaration(specifiers, declarators);
1486
1487             external = MkExternalDeclaration(decl);
1488             ast->Insert(curExternal.prev, external);
1489             external.symbol = symbol;
1490             symbol.externalGet = external;
1491
1492             ReplaceThisClassSpecifiers(specifiers, prop._class);
1493
1494             if(typeDecl)
1495                FreeDeclarator(typeDecl);
1496          }
1497          else
1498          {
1499             // Move declaration higher...
1500             ast->Move(symbol.externalGet, curExternal.prev);
1501          }
1502       }
1503
1504       // Set
1505       if(prop.Set)
1506       {
1507          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1508          {
1509             Declaration decl;
1510             OldList * specifiers, * declarators;
1511             Declarator d;
1512             OldList * params;
1513             Specifier spec;
1514             External external;
1515             Declarator typeDecl;
1516
1517             declarators = MkList();
1518             params = MkList();
1519
1520             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1521             if(!prop.conversion || prop._class.type == structClass)
1522             {
1523                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1524                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1525             }
1526
1527             specifiers = MkList();
1528
1529             {
1530                Context context = SetupTemplatesContext(prop._class);
1531                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1532                   MkDeclaratorIdentifier(MkIdentifier("value")));
1533                FinishTemplatesContext(context);
1534             }
1535             if(!strcmp(prop._class.base.fullName, "eda::Row") || !strcmp(prop._class.base.fullName, "eda::Id"))
1536                specifiers->Insert(null, MkSpecifier(CONST));
1537
1538             ListAdd(params, MkTypeName(specifiers, d));
1539
1540             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1541             //if(imported)
1542             if(dllImport)
1543                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1544             d = MkDeclaratorFunction(d, params);
1545
1546             // Make sure the simple _class's type is declared
1547             for(spec = specifiers->first; spec; spec = spec.next)
1548             {
1549                if(spec.type == nameSpecifier /*SpecifierClass*/)
1550                {
1551                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1552                   {
1553                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1554                      symbol._class = classSym.registered;
1555                      if(classSym.registered && classSym.registered.type == structClass)
1556                         DeclareStruct(spec.name, false);
1557                   }
1558                }
1559             }
1560
1561             ListAdd(declarators, MkInitDeclarator(d, null));
1562
1563             specifiers = MkList();
1564             //if(imported)
1565             if(dllImport)
1566                specifiers->Insert(null, MkSpecifier(EXTERN));
1567             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1568                specifiers->Insert(null, MkSpecifier(STATIC));
1569
1570             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1571             if(!prop.conversion || prop._class.type == structClass)
1572                ListAdd(specifiers, MkSpecifier(VOID));
1573             else
1574                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1575
1576             decl = MkDeclaration(specifiers, declarators);
1577
1578             external = MkExternalDeclaration(decl);
1579             ast->Insert(curExternal.prev, external);
1580             external.symbol = symbol;
1581             symbol.externalSet = external;
1582
1583             ReplaceThisClassSpecifiers(specifiers, prop._class);
1584          }
1585          else
1586          {
1587             // Move declaration higher...
1588             ast->Move(symbol.externalSet, curExternal.prev);
1589          }
1590       }
1591
1592       // Property (for Watchers)
1593       if(!symbol.externalPtr)
1594       {
1595          Declaration decl;
1596          External external;
1597          OldList * specifiers = MkList();
1598          char propName[1024];
1599
1600          if(imported)
1601             specifiers->Insert(null, MkSpecifier(EXTERN));
1602          else
1603          {
1604             specifiers->Insert(null, MkSpecifier(STATIC));
1605             specifiers->Add(MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
1606          }
1607
1608          ListAdd(specifiers, MkSpecifierName("Property"));
1609
1610          strcpy(propName, "__ecereProp_");
1611          FullClassNameCat(propName, prop._class.fullName, false);
1612          strcat(propName, "_");
1613          FullClassNameCat(propName, prop.name, true);
1614          // strcat(propName, prop.name);
1615          //MangleClassName(propName);
1616
1617          {
1618             OldList * list = MkList();
1619             ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1620
1621             if(!imported)
1622             {
1623                strcpy(propName, "__ecerePropM_");
1624                FullClassNameCat(propName, prop._class.fullName, false);
1625                strcat(propName, "_");
1626                // strcat(propName, prop.name);
1627                FullClassNameCat(propName, prop.name, true);
1628
1629                //MangleClassName(propName);
1630
1631                ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1632             }
1633             decl = MkDeclaration(specifiers, list);
1634          }
1635
1636          external = MkExternalDeclaration(decl);
1637          ast->Insert(curExternal.prev, external);
1638          external.symbol = symbol;
1639          symbol.externalPtr = external;
1640       }
1641       else
1642       {
1643          // Move declaration higher...
1644          ast->Move(symbol.externalPtr, curExternal.prev);
1645       }
1646
1647       symbol.id = curExternal.symbol.idCode;
1648    }
1649 }
1650
1651 // ***************** EXPRESSION PROCESSING ***************************
1652 public Type Dereference(Type source)
1653 {
1654    Type type = null;
1655    if(source)
1656    {
1657       if(source.kind == pointerType || source.kind == arrayType)
1658       {
1659          type = source.type;
1660          source.type.refCount++;
1661       }
1662       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1663       {
1664          type = Type
1665          {
1666             kind = charType;
1667             refCount = 1;
1668          };
1669       }
1670       // Support dereferencing of no head classes for now...
1671       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1672       {
1673          type = source;
1674          source.refCount++;
1675       }
1676       else
1677          Compiler_Error($"cannot dereference type\n");
1678    }
1679    return type;
1680 }
1681
1682 static Type Reference(Type source)
1683 {
1684    Type type = null;
1685    if(source)
1686    {
1687       type = Type
1688       {
1689          kind = pointerType;
1690          type = source;
1691          refCount = 1;
1692       };
1693       source.refCount++;
1694    }
1695    return type;
1696 }
1697
1698 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1699 {
1700    Identifier ident = member.identifiers ? member.identifiers->first : null;
1701    bool found = false;
1702    DataMember dataMember = null;
1703    Method method = null;
1704    bool freeType = false;
1705
1706    yylloc = member.loc;
1707
1708    if(!ident)
1709    {
1710       if(curMember)
1711       {
1712          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1713          if(*curMember)
1714          {
1715             found = true;
1716             dataMember = *curMember;
1717          }
1718       }
1719    }
1720    else
1721    {
1722       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1723       DataMember _subMemberStack[256];
1724       int _subMemberStackPos = 0;
1725
1726       // FILL MEMBER STACK
1727       if(!thisMember)
1728          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1729       if(thisMember)
1730       {
1731          dataMember = thisMember;
1732          if(curMember && thisMember.memberAccess == publicAccess)
1733          {
1734             *curMember = thisMember;
1735             *curClass = thisMember._class;
1736             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1737             *subMemberStackPos = _subMemberStackPos;
1738          }
1739          found = true;
1740       }
1741       else
1742       {
1743          // Setting a method
1744          method = eClass_FindMethod(_class, ident.string, privateModule);
1745          if(method && method.type == virtualMethod)
1746             found = true;
1747          else
1748             method = null;
1749       }
1750    }
1751
1752    if(found)
1753    {
1754       Type type = null;
1755       if(dataMember)
1756       {
1757          if(!dataMember.dataType && dataMember.dataTypeString)
1758          {
1759             //Context context = SetupTemplatesContext(dataMember._class);
1760             Context context = SetupTemplatesContext(_class);
1761             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1762             FinishTemplatesContext(context);
1763          }
1764          type = dataMember.dataType;
1765       }
1766       else if(method)
1767       {
1768          // This is for destination type...
1769          if(!method.dataType)
1770             ProcessMethodType(method);
1771          //DeclareMethod(method);
1772          // method.dataType = ((Symbol)method.symbol)->type;
1773          type = method.dataType;
1774       }
1775
1776       if(ident && ident.next)
1777       {
1778          for(ident = ident.next; ident && type; ident = ident.next)
1779          {
1780             if(type.kind == classType)
1781             {
1782                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1783                if(!dataMember)
1784                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1785                if(dataMember)
1786                   type = dataMember.dataType;
1787             }
1788             else if(type.kind == structType || type.kind == unionType)
1789             {
1790                Type memberType;
1791                for(memberType = type.members.first; memberType; memberType = memberType.next)
1792                {
1793                   if(!strcmp(memberType.name, ident.string))
1794                   {
1795                      type = memberType;
1796                      break;
1797                   }
1798                }
1799             }
1800          }
1801       }
1802
1803       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1804       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1805       {
1806          int id = 0;
1807          ClassTemplateParameter curParam = null;
1808          Class sClass;
1809          for(sClass = _class; sClass; sClass = sClass.base)
1810          {
1811             id = 0;
1812             if(sClass.templateClass) sClass = sClass.templateClass;
1813             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1814             {
1815                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1816                {
1817                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1818                   {
1819                      if(sClass.templateClass) sClass = sClass.templateClass;
1820                      id += sClass.templateParams.count;
1821                   }
1822                   break;
1823                }
1824                id++;
1825             }
1826             if(curParam) break;
1827          }
1828
1829          if(curParam)
1830          {
1831             ClassTemplateArgument arg = _class.templateArgs[id];
1832             if(arg.dataTypeString)
1833             {
1834                bool constant = type.constant;
1835                // FreeType(type);
1836                type = ProcessTypeString(arg.dataTypeString, false);
1837                if(type.kind == classType && constant) type.constant = true;
1838                else if(type.kind == pointerType)
1839                {
1840                   Type t = type.type;
1841                   while(t.kind == pointerType) t = t.type;
1842                   if(constant) t.constant = constant;
1843                }
1844                freeType = true;
1845                if(type && _class.templateClass)
1846                   type.passAsTemplate = true;
1847                if(type)
1848                {
1849                   // type.refCount++;
1850                   /*if(!exp.destType)
1851                   {
1852                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1853                      exp.destType.refCount++;
1854                   }*/
1855                }
1856             }
1857          }
1858       }
1859       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1860       {
1861          Class expClass = type._class.registered;
1862          Class cClass = null;
1863          int paramCount = 0;
1864          int lastParam = -1;
1865
1866          char templateString[1024];
1867          ClassTemplateParameter param;
1868          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1869          for(cClass = expClass; cClass; cClass = cClass.base)
1870          {
1871             int p = 0;
1872             if(cClass.templateClass) cClass = cClass.templateClass;
1873             for(param = cClass.templateParams.first; param; param = param.next)
1874             {
1875                int id = p;
1876                Class sClass;
1877                ClassTemplateArgument arg;
1878                for(sClass = cClass.base; sClass; sClass = sClass.base)
1879                {
1880                   if(sClass.templateClass) sClass = sClass.templateClass;
1881                   id += sClass.templateParams.count;
1882                }
1883                arg = expClass.templateArgs[id];
1884
1885                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1886                {
1887                   ClassTemplateParameter cParam;
1888                   //int p = numParams - sClass.templateParams.count;
1889                   int p = 0;
1890                   Class nextClass;
1891                   if(sClass.templateClass) sClass = sClass.templateClass;
1892
1893                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1894                   {
1895                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1896                      p += nextClass.templateParams.count;
1897                   }
1898
1899                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1900                   {
1901                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1902                      {
1903                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1904                         {
1905                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1906                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1907                            break;
1908                         }
1909                      }
1910                   }
1911                }
1912
1913                {
1914                   char argument[256];
1915                   argument[0] = '\0';
1916                   /*if(arg.name)
1917                   {
1918                      strcat(argument, arg.name.string);
1919                      strcat(argument, " = ");
1920                   }*/
1921                   switch(param.type)
1922                   {
1923                      case expression:
1924                      {
1925                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1926                         char expString[1024];
1927                         OldList * specs = MkList();
1928                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1929                         Expression exp;
1930                         char * string = PrintHexUInt64(arg.expression.ui64);
1931                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1932                         delete string;
1933
1934                         ProcessExpressionType(exp);
1935                         ComputeExpression(exp);
1936                         expString[0] = '\0';
1937                         PrintExpression(exp, expString);
1938                         strcat(argument, expString);
1939                         //delete exp;
1940                         FreeExpression(exp);
1941                         break;
1942                      }
1943                      case identifier:
1944                      {
1945                         strcat(argument, arg.member.name);
1946                         break;
1947                      }
1948                      case TemplateParameterType::type:
1949                      {
1950                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1951                            strcat(argument, arg.dataTypeString);
1952                         break;
1953                      }
1954                   }
1955                   if(argument[0])
1956                   {
1957                      if(paramCount) strcat(templateString, ", ");
1958                      if(lastParam != p - 1)
1959                      {
1960                         strcat(templateString, param.name);
1961                         strcat(templateString, " = ");
1962                      }
1963                      strcat(templateString, argument);
1964                      paramCount++;
1965                      lastParam = p;
1966                   }
1967                   p++;
1968                }
1969             }
1970          }
1971          {
1972             int len = strlen(templateString);
1973             if(templateString[len-1] == '<')
1974                len--;
1975             else
1976             {
1977                if(templateString[len-1] == '>')
1978                   templateString[len++] = ' ';
1979                templateString[len++] = '>';
1980             }
1981             templateString[len++] = '\0';
1982          }
1983          {
1984             Context context = SetupTemplatesContext(_class);
1985             if(freeType) FreeType(type);
1986             type = ProcessTypeString(templateString, false);
1987             freeType = true;
1988             FinishTemplatesContext(context);
1989          }
1990       }
1991
1992       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1993       {
1994          ProcessExpressionType(member.initializer.exp);
1995          if(!member.initializer.exp.expType)
1996          {
1997             if(inCompiler)
1998             {
1999                char expString[10240];
2000                expString[0] = '\0';
2001                PrintExpression(member.initializer.exp, expString);
2002                ChangeCh(expString, '\n', ' ');
2003                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
2004             }
2005          }
2006          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
2007          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false, true))
2008          {
2009             Compiler_Error($"incompatible instance method %s\n", ident.string);
2010          }
2011       }
2012       else if(member.initializer)
2013       {
2014          /*
2015          FreeType(member.exp.destType);
2016          member.exp.destType = type;
2017          if(member.exp.destType)
2018             member.exp.destType.refCount++;
2019          ProcessExpressionType(member.exp);
2020          */
2021
2022          ProcessInitializer(member.initializer, type);
2023       }
2024       if(freeType) FreeType(type);
2025    }
2026    else
2027    {
2028       if(_class && _class.type == unitClass)
2029       {
2030          if(member.initializer)
2031          {
2032             /*
2033             FreeType(member.exp.destType);
2034             member.exp.destType = MkClassType(_class.fullName);
2035             ProcessExpressionType(member.initializer, type);
2036             */
2037             Type type = MkClassType(_class.fullName);
2038             ProcessInitializer(member.initializer, type);
2039             FreeType(type);
2040          }
2041       }
2042       else
2043       {
2044          if(member.initializer)
2045          {
2046             //ProcessExpressionType(member.exp);
2047             ProcessInitializer(member.initializer, null);
2048          }
2049          if(ident)
2050          {
2051             if(method)
2052             {
2053                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
2054             }
2055             else if(_class)
2056             {
2057                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
2058                if(inCompiler)
2059                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
2060             }
2061          }
2062          else if(_class)
2063             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
2064       }
2065    }
2066 }
2067
2068 void ProcessInstantiationType(Instantiation inst)
2069 {
2070    yylloc = inst.loc;
2071    if(inst._class)
2072    {
2073       MembersInit members;
2074       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
2075       Class _class;
2076
2077       /*if(!inst._class.symbol)
2078          inst._class.symbol = FindClass(inst._class.name);*/
2079       classSym = inst._class.symbol;
2080       _class = classSym ? classSym.registered : null;
2081
2082       // DANGER: Patch for mutex not declaring its struct when not needed
2083       if(!_class || _class.type != noHeadClass)
2084          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
2085
2086       afterExternal = afterExternal ? afterExternal : curExternal;
2087
2088       if(inst.exp)
2089          ProcessExpressionType(inst.exp);
2090
2091       inst.isConstant = true;
2092       if(inst.members)
2093       {
2094          DataMember curMember = null;
2095          Class curClass = null;
2096          DataMember subMemberStack[256];
2097          int subMemberStackPos = 0;
2098
2099          for(members = inst.members->first; members; members = members.next)
2100          {
2101             switch(members.type)
2102             {
2103                case methodMembersInit:
2104                {
2105                   char name[1024];
2106                   static uint instMethodID = 0;
2107                   External external = curExternal;
2108                   Context context = curContext;
2109                   Declarator declarator = members.function.declarator;
2110                   Identifier nameID = GetDeclId(declarator);
2111                   char * unmangled = nameID ? nameID.string : null;
2112                   Expression exp;
2113                   External createdExternal = null;
2114
2115                   if(inCompiler)
2116                   {
2117                      char number[16];
2118                      //members.function.dontMangle = true;
2119                      strcpy(name, "__ecereInstMeth_");
2120                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2121                      strcat(name, "_");
2122                      strcat(name, nameID.string);
2123                      strcat(name, "_");
2124                      sprintf(number, "_%08d", instMethodID++);
2125                      strcat(name, number);
2126                      nameID.string = CopyString(name);
2127                   }
2128
2129                   // Do modifications here...
2130                   if(declarator)
2131                   {
2132                      Symbol symbol = declarator.symbol;
2133                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2134
2135                      if(method && method.type == virtualMethod)
2136                      {
2137                         symbol.method = method;
2138                         ProcessMethodType(method);
2139
2140                         if(!symbol.type.thisClass)
2141                         {
2142                            if(method.dataType.thisClass && currentClass &&
2143                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2144                            {
2145                               if(!currentClass.symbol)
2146                                  currentClass.symbol = FindClass(currentClass.fullName);
2147                               symbol.type.thisClass = currentClass.symbol;
2148                            }
2149                            else
2150                            {
2151                               if(!_class.symbol)
2152                                  _class.symbol = FindClass(_class.fullName);
2153                               symbol.type.thisClass = _class.symbol;
2154                            }
2155                         }
2156                         // TESTING THIS HERE:
2157                         DeclareType(symbol.type, true, true);
2158
2159                      }
2160                      else if(classSym)
2161                      {
2162                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2163                            unmangled, classSym.string);
2164                      }
2165                   }
2166
2167                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2168                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2169
2170                   if(nameID)
2171                   {
2172                      FreeSpecifier(nameID._class);
2173                      nameID._class = null;
2174                   }
2175
2176                   if(inCompiler)
2177                   {
2178                      //Type type = declarator.symbol.type;
2179                      External oldExternal = curExternal;
2180
2181                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2182                      // *** It was commented out for problems such as
2183                      /*
2184                            class VirtualDesktop : Window
2185                            {
2186                               clientSize = Size { };
2187                               Timer timer
2188                               {
2189                                  bool DelayExpired()
2190                                  {
2191                                     clientSize.w;
2192                                     return true;
2193                                  }
2194                               };
2195                            }
2196                      */
2197                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2198
2199                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2200
2201                      /*
2202                      if(strcmp(declarator.symbol.string, name))
2203                      {
2204                         printf("TOCHECK: Look out for this\n");
2205                         delete declarator.symbol.string;
2206                         declarator.symbol.string = CopyString(name);
2207                      }
2208
2209                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2210                      {
2211                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2212                         excludedSymbols->Remove(declarator.symbol);
2213                         globalContext.symbols.Add((BTNode)declarator.symbol);
2214                         if(strstr(declarator.symbol.string), "::")
2215                            globalContext.hasNameSpace = true;
2216
2217                      }
2218                      */
2219
2220                      //curExternal = curExternal.prev;
2221                      //afterExternal = afterExternal->next;
2222
2223                      //ProcessFunction(afterExternal->function);
2224
2225                      //curExternal = afterExternal;
2226                      {
2227                         External externalDecl;
2228                         externalDecl = MkExternalDeclaration(null);
2229                         ast->Insert(oldExternal.prev, externalDecl);
2230
2231                         // Which function does this process?
2232                         if(createdExternal.function)
2233                         {
2234                            ProcessFunction(createdExternal.function);
2235
2236                            //curExternal = oldExternal;
2237
2238                            {
2239                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2240
2241                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2242                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2243
2244                               //externalDecl = MkExternalDeclaration(decl);
2245
2246                               //***** ast->Insert(external.prev, externalDecl);
2247                               //ast->Insert(curExternal.prev, externalDecl);
2248                               externalDecl.declaration = decl;
2249                               if(decl.symbol && !decl.symbol.pointerExternal)
2250                                  decl.symbol.pointerExternal = externalDecl;
2251
2252                               // Trying this out...
2253                               declarator.symbol.pointerExternal = externalDecl;
2254                            }
2255                         }
2256                      }
2257                   }
2258                   else if(declarator)
2259                   {
2260                      curExternal = declarator.symbol.pointerExternal;
2261                      ProcessFunction((FunctionDefinition)members.function);
2262                   }
2263                   curExternal = external;
2264                   curContext = context;
2265
2266                   if(inCompiler)
2267                   {
2268                      FreeClassFunction(members.function);
2269
2270                      // In this pass, turn this into a MemberInitData
2271                      exp = QMkExpId(name);
2272                      members.type = dataMembersInit;
2273                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2274
2275                      delete unmangled;
2276                   }
2277                   break;
2278                }
2279                case dataMembersInit:
2280                {
2281                   if(members.dataMembers && classSym)
2282                   {
2283                      MemberInit member;
2284                      Location oldyyloc = yylloc;
2285                      for(member = members.dataMembers->first; member; member = member.next)
2286                      {
2287                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2288                         if(member.initializer && !member.initializer.isConstant)
2289                            inst.isConstant = false;
2290                      }
2291                      yylloc = oldyyloc;
2292                   }
2293                   break;
2294                }
2295             }
2296          }
2297       }
2298    }
2299 }
2300
2301 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2302 {
2303    // OPTIMIZATIONS: TESTING THIS...
2304    if(inCompiler)
2305    {
2306       if(type.kind == functionType)
2307       {
2308          Type param;
2309          if(declareParams)
2310          {
2311             for(param = type.params.first; param; param = param.next)
2312                DeclareType(param, declarePointers, true);
2313          }
2314          DeclareType(type.returnType, declarePointers, true);
2315       }
2316       else if(type.kind == pointerType && declarePointers)
2317          DeclareType(type.type, declarePointers, false);
2318       else if(type.kind == classType)
2319       {
2320          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2321             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2322       }
2323       else if(type.kind == structType || type.kind == unionType)
2324       {
2325          Type member;
2326          for(member = type.members.first; member; member = member.next)
2327             DeclareType(member, false, false);
2328       }
2329       else if(type.kind == arrayType)
2330          DeclareType(type.arrayType, declarePointers, false);
2331    }
2332 }
2333
2334 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2335 {
2336    ClassTemplateArgument * arg = null;
2337    int id = 0;
2338    ClassTemplateParameter curParam = null;
2339    Class sClass;
2340    for(sClass = _class; sClass; sClass = sClass.base)
2341    {
2342       id = 0;
2343       if(sClass.templateClass) sClass = sClass.templateClass;
2344       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2345       {
2346          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2347          {
2348             for(sClass = sClass.base; sClass; sClass = sClass.base)
2349             {
2350                if(sClass.templateClass) sClass = sClass.templateClass;
2351                id += sClass.templateParams.count;
2352             }
2353             break;
2354          }
2355          id++;
2356       }
2357       if(curParam) break;
2358    }
2359    if(curParam)
2360    {
2361       arg = &_class.templateArgs[id];
2362       if(arg && param.type == type)
2363          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2364    }
2365    return arg;
2366 }
2367
2368 public Context SetupTemplatesContext(Class _class)
2369 {
2370    Context context = PushContext();
2371    context.templateTypesOnly = true;
2372    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2373    {
2374       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2375       for(; param; param = param.next)
2376       {
2377          if(param.type == type && param.identifier)
2378          {
2379             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2380             curContext.templateTypes.Add((BTNode)type);
2381          }
2382       }
2383    }
2384    else if(_class)
2385    {
2386       Class sClass;
2387       for(sClass = _class; sClass; sClass = sClass.base)
2388       {
2389          ClassTemplateParameter p;
2390          for(p = sClass.templateParams.first; p; p = p.next)
2391          {
2392             //OldList * specs = MkList();
2393             //Declarator decl = null;
2394             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2395             if(p.type == type)
2396             {
2397                TemplateParameter param = p.param;
2398                TemplatedType type;
2399                if(!param)
2400                {
2401                   // ADD DATA TYPE HERE...
2402                   p.param = param = TemplateParameter
2403                   {
2404                      identifier = MkIdentifier(p.name), type = p.type,
2405                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2406                   };
2407                }
2408                type = TemplatedType { key = (uintptr)p.name, param = param };
2409                curContext.templateTypes.Add((BTNode)type);
2410             }
2411          }
2412       }
2413    }
2414    return context;
2415 }
2416
2417 public void FinishTemplatesContext(Context context)
2418 {
2419    PopContext(context);
2420    FreeContext(context);
2421    delete context;
2422 }
2423
2424 public void ProcessMethodType(Method method)
2425 {
2426    if(!method.dataType)
2427    {
2428       Context context = SetupTemplatesContext(method._class);
2429
2430       method.dataType = ProcessTypeString(method.dataTypeString, false);
2431
2432       FinishTemplatesContext(context);
2433
2434       if(method.type != virtualMethod && method.dataType)
2435       {
2436          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2437          {
2438             if(!method._class.symbol)
2439                method._class.symbol = FindClass(method._class.fullName);
2440             method.dataType.thisClass = method._class.symbol;
2441          }
2442       }
2443
2444       // Why was this commented out? Working fine without now...
2445
2446       /*
2447       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2448          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2449          */
2450    }
2451
2452    /*
2453    if(type)
2454    {
2455       char * par = strstr(type, "(");
2456       char * classOp = null;
2457       int classOpLen = 0;
2458       if(par)
2459       {
2460          int c;
2461          for(c = par-type-1; c >= 0; c++)
2462          {
2463             if(type[c] == ':' && type[c+1] == ':')
2464             {
2465                classOp = type + c - 1;
2466                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2467                {
2468                   classOp--;
2469                   classOpLen++;
2470                }
2471                break;
2472             }
2473             else if(!isspace(type[c]))
2474                break;
2475          }
2476       }
2477       if(classOp)
2478       {
2479          char temp[1024];
2480          int typeLen = strlen(type);
2481          memcpy(temp, classOp, classOpLen);
2482          temp[classOpLen] = '\0';
2483          if(temp[0])
2484             _class = eSystem_FindClass(module, temp);
2485          else
2486             _class = null;
2487          method.dataTypeString = new char[typeLen - classOpLen + 1];
2488          memcpy(method.dataTypeString, type, classOp - type);
2489          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2490       }
2491       else
2492          method.dataTypeString = type;
2493    }
2494    */
2495 }
2496
2497
2498 public void ProcessPropertyType(Property prop)
2499 {
2500    if(!prop.dataType)
2501    {
2502       Context context = SetupTemplatesContext(prop._class);
2503       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2504       FinishTemplatesContext(context);
2505    }
2506 }
2507
2508 public void DeclareMethod(Method method, const char * name)
2509 {
2510    Symbol symbol = method.symbol;
2511    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2512    {
2513       //bool imported = false;
2514       bool dllImport = false;
2515
2516       if(!method.dataType)
2517          method.dataType = ProcessTypeString(method.dataTypeString, false);
2518
2519       if(!symbol || symbol._import || method.type == virtualMethod)
2520       {
2521          if(!symbol || method.type == virtualMethod)
2522          {
2523             Symbol classSym;
2524             if(!method._class.symbol)
2525                method._class.symbol = FindClass(method._class.fullName);
2526             classSym = method._class.symbol;
2527             if(!classSym._import)
2528             {
2529                ModuleImport module;
2530
2531                if(method._class.module && method._class.module.name)
2532                   module = FindModule(method._class.module);
2533                else
2534                   module = mainModule;
2535                classSym._import = ClassImport
2536                {
2537                   name = CopyString(method._class.fullName);
2538                   isRemote = method._class.isRemote;
2539                };
2540                module.classes.Add(classSym._import);
2541             }
2542             if(!symbol)
2543             {
2544                symbol = method.symbol = Symbol { };
2545             }
2546             if(!symbol._import)
2547             {
2548                symbol._import = (ClassImport)MethodImport
2549                {
2550                   name = CopyString(method.name);
2551                   isVirtual = method.type == virtualMethod;
2552                };
2553                classSym._import.methods.Add(symbol._import);
2554             }
2555             if(!symbol)
2556             {
2557                // Set the symbol type
2558                /*
2559                if(!type.thisClass)
2560                {
2561                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2562                }
2563                else if(type.thisClass == (void *)-1)
2564                {
2565                   type.thisClass = null;
2566                }
2567                */
2568                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2569                symbol.type = method.dataType;
2570                if(symbol.type) symbol.type.refCount++;
2571             }
2572             /*
2573             if(!method.thisClass || strcmp(method.thisClass, "void"))
2574                symbol.type.params.Insert(null,
2575                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2576             */
2577          }
2578          if(!method.dataType.dllExport)
2579          {
2580             //imported = true;
2581             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2582                dllImport = true;
2583          }
2584       }
2585
2586       /* MOVING THIS UP
2587       if(!method.dataType)
2588          method.dataType = ((Symbol)method.symbol).type;
2589          //ProcessMethodType(method);
2590       */
2591
2592       if(method.type != virtualMethod && method.dataType)
2593          DeclareType(method.dataType, true, true);
2594
2595       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2596       {
2597          // We need a declaration here :)
2598          Declaration decl;
2599          OldList * specifiers, * declarators;
2600          Declarator d;
2601          Declarator funcDecl;
2602          External external;
2603
2604          specifiers = MkList();
2605          declarators = MkList();
2606
2607          //if(imported)
2608          if(dllImport)
2609             ListAdd(specifiers, MkSpecifier(EXTERN));
2610          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2611             ListAdd(specifiers, MkSpecifier(STATIC));
2612
2613          if(method.type == virtualMethod)
2614          {
2615             ListAdd(specifiers, MkSpecifier(INT));
2616             d = MkDeclaratorIdentifier(MkIdentifier(name));
2617          }
2618          else
2619          {
2620             d = MkDeclaratorIdentifier(MkIdentifier(name));
2621             //if(imported)
2622             if(dllImport)
2623                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2624             {
2625                Context context = SetupTemplatesContext(method._class);
2626                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2627                FinishTemplatesContext(context);
2628             }
2629             funcDecl = GetFuncDecl(d);
2630
2631             if(dllImport)
2632             {
2633                Specifier spec, next;
2634                for(spec = specifiers->first; spec; spec = next)
2635                {
2636                   next = spec.next;
2637                   if(spec.type == extendedSpecifier)
2638                   {
2639                      specifiers->Remove(spec);
2640                      FreeSpecifier(spec);
2641                   }
2642                }
2643             }
2644
2645             // Add this parameter if not a static method
2646             if(method.dataType && !method.dataType.staticMethod)
2647             {
2648                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2649                {
2650                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2651                   TypeName thisParam = MkTypeName(MkListOne(
2652                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2653                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2654                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2655                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2656
2657                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2658                   {
2659                      TypeName param = funcDecl.function.parameters->first;
2660                      funcDecl.function.parameters->Remove(param);
2661                      FreeTypeName(param);
2662                   }
2663
2664                   if(!funcDecl.function.parameters)
2665                      funcDecl.function.parameters = MkList();
2666                   funcDecl.function.parameters->Insert(null, thisParam);
2667                }
2668             }
2669             // Make sure we don't have empty parameter declarations for static methods...
2670             /*
2671             else if(!funcDecl.function.parameters)
2672             {
2673                funcDecl.function.parameters = MkList();
2674                funcDecl.function.parameters->Insert(null,
2675                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2676             }*/
2677          }
2678          // TESTING THIS:
2679          ProcessDeclarator(d);
2680
2681          ListAdd(declarators, MkInitDeclarator(d, null));
2682
2683          decl = MkDeclaration(specifiers, declarators);
2684
2685          ReplaceThisClassSpecifiers(specifiers, method._class);
2686
2687          // Keep a different symbol for the function definition than the declaration...
2688          if(symbol.pointerExternal)
2689          {
2690             Symbol functionSymbol { };
2691
2692             // Copy symbol
2693             {
2694                *functionSymbol = *symbol;
2695                functionSymbol.string = CopyString(symbol.string);
2696                if(functionSymbol.type)
2697                   functionSymbol.type.refCount++;
2698             }
2699
2700             excludedSymbols->Add(functionSymbol);
2701             symbol.pointerExternal.symbol = functionSymbol;
2702          }
2703          external = MkExternalDeclaration(decl);
2704          if(curExternal)
2705             ast->Insert(curExternal ? curExternal.prev : null, external);
2706          external.symbol = symbol;
2707          symbol.pointerExternal = external;
2708       }
2709       else if(ast)
2710       {
2711          // Move declaration higher...
2712          ast->Move(symbol.pointerExternal, curExternal.prev);
2713       }
2714
2715       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2716    }
2717 }
2718
2719 char * ReplaceThisClass(Class _class)
2720 {
2721    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2722    {
2723       bool first = true;
2724       int p = 0;
2725       ClassTemplateParameter param;
2726       int lastParam = -1;
2727
2728       char className[1024];
2729       strcpy(className, _class.fullName);
2730       for(param = _class.templateParams.first; param; param = param.next)
2731       {
2732          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2733          {
2734             if(first) strcat(className, "<");
2735             if(!first) strcat(className, ", ");
2736             if(lastParam + 1 != p)
2737             {
2738                strcat(className, param.name);
2739                strcat(className, " = ");
2740             }
2741             strcat(className, param.name);
2742             first = false;
2743             lastParam = p;
2744          }
2745          p++;
2746       }
2747       if(!first)
2748       {
2749          int len = strlen(className);
2750          if(className[len-1] == '>') className[len++] = ' ';
2751          className[len++] = '>';
2752          className[len++] = '\0';
2753       }
2754       return CopyString(className);
2755    }
2756    else
2757       return CopyString(_class.fullName);
2758 }
2759
2760 Type ReplaceThisClassType(Class _class)
2761 {
2762    Type type;
2763    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2764    {
2765       bool first = true;
2766       int p = 0;
2767       ClassTemplateParameter param;
2768       int lastParam = -1;
2769       char className[1024];
2770       strcpy(className, _class.fullName);
2771
2772       for(param = _class.templateParams.first; param; param = param.next)
2773       {
2774          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2775          {
2776             if(first) strcat(className, "<");
2777             if(!first) strcat(className, ", ");
2778             if(lastParam + 1 != p)
2779             {
2780                strcat(className, param.name);
2781                strcat(className, " = ");
2782             }
2783             strcat(className, param.name);
2784             first = false;
2785             lastParam = p;
2786          }
2787          p++;
2788       }
2789       if(!first)
2790       {
2791          int len = strlen(className);
2792          if(className[len-1] == '>') className[len++] = ' ';
2793          className[len++] = '>';
2794          className[len++] = '\0';
2795       }
2796       type = MkClassType(className);
2797       //type = ProcessTypeString(className, false);
2798    }
2799    else
2800    {
2801       type = MkClassType(_class.fullName);
2802       //type = ProcessTypeString(_class.fullName, false);
2803    }
2804    //type.wasThisClass = true;
2805    return type;
2806 }
2807
2808 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2809 {
2810    if(specs != null && _class)
2811    {
2812       Specifier spec;
2813       for(spec = specs.first; spec; spec = spec.next)
2814       {
2815          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2816          {
2817             spec.type = nameSpecifier;
2818             spec.name = ReplaceThisClass(_class);
2819             spec.symbol = FindClass(spec.name); //_class.symbol;
2820          }
2821       }
2822    }
2823 }
2824
2825 // Returns imported or not
2826 bool DeclareFunction(GlobalFunction function, char * name)
2827 {
2828    Symbol symbol = function.symbol;
2829    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2830    {
2831       bool imported = false;
2832       bool dllImport = false;
2833
2834       if(!function.dataType)
2835       {
2836          function.dataType = ProcessTypeString(function.dataTypeString, false);
2837          if(!function.dataType.thisClass)
2838             function.dataType.staticMethod = true;
2839       }
2840
2841       if(inCompiler)
2842       {
2843          if(!symbol)
2844          {
2845             ModuleImport module = FindModule(function.module);
2846             // WARNING: This is not added anywhere...
2847             symbol = function.symbol = Symbol {  };
2848
2849             if(module.name)
2850             {
2851                if(!function.dataType.dllExport)
2852                {
2853                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2854                   module.functions.Add(symbol._import);
2855                }
2856             }
2857             // Set the symbol type
2858             {
2859                symbol.type = ProcessTypeString(function.dataTypeString, false);
2860                if(!symbol.type.thisClass)
2861                   symbol.type.staticMethod = true;
2862             }
2863          }
2864          imported = symbol._import ? true : false;
2865          if(imported && function.module != privateModule && function.module.importType != staticImport)
2866             dllImport = true;
2867       }
2868
2869       DeclareType(function.dataType, true, true);
2870
2871       if(inCompiler)
2872       {
2873          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2874          {
2875             // We need a declaration here :)
2876             Declaration decl;
2877             OldList * specifiers, * declarators;
2878             Declarator d;
2879             Declarator funcDecl;
2880             External external;
2881
2882             specifiers = MkList();
2883             declarators = MkList();
2884
2885             //if(imported)
2886                ListAdd(specifiers, MkSpecifier(EXTERN));
2887             /*
2888             else
2889                ListAdd(specifiers, MkSpecifier(STATIC));
2890             */
2891
2892             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2893             //if(imported)
2894             if(dllImport)
2895                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2896
2897             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2898             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2899             if(function.module.importType == staticImport)
2900             {
2901                Specifier spec;
2902                for(spec = specifiers->first; spec; spec = spec.next)
2903                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2904                   {
2905                      specifiers->Remove(spec);
2906                      FreeSpecifier(spec);
2907                      break;
2908                   }
2909             }
2910
2911             funcDecl = GetFuncDecl(d);
2912
2913             // Make sure we don't have empty parameter declarations for static methods...
2914             if(funcDecl && !funcDecl.function.parameters)
2915             {
2916                funcDecl.function.parameters = MkList();
2917                funcDecl.function.parameters->Insert(null,
2918                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2919             }
2920
2921             ListAdd(declarators, MkInitDeclarator(d, null));
2922
2923             {
2924                Context oldCtx = curContext;
2925                curContext = globalContext;
2926                decl = MkDeclaration(specifiers, declarators);
2927                curContext = oldCtx;
2928             }
2929
2930             // Keep a different symbol for the function definition than the declaration...
2931             if(symbol.pointerExternal)
2932             {
2933                Symbol functionSymbol { };
2934                // Copy symbol
2935                {
2936                   *functionSymbol = *symbol;
2937                   functionSymbol.string = CopyString(symbol.string);
2938                   if(functionSymbol.type)
2939                      functionSymbol.type.refCount++;
2940                }
2941
2942                excludedSymbols->Add(functionSymbol);
2943
2944                symbol.pointerExternal.symbol = functionSymbol;
2945             }
2946             external = MkExternalDeclaration(decl);
2947             if(curExternal)
2948                ast->Insert(curExternal.prev, external);
2949             external.symbol = symbol;
2950             symbol.pointerExternal = external;
2951          }
2952          else
2953          {
2954             // Move declaration higher...
2955             ast->Move(symbol.pointerExternal, curExternal.prev);
2956          }
2957
2958          if(curExternal)
2959             symbol.id = curExternal.symbol.idCode;
2960       }
2961    }
2962    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2963 }
2964
2965 void DeclareGlobalData(GlobalData data)
2966 {
2967    Symbol symbol = data.symbol;
2968    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2969    {
2970       if(inCompiler)
2971       {
2972          if(!symbol)
2973             symbol = data.symbol = Symbol { };
2974       }
2975       if(!data.dataType)
2976          data.dataType = ProcessTypeString(data.dataTypeString, false);
2977       DeclareType(data.dataType, true, true);
2978       if(inCompiler)
2979       {
2980          if(!symbol.pointerExternal)
2981          {
2982             // We need a declaration here :)
2983             Declaration decl;
2984             OldList * specifiers, * declarators;
2985             Declarator d;
2986             External external;
2987
2988             specifiers = MkList();
2989             declarators = MkList();
2990
2991             ListAdd(specifiers, MkSpecifier(EXTERN));
2992             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2993             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2994
2995             ListAdd(declarators, MkInitDeclarator(d, null));
2996
2997             decl = MkDeclaration(specifiers, declarators);
2998             external = MkExternalDeclaration(decl);
2999             if(curExternal)
3000                ast->Insert(curExternal.prev, external);
3001             external.symbol = symbol;
3002             symbol.pointerExternal = external;
3003          }
3004          else
3005          {
3006             // Move declaration higher...
3007             ast->Move(symbol.pointerExternal, curExternal.prev);
3008          }
3009
3010          if(curExternal)
3011             symbol.id = curExternal.symbol.idCode;
3012       }
3013    }
3014 }
3015
3016 class Conversion : struct
3017 {
3018    Conversion prev, next;
3019    Property convert;
3020    bool isGet;
3021    Type resultType;
3022 };
3023
3024 static bool CheckConstCompatibility(Type source, Type dest, bool warn)
3025 {
3026    bool status = true;
3027    if(((source.kind == classType && source._class && source._class.registered) || source.kind == arrayType || source.kind == pointerType) &&
3028       ((dest.kind == classType && dest._class && dest._class.registered) || /*dest.kind == arrayType || */dest.kind == pointerType))
3029    {
3030       Class sourceClass = source.kind == classType ? source._class.registered : null;
3031       Class destClass = dest.kind == classType ? dest._class.registered : null;
3032       if((!sourceClass || (sourceClass && sourceClass.type == normalClass && !sourceClass.structSize)) &&
3033          (!destClass || (destClass && destClass.type == normalClass && !destClass.structSize)))
3034       {
3035          Type sourceType = source, destType = dest;
3036          while((sourceType.kind == pointerType || sourceType.kind == arrayType) && sourceType.type) sourceType = sourceType.type;
3037          while((destType.kind == pointerType || destType.kind == arrayType) && destType.type) destType = destType.type;
3038          if(!destType.constant && sourceType.constant)
3039          {
3040             status = false;
3041             if(warn)
3042                Compiler_Warning($"discarding const qualifier\n");
3043          }
3044       }
3045    }
3046    return status;
3047 }
3048
3049 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams,
3050                        bool isConversionExploration, bool warnConst)
3051 {
3052    if(source && dest)
3053    {
3054       if(warnConst)
3055          CheckConstCompatibility(source, dest, true);
3056       // Property convert;
3057
3058       if(source.kind == templateType && dest.kind != templateType)
3059       {
3060          Type type = ProcessTemplateParameterType(source.templateParameter);
3061          if(type) source = type;
3062       }
3063
3064       if(dest.kind == templateType && source.kind != templateType)
3065       {
3066          Type type = ProcessTemplateParameterType(dest.templateParameter);
3067          if(type) dest = type;
3068       }
3069
3070       if(dest.classObjectType == typedObject && dest.kind != functionType)
3071       {
3072          if(source.classObjectType != anyObject)
3073             return true;
3074          else
3075          {
3076             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
3077             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
3078             {
3079                return true;
3080             }
3081          }
3082       }
3083       else
3084       {
3085          if(source.kind != functionType && source.classObjectType == anyObject)
3086             return true;
3087          if(dest.kind != functionType && dest.classObjectType == anyObject && source.classObjectType != typedObject)
3088             return true;
3089       }
3090
3091       if((dest.kind == structType && source.kind == structType) ||
3092          (dest.kind == unionType && source.kind == unionType))
3093       {
3094          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
3095              (source.members.first && source.members.first == dest.members.first))
3096             return true;
3097       }
3098
3099       if(dest.kind == ellipsisType && source.kind != voidType)
3100          return true;
3101
3102       if(dest.kind == pointerType && dest.type.kind == voidType &&
3103          ((source.kind == classType && (!source._class || !source._class.registered || source._class.registered.type == structClass || source._class.registered.type == normalClass || source._class.registered.type == noHeadClass || source._class.registered.type == systemClass))
3104          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
3105
3106          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
3107
3108          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
3109          return true;
3110       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
3111          ((dest.kind == classType && (!dest._class || !dest._class.registered || dest._class.registered.type == structClass || dest._class.registered.type == normalClass || dest._class.registered.type == noHeadClass || dest._class.registered.type == systemClass))
3112          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
3113          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
3114
3115          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
3116          return true;
3117
3118       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
3119       {
3120          if(source._class.registered && source._class.registered.type == unitClass)
3121          {
3122             if(conversions != null)
3123             {
3124                if(source._class.registered == dest._class.registered)
3125                   return true;
3126             }
3127             else
3128             {
3129                Class sourceBase, destBase;
3130                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
3131                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
3132                if(sourceBase == destBase)
3133                   return true;
3134             }
3135          }
3136          // Don't match enum inheriting from other enum if resolving enumeration values
3137          // TESTING: !dest.classObjectType
3138          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3139             (enumBaseType ||
3140                (!source._class.registered || source._class.registered.type != enumClass) ||
3141                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3142             return true;
3143          else
3144          {
3145             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3146             if(enumBaseType &&
3147                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3148                ((source._class && source._class.registered && source._class.registered.type != enumClass) || source.kind == classType)) // Added this here for a base enum to be acceptable for a derived enum (#139)
3149             {
3150                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3151                {
3152                   return true;
3153                }
3154             }
3155          }
3156       }
3157
3158       // JUST ADDED THIS...
3159       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3160          return true;
3161
3162       if(doConversion)
3163       {
3164          // Just added this for Straight conversion of ColorAlpha => Color
3165          if(source.kind == classType)
3166          {
3167             Class _class;
3168             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3169             {
3170                Property convert;
3171                for(convert = _class.conversions.first; convert; convert = convert.next)
3172                {
3173                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3174                   {
3175                      Conversion after = (conversions != null) ? conversions.last : null;
3176
3177                      if(!convert.dataType)
3178                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3179                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3180                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3181                         MatchTypes(convert.dataType, dest, conversions, null, null,
3182                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3183                               convert.dataType.kind == classType, false, true, warnConst))
3184                      {
3185                         if(!conversions && !convert.Get)
3186                            return true;
3187                         else if(conversions != null)
3188                         {
3189                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3190                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3191                               (dest.kind != classType || dest._class.registered != _class.base))
3192                               return true;
3193                            else
3194                            {
3195                               Conversion conv { convert = convert, isGet = true };
3196                               // conversions.Add(conv);
3197                               conversions.Insert(after, conv);
3198
3199                               return true;
3200                            }
3201                         }
3202                      }
3203                   }
3204                }
3205             }
3206          }
3207
3208          // MOVING THIS??
3209
3210          if(dest.kind == classType)
3211          {
3212             Class _class;
3213             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3214             {
3215                Property convert;
3216                for(convert = _class.conversions.first; convert; convert = convert.next)
3217                {
3218                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3219                   {
3220                      Type constType = null;
3221                      bool success = false;
3222                      // Conversion after = (conversions != null) ? conversions.last : null;
3223
3224                      if(!convert.dataType)
3225                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3226
3227                      if(warnConst && convert.dataType.kind == pointerType && convert.dataType.type && dest.constant)
3228                      {
3229                         Type ptrType { };
3230                         constType = { kind = pointerType, refCount = 1, type = ptrType };
3231                         CopyTypeInto(ptrType, convert.dataType.type);
3232                         ptrType.constant = true;
3233                      }
3234
3235                      // Just added this equality check to prevent recursion.... Make it safer?
3236                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3237                      if((constType || convert.dataType != dest) && MatchTypes(source, constType ? constType : convert.dataType, conversions, null, null, true, false /*true*/, false, true, warnConst))
3238                      {
3239                         if(!conversions && !convert.Set)
3240                            success = true;
3241                         else if(conversions != null)
3242                         {
3243                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3244                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3245                               (source.kind != classType || source._class.registered != _class.base))
3246                               success = true;
3247                            else
3248                            {
3249                               // *** Testing this! ***
3250                               Conversion conv { convert = convert };
3251                               conversions.Add(conv);
3252                               //conversions.Insert(after, conv);
3253                               success = true;
3254                            }
3255                         }
3256                      }
3257                      if(constType)
3258                         FreeType(constType);
3259                      if(success)
3260                         return true;
3261                   }
3262                }
3263             }
3264             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3265             {
3266                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3267                   (source.kind != classType || source._class.registered.type != structClass))
3268                   return true;
3269             }*/
3270
3271             // TESTING THIS... IS THIS OK??
3272             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3273             {
3274                if(!dest._class.registered.dataType)
3275                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3276                // Only support this for classes...
3277                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3278                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3279                {
3280                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false, warnConst))
3281                   {
3282                      return true;
3283                   }
3284                }
3285             }
3286          }
3287
3288          // Moved this lower
3289          if(source.kind == classType)
3290          {
3291             Class _class;
3292             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3293             {
3294                Property convert;
3295                for(convert = _class.conversions.first; convert; convert = convert.next)
3296                {
3297                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3298                   {
3299                      Conversion after = (conversions != null) ? conversions.last : null;
3300
3301                      if(!convert.dataType)
3302                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3303                      if(convert.dataType != source &&
3304                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3305                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true, warnConst))
3306                      {
3307                         if(!conversions && !convert.Get)
3308                            return true;
3309                         else if(conversions != null)
3310                         {
3311                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3312                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3313                               (dest.kind != classType || dest._class.registered != _class.base))
3314                               return true;
3315                            else
3316                            {
3317                               Conversion conv { convert = convert, isGet = true };
3318
3319                               // conversions.Add(conv);
3320                               conversions.Insert(after, conv);
3321                               return true;
3322                            }
3323                         }
3324                      }
3325                   }
3326                }
3327             }
3328
3329             // TESTING THIS... IS THIS OK??
3330             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3331             {
3332                if(!source._class.registered.dataType)
3333                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3334                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3335                {
3336                   if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, source._class.registered.dataType.kind == classType, source._class.registered.dataType.kind == classType, false, false, warnConst))
3337                      return true;
3338                   // For bool to be accepted by byte, short, etc.
3339                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false, warnConst))
3340                      return true;
3341                }
3342             }
3343          }
3344       }
3345
3346       if(source.kind == classType || source.kind == subClassType)
3347          ;
3348       else if(dest.kind == source.kind &&
3349          (dest.kind != structType && dest.kind != unionType &&
3350           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3351           return true;
3352       // RECENTLY ADDED THESE
3353       else if(dest.kind == doubleType && source.kind == floatType)
3354          return true;
3355       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3356          return true;
3357       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3358          return true;
3359       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3360          return true;
3361       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3362          return true;
3363       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3364          return true;
3365       else if(source.kind == enumType &&
3366          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3367           return true;
3368       else if(dest.kind == enumType && !isConversionExploration &&
3369          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3370           return true;
3371       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3372               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3373       {
3374          Type paramSource, paramDest;
3375
3376          if(dest.kind == methodType)
3377             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3378          if(source.kind == methodType)
3379             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3380
3381          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3382          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3383          if(dest.kind == methodType)
3384             dest = dest.method.dataType;
3385          if(source.kind == methodType)
3386             source = source.method.dataType;
3387
3388          paramSource = source.params.first;
3389          if(paramSource && paramSource.kind == voidType) paramSource = null;
3390          paramDest = dest.params.first;
3391          if(paramDest && paramDest.kind == voidType) paramDest = null;
3392
3393
3394          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3395             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3396          {
3397             // Source thisClass must be derived from destination thisClass
3398             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3399                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3400             {
3401                if(paramDest && paramDest.kind == classType)
3402                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3403                else
3404                   Compiler_Error($"method class should not take an object\n");
3405                return false;
3406             }
3407             paramDest = paramDest.next;
3408          }
3409          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3410          {
3411             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3412             {
3413                if(dest.thisClass)
3414                {
3415                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3416                   {
3417                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3418                      return false;
3419                   }
3420                }
3421                else
3422                {
3423                   // THIS WAS BACKWARDS:
3424                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3425                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3426                   {
3427                      if(owningClassDest)
3428                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3429                      else
3430                         Compiler_Error($"overriding class expected to be derived from method class\n");
3431                      return false;
3432                   }
3433                }
3434                paramSource = paramSource.next;
3435             }
3436             else
3437             {
3438                if(dest.thisClass)
3439                {
3440                   // Source thisClass must be derived from destination thisClass
3441                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3442                   {
3443                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3444                      return false;
3445                   }
3446                }
3447                else
3448                {
3449                   // THIS WAS BACKWARDS TOO??
3450                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3451                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3452                   {
3453                      //if(owningClass)
3454                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3455                      //else
3456                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3457                      return false;
3458                   }
3459                }
3460             }
3461          }
3462
3463
3464          // Source return type must be derived from destination return type
3465          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false, warnConst))
3466          {
3467             Compiler_Warning($"incompatible return type for function\n");
3468             return false;
3469          }
3470          // The const check is backwards from the MatchTypes above (for derivative classes checks)
3471          else
3472             CheckConstCompatibility(dest.returnType, source.returnType, true);
3473
3474          // Check parameters
3475
3476          for(; paramDest; paramDest = paramDest.next)
3477          {
3478             if(!paramSource)
3479             {
3480                //Compiler_Warning($"not enough parameters\n");
3481                Compiler_Error($"not enough parameters\n");
3482                return false;
3483             }
3484             {
3485                Type paramDestType = paramDest;
3486                Type paramSourceType = paramSource;
3487                Type type = paramDestType;
3488
3489                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3490                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3491                   paramSource.kind != templateType)
3492                {
3493                   int id = 0;
3494                   ClassTemplateParameter curParam = null;
3495                   Class sClass;
3496                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3497                   {
3498                      id = 0;
3499                      if(sClass.templateClass) sClass = sClass.templateClass;
3500                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3501                      {
3502                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3503                         {
3504                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3505                            {
3506                               if(sClass.templateClass) sClass = sClass.templateClass;
3507                               id += sClass.templateParams.count;
3508                            }
3509                            break;
3510                         }
3511                         id++;
3512                      }
3513                      if(curParam) break;
3514                   }
3515
3516                   if(curParam)
3517                   {
3518                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3519                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3520                   }
3521                }
3522
3523                // paramDest must be derived from paramSource
3524                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false, warnConst) &&
3525                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false, warnConst)))
3526                {
3527                   char type[1024];
3528                   type[0] = 0;
3529                   PrintType(paramDest, type, false, true);
3530                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3531
3532                   if(paramDestType != paramDest)
3533                      FreeType(paramDestType);
3534                   return false;
3535                }
3536                if(paramDestType != paramDest)
3537                   FreeType(paramDestType);
3538             }
3539
3540             paramSource = paramSource.next;
3541          }
3542          if(paramSource)
3543          {
3544             Compiler_Error($"too many parameters\n");
3545             return false;
3546          }
3547          return true;
3548       }
3549       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3550       {
3551          return true;
3552       }
3553       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3554          (source.kind == arrayType || source.kind == pointerType))
3555       {
3556          // Pointers to pointer is incompatible with non normal/nohead classes
3557          if(!(dest.type && dest.type.kind == pointerType && source.type.kind == classType && source.type._class &&
3558             source.type._class.registered && (source.type._class.registered.type != normalClass && source.type._class.registered.type != noHeadClass) && !source.type.byReference))
3559          {
3560             ComputeTypeSize(source.type);
3561             ComputeTypeSize(dest.type);
3562             if(source.type.size == dest.type.size && MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3563                return true;
3564          }
3565       }
3566    }
3567    return false;
3568 }
3569
3570 static void FreeConvert(Conversion convert)
3571 {
3572    if(convert.resultType)
3573       FreeType(convert.resultType);
3574 }
3575
3576 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3577                               char * string, OldList conversions)
3578 {
3579    BTNamedLink link;
3580
3581    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3582    {
3583       Class _class = link.data;
3584       if(_class.type == enumClass)
3585       {
3586          OldList converts { };
3587          Type type { };
3588          type.kind = classType;
3589
3590          if(!_class.symbol)
3591             _class.symbol = FindClass(_class.fullName);
3592          type._class = _class.symbol;
3593
3594          if(MatchTypes(type, dest, &converts, null, null, dest.kind != classType || !dest._class || strcmp(dest._class.string, "bool"),
3595                false, false, false, false))
3596          {
3597             NamedLink64 value;
3598             Class enumClass = eSystem_FindClass(privateModule, "enum");
3599             if(enumClass)
3600             {
3601                Class baseClass;
3602                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3603                {
3604                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3605                   for(value = e.values.first; value; value = value.next)
3606                   {
3607                      if(!strcmp(value.name, string))
3608                         break;
3609                   }
3610                   if(value)
3611                   {
3612                      FreeType(sourceExp.expType);
3613
3614                      sourceExp.isConstant = true;
3615                      sourceExp.expType = MkClassType(baseClass.fullName);
3616                      if(inCompiler || inPreCompiler || inDebugger)
3617                      {
3618                         char constant[256];
3619                         FreeExpContents(sourceExp);
3620
3621                         sourceExp.type = constantExp;
3622                         if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "short") || !strcmp(baseClass.dataTypeString, "char"))
3623                            sprintf(constant, FORMAT64D, value.data);
3624                         else
3625                            sprintf(constant, FORMAT64HEXLL, value.data);
3626                         sourceExp.constant = CopyString(constant);
3627                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3628                      }
3629
3630                      while(converts.first)
3631                      {
3632                         Conversion convert = converts.first;
3633                         converts.Remove(convert);
3634                         conversions.Add(convert);
3635                      }
3636                      delete type;
3637                      return true;
3638                   }
3639                }
3640             }
3641          }
3642          if(converts.first)
3643             converts.Free(FreeConvert);
3644          delete type;
3645       }
3646    }
3647    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3648       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3649          return true;
3650    return false;
3651 }
3652
3653 public bool ModuleVisibility(Module searchIn, Module searchFor)
3654 {
3655    SubModule subModule;
3656
3657    if(searchFor == searchIn)
3658       return true;
3659
3660    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3661    {
3662       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3663       {
3664          if(ModuleVisibility(subModule.module, searchFor))
3665             return true;
3666       }
3667    }
3668    return false;
3669 }
3670
3671 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3672 {
3673    Module module;
3674
3675    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3676       return true;
3677    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3678       return true;
3679    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3680       return true;
3681
3682    for(module = mainModule.application.allModules.first; module; module = module.next)
3683    {
3684       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3685          return true;
3686    }
3687    return false;
3688 }
3689
3690 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3691 {
3692    Type source;
3693    Type realDest = dest;
3694    Type backupSourceExpType = null;
3695    Expression computedExp = sourceExp;
3696    dest.refCount++;
3697
3698    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3699       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3700    {
3701       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3702       ComputeExpression(computedExp /*sourceExp*/);
3703    }
3704
3705    source = sourceExp.expType;
3706
3707    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3708    {
3709       if(computedExp != sourceExp)
3710       {
3711          FreeExpression(computedExp);
3712          computedExp = sourceExp;
3713       }
3714       FreeType(dest);
3715       return true;
3716    }
3717
3718    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3719    {
3720        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3721        {
3722           Class sourceBase, destBase;
3723           for(sourceBase = source._class.registered;
3724               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3725               sourceBase = sourceBase.base);
3726           for(destBase = dest._class.registered;
3727               destBase && destBase.base && destBase.base.type != systemClass;
3728               destBase = destBase.base);
3729           //if(source._class.registered == dest._class.registered)
3730           if(sourceBase == destBase)
3731           {
3732             if(computedExp != sourceExp)
3733             {
3734                FreeExpression(computedExp);
3735                computedExp = sourceExp;
3736             }
3737             FreeType(dest);
3738             return true;
3739          }
3740       }
3741    }
3742
3743    if(source)
3744    {
3745       OldList * specs;
3746       bool flag = false;
3747       int64 value = MAXINT;
3748
3749       source.refCount++;
3750
3751       if(computedExp.type == constantExp)
3752       {
3753          if(source.isSigned)
3754             value = strtoll(computedExp.constant, null, 0);
3755          else
3756             value = strtoull(computedExp.constant, null, 0);
3757       }
3758       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3759       {
3760          if(source.isSigned)
3761             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3762          else
3763             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3764       }
3765       if(computedExp != sourceExp)
3766       {
3767          FreeExpression(computedExp);
3768          computedExp = sourceExp;
3769       }
3770
3771       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3772          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3773       {
3774          FreeType(source);
3775          source = Type { kind = intType, isSigned = false, refCount = 1 };
3776       }
3777
3778       if(dest.kind == classType)
3779       {
3780          Class _class = dest._class ? dest._class.registered : null;
3781
3782          if(_class && _class.type == unitClass)
3783          {
3784             if(source.kind != classType)
3785             {
3786                Type tempType { };
3787                Type tempDest, tempSource;
3788
3789                for(; _class.base.type != systemClass; _class = _class.base);
3790                tempSource = dest;
3791                tempDest = tempType;
3792
3793                tempType.kind = classType;
3794                if(!_class.symbol)
3795                   _class.symbol = FindClass(_class.fullName);
3796
3797                tempType._class = _class.symbol;
3798                tempType.truth = dest.truth;
3799                if(tempType._class)
3800                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3801
3802                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3803                backupSourceExpType = sourceExp.expType;
3804                sourceExp.expType = dest; dest.refCount++;
3805                //sourceExp.expType = MkClassType(_class.fullName);
3806                flag = true;
3807
3808                delete tempType;
3809             }
3810          }
3811
3812
3813          // Why wasn't there something like this?
3814          if(_class && _class.type == bitClass && source.kind != classType)
3815          {
3816             if(!dest._class.registered.dataType)
3817                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3818             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3819             {
3820                FreeType(source);
3821                FreeType(sourceExp.expType);
3822                source = sourceExp.expType = MkClassType(dest._class.string);
3823                source.refCount++;
3824
3825                //source.kind = classType;
3826                //source._class = dest._class;
3827             }
3828          }
3829
3830          // Adding two enumerations
3831          /*
3832          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3833          {
3834             if(!source._class.registered.dataType)
3835                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3836             if(!dest._class.registered.dataType)
3837                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3838
3839             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3840             {
3841                FreeType(source);
3842                source = sourceExp.expType = MkClassType(dest._class.string);
3843                source.refCount++;
3844
3845                //source.kind = classType;
3846                //source._class = dest._class;
3847             }
3848          }*/
3849
3850          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3851          {
3852             OldList * specs = MkList();
3853             Declarator decl;
3854             char string[1024];
3855
3856             ReadString(string, sourceExp.string);
3857             decl = SpecDeclFromString(string, specs, null);
3858
3859             FreeExpContents(sourceExp);
3860             FreeType(sourceExp.expType);
3861
3862             sourceExp.type = classExp;
3863             sourceExp._classExp.specifiers = specs;
3864             sourceExp._classExp.decl = decl;
3865             sourceExp.expType = dest;
3866             dest.refCount++;
3867
3868             FreeType(source);
3869             FreeType(dest);
3870             if(backupSourceExpType) FreeType(backupSourceExpType);
3871             return true;
3872          }
3873       }
3874       else if(source.kind == classType)
3875       {
3876          Class _class = source._class ? source._class.registered : null;
3877
3878          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3879          {
3880             /*
3881             if(dest.kind != classType)
3882             {
3883                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3884                if(!source._class.registered.dataType)
3885                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3886
3887                FreeType(dest);
3888                dest = MkClassType(source._class.string);
3889                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3890                //   dest = MkClassType(source._class.string);
3891             }
3892             */
3893
3894             if(dest.kind != classType)
3895             {
3896                Type tempType { };
3897                Type tempDest, tempSource;
3898
3899                if(!source._class.registered.dataType)
3900                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3901
3902                for(; _class.base.type != systemClass; _class = _class.base);
3903                tempDest = source;
3904                tempSource = tempType;
3905                tempType.kind = classType;
3906                tempType._class = FindClass(_class.fullName);
3907                tempType.truth = source.truth;
3908                tempType.classObjectType = source.classObjectType;
3909
3910                if(tempType._class)
3911                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3912
3913                // PUT THIS BACK TESTING UNITS?
3914                if(conversions && conversions.last)
3915                {
3916                   ((Conversion)(conversions.last)).resultType = dest;
3917                   dest.refCount++;
3918                }
3919
3920                FreeType(sourceExp.expType);
3921                sourceExp.expType = MkClassType(_class.fullName);
3922                sourceExp.expType.truth = source.truth;
3923                sourceExp.expType.classObjectType = source.classObjectType;
3924
3925                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3926
3927                if(!sourceExp.destType)
3928                {
3929                   FreeType(sourceExp.destType);
3930                   sourceExp.destType = sourceExp.expType;
3931                   if(sourceExp.expType)
3932                      sourceExp.expType.refCount++;
3933                }
3934                //flag = true;
3935                //source = _class.dataType;
3936
3937
3938                // TOCHECK: TESTING THIS NEW CODE
3939                if(!_class.dataType)
3940                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3941                FreeType(dest);
3942                dest = MkClassType(source._class.string);
3943                dest.truth = source.truth;
3944                dest.classObjectType = source.classObjectType;
3945
3946                FreeType(source);
3947                source = _class.dataType;
3948                source.refCount++;
3949
3950                delete tempType;
3951             }
3952          }
3953       }
3954
3955       if(!flag)
3956       {
3957          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3958          {
3959             FreeType(source);
3960             FreeType(dest);
3961             return true;
3962          }
3963       }
3964
3965       // Implicit Casts
3966       /*
3967       if(source.kind == classType)
3968       {
3969          Class _class = source._class.registered;
3970          if(_class.type == unitClass)
3971          {
3972             if(!_class.dataType)
3973                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3974             source = _class.dataType;
3975          }
3976       }*/
3977
3978       if(dest.kind == classType)
3979       {
3980          Class _class = dest._class ? dest._class.registered : null;
3981          bool fittingValue = false;
3982          if(_class && _class.type == enumClass)
3983          {
3984             Class enumClass = eSystem_FindClass(privateModule, "enum");
3985             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3986             if(c && value >= 0 && value <= c.largest)
3987                fittingValue = true;
3988          }
3989
3990          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3991             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3992          {
3993             if(_class.type == normalClass || _class.type == noHeadClass)
3994             {
3995                Expression newExp { };
3996                *newExp = *sourceExp;
3997                if(sourceExp.destType) sourceExp.destType.refCount++;
3998                if(sourceExp.expType)  sourceExp.expType.refCount++;
3999                sourceExp.type = castExp;
4000                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
4001                sourceExp.cast.exp = newExp;
4002                FreeType(sourceExp.expType);
4003                sourceExp.expType = null;
4004                ProcessExpressionType(sourceExp);
4005
4006                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
4007                if(!inCompiler)
4008                {
4009                   FreeType(sourceExp.expType);
4010                   sourceExp.expType = dest;
4011                }
4012
4013                FreeType(source);
4014                if(inCompiler) FreeType(dest);
4015
4016                if(backupSourceExpType) FreeType(backupSourceExpType);
4017                return true;
4018             }
4019
4020             if(!_class.dataType)
4021                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4022             FreeType(dest);
4023             dest = _class.dataType;
4024             dest.refCount++;
4025          }
4026
4027          // Accept lower precision types for units, since we want to keep the unit type
4028          if(dest.kind == doubleType &&
4029             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
4030              source.kind == charType || source.kind == _BoolType))
4031          {
4032             specs = MkListOne(MkSpecifier(DOUBLE));
4033          }
4034          else if(dest.kind == floatType &&
4035             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
4036             source.kind == _BoolType || source.kind == doubleType))
4037          {
4038             specs = MkListOne(MkSpecifier(FLOAT));
4039          }
4040          else if(dest.kind == int64Type && (source.kind == int64Type || 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(INT64));
4046          }
4047          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
4048             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4049          {
4050             specs = MkList();
4051             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4052             ListAdd(specs, MkSpecifier(INT));
4053          }
4054          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || 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(SHORT));
4060          }
4061          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
4062             source.kind == floatType || source.kind == doubleType))
4063          {
4064             specs = MkList();
4065             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4066             ListAdd(specs, MkSpecifier(CHAR));
4067          }
4068          else
4069          {
4070             FreeType(source);
4071             FreeType(dest);
4072             if(backupSourceExpType)
4073             {
4074                // Failed to convert: revert previous exp type
4075                if(sourceExp.expType) FreeType(sourceExp.expType);
4076                sourceExp.expType = backupSourceExpType;
4077             }
4078             return false;
4079          }
4080       }
4081       else if(dest.kind == doubleType &&
4082          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
4083           source.kind == _BoolType || source.kind == charType))
4084       {
4085          specs = MkListOne(MkSpecifier(DOUBLE));
4086       }
4087       else if(dest.kind == floatType &&
4088          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4089       {
4090          specs = MkListOne(MkSpecifier(FLOAT));
4091       }
4092       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4093          (value == 1 || value == 0))
4094       {
4095          specs = MkList();
4096          ListAdd(specs, MkSpecifier(BOOL));
4097       }
4098       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4099          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
4100       {
4101          specs = MkList();
4102          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4103          ListAdd(specs, MkSpecifier(CHAR));
4104       }
4105       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
4106          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
4107       {
4108          specs = MkList();
4109          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4110          ListAdd(specs, MkSpecifier(SHORT));
4111       }
4112       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
4113       {
4114          specs = MkList();
4115          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4116          ListAdd(specs, MkSpecifier(INT));
4117       }
4118       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
4119       {
4120          specs = MkList();
4121          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4122          ListAdd(specs, MkSpecifier(INT64));
4123       }
4124       else if(dest.kind == enumType &&
4125          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4126       {
4127          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
4128       }
4129       else
4130       {
4131          FreeType(source);
4132          FreeType(dest);
4133          if(backupSourceExpType)
4134          {
4135             // Failed to convert: revert previous exp type
4136             if(sourceExp.expType) FreeType(sourceExp.expType);
4137             sourceExp.expType = backupSourceExpType;
4138          }
4139          return false;
4140       }
4141
4142       if(!flag && !sourceExp.opDestType)
4143       {
4144          Expression newExp { };
4145          *newExp = *sourceExp;
4146          newExp.prev = null;
4147          newExp.next = null;
4148          if(sourceExp.destType) sourceExp.destType.refCount++;
4149          if(sourceExp.expType)  sourceExp.expType.refCount++;
4150
4151          sourceExp.type = castExp;
4152          if(realDest.kind == classType)
4153          {
4154             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
4155             FreeList(specs, FreeSpecifier);
4156          }
4157          else
4158             sourceExp.cast.typeName = MkTypeName(specs, null);
4159          if(newExp.type == opExp)
4160          {
4161             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4162          }
4163          else
4164             sourceExp.cast.exp = newExp;
4165
4166          FreeType(sourceExp.expType);
4167          sourceExp.expType = null;
4168          ProcessExpressionType(sourceExp);
4169       }
4170       else
4171          FreeList(specs, FreeSpecifier);
4172
4173       FreeType(dest);
4174       FreeType(source);
4175       if(backupSourceExpType) FreeType(backupSourceExpType);
4176
4177       return true;
4178    }
4179    else
4180    {
4181       if(computedExp != sourceExp)
4182       {
4183          FreeExpression(computedExp);
4184          computedExp = sourceExp;
4185       }
4186
4187       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4188       if(sourceExp.type == identifierExp)
4189       {
4190          Identifier id = sourceExp.identifier;
4191          if(dest.kind == classType)
4192          {
4193             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4194             {
4195                Class _class = dest._class.registered;
4196                Class enumClass = eSystem_FindClass(privateModule, "enum");
4197                if(enumClass)
4198                {
4199                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4200                   {
4201                      NamedLink64 value;
4202                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4203                      for(value = e.values.first; value; value = value.next)
4204                      {
4205                         if(!strcmp(value.name, id.string))
4206                            break;
4207                      }
4208                      if(value)
4209                      {
4210                         FreeType(sourceExp.expType);
4211
4212                         sourceExp.isConstant = true;
4213                         sourceExp.expType = MkClassType(_class.fullName);
4214                         if(inCompiler || inPreCompiler || inDebugger)
4215                         {
4216                            FreeExpContents(sourceExp);
4217
4218                            sourceExp.type = constantExp;
4219                            if(_class.dataTypeString && (!strcmp(_class.dataTypeString, "int") || !strcmp(_class.dataTypeString, "int64") || !strcmp(_class.dataTypeString, "short") || !strcmp(_class.dataTypeString, "char"))) // _class cannot be null here!
4220                               sourceExp.constant = PrintInt64(value.data);
4221                            else
4222                               sourceExp.constant = PrintUInt64(value.data);
4223                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4224                         }
4225                         FreeType(dest);
4226                         return true;
4227                      }
4228                   }
4229                }
4230             }
4231          }
4232
4233          // Loop through all enum classes
4234          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4235          {
4236             FreeType(dest);
4237             return true;
4238          }
4239       }
4240       FreeType(dest);
4241    }
4242    return false;
4243 }
4244
4245 #define TERTIARY(o, name, m, t, p) \
4246    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4247    {                                                              \
4248       exp.type = constantExp;                                    \
4249       exp.string = p(op1.m ? op2.m : op3.m);                     \
4250       if(!exp.expType) \
4251          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4252       return true;                                                \
4253    }
4254
4255 #define BINARY(o, name, m, t, p) \
4256    static bool name(Expression exp, Operand op1, Operand op2)   \
4257    {                                                              \
4258       t value2 = op2.m;                                           \
4259       exp.type = constantExp;                                    \
4260       exp.string = p((t)(op1.m o value2));                     \
4261       if(!exp.expType) \
4262          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4263       return true;                                                \
4264    }
4265
4266 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4267    static bool name(Expression exp, Operand op1, Operand op2)   \
4268    {                                                              \
4269       t value2 = op2.m;                                           \
4270       exp.type = constantExp;                                    \
4271       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4272       if(!exp.expType) \
4273          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4274       return true;                                                \
4275    }
4276
4277 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4278    static bool name(Expression exp, Operand op1, Operand op2)   \
4279    {                                                              \
4280       t value2 = op2.m;                                           \
4281       exp.type = constantExp;                                    \
4282       exp.string = p(op1.m o value2);             \
4283       if(!exp.expType) \
4284          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4285       return true;                                                \
4286    }
4287
4288 #define UNARY(o, name, m, t, p) \
4289    static bool name(Expression exp, Operand op1)                \
4290    {                                                              \
4291       exp.type = constantExp;                                    \
4292       exp.string = p((t)(o op1.m));                                   \
4293       if(!exp.expType) \
4294          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4295       return true;                                                \
4296    }
4297
4298 #define OPERATOR_ALL(macro, o, name) \
4299    macro(o, Int##name, i, int, PrintInt) \
4300    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4301    macro(o, Int64##name, i64, int64, PrintInt64) \
4302    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4303    macro(o, Short##name, s, short, PrintShort) \
4304    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4305    macro(o, Char##name, c, char, PrintChar) \
4306    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4307    macro(o, Float##name, f, float, PrintFloat) \
4308    macro(o, Double##name, d, double, PrintDouble)
4309
4310 #define OPERATOR_INTTYPES(macro, o, name) \
4311    macro(o, Int##name, i, int, PrintInt) \
4312    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4313    macro(o, Int64##name, i64, int64, PrintInt64) \
4314    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4315    macro(o, Short##name, s, short, PrintShort) \
4316    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4317    macro(o, Char##name, c, char, PrintChar) \
4318    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4319
4320 #define OPERATOR_REALTYPES(macro, o, name) \
4321    macro(o, Float##name, f, float, PrintFloat) \
4322    macro(o, Double##name, d, double, PrintDouble)
4323
4324 // binary arithmetic
4325 OPERATOR_ALL(BINARY, +, Add)
4326 OPERATOR_ALL(BINARY, -, Sub)
4327 OPERATOR_ALL(BINARY, *, Mul)
4328 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4329 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4330 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4331
4332 // unary arithmetic
4333 OPERATOR_ALL(UNARY, -, Neg)
4334
4335 // unary arithmetic increment and decrement
4336 OPERATOR_ALL(UNARY, ++, Inc)
4337 OPERATOR_ALL(UNARY, --, Dec)
4338
4339 // binary arithmetic assignment
4340 OPERATOR_ALL(BINARY, =, Asign)
4341 OPERATOR_ALL(BINARY, +=, AddAsign)
4342 OPERATOR_ALL(BINARY, -=, SubAsign)
4343 OPERATOR_ALL(BINARY, *=, MulAsign)
4344 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4345 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4346 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4347
4348 // binary bitwise
4349 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4350 OPERATOR_INTTYPES(BINARY, |, BitOr)
4351 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4352 OPERATOR_INTTYPES(BINARY, <<, LShift)
4353 OPERATOR_INTTYPES(BINARY, >>, RShift)
4354
4355 // unary bitwise
4356 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4357
4358 // binary bitwise assignment
4359 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4360 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4361 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4362 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4363 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4364
4365 // unary logical negation
4366 OPERATOR_INTTYPES(UNARY, !, Not)
4367
4368 // binary logical equality
4369 OPERATOR_ALL(BINARY, ==, Equ)
4370 OPERATOR_ALL(BINARY, !=, Nqu)
4371
4372 // binary logical
4373 OPERATOR_ALL(BINARY, &&, And)
4374 OPERATOR_ALL(BINARY, ||, Or)
4375
4376 // binary logical relational
4377 OPERATOR_ALL(BINARY, >, Grt)
4378 OPERATOR_ALL(BINARY, <, Sma)
4379 OPERATOR_ALL(BINARY, >=, GrtEqu)
4380 OPERATOR_ALL(BINARY, <=, SmaEqu)
4381
4382 // tertiary condition operator
4383 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4384
4385 //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
4386 #define OPERATOR_TABLE_ALL(name, type) \
4387     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4388                           type##Neg, \
4389                           type##Inc, type##Dec, \
4390                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4391                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4392                           type##BitNot, \
4393                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4394                           type##Not, \
4395                           type##Equ, type##Nqu, \
4396                           type##And, type##Or, \
4397                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4398                         }; \
4399
4400 #define OPERATOR_TABLE_INTTYPES(name, type) \
4401     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4402                           type##Neg, \
4403                           type##Inc, type##Dec, \
4404                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4405                           null, null, null, null, null, \
4406                           null, \
4407                           null, null, null, null, null, \
4408                           null, \
4409                           type##Equ, type##Nqu, \
4410                           type##And, type##Or, \
4411                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4412                         }; \
4413
4414 OPERATOR_TABLE_ALL(int, Int)
4415 OPERATOR_TABLE_ALL(uint, UInt)
4416 OPERATOR_TABLE_ALL(int64, Int64)
4417 OPERATOR_TABLE_ALL(uint64, UInt64)
4418 OPERATOR_TABLE_ALL(short, Short)
4419 OPERATOR_TABLE_ALL(ushort, UShort)
4420 OPERATOR_TABLE_INTTYPES(float, Float)
4421 OPERATOR_TABLE_INTTYPES(double, Double)
4422 OPERATOR_TABLE_ALL(char, Char)
4423 OPERATOR_TABLE_ALL(uchar, UChar)
4424
4425 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4426 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4427 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4428 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4429 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4430 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4431 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4432 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4433
4434 public void ReadString(char * output,  char * string)
4435 {
4436    int len = strlen(string);
4437    int c,d = 0;
4438    bool quoted = false, escaped = false;
4439    for(c = 0; c<len; c++)
4440    {
4441       char ch = string[c];
4442       if(escaped)
4443       {
4444          switch(ch)
4445          {
4446             case 'n': output[d] = '\n'; break;
4447             case 't': output[d] = '\t'; break;
4448             case 'a': output[d] = '\a'; break;
4449             case 'b': output[d] = '\b'; break;
4450             case 'f': output[d] = '\f'; break;
4451             case 'r': output[d] = '\r'; break;
4452             case 'v': output[d] = '\v'; break;
4453             case '\\': output[d] = '\\'; break;
4454             case '\"': output[d] = '\"'; break;
4455             case '\'': output[d] = '\''; break;
4456             default: output[d] = ch;
4457          }
4458          d++;
4459          escaped = false;
4460       }
4461       else
4462       {
4463          if(ch == '\"')
4464             quoted ^= true;
4465          else if(quoted)
4466          {
4467             if(ch == '\\')
4468                escaped = true;
4469             else
4470                output[d++] = ch;
4471          }
4472       }
4473    }
4474    output[d] = '\0';
4475 }
4476
4477 // String Unescape Copy
4478
4479 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4480 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4481 public int UnescapeString(char * d, char * s, int len)
4482 {
4483    int j = 0, k = 0;
4484    char ch;
4485    while(j < len && (ch = s[j]))
4486    {
4487       switch(ch)
4488       {
4489          case '\\':
4490             switch((ch = s[++j]))
4491             {
4492                case 'n': d[k] = '\n'; break;
4493                case 't': d[k] = '\t'; break;
4494                case 'a': d[k] = '\a'; break;
4495                case 'b': d[k] = '\b'; break;
4496                case 'f': d[k] = '\f'; break;
4497                case 'r': d[k] = '\r'; break;
4498                case 'v': d[k] = '\v'; break;
4499                case '\\': d[k] = '\\'; break;
4500                case '\"': d[k] = '\"'; break;
4501                case '\'': d[k] = '\''; break;
4502                default: d[k] = '\\'; d[k] = ch;
4503             }
4504             break;
4505          default:
4506             d[k] = ch;
4507       }
4508       j++, k++;
4509    }
4510    d[k] = '\0';
4511    return k;
4512 }
4513
4514 public char * OffsetEscapedString(char * s, int len, int offset)
4515 {
4516    char ch;
4517    int j = 0, k = 0;
4518    while(j < len && k < offset && (ch = s[j]))
4519    {
4520       if(ch == '\\') ++j;
4521       j++, k++;
4522    }
4523    return (k == offset) ? s + j : null;
4524 }
4525
4526 public Operand GetOperand(Expression exp)
4527 {
4528    Operand op { };
4529    Type type = exp.expType;
4530    if(type)
4531    {
4532       while(type.kind == classType &&
4533          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4534       {
4535          if(!type._class.registered.dataType)
4536             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4537          type = type._class.registered.dataType;
4538
4539       }
4540       if(exp.type == stringExp && op.kind == pointerType)
4541       {
4542          op.ui64 = (uint64)(uintptr)exp.string;
4543          op.kind = pointerType;
4544          op.ops = uint64Ops;
4545       }
4546       else if(exp.isConstant && exp.type == constantExp)
4547       {
4548          op.kind = type.kind;
4549          op.type = type;
4550
4551          switch(op.kind)
4552          {
4553             case _BoolType:
4554             case charType:
4555             {
4556                if(exp.constant[0] == '\'')
4557                {
4558                   op.c = exp.constant[1];
4559                   op.ops = charOps;
4560                }
4561                else if(type.isSigned)
4562                {
4563                   op.c = (char)strtol(exp.constant, null, 0);
4564                   op.ops = charOps;
4565                }
4566                else
4567                {
4568                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4569                   op.ops = ucharOps;
4570                }
4571                break;
4572             }
4573             case shortType:
4574                if(type.isSigned)
4575                {
4576                   op.s = (short)strtol(exp.constant, null, 0);
4577                   op.ops = shortOps;
4578                }
4579                else
4580                {
4581                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4582                   op.ops = ushortOps;
4583                }
4584                break;
4585             case intType:
4586             case longType:
4587                if(type.isSigned)
4588                {
4589                   op.i = (int)strtol(exp.constant, null, 0);
4590                   op.ops = intOps;
4591                }
4592                else
4593                {
4594                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4595                   op.ops = uintOps;
4596                }
4597                op.kind = intType;
4598                break;
4599             case int64Type:
4600                if(type.isSigned)
4601                {
4602                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4603                   op.ops = int64Ops;
4604                }
4605                else
4606                {
4607                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4608                   op.ops = uint64Ops;
4609                }
4610                op.kind = int64Type;
4611                break;
4612             case intPtrType:
4613                if(type.isSigned)
4614                {
4615                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4616                   op.ops = int64Ops;
4617                }
4618                else
4619                {
4620                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4621                   op.ops = uint64Ops;
4622                }
4623                op.kind = int64Type;
4624                break;
4625             case intSizeType:
4626                if(type.isSigned)
4627                {
4628                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4629                   op.ops = int64Ops;
4630                }
4631                else
4632                {
4633                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4634                   op.ops = uint64Ops;
4635                }
4636                op.kind = int64Type;
4637                break;
4638             case floatType:
4639                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4640                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4641                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4642                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4643                else
4644                   op.f = (float)strtod(exp.constant, null);
4645                op.ops = floatOps;
4646                break;
4647             case doubleType:
4648                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4649                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4650                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4651                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4652                else
4653                   op.d = (double)strtod(exp.constant, null);
4654                op.ops = doubleOps;
4655                break;
4656             //case classType:    For when we have operator overloading...
4657             // Pointer additions
4658             //case functionType:
4659             case arrayType:
4660             case pointerType:
4661             case classType:
4662                op.ui64 = _strtoui64(exp.constant, null, 0);
4663                op.kind = pointerType;
4664                op.ops = uint64Ops;
4665                // op.ptrSize =
4666                break;
4667          }
4668       }
4669    }
4670    return op;
4671 }
4672
4673 static int64 GetEnumValue(Class _class, void * ptr)
4674 {
4675    int64 v = 0;
4676    switch(_class.typeSize)
4677    {
4678       case 8:
4679          if(!strcmp(_class.dataTypeString, "uint64"))
4680             v = (int64)*(uint64 *)ptr;
4681          else
4682             v = (int64)*(int64 *)ptr;
4683          break;
4684       case 4:
4685          if(!strcmp(_class.dataTypeString, "uint"))
4686             v = (int64)*(uint *)ptr;
4687          else
4688             v = (int64)*(int *)ptr;
4689          break;
4690       case 2:
4691          if(!strcmp(_class.dataTypeString, "uint16"))
4692             v = (int64)*(uint16 *)ptr;
4693          else
4694             v = (int64)*(short *)ptr;
4695          break;
4696       case 1:
4697          if(!strcmp(_class.dataTypeString, "byte"))
4698             v = (int64)*(byte *)ptr;
4699          else
4700             v = (int64)*(char *)ptr;
4701          break;
4702    }
4703    return v;
4704 }
4705
4706 static __attribute__((unused)) void UnusedFunction()
4707 {
4708    int a;
4709    a.OnGetString(0,0,0);
4710 }
4711 default:
4712 extern int __ecereVMethodID_class_OnGetString;
4713 public:
4714
4715 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4716 {
4717    DataMember dataMember;
4718    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4719    {
4720       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4721          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4722       else
4723       {
4724          Expression exp { };
4725          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4726          Type type;
4727          void * ptr = inst.data + dataMember.offset + offset;
4728          char * result = null;
4729          exp.loc = member.loc = inst.loc;
4730          ((Identifier)member.identifiers->first).loc = inst.loc;
4731
4732          if(!dataMember.dataType)
4733             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4734          type = dataMember.dataType;
4735          if(type.kind == classType)
4736          {
4737             Class _class = type._class.registered;
4738             if(_class.type == enumClass)
4739             {
4740                Class enumClass = eSystem_FindClass(privateModule, "enum");
4741                if(enumClass)
4742                {
4743                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4744                   NamedLink64 item;
4745                   for(item = e.values.first; item; item = item.next)
4746                   {
4747                      if(item.data == GetEnumValue(_class, ptr))
4748                      {
4749                         result = item.name;
4750                         break;
4751                      }
4752                   }
4753                   if(result)
4754                   {
4755                      exp.identifier = MkIdentifier(result);
4756                      exp.type = identifierExp;
4757                      exp.destType = MkClassType(_class.fullName);
4758                      ProcessExpressionType(exp);
4759                   }
4760                }
4761             }
4762             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4763             {
4764                if(!_class.dataType)
4765                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4766                type = _class.dataType;
4767             }
4768          }
4769          if(!result)
4770          {
4771             switch(type.kind)
4772             {
4773                case floatType:
4774                {
4775                   FreeExpContents(exp);
4776
4777                   exp.constant = PrintFloat(*(float*)ptr);
4778                   exp.type = constantExp;
4779                   break;
4780                }
4781                case doubleType:
4782                {
4783                   FreeExpContents(exp);
4784
4785                   exp.constant = PrintDouble(*(double*)ptr);
4786                   exp.type = constantExp;
4787                   break;
4788                }
4789                case intType:
4790                {
4791                   FreeExpContents(exp);
4792
4793                   exp.constant = PrintInt(*(int*)ptr);
4794                   exp.type = constantExp;
4795                   break;
4796                }
4797                case int64Type:
4798                {
4799                   FreeExpContents(exp);
4800
4801                   exp.constant = PrintInt64(*(int64*)ptr);
4802                   exp.type = constantExp;
4803                   break;
4804                }
4805                case intPtrType:
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                case intSizeType:
4814                {
4815                   FreeExpContents(exp);
4816                   // TODO: This should probably use proper type
4817                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4818                   exp.type = constantExp;
4819                   break;
4820                }
4821                default:
4822                   Compiler_Error($"Unhandled type populating instance\n");
4823             }
4824          }
4825          ListAdd(memberList, member);
4826       }
4827
4828       if(parentDataMember.type == unionMember)
4829          break;
4830    }
4831 }
4832
4833 void PopulateInstance(Instantiation inst)
4834 {
4835    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4836    Class _class = classSym.registered;
4837    DataMember dataMember;
4838    OldList * memberList = MkList();
4839    // Added this check and ->Add to prevent memory leaks on bad code
4840    if(!inst.members)
4841       inst.members = MkListOne(MkMembersInitList(memberList));
4842    else
4843       inst.members->Add(MkMembersInitList(memberList));
4844    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4845    {
4846       if(!dataMember.isProperty)
4847       {
4848          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4849             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4850          else
4851          {
4852             Expression exp { };
4853             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4854             Type type;
4855             void * ptr = inst.data + dataMember.offset;
4856             char * result = null;
4857
4858             exp.loc = member.loc = inst.loc;
4859             ((Identifier)member.identifiers->first).loc = inst.loc;
4860
4861             if(!dataMember.dataType)
4862                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4863             type = dataMember.dataType;
4864             if(type.kind == classType)
4865             {
4866                Class _class = type._class.registered;
4867                if(_class.type == enumClass)
4868                {
4869                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4870                   if(enumClass)
4871                   {
4872                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4873                      NamedLink64 item;
4874                      for(item = e.values.first; item; item = item.next)
4875                      {
4876                         if(item.data == GetEnumValue(_class, ptr))
4877                         {
4878                            result = item.name;
4879                            break;
4880                         }
4881                      }
4882                   }
4883                   if(result)
4884                   {
4885                      exp.identifier = MkIdentifier(result);
4886                      exp.type = identifierExp;
4887                      exp.destType = MkClassType(_class.fullName);
4888                      ProcessExpressionType(exp);
4889                   }
4890                }
4891                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4892                {
4893                   if(!_class.dataType)
4894                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4895                   type = _class.dataType;
4896                }
4897             }
4898             if(!result)
4899             {
4900                switch(type.kind)
4901                {
4902                   case floatType:
4903                   {
4904                      exp.constant = PrintFloat(*(float*)ptr);
4905                      exp.type = constantExp;
4906                      break;
4907                   }
4908                   case doubleType:
4909                   {
4910                      exp.constant = PrintDouble(*(double*)ptr);
4911                      exp.type = constantExp;
4912                      break;
4913                   }
4914                   case intType:
4915                   {
4916                      exp.constant = PrintInt(*(int*)ptr);
4917                      exp.type = constantExp;
4918                      break;
4919                   }
4920                   case int64Type:
4921                   {
4922                      exp.constant = PrintInt64(*(int64*)ptr);
4923                      exp.type = constantExp;
4924                      break;
4925                   }
4926                   case intPtrType:
4927                   {
4928                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4929                      exp.type = constantExp;
4930                      break;
4931                   }
4932                   default:
4933                      Compiler_Error($"Unhandled type populating instance\n");
4934                }
4935             }
4936             ListAdd(memberList, member);
4937          }
4938       }
4939    }
4940 }
4941
4942 void ComputeInstantiation(Expression exp)
4943 {
4944    Instantiation inst = exp.instance;
4945    MembersInit members;
4946    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4947    Class _class = classSym ? classSym.registered : null;
4948    DataMember curMember = null;
4949    Class curClass = null;
4950    DataMember subMemberStack[256];
4951    int subMemberStackPos = 0;
4952    uint64 bits = 0;
4953
4954    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4955    {
4956       // Don't recompute the instantiation...
4957       // Non Simple classes will have become constants by now
4958       if(inst.data)
4959          return;
4960
4961       if(_class.type == normalClass || _class.type == noHeadClass)
4962       {
4963          inst.data = (byte *)eInstance_New(_class);
4964          if(_class.type == normalClass)
4965             ((Instance)inst.data)._refCount++;
4966       }
4967       else
4968          inst.data = new0 byte[_class.structSize];
4969    }
4970
4971    if(inst.members)
4972    {
4973       for(members = inst.members->first; members; members = members.next)
4974       {
4975          switch(members.type)
4976          {
4977             case dataMembersInit:
4978             {
4979                if(members.dataMembers)
4980                {
4981                   MemberInit member;
4982                   for(member = members.dataMembers->first; member; member = member.next)
4983                   {
4984                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4985                      bool found = false;
4986
4987                      Property prop = null;
4988                      DataMember dataMember = null;
4989                      uint dataMemberOffset;
4990
4991                      if(!ident)
4992                      {
4993                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4994                         if(curMember)
4995                         {
4996                            if(curMember.isProperty)
4997                               prop = (Property)curMember;
4998                            else
4999                            {
5000                               dataMember = curMember;
5001
5002                               // CHANGED THIS HERE
5003                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
5004
5005                               // 2013/17/29 -- It seems that this was missing here!
5006                               if(_class.type == normalClass)
5007                                  dataMemberOffset += _class.base.structSize;
5008                               // dataMemberOffset = dataMember.offset;
5009                            }
5010                            found = true;
5011                         }
5012                      }
5013                      else
5014                      {
5015                         prop = eClass_FindProperty(_class, ident.string, privateModule);
5016                         if(prop)
5017                         {
5018                            found = true;
5019                            if(prop.memberAccess == publicAccess)
5020                            {
5021                               curMember = (DataMember)prop;
5022                               curClass = prop._class;
5023                            }
5024                         }
5025                         else
5026                         {
5027                            DataMember _subMemberStack[256];
5028                            int _subMemberStackPos = 0;
5029
5030                            // FILL MEMBER STACK
5031                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
5032
5033                            if(dataMember)
5034                            {
5035                               found = true;
5036                               if(dataMember.memberAccess == publicAccess)
5037                               {
5038                                  curMember = dataMember;
5039                                  curClass = dataMember._class;
5040                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
5041                                  subMemberStackPos = _subMemberStackPos;
5042                               }
5043                            }
5044                         }
5045                      }
5046
5047                      if(found && member.initializer && member.initializer.type == expInitializer)
5048                      {
5049                         Expression value = member.initializer.exp;
5050                         Type type = null;
5051                         bool deepMember = false;
5052                         if(prop)
5053                         {
5054                            type = prop.dataType;
5055                         }
5056                         else if(dataMember)
5057                         {
5058                            if(!dataMember.dataType)
5059                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
5060
5061                            type = dataMember.dataType;
5062                         }
5063
5064                         if(ident && ident.next)
5065                         {
5066                            deepMember = true;
5067
5068                            // for(; ident && type; ident = ident.next)
5069                            for(ident = ident.next; ident && type; ident = ident.next)
5070                            {
5071                               if(type.kind == classType)
5072                               {
5073                                  prop = eClass_FindProperty(type._class.registered,
5074                                     ident.string, privateModule);
5075                                  if(prop)
5076                                     type = prop.dataType;
5077                                  else
5078                                  {
5079                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
5080                                        ident.string, &dataMemberOffset, privateModule, null, null);
5081                                     if(dataMember)
5082                                        type = dataMember.dataType;
5083                                  }
5084                               }
5085                               else if(type.kind == structType || type.kind == unionType)
5086                               {
5087                                  Type memberType;
5088                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
5089                                  {
5090                                     if(!strcmp(memberType.name, ident.string))
5091                                     {
5092                                        type = memberType;
5093                                        break;
5094                                     }
5095                                  }
5096                               }
5097                            }
5098                         }
5099                         if(value)
5100                         {
5101                            FreeType(value.destType);
5102                            value.destType = type;
5103                            if(type) type.refCount++;
5104                            ComputeExpression(value);
5105                         }
5106                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
5107                         {
5108                            if(type.kind == classType)
5109                            {
5110                               Class _class = type._class.registered;
5111                               if(_class.type == bitClass || _class.type == unitClass ||
5112                                  _class.type == enumClass)
5113                               {
5114                                  if(!_class.dataType)
5115                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5116                                  type = _class.dataType;
5117                               }
5118                            }
5119
5120                            if(dataMember)
5121                            {
5122                               void * ptr = inst.data + dataMemberOffset;
5123
5124                               if(value.type == constantExp)
5125                               {
5126                                  switch(type.kind)
5127                                  {
5128                                     case intType:
5129                                     {
5130                                        GetInt(value, (int*)ptr);
5131                                        break;
5132                                     }
5133                                     case int64Type:
5134                                     {
5135                                        GetInt64(value, (int64*)ptr);
5136                                        break;
5137                                     }
5138                                     case intPtrType:
5139                                     {
5140                                        GetIntPtr(value, (intptr*)ptr);
5141                                        break;
5142                                     }
5143                                     case intSizeType:
5144                                     {
5145                                        GetIntSize(value, (intsize*)ptr);
5146                                        break;
5147                                     }
5148                                     case floatType:
5149                                     {
5150                                        GetFloat(value, (float*)ptr);
5151                                        break;
5152                                     }
5153                                     case doubleType:
5154                                     {
5155                                        GetDouble(value, (double *)ptr);
5156                                        break;
5157                                     }
5158                                  }
5159                               }
5160                               else if(value.type == instanceExp)
5161                               {
5162                                  if(type.kind == classType)
5163                                  {
5164                                     Class _class = type._class.registered;
5165                                     if(_class.type == structClass)
5166                                     {
5167                                        ComputeTypeSize(type);
5168                                        if(value.instance.data)
5169                                           memcpy(ptr, value.instance.data, type.size);
5170                                     }
5171                                  }
5172                               }
5173                            }
5174                            else if(prop && prop.Set != (void *)(intptr)1)
5175                            {
5176                               if(value.type == instanceExp && value.instance.data)
5177                               {
5178                                  if(type.kind == classType)
5179                                  {
5180                                     Class _class = type._class.registered;
5181                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5182                                     {
5183                                        void (*Set)(void *, void *) = (void *)prop.Set;
5184                                        Set(inst.data, value.instance.data);
5185                                        PopulateInstance(inst);
5186                                     }
5187                                  }
5188                               }
5189                               else if(value.type == constantExp)
5190                               {
5191                                  switch(type.kind)
5192                                  {
5193                                     case doubleType:
5194                                     {
5195                                        void (*Set)(void *, double) = (void *)prop.Set;
5196                                        Set(inst.data, strtod(value.constant, null) );
5197                                        break;
5198                                     }
5199                                     case floatType:
5200                                     {
5201                                        void (*Set)(void *, float) = (void *)prop.Set;
5202                                        Set(inst.data, (float)(strtod(value.constant, null)));
5203                                        break;
5204                                     }
5205                                     case intType:
5206                                     {
5207                                        void (*Set)(void *, int) = (void *)prop.Set;
5208                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5209                                        break;
5210                                     }
5211                                     case int64Type:
5212                                     {
5213                                        void (*Set)(void *, int64) = (void *)prop.Set;
5214                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5215                                        break;
5216                                     }
5217                                     case intPtrType:
5218                                     {
5219                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5220                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5221                                        break;
5222                                     }
5223                                     case intSizeType:
5224                                     {
5225                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5226                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5227                                        break;
5228                                     }
5229                                  }
5230                               }
5231                               else if(value.type == stringExp)
5232                               {
5233                                  char temp[1024];
5234                                  ReadString(temp, value.string);
5235                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5236                               }
5237                            }
5238                         }
5239                         else if(!deepMember && type && _class.type == unitClass)
5240                         {
5241                            if(prop)
5242                            {
5243                               // Only support converting units to units for now...
5244                               if(value.type == constantExp)
5245                               {
5246                                  if(type.kind == classType)
5247                                  {
5248                                     Class _class = type._class.registered;
5249                                     if(_class.type == unitClass)
5250                                     {
5251                                        if(!_class.dataType)
5252                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5253                                        type = _class.dataType;
5254                                     }
5255                                  }
5256                                  // TODO: Assuming same base type for units...
5257                                  switch(type.kind)
5258                                  {
5259                                     case floatType:
5260                                     {
5261                                        float fValue;
5262                                        float (*Set)(float) = (void *)prop.Set;
5263                                        GetFloat(member.initializer.exp, &fValue);
5264                                        exp.constant = PrintFloat(Set(fValue));
5265                                        exp.type = constantExp;
5266                                        break;
5267                                     }
5268                                     case doubleType:
5269                                     {
5270                                        double dValue;
5271                                        double (*Set)(double) = (void *)prop.Set;
5272                                        GetDouble(member.initializer.exp, &dValue);
5273                                        exp.constant = PrintDouble(Set(dValue));
5274                                        exp.type = constantExp;
5275                                        break;
5276                                     }
5277                                  }
5278                               }
5279                            }
5280                         }
5281                         else if(!deepMember && type && _class.type == bitClass)
5282                         {
5283                            if(prop)
5284                            {
5285                               if(value.type == instanceExp && value.instance.data)
5286                               {
5287                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5288                                  bits = Set(value.instance.data);
5289                               }
5290                               else if(value.type == constantExp)
5291                               {
5292                               }
5293                            }
5294                            else if(dataMember)
5295                            {
5296                               BitMember bitMember = (BitMember) dataMember;
5297                               Type type;
5298                               uint64 part = 0;
5299                               bits = (bits & ~bitMember.mask);
5300                               if(!bitMember.dataType)
5301                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5302                               type = bitMember.dataType;
5303                               if(type.kind == classType && type._class && type._class.registered)
5304                               {
5305                                  if(!type._class.registered.dataType)
5306                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5307                                  type = type._class.registered.dataType;
5308                               }
5309                               switch(type.kind)
5310                               {
5311                                  case _BoolType:
5312                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5313                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (short *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5314                                  case intType:
5315                                  case longType:       { uint v; type.isSigned ? GetInt(value, (int *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5316                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (int64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5317                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (intptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5318                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (intsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5319                               }
5320                               bits |= part << bitMember.pos;
5321                            }
5322                         }
5323                      }
5324                      else
5325                      {
5326                         if(_class && _class.type == unitClass)
5327                         {
5328                            ComputeExpression(member.initializer.exp);
5329                            exp.constant = member.initializer.exp.constant;
5330                            exp.type = constantExp;
5331
5332                            member.initializer.exp.constant = null;
5333                         }
5334                      }
5335                   }
5336                }
5337                break;
5338             }
5339          }
5340       }
5341    }
5342    if(_class && _class.type == bitClass)
5343    {
5344       exp.constant = PrintHexUInt(bits);
5345       exp.type = constantExp;
5346    }
5347    if(exp.type != instanceExp)
5348    {
5349       FreeInstance(inst);
5350    }
5351 }
5352
5353 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5354 {
5355    bool result = false;
5356    switch(kind)
5357    {
5358       case shortType:
5359          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5360             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5361          break;
5362       case intType:
5363       case longType:
5364          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5365             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5366          break;
5367       case int64Type:
5368          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5369             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5370             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5371          break;
5372       case floatType:
5373          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5374             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5375             result = GetOpFloat(op, &op.f);
5376          break;
5377       case doubleType:
5378          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5379             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5380             result = GetOpDouble(op, &op.d);
5381          break;
5382       case pointerType:
5383          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5384             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5385             result = GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5386          break;
5387       case enumType:
5388          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5389             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5390             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5391          break;
5392       case intPtrType:
5393          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5394             result = isSigned ? GetOpInt64 /*GetOpIntPtr*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5395          break;
5396       case intSizeType:
5397          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5398             result = isSigned ? GetOpInt64 /*GetOpIntSize*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntSize*/(op, &op.ui64);
5399          break;
5400    }
5401    return result;
5402 }
5403
5404 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5405 {
5406    if(exp.op.op == SIZEOF)
5407    {
5408       FreeExpContents(exp);
5409       exp.type = constantExp;
5410       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5411    }
5412    else
5413    {
5414       if(!exp.op.exp1)
5415       {
5416          switch(exp.op.op)
5417          {
5418             // unary arithmetic
5419             case '+':
5420             {
5421                // Provide default unary +
5422                Expression exp2 = exp.op.exp2;
5423                exp.op.exp2 = null;
5424                FreeExpContents(exp);
5425                FreeType(exp.expType);
5426                FreeType(exp.destType);
5427                *exp = *exp2;
5428                delete exp2;
5429                break;
5430             }
5431             case '-':
5432                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5433                break;
5434             // unary arithmetic increment and decrement
5435                   //OPERATOR_ALL(UNARY, ++, Inc)
5436                   //OPERATOR_ALL(UNARY, --, Dec)
5437             // unary bitwise
5438             case '~':
5439                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5440                break;
5441             // unary logical negation
5442             case '!':
5443                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5444                break;
5445          }
5446       }
5447       else
5448       {
5449          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5450          {
5451             if(Promote(op2, op1.kind, op1.type.isSigned))
5452                op2.kind = op1.kind, op2.ops = op1.ops;
5453             else if(Promote(op1, op2.kind, op2.type.isSigned))
5454                op1.kind = op2.kind, op1.ops = op2.ops;
5455          }
5456          switch(exp.op.op)
5457          {
5458             // binary arithmetic
5459             case '+':
5460                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5461                break;
5462             case '-':
5463                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5464                break;
5465             case '*':
5466                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5467                break;
5468             case '/':
5469                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5470                break;
5471             case '%':
5472                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5473                break;
5474             // binary arithmetic assignment
5475                   //OPERATOR_ALL(BINARY, =, Asign)
5476                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5477                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5478                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5479                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5480                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5481             // binary bitwise
5482             case '&':
5483                if(exp.op.exp2)
5484                {
5485                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5486                }
5487                break;
5488             case '|':
5489                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5490                break;
5491             case '^':
5492                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5493                break;
5494             case LEFT_OP:
5495                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5496                break;
5497             case RIGHT_OP:
5498                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5499                break;
5500             // binary bitwise assignment
5501                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5502                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5503                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5504                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5505                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5506             // binary logical equality
5507             case EQ_OP:
5508                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5509                break;
5510             case NE_OP:
5511                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5512                break;
5513             // binary logical
5514             case AND_OP:
5515                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5516                break;
5517             case OR_OP:
5518                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5519                break;
5520             // binary logical relational
5521             case '>':
5522                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5523                break;
5524             case '<':
5525                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5526                break;
5527             case GE_OP:
5528                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5529                break;
5530             case LE_OP:
5531                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5532                break;
5533          }
5534       }
5535    }
5536 }
5537
5538 void ComputeExpression(Expression exp)
5539 {
5540 #ifdef _DEBUG
5541    char expString[10240];
5542    expString[0] = '\0';
5543    PrintExpression(exp, expString);
5544 #endif
5545
5546    switch(exp.type)
5547    {
5548       case identifierExp:
5549       {
5550          Identifier id = exp.identifier;
5551          if(id && exp.isConstant && !inCompiler && !inPreCompiler && !inDebugger)
5552          {
5553             Class c = (exp.expType && exp.expType.kind == classType && exp.expType._class) ? exp.expType._class.registered : null;
5554             if(c && c.type == enumClass)
5555             {
5556                Class enumClass = eSystem_FindClass(privateModule, "enum");
5557                if(enumClass)
5558                {
5559                   NamedLink64 value;
5560                   EnumClassData e = ACCESS_CLASSDATA(c, enumClass);
5561                   for(value = e.values.first; value; value = value.next)
5562                   {
5563                      if(!strcmp(value.name, id.string))
5564                         break;
5565                   }
5566                   if(value)
5567                   {
5568                      const String dts = c.dataTypeString;
5569                      FreeExpContents(exp);
5570                      exp.type = constantExp;
5571                      exp.constant = (dts && (!strcmp(dts, "int") || !strcmp(dts, "int64") || !strcmp(dts, "short") || !strcmp(dts, "char"))) ? PrintInt64(value.data) : PrintUInt64(value.data);
5572                   }
5573                }
5574             }
5575          }
5576          break;
5577       }
5578       case instanceExp:
5579       {
5580          ComputeInstantiation(exp);
5581          break;
5582       }
5583       /*
5584       case constantExp:
5585          break;
5586       */
5587       case opExp:
5588       {
5589          Expression exp1, exp2 = null;
5590          Operand op1 { };
5591          Operand op2 { };
5592
5593          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5594          if(exp.op.exp2)
5595          {
5596             Expression e = exp.op.exp2;
5597
5598             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5599             {
5600                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5601                {
5602                   if(e.type == extensionCompoundExp)
5603                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5604                   else
5605                      e = e.list->last;
5606                }
5607             }
5608             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5609             {
5610                if(e.type == stringExp && e.string)
5611                {
5612                   char * string = e.string;
5613                   int len = strlen(string);
5614                   char * tmp = new char[len-2+1];
5615                   len = UnescapeString(tmp, string + 1, len - 2);
5616                   delete tmp;
5617                   FreeExpContents(exp);
5618                   exp.type = constantExp;
5619                   exp.constant = PrintUInt(len + 1);
5620                }
5621                else
5622                {
5623                   Type type = e.expType;
5624                   type.refCount++;
5625                   FreeExpContents(exp);
5626                   exp.type = constantExp;
5627                   exp.constant = PrintUInt(ComputeTypeSize(type));
5628                   FreeType(type);
5629                }
5630                break;
5631             }
5632             else
5633                ComputeExpression(exp.op.exp2);
5634          }
5635          if(exp.op.exp1)
5636          {
5637             ComputeExpression(exp.op.exp1);
5638             exp1 = exp.op.exp1;
5639             exp2 = exp.op.exp2;
5640             op1 = GetOperand(exp1);
5641             if(op1.type) op1.type.refCount++;
5642             if(exp2)
5643             {
5644                op2 = GetOperand(exp2);
5645                if(op2.type) op2.type.refCount++;
5646             }
5647          }
5648          else
5649          {
5650             exp1 = exp.op.exp2;
5651             op1 = GetOperand(exp1);
5652             if(op1.type) op1.type.refCount++;
5653          }
5654
5655          CallOperator(exp, exp1, exp2, op1, op2);
5656          /*
5657          switch(exp.op.op)
5658          {
5659             // Unary operators
5660             case '&':
5661                // Also binary
5662                if(exp.op.exp1 && exp.op.exp2)
5663                {
5664                   // Binary And
5665                   if(op1.ops.BitAnd)
5666                   {
5667                      FreeExpContents(exp);
5668                      op1.ops.BitAnd(exp, op1, op2);
5669                   }
5670                }
5671                break;
5672             case '*':
5673                if(exp.op.exp1)
5674                {
5675                   if(op1.ops.Mul)
5676                   {
5677                      FreeExpContents(exp);
5678                      op1.ops.Mul(exp, op1, op2);
5679                   }
5680                }
5681                break;
5682             case '+':
5683                if(exp.op.exp1)
5684                {
5685                   if(op1.ops.Add)
5686                   {
5687                      FreeExpContents(exp);
5688                      op1.ops.Add(exp, op1, op2);
5689                   }
5690                }
5691                else
5692                {
5693                   // Provide default unary +
5694                   Expression exp2 = exp.op.exp2;
5695                   exp.op.exp2 = null;
5696                   FreeExpContents(exp);
5697                   FreeType(exp.expType);
5698                   FreeType(exp.destType);
5699
5700                   *exp = *exp2;
5701                   delete exp2;
5702                }
5703                break;
5704             case '-':
5705                if(exp.op.exp1)
5706                {
5707                   if(op1.ops.Sub)
5708                   {
5709                      FreeExpContents(exp);
5710                      op1.ops.Sub(exp, op1, op2);
5711                   }
5712                }
5713                else
5714                {
5715                   if(op1.ops.Neg)
5716                   {
5717                      FreeExpContents(exp);
5718                      op1.ops.Neg(exp, op1);
5719                   }
5720                }
5721                break;
5722             case '~':
5723                if(op1.ops.BitNot)
5724                {
5725                   FreeExpContents(exp);
5726                   op1.ops.BitNot(exp, op1);
5727                }
5728                break;
5729             case '!':
5730                if(op1.ops.Not)
5731                {
5732                   FreeExpContents(exp);
5733                   op1.ops.Not(exp, op1);
5734                }
5735                break;
5736             // Binary only operators
5737             case '/':
5738                if(op1.ops.Div)
5739                {
5740                   FreeExpContents(exp);
5741                   op1.ops.Div(exp, op1, op2);
5742                }
5743                break;
5744             case '%':
5745                if(op1.ops.Mod)
5746                {
5747                   FreeExpContents(exp);
5748                   op1.ops.Mod(exp, op1, op2);
5749                }
5750                break;
5751             case LEFT_OP:
5752                break;
5753             case RIGHT_OP:
5754                break;
5755             case '<':
5756                if(exp.op.exp1)
5757                {
5758                   if(op1.ops.Sma)
5759                   {
5760                      FreeExpContents(exp);
5761                      op1.ops.Sma(exp, op1, op2);
5762                   }
5763                }
5764                break;
5765             case '>':
5766                if(exp.op.exp1)
5767                {
5768                   if(op1.ops.Grt)
5769                   {
5770                      FreeExpContents(exp);
5771                      op1.ops.Grt(exp, op1, op2);
5772                   }
5773                }
5774                break;
5775             case LE_OP:
5776                if(exp.op.exp1)
5777                {
5778                   if(op1.ops.SmaEqu)
5779                   {
5780                      FreeExpContents(exp);
5781                      op1.ops.SmaEqu(exp, op1, op2);
5782                   }
5783                }
5784                break;
5785             case GE_OP:
5786                if(exp.op.exp1)
5787                {
5788                   if(op1.ops.GrtEqu)
5789                   {
5790                      FreeExpContents(exp);
5791                      op1.ops.GrtEqu(exp, op1, op2);
5792                   }
5793                }
5794                break;
5795             case EQ_OP:
5796                if(exp.op.exp1)
5797                {
5798                   if(op1.ops.Equ)
5799                   {
5800                      FreeExpContents(exp);
5801                      op1.ops.Equ(exp, op1, op2);
5802                   }
5803                }
5804                break;
5805             case NE_OP:
5806                if(exp.op.exp1)
5807                {
5808                   if(op1.ops.Nqu)
5809                   {
5810                      FreeExpContents(exp);
5811                      op1.ops.Nqu(exp, op1, op2);
5812                   }
5813                }
5814                break;
5815             case '|':
5816                if(op1.ops.BitOr)
5817                {
5818                   FreeExpContents(exp);
5819                   op1.ops.BitOr(exp, op1, op2);
5820                }
5821                break;
5822             case '^':
5823                if(op1.ops.BitXor)
5824                {
5825                   FreeExpContents(exp);
5826                   op1.ops.BitXor(exp, op1, op2);
5827                }
5828                break;
5829             case AND_OP:
5830                break;
5831             case OR_OP:
5832                break;
5833             case SIZEOF:
5834                FreeExpContents(exp);
5835                exp.type = constantExp;
5836                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5837                break;
5838          }
5839          */
5840          if(op1.type) FreeType(op1.type);
5841          if(op2.type) FreeType(op2.type);
5842          break;
5843       }
5844       case bracketsExp:
5845       case extensionExpressionExp:
5846       {
5847          Expression e, n;
5848          for(e = exp.list->first; e; e = n)
5849          {
5850             n = e.next;
5851             if(!n)
5852             {
5853                OldList * list = exp.list;
5854                Expression prev = exp.prev;
5855                Expression next = exp.next;
5856                ComputeExpression(e);
5857                //FreeExpContents(exp);
5858                FreeType(exp.expType);
5859                FreeType(exp.destType);
5860                *exp = *e;
5861                exp.prev = prev;
5862                exp.next = next;
5863                delete e;
5864                delete list;
5865             }
5866             else
5867             {
5868                FreeExpression(e);
5869             }
5870          }
5871          break;
5872       }
5873       /*
5874
5875       case ExpIndex:
5876       {
5877          Expression e;
5878          exp.isConstant = true;
5879
5880          ComputeExpression(exp.index.exp);
5881          if(!exp.index.exp.isConstant)
5882             exp.isConstant = false;
5883
5884          for(e = exp.index.index->first; e; e = e.next)
5885          {
5886             ComputeExpression(e);
5887             if(!e.next)
5888             {
5889                // Check if this type is int
5890             }
5891             if(!e.isConstant)
5892                exp.isConstant = false;
5893          }
5894          exp.expType = Dereference(exp.index.exp.expType);
5895          break;
5896       }
5897       */
5898       case memberExp:
5899       {
5900          Expression memberExp = exp.member.exp;
5901          Identifier memberID = exp.member.member;
5902
5903          Type type;
5904          ComputeExpression(exp.member.exp);
5905          type = exp.member.exp.expType;
5906          if(type)
5907          {
5908             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);
5909             Property prop = null;
5910             DataMember member = null;
5911             Class convertTo = null;
5912             if(type.kind == subClassType && exp.member.exp.type == classExp)
5913                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5914
5915             if(!_class)
5916             {
5917                char string[256];
5918                Symbol classSym;
5919                string[0] = '\0';
5920                PrintTypeNoConst(type, string, false, true);
5921                classSym = FindClass(string);
5922                _class = classSym ? classSym.registered : null;
5923             }
5924
5925             if(exp.member.member)
5926             {
5927                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5928                if(!prop)
5929                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5930             }
5931             if(!prop && !member && _class && exp.member.member)
5932             {
5933                Symbol classSym = FindClass(exp.member.member.string);
5934                convertTo = _class;
5935                _class = classSym ? classSym.registered : null;
5936                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5937             }
5938
5939             if(prop)
5940             {
5941                if(prop.compiled)
5942                {
5943                   Type type = prop.dataType;
5944                   // TODO: Assuming same base type for units...
5945                   if(_class.type == unitClass)
5946                   {
5947                      if(type.kind == classType)
5948                      {
5949                         Class _class = type._class.registered;
5950                         if(_class.type == unitClass)
5951                         {
5952                            if(!_class.dataType)
5953                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5954                            type = _class.dataType;
5955                         }
5956                      }
5957                      switch(type.kind)
5958                      {
5959                         case floatType:
5960                         {
5961                            float value;
5962                            float (*Get)(float) = (void *)prop.Get;
5963                            GetFloat(exp.member.exp, &value);
5964                            exp.constant = PrintFloat(Get ? Get(value) : value);
5965                            exp.type = constantExp;
5966                            break;
5967                         }
5968                         case doubleType:
5969                         {
5970                            double value;
5971                            double (*Get)(double);
5972                            GetDouble(exp.member.exp, &value);
5973
5974                            if(convertTo)
5975                               Get = (void *)prop.Set;
5976                            else
5977                               Get = (void *)prop.Get;
5978                            exp.constant = PrintDouble(Get ? Get(value) : value);
5979                            exp.type = constantExp;
5980                            break;
5981                         }
5982                      }
5983                   }
5984                   else
5985                   {
5986                      if(convertTo)
5987                      {
5988                         Expression value = exp.member.exp;
5989                         Type type;
5990                         if(!prop.dataType)
5991                            ProcessPropertyType(prop);
5992
5993                         type = prop.dataType;
5994                         if(!type)
5995                         {
5996                             // printf("Investigate this\n");
5997                         }
5998                         else if(_class.type == structClass)
5999                         {
6000                            switch(type.kind)
6001                            {
6002                               case classType:
6003                               {
6004                                  Class propertyClass = type._class.registered;
6005                                  if(propertyClass.type == structClass && value.type == instanceExp)
6006                                  {
6007                                     void (*Set)(void *, void *) = (void *)prop.Set;
6008                                     exp.instance = Instantiation { };
6009                                     exp.instance.data = new0 byte[_class.structSize];
6010                                     exp.instance._class = MkSpecifierName(_class.fullName);
6011                                     exp.instance.loc = exp.loc;
6012                                     exp.type = instanceExp;
6013                                     Set(exp.instance.data, value.instance.data);
6014                                     PopulateInstance(exp.instance);
6015                                  }
6016                                  break;
6017                               }
6018                               case intType:
6019                               {
6020                                  int intValue;
6021                                  void (*Set)(void *, int) = (void *)prop.Set;
6022
6023                                  exp.instance = Instantiation { };
6024                                  exp.instance.data = new0 byte[_class.structSize];
6025                                  exp.instance._class = MkSpecifierName(_class.fullName);
6026                                  exp.instance.loc = exp.loc;
6027                                  exp.type = instanceExp;
6028
6029                                  GetInt(value, &intValue);
6030
6031                                  Set(exp.instance.data, intValue);
6032                                  PopulateInstance(exp.instance);
6033                                  break;
6034                               }
6035                               case int64Type:
6036                               {
6037                                  int64 intValue;
6038                                  void (*Set)(void *, int64) = (void *)prop.Set;
6039
6040                                  exp.instance = Instantiation { };
6041                                  exp.instance.data = new0 byte[_class.structSize];
6042                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
6043                                  exp.instance.loc = exp.loc;
6044                                  exp.type = instanceExp;
6045
6046                                  GetInt64(value, &intValue);
6047
6048                                  Set(exp.instance.data, intValue);
6049                                  PopulateInstance(exp.instance);
6050                                  break;
6051                               }
6052                               case intPtrType:
6053                               {
6054                                  // TOFIX:
6055                                  intptr intValue;
6056                                  void (*Set)(void *, intptr) = (void *)prop.Set;
6057
6058                                  exp.instance = Instantiation { };
6059                                  exp.instance.data = new0 byte[_class.structSize];
6060                                  exp.instance._class = MkSpecifierName(_class.fullName);
6061                                  exp.instance.loc = exp.loc;
6062                                  exp.type = instanceExp;
6063
6064                                  GetIntPtr(value, &intValue);
6065
6066                                  Set(exp.instance.data, intValue);
6067                                  PopulateInstance(exp.instance);
6068                                  break;
6069                               }
6070                               case intSizeType:
6071                               {
6072                                  // TOFIX:
6073                                  intsize intValue;
6074                                  void (*Set)(void *, intsize) = (void *)prop.Set;
6075
6076                                  exp.instance = Instantiation { };
6077                                  exp.instance.data = new0 byte[_class.structSize];
6078                                  exp.instance._class = MkSpecifierName(_class.fullName);
6079                                  exp.instance.loc = exp.loc;
6080                                  exp.type = instanceExp;
6081
6082                                  GetIntSize(value, &intValue);
6083
6084                                  Set(exp.instance.data, intValue);
6085                                  PopulateInstance(exp.instance);
6086                                  break;
6087                               }
6088                               case floatType:
6089                               {
6090                                  float floatValue;
6091                                  void (*Set)(void *, float) = (void *)prop.Set;
6092
6093                                  exp.instance = Instantiation { };
6094                                  exp.instance.data = new0 byte[_class.structSize];
6095                                  exp.instance._class = MkSpecifierName(_class.fullName);
6096                                  exp.instance.loc = exp.loc;
6097                                  exp.type = instanceExp;
6098
6099                                  GetFloat(value, &floatValue);
6100
6101                                  Set(exp.instance.data, floatValue);
6102                                  PopulateInstance(exp.instance);
6103                                  break;
6104                               }
6105                               case doubleType:
6106                               {
6107                                  double doubleValue;
6108                                  void (*Set)(void *, double) = (void *)prop.Set;
6109
6110                                  exp.instance = Instantiation { };
6111                                  exp.instance.data = new0 byte[_class.structSize];
6112                                  exp.instance._class = MkSpecifierName(_class.fullName);
6113                                  exp.instance.loc = exp.loc;
6114                                  exp.type = instanceExp;
6115
6116                                  GetDouble(value, &doubleValue);
6117
6118                                  Set(exp.instance.data, doubleValue);
6119                                  PopulateInstance(exp.instance);
6120                                  break;
6121                               }
6122                            }
6123                         }
6124                         else if(_class.type == bitClass)
6125                         {
6126                            switch(type.kind)
6127                            {
6128                               case classType:
6129                               {
6130                                  Class propertyClass = type._class.registered;
6131                                  if(propertyClass.type == structClass && value.instance.data)
6132                                  {
6133                                     unsigned int (*Set)(void *) = (void *)prop.Set;
6134                                     unsigned int bits = Set(value.instance.data);
6135                                     exp.constant = PrintHexUInt(bits);
6136                                     exp.type = constantExp;
6137                                     break;
6138                                  }
6139                                  else if(_class.type == bitClass)
6140                                  {
6141                                     unsigned int value;
6142                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
6143                                     unsigned int bits;
6144
6145                                     GetUInt(exp.member.exp, &value);
6146                                     bits = Set(value);
6147                                     exp.constant = PrintHexUInt(bits);
6148                                     exp.type = constantExp;
6149                                  }
6150                               }
6151                            }
6152                         }
6153                      }
6154                      else
6155                      {
6156                         if(_class.type == bitClass)
6157                         {
6158                            unsigned int value;
6159                            GetUInt(exp.member.exp, &value);
6160
6161                            switch(type.kind)
6162                            {
6163                               case classType:
6164                               {
6165                                  Class _class = type._class.registered;
6166                                  if(_class.type == structClass)
6167                                  {
6168                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
6169
6170                                     exp.instance = Instantiation { };
6171                                     exp.instance.data = new0 byte[_class.structSize];
6172                                     exp.instance._class = MkSpecifierName(_class.fullName);
6173                                     exp.instance.loc = exp.loc;
6174                                     //exp.instance.fullSet = true;
6175                                     exp.type = instanceExp;
6176                                     Get(value, exp.instance.data);
6177                                     PopulateInstance(exp.instance);
6178                                  }
6179                                  else if(_class.type == bitClass)
6180                                  {
6181                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6182                                     uint64 bits = Get(value);
6183                                     exp.constant = PrintHexUInt64(bits);
6184                                     exp.type = constantExp;
6185                                  }
6186                                  break;
6187                               }
6188                            }
6189                         }
6190                         else if(_class.type == structClass)
6191                         {
6192                            byte * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6193                            switch(type.kind)
6194                            {
6195                               case classType:
6196                               {
6197                                  Class _class = type._class.registered;
6198                                  if(_class.type == structClass && value)
6199                                  {
6200                                     void (*Get)(void *, void *) = (void *)prop.Get;
6201
6202                                     exp.instance = Instantiation { };
6203                                     exp.instance.data = new0 byte[_class.structSize];
6204                                     exp.instance._class = MkSpecifierName(_class.fullName);
6205                                     exp.instance.loc = exp.loc;
6206                                     //exp.instance.fullSet = true;
6207                                     exp.type = instanceExp;
6208                                     Get(value, exp.instance.data);
6209                                     PopulateInstance(exp.instance);
6210                                  }
6211                                  break;
6212                               }
6213                            }
6214                         }
6215                         /*else
6216                         {
6217                            char * value = exp.member.exp.instance.data;
6218                            switch(type.kind)
6219                            {
6220                               case classType:
6221                               {
6222                                  Class _class = type._class.registered;
6223                                  if(_class.type == normalClass)
6224                                  {
6225                                     void *(*Get)(void *) = (void *)prop.Get;
6226
6227                                     exp.instance = Instantiation { };
6228                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6229                                     exp.type = instanceExp;
6230                                     exp.instance.data = Get(value, exp.instance.data);
6231                                  }
6232                                  break;
6233                               }
6234                            }
6235                         }
6236                         */
6237                      }
6238                   }
6239                }
6240                else
6241                {
6242                   exp.isConstant = false;
6243                }
6244             }
6245             else if(member)
6246             {
6247             }
6248          }
6249
6250          if(exp.type != ExpressionType::memberExp)
6251          {
6252             FreeExpression(memberExp);
6253             FreeIdentifier(memberID);
6254          }
6255          break;
6256       }
6257       case typeSizeExp:
6258       {
6259          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6260          FreeExpContents(exp);
6261          exp.constant = PrintUInt(ComputeTypeSize(type));
6262          exp.type = constantExp;
6263          FreeType(type);
6264          break;
6265       }
6266       case classSizeExp:
6267       {
6268          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6269          if(classSym && classSym.registered)
6270          {
6271             if(classSym.registered.fixed)
6272             {
6273                FreeSpecifier(exp._class);
6274                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6275                exp.type = constantExp;
6276             }
6277             else
6278             {
6279                char className[1024];
6280                strcpy(className, "__ecereClass_");
6281                FullClassNameCat(className, classSym.string, true);
6282                //MangleClassName(className);
6283
6284                DeclareClass(classSym, className);
6285
6286                FreeExpContents(exp);
6287                exp.type = pointerExp;
6288                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6289                exp.member.member = MkIdentifier("structSize");
6290             }
6291          }
6292          break;
6293       }
6294       case castExp:
6295       //case constantExp:
6296       {
6297          Type type;
6298          Expression e = exp;
6299          if(exp.type == castExp)
6300          {
6301             if(exp.cast.exp)
6302                ComputeExpression(exp.cast.exp);
6303             e = exp.cast.exp;
6304          }
6305          if(e && exp.expType)
6306          {
6307             /*if(exp.destType)
6308                type = exp.destType;
6309             else*/
6310                type = exp.expType;
6311             if(type.kind == classType)
6312             {
6313                Class _class = type._class.registered;
6314                if(_class && (_class.type == unitClass || _class.type == bitClass))
6315                {
6316                   if(!_class.dataType)
6317                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6318                   type = _class.dataType;
6319                }
6320             }
6321
6322             switch(type.kind)
6323             {
6324                case _BoolType:
6325                case charType:
6326                   if(type.isSigned)
6327                   {
6328                      char value = 0;
6329                      if(GetChar(e, &value))
6330                      {
6331                         FreeExpContents(exp);
6332                         exp.constant = PrintChar(value);
6333                         exp.type = constantExp;
6334                      }
6335                   }
6336                   else
6337                   {
6338                      unsigned char value = 0;
6339                      if(GetUChar(e, &value))
6340                      {
6341                         FreeExpContents(exp);
6342                         exp.constant = PrintUChar(value);
6343                         exp.type = constantExp;
6344                      }
6345                   }
6346                   break;
6347                case shortType:
6348                   if(type.isSigned)
6349                   {
6350                      short value = 0;
6351                      if(GetShort(e, &value))
6352                      {
6353                         FreeExpContents(exp);
6354                         exp.constant = PrintShort(value);
6355                         exp.type = constantExp;
6356                      }
6357                   }
6358                   else
6359                   {
6360                      unsigned short value = 0;
6361                      if(GetUShort(e, &value))
6362                      {
6363                         FreeExpContents(exp);
6364                         exp.constant = PrintUShort(value);
6365                         exp.type = constantExp;
6366                      }
6367                   }
6368                   break;
6369                case intType:
6370                   if(type.isSigned)
6371                   {
6372                      int value = 0;
6373                      if(GetInt(e, &value))
6374                      {
6375                         FreeExpContents(exp);
6376                         exp.constant = PrintInt(value);
6377                         exp.type = constantExp;
6378                      }
6379                   }
6380                   else
6381                   {
6382                      unsigned int value = 0;
6383                      if(GetUInt(e, &value))
6384                      {
6385                         FreeExpContents(exp);
6386                         exp.constant = PrintUInt(value);
6387                         exp.type = constantExp;
6388                      }
6389                   }
6390                   break;
6391                case int64Type:
6392                   if(type.isSigned)
6393                   {
6394                      int64 value = 0;
6395                      if(GetInt64(e, &value))
6396                      {
6397                         FreeExpContents(exp);
6398                         exp.constant = PrintInt64(value);
6399                         exp.type = constantExp;
6400                      }
6401                   }
6402                   else
6403                   {
6404                      uint64 value = 0;
6405                      if(GetUInt64(e, &value))
6406                      {
6407                         FreeExpContents(exp);
6408                         exp.constant = PrintUInt64(value);
6409                         exp.type = constantExp;
6410                      }
6411                   }
6412                   break;
6413                case intPtrType:
6414                   if(type.isSigned)
6415                   {
6416                      intptr value = 0;
6417                      if(GetIntPtr(e, &value))
6418                      {
6419                         FreeExpContents(exp);
6420                         exp.constant = PrintInt64((int64)value);
6421                         exp.type = constantExp;
6422                      }
6423                   }
6424                   else
6425                   {
6426                      uintptr value = 0;
6427                      if(GetUIntPtr(e, &value))
6428                      {
6429                         FreeExpContents(exp);
6430                         exp.constant = PrintUInt64((uint64)value);
6431                         exp.type = constantExp;
6432                      }
6433                   }
6434                   break;
6435                case intSizeType:
6436                   if(type.isSigned)
6437                   {
6438                      intsize value = 0;
6439                      if(GetIntSize(e, &value))
6440                      {
6441                         FreeExpContents(exp);
6442                         exp.constant = PrintInt64((int64)value);
6443                         exp.type = constantExp;
6444                      }
6445                   }
6446                   else
6447                   {
6448                      uintsize value = 0;
6449                      if(GetUIntSize(e, &value))
6450                      {
6451                         FreeExpContents(exp);
6452                         exp.constant = PrintUInt64((uint64)value);
6453                         exp.type = constantExp;
6454                      }
6455                   }
6456                   break;
6457                case floatType:
6458                {
6459                   float value = 0;
6460                   if(GetFloat(e, &value))
6461                   {
6462                      FreeExpContents(exp);
6463                      exp.constant = PrintFloat(value);
6464                      exp.type = constantExp;
6465                   }
6466                   break;
6467                }
6468                case doubleType:
6469                {
6470                   double value = 0;
6471                   if(GetDouble(e, &value))
6472                   {
6473                      FreeExpContents(exp);
6474                      exp.constant = PrintDouble(value);
6475                      exp.type = constantExp;
6476                   }
6477                   break;
6478                }
6479             }
6480          }
6481          break;
6482       }
6483       case conditionExp:
6484       {
6485          Operand op1 { };
6486          Operand op2 { };
6487          Operand op3 { };
6488
6489          if(exp.cond.exp)
6490             // Caring only about last expression for now...
6491             ComputeExpression(exp.cond.exp->last);
6492          if(exp.cond.elseExp)
6493             ComputeExpression(exp.cond.elseExp);
6494          if(exp.cond.cond)
6495             ComputeExpression(exp.cond.cond);
6496
6497          op1 = GetOperand(exp.cond.cond);
6498          if(op1.type) op1.type.refCount++;
6499          op2 = GetOperand(exp.cond.exp->last);
6500          if(op2.type) op2.type.refCount++;
6501          op3 = GetOperand(exp.cond.elseExp);
6502          if(op3.type) op3.type.refCount++;
6503
6504          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6505          if(op1.type) FreeType(op1.type);
6506          if(op2.type) FreeType(op2.type);
6507          if(op3.type) FreeType(op3.type);
6508          break;
6509       }
6510    }
6511 }
6512
6513 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6514 {
6515    bool result = true;
6516    if(destType)
6517    {
6518       OldList converts { };
6519       Conversion convert;
6520
6521       if(destType.kind == voidType)
6522          return false;
6523
6524       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6525          result = false;
6526       if(converts.count)
6527       {
6528          // for(convert = converts.last; convert; convert = convert.prev)
6529          for(convert = converts.first; convert; convert = convert.next)
6530          {
6531             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6532             if(!empty)
6533             {
6534                Expression newExp { };
6535                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6536
6537                // TODO: Check this...
6538                *newExp = *exp;
6539                newExp.prev = null;
6540                newExp.next = null;
6541                newExp.destType = null;
6542
6543                if(convert.isGet)
6544                {
6545                   // [exp].ColorRGB
6546                   exp.type = memberExp;
6547                   exp.addedThis = true;
6548                   exp.member.exp = newExp;
6549                   FreeType(exp.member.exp.expType);
6550
6551                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6552                   exp.member.exp.expType.classObjectType = objectType;
6553                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6554                   exp.member.memberType = propertyMember;
6555                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6556                   // TESTING THIS... for (int)degrees
6557                   exp.needCast = true;
6558                   if(exp.expType) exp.expType.refCount++;
6559                   ApplyAnyObjectLogic(exp.member.exp);
6560                }
6561                else
6562                {
6563
6564                   /*if(exp.isConstant)
6565                   {
6566                      // Color { ColorRGB = [exp] };
6567                      exp.type = instanceExp;
6568                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6569                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6570                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6571                   }
6572                   else*/
6573                   {
6574                      // If not constant, don't turn it yet into an instantiation
6575                      // (Go through the deep members system first)
6576                      exp.type = memberExp;
6577                      exp.addedThis = true;
6578                      exp.member.exp = newExp;
6579
6580                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6581                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6582                         newExp.expType._class.registered.type == noHeadClass)
6583                      {
6584                         newExp.byReference = true;
6585                      }
6586
6587                      FreeType(exp.member.exp.expType);
6588                      /*exp.member.exp.expType = convert.convert.dataType;
6589                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6590                      exp.member.exp.expType = null;
6591                      if(convert.convert.dataType)
6592                      {
6593                         exp.member.exp.expType = { };
6594                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6595                         exp.member.exp.expType.refCount = 1;
6596                         exp.member.exp.expType.classObjectType = objectType;
6597                         ApplyAnyObjectLogic(exp.member.exp);
6598                      }
6599
6600                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6601                      exp.member.memberType = reverseConversionMember;
6602                      exp.expType = convert.resultType ? convert.resultType :
6603                         MkClassType(convert.convert._class.fullName);
6604                      exp.needCast = true;
6605                      if(convert.resultType) convert.resultType.refCount++;
6606                   }
6607                }
6608             }
6609             else
6610             {
6611                FreeType(exp.expType);
6612                if(convert.isGet)
6613                {
6614                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6615                   if(exp.destType.casted)
6616                      exp.needCast = true;
6617                   if(exp.expType) exp.expType.refCount++;
6618                }
6619                else
6620                {
6621                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6622                   if(exp.destType.casted)
6623                      exp.needCast = true;
6624                   if(convert.resultType)
6625                      convert.resultType.refCount++;
6626                }
6627             }
6628          }
6629          if(exp.isConstant && inCompiler)
6630             ComputeExpression(exp);
6631
6632          converts.Free(FreeConvert);
6633       }
6634
6635       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6636       {
6637          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6638       }
6639       if(!result && exp.expType && exp.destType)
6640       {
6641          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6642              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6643             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6644             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6645             result = true;
6646       }
6647    }
6648    // if(result) CheckTemplateTypes(exp);
6649    return result;
6650 }
6651
6652 void CheckTemplateTypes(Expression exp)
6653 {
6654    Expression nbExp = GetNonBracketsExp(exp);
6655    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6656       (nbExp == exp || nbExp.type != castExp))
6657    {
6658       Expression newExp { };
6659       Context context;
6660       *newExp = *exp;
6661       if(exp.destType) exp.destType.refCount++;
6662       if(exp.expType)  exp.expType.refCount++;
6663       newExp.prev = null;
6664       newExp.next = null;
6665
6666       switch(exp.expType.kind)
6667       {
6668          case doubleType:
6669             if(exp.destType.classObjectType)
6670             {
6671                // We need to pass the address, just pass it along (Undo what was done above)
6672                if(exp.destType) exp.destType.refCount--;
6673                if(exp.expType)  exp.expType.refCount--;
6674                delete newExp;
6675             }
6676             else
6677             {
6678                // If we're looking for value:
6679                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6680                OldList * specs;
6681                OldList * unionDefs = MkList();
6682                OldList * statements = MkList();
6683                context = PushContext();
6684                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6685                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6686                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6687                exp.type = extensionCompoundExp;
6688                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6689                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6690                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6691                exp.compound.compound.context = context;
6692                PopContext(context);
6693             }
6694             break;
6695          default:
6696             exp.type = castExp;
6697             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6698             if(exp.expType.isPointerType)
6699                exp.cast.exp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), MkExpBrackets(MkListOne(newExp)));
6700             else
6701                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6702             exp.needCast = true;
6703             break;
6704       }
6705    }
6706    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6707    {
6708       Expression newExp { };
6709       Context context;
6710       *newExp = *exp;
6711       if(exp.destType) exp.destType.refCount++;
6712       if(exp.expType)  exp.expType.refCount++;
6713       newExp.prev = null;
6714       newExp.next = null;
6715
6716       switch(exp.expType.kind)
6717       {
6718          case doubleType:
6719             if(exp.destType.classObjectType)
6720             {
6721                // We need to pass the address, just pass it along (Undo what was done above)
6722                if(exp.destType) exp.destType.refCount--;
6723                if(exp.expType)  exp.expType.refCount--;
6724                delete newExp;
6725             }
6726             else
6727             {
6728                // If we're looking for value:
6729                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6730                OldList * specs;
6731                OldList * unionDefs = MkList();
6732                OldList * statements = MkList();
6733                context = PushContext();
6734                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6735                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6736                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6737                exp.type = extensionCompoundExp;
6738                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6739                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6740                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6741                exp.compound.compound.context = context;
6742                PopContext(context);
6743             }
6744             break;
6745          case classType:
6746          {
6747             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6748             {
6749                exp.type = bracketsExp;
6750
6751                if(exp.expType.isPointerType)
6752                   newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6753                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6754                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6755                ProcessExpressionType(exp.list->first);
6756                break;
6757             }
6758             else
6759             {
6760                exp.type = bracketsExp;
6761                if(exp.expType.isPointerType)
6762                {
6763                   exp.needTemplateCast = 2;
6764                   newExp.needCast = true;
6765                   newExp.needTemplateCast = 2;
6766                   newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6767                }
6768
6769                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6770                exp.needTemplateCast = 2;
6771                newExp.needCast = true;
6772                newExp.needTemplateCast = 2;
6773                ProcessExpressionType(exp.list->first);
6774                break;
6775             }
6776          }
6777          default:
6778          {
6779             if(exp.expType.kind == templateType)
6780             {
6781                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6782                if(type)
6783                {
6784                   FreeType(exp.destType);
6785                   FreeType(exp.expType);
6786                   delete newExp;
6787                   break;
6788                }
6789             }
6790             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6791             {
6792                exp.type = opExp;
6793                exp.op.op = '*';
6794                exp.op.exp1 = null;
6795                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6796                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6797             }
6798             else
6799             {
6800                char typeString[1024];
6801                Declarator decl;
6802                OldList * specs = MkList();
6803                typeString[0] = '\0';
6804                PrintType(exp.expType, typeString, false, false);
6805                decl = SpecDeclFromString(typeString, specs, null);
6806
6807                exp.type = castExp;
6808                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6809                exp.cast.typeName = MkTypeName(specs, decl);
6810                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6811                exp.cast.exp.needCast = true;
6812             }
6813             break;
6814          }
6815       }
6816    }
6817 }
6818 // TODO: The Symbol tree should be reorganized by namespaces
6819 // Name Space:
6820 //    - Tree of all symbols within (stored without namespace)
6821 //    - Tree of sub-namespaces
6822
6823 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6824 {
6825    int nsLen = strlen(nameSpace);
6826    Symbol symbol;
6827    // Start at the name space prefix
6828    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6829    {
6830       char * s = symbol.string;
6831       if(!strncmp(s, nameSpace, nsLen))
6832       {
6833          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6834          int c;
6835          char * namePart;
6836          for(c = strlen(s)-1; c >= 0; c--)
6837             if(s[c] == ':')
6838                break;
6839
6840          namePart = s+c+1;
6841          if(!strcmp(namePart, name))
6842          {
6843             // TODO: Error on ambiguity
6844             return symbol;
6845          }
6846       }
6847       else
6848          break;
6849    }
6850    return null;
6851 }
6852
6853 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6854 {
6855    int c;
6856    char nameSpace[1024];
6857    const char * namePart;
6858    bool gotColon = false;
6859
6860    nameSpace[0] = '\0';
6861    for(c = strlen(name)-1; c >= 0; c--)
6862       if(name[c] == ':')
6863       {
6864          gotColon = true;
6865          break;
6866       }
6867
6868    namePart = name+c+1;
6869    while(c >= 0 && name[c] == ':') c--;
6870    if(c >= 0)
6871    {
6872       // Try an exact match first
6873       Symbol symbol = (Symbol)tree.FindString(name);
6874       if(symbol)
6875          return symbol;
6876
6877       // Namespace specified
6878       memcpy(nameSpace, name, c + 1);
6879       nameSpace[c+1] = 0;
6880
6881       return ScanWithNameSpace(tree, nameSpace, namePart);
6882    }
6883    else if(gotColon)
6884    {
6885       // Looking for a global symbol, e.g. ::Sleep()
6886       Symbol symbol = (Symbol)tree.FindString(namePart);
6887       return symbol;
6888    }
6889    else
6890    {
6891       // Name only (no namespace specified)
6892       Symbol symbol = (Symbol)tree.FindString(namePart);
6893       if(symbol)
6894          return symbol;
6895       return ScanWithNameSpace(tree, "", namePart);
6896    }
6897    return null;
6898 }
6899
6900 static void ProcessDeclaration(Declaration decl);
6901
6902 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6903 {
6904 #ifdef _DEBUG
6905    //Time startTime = GetTime();
6906 #endif
6907    // Optimize this later? Do this before/less?
6908    Context ctx;
6909    Symbol symbol = null;
6910    // First, check if the identifier is declared inside the function
6911    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6912
6913    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6914    {
6915       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6916       {
6917          symbol = null;
6918          if(thisNameSpace)
6919          {
6920             char curName[1024];
6921             strcpy(curName, thisNameSpace);
6922             strcat(curName, "::");
6923             strcat(curName, name);
6924             // Try to resolve in current namespace first
6925             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6926          }
6927          if(!symbol)
6928             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6929       }
6930       else
6931          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6932
6933       if(symbol || ctx == endContext) break;
6934    }
6935    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6936    {
6937       if(symbol.pointerExternal.type == functionExternal)
6938       {
6939          FunctionDefinition function = symbol.pointerExternal.function;
6940
6941          // Modified this recently...
6942          Context tmpContext = curContext;
6943          curContext = null;
6944          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6945          curContext = tmpContext;
6946
6947          symbol.pointerExternal.symbol = symbol;
6948
6949          // TESTING THIS:
6950          DeclareType(symbol.type, true, true);
6951
6952          ast->Insert(curExternal.prev, symbol.pointerExternal);
6953
6954          symbol.id = curExternal.symbol.idCode;
6955
6956       }
6957       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6958       {
6959          ast->Move(symbol.pointerExternal, curExternal.prev);
6960          symbol.id = curExternal.symbol.idCode;
6961       }
6962    }
6963 #ifdef _DEBUG
6964    //findSymbolTotalTime += GetTime() - startTime;
6965 #endif
6966    return symbol;
6967 }
6968
6969 static void GetTypeSpecs(Type type, OldList * specs)
6970 {
6971    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6972    switch(type.kind)
6973    {
6974       case classType:
6975       {
6976          if(type._class.registered)
6977          {
6978             if(!type._class.registered.dataType)
6979                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6980             GetTypeSpecs(type._class.registered.dataType, specs);
6981          }
6982          break;
6983       }
6984       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6985       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6986       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6987       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6988       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6989       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6990       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6991       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6992       case intType:
6993       default:
6994          ListAdd(specs, MkSpecifier(INT)); break;
6995    }
6996 }
6997
6998 static void PrintArraySize(Type arrayType, char * string)
6999 {
7000    char size[256];
7001    size[0] = '\0';
7002    strcat(size, "[");
7003    if(arrayType.enumClass)
7004       strcat(size, arrayType.enumClass.string);
7005    else if(arrayType.arraySizeExp)
7006       PrintExpression(arrayType.arraySizeExp, size);
7007    strcat(size, "]");
7008    strcat(string, size);
7009 }
7010
7011 // WARNING : This function expects a null terminated string since it recursively concatenate...
7012 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
7013 {
7014    if(type)
7015    {
7016       if(printConst && type.constant)
7017          strcat(string, "const ");
7018       switch(type.kind)
7019       {
7020          case classType:
7021          {
7022             Symbol c = type._class;
7023             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
7024             //       look into merging with thisclass ?
7025             if(type.classObjectType == typedObject)
7026                strcat(string, "typed_object");
7027             else if(type.classObjectType == anyObject)
7028                strcat(string, "any_object");
7029             else
7030             {
7031                if(c && c.string)
7032                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
7033             }
7034             if(type.byReference)
7035                strcat(string, " &");
7036             break;
7037          }
7038          case voidType: strcat(string, "void"); break;
7039          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
7040          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
7041          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
7042          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
7043          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
7044          case _BoolType: strcat(string, "_Bool"); break;
7045          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
7046          case floatType: strcat(string, "float"); break;
7047          case doubleType: strcat(string, "double"); break;
7048          case structType:
7049             if(type.enumName)
7050             {
7051                strcat(string, "struct ");
7052                strcat(string, type.enumName);
7053             }
7054             else if(type.typeName)
7055                strcat(string, type.typeName);
7056             else
7057             {
7058                Type member;
7059                strcat(string, "struct { ");
7060                for(member = type.members.first; member; member = member.next)
7061                {
7062                   PrintType(member, string, true, fullName);
7063                   strcat(string,"; ");
7064                }
7065                strcat(string,"}");
7066             }
7067             break;
7068          case unionType:
7069             if(type.enumName)
7070             {
7071                strcat(string, "union ");
7072                strcat(string, type.enumName);
7073             }
7074             else if(type.typeName)
7075                strcat(string, type.typeName);
7076             else
7077             {
7078                strcat(string, "union ");
7079                strcat(string,"(unnamed)");
7080             }
7081             break;
7082          case enumType:
7083             if(type.enumName)
7084             {
7085                strcat(string, "enum ");
7086                strcat(string, type.enumName);
7087             }
7088             else if(type.typeName)
7089                strcat(string, type.typeName);
7090             else
7091                strcat(string, "int"); // "enum");
7092             break;
7093          case ellipsisType:
7094             strcat(string, "...");
7095             break;
7096          case subClassType:
7097             strcat(string, "subclass(");
7098             strcat(string, type._class ? type._class.string : "int");
7099             strcat(string, ")");
7100             break;
7101          case templateType:
7102             strcat(string, type.templateParameter.identifier.string);
7103             break;
7104          case thisClassType:
7105             strcat(string, "thisclass");
7106             break;
7107          case vaListType:
7108             strcat(string, "__builtin_va_list");
7109             break;
7110       }
7111    }
7112 }
7113
7114 static void PrintName(Type type, char * string, bool fullName)
7115 {
7116    if(type.name && type.name[0])
7117    {
7118       if(fullName)
7119          strcat(string, type.name);
7120       else
7121       {
7122          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
7123          if(name) name += 2; else name = type.name;
7124          strcat(string, name);
7125       }
7126    }
7127 }
7128
7129 static void PrintAttribs(Type type, char * string)
7130 {
7131    if(type)
7132    {
7133       if(type.dllExport)   strcat(string, "dllexport ");
7134       if(type.attrStdcall) strcat(string, "stdcall ");
7135    }
7136 }
7137
7138 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
7139 {
7140    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7141    {
7142       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7143          PrintAttribs(type, string);
7144       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7145          strcat(string, " const");
7146       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7147       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7148          strcat(string, " (");
7149       if(type.kind == pointerType)
7150       {
7151          if(type.type.kind == functionType || type.type.kind == methodType)
7152             PrintAttribs(type.type, string);
7153       }
7154       if(type.kind == pointerType)
7155       {
7156          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7157             strcat(string, "*");
7158          else
7159             strcat(string, " *");
7160       }
7161       if(printConst && type.constant && type.kind == pointerType)
7162          strcat(string, " const");
7163    }
7164    else
7165       PrintTypeSpecs(type, string, fullName, printConst);
7166 }
7167
7168 static void PostPrintType(Type type, char * string, bool fullName)
7169 {
7170    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7171       strcat(string, ")");
7172    if(type.kind == arrayType)
7173       PrintArraySize(type, string);
7174    else if(type.kind == functionType)
7175    {
7176       Type param;
7177       strcat(string, "(");
7178       for(param = type.params.first; param; param = param.next)
7179       {
7180          PrintType(param, string, true, fullName);
7181          if(param.next) strcat(string, ", ");
7182       }
7183       strcat(string, ")");
7184    }
7185    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7186       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7187 }
7188
7189 // *****
7190 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7191 // *****
7192 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7193 {
7194    PrePrintType(type, string, fullName, null, printConst);
7195
7196    if(type.thisClass || (printName && type.name && type.name[0]))
7197       strcat(string, " ");
7198    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7199    {
7200       Symbol _class = type.thisClass;
7201       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7202       {
7203          if(type.classObjectType == classPointer)
7204             strcat(string, "class");
7205          else
7206             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7207       }
7208       else if(_class && _class.string)
7209       {
7210          String s = _class.string;
7211          if(fullName)
7212             strcat(string, s);
7213          else
7214          {
7215             char * name = RSearchString(s, "::", strlen(s), true, false);
7216             if(name) name += 2; else name = s;
7217             strcat(string, name);
7218          }
7219       }
7220       strcat(string, "::");
7221    }
7222
7223    if(printName && type.name)
7224       PrintName(type, string, fullName);
7225    PostPrintType(type, string, fullName);
7226    if(type.bitFieldCount)
7227    {
7228       char count[100];
7229       sprintf(count, ":%d", type.bitFieldCount);
7230       strcat(string, count);
7231    }
7232 }
7233
7234 void PrintType(Type type, char * string, bool printName, bool fullName)
7235 {
7236    _PrintType(type, string, printName, fullName, true);
7237 }
7238
7239 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7240 {
7241    _PrintType(type, string, printName, fullName, false);
7242 }
7243
7244 static Type FindMember(Type type, char * string)
7245 {
7246    Type memberType;
7247    for(memberType = type.members.first; memberType; memberType = memberType.next)
7248    {
7249       if(!memberType.name)
7250       {
7251          Type subType = FindMember(memberType, string);
7252          if(subType)
7253             return subType;
7254       }
7255       else if(!strcmp(memberType.name, string))
7256          return memberType;
7257    }
7258    return null;
7259 }
7260
7261 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7262 {
7263    Type memberType;
7264    for(memberType = type.members.first; memberType; memberType = memberType.next)
7265    {
7266       if(!memberType.name)
7267       {
7268          Type subType = FindMember(memberType, string);
7269          if(subType)
7270          {
7271             *offset += memberType.offset;
7272             return subType;
7273          }
7274       }
7275       else if(!strcmp(memberType.name, string))
7276       {
7277          *offset += memberType.offset;
7278          return memberType;
7279       }
7280    }
7281    return null;
7282 }
7283
7284 public bool GetParseError() { return parseError; }
7285
7286 Expression ParseExpressionString(char * expression)
7287 {
7288    parseError = false;
7289
7290    fileInput = TempFile { };
7291    fileInput.Write(expression, 1, strlen(expression));
7292    fileInput.Seek(0, start);
7293
7294    echoOn = false;
7295    parsedExpression = null;
7296    resetScanner();
7297    expression_yyparse();
7298    delete fileInput;
7299
7300    return parsedExpression;
7301 }
7302
7303 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7304 {
7305    Identifier id = exp.identifier;
7306    Method method = null;
7307    Property prop = null;
7308    DataMember member = null;
7309    ClassProperty classProp = null;
7310
7311    if(_class && _class.type == enumClass)
7312    {
7313       NamedLink64 value = null;
7314       Class enumClass = eSystem_FindClass(privateModule, "enum");
7315       if(enumClass)
7316       {
7317          Class baseClass;
7318          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7319          {
7320             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7321             for(value = e.values.first; value; value = value.next)
7322             {
7323                if(!strcmp(value.name, id.string))
7324                   break;
7325             }
7326             if(value)
7327             {
7328                exp.isConstant = true;
7329                if(inCompiler || inPreCompiler || inDebugger)
7330                {
7331                   char constant[256];
7332                   FreeExpContents(exp);
7333
7334                   exp.type = constantExp;
7335                   if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "char") || !strcmp(baseClass.dataTypeString, "short"))
7336                      sprintf(constant, FORMAT64D, value.data);
7337                   else
7338                      sprintf(constant, FORMAT64HEX, value.data);
7339                   exp.constant = CopyString(constant);
7340                }
7341                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7342                exp.expType = MkClassType(baseClass.fullName);
7343                break;
7344             }
7345          }
7346       }
7347       if(value)
7348          return true;
7349    }
7350    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7351    {
7352       ProcessMethodType(method);
7353       exp.expType = Type
7354       {
7355          refCount = 1;
7356          kind = methodType;
7357          method = method;
7358          // Crash here?
7359          // TOCHECK: Put it back to what it was...
7360          // methodClass = _class;
7361          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7362       };
7363       //id._class = null;
7364       return true;
7365    }
7366    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7367    {
7368       if(!prop.dataType)
7369          ProcessPropertyType(prop);
7370       exp.expType = prop.dataType;
7371       if(prop.dataType) prop.dataType.refCount++;
7372       return true;
7373    }
7374    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7375    {
7376       if(!member.dataType)
7377          member.dataType = ProcessTypeString(member.dataTypeString, false);
7378       exp.expType = member.dataType;
7379       if(member.dataType) member.dataType.refCount++;
7380       return true;
7381    }
7382    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7383    {
7384       if(!classProp.dataType)
7385          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7386
7387       if(classProp.constant)
7388       {
7389          FreeExpContents(exp);
7390
7391          exp.isConstant = true;
7392          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7393          {
7394             //char constant[256];
7395             exp.type = stringExp;
7396             exp.constant = QMkString((char *)(uintptr)classProp.Get(_class));
7397          }
7398          else
7399          {
7400             char constant[256];
7401             exp.type = constantExp;
7402             sprintf(constant, "%d", (int)classProp.Get(_class));
7403             exp.constant = CopyString(constant);
7404          }
7405       }
7406       else
7407       {
7408          // TO IMPLEMENT...
7409       }
7410
7411       exp.expType = classProp.dataType;
7412       if(classProp.dataType) classProp.dataType.refCount++;
7413       return true;
7414    }
7415    return false;
7416 }
7417
7418 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7419 {
7420    BinaryTree * tree = &nameSpace.functions;
7421    GlobalData data = (GlobalData)tree->FindString(name);
7422    NameSpace * child;
7423    if(!data)
7424    {
7425       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7426       {
7427          data = ScanGlobalData(child, name);
7428          if(data)
7429             break;
7430       }
7431    }
7432    return data;
7433 }
7434
7435 static GlobalData FindGlobalData(char * name)
7436 {
7437    int start = 0, c;
7438    NameSpace * nameSpace;
7439    nameSpace = globalData;
7440    for(c = 0; name[c]; c++)
7441    {
7442       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7443       {
7444          NameSpace * newSpace;
7445          char * spaceName = new char[c - start + 1];
7446          strncpy(spaceName, name + start, c - start);
7447          spaceName[c-start] = '\0';
7448          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7449          delete spaceName;
7450          if(!newSpace)
7451             return null;
7452          nameSpace = newSpace;
7453          if(name[c] == ':') c++;
7454          start = c+1;
7455       }
7456    }
7457    if(c - start)
7458    {
7459       return ScanGlobalData(nameSpace, name + start);
7460    }
7461    return null;
7462 }
7463
7464 static int definedExpStackPos;
7465 static void * definedExpStack[512];
7466
7467 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7468 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7469 {
7470    Expression prev = checkedExp.prev, next = checkedExp.next;
7471
7472    FreeExpContents(checkedExp);
7473    FreeType(checkedExp.expType);
7474    FreeType(checkedExp.destType);
7475
7476    *checkedExp = *newExp;
7477
7478    delete newExp;
7479
7480    checkedExp.prev = prev;
7481    checkedExp.next = next;
7482 }
7483
7484 void ApplyAnyObjectLogic(Expression e)
7485 {
7486    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7487 #ifdef _DEBUG
7488    char debugExpString[4096];
7489    debugExpString[0] = '\0';
7490    PrintExpression(e, debugExpString);
7491 #endif
7492
7493    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7494    {
7495       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7496       //ellipsisDestType = destType;
7497       if(e && e.expType)
7498       {
7499          Type type = e.expType;
7500          Class _class = null;
7501          //Type destType = e.destType;
7502
7503          if(type.kind == classType && type._class && type._class.registered)
7504          {
7505             _class = type._class.registered;
7506          }
7507          else if(type.kind == subClassType)
7508          {
7509             _class = FindClass("ecere::com::Class").registered;
7510          }
7511          else
7512          {
7513             char string[1024] = "";
7514             Symbol classSym;
7515
7516             PrintTypeNoConst(type, string, false, true);
7517             classSym = FindClass(string);
7518             if(classSym) _class = classSym.registered;
7519          }
7520
7521          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...
7522             (!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))) ||
7523             destType.byReference)))
7524          {
7525             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7526             {
7527                Expression checkedExp = e, newExp;
7528
7529                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7530                {
7531                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7532                   {
7533                      if(checkedExp.type == extensionCompoundExp)
7534                      {
7535                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7536                      }
7537                      else
7538                         checkedExp = checkedExp.list->last;
7539                   }
7540                   else if(checkedExp.type == castExp)
7541                      checkedExp = checkedExp.cast.exp;
7542                }
7543
7544                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7545                {
7546                   newExp = checkedExp.op.exp2;
7547                   checkedExp.op.exp2 = null;
7548                   FreeExpContents(checkedExp);
7549
7550                   if(e.expType && e.expType.passAsTemplate)
7551                   {
7552                      char size[100];
7553                      ComputeTypeSize(e.expType);
7554                      sprintf(size, "%d", e.expType.size);
7555                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7556                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7557                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7558                   }
7559
7560                   ReplaceExpContents(checkedExp, newExp);
7561                   e.byReference = true;
7562                }
7563                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7564                {
7565                   Expression checkedExp; //, newExp;
7566
7567                   {
7568                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7569                      bool hasAddress =
7570                         e.type == identifierExp ||
7571                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7572                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7573                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7574                         e.type == indexExp;
7575
7576                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7577                      {
7578                         Context context = PushContext();
7579                         Declarator decl;
7580                         OldList * specs = MkList();
7581                         char typeString[1024];
7582                         Expression newExp { };
7583
7584                         typeString[0] = '\0';
7585                         *newExp = *e;
7586
7587                         //if(e.destType) e.destType.refCount++;
7588                         // if(exp.expType) exp.expType.refCount++;
7589                         newExp.prev = null;
7590                         newExp.next = null;
7591                         newExp.expType = null;
7592
7593                         PrintTypeNoConst(e.expType, typeString, false, true);
7594                         decl = SpecDeclFromString(typeString, specs, null);
7595                         newExp.destType = ProcessType(specs, decl);
7596
7597                         curContext = context;
7598
7599                         // We need a current compound for this
7600                         if(curCompound)
7601                         {
7602                            char name[100];
7603                            OldList * stmts = MkList();
7604                            e.type = extensionCompoundExp;
7605                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7606                            if(!curCompound.compound.declarations)
7607                               curCompound.compound.declarations = MkList();
7608                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7609                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7610                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7611                            e.compound = MkCompoundStmt(null, stmts);
7612                         }
7613                         else
7614                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7615
7616                         /*
7617                         e.compound = MkCompoundStmt(
7618                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7619                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7620
7621                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7622                         */
7623
7624                         {
7625                            Type type = e.destType;
7626                            e.destType = { };
7627                            CopyTypeInto(e.destType, type);
7628                            e.destType.refCount = 1;
7629                            e.destType.classObjectType = none;
7630                            FreeType(type);
7631                         }
7632
7633                         e.compound.compound.context = context;
7634                         PopContext(context);
7635                         curContext = context.parent;
7636                      }
7637                   }
7638
7639                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7640                   checkedExp = e;
7641                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7642                   {
7643                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7644                      {
7645                         if(checkedExp.type == extensionCompoundExp)
7646                         {
7647                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7648                         }
7649                         else
7650                            checkedExp = checkedExp.list->last;
7651                      }
7652                      else if(checkedExp.type == castExp)
7653                         checkedExp = checkedExp.cast.exp;
7654                   }
7655                   {
7656                      Expression operand { };
7657                      operand = *checkedExp;
7658                      checkedExp.Clear();
7659                      checkedExp.destType = ProcessTypeString("void *", false);
7660                      checkedExp.expType = checkedExp.destType;
7661                      checkedExp.destType.refCount++;
7662
7663                      checkedExp.type = opExp;
7664                      checkedExp.op.op = '&';
7665                      checkedExp.op.exp1 = null;
7666                      checkedExp.op.exp2 = operand;
7667
7668                      //newExp = MkExpOp(null, '&', checkedExp);
7669                   }
7670                   //ReplaceExpContents(checkedExp, newExp);
7671                }
7672             }
7673          }
7674       }
7675    }
7676    {
7677       // If expression type is a simple class, make it an address
7678       // FixReference(e, true);
7679    }
7680 //#if 0
7681    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7682       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7683          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7684    {
7685       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"))
7686       {
7687          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7688       }
7689       else
7690       {
7691          Expression thisExp { };
7692
7693          *thisExp = *e;
7694          thisExp.prev = null;
7695          thisExp.next = null;
7696          e.Clear();
7697
7698          e.type = bracketsExp;
7699          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7700          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7701             ((Expression)e.list->first).byReference = true;
7702
7703          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7704          {
7705             e.expType = thisExp.expType;
7706             e.expType.refCount++;
7707          }
7708          else*/
7709          {
7710             e.expType = { };
7711             CopyTypeInto(e.expType, thisExp.expType);
7712             e.expType.byReference = false;
7713             e.expType.refCount = 1;
7714
7715             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7716                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7717             {
7718                e.expType.classObjectType = none;
7719             }
7720          }
7721       }
7722    }
7723 // TOFIX: Try this for a nice IDE crash!
7724 //#endif
7725    // The other way around
7726    else
7727 //#endif
7728    if(destType && e.expType &&
7729          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7730          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7731          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7732    {
7733       if(destType.kind == ellipsisType)
7734       {
7735          Compiler_Error($"Unspecified type\n");
7736       }
7737       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7738       {
7739          bool byReference = e.expType.byReference;
7740          Expression thisExp { };
7741          Declarator decl;
7742          OldList * specs = MkList();
7743          char typeString[1024]; // Watch buffer overruns
7744          Type type;
7745          ClassObjectType backupClassObjectType;
7746          bool backupByReference;
7747
7748          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7749             type = e.expType;
7750          else
7751             type = destType;
7752
7753          backupClassObjectType = type.classObjectType;
7754          backupByReference = type.byReference;
7755
7756          type.classObjectType = none;
7757          type.byReference = false;
7758
7759          typeString[0] = '\0';
7760          PrintType(type, typeString, false, true);
7761          decl = SpecDeclFromString(typeString, specs, null);
7762
7763          type.classObjectType = backupClassObjectType;
7764          type.byReference = backupByReference;
7765
7766          *thisExp = *e;
7767          thisExp.prev = null;
7768          thisExp.next = null;
7769          e.Clear();
7770
7771          if( ( type.kind == classType && type._class && type._class.registered &&
7772                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7773                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7774              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7775              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7776          {
7777             e.type = opExp;
7778             e.op.op = '*';
7779             e.op.exp1 = null;
7780             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7781
7782             e.expType = { };
7783             CopyTypeInto(e.expType, type);
7784             e.expType.byReference = false;
7785             e.expType.refCount = 1;
7786          }
7787          else
7788          {
7789             e.type = castExp;
7790             e.cast.typeName = MkTypeName(specs, decl);
7791             e.cast.exp = thisExp;
7792             e.byReference = true;
7793             e.expType = type;
7794             type.refCount++;
7795          }
7796          e.destType = destType;
7797          destType.refCount++;
7798       }
7799    }
7800 }
7801
7802 void ApplyLocation(Expression exp, Location loc)
7803 {
7804    exp.loc = loc;
7805    switch(exp.type)
7806    {
7807       case opExp:
7808          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7809          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7810          break;
7811       case bracketsExp:
7812          if(exp.list)
7813          {
7814             Expression e;
7815             for(e = exp.list->first; e; e = e.next)
7816                ApplyLocation(e, loc);
7817          }
7818          break;
7819       case indexExp:
7820          if(exp.index.index)
7821          {
7822             Expression e;
7823             for(e = exp.index.index->first; e; e = e.next)
7824                ApplyLocation(e, loc);
7825          }
7826          if(exp.index.exp)
7827             ApplyLocation(exp.index.exp, loc);
7828          break;
7829       case callExp:
7830          if(exp.call.arguments)
7831          {
7832             Expression arg;
7833             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7834                ApplyLocation(arg, loc);
7835          }
7836          if(exp.call.exp)
7837             ApplyLocation(exp.call.exp, loc);
7838          break;
7839       case memberExp:
7840       case pointerExp:
7841          if(exp.member.exp)
7842             ApplyLocation(exp.member.exp, loc);
7843          break;
7844       case castExp:
7845          if(exp.cast.exp)
7846             ApplyLocation(exp.cast.exp, loc);
7847          break;
7848       case conditionExp:
7849          if(exp.cond.exp)
7850          {
7851             Expression e;
7852             for(e = exp.cond.exp->first; e; e = e.next)
7853                ApplyLocation(e, loc);
7854          }
7855          if(exp.cond.cond)
7856             ApplyLocation(exp.cond.cond, loc);
7857          if(exp.cond.elseExp)
7858             ApplyLocation(exp.cond.elseExp, loc);
7859          break;
7860       case vaArgExp:
7861          if(exp.vaArg.exp)
7862             ApplyLocation(exp.vaArg.exp, loc);
7863          break;
7864       default:
7865          break;
7866    }
7867 }
7868
7869 void ProcessExpressionType(Expression exp)
7870 {
7871    bool unresolved = false;
7872    Location oldyylloc = yylloc;
7873    bool notByReference = false;
7874 #ifdef _DEBUG
7875    char debugExpString[4096];
7876    debugExpString[0] = '\0';
7877    PrintExpression(exp, debugExpString);
7878 #endif
7879    if(!exp || exp.expType)
7880       return;
7881
7882    //eSystem_Logf("%s\n", expString);
7883
7884    // Testing this here
7885    yylloc = exp.loc;
7886    switch(exp.type)
7887    {
7888       case identifierExp:
7889       {
7890          Identifier id = exp.identifier;
7891          if(!id || !topContext) return;
7892
7893          // DOING THIS LATER NOW...
7894          if(id._class && id._class.name)
7895          {
7896             id.classSym = id._class.symbol; // FindClass(id._class.name);
7897             /* TODO: Name Space Fix ups
7898             if(!id.classSym)
7899                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7900             */
7901          }
7902
7903          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7904          {
7905             exp.expType = ProcessTypeString("Module", true);
7906             break;
7907          }
7908          else */
7909          if(!strcmp(id.string, "__runtimePlatform"))
7910          {
7911             exp.expType = ProcessTypeString("ecere::com::Platform", true);
7912             break;
7913          }
7914          else if(strstr(id.string, "__ecereClass") == id.string)
7915          {
7916             exp.expType = ProcessTypeString("ecere::com::Class", true);
7917             break;
7918          }
7919          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7920          {
7921             // Added this here as well
7922             ReplaceClassMembers(exp, thisClass);
7923             if(exp.type != identifierExp)
7924             {
7925                ProcessExpressionType(exp);
7926                break;
7927             }
7928
7929             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7930                break;
7931          }
7932          else
7933          {
7934             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7935             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7936             if(!symbol/* && exp.destType*/)
7937             {
7938                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7939                   break;
7940                else
7941                {
7942                   if(thisClass)
7943                   {
7944                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7945                      if(exp.type != identifierExp)
7946                      {
7947                         ProcessExpressionType(exp);
7948                         break;
7949                      }
7950                   }
7951                   // Static methods called from inside the _class
7952                   else if(currentClass && !id._class)
7953                   {
7954                      if(ResolveIdWithClass(exp, currentClass, true))
7955                         break;
7956                   }
7957                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7958                }
7959             }
7960
7961             // If we manage to resolve this symbol
7962             if(symbol)
7963             {
7964                Type type = symbol.type;
7965                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7966
7967                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7968                {
7969                   Context context = SetupTemplatesContext(_class);
7970                   type = ReplaceThisClassType(_class);
7971                   FinishTemplatesContext(context);
7972                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7973                }
7974
7975                FreeSpecifier(id._class);
7976                id._class = null;
7977                delete id.string;
7978                id.string = CopyString(symbol.string);
7979
7980                id.classSym = null;
7981                exp.expType = type;
7982                if(type)
7983                   type.refCount++;
7984
7985                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7986                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7987                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7988                   // Add missing cases here... enum Classes...
7989                   exp.isConstant = true;
7990
7991                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7992                if(symbol.isParam || !strcmp(id.string, "this"))
7993                {
7994                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7995                      exp.byReference = true;
7996
7997                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7998                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7999                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
8000                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
8001                   {
8002                      Identifier id = exp.identifier;
8003                      exp.type = bracketsExp;
8004                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
8005                   }*/
8006                }
8007
8008                if(symbol.isIterator)
8009                {
8010                   if(symbol.isIterator == 3)
8011                   {
8012                      exp.type = bracketsExp;
8013                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
8014                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
8015                      exp.expType = null;
8016                      ProcessExpressionType(exp);
8017                   }
8018                   else if(symbol.isIterator != 4)
8019                   {
8020                      exp.type = memberExp;
8021                      exp.member.exp = MkExpIdentifier(exp.identifier);
8022                      exp.member.exp.expType = exp.expType;
8023                      /*if(symbol.isIterator == 6)
8024                         exp.member.member = MkIdentifier("key");
8025                      else*/
8026                         exp.member.member = MkIdentifier("data");
8027                      exp.expType = null;
8028                      ProcessExpressionType(exp);
8029                   }
8030                }
8031                break;
8032             }
8033             else
8034             {
8035                DefinedExpression definedExp = null;
8036                if(thisNameSpace && !(id._class && !id._class.name))
8037                {
8038                   char name[1024];
8039                   strcpy(name, thisNameSpace);
8040                   strcat(name, "::");
8041                   strcat(name, id.string);
8042                   definedExp = eSystem_FindDefine(privateModule, name);
8043                }
8044                if(!definedExp)
8045                   definedExp = eSystem_FindDefine(privateModule, id.string);
8046                if(definedExp)
8047                {
8048                   int c;
8049                   for(c = 0; c<definedExpStackPos; c++)
8050                      if(definedExpStack[c] == definedExp)
8051                         break;
8052                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
8053                   {
8054                      Location backupYylloc = yylloc;
8055                      File backInput = fileInput;
8056                      definedExpStack[definedExpStackPos++] = definedExp;
8057
8058                      fileInput = TempFile { };
8059                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
8060                      fileInput.Seek(0, start);
8061
8062                      echoOn = false;
8063                      parsedExpression = null;
8064                      resetScanner();
8065                      expression_yyparse();
8066                      delete fileInput;
8067                      if(backInput)
8068                         fileInput = backInput;
8069
8070                      yylloc = backupYylloc;
8071
8072                      if(parsedExpression)
8073                      {
8074                         FreeIdentifier(id);
8075                         exp.type = bracketsExp;
8076                         exp.list = MkListOne(parsedExpression);
8077                         ApplyLocation(parsedExpression, yylloc);
8078                         ProcessExpressionType(exp);
8079                         definedExpStackPos--;
8080                         return;
8081                      }
8082                      definedExpStackPos--;
8083                   }
8084                   else
8085                   {
8086                      if(inCompiler)
8087                      {
8088                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
8089                      }
8090                   }
8091                }
8092                else
8093                {
8094                   GlobalData data = null;
8095                   if(thisNameSpace && !(id._class && !id._class.name))
8096                   {
8097                      char name[1024];
8098                      strcpy(name, thisNameSpace);
8099                      strcat(name, "::");
8100                      strcat(name, id.string);
8101                      data = FindGlobalData(name);
8102                   }
8103                   if(!data)
8104                      data = FindGlobalData(id.string);
8105                   if(data)
8106                   {
8107                      DeclareGlobalData(data);
8108                      exp.expType = data.dataType;
8109                      if(data.dataType) data.dataType.refCount++;
8110
8111                      delete id.string;
8112                      id.string = CopyString(data.fullName);
8113                      FreeSpecifier(id._class);
8114                      id._class = null;
8115
8116                      break;
8117                   }
8118                   else
8119                   {
8120                      GlobalFunction function = null;
8121                      if(thisNameSpace && !(id._class && !id._class.name))
8122                      {
8123                         char name[1024];
8124                         strcpy(name, thisNameSpace);
8125                         strcat(name, "::");
8126                         strcat(name, id.string);
8127                         function = eSystem_FindFunction(privateModule, name);
8128                      }
8129                      if(!function)
8130                         function = eSystem_FindFunction(privateModule, id.string);
8131                      if(function)
8132                      {
8133                         char name[1024];
8134                         delete id.string;
8135                         id.string = CopyString(function.name);
8136                         name[0] = 0;
8137
8138                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8139                            strcpy(name, "__ecereFunction_");
8140                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8141                         if(DeclareFunction(function, name))
8142                         {
8143                            delete id.string;
8144                            id.string = CopyString(name);
8145                         }
8146                         exp.expType = function.dataType;
8147                         if(function.dataType) function.dataType.refCount++;
8148
8149                         FreeSpecifier(id._class);
8150                         id._class = null;
8151
8152                         break;
8153                      }
8154                   }
8155                }
8156             }
8157          }
8158          unresolved = true;
8159          break;
8160       }
8161       case instanceExp:
8162       {
8163          // Class _class;
8164          // Symbol classSym;
8165
8166          if(!exp.instance._class)
8167          {
8168             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8169             {
8170                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8171             }
8172          }
8173
8174          //classSym = FindClass(exp.instance._class.fullName);
8175          //_class = classSym ? classSym.registered : null;
8176
8177          ProcessInstantiationType(exp.instance);
8178
8179          exp.isConstant = exp.instance.isConstant;
8180
8181          /*
8182          if(_class.type == unitClass && _class.base.type != systemClass)
8183          {
8184             {
8185                Type destType = exp.destType;
8186
8187                exp.destType = MkClassType(_class.base.fullName);
8188                exp.expType = MkClassType(_class.fullName);
8189                CheckExpressionType(exp, exp.destType, true);
8190
8191                exp.destType = destType;
8192             }
8193             exp.expType = MkClassType(_class.fullName);
8194          }
8195          else*/
8196          if(exp.instance._class)
8197          {
8198             exp.expType = MkClassType(exp.instance._class.name);
8199             /*if(exp.expType._class && exp.expType._class.registered &&
8200                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8201                exp.expType.byReference = true;*/
8202          }
8203          break;
8204       }
8205       case constantExp:
8206       {
8207          if(!exp.expType)
8208          {
8209             char * constant = exp.constant;
8210             Type type
8211             {
8212                refCount = 1;
8213                constant = true;
8214             };
8215             exp.expType = type;
8216
8217             if(constant[0] == '\'')
8218             {
8219                if((int)((byte *)constant)[1] > 127)
8220                {
8221                   int nb;
8222                   unichar ch = UTF8GetChar(constant + 1, &nb);
8223                   if(nb < 2) ch = constant[1];
8224                   delete constant;
8225                   exp.constant = PrintUInt(ch);
8226                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8227                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8228                   type._class = FindClass("unichar");
8229
8230                   type.isSigned = false;
8231                }
8232                else
8233                {
8234                   type.kind = charType;
8235                   type.isSigned = true;
8236                }
8237             }
8238             else
8239             {
8240                char * dot = strchr(constant, '.');
8241                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8242                char * exponent;
8243                if(isHex)
8244                {
8245                   exponent = strchr(constant, 'p');
8246                   if(!exponent) exponent = strchr(constant, 'P');
8247                }
8248                else
8249                {
8250                   exponent = strchr(constant, 'e');
8251                   if(!exponent) exponent = strchr(constant, 'E');
8252                }
8253
8254                if(dot || exponent)
8255                {
8256                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8257                      type.kind = floatType;
8258                   else
8259                      type.kind = doubleType;
8260                   type.isSigned = true;
8261                }
8262                else
8263                {
8264                   bool isSigned = constant[0] == '-';
8265                   char * endP = null;
8266                   int64 i64 = strtoll(constant, &endP, 0);
8267                   uint64 ui64 = strtoull(constant, &endP, 0);
8268                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
8269                   if(isSigned)
8270                   {
8271                      if(i64 < MININT)
8272                         is64Bit = true;
8273                   }
8274                   else
8275                   {
8276                      if(ui64 > MAXINT)
8277                      {
8278                         if(ui64 > MAXDWORD)
8279                         {
8280                            is64Bit = true;
8281                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8282                               isSigned = true;
8283                         }
8284                      }
8285                      else if(constant[0] != '0' || !constant[1])
8286                         isSigned = true;
8287                   }
8288                   type.kind = is64Bit ? int64Type : intType;
8289                   type.isSigned = isSigned;
8290                }
8291             }
8292             exp.isConstant = true;
8293             if(exp.destType && exp.destType.kind == doubleType)
8294                type.kind = doubleType;
8295             else if(exp.destType && exp.destType.kind == floatType)
8296                type.kind = floatType;
8297             else if(exp.destType && exp.destType.kind == int64Type)
8298                type.kind = int64Type;
8299          }
8300          break;
8301       }
8302       case stringExp:
8303       {
8304          exp.isConstant = true;      // Why wasn't this constant?
8305          exp.expType = Type
8306          {
8307             refCount = 1;
8308             kind = pointerType;
8309             type = Type
8310             {
8311                refCount = 1;
8312                kind = exp.wideString ? shortType : charType;
8313                constant = true;
8314                isSigned = exp.wideString ? false : true;
8315             }
8316          };
8317          break;
8318       }
8319       case newExp:
8320       case new0Exp:
8321          ProcessExpressionType(exp._new.size);
8322          exp.expType = Type
8323          {
8324             refCount = 1;
8325             kind = pointerType;
8326             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8327          };
8328          DeclareType(exp.expType.type, false, false);
8329          break;
8330       case renewExp:
8331       case renew0Exp:
8332          ProcessExpressionType(exp._renew.size);
8333          ProcessExpressionType(exp._renew.exp);
8334          exp.expType = Type
8335          {
8336             refCount = 1;
8337             kind = pointerType;
8338             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8339          };
8340          DeclareType(exp.expType.type, false, false);
8341          break;
8342       case opExp:
8343       {
8344          bool assign = false, boolResult = false, boolOps = false;
8345          Type type1 = null, type2 = null;
8346          bool useDestType = false, useSideType = false;
8347          Location oldyylloc = yylloc;
8348          bool useSideUnit = false;
8349          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8350
8351          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8352          Type dummy
8353          {
8354             count = 1;
8355             refCount = 1;
8356          };
8357
8358          switch(exp.op.op)
8359          {
8360             // Assignment Operators
8361             case '=':
8362             case MUL_ASSIGN:
8363             case DIV_ASSIGN:
8364             case MOD_ASSIGN:
8365             case ADD_ASSIGN:
8366             case SUB_ASSIGN:
8367             case LEFT_ASSIGN:
8368             case RIGHT_ASSIGN:
8369             case AND_ASSIGN:
8370             case XOR_ASSIGN:
8371             case OR_ASSIGN:
8372                assign = true;
8373                break;
8374             // boolean Operators
8375             case '!':
8376                // Expect boolean operators
8377                //boolOps = true;
8378                //boolResult = true;
8379                break;
8380             case AND_OP:
8381             case OR_OP:
8382                // Expect boolean operands
8383                boolOps = true;
8384                boolResult = true;
8385                break;
8386             // Comparisons
8387             case EQ_OP:
8388             case '<':
8389             case '>':
8390             case LE_OP:
8391             case GE_OP:
8392             case NE_OP:
8393                // Gives boolean result
8394                boolResult = true;
8395                useSideType = true;
8396                break;
8397             case '+':
8398             case '-':
8399                useSideUnit = true;
8400                useSideType = true;
8401                useDestType = true;
8402                break;
8403
8404             case LEFT_OP:
8405             case RIGHT_OP:
8406                useSideType = true;
8407                useDestType = true;
8408                break;
8409
8410             case '|':
8411             case '^':
8412                useSideType = true;
8413                useDestType = true;
8414                break;
8415
8416             case '/':
8417             case '%':
8418                useSideType = true;
8419                useDestType = true;
8420                break;
8421             case '&':
8422             case '*':
8423                if(exp.op.exp1)
8424                {
8425                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8426                   useSideType = true;
8427                   useDestType = true;
8428                }
8429                break;
8430
8431             /*// Implement speed etc.
8432             case '*':
8433             case '/':
8434                break;
8435             */
8436          }
8437          if(exp.op.op == '&')
8438          {
8439             // Added this here earlier for Iterator address as key
8440             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8441             {
8442                Identifier id = exp.op.exp2.identifier;
8443                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8444                if(symbol && symbol.isIterator == 2)
8445                {
8446                   exp.type = memberExp;
8447                   exp.member.exp = exp.op.exp2;
8448                   exp.member.member = MkIdentifier("key");
8449                   exp.expType = null;
8450                   exp.op.exp2.expType = symbol.type;
8451                   symbol.type.refCount++;
8452                   ProcessExpressionType(exp);
8453                   FreeType(dummy);
8454                   break;
8455                }
8456                // exp.op.exp2.usage.usageRef = true;
8457             }
8458          }
8459
8460          //dummy.kind = TypeDummy;
8461          if(exp.op.exp1)
8462          {
8463             // Added this check here to use the dest type only for units derived from the base unit
8464             // So that untyped units will use the side unit as opposed to the untyped destination unit
8465             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8466             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8467                useDestType = false;
8468
8469             if(destClass && useDestType &&
8470               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8471
8472               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8473             {
8474                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8475                exp.op.exp1.destType = exp.destType;
8476                exp.op.exp1.opDestType = true;
8477                if(exp.destType)
8478                   exp.destType.refCount++;
8479             }
8480             else if(!assign)
8481             {
8482                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8483                exp.op.exp1.destType = dummy;
8484                dummy.refCount++;
8485             }
8486
8487             // TESTING THIS HERE...
8488             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8489                ProcessExpressionType(exp.op.exp1);
8490             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8491
8492             exp.op.exp1.opDestType = false;
8493
8494             // Fix for unit and ++ / --
8495             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8496                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8497             {
8498                exp.op.exp2 = MkExpConstant("1");
8499                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8500                assign = true;
8501             }
8502
8503             if(exp.op.exp1.destType == dummy)
8504             {
8505                FreeType(dummy);
8506                exp.op.exp1.destType = null;
8507             }
8508             type1 = exp.op.exp1.expType;
8509          }
8510
8511          if(exp.op.exp2)
8512          {
8513             char expString[10240];
8514             expString[0] = '\0';
8515             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8516             {
8517                if(exp.op.exp1)
8518                {
8519                   exp.op.exp2.destType = exp.op.exp1.expType;
8520                   if(exp.op.exp1.expType)
8521                      exp.op.exp1.expType.refCount++;
8522                }
8523                else
8524                {
8525                   exp.op.exp2.destType = exp.destType;
8526                   if(!exp.op.exp1 || exp.op.op != '&')
8527                      exp.op.exp2.opDestType = true;
8528                   if(exp.destType)
8529                      exp.destType.refCount++;
8530                }
8531
8532                if(type1) type1.refCount++;
8533                exp.expType = type1;
8534             }
8535             else if(assign)
8536             {
8537                if(inCompiler)
8538                   PrintExpression(exp.op.exp2, expString);
8539
8540                if(type1 && type1.kind == pointerType)
8541                {
8542                   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 ||
8543                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8544                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8545                   else if(exp.op.op == '=')
8546                   {
8547                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8548                      exp.op.exp2.destType = type1;
8549                      if(type1)
8550                         type1.refCount++;
8551                   }
8552                }
8553                else
8554                {
8555                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8556                   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/* ||
8557                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8558                   else
8559                   {
8560                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8561                      exp.op.exp2.destType = type1;
8562                      if(type1)
8563                         type1.refCount++;
8564                   }
8565                }
8566                if(type1) type1.refCount++;
8567                exp.expType = type1;
8568             }
8569             else if(destClass &&
8570                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8571                   (destClass.type == enumClass && useDestType)))
8572             {
8573                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8574                exp.op.exp2.destType = exp.destType;
8575                if(exp.op.op != '&')
8576                   exp.op.exp2.opDestType = true;
8577                if(exp.destType)
8578                   exp.destType.refCount++;
8579             }
8580             else
8581             {
8582                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8583                exp.op.exp2.destType = dummy;
8584                dummy.refCount++;
8585             }
8586
8587             // TESTING THIS HERE... (DANGEROUS)
8588             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8589                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8590             {
8591                FreeType(exp.op.exp2.destType);
8592                exp.op.exp2.destType = type1;
8593                type1.refCount++;
8594             }
8595             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8596             // Cannot lose the cast on a sizeof
8597             if(exp.op.op == SIZEOF)
8598             {
8599                Expression e = exp.op.exp2;
8600                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8601                {
8602                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8603                   {
8604                      if(e.type == extensionCompoundExp)
8605                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8606                      else
8607                         e = e.list->last;
8608                   }
8609                }
8610                if(e.type == castExp && e.cast.exp)
8611                   e.cast.exp.needCast = true;
8612             }
8613             ProcessExpressionType(exp.op.exp2);
8614             exp.op.exp2.opDestType = false;
8615             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8616
8617             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8618             {
8619                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)
8620                {
8621                   if(exp.op.op != '=' && type1.type.kind == voidType)
8622                      Compiler_Error($"void *: unknown size\n");
8623                }
8624                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||
8625                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8626                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8627                               exp.op.exp2.expType._class.registered.type == structClass ||
8628                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8629                {
8630                   if(exp.op.op == ADD_ASSIGN)
8631                      Compiler_Error($"cannot add two pointers\n");
8632                }
8633                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8634                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8635                {
8636                   if(exp.op.op == ADD_ASSIGN)
8637                      Compiler_Error($"cannot add two pointers\n");
8638                }
8639                else if(inCompiler)
8640                {
8641                   char type1String[1024];
8642                   char type2String[1024];
8643                   type1String[0] = '\0';
8644                   type2String[0] = '\0';
8645
8646                   PrintType(exp.op.exp2.expType, type1String, false, true);
8647                   PrintType(type1, type2String, false, true);
8648                   ChangeCh(expString, '\n', ' ');
8649                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8650                }
8651             }
8652
8653             if(exp.op.exp2.destType == dummy)
8654             {
8655                FreeType(dummy);
8656                exp.op.exp2.destType = null;
8657             }
8658
8659             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8660             {
8661                type2 = { };
8662                type2.refCount = 1;
8663                CopyTypeInto(type2, exp.op.exp2.expType);
8664                type2.isSigned = true;
8665             }
8666             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8667             {
8668                type2 = { kind = intType };
8669                type2.refCount = 1;
8670                type2.isSigned = true;
8671             }
8672             else
8673             {
8674                type2 = exp.op.exp2.expType;
8675                if(type2) type2.refCount++;
8676             }
8677          }
8678
8679          dummy.kind = voidType;
8680
8681          if(exp.op.op == SIZEOF)
8682          {
8683             exp.expType = Type
8684             {
8685                refCount = 1;
8686                kind = intSizeType;
8687             };
8688             exp.isConstant = true;
8689          }
8690          // Get type of dereferenced pointer
8691          else if(exp.op.op == '*' && !exp.op.exp1)
8692          {
8693             exp.expType = Dereference(type2);
8694             if(type2 && type2.kind == classType)
8695                notByReference = true;
8696          }
8697          else if(exp.op.op == '&' && !exp.op.exp1)
8698             exp.expType = Reference(type2);
8699          else if(!assign)
8700          {
8701             if(boolOps)
8702             {
8703                if(exp.op.exp1)
8704                {
8705                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8706                   exp.op.exp1.destType = MkClassType("bool");
8707                   exp.op.exp1.destType.truth = true;
8708                   if(!exp.op.exp1.expType)
8709                      ProcessExpressionType(exp.op.exp1);
8710                   else
8711                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8712                   FreeType(exp.op.exp1.expType);
8713                   exp.op.exp1.expType = MkClassType("bool");
8714                   exp.op.exp1.expType.truth = true;
8715                }
8716                if(exp.op.exp2)
8717                {
8718                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8719                   exp.op.exp2.destType = MkClassType("bool");
8720                   exp.op.exp2.destType.truth = true;
8721                   if(!exp.op.exp2.expType)
8722                      ProcessExpressionType(exp.op.exp2);
8723                   else
8724                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8725                   FreeType(exp.op.exp2.expType);
8726                   exp.op.exp2.expType = MkClassType("bool");
8727                   exp.op.exp2.expType.truth = true;
8728                }
8729             }
8730             else if(exp.op.exp1 && exp.op.exp2 &&
8731                ((useSideType /*&&
8732                      (useSideUnit ||
8733                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8734                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8735                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8736                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8737             {
8738                if(type1 && type2 &&
8739                   // If either both are class or both are not class
8740                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8741                {
8742                   // Added this check for enum subtraction to result in an int type:
8743                   if(exp.op.op == '-' &&
8744                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8745                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8746                   {
8747                      Type intType;
8748                      if(!type1._class.registered.dataType)
8749                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8750                      if(!type2._class.registered.dataType)
8751                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8752
8753                      intType = ProcessTypeString(
8754                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8755
8756                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8757                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8758                      exp.op.exp1.destType = intType;
8759                      exp.op.exp2.destType = intType;
8760                      intType.refCount++;
8761                   }
8762                   else
8763                   {
8764                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8765                      exp.op.exp2.destType = type1;
8766                      type1.refCount++;
8767                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8768                      exp.op.exp1.destType = type2;
8769                      type2.refCount++;
8770                   }
8771
8772                   // Warning here for adding Radians + Degrees with no destination type
8773                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8774                      type1._class.registered && type1._class.registered.type == unitClass &&
8775                      type2._class.registered && type2._class.registered.type == unitClass &&
8776                      type1._class.registered != type2._class.registered)
8777                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8778                         type1._class.string, type2._class.string, type1._class.string);
8779
8780                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8781                   {
8782                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8783                      if(argExp)
8784                      {
8785                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8786
8787                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8788                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8789                            exp.op.exp1)));
8790
8791                         ProcessExpressionType(exp.op.exp1);
8792
8793                         if(type2.kind != pointerType)
8794                         {
8795                            ProcessExpressionType(classExp);
8796
8797                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8798
8799                            if(!exp.op.exp2.expType)
8800                            {
8801                               if(type2)
8802                                  FreeType(type2);
8803                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8804                               type2.refCount++;
8805                            }
8806
8807                            ProcessExpressionType(exp.op.exp2);
8808                         }
8809                      }
8810                   }
8811
8812                   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)))
8813                   {
8814                      if(type1.kind != classType && type1.type.kind == voidType)
8815                         Compiler_Error($"void *: unknown size\n");
8816                      exp.expType = type1;
8817                      if(type1) type1.refCount++;
8818                   }
8819                   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)))
8820                   {
8821                      if(type2.kind != classType && type2.type.kind == voidType)
8822                         Compiler_Error($"void *: unknown size\n");
8823                      exp.expType = type2;
8824                      if(type2) type2.refCount++;
8825                   }
8826                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8827                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8828                   {
8829                      Compiler_Warning($"different levels of indirection\n");
8830                   }
8831                   else
8832                   {
8833                      bool success = false;
8834                      if(type1.kind == pointerType && type2.kind == pointerType)
8835                      {
8836                         if(exp.op.op == '+')
8837                            Compiler_Error($"cannot add two pointers\n");
8838                         else if(exp.op.op == '-')
8839                         {
8840                            // Pointer Subtraction gives integer
8841                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8842                            {
8843                               exp.expType = Type
8844                               {
8845                                  kind = intType;
8846                                  refCount = 1;
8847                               };
8848                               success = true;
8849
8850                               if(type1.type.kind == templateType)
8851                               {
8852                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8853                                  if(argExp)
8854                                  {
8855                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8856
8857                                     ProcessExpressionType(classExp);
8858
8859                                     exp.type = bracketsExp;
8860                                     exp.list = MkListOne(MkExpOp(
8861                                        MkExpBrackets(MkListOne(MkExpOp(
8862                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8863                                              , exp.op.op,
8864                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8865                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8866
8867                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8868                                     FreeType(dummy);
8869                                     return;
8870                                  }
8871                               }
8872                            }
8873                         }
8874                      }
8875
8876                      if(!success && exp.op.exp1.type == constantExp)
8877                      {
8878                         // If first expression is constant, try to match that first
8879                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8880                         {
8881                            if(exp.expType) FreeType(exp.expType);
8882                            exp.expType = exp.op.exp1.destType;
8883                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8884                            success = true;
8885                         }
8886                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8887                         {
8888                            if(exp.expType) FreeType(exp.expType);
8889                            exp.expType = exp.op.exp2.destType;
8890                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8891                            success = true;
8892                         }
8893                      }
8894                      else if(!success)
8895                      {
8896                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8897                         {
8898                            if(exp.expType) FreeType(exp.expType);
8899                            exp.expType = exp.op.exp2.destType;
8900                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8901                            success = true;
8902                         }
8903                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8904                         {
8905                            if(exp.expType) FreeType(exp.expType);
8906                            exp.expType = exp.op.exp1.destType;
8907                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8908                            success = true;
8909                         }
8910                      }
8911                      if(!success)
8912                      {
8913                         char expString1[10240];
8914                         char expString2[10240];
8915                         char type1[1024];
8916                         char type2[1024];
8917                         expString1[0] = '\0';
8918                         expString2[0] = '\0';
8919                         type1[0] = '\0';
8920                         type2[0] = '\0';
8921                         if(inCompiler)
8922                         {
8923                            PrintExpression(exp.op.exp1, expString1);
8924                            ChangeCh(expString1, '\n', ' ');
8925                            PrintExpression(exp.op.exp2, expString2);
8926                            ChangeCh(expString2, '\n', ' ');
8927                            PrintType(exp.op.exp1.expType, type1, false, true);
8928                            PrintType(exp.op.exp2.expType, type2, false, true);
8929                         }
8930
8931                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8932                      }
8933                   }
8934                }
8935                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8936                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8937                {
8938                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8939                   // Convert e.g. / 4 into / 4.0
8940                   exp.op.exp1.destType = type2._class.registered.dataType;
8941                   if(type2._class.registered.dataType)
8942                      type2._class.registered.dataType.refCount++;
8943                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8944                   exp.expType = type2;
8945                   if(type2) type2.refCount++;
8946                }
8947                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8948                {
8949                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8950                   // Convert e.g. / 4 into / 4.0
8951                   exp.op.exp2.destType = type1._class.registered.dataType;
8952                   if(type1._class.registered.dataType)
8953                      type1._class.registered.dataType.refCount++;
8954                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8955                   exp.expType = type1;
8956                   if(type1) type1.refCount++;
8957                }
8958                else if(type1)
8959                {
8960                   bool valid = false;
8961
8962                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8963                   {
8964                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8965
8966                      if(!type1._class.registered.dataType)
8967                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8968                      exp.op.exp2.destType = type1._class.registered.dataType;
8969                      exp.op.exp2.destType.refCount++;
8970
8971                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8972                      if(type2)
8973                         FreeType(type2);
8974                      type2 = exp.op.exp2.destType;
8975                      if(type2) type2.refCount++;
8976
8977                      exp.expType = type2;
8978                      type2.refCount++;
8979                   }
8980
8981                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8982                   {
8983                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8984
8985                      if(!type2._class.registered.dataType)
8986                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8987                      exp.op.exp1.destType = type2._class.registered.dataType;
8988                      exp.op.exp1.destType.refCount++;
8989
8990                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8991                      type1 = exp.op.exp1.destType;
8992                      exp.expType = type1;
8993                      type1.refCount++;
8994                   }
8995
8996                   // TESTING THIS NEW CODE
8997                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8998                   {
8999                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
9000                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
9001                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
9002                      {
9003                         // Convert the enum to an int instead for these operators
9004                         if(op1IsEnum && exp.op.exp2.expType)
9005                         {
9006                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
9007                            {
9008                               if(exp.expType) FreeType(exp.expType);
9009                               exp.expType = exp.op.exp2.expType;
9010                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9011                               valid = true;
9012                            }
9013                         }
9014                         else if(op2IsEnum && exp.op.exp1.expType)
9015                         {
9016                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
9017                            {
9018                               if(exp.expType) FreeType(exp.expType);
9019                               exp.expType = exp.op.exp1.expType;
9020                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9021                               valid = true;
9022                            }
9023                         }
9024                      }
9025                      else
9026                      {
9027                         if(op1IsEnum && exp.op.exp2.expType)
9028                         {
9029                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
9030                            {
9031                               if(exp.expType) FreeType(exp.expType);
9032                               exp.expType = exp.op.exp1.expType;
9033                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9034                               valid = true;
9035                            }
9036                         }
9037                         else if(op2IsEnum && exp.op.exp1.expType)
9038                         {
9039                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
9040                            {
9041                               if(exp.expType) FreeType(exp.expType);
9042                               exp.expType = exp.op.exp2.expType;
9043                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9044                               valid = true;
9045                            }
9046                         }
9047                      }
9048                   }
9049
9050                   if(!valid)
9051                   {
9052                      // 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
9053                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
9054                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
9055                      {
9056                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9057                         exp.op.exp1.destType = type2;
9058                         type2.refCount++;
9059
9060                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9061                         {
9062                            if(exp.expType) FreeType(exp.expType);
9063                            exp.expType = exp.op.exp1.destType;
9064                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9065                         }
9066                      }
9067                      else
9068                      {
9069                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9070                         exp.op.exp2.destType = type1;
9071                         type1.refCount++;
9072
9073                      /*
9074                      // Maybe this was meant to be an enum...
9075                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9076                      {
9077                         Type oldType = exp.op.exp2.expType;
9078                         exp.op.exp2.expType = null;
9079                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
9080                            FreeType(oldType);
9081                         else
9082                            exp.op.exp2.expType = oldType;
9083                      }
9084                      */
9085
9086                      /*
9087                      // TESTING THIS HERE... LATEST ADDITION
9088                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9089                      {
9090                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9091                         exp.op.exp2.destType = type2._class.registered.dataType;
9092                         if(type2._class.registered.dataType)
9093                            type2._class.registered.dataType.refCount++;
9094                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
9095
9096                         //exp.expType = type2._class.registered.dataType; //type2;
9097                         //if(type2) type2.refCount++;
9098                      }
9099
9100                      // TESTING THIS HERE... LATEST ADDITION
9101                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9102                      {
9103                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9104                         exp.op.exp1.destType = type1._class.registered.dataType;
9105                         if(type1._class.registered.dataType)
9106                            type1._class.registered.dataType.refCount++;
9107                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
9108                         exp.expType = type1._class.registered.dataType; //type1;
9109                         if(type1) type1.refCount++;
9110                      }
9111                      */
9112
9113                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
9114                         {
9115                            if(exp.expType) FreeType(exp.expType);
9116                            exp.expType = exp.op.exp2.destType;
9117                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
9118                         }
9119                         else if(type1 && type2)
9120                         {
9121                            char expString1[10240];
9122                            char expString2[10240];
9123                            char type1String[1024];
9124                            char type2String[1024];
9125                            expString1[0] = '\0';
9126                            expString2[0] = '\0';
9127                            type1String[0] = '\0';
9128                            type2String[0] = '\0';
9129                            if(inCompiler)
9130                            {
9131                               PrintExpression(exp.op.exp1, expString1);
9132                               ChangeCh(expString1, '\n', ' ');
9133                               PrintExpression(exp.op.exp2, expString2);
9134                               ChangeCh(expString2, '\n', ' ');
9135                               PrintType(exp.op.exp1.expType, type1String, false, true);
9136                               PrintType(exp.op.exp2.expType, type2String, false, true);
9137                            }
9138
9139                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9140
9141                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9142                            {
9143                               exp.expType = exp.op.exp1.expType;
9144                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9145                            }
9146                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9147                            {
9148                               exp.expType = exp.op.exp2.expType;
9149                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9150                            }
9151                         }
9152                      }
9153                   }
9154                }
9155                else if(type2)
9156                {
9157                   // Maybe this was meant to be an enum...
9158                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9159                   {
9160                      Type oldType = exp.op.exp1.expType;
9161                      exp.op.exp1.expType = null;
9162                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9163                         FreeType(oldType);
9164                      else
9165                         exp.op.exp1.expType = oldType;
9166                   }
9167
9168                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9169                   exp.op.exp1.destType = type2;
9170                   type2.refCount++;
9171                   /*
9172                   // TESTING THIS HERE... LATEST ADDITION
9173                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9174                   {
9175                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9176                      exp.op.exp1.destType = type1._class.registered.dataType;
9177                      if(type1._class.registered.dataType)
9178                         type1._class.registered.dataType.refCount++;
9179                   }
9180
9181                   // TESTING THIS HERE... LATEST ADDITION
9182                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9183                   {
9184                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9185                      exp.op.exp2.destType = type2._class.registered.dataType;
9186                      if(type2._class.registered.dataType)
9187                         type2._class.registered.dataType.refCount++;
9188                   }
9189                   */
9190
9191                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9192                   {
9193                      if(exp.expType) FreeType(exp.expType);
9194                      exp.expType = exp.op.exp1.destType;
9195                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9196                   }
9197                }
9198             }
9199             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9200             {
9201                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9202                {
9203                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9204                   // Convert e.g. / 4 into / 4.0
9205                   exp.op.exp1.destType = type2._class.registered.dataType;
9206                   if(type2._class.registered.dataType)
9207                      type2._class.registered.dataType.refCount++;
9208                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9209                }
9210                if(exp.op.op == '!')
9211                {
9212                   exp.expType = MkClassType("bool");
9213                   exp.expType.truth = true;
9214                }
9215                else
9216                {
9217                   exp.expType = type2;
9218                   if(type2) type2.refCount++;
9219                }
9220             }
9221             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9222             {
9223                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9224                {
9225                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9226                   // Convert e.g. / 4 into / 4.0
9227                   exp.op.exp2.destType = type1._class.registered.dataType;
9228                   if(type1._class.registered.dataType)
9229                      type1._class.registered.dataType.refCount++;
9230                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9231                }
9232                exp.expType = type1;
9233                if(type1) type1.refCount++;
9234             }
9235          }
9236
9237          yylloc = exp.loc;
9238          if(exp.op.exp1 && !exp.op.exp1.expType)
9239          {
9240             char expString[10000];
9241             expString[0] = '\0';
9242             if(inCompiler)
9243             {
9244                PrintExpression(exp.op.exp1, expString);
9245                ChangeCh(expString, '\n', ' ');
9246             }
9247             if(expString[0])
9248                Compiler_Error($"couldn't determine type of %s\n", expString);
9249          }
9250          if(exp.op.exp2 && !exp.op.exp2.expType)
9251          {
9252             char expString[10240];
9253             expString[0] = '\0';
9254             if(inCompiler)
9255             {
9256                PrintExpression(exp.op.exp2, expString);
9257                ChangeCh(expString, '\n', ' ');
9258             }
9259             if(expString[0])
9260                Compiler_Error($"couldn't determine type of %s\n", expString);
9261          }
9262
9263          if(boolResult)
9264          {
9265             FreeType(exp.expType);
9266             exp.expType = MkClassType("bool");
9267             exp.expType.truth = true;
9268          }
9269
9270          if(exp.op.op != SIZEOF)
9271             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9272                (!exp.op.exp2 || exp.op.exp2.isConstant);
9273
9274          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9275          {
9276             DeclareType(exp.op.exp2.expType, false, false);
9277          }
9278
9279          if(exp.op.op == DELETE && exp.op.exp2 && exp.op.exp2.expType && exp.op.exp2.expType.specConst)
9280             Compiler_Warning($"deleting const qualified object\n");
9281
9282          yylloc = oldyylloc;
9283
9284          FreeType(dummy);
9285          if(type2)
9286             FreeType(type2);
9287          break;
9288       }
9289       case bracketsExp:
9290       case extensionExpressionExp:
9291       {
9292          Expression e;
9293          exp.isConstant = true;
9294          for(e = exp.list->first; e; e = e.next)
9295          {
9296             bool inced = false;
9297             if(!e.next)
9298             {
9299                FreeType(e.destType);
9300                e.opDestType = exp.opDestType;
9301                e.destType = exp.destType;
9302                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9303             }
9304             ProcessExpressionType(e);
9305             if(inced)
9306                exp.destType.count--;
9307             if(!exp.expType && !e.next)
9308             {
9309                exp.expType = e.expType;
9310                if(e.expType) e.expType.refCount++;
9311             }
9312             if(!e.isConstant)
9313                exp.isConstant = false;
9314          }
9315
9316          // In case a cast became a member...
9317          e = exp.list->first;
9318          if(!e.next && e.type == memberExp)
9319          {
9320             // Preserve prev, next
9321             Expression next = exp.next, prev = exp.prev;
9322
9323
9324             FreeType(exp.expType);
9325             FreeType(exp.destType);
9326             delete exp.list;
9327
9328             *exp = *e;
9329
9330             exp.prev = prev;
9331             exp.next = next;
9332
9333             delete e;
9334
9335             ProcessExpressionType(exp);
9336          }
9337          break;
9338       }
9339       case indexExp:
9340       {
9341          Expression e;
9342          exp.isConstant = true;
9343
9344          ProcessExpressionType(exp.index.exp);
9345          if(!exp.index.exp.isConstant)
9346             exp.isConstant = false;
9347
9348          if(exp.index.exp.expType)
9349          {
9350             Type source = exp.index.exp.expType;
9351             if(source.kind == classType && source._class && source._class.registered)
9352             {
9353                Class _class = source._class.registered;
9354                Class c = _class.templateClass ? _class.templateClass : _class;
9355                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9356                {
9357                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9358
9359                   if(exp.index.index && exp.index.index->last)
9360                   {
9361                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9362
9363                      if(type.kind == classType) type.constant = true;
9364                      else if(type.kind == pointerType)
9365                      {
9366                         Type t = type;
9367                         while(t.kind == pointerType) t = t.type;
9368                         t.constant = true;
9369                      }
9370
9371                      ((Expression)exp.index.index->last).destType = type;
9372                   }
9373                }
9374             }
9375          }
9376
9377          for(e = exp.index.index->first; e; e = e.next)
9378          {
9379             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9380             {
9381                if(e.destType) FreeType(e.destType);
9382                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9383             }
9384             ProcessExpressionType(e);
9385             if(!e.next)
9386             {
9387                // Check if this type is int
9388             }
9389             if(!e.isConstant)
9390                exp.isConstant = false;
9391          }
9392
9393          if(!exp.expType)
9394             exp.expType = Dereference(exp.index.exp.expType);
9395          if(exp.expType)
9396             DeclareType(exp.expType, false, false);
9397          break;
9398       }
9399       case callExp:
9400       {
9401          Expression e;
9402          Type functionType;
9403          Type methodType = null;
9404          char name[1024];
9405          name[0] = '\0';
9406
9407          if(inCompiler)
9408          {
9409             PrintExpression(exp.call.exp,  name);
9410             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9411             {
9412                //exp.call.exp.expType = null;
9413                PrintExpression(exp.call.exp,  name);
9414             }
9415          }
9416          if(exp.call.exp.type == identifierExp)
9417          {
9418             Expression idExp = exp.call.exp;
9419             Identifier id = idExp.identifier;
9420             if(!strcmp(id.string, "__builtin_frame_address"))
9421             {
9422                exp.expType = ProcessTypeString("void *", true);
9423                if(exp.call.arguments && exp.call.arguments->first)
9424                   ProcessExpressionType(exp.call.arguments->first);
9425                break;
9426             }
9427             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9428             {
9429                exp.expType = ProcessTypeString("int", true);
9430                if(exp.call.arguments && exp.call.arguments->first)
9431                   ProcessExpressionType(exp.call.arguments->first);
9432                break;
9433             }
9434             else if(!strcmp(id.string, "Max") ||
9435                !strcmp(id.string, "Min") ||
9436                !strcmp(id.string, "Sgn") ||
9437                !strcmp(id.string, "Abs"))
9438             {
9439                Expression a = null;
9440                Expression b = null;
9441                Expression tempExp1 = null, tempExp2 = null;
9442                if((!strcmp(id.string, "Max") ||
9443                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9444                {
9445                   a = exp.call.arguments->first;
9446                   b = exp.call.arguments->last;
9447                   tempExp1 = a;
9448                   tempExp2 = b;
9449                }
9450                else if(exp.call.arguments->count == 1)
9451                {
9452                   a = exp.call.arguments->first;
9453                   tempExp1 = a;
9454                }
9455
9456                if(a)
9457                {
9458                   exp.call.arguments->Clear();
9459                   idExp.identifier = null;
9460
9461                   FreeExpContents(exp);
9462
9463                   ProcessExpressionType(a);
9464                   if(b)
9465                      ProcessExpressionType(b);
9466
9467                   exp.type = bracketsExp;
9468                   exp.list = MkList();
9469
9470                   if(a.expType && (!b || b.expType))
9471                   {
9472                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9473                      {
9474                         // Use the simpleStruct name/ids for now...
9475                         if(inCompiler)
9476                         {
9477                            OldList * specs = MkList();
9478                            OldList * decls = MkList();
9479                            Declaration decl;
9480                            char temp1[1024], temp2[1024];
9481
9482                            GetTypeSpecs(a.expType, specs);
9483
9484                            if(a && !a.isConstant && a.type != identifierExp)
9485                            {
9486                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9487                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9488                               tempExp1 = QMkExpId(temp1);
9489                               tempExp1.expType = a.expType;
9490                               if(a.expType)
9491                                  a.expType.refCount++;
9492                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9493                            }
9494                            if(b && !b.isConstant && b.type != identifierExp)
9495                            {
9496                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9497                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9498                               tempExp2 = QMkExpId(temp2);
9499                               tempExp2.expType = b.expType;
9500                               if(b.expType)
9501                                  b.expType.refCount++;
9502                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9503                            }
9504
9505                            decl = MkDeclaration(specs, decls);
9506                            if(!curCompound.compound.declarations)
9507                               curCompound.compound.declarations = MkList();
9508                            curCompound.compound.declarations->Insert(null, decl);
9509                         }
9510                      }
9511                   }
9512
9513                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9514                   {
9515                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9516                      ListAdd(exp.list,
9517                         MkExpCondition(MkExpBrackets(MkListOne(
9518                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9519                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9520                      exp.expType = a.expType;
9521                      if(a.expType)
9522                         a.expType.refCount++;
9523                   }
9524                   else if(!strcmp(id.string, "Abs"))
9525                   {
9526                      ListAdd(exp.list,
9527                         MkExpCondition(MkExpBrackets(MkListOne(
9528                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9529                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9530                      exp.expType = a.expType;
9531                      if(a.expType)
9532                         a.expType.refCount++;
9533                   }
9534                   else if(!strcmp(id.string, "Sgn"))
9535                   {
9536                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9537                      ListAdd(exp.list,
9538                         MkExpCondition(MkExpBrackets(MkListOne(
9539                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9540                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9541                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9542                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9543                      exp.expType = ProcessTypeString("int", false);
9544                   }
9545
9546                   FreeExpression(tempExp1);
9547                   if(tempExp2) FreeExpression(tempExp2);
9548
9549                   FreeIdentifier(id);
9550                   break;
9551                }
9552             }
9553          }
9554
9555          {
9556             Type dummy
9557             {
9558                count = 1;
9559                refCount = 1;
9560             };
9561             if(!exp.call.exp.destType)
9562             {
9563                exp.call.exp.destType = dummy;
9564                dummy.refCount++;
9565             }
9566             ProcessExpressionType(exp.call.exp);
9567             if(exp.call.exp.destType == dummy)
9568             {
9569                FreeType(dummy);
9570                exp.call.exp.destType = null;
9571             }
9572             FreeType(dummy);
9573          }
9574
9575          // Check argument types against parameter types
9576          functionType = exp.call.exp.expType;
9577
9578          if(functionType && functionType.kind == TypeKind::methodType)
9579          {
9580             methodType = functionType;
9581             functionType = methodType.method.dataType;
9582
9583             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9584             // TOCHECK: Instead of doing this here could this be done per param?
9585             if(exp.call.exp.expType.usedClass)
9586             {
9587                char typeString[1024];
9588                typeString[0] = '\0';
9589                {
9590                   Symbol back = functionType.thisClass;
9591                   // Do not output class specifier here (thisclass was added to this)
9592                   functionType.thisClass = null;
9593                   PrintType(functionType, typeString, true, true);
9594                   functionType.thisClass = back;
9595                }
9596                if(strstr(typeString, "thisclass"))
9597                {
9598                   OldList * specs = MkList();
9599                   Declarator decl;
9600                   {
9601                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9602
9603                      decl = SpecDeclFromString(typeString, specs, null);
9604
9605                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9606                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9607                         exp.call.exp.expType.usedClass))
9608                         thisClassParams = false;
9609
9610                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9611                      {
9612                         Class backupThisClass = thisClass;
9613                         thisClass = exp.call.exp.expType.usedClass;
9614                         ProcessDeclarator(decl);
9615                         thisClass = backupThisClass;
9616                      }
9617
9618                      thisClassParams = true;
9619
9620                      functionType = ProcessType(specs, decl);
9621                      functionType.refCount = 0;
9622                      FinishTemplatesContext(context);
9623
9624                      // Mark parameters that were 'thisclass'
9625                      /*{
9626                         Type p, op;
9627                         for(p = functionType.params.first, op = methodType.method.dataType.params.first; p && op; p = p.next, op = op.next)
9628                            p.wasThisClass = op.kind == thisClassType;
9629                      }*/
9630                   }
9631
9632                   FreeList(specs, FreeSpecifier);
9633                   FreeDeclarator(decl);
9634                 }
9635             }
9636          }
9637          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9638          {
9639             Type type = functionType.type;
9640             if(!functionType.refCount)
9641             {
9642                functionType.type = null;
9643                FreeType(functionType);
9644             }
9645             //methodType = functionType;
9646             functionType = type;
9647          }
9648          if(functionType && functionType.kind != TypeKind::functionType)
9649          {
9650             Compiler_Error($"called object %s is not a function\n", name);
9651          }
9652          else if(functionType)
9653          {
9654             bool emptyParams = false, noParams = false;
9655             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9656             Type type = functionType.params.first;
9657             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9658             int extra = 0;
9659             Location oldyylloc = yylloc;
9660
9661             if(!type) emptyParams = true;
9662
9663             // WORKING ON THIS:
9664             if(functionType.extraParam && e && functionType.thisClass)
9665             {
9666                e.destType = MkClassType(functionType.thisClass.string);
9667                e = e.next;
9668             }
9669
9670             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9671             // Fixed #141 by adding '&& !functionType.extraParam'
9672             if(!functionType.staticMethod && !functionType.extraParam)
9673             {
9674                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9675                   memberExp.member.exp.expType._class)
9676                {
9677                   type = MkClassType(memberExp.member.exp.expType._class.string);
9678                   if(e)
9679                   {
9680                      e.destType = type;
9681                      e = e.next;
9682                      type = functionType.params.first;
9683                   }
9684                   else
9685                      type.refCount = 0;
9686                }
9687                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9688                {
9689                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9690                   type.byReference = functionType.byReference;
9691                   type.typedByReference = functionType.typedByReference;
9692                   if(e)
9693                   {
9694                      // Allow manually passing a class for typed object
9695                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9696                         e = e.next;
9697                      e.destType = type;
9698                      e = e.next;
9699                      type = functionType.params.first;
9700                   }
9701                   else
9702                      type.refCount = 0;
9703                   //extra = 1;
9704                }
9705             }
9706
9707             if(type && type.kind == voidType)
9708             {
9709                noParams = true;
9710                if(!type.refCount) FreeType(type);
9711                type = null;
9712             }
9713
9714             for( ; e; e = e.next)
9715             {
9716                if(!type && !emptyParams)
9717                {
9718                   yylloc = e.loc;
9719                   if(methodType && methodType.methodClass)
9720                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9721                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9722                         noParams ? 0 : functionType.params.count);
9723                   else
9724                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9725                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9726                         noParams ? 0 : functionType.params.count);
9727                   break;
9728                }
9729
9730                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9731                {
9732                   Type templatedType = null;
9733                   Class _class = methodType.usedClass;
9734                   ClassTemplateParameter curParam = null;
9735                   int id = 0;
9736                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9737                   {
9738                      Class sClass;
9739                      for(sClass = _class; sClass; sClass = sClass.base)
9740                      {
9741                         if(sClass.templateClass) sClass = sClass.templateClass;
9742                         id = 0;
9743                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9744                         {
9745                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9746                            {
9747                               Class nextClass;
9748                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9749                               {
9750                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9751                                  id += nextClass.templateParams.count;
9752                               }
9753                               break;
9754                            }
9755                            id++;
9756                         }
9757                         if(curParam) break;
9758                      }
9759                   }
9760                   if(curParam && _class.templateArgs[id].dataTypeString)
9761                   {
9762                      bool constant = type.constant;
9763                      ClassTemplateArgument arg = _class.templateArgs[id];
9764                      {
9765                         Context context = SetupTemplatesContext(_class);
9766
9767                         /*if(!arg.dataType)
9768                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9769                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9770                         FinishTemplatesContext(context);
9771                      }
9772
9773                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9774                      else if(templatedType.kind == pointerType)
9775                      {
9776                         Type t = templatedType.type;
9777                         while(t.kind == pointerType) t = t.type;
9778                         if(constant) t.constant = constant;
9779                      }
9780
9781                      e.destType = templatedType;
9782                      if(templatedType)
9783                      {
9784                         templatedType.passAsTemplate = true;
9785                         // templatedType.refCount++;
9786                      }
9787                   }
9788                   else
9789                   {
9790                      e.destType = type;
9791                      if(type) type.refCount++;
9792                   }
9793                }
9794                else
9795                {
9796                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9797                   {
9798                      e.destType = type.prev;
9799                      e.destType.refCount++;
9800                   }
9801                   else
9802                   {
9803                      e.destType = type;
9804                      if(type) type.refCount++;
9805                   }
9806                }
9807                // Don't reach the end for the ellipsis
9808                if(type && type.kind != ellipsisType)
9809                {
9810                   Type next = type.next;
9811                   if(!type.refCount) FreeType(type);
9812                   type = next;
9813                }
9814             }
9815
9816             if(type && type.kind != ellipsisType)
9817             {
9818                if(methodType && methodType.methodClass)
9819                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9820                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9821                      functionType.params.count + extra);
9822                else
9823                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9824                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9825                      functionType.params.count + extra);
9826             }
9827             yylloc = oldyylloc;
9828             if(type && !type.refCount) FreeType(type);
9829          }
9830          else
9831          {
9832             functionType = Type
9833             {
9834                refCount = 0;
9835                kind = TypeKind::functionType;
9836             };
9837
9838             if(exp.call.exp.type == identifierExp)
9839             {
9840                char * string = exp.call.exp.identifier.string;
9841                if(inCompiler)
9842                {
9843                   Symbol symbol;
9844                   Location oldyylloc = yylloc;
9845
9846                   yylloc = exp.call.exp.identifier.loc;
9847                   if(strstr(string, "__builtin_") == string)
9848                   {
9849                      if(exp.destType)
9850                      {
9851                         functionType.returnType = exp.destType;
9852                         exp.destType.refCount++;
9853                      }
9854                   }
9855                   else
9856                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9857                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9858                   globalContext.symbols.Add((BTNode)symbol);
9859                   if(strstr(symbol.string, "::"))
9860                      globalContext.hasNameSpace = true;
9861
9862                   yylloc = oldyylloc;
9863                }
9864             }
9865             else if(exp.call.exp.type == memberExp)
9866             {
9867                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9868                   exp.call.exp.member.member.string);*/
9869             }
9870             else
9871                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9872
9873             if(!functionType.returnType)
9874             {
9875                functionType.returnType = Type
9876                {
9877                   refCount = 1;
9878                   kind = intType;
9879                };
9880             }
9881          }
9882          if(functionType && functionType.kind == TypeKind::functionType)
9883          {
9884             exp.expType = functionType.returnType;
9885
9886             if(functionType.returnType)
9887                functionType.returnType.refCount++;
9888
9889             if(!functionType.refCount)
9890                FreeType(functionType);
9891          }
9892
9893          if(exp.call.arguments)
9894          {
9895             for(e = exp.call.arguments->first; e; e = e.next)
9896                ProcessExpressionType(e);
9897          }
9898          break;
9899       }
9900       case memberExp:
9901       {
9902          Type type;
9903          Location oldyylloc = yylloc;
9904          bool thisPtr;
9905          Expression checkExp = exp.member.exp;
9906          while(checkExp)
9907          {
9908             if(checkExp.type == castExp)
9909                checkExp = checkExp.cast.exp;
9910             else if(checkExp.type == bracketsExp)
9911                checkExp = checkExp.list ? checkExp.list->first : null;
9912             else
9913                break;
9914          }
9915
9916          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9917          exp.thisPtr = thisPtr;
9918
9919          // DOING THIS LATER NOW...
9920          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9921          {
9922             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9923             /* TODO: Name Space Fix ups
9924             if(!exp.member.member.classSym)
9925                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9926             */
9927          }
9928
9929          ProcessExpressionType(exp.member.exp);
9930          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9931             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9932          {
9933             exp.isConstant = false;
9934          }
9935          else
9936             exp.isConstant = exp.member.exp.isConstant;
9937          type = exp.member.exp.expType;
9938
9939          yylloc = exp.loc;
9940
9941          if(type && (type.kind == templateType))
9942          {
9943             Class _class = thisClass ? thisClass : currentClass;
9944             ClassTemplateParameter param = null;
9945             if(_class)
9946             {
9947                for(param = _class.templateParams.first; param; param = param.next)
9948                {
9949                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9950                      break;
9951                }
9952             }
9953             if(param && param.defaultArg.member)
9954             {
9955                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9956                if(argExp)
9957                {
9958                   Expression expMember = exp.member.exp;
9959                   Declarator decl;
9960                   OldList * specs = MkList();
9961                   char thisClassTypeString[1024];
9962
9963                   FreeIdentifier(exp.member.member);
9964
9965                   ProcessExpressionType(argExp);
9966
9967                   {
9968                      char * colon = strstr(param.defaultArg.memberString, "::");
9969                      if(colon)
9970                      {
9971                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9972                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9973                      }
9974                      else
9975                         strcpy(thisClassTypeString, _class.fullName);
9976                   }
9977
9978                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9979
9980                   exp.expType = ProcessType(specs, decl);
9981                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9982                   {
9983                      Class expClass = exp.expType._class.registered;
9984                      Class cClass = null;
9985                      int paramCount = 0;
9986                      int lastParam = -1;
9987
9988                      char templateString[1024];
9989                      ClassTemplateParameter param;
9990                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9991                      for(cClass = expClass; cClass; cClass = cClass.base)
9992                      {
9993                         int p = 0;
9994                         for(param = cClass.templateParams.first; param; param = param.next)
9995                         {
9996                            int id = p;
9997                            Class sClass;
9998                            ClassTemplateArgument arg;
9999                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
10000                            arg = expClass.templateArgs[id];
10001
10002                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
10003                            {
10004                               ClassTemplateParameter cParam;
10005                               //int p = numParams - sClass.templateParams.count;
10006                               int p = 0;
10007                               Class nextClass;
10008                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
10009
10010                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
10011                               {
10012                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
10013                                  {
10014                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10015                                     {
10016                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
10017                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
10018                                        break;
10019                                     }
10020                                  }
10021                               }
10022                            }
10023
10024                            {
10025                               char argument[256];
10026                               argument[0] = '\0';
10027                               /*if(arg.name)
10028                               {
10029                                  strcat(argument, arg.name.string);
10030                                  strcat(argument, " = ");
10031                               }*/
10032                               switch(param.type)
10033                               {
10034                                  case expression:
10035                                  {
10036                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10037                                     char expString[1024];
10038                                     OldList * specs = MkList();
10039                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10040                                     Expression exp;
10041                                     char * string = PrintHexUInt64(arg.expression.ui64);
10042                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10043                                     delete string;
10044
10045                                     ProcessExpressionType(exp);
10046                                     ComputeExpression(exp);
10047                                     expString[0] = '\0';
10048                                     PrintExpression(exp, expString);
10049                                     strcat(argument, expString);
10050                                     // delete exp;
10051                                     FreeExpression(exp);
10052                                     break;
10053                                  }
10054                                  case identifier:
10055                                  {
10056                                     strcat(argument, arg.member.name);
10057                                     break;
10058                                  }
10059                                  case TemplateParameterType::type:
10060                                  {
10061                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10062                                     {
10063                                        if(!strcmp(arg.dataTypeString, "thisclass"))
10064                                           strcat(argument, thisClassTypeString);
10065                                        else
10066                                           strcat(argument, arg.dataTypeString);
10067                                     }
10068                                     break;
10069                                  }
10070                               }
10071                               if(argument[0])
10072                               {
10073                                  if(paramCount) strcat(templateString, ", ");
10074                                  if(lastParam != p - 1)
10075                                  {
10076                                     strcat(templateString, param.name);
10077                                     strcat(templateString, " = ");
10078                                  }
10079                                  strcat(templateString, argument);
10080                                  paramCount++;
10081                                  lastParam = p;
10082                               }
10083                               p++;
10084                            }
10085                         }
10086                      }
10087                      {
10088                         int len = strlen(templateString);
10089                         if(templateString[len-1] == '>') templateString[len++] = ' ';
10090                         templateString[len++] = '>';
10091                         templateString[len++] = '\0';
10092                      }
10093                      {
10094                         Context context = SetupTemplatesContext(_class);
10095                         FreeType(exp.expType);
10096                         exp.expType = ProcessTypeString(templateString, false);
10097                         FinishTemplatesContext(context);
10098                      }
10099                   }
10100
10101                   if(!expMember.expType.isPointerType)
10102                      expMember = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), expMember);
10103                   // *([expType] *)(((byte *)(uintptr)[exp.member.exp]) + [argExp].member.offset)
10104                   exp.type = bracketsExp;
10105                   exp.list = MkListOne(MkExpOp(null, '*',
10106                   /*opExp;
10107                   exp.op.op = '*';
10108                   exp.op.exp1 = null;
10109                   exp.op.exp2 = */
10110                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
10111                      MkExpBrackets(MkListOne(
10112                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
10113                            expMember))),
10114                               '+',
10115                               MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
10116                               '+',
10117                               MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
10118
10119                            ));
10120                }
10121             }
10122             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
10123                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
10124             {
10125                type = ProcessTemplateParameterType(type.templateParameter);
10126             }
10127          }
10128          // TODO: *** This seems to be where we should add method support for all basic types ***
10129          if(type && (type.kind == templateType));
10130          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
10131                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
10132                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
10133                           (type.kind == pointerType && type.type.kind == charType)))
10134          {
10135             Identifier id = exp.member.member;
10136             TypeKind typeKind = type.kind;
10137             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10138             if(typeKind == subClassType && exp.member.exp.type == classExp)
10139             {
10140                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10141                typeKind = classType;
10142             }
10143
10144             if(id)
10145             {
10146                if(typeKind == intType || typeKind == enumType)
10147                   _class = eSystem_FindClass(privateModule, "int");
10148                else if(!_class)
10149                {
10150                   if(type.kind == classType && type._class && type._class.registered)
10151                   {
10152                      _class = type._class.registered;
10153                   }
10154                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10155                   {
10156                      _class = FindClass("char *").registered;
10157                   }
10158                   else if(type.kind == pointerType)
10159                   {
10160                      _class = eSystem_FindClass(privateModule, "uintptr");
10161                      FreeType(exp.expType);
10162                      exp.expType = ProcessTypeString("uintptr", false);
10163                      exp.byReference = true;
10164                   }
10165                   else
10166                   {
10167                      char string[1024] = "";
10168                      Symbol classSym;
10169                      PrintTypeNoConst(type, string, false, true);
10170                      classSym = FindClass(string);
10171                      if(classSym) _class = classSym.registered;
10172                   }
10173                }
10174             }
10175
10176             if(_class && id)
10177             {
10178                /*bool thisPtr =
10179                   (exp.member.exp.type == identifierExp &&
10180                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10181                Property prop = null;
10182                Method method = null;
10183                DataMember member = null;
10184                Property revConvert = null;
10185                ClassProperty classProp = null;
10186
10187                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10188                   exp.member.memberType = propertyMember;
10189
10190                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10191                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10192
10193                if(typeKind != subClassType)
10194                {
10195                   // Prioritize data members over properties for "this"
10196                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10197                   {
10198                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10199                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10200                      {
10201                         prop = eClass_FindProperty(_class, id.string, privateModule);
10202                         if(prop)
10203                            member = null;
10204                      }
10205                      if(!member && !prop)
10206                         prop = eClass_FindProperty(_class, id.string, privateModule);
10207                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10208                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10209                         exp.member.thisPtr = true;
10210                   }
10211                   // Prioritize properties over data members otherwise
10212                   else
10213                   {
10214                      bool useMemberForNonConst = false;
10215                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10216                      if(!id.classSym)
10217                      {
10218                         prop = eClass_FindProperty(_class, id.string, null);
10219
10220                         useMemberForNonConst = prop && exp.destType &&
10221                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10222                               !strncmp(prop.dataTypeString, "const ", 6);
10223
10224                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10225                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10226                      }
10227
10228                      if((!prop || useMemberForNonConst) && !member)
10229                      {
10230                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10231                         if(!method)
10232                         {
10233                            prop = eClass_FindProperty(_class, id.string, privateModule);
10234
10235                            useMemberForNonConst |= prop && exp.destType &&
10236                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10237                                  !strncmp(prop.dataTypeString, "const ", 6);
10238
10239                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10240                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10241                         }
10242                      }
10243
10244                      if(member && prop)
10245                      {
10246                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10247                            prop = null;
10248                         else
10249                            member = null;
10250                      }
10251                   }
10252                }
10253                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10254                   method = eClass_FindMethod(_class, id.string, privateModule);
10255                if(!prop && !member && !method)
10256                {
10257                   if(typeKind == subClassType)
10258                   {
10259                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10260                      if(classProp)
10261                      {
10262                         exp.member.memberType = classPropertyMember;
10263                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10264                      }
10265                      else
10266                      {
10267                         // Assume this is a class_data member
10268                         char structName[1024];
10269                         Identifier id = exp.member.member;
10270                         Expression classExp = exp.member.exp;
10271                         type.refCount++;
10272
10273                         FreeType(classExp.expType);
10274                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10275
10276                         strcpy(structName, "__ecereClassData_");
10277                         FullClassNameCat(structName, type._class.string, false);
10278                         exp.type = pointerExp;
10279                         exp.member.member = id;
10280
10281                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10282                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10283                               MkExpBrackets(MkListOne(MkExpOp(
10284                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10285                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10286                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10287                                  )));
10288
10289                         FreeType(type);
10290
10291                         ProcessExpressionType(exp);
10292                         return;
10293                      }
10294                   }
10295                   else
10296                   {
10297                      // Check for reverse conversion
10298                      // (Convert in an instantiation later, so that we can use
10299                      //  deep properties system)
10300                      Symbol classSym = FindClass(id.string);
10301                      if(classSym)
10302                      {
10303                         Class convertClass = classSym.registered;
10304                         if(convertClass)
10305                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10306                      }
10307                   }
10308                }
10309
10310                //if(!exp.member.exp.destType)
10311                if(exp.member.exp.destType)
10312                   FreeType(exp.member.exp.destType);
10313                {
10314                   if(method && !method._class.symbol)
10315                      method._class.symbol = FindClass(method._class.fullName);
10316                   if(prop && !prop._class.symbol)
10317                      prop._class.symbol = FindClass(prop._class.fullName);
10318
10319                   exp.member.exp.destType = Type
10320                   {
10321                      refCount = 1;
10322                      kind = classType;
10323                      _class = prop ? prop._class.symbol : method ? method._class.symbol : _class.symbol;
10324                      // wasThisClass = type ? type.wasThisClass : false;
10325                   };
10326                }
10327
10328                if(prop)
10329                {
10330                   exp.member.memberType = propertyMember;
10331                   if(!prop.dataType)
10332                      ProcessPropertyType(prop);
10333                   exp.expType = prop.dataType;
10334                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10335                   {
10336                      Type type { };
10337                      CopyTypeInto(type, exp.expType);
10338                      type.refCount = 1;
10339                      type.constant = true;
10340                      exp.expType = type;
10341                   }
10342                   else if(prop.dataType)
10343                      prop.dataType.refCount++;
10344                }
10345                else if(member)
10346                {
10347                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10348                   {
10349                      FreeExpContents(exp);
10350                      exp.type = identifierExp;
10351                      exp.identifier = MkIdentifier("class");
10352                      ProcessExpressionType(exp);
10353                      return;
10354                   }
10355
10356                   exp.member.memberType = dataMember;
10357                   DeclareStruct(_class.fullName, false);
10358                   if(!member.dataType)
10359                   {
10360                      Context context = SetupTemplatesContext(_class);
10361                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10362                      FinishTemplatesContext(context);
10363                   }
10364                   exp.expType = member.dataType;
10365                   if(member.dataType) member.dataType.refCount++;
10366                }
10367                else if(revConvert)
10368                {
10369                   exp.member.memberType = reverseConversionMember;
10370                   exp.expType = MkClassType(revConvert._class.fullName);
10371                }
10372                else if(method)
10373                {
10374                   //if(inCompiler)
10375                   {
10376                      /*if(id._class)
10377                      {
10378                         exp.type = identifierExp;
10379                         exp.identifier = exp.member.member;
10380                      }
10381                      else*/
10382                         exp.member.memberType = methodMember;
10383                   }
10384                   if(!method.dataType)
10385                      ProcessMethodType(method);
10386                   exp.expType = Type
10387                   {
10388                      refCount = 1;
10389                      kind = methodType;
10390                      method = method;
10391                   };
10392
10393                   // Tricky spot here... To use instance versus class virtual table
10394                   // Put it back to what it was... What did we break?
10395
10396                   // Had to put it back for overriding Main of Thread global instance
10397
10398                   //exp.expType.methodClass = _class;
10399                   exp.expType.methodClass = (id && id._class) ? _class : null;
10400
10401                   // Need the actual class used for templated classes
10402                   exp.expType.usedClass = _class;
10403                }
10404                else if(!classProp)
10405                {
10406                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10407                   {
10408                      FreeExpContents(exp);
10409                      exp.type = identifierExp;
10410                      exp.identifier = MkIdentifier("class");
10411                      FreeType(exp.expType);
10412                      exp.expType = MkClassType("ecere::com::Class");
10413                      return;
10414                   }
10415                   yylloc = exp.member.member.loc;
10416                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10417                   if(inCompiler)
10418                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10419                }
10420
10421                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10422                {
10423                   Class tClass;
10424
10425                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10426                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10427
10428                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10429                   {
10430                      int id = 0;
10431                      ClassTemplateParameter curParam = null;
10432                      Class sClass;
10433
10434                      for(sClass = tClass; sClass; sClass = sClass.base)
10435                      {
10436                         id = 0;
10437                         if(sClass.templateClass) sClass = sClass.templateClass;
10438                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10439                         {
10440                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10441                            {
10442                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10443                                  id += sClass.templateParams.count;
10444                               break;
10445                            }
10446                            id++;
10447                         }
10448                         if(curParam) break;
10449                      }
10450
10451                      if(curParam && tClass.templateArgs[id].dataTypeString)
10452                      {
10453                         ClassTemplateArgument arg = tClass.templateArgs[id];
10454                         Context context = SetupTemplatesContext(tClass);
10455                         bool constant = exp.expType.constant;
10456                         /*if(!arg.dataType)
10457                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10458                         FreeType(exp.expType);
10459
10460                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10461                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10462                         else if(exp.expType.kind == pointerType)
10463                         {
10464                            Type t = exp.expType.type;
10465                            while(t.kind == pointerType) t = t.type;
10466                            if(constant) t.constant = constant;
10467                         }
10468                         if(exp.expType)
10469                         {
10470                            if(exp.expType.kind == thisClassType)
10471                            {
10472                               FreeType(exp.expType);
10473                               exp.expType = ReplaceThisClassType(_class);
10474                            }
10475
10476                            if(tClass.templateClass && (exp.expType.kind != templateType || (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType))))
10477                               exp.expType.passAsTemplate = true;
10478                            //exp.expType.refCount++;
10479                            if(!exp.destType)
10480                            {
10481                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10482                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10483                               else if(exp.destType.kind == pointerType)
10484                               {
10485                                  Type t = exp.destType.type;
10486                                  while(t.kind == pointerType) t = t.type;
10487                                  if(constant) t.constant = constant;
10488                               }
10489
10490                               //exp.destType.refCount++;
10491
10492                               if(exp.destType.kind == thisClassType)
10493                               {
10494                                  FreeType(exp.destType);
10495                                  exp.destType = ReplaceThisClassType(_class);
10496                               }
10497                            }
10498                         }
10499                         FinishTemplatesContext(context);
10500                      }
10501                   }
10502                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10503                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10504                   {
10505                      int id = 0;
10506                      ClassTemplateParameter curParam = null;
10507                      Class sClass;
10508
10509                      for(sClass = tClass; sClass; sClass = sClass.base)
10510                      {
10511                         id = 0;
10512                         if(sClass.templateClass) sClass = sClass.templateClass;
10513                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10514                         {
10515                            if(curParam.type == TemplateParameterType::type &&
10516                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10517                            {
10518                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10519                                  id += sClass.templateParams.count;
10520                               break;
10521                            }
10522                            id++;
10523                         }
10524                         if(curParam) break;
10525                      }
10526
10527                      if(curParam)
10528                      {
10529                         ClassTemplateArgument arg = tClass.templateArgs[id];
10530                         Context context = SetupTemplatesContext(tClass);
10531                         Type basicType;
10532                         /*if(!arg.dataType)
10533                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10534
10535                         basicType = ProcessTypeString(arg.dataTypeString, false);
10536                         if(basicType)
10537                         {
10538                            if(basicType.kind == thisClassType)
10539                            {
10540                               FreeType(basicType);
10541                               basicType = ReplaceThisClassType(_class);
10542                            }
10543
10544                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10545                            if(tClass.templateClass)
10546                               basicType.passAsTemplate = true;
10547                            */
10548
10549                            FreeType(exp.expType);
10550
10551                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10552                            //exp.expType.refCount++;
10553                            if(!exp.destType)
10554                            {
10555                               exp.destType = exp.expType;
10556                               exp.destType.refCount++;
10557                            }
10558
10559                            {
10560                               Expression newExp { };
10561                               OldList * specs = MkList();
10562                               Declarator decl;
10563                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10564                               *newExp = *exp;
10565                               if(exp.destType) exp.destType.refCount++;
10566                               if(exp.expType)  exp.expType.refCount++;
10567                               exp.type = castExp;
10568                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10569                               exp.cast.exp = newExp;
10570                               //FreeType(exp.expType);
10571                               //exp.expType = null;
10572                               //ProcessExpressionType(sourceExp);
10573                            }
10574                         }
10575                         FinishTemplatesContext(context);
10576                      }
10577                   }
10578                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10579                   {
10580                      Class expClass = exp.expType._class.registered;
10581                      if(expClass)
10582                      {
10583                         Class cClass = null;
10584                         int p = 0;
10585                         int paramCount = 0;
10586                         int lastParam = -1;
10587                         char templateString[1024];
10588                         ClassTemplateParameter param;
10589                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10590                         while(cClass != expClass)
10591                         {
10592                            Class sClass;
10593                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10594                            cClass = sClass;
10595
10596                            for(param = cClass.templateParams.first; param; param = param.next)
10597                            {
10598                               Class cClassCur = null;
10599                               int cp = 0;
10600                               ClassTemplateParameter paramCur = null;
10601                               ClassTemplateArgument arg;
10602                               while(cClassCur != tClass && !paramCur)
10603                               {
10604                                  Class sClassCur;
10605                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10606                                  cClassCur = sClassCur;
10607
10608                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10609                                  {
10610                                     if(!strcmp(paramCur.name, param.name))
10611                                     {
10612
10613                                        break;
10614                                     }
10615                                     cp++;
10616                                  }
10617                               }
10618                               if(paramCur && paramCur.type == TemplateParameterType::type)
10619                                  arg = tClass.templateArgs[cp];
10620                               else
10621                                  arg = expClass.templateArgs[p];
10622
10623                               {
10624                                  char argument[256];
10625                                  argument[0] = '\0';
10626                                  /*if(arg.name)
10627                                  {
10628                                     strcat(argument, arg.name.string);
10629                                     strcat(argument, " = ");
10630                                  }*/
10631                                  switch(param.type)
10632                                  {
10633                                     case expression:
10634                                     {
10635                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10636                                        char expString[1024];
10637                                        OldList * specs = MkList();
10638                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10639                                        Expression exp;
10640                                        char * string = PrintHexUInt64(arg.expression.ui64);
10641                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10642                                        delete string;
10643
10644                                        ProcessExpressionType(exp);
10645                                        ComputeExpression(exp);
10646                                        expString[0] = '\0';
10647                                        PrintExpression(exp, expString);
10648                                        strcat(argument, expString);
10649                                        // delete exp;
10650                                        FreeExpression(exp);
10651                                        break;
10652                                     }
10653                                     case identifier:
10654                                     {
10655                                        strcat(argument, arg.member.name);
10656                                        break;
10657                                     }
10658                                     case TemplateParameterType::type:
10659                                     {
10660                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10661                                           strcat(argument, arg.dataTypeString);
10662                                        break;
10663                                     }
10664                                  }
10665                                  if(argument[0])
10666                                  {
10667                                     if(paramCount) strcat(templateString, ", ");
10668                                     if(lastParam != p - 1)
10669                                     {
10670                                        strcat(templateString, param.name);
10671                                        strcat(templateString, " = ");
10672                                     }
10673                                     strcat(templateString, argument);
10674                                     paramCount++;
10675                                     lastParam = p;
10676                                  }
10677                               }
10678                               p++;
10679                            }
10680                         }
10681                         {
10682                            int len = strlen(templateString);
10683                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10684                            templateString[len++] = '>';
10685                            templateString[len++] = '\0';
10686                         }
10687
10688                         FreeType(exp.expType);
10689                         {
10690                            Context context = SetupTemplatesContext(tClass);
10691                            exp.expType = ProcessTypeString(templateString, false);
10692                            FinishTemplatesContext(context);
10693                         }
10694                      }
10695                   }
10696                }
10697             }
10698             else
10699                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10700          }
10701          else if(type && (type.kind == structType || type.kind == unionType))
10702          {
10703             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10704             if(memberType)
10705             {
10706                exp.expType = memberType;
10707                if(memberType)
10708                   memberType.refCount++;
10709             }
10710          }
10711          else
10712          {
10713             char expString[10240];
10714             expString[0] = '\0';
10715             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10716             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10717          }
10718
10719          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10720          {
10721             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10722             {
10723                Identifier id = exp.member.member;
10724                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10725                if(_class)
10726                {
10727                   FreeType(exp.expType);
10728                   exp.expType = ReplaceThisClassType(_class);
10729                }
10730             }
10731          }
10732          yylloc = oldyylloc;
10733          break;
10734       }
10735       // Convert x->y into (*x).y
10736       case pointerExp:
10737       {
10738          Type destType = exp.destType;
10739
10740          // DOING THIS LATER NOW...
10741          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10742          {
10743             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10744             /* TODO: Name Space Fix ups
10745             if(!exp.member.member.classSym)
10746                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10747             */
10748          }
10749
10750          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10751          exp.type = memberExp;
10752          if(destType)
10753             destType.count++;
10754          ProcessExpressionType(exp);
10755          if(destType)
10756             destType.count--;
10757          break;
10758       }
10759       case classSizeExp:
10760       {
10761          //ComputeExpression(exp);
10762
10763          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10764          if(classSym && classSym.registered)
10765          {
10766             if(classSym.registered.type == noHeadClass)
10767             {
10768                char name[1024];
10769                name[0] = '\0';
10770                DeclareStruct(classSym.string, false);
10771                FreeSpecifier(exp._class);
10772                exp.type = typeSizeExp;
10773                FullClassNameCat(name, classSym.string, false);
10774                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10775             }
10776             else
10777             {
10778                if(classSym.registered.fixed)
10779                {
10780                   FreeSpecifier(exp._class);
10781                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10782                   exp.type = constantExp;
10783                }
10784                else
10785                {
10786                   char className[1024];
10787                   strcpy(className, "__ecereClass_");
10788                   FullClassNameCat(className, classSym.string, true);
10789                   //MangleClassName(className);
10790
10791                   DeclareClass(classSym, className);
10792
10793                   FreeExpContents(exp);
10794                   exp.type = pointerExp;
10795                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10796                   exp.member.member = MkIdentifier("structSize");
10797                }
10798             }
10799          }
10800
10801          exp.expType = Type
10802          {
10803             refCount = 1;
10804             kind = intSizeType;
10805          };
10806          // exp.isConstant = true;
10807          break;
10808       }
10809       case typeSizeExp:
10810       {
10811          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10812
10813          exp.expType = Type
10814          {
10815             refCount = 1;
10816             kind = intSizeType;
10817          };
10818          exp.isConstant = true;
10819
10820          DeclareType(type, false, false);
10821          FreeType(type);
10822          break;
10823       }
10824       case castExp:
10825       {
10826          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10827          type.count = 1;
10828          FreeType(exp.cast.exp.destType);
10829          exp.cast.exp.destType = type;
10830          type.refCount++;
10831          type.casted = true;
10832          ProcessExpressionType(exp.cast.exp);
10833          type.casted = false;
10834          type.count = 0;
10835          exp.expType = type;
10836          //type.refCount++;
10837
10838          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10839          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10840          {
10841             void * prev = exp.prev, * next = exp.next;
10842             Type expType = exp.cast.exp.destType;
10843             Expression castExp = exp.cast.exp;
10844             Type destType = exp.destType;
10845
10846             if(expType) expType.refCount++;
10847
10848             //FreeType(exp.destType);
10849             FreeType(exp.expType);
10850             FreeTypeName(exp.cast.typeName);
10851
10852             *exp = *castExp;
10853             FreeType(exp.expType);
10854             FreeType(exp.destType);
10855
10856             exp.expType = expType;
10857             exp.destType = destType;
10858
10859             delete castExp;
10860
10861             exp.prev = prev;
10862             exp.next = next;
10863
10864          }
10865          else
10866          {
10867             exp.isConstant = exp.cast.exp.isConstant;
10868          }
10869          //FreeType(type);
10870          break;
10871       }
10872       case extensionInitializerExp:
10873       {
10874          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10875          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10876          // ProcessInitializer(exp.initializer.initializer, type);
10877          exp.expType = type;
10878          break;
10879       }
10880       case vaArgExp:
10881       {
10882          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10883          ProcessExpressionType(exp.vaArg.exp);
10884          exp.expType = type;
10885          break;
10886       }
10887       case conditionExp:
10888       {
10889          Expression e;
10890          exp.isConstant = true;
10891
10892          FreeType(exp.cond.cond.destType);
10893          exp.cond.cond.destType = MkClassType("bool");
10894          exp.cond.cond.destType.truth = true;
10895          ProcessExpressionType(exp.cond.cond);
10896          if(!exp.cond.cond.isConstant)
10897             exp.isConstant = false;
10898          for(e = exp.cond.exp->first; e; e = e.next)
10899          {
10900             if(!e.next)
10901             {
10902                FreeType(e.destType);
10903                e.destType = exp.destType;
10904                if(e.destType) e.destType.refCount++;
10905             }
10906             ProcessExpressionType(e);
10907             if(!e.next)
10908             {
10909                exp.expType = e.expType;
10910                if(e.expType) e.expType.refCount++;
10911             }
10912             if(!e.isConstant)
10913                exp.isConstant = false;
10914          }
10915
10916          FreeType(exp.cond.elseExp.destType);
10917          // Added this check if we failed to find an expType
10918          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10919
10920          // Reversed it...
10921          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10922
10923          if(exp.cond.elseExp.destType)
10924             exp.cond.elseExp.destType.refCount++;
10925          ProcessExpressionType(exp.cond.elseExp);
10926
10927          // FIXED THIS: Was done before calling process on elseExp
10928          if(!exp.cond.elseExp.isConstant)
10929             exp.isConstant = false;
10930          break;
10931       }
10932       case extensionCompoundExp:
10933       {
10934          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10935          {
10936             Statement last = exp.compound.compound.statements->last;
10937             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10938             {
10939                ((Expression)last.expressions->last).destType = exp.destType;
10940                if(exp.destType)
10941                   exp.destType.refCount++;
10942             }
10943             ProcessStatement(exp.compound);
10944             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10945             if(exp.expType)
10946                exp.expType.refCount++;
10947          }
10948          break;
10949       }
10950       case classExp:
10951       {
10952          Specifier spec = exp._classExp.specifiers->first;
10953          if(spec && spec.type == nameSpecifier)
10954          {
10955             exp.expType = MkClassType(spec.name);
10956             exp.expType.kind = subClassType;
10957             exp.byReference = true;
10958          }
10959          else
10960          {
10961             exp.expType = MkClassType("ecere::com::Class");
10962             exp.byReference = true;
10963          }
10964          break;
10965       }
10966       case classDataExp:
10967       {
10968          Class _class = thisClass ? thisClass : currentClass;
10969          if(_class)
10970          {
10971             Identifier id = exp.classData.id;
10972             char structName[1024];
10973             Expression classExp;
10974             strcpy(structName, "__ecereClassData_");
10975             FullClassNameCat(structName, _class.fullName, false);
10976             exp.type = pointerExp;
10977             exp.member.member = id;
10978             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10979                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10980             else
10981                classExp = MkExpIdentifier(MkIdentifier("class"));
10982
10983             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10984                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10985                   MkExpBrackets(MkListOne(MkExpOp(
10986                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10987                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10988                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10989                      )));
10990
10991             ProcessExpressionType(exp);
10992             return;
10993          }
10994          break;
10995       }
10996       case arrayExp:
10997       {
10998          Type type = null;
10999          const char * typeString = null;
11000          char typeStringBuf[1024];
11001          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
11002             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
11003          {
11004             Class templateClass = exp.destType._class.registered;
11005             typeString = templateClass.templateArgs[2].dataTypeString;
11006          }
11007          else if(exp.list)
11008          {
11009             // Guess type from expressions in the array
11010             Expression e;
11011             for(e = exp.list->first; e; e = e.next)
11012             {
11013                ProcessExpressionType(e);
11014                if(e.expType)
11015                {
11016                   if(!type) { type = e.expType; type.refCount++; }
11017                   else
11018                   {
11019                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11020                      if(!MatchTypeExpression(e, type, null, false, true))
11021                      {
11022                         FreeType(type);
11023                         type = e.expType;
11024                         e.expType = null;
11025
11026                         e = exp.list->first;
11027                         ProcessExpressionType(e);
11028                         if(e.expType)
11029                         {
11030                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
11031                            if(!MatchTypeExpression(e, type, null, false, true))
11032                            {
11033                               FreeType(e.expType);
11034                               e.expType = null;
11035                               FreeType(type);
11036                               type = null;
11037                               break;
11038                            }
11039                         }
11040                      }
11041                   }
11042                   if(e.expType)
11043                   {
11044                      FreeType(e.expType);
11045                      e.expType = null;
11046                   }
11047                }
11048             }
11049             if(type)
11050             {
11051                typeStringBuf[0] = '\0';
11052                PrintTypeNoConst(type, typeStringBuf, false, true);
11053                typeString = typeStringBuf;
11054                FreeType(type);
11055                type = null;
11056             }
11057          }
11058          if(typeString)
11059          {
11060             /*
11061             (Container)& (struct BuiltInContainer)
11062             {
11063                ._vTbl = class(BuiltInContainer)._vTbl,
11064                ._class = class(BuiltInContainer),
11065                .refCount = 0,
11066                .data = (int[]){ 1, 7, 3, 4, 5 },
11067                .count = 5,
11068                .type = class(int),
11069             }
11070             */
11071             char templateString[1024];
11072             OldList * initializers = MkList();
11073             OldList * structInitializers = MkList();
11074             OldList * specs = MkList();
11075             Expression expExt;
11076             Declarator decl = SpecDeclFromString(typeString, specs, null);
11077             sprintf(templateString, "Container<%s>", typeString);
11078
11079             if(exp.list)
11080             {
11081                Expression e;
11082                type = ProcessTypeString(typeString, false);
11083                while((e = exp.list->first))
11084                {
11085                   exp.list->Remove(e);
11086                   e.destType = type;
11087                   type.refCount++;
11088                   ProcessExpressionType(e);
11089                   ListAdd(initializers, MkInitializerAssignment(e));
11090                }
11091                FreeType(type);
11092                delete exp.list;
11093             }
11094
11095             DeclareStruct("ecere::com::BuiltInContainer", false);
11096
11097             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
11098                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11099             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
11100                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11101             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
11102                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11103             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
11104                MkTypeName(specs, MkDeclaratorArray(decl, null)),
11105                MkInitializerList(initializers))));
11106                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11107             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
11108                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11109             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
11110                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11111             exp.expType = ProcessTypeString(templateString, false);
11112             exp.type = bracketsExp;
11113             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
11114                MkExpOp(null, '&',
11115                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
11116                   MkInitializerList(structInitializers)))));
11117             ProcessExpressionType(expExt);
11118          }
11119          else
11120          {
11121             exp.expType = ProcessTypeString("Container", false);
11122             Compiler_Error($"Couldn't determine type of array elements\n");
11123          }
11124          break;
11125       }
11126    }
11127
11128    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
11129    {
11130       FreeType(exp.expType);
11131       exp.expType = ReplaceThisClassType(thisClass);
11132    }
11133
11134    // Resolve structures here
11135    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
11136    {
11137       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
11138       // TODO: Fix members reference...
11139       if(symbol)
11140       {
11141          if(exp.expType.kind != enumType)
11142          {
11143             Type member;
11144             String enumName = CopyString(exp.expType.enumName);
11145
11146             // Fixed a memory leak on self-referencing C structs typedefs
11147             // by instantiating a new type rather than simply copying members
11148             // into exp.expType
11149             FreeType(exp.expType);
11150             exp.expType = Type { };
11151             exp.expType.kind = symbol.type.kind;
11152             exp.expType.refCount++;
11153             exp.expType.enumName = enumName;
11154
11155             exp.expType.members = symbol.type.members;
11156             for(member = symbol.type.members.first; member; member = member.next)
11157                member.refCount++;
11158          }
11159          else
11160          {
11161             NamedLink64 member;
11162             for(member = symbol.type.members.first; member; member = member.next)
11163             {
11164                NamedLink64 value { name = CopyString(member.name) };
11165                exp.expType.members.Add(value);
11166             }
11167          }
11168       }
11169    }
11170
11171    yylloc = exp.loc;
11172    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11173    else if(exp.destType && !exp.destType.keepCast)
11174    {
11175       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11176          exp.needTemplateCast = 1;
11177
11178       if(exp.destType.kind == voidType);
11179       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11180       {
11181          if(!exp.destType.count || unresolved)
11182          {
11183             if(!exp.expType)
11184             {
11185                yylloc = exp.loc;
11186                if(exp.destType.kind != ellipsisType)
11187                {
11188                   char type2[1024];
11189                   type2[0] = '\0';
11190                   if(inCompiler)
11191                   {
11192                      char expString[10240];
11193                      expString[0] = '\0';
11194
11195                      PrintType(exp.destType, type2, false, true);
11196
11197                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11198                      if(unresolved)
11199                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11200                      else if(exp.type != dummyExp)
11201                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11202                   }
11203                }
11204                else
11205                {
11206                   char expString[10240] ;
11207                   expString[0] = '\0';
11208                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11209
11210                   if(unresolved)
11211                      Compiler_Error($"unresolved identifier %s\n", expString);
11212                   else if(exp.type != dummyExp)
11213                      Compiler_Error($"couldn't determine type of %s\n", expString);
11214                }
11215             }
11216             else
11217             {
11218                char type1[1024];
11219                char type2[1024];
11220                type1[0] = '\0';
11221                type2[0] = '\0';
11222                if(inCompiler)
11223                {
11224                   PrintType(exp.expType, type1, false, true);
11225                   PrintType(exp.destType, type2, false, true);
11226                }
11227
11228                //CheckExpressionType(exp, exp.destType, false);
11229
11230                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11231                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11232                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11233                else
11234                {
11235                   char expString[10240];
11236                   expString[0] = '\0';
11237                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11238
11239 #ifdef _DEBUG
11240                   CheckExpressionType(exp, exp.destType, false, true);
11241 #endif
11242                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11243                   if(!sourceFile || (!strstr(sourceFile, "src\\lexer.ec") && !strstr(sourceFile, "src/lexer.ec") &&
11244                                      !strstr(sourceFile, "src\\grammar.ec") && !strstr(sourceFile, "src/grammar.ec") &&
11245                                      !strstr(sourceFile, "src\\type.ec") && !strstr(sourceFile, "src/type.ec") &&
11246                                      !strstr(sourceFile, "src\\expression.ec") && !strstr(sourceFile, "src/expression.ec")))
11247                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11248
11249                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11250                   FreeType(exp.expType);
11251                   exp.destType.refCount++;
11252                   exp.expType = exp.destType;
11253                }
11254             }
11255          }
11256       }
11257       // Cast function pointers to void * as eC already checked compatibility
11258       else if(exp.destType && exp.destType.kind == pointerType && exp.destType.type && exp.destType.type.kind == functionType &&
11259               exp.expType && (exp.expType.kind == functionType || exp.expType.kind == methodType))
11260       {
11261          Expression nbExp = GetNonBracketsExp(exp);
11262          if(nbExp.type != castExp || !IsVoidPtrCast(nbExp.cast.typeName))
11263          {
11264             Expression e = MoveExpContents(exp);
11265             exp.cast.exp = MkExpBrackets(MkListOne(e));
11266             exp.type = castExp;
11267             exp.cast.exp.destType = exp.destType;
11268             if(exp.destType) exp.destType.refCount++;
11269             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
11270          }
11271       }
11272    }
11273    else if(unresolved)
11274    {
11275       if(exp.identifier._class && exp.identifier._class.name)
11276          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11277       else if(exp.identifier.string && exp.identifier.string[0])
11278          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11279    }
11280    else if(!exp.expType && exp.type != dummyExp)
11281    {
11282       char expString[10240];
11283       expString[0] = '\0';
11284       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11285       Compiler_Error($"couldn't determine type of %s\n", expString);
11286    }
11287
11288    // Let's try to support any_object & typed_object here:
11289    if(inCompiler)
11290       ApplyAnyObjectLogic(exp);
11291
11292    // Mark nohead classes as by reference, unless we're casting them to an integral type
11293    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11294       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11295          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11296           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11297    {
11298       exp.byReference = true;
11299    }
11300    yylloc = oldyylloc;
11301 }
11302
11303 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11304 {
11305    // THIS CODE WILL FIND NEXT MEMBER...
11306    if(*curMember)
11307    {
11308       *curMember = (*curMember).next;
11309
11310       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11311       {
11312          *curMember = subMemberStack[--(*subMemberStackPos)];
11313          *curMember = (*curMember).next;
11314       }
11315
11316       // SKIP ALL PROPERTIES HERE...
11317       while((*curMember) && (*curMember).isProperty)
11318          *curMember = (*curMember).next;
11319
11320       if(subMemberStackPos)
11321       {
11322          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11323          {
11324             subMemberStack[(*subMemberStackPos)++] = *curMember;
11325
11326             *curMember = (*curMember).members.first;
11327             while(*curMember && (*curMember).isProperty)
11328                *curMember = (*curMember).next;
11329          }
11330       }
11331    }
11332    while(!*curMember)
11333    {
11334       if(!*curMember)
11335       {
11336          if(subMemberStackPos && *subMemberStackPos)
11337          {
11338             *curMember = subMemberStack[--(*subMemberStackPos)];
11339             *curMember = (*curMember).next;
11340          }
11341          else
11342          {
11343             Class lastCurClass = *curClass;
11344
11345             if(*curClass == _class) break;     // REACHED THE END
11346
11347             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11348             *curMember = (*curClass).membersAndProperties.first;
11349          }
11350
11351          while((*curMember) && (*curMember).isProperty)
11352             *curMember = (*curMember).next;
11353          if(subMemberStackPos)
11354          {
11355             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11356             {
11357                subMemberStack[(*subMemberStackPos)++] = *curMember;
11358
11359                *curMember = (*curMember).members.first;
11360                while(*curMember && (*curMember).isProperty)
11361                   *curMember = (*curMember).next;
11362             }
11363          }
11364       }
11365    }
11366 }
11367
11368
11369 static void ProcessInitializer(Initializer init, Type type)
11370 {
11371    switch(init.type)
11372    {
11373       case expInitializer:
11374          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11375          {
11376             // TESTING THIS FOR SHUTTING = 0 WARNING
11377             if(init.exp && !init.exp.destType)
11378             {
11379                FreeType(init.exp.destType);
11380                init.exp.destType = type;
11381                if(type) type.refCount++;
11382             }
11383             if(init.exp)
11384             {
11385                ProcessExpressionType(init.exp);
11386                init.isConstant = init.exp.isConstant;
11387             }
11388             break;
11389          }
11390          else
11391          {
11392             Expression exp = init.exp;
11393             Instantiation inst = exp.instance;
11394             MembersInit members;
11395
11396             init.type = listInitializer;
11397             init.list = MkList();
11398
11399             if(inst.members)
11400             {
11401                for(members = inst.members->first; members; members = members.next)
11402                {
11403                   if(members.type == dataMembersInit)
11404                   {
11405                      MemberInit member;
11406                      for(member = members.dataMembers->first; member; member = member.next)
11407                      {
11408                         ListAdd(init.list, member.initializer);
11409                         member.initializer = null;
11410                      }
11411                   }
11412                   // Discard all MembersInitMethod
11413                }
11414             }
11415             FreeExpression(exp);
11416          }
11417       case listInitializer:
11418       {
11419          Initializer i;
11420          Type initializerType = null;
11421          Class curClass = null;
11422          DataMember curMember = null;
11423          DataMember subMemberStack[256];
11424          int subMemberStackPos = 0;
11425
11426          if(type && type.kind == arrayType)
11427             initializerType = Dereference(type);
11428          else if(type && (type.kind == structType || type.kind == unionType))
11429             initializerType = type.members.first;
11430
11431          for(i = init.list->first; i; i = i.next)
11432          {
11433             if(type && type.kind == classType && type._class && type._class.registered)
11434             {
11435                // 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)
11436                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11437                // TODO: Generate error on initializing a private data member this way from another module...
11438                if(curMember)
11439                {
11440                   if(!curMember.dataType)
11441                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11442                   initializerType = curMember.dataType;
11443                }
11444             }
11445             ProcessInitializer(i, initializerType);
11446             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11447                initializerType = initializerType.next;
11448             if(!i.isConstant)
11449                init.isConstant = false;
11450          }
11451
11452          if(type && type.kind == arrayType)
11453             FreeType(initializerType);
11454
11455          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11456          {
11457             Compiler_Error($"Assigning list initializer to non list\n");
11458          }
11459          break;
11460       }
11461    }
11462 }
11463
11464 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11465 {
11466    switch(spec.type)
11467    {
11468       case baseSpecifier:
11469       {
11470          if(spec.specifier == THISCLASS)
11471          {
11472             if(thisClass)
11473             {
11474                spec.type = nameSpecifier;
11475                spec.name = ReplaceThisClass(thisClass);
11476                spec.symbol = FindClass(spec.name);
11477                ProcessSpecifier(spec, declareStruct);
11478             }
11479          }
11480          break;
11481       }
11482       case nameSpecifier:
11483       {
11484          Symbol symbol = FindType(curContext, spec.name);
11485          if(symbol)
11486             DeclareType(symbol.type, true, true);
11487          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11488             DeclareStruct(spec.name, false);
11489          break;
11490       }
11491       case enumSpecifier:
11492       {
11493          Enumerator e;
11494          if(spec.list)
11495          {
11496             for(e = spec.list->first; e; e = e.next)
11497             {
11498                if(e.exp)
11499                   ProcessExpressionType(e.exp);
11500             }
11501          }
11502          // Fall through for IDE type processing
11503          if(inCompiler)
11504             break;
11505       }
11506       case structSpecifier:
11507       case unionSpecifier:
11508       {
11509          if(spec.definitions)
11510          {
11511             //ClassDef def;
11512             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11513             //if(symbol)
11514                ProcessClass(spec.definitions, symbol);
11515             /*else
11516             {
11517                for(def = spec.definitions->first; def; def = def.next)
11518                {
11519                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11520                      ProcessDeclaration(def.decl);
11521                }
11522             }*/
11523          }
11524          break;
11525       }
11526       /*
11527       case classSpecifier:
11528       {
11529          Symbol classSym = FindClass(spec.name);
11530          if(classSym && classSym.registered && classSym.registered.type == structClass)
11531             DeclareStruct(spec.name, false);
11532          break;
11533       }
11534       */
11535    }
11536 }
11537
11538
11539 static void ProcessDeclarator(Declarator decl)
11540 {
11541    switch(decl.type)
11542    {
11543       case identifierDeclarator:
11544          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11545          {
11546             FreeSpecifier(decl.identifier._class);
11547             decl.identifier._class = null;
11548          }
11549          break;
11550       case arrayDeclarator:
11551          if(decl.array.exp)
11552             ProcessExpressionType(decl.array.exp);
11553       case structDeclarator:
11554       case bracketsDeclarator:
11555       case functionDeclarator:
11556       case pointerDeclarator:
11557       case extendedDeclarator:
11558       case extendedDeclaratorEnd:
11559          if(decl.declarator)
11560             ProcessDeclarator(decl.declarator);
11561          if(decl.type == functionDeclarator)
11562          {
11563             Identifier id = GetDeclId(decl);
11564             if(id && id._class)
11565             {
11566                TypeName param
11567                {
11568                   qualifiers = MkListOne(id._class);
11569                   declarator = null;
11570                };
11571                if(!decl.function.parameters)
11572                   decl.function.parameters = MkList();
11573                decl.function.parameters->Insert(null, param);
11574                id._class = null;
11575             }
11576             if(decl.function.parameters)
11577             {
11578                TypeName param;
11579
11580                for(param = decl.function.parameters->first; param; param = param.next)
11581                {
11582                   if(param.qualifiers && param.qualifiers->first)
11583                   {
11584                      Specifier spec = param.qualifiers->first;
11585                      if(spec && spec.specifier == TYPED_OBJECT)
11586                      {
11587                         Declarator d = param.declarator;
11588                         TypeName newParam
11589                         {
11590                            qualifiers = MkListOne(MkSpecifier(VOID));
11591                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11592                         };
11593                         if(d.type != pointerDeclarator)
11594                            newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11595
11596                         FreeList(param.qualifiers, FreeSpecifier);
11597
11598                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11599                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11600
11601                         decl.function.parameters->Insert(param, newParam);
11602                         param = newParam;
11603                      }
11604                      else if(spec && spec.specifier == ANY_OBJECT)
11605                      {
11606                         Declarator d = param.declarator;
11607
11608                         FreeList(param.qualifiers, FreeSpecifier);
11609
11610                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11611                         if(d.type != pointerDeclarator)
11612                            param.qualifiers->Insert(null, MkSpecifier(CONST));
11613                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11614                      }
11615                      else if(spec.specifier == THISCLASS)
11616                      {
11617                         if(thisClass)
11618                         {
11619                            spec.type = nameSpecifier;
11620                            spec.name = ReplaceThisClass(thisClass);
11621                            spec.symbol = FindClass(spec.name);
11622                            ProcessSpecifier(spec, false);
11623                         }
11624                      }
11625                   }
11626
11627                   if(param.declarator)
11628                      ProcessDeclarator(param.declarator);
11629                }
11630             }
11631          }
11632          break;
11633    }
11634 }
11635
11636 static void ProcessDeclaration(Declaration decl)
11637 {
11638    yylloc = decl.loc;
11639    switch(decl.type)
11640    {
11641       case initDeclaration:
11642       {
11643          bool declareStruct = false;
11644          /*
11645          lineNum = decl.pos.line;
11646          column = decl.pos.col;
11647          */
11648
11649          if(decl.declarators)
11650          {
11651             InitDeclarator d;
11652
11653             for(d = decl.declarators->first; d; d = d.next)
11654             {
11655                Type type, subType;
11656                ProcessDeclarator(d.declarator);
11657
11658                type = ProcessType(decl.specifiers, d.declarator);
11659
11660                if(d.initializer)
11661                {
11662                   ProcessInitializer(d.initializer, type);
11663
11664                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11665
11666                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11667                      d.initializer.exp.type == instanceExp)
11668                   {
11669                      if(type.kind == classType && type._class ==
11670                         d.initializer.exp.expType._class)
11671                      {
11672                         Instantiation inst = d.initializer.exp.instance;
11673                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11674
11675                         d.initializer.exp.instance = null;
11676                         if(decl.specifiers)
11677                            FreeList(decl.specifiers, FreeSpecifier);
11678                         FreeList(decl.declarators, FreeInitDeclarator);
11679
11680                         d = null;
11681
11682                         decl.type = instDeclaration;
11683                         decl.inst = inst;
11684                      }
11685                   }
11686                }
11687                for(subType = type; subType;)
11688                {
11689                   if(subType.kind == classType)
11690                   {
11691                      declareStruct = true;
11692                      break;
11693                   }
11694                   else if(subType.kind == pointerType)
11695                      break;
11696                   else if(subType.kind == arrayType)
11697                      subType = subType.arrayType;
11698                   else
11699                      break;
11700                }
11701
11702                FreeType(type);
11703                if(!d) break;
11704             }
11705          }
11706
11707          if(decl.specifiers)
11708          {
11709             Specifier s;
11710             for(s = decl.specifiers->first; s; s = s.next)
11711             {
11712                ProcessSpecifier(s, declareStruct);
11713             }
11714          }
11715          break;
11716       }
11717       case instDeclaration:
11718       {
11719          ProcessInstantiationType(decl.inst);
11720          break;
11721       }
11722       case structDeclaration:
11723       {
11724          Specifier spec;
11725          Declarator d;
11726          bool declareStruct = false;
11727
11728          if(decl.declarators)
11729          {
11730             for(d = decl.declarators->first; d; d = d.next)
11731             {
11732                Type type = ProcessType(decl.specifiers, d.declarator);
11733                Type subType;
11734                ProcessDeclarator(d);
11735                for(subType = type; subType;)
11736                {
11737                   if(subType.kind == classType)
11738                   {
11739                      declareStruct = true;
11740                      break;
11741                   }
11742                   else if(subType.kind == pointerType)
11743                      break;
11744                   else if(subType.kind == arrayType)
11745                      subType = subType.arrayType;
11746                   else
11747                      break;
11748                }
11749                FreeType(type);
11750             }
11751          }
11752          if(decl.specifiers)
11753          {
11754             for(spec = decl.specifiers->first; spec; spec = spec.next)
11755                ProcessSpecifier(spec, declareStruct);
11756          }
11757          break;
11758       }
11759    }
11760 }
11761
11762 static FunctionDefinition curFunction;
11763
11764 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11765 {
11766    char propName[1024], propNameM[1024];
11767    char getName[1024], setName[1024];
11768    OldList * args;
11769
11770    DeclareProperty(prop, setName, getName);
11771
11772    // eInstance_FireWatchers(object, prop);
11773    strcpy(propName, "__ecereProp_");
11774    FullClassNameCat(propName, prop._class.fullName, false);
11775    strcat(propName, "_");
11776    // strcat(propName, prop.name);
11777    FullClassNameCat(propName, prop.name, true);
11778    //MangleClassName(propName);
11779
11780    strcpy(propNameM, "__ecerePropM_");
11781    FullClassNameCat(propNameM, prop._class.fullName, false);
11782    strcat(propNameM, "_");
11783    // strcat(propNameM, prop.name);
11784    FullClassNameCat(propNameM, prop.name, true);
11785    //MangleClassName(propNameM);
11786
11787    if(prop.isWatchable)
11788    {
11789       args = MkList();
11790       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11791       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11792       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11793
11794       args = MkList();
11795       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11796       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11797       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11798    }
11799
11800
11801    {
11802       args = MkList();
11803       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11804       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11805       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11806
11807       args = MkList();
11808       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11809       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11810       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11811    }
11812
11813    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11814       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11815       curFunction.propSet.fireWatchersDone = true;
11816 }
11817
11818 static void ProcessStatement(Statement stmt)
11819 {
11820    yylloc = stmt.loc;
11821    /*
11822    lineNum = stmt.pos.line;
11823    column = stmt.pos.col;
11824    */
11825    switch(stmt.type)
11826    {
11827       case labeledStmt:
11828          ProcessStatement(stmt.labeled.stmt);
11829          break;
11830       case caseStmt:
11831          // This expression should be constant...
11832          if(stmt.caseStmt.exp)
11833          {
11834             FreeType(stmt.caseStmt.exp.destType);
11835             stmt.caseStmt.exp.destType = curSwitchType;
11836             if(curSwitchType) curSwitchType.refCount++;
11837             ProcessExpressionType(stmt.caseStmt.exp);
11838             ComputeExpression(stmt.caseStmt.exp);
11839          }
11840          if(stmt.caseStmt.stmt)
11841             ProcessStatement(stmt.caseStmt.stmt);
11842          break;
11843       case compoundStmt:
11844       {
11845          if(stmt.compound.context)
11846          {
11847             Declaration decl;
11848             Statement s;
11849
11850             Statement prevCompound = curCompound;
11851             Context prevContext = curContext;
11852
11853             if(!stmt.compound.isSwitch)
11854                curCompound = stmt;
11855             curContext = stmt.compound.context;
11856
11857             if(stmt.compound.declarations)
11858             {
11859                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11860                   ProcessDeclaration(decl);
11861             }
11862             if(stmt.compound.statements)
11863             {
11864                for(s = stmt.compound.statements->first; s; s = s.next)
11865                   ProcessStatement(s);
11866             }
11867
11868             curContext = prevContext;
11869             curCompound = prevCompound;
11870          }
11871          break;
11872       }
11873       case expressionStmt:
11874       {
11875          Expression exp;
11876          if(stmt.expressions)
11877          {
11878             for(exp = stmt.expressions->first; exp; exp = exp.next)
11879                ProcessExpressionType(exp);
11880          }
11881          break;
11882       }
11883       case ifStmt:
11884       {
11885          Expression exp;
11886
11887          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11888          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11889          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11890          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11891          {
11892             ProcessExpressionType(exp);
11893          }
11894          if(stmt.ifStmt.stmt)
11895             ProcessStatement(stmt.ifStmt.stmt);
11896          if(stmt.ifStmt.elseStmt)
11897             ProcessStatement(stmt.ifStmt.elseStmt);
11898          break;
11899       }
11900       case switchStmt:
11901       {
11902          Type oldSwitchType = curSwitchType;
11903          if(stmt.switchStmt.exp)
11904          {
11905             Expression exp;
11906             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11907             {
11908                if(!exp.next)
11909                {
11910                   /*
11911                   Type destType
11912                   {
11913                      kind = intType;
11914                      refCount = 1;
11915                   };
11916                   e.exp.destType = destType;
11917                   */
11918
11919                   ProcessExpressionType(exp);
11920                }
11921                if(!exp.next)
11922                   curSwitchType = exp.expType;
11923             }
11924          }
11925          ProcessStatement(stmt.switchStmt.stmt);
11926          curSwitchType = oldSwitchType;
11927          break;
11928       }
11929       case whileStmt:
11930       {
11931          if(stmt.whileStmt.exp)
11932          {
11933             Expression exp;
11934
11935             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11936             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11937             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11938             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11939             {
11940                ProcessExpressionType(exp);
11941             }
11942          }
11943          if(stmt.whileStmt.stmt)
11944             ProcessStatement(stmt.whileStmt.stmt);
11945          break;
11946       }
11947       case doWhileStmt:
11948       {
11949          if(stmt.doWhile.exp)
11950          {
11951             Expression exp;
11952
11953             if(stmt.doWhile.exp->last)
11954             {
11955                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11956                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11957                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11958             }
11959             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11960             {
11961                ProcessExpressionType(exp);
11962             }
11963          }
11964          if(stmt.doWhile.stmt)
11965             ProcessStatement(stmt.doWhile.stmt);
11966          break;
11967       }
11968       case forStmt:
11969       {
11970          Expression exp;
11971          if(stmt.forStmt.init)
11972             ProcessStatement(stmt.forStmt.init);
11973
11974          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11975          {
11976             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11977             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11978             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11979          }
11980
11981          if(stmt.forStmt.check)
11982             ProcessStatement(stmt.forStmt.check);
11983          if(stmt.forStmt.increment)
11984          {
11985             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11986                ProcessExpressionType(exp);
11987          }
11988
11989          if(stmt.forStmt.stmt)
11990             ProcessStatement(stmt.forStmt.stmt);
11991          break;
11992       }
11993       case forEachStmt:
11994       {
11995          Identifier id = stmt.forEachStmt.id;
11996          OldList * exp = stmt.forEachStmt.exp;
11997          OldList * filter = stmt.forEachStmt.filter;
11998          Statement block = stmt.forEachStmt.stmt;
11999          char iteratorType[1024];
12000          Type source;
12001          Expression e;
12002          bool isBuiltin = exp && exp->last &&
12003             (((Expression)exp->last).type == ExpressionType::arrayExp ||
12004               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
12005          Expression arrayExp;
12006          const char * typeString = null;
12007          int builtinCount = 0;
12008
12009          for(e = exp ? exp->first : null; e; e = e.next)
12010          {
12011             if(!e.next)
12012             {
12013                FreeType(e.destType);
12014                e.destType = ProcessTypeString("Container", false);
12015             }
12016             if(!isBuiltin || e.next)
12017                ProcessExpressionType(e);
12018          }
12019
12020          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
12021          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
12022             eClass_IsDerived(source._class.registered, containerClass)))
12023          {
12024             Class _class = source ? source._class.registered : null;
12025             Symbol symbol;
12026             Expression expIt = null;
12027             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
12028             Class arrayClass = eSystem_FindClass(privateModule, "Array");
12029             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
12030             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
12031
12032             if(inCompiler)
12033             {
12034                stmt.type = compoundStmt;
12035
12036                stmt.compound.context = Context { };
12037                stmt.compound.context.parent = curContext;
12038                curContext = stmt.compound.context;
12039             }
12040
12041             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
12042             {
12043                Class mapClass = eSystem_FindClass(privateModule, "Map");
12044                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
12045                isCustomAVLTree = true;
12046                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
12047                   isAVLTree = true;
12048                else */if(eClass_IsDerived(source._class.registered, mapClass))
12049                   isMap = true;
12050             }
12051             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
12052             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
12053             {
12054                Class listClass = eSystem_FindClass(privateModule, "List");
12055                isLinkList = true;
12056                isList = eClass_IsDerived(source._class.registered, listClass);
12057             }
12058
12059             if(inCompiler && isArray)
12060             {
12061                Declarator decl;
12062                OldList * specs = MkList();
12063                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12064                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12065                stmt.compound.declarations = MkListOne(
12066                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12067                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12068                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
12069                      MkInitializerAssignment(MkExpBrackets(exp))))));
12070             }
12071             else if(isBuiltin)
12072             {
12073                Type type = null;
12074                char typeStringBuf[1024];
12075
12076                // TODO: Merge this code?
12077                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
12078                if(((Expression)exp->last).type == castExp)
12079                {
12080                   TypeName typeName = ((Expression)exp->last).cast.typeName;
12081                   if(typeName)
12082                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
12083                }
12084
12085                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
12086                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
12087                   arrayExp.destType._class.registered.templateArgs)
12088                {
12089                   Class templateClass = arrayExp.destType._class.registered;
12090                   typeString = templateClass.templateArgs[2].dataTypeString;
12091                }
12092                else if(arrayExp.list)
12093                {
12094                   // Guess type from expressions in the array
12095                   Expression e;
12096                   for(e = arrayExp.list->first; e; e = e.next)
12097                   {
12098                      ProcessExpressionType(e);
12099                      if(e.expType)
12100                      {
12101                         if(!type) { type = e.expType; type.refCount++; }
12102                         else
12103                         {
12104                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
12105                            if(!MatchTypeExpression(e, type, null, false, true))
12106                            {
12107                               FreeType(type);
12108                               type = e.expType;
12109                               e.expType = null;
12110
12111                               e = arrayExp.list->first;
12112                               ProcessExpressionType(e);
12113                               if(e.expType)
12114                               {
12115                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
12116                                  if(!MatchTypeExpression(e, type, null, false, true))
12117                                  {
12118                                     FreeType(e.expType);
12119                                     e.expType = null;
12120                                     FreeType(type);
12121                                     type = null;
12122                                     break;
12123                                  }
12124                               }
12125                            }
12126                         }
12127                         if(e.expType)
12128                         {
12129                            FreeType(e.expType);
12130                            e.expType = null;
12131                         }
12132                      }
12133                   }
12134                   if(type)
12135                   {
12136                      typeStringBuf[0] = '\0';
12137                      PrintType(type, typeStringBuf, false, true);
12138                      typeString = typeStringBuf;
12139                      FreeType(type);
12140                   }
12141                }
12142                if(typeString)
12143                {
12144                   if(inCompiler)
12145                   {
12146                      OldList * initializers = MkList();
12147                      Declarator decl;
12148                      OldList * specs = MkList();
12149                      if(arrayExp.list)
12150                      {
12151                         Expression e;
12152
12153                         builtinCount = arrayExp.list->count;
12154                         type = ProcessTypeString(typeString, false);
12155                         while((e = arrayExp.list->first))
12156                         {
12157                            arrayExp.list->Remove(e);
12158                            e.destType = type;
12159                            type.refCount++;
12160                            ProcessExpressionType(e);
12161                            if(inCompiler)
12162                               ListAdd(initializers, MkInitializerAssignment(e));
12163                         }
12164                         FreeType(type);
12165                         delete arrayExp.list;
12166                      }
12167                      decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12168
12169                      stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12170                         MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12171
12172                      ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12173                         PlugDeclarator(
12174                            /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12175                            ), MkInitializerList(initializers)))));
12176                      FreeList(exp, FreeExpression);
12177                   }
12178                   else if(arrayExp.list)
12179                   {
12180                      Expression e;
12181                      type = ProcessTypeString(typeString, false);
12182                      for(e = arrayExp.list->first; e; e = e.next)
12183                      {
12184                         e.destType = type;
12185                         type.refCount++;
12186                         ProcessExpressionType(e);
12187                      }
12188                      FreeType(type);
12189                   }
12190                }
12191                else
12192                {
12193                   arrayExp.expType = ProcessTypeString("Container", false);
12194                   Compiler_Error($"Couldn't determine type of array elements\n");
12195                }
12196
12197                /*
12198                Declarator decl;
12199                OldList * specs = MkList();
12200
12201                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12202                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12203                stmt.compound.declarations = MkListOne(
12204                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12205                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12206                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12207                      MkInitializerAssignment(MkExpBrackets(exp))))));
12208                */
12209             }
12210             else if(inCompiler && isLinkList && !isList)
12211             {
12212                Declarator decl;
12213                OldList * specs = MkList();
12214                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12215                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12216                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12217                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12218                      MkInitializerAssignment(MkExpBrackets(exp))))));
12219             }
12220             /*else if(isCustomAVLTree)
12221             {
12222                Declarator decl;
12223                OldList * specs = MkList();
12224                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12225                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12226                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12227                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12228                      MkInitializerAssignment(MkExpBrackets(exp))))));
12229             }*/
12230             else if(inCompiler && _class.templateArgs)
12231             {
12232                if(isMap)
12233                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12234                else
12235                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12236
12237                stmt.compound.declarations = MkListOne(
12238                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12239                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12240                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12241             }
12242             if(inCompiler)
12243             {
12244                symbol = FindSymbol(id.string, curContext, curContext, false, false);
12245
12246                if(block)
12247                {
12248                   // Reparent sub-contexts in this statement
12249                   switch(block.type)
12250                   {
12251                      case compoundStmt:
12252                         if(block.compound.context)
12253                            block.compound.context.parent = stmt.compound.context;
12254                         break;
12255                      case ifStmt:
12256                         if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12257                            block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12258                         if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12259                            block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12260                         break;
12261                      case switchStmt:
12262                         if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12263                            block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12264                         break;
12265                      case whileStmt:
12266                         if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12267                            block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12268                         break;
12269                      case doWhileStmt:
12270                         if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12271                            block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12272                         break;
12273                      case forStmt:
12274                         if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12275                            block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12276                         break;
12277                      case forEachStmt:
12278                         if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12279                            block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12280                         break;
12281                      /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12282                      case labeledStmt:
12283                      case caseStmt
12284                      case expressionStmt:
12285                      case gotoStmt:
12286                      case continueStmt:
12287                      case breakStmt
12288                      case returnStmt:
12289                      case asmStmt:
12290                      case badDeclarationStmt:
12291                      case fireWatchersStmt:
12292                      case stopWatchingStmt:
12293                      case watchStmt:
12294                      */
12295                   }
12296                }
12297
12298                if(filter)
12299                {
12300                   block = MkIfStmt(filter, block, null);
12301                }
12302                if(isArray)
12303                {
12304                   stmt.compound.statements = MkListOne(MkForStmt(
12305                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12306                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12307                         MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12308                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12309                      block));
12310                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12311                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12312                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12313                }
12314                else if(isBuiltin)
12315                {
12316                   char count[128];
12317                   //OldList * specs = MkList();
12318                   // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12319
12320                   sprintf(count, "%d", builtinCount);
12321
12322                   stmt.compound.statements = MkListOne(MkForStmt(
12323                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12324                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12325                         MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12326                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12327                      block));
12328
12329                   /*
12330                   Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12331                   stmt.compound.statements = MkListOne(MkForStmt(
12332                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12333                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12334                         MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12335                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12336                      block));
12337                  */
12338                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12339                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12340                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12341                }
12342                else if(isLinkList && !isList)
12343                {
12344                   Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12345                   Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12346                   if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12347                      !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12348                   {
12349                      stmt.compound.statements = MkListOne(MkForStmt(
12350                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12351                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12352                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12353                         block));
12354                   }
12355                   else
12356                   {
12357                      OldList * specs = MkList();
12358                      Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12359                      stmt.compound.statements = MkListOne(MkForStmt(
12360                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12361                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12362                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12363                            MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12364                               MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12365                         block));
12366                   }
12367                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12368                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12369                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12370                }
12371                /*else if(isCustomAVLTree)
12372                {
12373                   stmt.compound.statements = MkListOne(MkForStmt(
12374                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12375                         MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12376                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12377                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12378                      block));
12379
12380                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12381                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12382                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12383                }*/
12384                else
12385                {
12386                   stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12387                      MkIdentifier("Next")), null)), block));
12388                }
12389                ProcessExpressionType(expIt);
12390                if(stmt.compound.declarations->first)
12391                   ProcessDeclaration(stmt.compound.declarations->first);
12392
12393                if(symbol)
12394                   symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12395
12396                ProcessStatement(stmt);
12397             }
12398             else
12399                ProcessStatement(stmt.forEachStmt.stmt);
12400             if(inCompiler)
12401                curContext = stmt.compound.context.parent;
12402             break;
12403          }
12404          else
12405          {
12406             Compiler_Error($"Expression is not a container\n");
12407          }
12408          break;
12409       }
12410       case gotoStmt:
12411          break;
12412       case continueStmt:
12413          break;
12414       case breakStmt:
12415          break;
12416       case returnStmt:
12417       {
12418          Expression exp;
12419          if(stmt.expressions)
12420          {
12421             for(exp = stmt.expressions->first; exp; exp = exp.next)
12422             {
12423                if(!exp.next)
12424                {
12425                   if(curFunction && !curFunction.type)
12426                      curFunction.type = ProcessType(
12427                         curFunction.specifiers, curFunction.declarator);
12428                   FreeType(exp.destType);
12429                   // TODO: current property if not compiling
12430                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12431                   if(exp.destType) exp.destType.refCount++;
12432                }
12433                ProcessExpressionType(exp);
12434             }
12435          }
12436          break;
12437       }
12438       case badDeclarationStmt:
12439       {
12440          ProcessDeclaration(stmt.decl);
12441          break;
12442       }
12443       case asmStmt:
12444       {
12445          AsmField field;
12446          if(stmt.asmStmt.inputFields)
12447          {
12448             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12449                if(field.expression)
12450                   ProcessExpressionType(field.expression);
12451          }
12452          if(stmt.asmStmt.outputFields)
12453          {
12454             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12455                if(field.expression)
12456                   ProcessExpressionType(field.expression);
12457          }
12458          if(stmt.asmStmt.clobberedFields)
12459          {
12460             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12461             {
12462                if(field.expression)
12463                   ProcessExpressionType(field.expression);
12464             }
12465          }
12466          break;
12467       }
12468       case watchStmt:
12469       {
12470          PropertyWatch propWatch;
12471          OldList * watches = stmt._watch.watches;
12472          Expression object = stmt._watch.object;
12473          Expression watcher = stmt._watch.watcher;
12474          if(watcher)
12475             ProcessExpressionType(watcher);
12476          if(object)
12477             ProcessExpressionType(object);
12478
12479          if(inCompiler)
12480          {
12481             if(watcher || thisClass)
12482             {
12483                External external = curExternal;
12484                Context context = curContext;
12485
12486                stmt.type = expressionStmt;
12487                stmt.expressions = MkList();
12488
12489                curExternal = external.prev;
12490
12491                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12492                {
12493                   ClassFunction func;
12494                   char watcherName[1024];
12495                   Class watcherClass = watcher ?
12496                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12497                   External createdExternal;
12498
12499                   // Create a declaration above
12500                   External externalDecl = MkExternalDeclaration(null);
12501                   ast->Insert(curExternal.prev, externalDecl);
12502
12503                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12504                   if(propWatch.deleteWatch)
12505                      strcat(watcherName, "_delete");
12506                   else
12507                   {
12508                      Identifier propID;
12509                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12510                      {
12511                         strcat(watcherName, "_");
12512                         strcat(watcherName, propID.string);
12513                      }
12514                   }
12515
12516                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12517                   {
12518                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12519                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12520                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12521                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12522                      ProcessClassFunctionBody(func, propWatch.compound);
12523                      propWatch.compound = null;
12524
12525                      //afterExternal = afterExternal ? afterExternal : curExternal;
12526
12527                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12528                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12529                      // TESTING THIS...
12530                      createdExternal.symbol.idCode = external.symbol.idCode;
12531
12532                      curExternal = createdExternal;
12533                      ProcessFunction(createdExternal.function);
12534
12535
12536                      // Create a declaration above
12537                      {
12538                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12539                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12540                         externalDecl.declaration = decl;
12541                         if(decl.symbol && !decl.symbol.pointerExternal)
12542                            decl.symbol.pointerExternal = externalDecl;
12543                      }
12544
12545                      if(propWatch.deleteWatch)
12546                      {
12547                         OldList * args = MkList();
12548                         ListAdd(args, CopyExpression(object));
12549                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12550                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12551                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12552                      }
12553                      else
12554                      {
12555                         Class _class = object.expType._class.registered;
12556                         Identifier propID;
12557
12558                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12559                         {
12560                            char propName[1024];
12561                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12562                            if(prop)
12563                            {
12564                               char getName[1024], setName[1024];
12565                               OldList * args = MkList();
12566
12567                               DeclareProperty(prop, setName, getName);
12568
12569                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12570                               strcpy(propName, "__ecereProp_");
12571                               FullClassNameCat(propName, prop._class.fullName, false);
12572                               strcat(propName, "_");
12573                               // strcat(propName, prop.name);
12574                               FullClassNameCat(propName, prop.name, true);
12575
12576                               ListAdd(args, CopyExpression(object));
12577                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12578                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12579                               ListAdd(args, MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpIdentifier(MkIdentifier(watcherName))));
12580
12581                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12582                            }
12583                            else
12584                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12585                         }
12586                      }
12587                   }
12588                   else
12589                      Compiler_Error($"Invalid watched object\n");
12590                }
12591
12592                curExternal = external;
12593                curContext = context;
12594
12595                if(watcher)
12596                   FreeExpression(watcher);
12597                if(object)
12598                   FreeExpression(object);
12599                FreeList(watches, FreePropertyWatch);
12600             }
12601             else
12602                Compiler_Error($"No observer specified and not inside a _class\n");
12603          }
12604          else
12605          {
12606             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12607             {
12608                ProcessStatement(propWatch.compound);
12609             }
12610
12611          }
12612          break;
12613       }
12614       case fireWatchersStmt:
12615       {
12616          OldList * watches = stmt._watch.watches;
12617          Expression object = stmt._watch.object;
12618          Class _class;
12619          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12620          // printf("%X\n", watches);
12621          // printf("%X\n", stmt._watch.watches);
12622          if(object)
12623             ProcessExpressionType(object);
12624
12625          if(inCompiler)
12626          {
12627             _class = object ?
12628                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12629
12630             if(_class)
12631             {
12632                Identifier propID;
12633
12634                stmt.type = expressionStmt;
12635                stmt.expressions = MkList();
12636
12637                // Check if we're inside a property set
12638                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12639                {
12640                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12641                }
12642                else if(!watches)
12643                {
12644                   //Compiler_Error($"No property specified and not inside a property set\n");
12645                }
12646                if(watches)
12647                {
12648                   for(propID = watches->first; propID; propID = propID.next)
12649                   {
12650                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12651                      if(prop)
12652                      {
12653                         CreateFireWatcher(prop, object, stmt);
12654                      }
12655                      else
12656                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12657                   }
12658                }
12659                else
12660                {
12661                   // Fire all properties!
12662                   Property prop;
12663                   Class base;
12664                   for(base = _class; base; base = base.base)
12665                   {
12666                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12667                      {
12668                         if(prop.isProperty && prop.isWatchable)
12669                         {
12670                            CreateFireWatcher(prop, object, stmt);
12671                         }
12672                      }
12673                   }
12674                }
12675
12676                if(object)
12677                   FreeExpression(object);
12678                FreeList(watches, FreeIdentifier);
12679             }
12680             else
12681                Compiler_Error($"Invalid object specified and not inside a class\n");
12682          }
12683          break;
12684       }
12685       case stopWatchingStmt:
12686       {
12687          OldList * watches = stmt._watch.watches;
12688          Expression object = stmt._watch.object;
12689          Expression watcher = stmt._watch.watcher;
12690          Class _class;
12691          if(object)
12692             ProcessExpressionType(object);
12693          if(watcher)
12694             ProcessExpressionType(watcher);
12695          if(inCompiler)
12696          {
12697             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12698
12699             if(watcher || thisClass)
12700             {
12701                if(_class)
12702                {
12703                   Identifier propID;
12704
12705                   stmt.type = expressionStmt;
12706                   stmt.expressions = MkList();
12707
12708                   if(!watches)
12709                   {
12710                      OldList * args;
12711                      // eInstance_StopWatching(object, null, watcher);
12712                      args = MkList();
12713                      ListAdd(args, CopyExpression(object));
12714                      ListAdd(args, MkExpConstant("0"));
12715                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12716                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12717                   }
12718                   else
12719                   {
12720                      for(propID = watches->first; propID; propID = propID.next)
12721                      {
12722                         char propName[1024];
12723                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12724                         if(prop)
12725                         {
12726                            char getName[1024], setName[1024];
12727                            OldList * args = MkList();
12728
12729                            DeclareProperty(prop, setName, getName);
12730
12731                            // eInstance_StopWatching(object, prop, watcher);
12732                            strcpy(propName, "__ecereProp_");
12733                            FullClassNameCat(propName, prop._class.fullName, false);
12734                            strcat(propName, "_");
12735                            // strcat(propName, prop.name);
12736                            FullClassNameCat(propName, prop.name, true);
12737                            //MangleClassName(propName);
12738
12739                            ListAdd(args, CopyExpression(object));
12740                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12741                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12742                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12743                         }
12744                         else
12745                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12746                      }
12747                   }
12748
12749                   if(object)
12750                      FreeExpression(object);
12751                   if(watcher)
12752                      FreeExpression(watcher);
12753                   FreeList(watches, FreeIdentifier);
12754                }
12755                else
12756                   Compiler_Error($"Invalid object specified and not inside a class\n");
12757             }
12758             else
12759                Compiler_Error($"No observer specified and not inside a class\n");
12760          }
12761          break;
12762       }
12763    }
12764 }
12765
12766 static void ProcessFunction(FunctionDefinition function)
12767 {
12768    Identifier id = GetDeclId(function.declarator);
12769    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12770    Type type = symbol ? symbol.type : null;
12771    Class oldThisClass = thisClass;
12772    Context oldTopContext = topContext;
12773
12774    yylloc = function.loc;
12775    // Process thisClass
12776
12777    if(type && type.thisClass)
12778    {
12779       Symbol classSym = type.thisClass;
12780       Class _class = type.thisClass.registered;
12781       char className[1024];
12782       char structName[1024];
12783       Declarator funcDecl;
12784       Symbol thisSymbol;
12785
12786       bool typedObject = false;
12787
12788       if(_class && !_class.base)
12789       {
12790          _class = currentClass;
12791          if(_class && !_class.symbol)
12792             _class.symbol = FindClass(_class.fullName);
12793          classSym = _class ? _class.symbol : null;
12794          typedObject = true;
12795       }
12796
12797       thisClass = _class;
12798
12799       if(inCompiler && _class)
12800       {
12801          if(type.kind == functionType)
12802          {
12803             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12804             {
12805                //TypeName param = symbol.type.params.first;
12806                Type param = symbol.type.params.first;
12807                symbol.type.params.Remove(param);
12808                //FreeTypeName(param);
12809                FreeType(param);
12810             }
12811             if(type.classObjectType != classPointer)
12812             {
12813                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12814                symbol.type.staticMethod = true;
12815                symbol.type.thisClass = null;
12816
12817                // HIGH DANGER: VERIFYING THIS...
12818                symbol.type.extraParam = false;
12819             }
12820          }
12821
12822          strcpy(className, "__ecereClass_");
12823          FullClassNameCat(className, _class.fullName, true);
12824
12825          //MangleClassName(className);
12826
12827          structName[0] = 0;
12828          FullClassNameCat(structName, _class.fullName, false);
12829
12830          // [class] this
12831
12832
12833          funcDecl = GetFuncDecl(function.declarator);
12834          if(funcDecl)
12835          {
12836             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12837             {
12838                TypeName param = funcDecl.function.parameters->first;
12839                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12840                {
12841                   funcDecl.function.parameters->Remove(param);
12842                   FreeTypeName(param);
12843                }
12844             }
12845
12846             // DANGER: Watch for this... Check if it's a Conversion?
12847             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12848
12849             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12850             if(!function.propertyNoThis)
12851             {
12852                TypeName thisParam = null;
12853
12854                if(type.classObjectType != classPointer)
12855                {
12856                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12857                   if(!funcDecl.function.parameters)
12858                      funcDecl.function.parameters = MkList();
12859                   funcDecl.function.parameters->Insert(null, thisParam);
12860                }
12861
12862                if(typedObject)
12863                {
12864                   if(type.classObjectType != classPointer)
12865                   {
12866                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12867                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12868                   }
12869
12870                   thisParam = TypeName
12871                   {
12872                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12873                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12874                   };
12875                   funcDecl.function.parameters->Insert(null, thisParam);
12876                }
12877             }
12878          }
12879
12880          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12881          {
12882             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12883             funcDecl = GetFuncDecl(initDecl.declarator);
12884             if(funcDecl)
12885             {
12886                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12887                {
12888                   TypeName param = funcDecl.function.parameters->first;
12889                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12890                   {
12891                      funcDecl.function.parameters->Remove(param);
12892                      FreeTypeName(param);
12893                   }
12894                }
12895
12896                if(type.classObjectType != classPointer)
12897                {
12898                   // DANGER: Watch for this... Check if it's a Conversion?
12899                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12900                   {
12901                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12902
12903                      if(!funcDecl.function.parameters)
12904                         funcDecl.function.parameters = MkList();
12905                      funcDecl.function.parameters->Insert(null, thisParam);
12906                   }
12907                }
12908             }
12909          }
12910       }
12911
12912       // Add this to the context
12913       if(function.body)
12914       {
12915          if(type.classObjectType != classPointer)
12916          {
12917             thisSymbol = Symbol
12918             {
12919                string = CopyString("this");
12920                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12921             };
12922             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12923
12924             if(typedObject && thisSymbol.type)
12925             {
12926                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12927                thisSymbol.type.byReference = type.byReference;
12928                thisSymbol.type.typedByReference = type.byReference;
12929                /*
12930                thisSymbol = Symbol { string = CopyString("class") };
12931                function.body.compound.context.symbols.Add(thisSymbol);
12932                */
12933             }
12934          }
12935       }
12936
12937       // Pointer to class data
12938
12939       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12940       {
12941          DataMember member = null;
12942          {
12943             Class base;
12944             for(base = _class; base && base.type != systemClass; base = base.next)
12945             {
12946                for(member = base.membersAndProperties.first; member; member = member.next)
12947                   if(!member.isProperty)
12948                      break;
12949                if(member)
12950                   break;
12951             }
12952          }
12953          for(member = _class.membersAndProperties.first; member; member = member.next)
12954             if(!member.isProperty)
12955                break;
12956          if(member)
12957          {
12958             char pointerName[1024];
12959
12960             Declaration decl;
12961             Initializer initializer;
12962             Expression exp, bytePtr;
12963
12964             strcpy(pointerName, "__ecerePointer_");
12965             FullClassNameCat(pointerName, _class.fullName, false);
12966             {
12967                char className[1024];
12968                strcpy(className, "__ecereClass_");
12969                FullClassNameCat(className, classSym.string, true);
12970                //MangleClassName(className);
12971
12972                // Testing This
12973                DeclareClass(classSym, className);
12974             }
12975
12976             // ((byte *) this)
12977             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12978
12979             if(_class.fixed)
12980             {
12981                char string[256];
12982                sprintf(string, "%d", _class.offset);
12983                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12984             }
12985             else
12986             {
12987                // ([bytePtr] + [className]->offset)
12988                exp = QBrackets(MkExpOp(bytePtr, '+',
12989                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12990             }
12991
12992             // (this ? [exp] : 0)
12993             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12994             exp.expType = Type
12995             {
12996                refCount = 1;
12997                kind = pointerType;
12998                type = Type { refCount = 1, kind = voidType };
12999             };
13000
13001             if(function.body)
13002             {
13003                yylloc = function.body.loc;
13004                // ([structName] *) [exp]
13005                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
13006                initializer = MkInitializerAssignment(
13007                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
13008
13009                // [structName] * [pointerName] = [initializer];
13010                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
13011
13012                {
13013                   Context prevContext = curContext;
13014                   OldList * list;
13015                   curContext = function.body.compound.context;
13016
13017                   decl = MkDeclaration((list = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null))),
13018                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
13019                   list->Insert(null, MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
13020
13021                   curContext = prevContext;
13022                }
13023
13024                // WHY?
13025                decl.symbol = null;
13026
13027                if(!function.body.compound.declarations)
13028                   function.body.compound.declarations = MkList();
13029                function.body.compound.declarations->Insert(null, decl);
13030             }
13031          }
13032       }
13033
13034
13035       // Loop through the function and replace undeclared identifiers
13036       // which are a member of the class (methods, properties or data)
13037       // by "this.[member]"
13038    }
13039    else
13040       thisClass = null;
13041
13042    if(id)
13043    {
13044       FreeSpecifier(id._class);
13045       id._class = null;
13046
13047       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
13048       {
13049          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
13050          id = GetDeclId(initDecl.declarator);
13051
13052          FreeSpecifier(id._class);
13053          id._class = null;
13054       }
13055    }
13056    if(function.body)
13057       topContext = function.body.compound.context;
13058    {
13059       FunctionDefinition oldFunction = curFunction;
13060       curFunction = function;
13061       if(function.body)
13062          ProcessStatement(function.body);
13063
13064       // If this is a property set and no firewatchers has been done yet, add one here
13065       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
13066       {
13067          Statement prevCompound = curCompound;
13068          Context prevContext = curContext;
13069
13070          Statement fireWatchers = MkFireWatchersStmt(null, null);
13071          if(!function.body.compound.statements) function.body.compound.statements = MkList();
13072          ListAdd(function.body.compound.statements, fireWatchers);
13073
13074          curCompound = function.body;
13075          curContext = function.body.compound.context;
13076
13077          ProcessStatement(fireWatchers);
13078
13079          curContext = prevContext;
13080          curCompound = prevCompound;
13081
13082       }
13083
13084       curFunction = oldFunction;
13085    }
13086
13087    if(function.declarator)
13088    {
13089       ProcessDeclarator(function.declarator);
13090    }
13091
13092    topContext = oldTopContext;
13093    thisClass = oldThisClass;
13094 }
13095
13096 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
13097 static void ProcessClass(OldList definitions, Symbol symbol)
13098 {
13099    ClassDef def;
13100    External external = curExternal;
13101    Class regClass = symbol ? symbol.registered : null;
13102
13103    // Process all functions
13104    for(def = definitions.first; def; def = def.next)
13105    {
13106       if(def.type == functionClassDef)
13107       {
13108          if(def.function.declarator)
13109             curExternal = def.function.declarator.symbol.pointerExternal;
13110          else
13111             curExternal = external;
13112
13113          ProcessFunction((FunctionDefinition)def.function);
13114       }
13115       else if(def.type == declarationClassDef)
13116       {
13117          if(def.decl.type == instDeclaration)
13118          {
13119             thisClass = regClass;
13120             ProcessInstantiationType(def.decl.inst);
13121             thisClass = null;
13122          }
13123          // Testing this
13124          else
13125          {
13126             Class backThisClass = thisClass;
13127             if(regClass) thisClass = regClass;
13128             ProcessDeclaration(def.decl);
13129             thisClass = backThisClass;
13130          }
13131       }
13132       else if(def.type == defaultPropertiesClassDef && def.defProperties)
13133       {
13134          MemberInit defProperty;
13135
13136          // Add this to the context
13137          Symbol thisSymbol = Symbol
13138          {
13139             string = CopyString("this");
13140             type = regClass ? MkClassType(regClass.fullName) : null;
13141          };
13142          globalContext.symbols.Add((BTNode)thisSymbol);
13143
13144          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
13145          {
13146             thisClass = regClass;
13147             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
13148             thisClass = null;
13149          }
13150
13151          globalContext.symbols.Remove((BTNode)thisSymbol);
13152          FreeSymbol(thisSymbol);
13153       }
13154       else if(def.type == propertyClassDef && def.propertyDef)
13155       {
13156          PropertyDef prop = def.propertyDef;
13157
13158          // Add this to the context
13159          /*
13160          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
13161          globalContext.symbols.Add(thisSymbol);
13162          */
13163
13164          thisClass = regClass;
13165          if(prop.setStmt)
13166          {
13167             if(regClass)
13168             {
13169                Symbol thisSymbol
13170                {
13171                   string = CopyString("this");
13172                   type = MkClassType(regClass.fullName);
13173                };
13174                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13175             }
13176
13177             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13178             ProcessStatement(prop.setStmt);
13179          }
13180          if(prop.getStmt)
13181          {
13182             if(regClass)
13183             {
13184                Symbol thisSymbol
13185                {
13186                   string = CopyString("this");
13187                   type = MkClassType(regClass.fullName);
13188                };
13189                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13190             }
13191
13192             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13193             ProcessStatement(prop.getStmt);
13194          }
13195          if(prop.issetStmt)
13196          {
13197             if(regClass)
13198             {
13199                Symbol thisSymbol
13200                {
13201                   string = CopyString("this");
13202                   type = MkClassType(regClass.fullName);
13203                };
13204                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13205             }
13206
13207             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13208             ProcessStatement(prop.issetStmt);
13209          }
13210
13211          thisClass = null;
13212
13213          /*
13214          globalContext.symbols.Remove(thisSymbol);
13215          FreeSymbol(thisSymbol);
13216          */
13217       }
13218       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13219       {
13220          PropertyWatch propertyWatch = def.propertyWatch;
13221
13222          thisClass = regClass;
13223          if(propertyWatch.compound)
13224          {
13225             Symbol thisSymbol
13226             {
13227                string = CopyString("this");
13228                type = regClass ? MkClassType(regClass.fullName) : null;
13229             };
13230
13231             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13232
13233             curExternal = null;
13234             ProcessStatement(propertyWatch.compound);
13235          }
13236          thisClass = null;
13237       }
13238    }
13239 }
13240
13241 void DeclareFunctionUtil(const String s)
13242 {
13243    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13244    if(function)
13245    {
13246       char name[1024];
13247       name[0] = 0;
13248       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13249          strcpy(name, "__ecereFunction_");
13250       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13251       DeclareFunction(function, name);
13252    }
13253 }
13254
13255 void ComputeDataTypes()
13256 {
13257    External external;
13258    External temp { };
13259    External after = null;
13260
13261    currentClass = null;
13262
13263    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13264
13265    for(external = ast->first; external; external = external.next)
13266    {
13267       if(external.type == declarationExternal)
13268       {
13269          Declaration decl = external.declaration;
13270          if(decl)
13271          {
13272             OldList * decls = decl.declarators;
13273             if(decls)
13274             {
13275                InitDeclarator initDecl = decls->first;
13276                if(initDecl)
13277                {
13278                   Declarator declarator = initDecl.declarator;
13279                   if(declarator && declarator.type == identifierDeclarator)
13280                   {
13281                      Identifier id = declarator.identifier;
13282                      if(id && id.string)
13283                      {
13284                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
13285                         {
13286                            external.symbol.id = -1001, external.symbol.idCode = -1001;
13287                            after = external;
13288                         }
13289                      }
13290                   }
13291                }
13292             }
13293          }
13294        }
13295    }
13296
13297    {
13298       // Workaround until we have proper toposort for declarations reordering
13299       External e = MkExternalDeclaration(MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Instance"), null)), null));
13300       ast->Insert(after, e);
13301       after = e;
13302    }
13303
13304    temp.symbol = Symbol { id = -1000, idCode = -1000 };
13305    ast->Insert(after, temp);
13306    curExternal = temp;
13307
13308    DeclareFunctionUtil("eSystem_New");
13309    DeclareFunctionUtil("eSystem_New0");
13310    DeclareFunctionUtil("eSystem_Renew");
13311    DeclareFunctionUtil("eSystem_Renew0");
13312    DeclareFunctionUtil("eSystem_Delete");
13313    DeclareFunctionUtil("eClass_GetProperty");
13314    DeclareFunctionUtil("eClass_SetProperty");
13315    DeclareFunctionUtil("eInstance_FireSelfWatchers");
13316    DeclareFunctionUtil("eInstance_SetMethod");
13317    DeclareFunctionUtil("eInstance_IncRef");
13318    DeclareFunctionUtil("eInstance_StopWatching");
13319    DeclareFunctionUtil("eInstance_Watch");
13320    DeclareFunctionUtil("eInstance_FireWatchers");
13321    if(memoryGuard)
13322    {
13323       DeclareFunctionUtil("MemoryGuard_PushLoc");
13324       DeclareFunctionUtil("MemoryGuard_PopLoc");
13325    }
13326
13327    DeclareStruct("ecere::com::Class", false);
13328    DeclareStruct("ecere::com::Instance", false);
13329    DeclareStruct("ecere::com::Property", false);
13330    DeclareStruct("ecere::com::DataMember", false);
13331    DeclareStruct("ecere::com::Method", false);
13332    DeclareStruct("ecere::com::SerialBuffer", false);
13333    DeclareStruct("ecere::com::ClassTemplateArgument", false);
13334
13335    ast->Remove(temp);
13336
13337    for(external = ast->first; external; external = external.next)
13338    {
13339       afterExternal = curExternal = external;
13340       if(external.type == functionExternal)
13341       {
13342          currentClass = external.function._class;
13343          ProcessFunction(external.function);
13344       }
13345       // There shouldn't be any _class member access here anyways...
13346       else if(external.type == declarationExternal)
13347       {
13348          currentClass = null;
13349          if(external.declaration)
13350             ProcessDeclaration(external.declaration);
13351       }
13352       else if(external.type == classExternal)
13353       {
13354          ClassDefinition _class = external._class;
13355          currentClass = external.symbol.registered;
13356          if(_class.definitions)
13357          {
13358             ProcessClass(_class.definitions, _class.symbol);
13359          }
13360          if(inCompiler)
13361          {
13362             // Free class data...
13363             ast->Remove(external);
13364             delete external;
13365          }
13366       }
13367       else if(external.type == nameSpaceExternal)
13368       {
13369          thisNameSpace = external.id.string;
13370       }
13371    }
13372    currentClass = null;
13373    thisNameSpace = null;
13374    curExternal = null;
13375
13376    delete temp.symbol;
13377    delete temp;
13378 }