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