compiler/libec: Fixed various memory leaks and bugs
[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 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%I64XLL"*/, 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    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
233    return CopyString(temp);
234 }
235
236 public char * PrintUInt64(uint64 result)
237 {
238    char temp[100];
239    if(result > MAXINT64)
240       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
241    else
242       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
243    return CopyString(temp);
244 }
245
246 public char * PrintHexUInt(uint64 result)
247 {
248    char temp[100];
249    if(result > MAXDWORD)
250       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
251    else
252       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
253    if(result > MAXDWORD)
254       strcat(temp, "LL");
255    return CopyString(temp);
256 }
257
258 public char * PrintHexUInt64(uint64 result)
259 {
260    char temp[100];
261    if(result > MAXDWORD)
262       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
263    else
264       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
265    return CopyString(temp);
266 }
267
268 public char * PrintShort(short result)
269 {
270    char temp[100];
271    sprintf(temp, "%d", (unsigned short)result);
272    return CopyString(temp);
273 }
274
275 public char * PrintUShort(unsigned short result)
276 {
277    char temp[100];
278    if(result > 32767)
279       sprintf(temp, "0x%X", (int)result);
280    else
281       sprintf(temp, "%d", (int)result);
282    return CopyString(temp);
283 }
284
285 public char * PrintChar(char result)
286 {
287    char temp[100];
288    if(result > 0 && isprint(result))
289       sprintf(temp, "'%c'", result);
290    else if(result < 0)
291       sprintf(temp, "%d", (int)result);
292    else
293       //sprintf(temp, "%#X", result);
294       sprintf(temp, "0x%X", (unsigned char)result);
295    return CopyString(temp);
296 }
297
298 public char * PrintUChar(unsigned char result)
299 {
300    char temp[100];
301    sprintf(temp, "0x%X", result);
302    return CopyString(temp);
303 }
304
305 public char * PrintFloat(float result)
306 {
307    char temp[350];
308    if(result.isInf)
309    {
310       if(result.signBit)
311          strcpy(temp, "-inf");
312       else
313          strcpy(temp, "inf");
314    }
315    else if(result.isNan)
316    {
317       if(result.signBit)
318          strcpy(temp, "-nan");
319       else
320          strcpy(temp, "nan");
321    }
322    else
323       sprintf(temp, "%.16ff", result);
324    return CopyString(temp);
325 }
326
327 public char * PrintDouble(double result)
328 {
329    char temp[350];
330    if(result.isInf)
331    {
332       if(result.signBit)
333          strcpy(temp, "-inf");
334       else
335          strcpy(temp, "inf");
336    }
337    else if(result.isNan)
338    {
339       if(result.signBit)
340          strcpy(temp, "-nan");
341       else
342          strcpy(temp, "nan");
343    }
344    else
345       sprintf(temp, "%.16f", result);
346    return CopyString(temp);
347 }
348
349 ////////////////////////////////////////////////////////////////////////
350 ////////////////////////////////////////////////////////////////////////
351
352 //public Operand GetOperand(Expression exp);
353
354 #define GETVALUE(name, t) \
355    public bool GetOp##name(Operand op2, t * value2) \
356    {                                                        \
357       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
358       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
359       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
360       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
361       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
362       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
363       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
364       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
365       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
366       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
367       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
368       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
369       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
370       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
371       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
372       else                                                                          \
373          return false;                                                              \
374       return true;                                                                  \
375    } \
376    public bool Get##name(Expression exp, t * value2) \
377    {                                                        \
378       Operand op2 = GetOperand(exp);                        \
379       return GetOp##name(op2, value2); \
380    }
381
382 // To help the deubugger currently not preprocessing...
383 #define HELP(x) x
384
385 GETVALUE(Int, HELP(int));
386 GETVALUE(UInt, HELP(unsigned int));
387 GETVALUE(Int64, HELP(int64));
388 GETVALUE(UInt64, HELP(uint64));
389 GETVALUE(IntPtr, HELP(intptr));
390 GETVALUE(UIntPtr, HELP(uintptr));
391 GETVALUE(IntSize, HELP(intsize));
392 GETVALUE(UIntSize, HELP(uintsize));
393 GETVALUE(Short, HELP(short));
394 GETVALUE(UShort, HELP(unsigned short));
395 GETVALUE(Char, HELP(char));
396 GETVALUE(UChar, HELP(unsigned char));
397 GETVALUE(Float, HELP(float));
398 GETVALUE(Double, HELP(double));
399
400 void ComputeExpression(Expression exp);
401
402 void ComputeClassMembers(Class _class, bool isMember)
403 {
404    DataMember member = isMember ? (DataMember) _class : null;
405    Context context = isMember ? null : SetupTemplatesContext(_class);
406    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
407                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
408    {
409       int c;
410       int unionMemberOffset = 0;
411       int bitFields = 0;
412
413       /*
414       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
415          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
416       */
417
418       if(member)
419       {
420          member.memberOffset = 0;
421          if(targetBits < sizeof(void *) * 8)
422             member.structAlignment = 0;
423       }
424       else if(targetBits < sizeof(void *) * 8)
425          _class.structAlignment = 0;
426
427       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
428       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
429          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
430
431       if(!member && _class.destructionWatchOffset)
432          _class.memberOffset += sizeof(OldList);
433
434       // To avoid reentrancy...
435       //_class.structSize = -1;
436
437       {
438          DataMember dataMember;
439          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
440          {
441             if(!dataMember.isProperty)
442             {
443                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
444                {
445                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
446                   /*if(!dataMember.dataType)
447                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
448                      */
449                }
450             }
451          }
452       }
453
454       {
455          DataMember dataMember;
456          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
457          {
458             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
459             {
460                if(!isMember && _class.type == bitClass && dataMember.dataType)
461                {
462                   BitMember bitMember = (BitMember) dataMember;
463                   uint64 mask = 0;
464                   int d;
465
466                   ComputeTypeSize(dataMember.dataType);
467
468                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
469                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
470
471                   _class.memberOffset = bitMember.pos + bitMember.size;
472                   for(d = 0; d<bitMember.size; d++)
473                   {
474                      if(d)
475                         mask <<= 1;
476                      mask |= 1;
477                   }
478                   bitMember.mask = mask << bitMember.pos;
479                }
480                else if(dataMember.type == normalMember && dataMember.dataType)
481                {
482                   int size;
483                   int alignment = 0;
484
485                   // Prevent infinite recursion
486                   if(dataMember.dataType.kind != classType ||
487                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
488                      _class.type != structClass)))
489                      ComputeTypeSize(dataMember.dataType);
490
491                   if(dataMember.dataType.bitFieldCount)
492                   {
493                      bitFields += dataMember.dataType.bitFieldCount;
494                      size = 0;
495                   }
496                   else
497                   {
498                      if(bitFields)
499                      {
500                         int size = (bitFields + 7) / 8;
501
502                         if(isMember)
503                         {
504                            // TESTING THIS PADDING CODE
505                            if(alignment)
506                            {
507                               member.structAlignment = Max(member.structAlignment, alignment);
508
509                               if(member.memberOffset % alignment)
510                                  member.memberOffset += alignment - (member.memberOffset % alignment);
511                            }
512
513                            dataMember.offset = member.memberOffset;
514                            if(member.type == unionMember)
515                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
516                            else
517                            {
518                               member.memberOffset += size;
519                            }
520                         }
521                         else
522                         {
523                            // TESTING THIS PADDING CODE
524                            if(alignment)
525                            {
526                               _class.structAlignment = Max(_class.structAlignment, alignment);
527
528                               if(_class.memberOffset % alignment)
529                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
530                            }
531
532                            dataMember.offset = _class.memberOffset;
533                            _class.memberOffset += size;
534                         }
535                         bitFields = 0;
536                      }
537                      size = dataMember.dataType.size;
538                      alignment = dataMember.dataType.alignment;
539                   }
540
541                   if(isMember)
542                   {
543                      // TESTING THIS PADDING CODE
544                      if(alignment)
545                      {
546                         member.structAlignment = Max(member.structAlignment, alignment);
547
548                         if(member.memberOffset % alignment)
549                            member.memberOffset += alignment - (member.memberOffset % alignment);
550                      }
551
552                      dataMember.offset = member.memberOffset;
553                      if(member.type == unionMember)
554                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
555                      else
556                      {
557                         member.memberOffset += size;
558                      }
559                   }
560                   else
561                   {
562                      // TESTING THIS PADDING CODE
563                      if(alignment)
564                      {
565                         _class.structAlignment = Max(_class.structAlignment, alignment);
566
567                         if(_class.memberOffset % alignment)
568                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
569                      }
570
571                      dataMember.offset = _class.memberOffset;
572                      _class.memberOffset += size;
573                   }
574                }
575                else
576                {
577                   int alignment;
578
579                   ComputeClassMembers((Class)dataMember, true);
580                   alignment = dataMember.structAlignment;
581
582                   if(isMember)
583                   {
584                      if(alignment)
585                      {
586                         if(member.memberOffset % alignment)
587                            member.memberOffset += alignment - (member.memberOffset % alignment);
588
589                         member.structAlignment = Max(member.structAlignment, alignment);
590                      }
591                      dataMember.offset = member.memberOffset;
592                      if(member.type == unionMember)
593                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
594                      else
595                         member.memberOffset += dataMember.memberOffset;
596                   }
597                   else
598                   {
599                      if(alignment)
600                      {
601                         if(_class.memberOffset % alignment)
602                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
603                         _class.structAlignment = Max(_class.structAlignment, alignment);
604                      }
605                      dataMember.offset = _class.memberOffset;
606                      _class.memberOffset += dataMember.memberOffset;
607                   }
608                }
609             }
610          }
611          if(bitFields)
612          {
613             int alignment = 0;
614             int size = (bitFields + 7) / 8;
615
616             if(isMember)
617             {
618                // TESTING THIS PADDING CODE
619                if(alignment)
620                {
621                   member.structAlignment = Max(member.structAlignment, alignment);
622
623                   if(member.memberOffset % alignment)
624                      member.memberOffset += alignment - (member.memberOffset % alignment);
625                }
626
627                if(member.type == unionMember)
628                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
629                else
630                {
631                   member.memberOffset += size;
632                }
633             }
634             else
635             {
636                // TESTING THIS PADDING CODE
637                if(alignment)
638                {
639                   _class.structAlignment = Max(_class.structAlignment, alignment);
640
641                   if(_class.memberOffset % alignment)
642                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
643                }
644                _class.memberOffset += size;
645             }
646             bitFields = 0;
647          }
648       }
649       if(member && member.type == unionMember)
650       {
651          member.memberOffset = unionMemberOffset;
652       }
653
654       if(!isMember)
655       {
656          /*if(_class.type == structClass)
657             _class.size = _class.memberOffset;
658          else
659          */
660
661          if(_class.type != bitClass)
662          {
663             int extra = 0;
664             if(_class.structAlignment)
665             {
666                if(_class.memberOffset % _class.structAlignment)
667                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
668             }
669             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
670             if(!member)
671             {
672                Property prop;
673                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
674                {
675                   if(prop.isProperty && prop.isWatchable)
676                   {
677                      prop.watcherOffset = _class.structSize;
678                      _class.structSize += sizeof(OldList);
679                   }
680                }
681             }
682
683             // Fix Derivatives
684             {
685                OldLink derivative;
686                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
687                {
688                   Class deriv = derivative.data;
689
690                   if(deriv.computeSize)
691                   {
692                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
693                      deriv.offset = /*_class.offset + */_class.structSize;
694                      deriv.memberOffset = 0;
695                      // ----------------------
696
697                      deriv.structSize = deriv.offset;
698
699                      ComputeClassMembers(deriv, false);
700                   }
701                }
702             }
703          }
704       }
705    }
706    if(context)
707       FinishTemplatesContext(context);
708 }
709
710 public void ComputeModuleClasses(Module module)
711 {
712    Class _class;
713    OldLink subModule;
714
715    for(subModule = module.modules.first; subModule; subModule = subModule.next)
716       ComputeModuleClasses(subModule.data);
717    for(_class = module.classes.first; _class; _class = _class.next)
718       ComputeClassMembers(_class, false);
719 }
720
721
722 public int ComputeTypeSize(Type type)
723 {
724    uint size = type ? type.size : 0;
725    if(!size && type && !type.computing)
726    {
727       type.computing = true;
728       switch(type.kind)
729       {
730          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
731          case charType: type.alignment = size = sizeof(char); break;
732          case intType: type.alignment = size = sizeof(int); break;
733          case int64Type: type.alignment = size = sizeof(int64); break;
734          case intPtrType: type.alignment = size = targetBits / 8; break;
735          case intSizeType: type.alignment = size = targetBits / 8; break;
736          case longType: type.alignment = size = sizeof(long); break;
737          case shortType: type.alignment = size = sizeof(short); break;
738          case floatType: type.alignment = size = sizeof(float); break;
739          case doubleType: type.alignment = size = sizeof(double); break;
740          case classType:
741          {
742             Class _class = type._class ? type._class.registered : null;
743
744             if(_class && _class.type == structClass)
745             {
746                // Ensure all members are properly registered
747                ComputeClassMembers(_class, false);
748                type.alignment = _class.structAlignment;
749                size = _class.structSize;
750                if(type.alignment && size % type.alignment)
751                   size += type.alignment - (size % type.alignment);
752
753             }
754             else if(_class && (_class.type == unitClass ||
755                    _class.type == enumClass ||
756                    _class.type == bitClass))
757             {
758                if(!_class.dataType)
759                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
760                size = type.alignment = ComputeTypeSize(_class.dataType);
761             }
762             else
763                size = type.alignment = targetBits / 8; // sizeof(Instance *);
764             break;
765          }
766          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
767          case arrayType:
768             if(type.arraySizeExp)
769             {
770                ProcessExpressionType(type.arraySizeExp);
771                ComputeExpression(type.arraySizeExp);
772                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
773                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
774                {
775                   Location oldLoc = yylloc;
776                   // bool isConstant = type.arraySizeExp.isConstant;
777                   char expression[10240];
778                   expression[0] = '\0';
779                   type.arraySizeExp.expType = null;
780                   yylloc = type.arraySizeExp.loc;
781                   if(inCompiler)
782                      PrintExpression(type.arraySizeExp, expression);
783                   Compiler_Error($"Array size not constant int (%s)\n", expression);
784                   yylloc = oldLoc;
785                }
786                GetInt(type.arraySizeExp, &type.arraySize);
787             }
788             else if(type.enumClass)
789             {
790                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
791                {
792                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
793                }
794                else
795                   type.arraySize = 0;
796             }
797             else
798             {
799                // Unimplemented auto size
800                type.arraySize = 0;
801             }
802
803             size = ComputeTypeSize(type.type) * type.arraySize;
804             if(type.type)
805                type.alignment = type.type.alignment;
806
807             break;
808          case structType:
809          {
810             Type member;
811             for(member = type.members.first; member; member = member.next)
812             {
813                uint addSize = ComputeTypeSize(member);
814
815                member.offset = size;
816                if(member.alignment && size % member.alignment)
817                   member.offset += member.alignment - (size % member.alignment);
818                size = member.offset;
819
820                type.alignment = Max(type.alignment, member.alignment);
821                size += addSize;
822             }
823             if(type.alignment && size % type.alignment)
824                size += type.alignment - (size % type.alignment);
825             break;
826          }
827          case unionType:
828          {
829             Type member;
830             for(member = type.members.first; member; member = member.next)
831             {
832                uint addSize = ComputeTypeSize(member);
833
834                member.offset = size;
835                if(member.alignment && size % member.alignment)
836                   member.offset += member.alignment - (size % member.alignment);
837                size = member.offset;
838
839                type.alignment = Max(type.alignment, member.alignment);
840                size = Max(size, addSize);
841             }
842             if(type.alignment && size % type.alignment)
843                size += type.alignment - (size % type.alignment);
844             break;
845          }
846          case templateType:
847          {
848             TemplateParameter param = type.templateParameter;
849             Type baseType = ProcessTemplateParameterType(param);
850             if(baseType)
851             {
852                size = ComputeTypeSize(baseType);
853                type.alignment = baseType.alignment;
854             }
855             else
856                type.alignment = size = sizeof(uint64);
857             break;
858          }
859          case enumType:
860          {
861             type.alignment = size = sizeof(enum { test });
862             break;
863          }
864          case thisClassType:
865          {
866             type.alignment = size = targetBits / 8; //sizeof(void *);
867             break;
868          }
869       }
870       type.size = size;
871       type.computing = false;
872    }
873    return size;
874 }
875
876
877 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
878 {
879    // This function is in need of a major review when implementing private members etc.
880    DataMember topMember = isMember ? (DataMember) _class : null;
881    uint totalSize = 0;
882    uint maxSize = 0;
883    int alignment, size;
884    DataMember member;
885    Context context = isMember ? null : SetupTemplatesContext(_class);
886    if(addedPadding)
887       *addedPadding = false;
888
889    if(!isMember && _class.base)
890    {
891       maxSize = _class.structSize;
892       //if(_class.base.type != systemClass) // Commented out with new Instance _class
893       {
894          // DANGER: Testing this noHeadClass here...
895          if(_class.type == structClass || _class.type == noHeadClass)
896             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
897          else
898          {
899             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
900             if(maxSize > baseSize)
901                maxSize -= baseSize;
902             else
903                maxSize = 0;
904          }
905       }
906    }
907
908    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
909    {
910       if(!member.isProperty)
911       {
912          switch(member.type)
913          {
914             case normalMember:
915             {
916                if(member.dataTypeString)
917                {
918                   OldList * specs = MkList(), * decls = MkList();
919                   Declarator decl;
920
921                   decl = SpecDeclFromString(member.dataTypeString, specs,
922                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
923                   ListAdd(decls, MkStructDeclarator(decl, null));
924                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
925
926                   if(!member.dataType)
927                      member.dataType = ProcessType(specs, decl);
928
929                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
930
931                   {
932                      Type type = ProcessType(specs, decl);
933                      DeclareType(member.dataType, false, false);
934                      FreeType(type);
935                   }
936                   /*
937                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
938                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
939                      DeclareStruct(member.dataType._class.string, false);
940                   */
941
942                   ComputeTypeSize(member.dataType);
943                   size = member.dataType.size;
944                   alignment = member.dataType.alignment;
945
946                   if(alignment)
947                   {
948                      if(totalSize % alignment)
949                         totalSize += alignment - (totalSize % alignment);
950                   }
951                   totalSize += size;
952                }
953                break;
954             }
955             case unionMember:
956             case structMember:
957             {
958                OldList * specs = MkList(), * list = MkList();
959
960                size = 0;
961                AddMembers(list, (Class)member, true, &size, topClass, null);
962                ListAdd(specs,
963                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
964                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
965                alignment = member.structAlignment;
966
967                if(alignment)
968                {
969                   if(totalSize % alignment)
970                      totalSize += alignment - (totalSize % alignment);
971                }
972                totalSize += size;
973                break;
974             }
975          }
976       }
977    }
978    if(retSize)
979    {
980       if(topMember && topMember.type == unionMember)
981          *retSize = Max(*retSize, totalSize);
982       else
983          *retSize += totalSize;
984    }
985    else if(totalSize < maxSize && _class.type != systemClass)
986    {
987       int autoPadding = 0;
988       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
989          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
990       if(totalSize + autoPadding < maxSize)
991       {
992          char sizeString[50];
993          sprintf(sizeString, "%d", maxSize - totalSize);
994          ListAdd(declarations,
995             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
996             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
997          if(addedPadding)
998             *addedPadding = true;
999       }
1000    }
1001    if(context)
1002       FinishTemplatesContext(context);
1003    return topMember ? topMember.memberID : _class.memberID;
1004 }
1005
1006 static int DeclareMembers(Class _class, bool isMember)
1007 {
1008    DataMember topMember = isMember ? (DataMember) _class : null;
1009    uint totalSize = 0;
1010    DataMember member;
1011    Context context = isMember ? null : SetupTemplatesContext(_class);
1012
1013    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1014       DeclareMembers(_class.base, false);
1015
1016    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1017    {
1018       if(!member.isProperty)
1019       {
1020          switch(member.type)
1021          {
1022             case normalMember:
1023             {
1024                /*
1025                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1026                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1027                   DeclareStruct(member.dataType._class.string, false);
1028                   */
1029                if(!member.dataType && member.dataTypeString)
1030                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1031                if(member.dataType)
1032                   DeclareType(member.dataType, false, false);
1033                break;
1034             }
1035             case unionMember:
1036             case structMember:
1037             {
1038                DeclareMembers((Class)member, true);
1039                break;
1040             }
1041          }
1042       }
1043    }
1044    if(context)
1045       FinishTemplatesContext(context);
1046
1047    return topMember ? topMember.memberID : _class.memberID;
1048 }
1049
1050 void DeclareStruct(char * name, bool skipNoHead)
1051 {
1052    External external = null;
1053    Symbol classSym = FindClass(name);
1054
1055    if(!inCompiler || !classSym) return;
1056
1057    // We don't need any declaration for bit classes...
1058    if(classSym.registered &&
1059       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1060       return;
1061
1062    /*if(classSym.registered.templateClass)
1063       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1064    */
1065
1066    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1067    {
1068       // Add typedef struct
1069       Declaration decl;
1070       OldList * specifiers, * declarators;
1071       OldList * declarations = null;
1072       char structName[1024];
1073       Specifier spec = null;
1074       external = (classSym.registered && classSym.registered.type == structClass) ?
1075          classSym.pointerExternal : classSym.structExternal;
1076
1077       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1078       // Moved this one up because DeclareClass done later will need it
1079
1080       classSym.declaring++;
1081
1082       if(strchr(classSym.string, '<'))
1083       {
1084          if(classSym.registered.templateClass)
1085          {
1086             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1087             classSym.declaring--;
1088          }
1089          return;
1090       }
1091
1092       //if(!skipNoHead)
1093          DeclareMembers(classSym.registered, false);
1094
1095       structName[0] = 0;
1096       FullClassNameCat(structName, name, false);
1097
1098       if(external && external.declaration && external.declaration.specifiers)
1099       {
1100          for(spec = external.declaration.specifiers->first; spec; spec = spec.next)
1101          {
1102             if(spec.type == structSpecifier || spec.type == unionSpecifier)
1103                break;
1104          }
1105       }
1106
1107       /*if(!external)
1108          external = MkExternalDeclaration(null);*/
1109
1110       if(!skipNoHead && (!spec || !spec.definitions))
1111       {
1112          bool addedPadding = false;
1113          classSym.declaredStructSym = true;
1114
1115          declarations = MkList();
1116
1117          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1118
1119          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1120          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1121
1122          if(!declarations->count || (declarations->count == 1 && addedPadding))
1123          {
1124             FreeList(declarations, FreeClassDef);
1125             declarations = null;
1126          }
1127       }
1128       if(skipNoHead || declarations)
1129       {
1130          if(spec)
1131          {
1132             if(declarations)
1133                spec.definitions = declarations;
1134
1135             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1136             {
1137                // TODO: Fix this
1138                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1139
1140                // DANGER
1141                if(classSym.structExternal)
1142                   ast->Move(classSym.structExternal, curExternal.prev);
1143                ast->Move(classSym.pointerExternal, curExternal.prev);
1144
1145                classSym.id = curExternal.symbol.idCode;
1146                classSym.idCode = curExternal.symbol.idCode;
1147                // external = classSym.pointerExternal;
1148                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1149             }
1150          }
1151          else
1152          {
1153             if(!external)
1154                external = MkExternalDeclaration(null);
1155
1156             specifiers = MkList();
1157             declarators = MkList();
1158             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1159
1160             /*
1161             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1162             ListAdd(declarators, MkInitDeclarator(d, null));
1163             */
1164             external.declaration = decl = MkDeclaration(specifiers, declarators);
1165             if(decl.symbol && !decl.symbol.pointerExternal)
1166                decl.symbol.pointerExternal = external;
1167
1168             // For simple classes, keep the declaration as the external to move around
1169             if(classSym.registered && classSym.registered.type == structClass)
1170             {
1171                char className[1024];
1172                strcpy(className, "__ecereClass_");
1173                FullClassNameCat(className, classSym.string, true);
1174                MangleClassName(className);
1175
1176                // Testing This
1177                DeclareClass(classSym, className);
1178
1179                external.symbol = classSym;
1180                classSym.pointerExternal = external;
1181                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1182                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1183             }
1184             else
1185             {
1186                char className[1024];
1187                strcpy(className, "__ecereClass_");
1188                FullClassNameCat(className, classSym.string, true);
1189                MangleClassName(className);
1190
1191                // TOFIX: TESTING THIS...
1192                classSym.structExternal = external;
1193                DeclareClass(classSym, className);
1194                external.symbol = classSym;
1195             }
1196
1197             //if(curExternal)
1198                ast->Insert(curExternal ? curExternal.prev : null, external);
1199          }
1200       }
1201
1202       classSym.declaring--;
1203    }
1204    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1205    {
1206       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1207       // Moved this one up because DeclareClass done later will need it
1208
1209       // TESTING THIS:
1210       classSym.declaring++;
1211
1212       //if(!skipNoHead)
1213       {
1214          if(classSym.registered)
1215             DeclareMembers(classSym.registered, false);
1216       }
1217
1218       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1219       {
1220          // TODO: Fix this
1221          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1222
1223          // DANGER
1224          if(classSym.structExternal)
1225             ast->Move(classSym.structExternal, curExternal.prev);
1226          ast->Move(classSym.pointerExternal, curExternal.prev);
1227
1228          classSym.id = curExternal.symbol.idCode;
1229          classSym.idCode = curExternal.symbol.idCode;
1230          // external = classSym.pointerExternal;
1231          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1232       }
1233
1234       classSym.declaring--;
1235    }
1236    //return external;
1237 }
1238
1239 void DeclareProperty(Property prop, char * setName, char * getName)
1240 {
1241    Symbol symbol = prop.symbol;
1242    char propName[1024];
1243
1244    strcpy(setName, "__ecereProp_");
1245    FullClassNameCat(setName, prop._class.fullName, false);
1246    strcat(setName, "_Set_");
1247    // strcat(setName, prop.name);
1248    FullClassNameCat(setName, prop.name, true);
1249
1250    strcpy(getName, "__ecereProp_");
1251    FullClassNameCat(getName, prop._class.fullName, false);
1252    strcat(getName, "_Get_");
1253    FullClassNameCat(getName, prop.name, true);
1254    // strcat(getName, prop.name);
1255
1256    strcpy(propName, "__ecereProp_");
1257    FullClassNameCat(propName, prop._class.fullName, false);
1258    strcat(propName, "_");
1259    FullClassNameCat(propName, prop.name, true);
1260    // strcat(propName, prop.name);
1261
1262    // To support "char *" property
1263    MangleClassName(getName);
1264    MangleClassName(setName);
1265    MangleClassName(propName);
1266
1267    if(prop._class.type == structClass)
1268       DeclareStruct(prop._class.fullName, false);
1269
1270    if(!symbol || curExternal.symbol.idCode < symbol.id)
1271    {
1272       bool imported = false;
1273       bool dllImport = false;
1274       if(!symbol || symbol._import)
1275       {
1276          if(!symbol)
1277          {
1278             Symbol classSym;
1279             if(!prop._class.symbol)
1280                prop._class.symbol = FindClass(prop._class.fullName);
1281             classSym = prop._class.symbol;
1282             if(classSym && !classSym._import)
1283             {
1284                ModuleImport module;
1285
1286                if(prop._class.module)
1287                   module = FindModule(prop._class.module);
1288                else
1289                   module = mainModule;
1290
1291                classSym._import = ClassImport
1292                {
1293                   name = CopyString(prop._class.fullName);
1294                   isRemote = prop._class.isRemote;
1295                };
1296                module.classes.Add(classSym._import);
1297             }
1298             symbol = prop.symbol = Symbol { };
1299             symbol._import = (ClassImport)PropertyImport
1300             {
1301                name = CopyString(prop.name);
1302                isVirtual = false; //prop.isVirtual;
1303                hasSet = prop.Set ? true : false;
1304                hasGet = prop.Get ? true : false;
1305             };
1306             if(classSym)
1307                classSym._import.properties.Add(symbol._import);
1308          }
1309          imported = true;
1310          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1311          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1312             prop._class.module.importType != staticImport)
1313             dllImport = true;
1314       }
1315
1316       if(!symbol.type)
1317       {
1318          Context context = SetupTemplatesContext(prop._class);
1319          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1320          FinishTemplatesContext(context);
1321       }
1322
1323       // Get
1324       if(prop.Get)
1325       {
1326          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1327          {
1328             Declaration decl;
1329             OldList * specifiers, * declarators;
1330             Declarator d;
1331             OldList * params;
1332             Specifier spec;
1333             External external;
1334             Declarator typeDecl;
1335             bool simple = false;
1336
1337             specifiers = MkList();
1338             declarators = MkList();
1339             params = MkList();
1340
1341             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1342                MkDeclaratorIdentifier(MkIdentifier("this"))));
1343
1344             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1345             //if(imported)
1346             if(dllImport)
1347                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1348
1349             {
1350                Context context = SetupTemplatesContext(prop._class);
1351                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1352                FinishTemplatesContext(context);
1353             }
1354
1355             // Make sure the simple _class's type is declared
1356             for(spec = specifiers->first; spec; spec = spec.next)
1357             {
1358                if(spec.type == nameSpecifier /*SpecifierClass*/)
1359                {
1360                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1361                   {
1362                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1363                      symbol._class = classSym.registered;
1364                      if(classSym.registered && classSym.registered.type == structClass)
1365                      {
1366                         DeclareStruct(spec.name, false);
1367                         simple = true;
1368                      }
1369                   }
1370                }
1371             }
1372
1373             if(!simple)
1374                d = PlugDeclarator(typeDecl, d);
1375             else
1376             {
1377                ListAdd(params, MkTypeName(specifiers,
1378                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1379                specifiers = MkList();
1380             }
1381
1382             d = MkDeclaratorFunction(d, params);
1383
1384             //if(imported)
1385             if(dllImport)
1386                specifiers->Insert(null, MkSpecifier(EXTERN));
1387             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1388                specifiers->Insert(null, MkSpecifier(STATIC));
1389             if(simple)
1390                ListAdd(specifiers, MkSpecifier(VOID));
1391
1392             ListAdd(declarators, MkInitDeclarator(d, null));
1393
1394             decl = MkDeclaration(specifiers, declarators);
1395
1396             external = MkExternalDeclaration(decl);
1397             ast->Insert(curExternal.prev, external);
1398             external.symbol = symbol;
1399             symbol.externalGet = external;
1400
1401             ReplaceThisClassSpecifiers(specifiers, prop._class);
1402
1403             if(typeDecl)
1404                FreeDeclarator(typeDecl);
1405          }
1406          else
1407          {
1408             // Move declaration higher...
1409             ast->Move(symbol.externalGet, curExternal.prev);
1410          }
1411       }
1412
1413       // Set
1414       if(prop.Set)
1415       {
1416          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1417          {
1418             Declaration decl;
1419             OldList * specifiers, * declarators;
1420             Declarator d;
1421             OldList * params;
1422             Specifier spec;
1423             External external;
1424             Declarator typeDecl;
1425
1426             declarators = MkList();
1427             params = MkList();
1428
1429             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1430             if(!prop.conversion || prop._class.type == structClass)
1431             {
1432                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1433                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1434             }
1435
1436             specifiers = MkList();
1437
1438             {
1439                Context context = SetupTemplatesContext(prop._class);
1440                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1441                   MkDeclaratorIdentifier(MkIdentifier("value")));
1442                FinishTemplatesContext(context);
1443             }
1444             ListAdd(params, MkTypeName(specifiers, d));
1445
1446             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1447             //if(imported)
1448             if(dllImport)
1449                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1450             d = MkDeclaratorFunction(d, params);
1451
1452             // Make sure the simple _class's type is declared
1453             for(spec = specifiers->first; spec; spec = spec.next)
1454             {
1455                if(spec.type == nameSpecifier /*SpecifierClass*/)
1456                {
1457                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1458                   {
1459                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1460                      symbol._class = classSym.registered;
1461                      if(classSym.registered && classSym.registered.type == structClass)
1462                         DeclareStruct(spec.name, false);
1463                   }
1464                }
1465             }
1466
1467             ListAdd(declarators, MkInitDeclarator(d, null));
1468
1469             specifiers = MkList();
1470             //if(imported)
1471             if(dllImport)
1472                specifiers->Insert(null, MkSpecifier(EXTERN));
1473             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1474                specifiers->Insert(null, MkSpecifier(STATIC));
1475
1476             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1477             if(!prop.conversion || prop._class.type == structClass)
1478                ListAdd(specifiers, MkSpecifier(VOID));
1479             else
1480                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1481
1482             decl = MkDeclaration(specifiers, declarators);
1483
1484             external = MkExternalDeclaration(decl);
1485             ast->Insert(curExternal.prev, external);
1486             external.symbol = symbol;
1487             symbol.externalSet = external;
1488
1489             ReplaceThisClassSpecifiers(specifiers, prop._class);
1490          }
1491          else
1492          {
1493             // Move declaration higher...
1494             ast->Move(symbol.externalSet, curExternal.prev);
1495          }
1496       }
1497
1498       // Property (for Watchers)
1499       if(!symbol.externalPtr)
1500       {
1501          Declaration decl;
1502          External external;
1503          OldList * specifiers = MkList();
1504
1505          if(imported)
1506             specifiers->Insert(null, MkSpecifier(EXTERN));
1507          else
1508             specifiers->Insert(null, MkSpecifier(STATIC));
1509
1510          ListAdd(specifiers, MkSpecifierName("Property"));
1511
1512          {
1513             OldList * list = MkList();
1514             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1515                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1516
1517             if(!imported)
1518             {
1519                strcpy(propName, "__ecerePropM_");
1520                FullClassNameCat(propName, prop._class.fullName, false);
1521                strcat(propName, "_");
1522                // strcat(propName, prop.name);
1523                FullClassNameCat(propName, prop.name, true);
1524
1525                MangleClassName(propName);
1526
1527                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1528                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1529             }
1530             decl = MkDeclaration(specifiers, list);
1531          }
1532
1533          external = MkExternalDeclaration(decl);
1534          ast->Insert(curExternal.prev, external);
1535          external.symbol = symbol;
1536          symbol.externalPtr = external;
1537       }
1538       else
1539       {
1540          // Move declaration higher...
1541          ast->Move(symbol.externalPtr, curExternal.prev);
1542       }
1543
1544       symbol.id = curExternal.symbol.idCode;
1545    }
1546 }
1547
1548 // ***************** EXPRESSION PROCESSING ***************************
1549 public Type Dereference(Type source)
1550 {
1551    Type type = null;
1552    if(source)
1553    {
1554       if(source.kind == pointerType || source.kind == arrayType)
1555       {
1556          type = source.type;
1557          source.type.refCount++;
1558       }
1559       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1560       {
1561          type = Type
1562          {
1563             kind = charType;
1564             refCount = 1;
1565          };
1566       }
1567       // Support dereferencing of no head classes for now...
1568       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1569       {
1570          type = source;
1571          source.refCount++;
1572       }
1573       else
1574          Compiler_Error($"cannot dereference type\n");
1575    }
1576    return type;
1577 }
1578
1579 static Type Reference(Type source)
1580 {
1581    Type type = null;
1582    if(source)
1583    {
1584       type = Type
1585       {
1586          kind = pointerType;
1587          type = source;
1588          refCount = 1;
1589       };
1590       source.refCount++;
1591    }
1592    return type;
1593 }
1594
1595 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1596 {
1597    Identifier ident = member.identifiers ? member.identifiers->first : null;
1598    bool found = false;
1599    DataMember dataMember = null;
1600    Method method = null;
1601    bool freeType = false;
1602
1603    yylloc = member.loc;
1604
1605    if(!ident)
1606    {
1607       if(curMember)
1608       {
1609          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1610          if(*curMember)
1611          {
1612             found = true;
1613             dataMember = *curMember;
1614          }
1615       }
1616    }
1617    else
1618    {
1619       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1620       DataMember _subMemberStack[256];
1621       int _subMemberStackPos = 0;
1622
1623       // FILL MEMBER STACK
1624       if(!thisMember)
1625          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1626       if(thisMember)
1627       {
1628          dataMember = thisMember;
1629          if(curMember && thisMember.memberAccess == publicAccess)
1630          {
1631             *curMember = thisMember;
1632             *curClass = thisMember._class;
1633             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1634             *subMemberStackPos = _subMemberStackPos;
1635          }
1636          found = true;
1637       }
1638       else
1639       {
1640          // Setting a method
1641          method = eClass_FindMethod(_class, ident.string, privateModule);
1642          if(method && method.type == virtualMethod)
1643             found = true;
1644          else
1645             method = null;
1646       }
1647    }
1648
1649    if(found)
1650    {
1651       Type type = null;
1652       if(dataMember)
1653       {
1654          if(!dataMember.dataType && dataMember.dataTypeString)
1655          {
1656             //Context context = SetupTemplatesContext(dataMember._class);
1657             Context context = SetupTemplatesContext(_class);
1658             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1659             FinishTemplatesContext(context);
1660          }
1661          type = dataMember.dataType;
1662       }
1663       else if(method)
1664       {
1665          // This is for destination type...
1666          if(!method.dataType)
1667             ProcessMethodType(method);
1668          //DeclareMethod(method);
1669          // method.dataType = ((Symbol)method.symbol)->type;
1670          type = method.dataType;
1671       }
1672
1673       if(ident && ident.next)
1674       {
1675          for(ident = ident.next; ident && type; ident = ident.next)
1676          {
1677             if(type.kind == classType)
1678             {
1679                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1680                if(!dataMember)
1681                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1682                if(dataMember)
1683                   type = dataMember.dataType;
1684             }
1685             else if(type.kind == structType || type.kind == unionType)
1686             {
1687                Type memberType;
1688                for(memberType = type.members.first; memberType; memberType = memberType.next)
1689                {
1690                   if(!strcmp(memberType.name, ident.string))
1691                   {
1692                      type = memberType;
1693                      break;
1694                   }
1695                }
1696             }
1697          }
1698       }
1699
1700       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1701       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1702       {
1703          int id = 0;
1704          ClassTemplateParameter curParam = null;
1705          Class sClass;
1706          for(sClass = _class; sClass; sClass = sClass.base)
1707          {
1708             id = 0;
1709             if(sClass.templateClass) sClass = sClass.templateClass;
1710             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1711             {
1712                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1713                {
1714                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1715                   {
1716                      if(sClass.templateClass) sClass = sClass.templateClass;
1717                      id += sClass.templateParams.count;
1718                   }
1719                   break;
1720                }
1721                id++;
1722             }
1723             if(curParam) break;
1724          }
1725
1726          if(curParam)
1727          {
1728             ClassTemplateArgument arg = _class.templateArgs[id];
1729             if(arg.dataTypeString)
1730             {
1731                // FreeType(type);
1732                type = ProcessTypeString(arg.dataTypeString, false);
1733                freeType = true;
1734                if(type && _class.templateClass)
1735                   type.passAsTemplate = true;
1736                if(type)
1737                {
1738                   // type.refCount++;
1739                   /*if(!exp.destType)
1740                   {
1741                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1742                      exp.destType.refCount++;
1743                   }*/
1744                }
1745             }
1746          }
1747       }
1748       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1749       {
1750          Class expClass = type._class.registered;
1751          Class cClass = null;
1752          int c;
1753          int paramCount = 0;
1754          int lastParam = -1;
1755
1756          char templateString[1024];
1757          ClassTemplateParameter param;
1758          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1759          for(cClass = expClass; cClass; cClass = cClass.base)
1760          {
1761             int p = 0;
1762             if(cClass.templateClass) cClass = cClass.templateClass;
1763             for(param = cClass.templateParams.first; param; param = param.next)
1764             {
1765                int id = p;
1766                Class sClass;
1767                ClassTemplateArgument arg;
1768                for(sClass = cClass.base; sClass; sClass = sClass.base)
1769                {
1770                   if(sClass.templateClass) sClass = sClass.templateClass;
1771                   id += sClass.templateParams.count;
1772                }
1773                arg = expClass.templateArgs[id];
1774
1775                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1776                {
1777                   ClassTemplateParameter cParam;
1778                   //int p = numParams - sClass.templateParams.count;
1779                   int p = 0;
1780                   Class nextClass;
1781                   if(sClass.templateClass) sClass = sClass.templateClass;
1782
1783                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1784                   {
1785                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1786                      p += nextClass.templateParams.count;
1787                   }
1788
1789                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1790                   {
1791                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1792                      {
1793                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1794                         {
1795                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1796                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1797                            break;
1798                         }
1799                      }
1800                   }
1801                }
1802
1803                {
1804                   char argument[256];
1805                   argument[0] = '\0';
1806                   /*if(arg.name)
1807                   {
1808                      strcat(argument, arg.name.string);
1809                      strcat(argument, " = ");
1810                   }*/
1811                   switch(param.type)
1812                   {
1813                      case expression:
1814                      {
1815                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1816                         char expString[1024];
1817                         OldList * specs = MkList();
1818                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1819                         Expression exp;
1820                         char * string = PrintHexUInt64(arg.expression.ui64);
1821                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1822                         delete string;
1823
1824                         ProcessExpressionType(exp);
1825                         ComputeExpression(exp);
1826                         expString[0] = '\0';
1827                         PrintExpression(exp, expString);
1828                         strcat(argument, expString);
1829                         //delete exp;
1830                         FreeExpression(exp);
1831                         break;
1832                      }
1833                      case identifier:
1834                      {
1835                         strcat(argument, arg.member.name);
1836                         break;
1837                      }
1838                      case TemplateParameterType::type:
1839                      {
1840                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1841                            strcat(argument, arg.dataTypeString);
1842                         break;
1843                      }
1844                   }
1845                   if(argument[0])
1846                   {
1847                      if(paramCount) strcat(templateString, ", ");
1848                      if(lastParam != p - 1)
1849                      {
1850                         strcat(templateString, param.name);
1851                         strcat(templateString, " = ");
1852                      }
1853                      strcat(templateString, argument);
1854                      paramCount++;
1855                      lastParam = p;
1856                   }
1857                   p++;
1858                }
1859             }
1860          }
1861          {
1862             int len = strlen(templateString);
1863             if(templateString[len-1] == '<')
1864                len--;
1865             else
1866             {
1867                if(templateString[len-1] == '>')
1868                   templateString[len++] = ' ';
1869                templateString[len++] = '>';
1870             }
1871             templateString[len++] = '\0';
1872          }
1873          {
1874             Context context = SetupTemplatesContext(_class);
1875             if(freeType) FreeType(type);
1876             type = ProcessTypeString(templateString, false);
1877             freeType = true;
1878             FinishTemplatesContext(context);
1879          }
1880       }
1881
1882       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1883       {
1884          ProcessExpressionType(member.initializer.exp);
1885          if(!member.initializer.exp.expType)
1886          {
1887             if(inCompiler)
1888             {
1889                char expString[10240];
1890                expString[0] = '\0';
1891                PrintExpression(member.initializer.exp, expString);
1892                ChangeCh(expString, '\n', ' ');
1893                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1894             }
1895          }
1896          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1897          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1898          {
1899             Compiler_Error($"incompatible instance method %s\n", ident.string);
1900          }
1901       }
1902       else if(member.initializer)
1903       {
1904          /*
1905          FreeType(member.exp.destType);
1906          member.exp.destType = type;
1907          if(member.exp.destType)
1908             member.exp.destType.refCount++;
1909          ProcessExpressionType(member.exp);
1910          */
1911
1912          ProcessInitializer(member.initializer, type);
1913       }
1914       if(freeType) FreeType(type);
1915    }
1916    else
1917    {
1918       if(_class && _class.type == unitClass)
1919       {
1920          if(member.initializer)
1921          {
1922             /*
1923             FreeType(member.exp.destType);
1924             member.exp.destType = MkClassType(_class.fullName);
1925             ProcessExpressionType(member.initializer, type);
1926             */
1927             Type type = MkClassType(_class.fullName);
1928             ProcessInitializer(member.initializer, type);
1929             FreeType(type);
1930          }
1931       }
1932       else
1933       {
1934          if(member.initializer)
1935          {
1936             //ProcessExpressionType(member.exp);
1937             ProcessInitializer(member.initializer, null);
1938          }
1939          if(ident)
1940          {
1941             if(method)
1942             {
1943                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1944             }
1945             else if(_class)
1946             {
1947                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1948                if(inCompiler)
1949                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1950             }
1951          }
1952          else if(_class)
1953             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1954       }
1955    }
1956 }
1957
1958 void ProcessInstantiationType(Instantiation inst)
1959 {
1960    yylloc = inst.loc;
1961    if(inst._class)
1962    {
1963       MembersInit members;
1964       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1965       Class _class;
1966
1967       /*if(!inst._class.symbol)
1968          inst._class.symbol = FindClass(inst._class.name);*/
1969       classSym = inst._class.symbol;
1970       _class = classSym ? classSym.registered : null;
1971
1972       // DANGER: Patch for mutex not declaring its struct when not needed
1973       if(!_class || _class.type != noHeadClass)
1974          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1975
1976       afterExternal = afterExternal ? afterExternal : curExternal;
1977
1978       if(inst.exp)
1979          ProcessExpressionType(inst.exp);
1980
1981       inst.isConstant = true;
1982       if(inst.members)
1983       {
1984          DataMember curMember = null;
1985          Class curClass = null;
1986          DataMember subMemberStack[256];
1987          int subMemberStackPos = 0;
1988
1989          for(members = inst.members->first; members; members = members.next)
1990          {
1991             switch(members.type)
1992             {
1993                case methodMembersInit:
1994                {
1995                   char name[1024];
1996                   static uint instMethodID = 0;
1997                   External external = curExternal;
1998                   Context context = curContext;
1999                   Declarator declarator = members.function.declarator;
2000                   Identifier nameID = GetDeclId(declarator);
2001                   char * unmangled = nameID ? nameID.string : null;
2002                   Expression exp;
2003                   External createdExternal = null;
2004
2005                   if(inCompiler)
2006                   {
2007                      char number[16];
2008                      //members.function.dontMangle = true;
2009                      strcpy(name, "__ecereInstMeth_");
2010                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2011                      strcat(name, "_");
2012                      strcat(name, nameID.string);
2013                      strcat(name, "_");
2014                      sprintf(number, "_%08d", instMethodID++);
2015                      strcat(name, number);
2016                      nameID.string = CopyString(name);
2017                   }
2018
2019                   // Do modifications here...
2020                   if(declarator)
2021                   {
2022                      Symbol symbol = declarator.symbol;
2023                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2024
2025                      if(method && method.type == virtualMethod)
2026                      {
2027                         symbol.method = method;
2028                         ProcessMethodType(method);
2029
2030                         if(!symbol.type.thisClass)
2031                         {
2032                            if(method.dataType.thisClass && currentClass &&
2033                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2034                            {
2035                               if(!currentClass.symbol)
2036                                  currentClass.symbol = FindClass(currentClass.fullName);
2037                               symbol.type.thisClass = currentClass.symbol;
2038                            }
2039                            else
2040                            {
2041                               if(!_class.symbol)
2042                                  _class.symbol = FindClass(_class.fullName);
2043                               symbol.type.thisClass = _class.symbol;
2044                            }
2045                         }
2046                         // TESTING THIS HERE:
2047                         DeclareType(symbol.type, true, true);
2048
2049                      }
2050                      else if(classSym)
2051                      {
2052                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2053                            unmangled, classSym.string);
2054                      }
2055                   }
2056
2057                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2058                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2059
2060                   if(nameID)
2061                   {
2062                      FreeSpecifier(nameID._class);
2063                      nameID._class = null;
2064                   }
2065
2066                   if(inCompiler)
2067                   {
2068
2069                      Type type = declarator.symbol.type;
2070                      External oldExternal = curExternal;
2071
2072                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2073                      // *** It was commented out for problems such as
2074                      /*
2075                            class VirtualDesktop : Window
2076                            {
2077                               clientSize = Size { };
2078                               Timer timer
2079                               {
2080                                  bool DelayExpired()
2081                                  {
2082                                     clientSize.w;
2083                                     return true;
2084                                  }
2085                               };
2086                            }
2087                      */
2088                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2089
2090                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2091
2092                      /*
2093                      if(strcmp(declarator.symbol.string, name))
2094                      {
2095                         printf("TOCHECK: Look out for this\n");
2096                         delete declarator.symbol.string;
2097                         declarator.symbol.string = CopyString(name);
2098                      }
2099
2100                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2101                      {
2102                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2103                         excludedSymbols->Remove(declarator.symbol);
2104                         globalContext.symbols.Add((BTNode)declarator.symbol);
2105                         if(strstr(declarator.symbol.string), "::")
2106                            globalContext.hasNameSpace = true;
2107
2108                      }
2109                      */
2110
2111                      //curExternal = curExternal.prev;
2112                      //afterExternal = afterExternal->next;
2113
2114                      //ProcessFunction(afterExternal->function);
2115
2116                      //curExternal = afterExternal;
2117                      {
2118                         External externalDecl;
2119                         externalDecl = MkExternalDeclaration(null);
2120                         ast->Insert(oldExternal.prev, externalDecl);
2121
2122                         // Which function does this process?
2123                         if(createdExternal.function)
2124                         {
2125                            ProcessFunction(createdExternal.function);
2126
2127                            //curExternal = oldExternal;
2128
2129                            {
2130                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2131
2132                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2133                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2134
2135                               //externalDecl = MkExternalDeclaration(decl);
2136
2137                               //***** ast->Insert(external.prev, externalDecl);
2138                               //ast->Insert(curExternal.prev, externalDecl);
2139                               externalDecl.declaration = decl;
2140                               if(decl.symbol && !decl.symbol.pointerExternal)
2141                                  decl.symbol.pointerExternal = externalDecl;
2142
2143                               // Trying this out...
2144                               declarator.symbol.pointerExternal = externalDecl;
2145                            }
2146                         }
2147                      }
2148                   }
2149                   else if(declarator)
2150                   {
2151                      curExternal = declarator.symbol.pointerExternal;
2152                      ProcessFunction((FunctionDefinition)members.function);
2153                   }
2154                   curExternal = external;
2155                   curContext = context;
2156
2157                   if(inCompiler)
2158                   {
2159                      FreeClassFunction(members.function);
2160
2161                      // In this pass, turn this into a MemberInitData
2162                      exp = QMkExpId(name);
2163                      members.type = dataMembersInit;
2164                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2165
2166                      delete unmangled;
2167                   }
2168                   break;
2169                }
2170                case dataMembersInit:
2171                {
2172                   if(members.dataMembers && classSym)
2173                   {
2174                      MemberInit member;
2175                      Location oldyyloc = yylloc;
2176                      for(member = members.dataMembers->first; member; member = member.next)
2177                      {
2178                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2179                         if(member.initializer && !member.initializer.isConstant)
2180                            inst.isConstant = false;
2181                      }
2182                      yylloc = oldyyloc;
2183                   }
2184                   break;
2185                }
2186             }
2187          }
2188       }
2189    }
2190 }
2191
2192 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2193 {
2194    // OPTIMIZATIONS: TESTING THIS...
2195    if(inCompiler)
2196    {
2197       if(type.kind == functionType)
2198       {
2199          Type param;
2200          if(declareParams)
2201          {
2202             for(param = type.params.first; param; param = param.next)
2203                DeclareType(param, declarePointers, true);
2204          }
2205          DeclareType(type.returnType, declarePointers, true);
2206       }
2207       else if(type.kind == pointerType && declarePointers)
2208          DeclareType(type.type, declarePointers, false);
2209       else if(type.kind == classType)
2210       {
2211          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2212             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2213       }
2214       else if(type.kind == structType || type.kind == unionType)
2215       {
2216          Type member;
2217          for(member = type.members.first; member; member = member.next)
2218             DeclareType(member, false, false);
2219       }
2220       else if(type.kind == arrayType)
2221          DeclareType(type.arrayType, declarePointers, false);
2222    }
2223 }
2224
2225 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2226 {
2227    ClassTemplateArgument * arg = null;
2228    int id = 0;
2229    ClassTemplateParameter curParam = null;
2230    Class sClass;
2231    for(sClass = _class; sClass; sClass = sClass.base)
2232    {
2233       id = 0;
2234       if(sClass.templateClass) sClass = sClass.templateClass;
2235       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2236       {
2237          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2238          {
2239             for(sClass = sClass.base; sClass; sClass = sClass.base)
2240             {
2241                if(sClass.templateClass) sClass = sClass.templateClass;
2242                id += sClass.templateParams.count;
2243             }
2244             break;
2245          }
2246          id++;
2247       }
2248       if(curParam) break;
2249    }
2250    if(curParam)
2251    {
2252       arg = &_class.templateArgs[id];
2253       if(arg && param.type == type)
2254          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2255    }
2256    return arg;
2257 }
2258
2259 public Context SetupTemplatesContext(Class _class)
2260 {
2261    Context context = PushContext();
2262    context.templateTypesOnly = true;
2263    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2264    {
2265       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2266       for(; param; param = param.next)
2267       {
2268          if(param.type == type && param.identifier)
2269          {
2270             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2271             curContext.templateTypes.Add((BTNode)type);
2272          }
2273       }
2274    }
2275    else if(_class)
2276    {
2277       Class sClass;
2278       for(sClass = _class; sClass; sClass = sClass.base)
2279       {
2280          ClassTemplateParameter p;
2281          for(p = sClass.templateParams.first; p; p = p.next)
2282          {
2283             //OldList * specs = MkList();
2284             //Declarator decl = null;
2285             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2286             if(p.type == type)
2287             {
2288                TemplateParameter param = p.param;
2289                TemplatedType type;
2290                if(!param)
2291                {
2292                   // ADD DATA TYPE HERE...
2293                   p.param = param = TemplateParameter
2294                   {
2295                      identifier = MkIdentifier(p.name), type = p.type,
2296                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2297                   };
2298                }
2299                type = TemplatedType { key = (uintptr)p.name, param = param };
2300                curContext.templateTypes.Add((BTNode)type);
2301             }
2302          }
2303       }
2304    }
2305    return context;
2306 }
2307
2308 public void FinishTemplatesContext(Context context)
2309 {
2310    PopContext(context);
2311    FreeContext(context);
2312    delete context;
2313 }
2314
2315 public void ProcessMethodType(Method method)
2316 {
2317    if(!method.dataType)
2318    {
2319       Context context = SetupTemplatesContext(method._class);
2320
2321       method.dataType = ProcessTypeString(method.dataTypeString, false);
2322
2323       FinishTemplatesContext(context);
2324
2325       if(method.type != virtualMethod && method.dataType)
2326       {
2327          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2328          {
2329             if(!method._class.symbol)
2330                method._class.symbol = FindClass(method._class.fullName);
2331             method.dataType.thisClass = method._class.symbol;
2332          }
2333       }
2334
2335       // Why was this commented out? Working fine without now...
2336
2337       /*
2338       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2339          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2340          */
2341    }
2342
2343    /*
2344    if(type)
2345    {
2346       char * par = strstr(type, "(");
2347       char * classOp = null;
2348       int classOpLen = 0;
2349       if(par)
2350       {
2351          int c;
2352          for(c = par-type-1; c >= 0; c++)
2353          {
2354             if(type[c] == ':' && type[c+1] == ':')
2355             {
2356                classOp = type + c - 1;
2357                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2358                {
2359                   classOp--;
2360                   classOpLen++;
2361                }
2362                break;
2363             }
2364             else if(!isspace(type[c]))
2365                break;
2366          }
2367       }
2368       if(classOp)
2369       {
2370          char temp[1024];
2371          int typeLen = strlen(type);
2372          memcpy(temp, classOp, classOpLen);
2373          temp[classOpLen] = '\0';
2374          if(temp[0])
2375             _class = eSystem_FindClass(module, temp);
2376          else
2377             _class = null;
2378          method.dataTypeString = new char[typeLen - classOpLen + 1];
2379          memcpy(method.dataTypeString, type, classOp - type);
2380          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2381       }
2382       else
2383          method.dataTypeString = type;
2384    }
2385    */
2386 }
2387
2388
2389 public void ProcessPropertyType(Property prop)
2390 {
2391    if(!prop.dataType)
2392    {
2393       Context context = SetupTemplatesContext(prop._class);
2394       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2395       FinishTemplatesContext(context);
2396    }
2397 }
2398
2399 public void DeclareMethod(Method method, char * name)
2400 {
2401    Symbol symbol = method.symbol;
2402    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2403    {
2404       bool imported = false;
2405       bool dllImport = false;
2406
2407       if(!method.dataType)
2408          method.dataType = ProcessTypeString(method.dataTypeString, false);
2409
2410       if(!symbol || symbol._import || method.type == virtualMethod)
2411       {
2412          if(!symbol || method.type == virtualMethod)
2413          {
2414             Symbol classSym;
2415             if(!method._class.symbol)
2416                method._class.symbol = FindClass(method._class.fullName);
2417             classSym = method._class.symbol;
2418             if(!classSym._import)
2419             {
2420                ModuleImport module;
2421
2422                if(method._class.module && method._class.module.name)
2423                   module = FindModule(method._class.module);
2424                else
2425                   module = mainModule;
2426                classSym._import = ClassImport
2427                {
2428                   name = CopyString(method._class.fullName);
2429                   isRemote = method._class.isRemote;
2430                };
2431                module.classes.Add(classSym._import);
2432             }
2433             if(!symbol)
2434             {
2435                symbol = method.symbol = Symbol { };
2436             }
2437             if(!symbol._import)
2438             {
2439                symbol._import = (ClassImport)MethodImport
2440                {
2441                   name = CopyString(method.name);
2442                   isVirtual = method.type == virtualMethod;
2443                };
2444                classSym._import.methods.Add(symbol._import);
2445             }
2446             if(!symbol)
2447             {
2448                // Set the symbol type
2449                /*
2450                if(!type.thisClass)
2451                {
2452                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2453                }
2454                else if(type.thisClass == (void *)-1)
2455                {
2456                   type.thisClass = null;
2457                }
2458                */
2459                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2460                symbol.type = method.dataType;
2461                if(symbol.type) symbol.type.refCount++;
2462             }
2463             /*
2464             if(!method.thisClass || strcmp(method.thisClass, "void"))
2465                symbol.type.params.Insert(null,
2466                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2467             */
2468          }
2469          if(!method.dataType.dllExport)
2470          {
2471             imported = true;
2472             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2473                dllImport = true;
2474          }
2475       }
2476
2477       /* MOVING THIS UP
2478       if(!method.dataType)
2479          method.dataType = ((Symbol)method.symbol).type;
2480          //ProcessMethodType(method);
2481       */
2482
2483       if(method.type != virtualMethod && method.dataType)
2484          DeclareType(method.dataType, true, true);
2485
2486       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2487       {
2488          // We need a declaration here :)
2489          Declaration decl;
2490          OldList * specifiers, * declarators;
2491          Declarator d;
2492          Declarator funcDecl;
2493          External external;
2494
2495          specifiers = MkList();
2496          declarators = MkList();
2497
2498          //if(imported)
2499          if(dllImport)
2500             ListAdd(specifiers, MkSpecifier(EXTERN));
2501          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2502             ListAdd(specifiers, MkSpecifier(STATIC));
2503
2504          if(method.type == virtualMethod)
2505          {
2506             ListAdd(specifiers, MkSpecifier(INT));
2507             d = MkDeclaratorIdentifier(MkIdentifier(name));
2508          }
2509          else
2510          {
2511             d = MkDeclaratorIdentifier(MkIdentifier(name));
2512             //if(imported)
2513             if(dllImport)
2514                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2515             {
2516                Context context = SetupTemplatesContext(method._class);
2517                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2518                FinishTemplatesContext(context);
2519             }
2520             funcDecl = GetFuncDecl(d);
2521
2522             if(dllImport)
2523             {
2524                Specifier spec, next;
2525                for(spec = specifiers->first; spec; spec = next)
2526                {
2527                   next = spec.next;
2528                   if(spec.type == extendedSpecifier)
2529                   {
2530                      specifiers->Remove(spec);
2531                      FreeSpecifier(spec);
2532                   }
2533                }
2534             }
2535
2536             // Add this parameter if not a static method
2537             if(method.dataType && !method.dataType.staticMethod)
2538             {
2539                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2540                {
2541                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2542                   TypeName thisParam = MkTypeName(MkListOne(
2543                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2544                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2545                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2546                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2547
2548                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2549                   {
2550                      TypeName param = funcDecl.function.parameters->first;
2551                      funcDecl.function.parameters->Remove(param);
2552                      FreeTypeName(param);
2553                   }
2554
2555                   if(!funcDecl.function.parameters)
2556                      funcDecl.function.parameters = MkList();
2557                   funcDecl.function.parameters->Insert(null, thisParam);
2558                }
2559             }
2560             // Make sure we don't have empty parameter declarations for static methods...
2561             /*
2562             else if(!funcDecl.function.parameters)
2563             {
2564                funcDecl.function.parameters = MkList();
2565                funcDecl.function.parameters->Insert(null,
2566                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2567             }*/
2568          }
2569          // TESTING THIS:
2570          ProcessDeclarator(d);
2571
2572          ListAdd(declarators, MkInitDeclarator(d, null));
2573
2574          decl = MkDeclaration(specifiers, declarators);
2575
2576          ReplaceThisClassSpecifiers(specifiers, method._class);
2577
2578          // Keep a different symbol for the function definition than the declaration...
2579          if(symbol.pointerExternal)
2580          {
2581             Symbol functionSymbol { };
2582
2583             // Copy symbol
2584             {
2585                *functionSymbol = *symbol;
2586                functionSymbol.string = CopyString(symbol.string);
2587                if(functionSymbol.type)
2588                   functionSymbol.type.refCount++;
2589             }
2590
2591             excludedSymbols->Add(functionSymbol);
2592             symbol.pointerExternal.symbol = functionSymbol;
2593          }
2594          external = MkExternalDeclaration(decl);
2595          if(curExternal)
2596             ast->Insert(curExternal ? curExternal.prev : null, external);
2597          external.symbol = symbol;
2598          symbol.pointerExternal = external;
2599       }
2600       else if(ast)
2601       {
2602          // Move declaration higher...
2603          ast->Move(symbol.pointerExternal, curExternal.prev);
2604       }
2605
2606       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2607    }
2608 }
2609
2610 char * ReplaceThisClass(Class _class)
2611 {
2612    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2613    {
2614       bool first = true;
2615       int p = 0;
2616       ClassTemplateParameter param;
2617       int lastParam = -1;
2618
2619       char className[1024];
2620       strcpy(className, _class.fullName);
2621       for(param = _class.templateParams.first; param; param = param.next)
2622       {
2623          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2624          {
2625             if(first) strcat(className, "<");
2626             if(!first) strcat(className, ", ");
2627             if(lastParam + 1 != p)
2628             {
2629                strcat(className, param.name);
2630                strcat(className, " = ");
2631             }
2632             strcat(className, param.name);
2633             first = false;
2634             lastParam = p;
2635          }
2636          p++;
2637       }
2638       if(!first)
2639       {
2640          int len = strlen(className);
2641          if(className[len-1] == '>') className[len++] = ' ';
2642          className[len++] = '>';
2643          className[len++] = '\0';
2644       }
2645       return CopyString(className);
2646    }
2647    else
2648       return CopyString(_class.fullName);
2649 }
2650
2651 Type ReplaceThisClassType(Class _class)
2652 {
2653    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2654    {
2655       bool first = true;
2656       int p = 0;
2657       ClassTemplateParameter param;
2658       int lastParam = -1;
2659       char className[1024];
2660       strcpy(className, _class.fullName);
2661
2662       for(param = _class.templateParams.first; param; param = param.next)
2663       {
2664          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2665          {
2666             if(first) strcat(className, "<");
2667             if(!first) strcat(className, ", ");
2668             if(lastParam + 1 != p)
2669             {
2670                strcat(className, param.name);
2671                strcat(className, " = ");
2672             }
2673             strcat(className, param.name);
2674             first = false;
2675             lastParam = p;
2676          }
2677          p++;
2678       }
2679       if(!first)
2680       {
2681          int len = strlen(className);
2682          if(className[len-1] == '>') className[len++] = ' ';
2683          className[len++] = '>';
2684          className[len++] = '\0';
2685       }
2686       return MkClassType(className);
2687       //return ProcessTypeString(className, false);
2688    }
2689    else
2690    {
2691       return MkClassType(_class.fullName);
2692       //return ProcessTypeString(_class.fullName, false);
2693    }
2694 }
2695
2696 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2697 {
2698    if(specs != null && _class)
2699    {
2700       Specifier spec;
2701       for(spec = specs.first; spec; spec = spec.next)
2702       {
2703          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2704          {
2705             spec.type = nameSpecifier;
2706             spec.name = ReplaceThisClass(_class);
2707             spec.symbol = FindClass(spec.name); //_class.symbol;
2708          }
2709       }
2710    }
2711 }
2712
2713 // Returns imported or not
2714 bool DeclareFunction(GlobalFunction function, char * name)
2715 {
2716    Symbol symbol = function.symbol;
2717    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2718    {
2719       bool imported = false;
2720       bool dllImport = false;
2721
2722       if(!function.dataType)
2723       {
2724          function.dataType = ProcessTypeString(function.dataTypeString, false);
2725          if(!function.dataType.thisClass)
2726             function.dataType.staticMethod = true;
2727       }
2728
2729       if(inCompiler)
2730       {
2731          if(!symbol)
2732          {
2733             ModuleImport module = FindModule(function.module);
2734             // WARNING: This is not added anywhere...
2735             symbol = function.symbol = Symbol {  };
2736
2737             if(module.name)
2738             {
2739                if(!function.dataType.dllExport)
2740                {
2741                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2742                   module.functions.Add(symbol._import);
2743                }
2744             }
2745             // Set the symbol type
2746             {
2747                symbol.type = ProcessTypeString(function.dataTypeString, false);
2748                if(!symbol.type.thisClass)
2749                   symbol.type.staticMethod = true;
2750             }
2751          }
2752          imported = symbol._import ? true : false;
2753          if(imported && function.module != privateModule && function.module.importType != staticImport)
2754             dllImport = true;
2755       }
2756
2757       DeclareType(function.dataType, true, true);
2758
2759       if(inCompiler)
2760       {
2761          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2762          {
2763             // We need a declaration here :)
2764             Declaration decl;
2765             OldList * specifiers, * declarators;
2766             Declarator d;
2767             Declarator funcDecl;
2768             External external;
2769
2770             specifiers = MkList();
2771             declarators = MkList();
2772
2773             //if(imported)
2774                ListAdd(specifiers, MkSpecifier(EXTERN));
2775             /*
2776             else
2777                ListAdd(specifiers, MkSpecifier(STATIC));
2778             */
2779
2780             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2781             //if(imported)
2782             if(dllImport)
2783                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2784
2785             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2786             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2787             if(function.module.importType == staticImport)
2788             {
2789                Specifier spec;
2790                for(spec = specifiers->first; spec; spec = spec.next)
2791                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2792                   {
2793                      specifiers->Remove(spec);
2794                      FreeSpecifier(spec);
2795                      break;
2796                   }
2797             }
2798
2799             funcDecl = GetFuncDecl(d);
2800
2801             // Make sure we don't have empty parameter declarations for static methods...
2802             if(funcDecl && !funcDecl.function.parameters)
2803             {
2804                funcDecl.function.parameters = MkList();
2805                funcDecl.function.parameters->Insert(null,
2806                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2807             }
2808
2809             ListAdd(declarators, MkInitDeclarator(d, null));
2810
2811             {
2812                Context oldCtx = curContext;
2813                curContext = globalContext;
2814                decl = MkDeclaration(specifiers, declarators);
2815                curContext = oldCtx;
2816             }
2817
2818             // Keep a different symbol for the function definition than the declaration...
2819             if(symbol.pointerExternal)
2820             {
2821                Symbol functionSymbol { };
2822                // Copy symbol
2823                {
2824                   *functionSymbol = *symbol;
2825                   functionSymbol.string = CopyString(symbol.string);
2826                   if(functionSymbol.type)
2827                      functionSymbol.type.refCount++;
2828                }
2829
2830                excludedSymbols->Add(functionSymbol);
2831
2832                symbol.pointerExternal.symbol = functionSymbol;
2833             }
2834             external = MkExternalDeclaration(decl);
2835             if(curExternal)
2836                ast->Insert(curExternal.prev, external);
2837             external.symbol = symbol;
2838             symbol.pointerExternal = external;
2839          }
2840          else
2841          {
2842             // Move declaration higher...
2843             ast->Move(symbol.pointerExternal, curExternal.prev);
2844          }
2845
2846          if(curExternal)
2847             symbol.id = curExternal.symbol.idCode;
2848       }
2849    }
2850    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2851 }
2852
2853 void DeclareGlobalData(GlobalData data)
2854 {
2855    Symbol symbol = data.symbol;
2856    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2857    {
2858       if(inCompiler)
2859       {
2860          if(!symbol)
2861             symbol = data.symbol = Symbol { };
2862       }
2863       if(!data.dataType)
2864          data.dataType = ProcessTypeString(data.dataTypeString, false);
2865       DeclareType(data.dataType, true, true);
2866       if(inCompiler)
2867       {
2868          if(!symbol.pointerExternal)
2869          {
2870             // We need a declaration here :)
2871             Declaration decl;
2872             OldList * specifiers, * declarators;
2873             Declarator d;
2874             External external;
2875
2876             specifiers = MkList();
2877             declarators = MkList();
2878
2879             ListAdd(specifiers, MkSpecifier(EXTERN));
2880             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2881             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2882
2883             ListAdd(declarators, MkInitDeclarator(d, null));
2884
2885             decl = MkDeclaration(specifiers, declarators);
2886             external = MkExternalDeclaration(decl);
2887             if(curExternal)
2888                ast->Insert(curExternal.prev, external);
2889             external.symbol = symbol;
2890             symbol.pointerExternal = external;
2891          }
2892          else
2893          {
2894             // Move declaration higher...
2895             ast->Move(symbol.pointerExternal, curExternal.prev);
2896          }
2897
2898          if(curExternal)
2899             symbol.id = curExternal.symbol.idCode;
2900       }
2901    }
2902 }
2903
2904 class Conversion : struct
2905 {
2906    Conversion prev, next;
2907    Property convert;
2908    bool isGet;
2909    Type resultType;
2910 };
2911
2912 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2913 {
2914    if(source && dest)
2915    {
2916       // Property convert;
2917
2918       if(source.kind == templateType && dest.kind != templateType)
2919       {
2920          Type type = ProcessTemplateParameterType(source.templateParameter);
2921          if(type) source = type;
2922       }
2923
2924       if(dest.kind == templateType && source.kind != templateType)
2925       {
2926          Type type = ProcessTemplateParameterType(dest.templateParameter);
2927          if(type) dest = type;
2928       }
2929
2930       if(dest.classObjectType == typedObject)
2931       {
2932          if(source.classObjectType != anyObject)
2933             return true;
2934          else
2935          {
2936             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2937             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2938             {
2939                return true;
2940             }
2941          }
2942       }
2943       else
2944       {
2945          if(source.classObjectType == anyObject)
2946             return true;
2947          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2948             return true;
2949       }
2950
2951       if((dest.kind == structType && source.kind == structType) ||
2952          (dest.kind == unionType && source.kind == unionType))
2953       {
2954          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2955              (source.members.first && source.members.first == dest.members.first))
2956             return true;
2957       }
2958
2959       if(dest.kind == ellipsisType && source.kind != voidType)
2960          return true;
2961
2962       if(dest.kind == pointerType && dest.type.kind == voidType &&
2963          ((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))
2964          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2965
2966          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2967
2968          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2969          return true;
2970       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2971          ((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))
2972          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2973
2974          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2975
2976          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2977          return true;
2978
2979       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2980       {
2981          if(source._class.registered && source._class.registered.type == unitClass)
2982          {
2983             if(conversions != null)
2984             {
2985                if(source._class.registered == dest._class.registered)
2986                   return true;
2987             }
2988             else
2989             {
2990                Class sourceBase, destBase;
2991                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2992                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2993                if(sourceBase == destBase)
2994                   return true;
2995             }
2996          }
2997          // Don't match enum inheriting from other enum if resolving enumeration values
2998          // TESTING: !dest.classObjectType
2999          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3000             (enumBaseType ||
3001                (!source._class.registered || source._class.registered.type != enumClass) ||
3002                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3003             return true;
3004          else
3005          {
3006             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3007             if(enumBaseType &&
3008                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3009                ((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)
3010             {
3011                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3012                {
3013                   return true;
3014                }
3015             }
3016          }
3017       }
3018
3019       // JUST ADDED THIS...
3020       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3021          return true;
3022
3023       if(doConversion)
3024       {
3025          // Just added this for Straight conversion of ColorAlpha => Color
3026          if(source.kind == classType)
3027          {
3028             Class _class;
3029             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3030             {
3031                Property convert;
3032                for(convert = _class.conversions.first; convert; convert = convert.next)
3033                {
3034                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3035                   {
3036                      Conversion after = (conversions != null) ? conversions.last : null;
3037
3038                      if(!convert.dataType)
3039                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3040                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
3041                      {
3042                         if(!conversions && !convert.Get)
3043                            return true;
3044                         else if(conversions != null)
3045                         {
3046                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3047                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3048                               (dest.kind != classType || dest._class.registered != _class.base))
3049                               return true;
3050                            else
3051                            {
3052                               Conversion conv { convert = convert, isGet = true };
3053                               // conversions.Add(conv);
3054                               conversions.Insert(after, conv);
3055                               return true;
3056                            }
3057                         }
3058                      }
3059                   }
3060                }
3061             }
3062          }
3063
3064          // MOVING THIS??
3065
3066          if(dest.kind == classType)
3067          {
3068             Class _class;
3069             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3070             {
3071                Property convert;
3072                for(convert = _class.conversions.first; convert; convert = convert.next)
3073                {
3074                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3075                   {
3076                      // Conversion after = (conversions != null) ? conversions.last : null;
3077
3078                      if(!convert.dataType)
3079                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3080                      // Just added this equality check to prevent recursion.... Make it safer?
3081                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3082                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3083                      {
3084                         if(!conversions && !convert.Set)
3085                            return true;
3086                         else if(conversions != null)
3087                         {
3088                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3089                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3090                               (source.kind != classType || source._class.registered != _class.base))
3091                               return true;
3092                            else
3093                            {
3094                               // *** Testing this! ***
3095                               Conversion conv { convert = convert };
3096                               conversions.Add(conv);
3097                               //conversions.Insert(after, conv);
3098                               return true;
3099                            }
3100                         }
3101                      }
3102                   }
3103                }
3104             }
3105             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3106             {
3107                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3108                   (source.kind != classType || source._class.registered.type != structClass))
3109                   return true;
3110             }*/
3111
3112             // TESTING THIS... IS THIS OK??
3113             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3114             {
3115                if(!dest._class.registered.dataType)
3116                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3117                // Only support this for classes...
3118                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3119                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3120                {
3121                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3122                   {
3123                      return true;
3124                   }
3125                }
3126             }
3127          }
3128
3129          // Moved this lower
3130          if(source.kind == classType)
3131          {
3132             Class _class;
3133             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3134             {
3135                Property convert;
3136                for(convert = _class.conversions.first; convert; convert = convert.next)
3137                {
3138                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3139                   {
3140                      Conversion after = (conversions != null) ? conversions.last : null;
3141
3142                      if(!convert.dataType)
3143                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3144                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3145                      {
3146                         if(!conversions && !convert.Get)
3147                            return true;
3148                         else if(conversions != null)
3149                         {
3150                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3151                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3152                               (dest.kind != classType || dest._class.registered != _class.base))
3153                               return true;
3154                            else
3155                            {
3156                               Conversion conv { convert = convert, isGet = true };
3157
3158                               // conversions.Add(conv);
3159                               conversions.Insert(after, conv);
3160                               return true;
3161                            }
3162                         }
3163                      }
3164                   }
3165                }
3166             }
3167
3168             // TESTING THIS... IS THIS OK??
3169             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3170             {
3171                if(!source._class.registered.dataType)
3172                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3173                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3174                {
3175                   return true;
3176                }
3177             }
3178          }
3179       }
3180
3181       if(source.kind == classType || source.kind == subClassType)
3182          ;
3183       else if(dest.kind == source.kind &&
3184          (dest.kind != structType && dest.kind != unionType &&
3185           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3186           return true;
3187       // RECENTLY ADDED THESE
3188       else if(dest.kind == doubleType && source.kind == floatType)
3189          return true;
3190       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3191          return true;
3192       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3193          return true;
3194       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3195          return true;
3196       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3197          return true;
3198       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3199          return true;
3200       else if(source.kind == enumType &&
3201          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3202           return true;
3203       else if(dest.kind == enumType &&
3204          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3205           return true;
3206       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3207               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3208       {
3209          Type paramSource, paramDest;
3210
3211          if(dest.kind == methodType)
3212             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3213          if(source.kind == methodType)
3214             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3215
3216          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3217          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3218          if(dest.kind == methodType)
3219             dest = dest.method.dataType;
3220          if(source.kind == methodType)
3221             source = source.method.dataType;
3222
3223          paramSource = source.params.first;
3224          if(paramSource && paramSource.kind == voidType) paramSource = null;
3225          paramDest = dest.params.first;
3226          if(paramDest && paramDest.kind == voidType) paramDest = null;
3227
3228
3229          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3230             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3231          {
3232             // Source thisClass must be derived from destination thisClass
3233             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3234                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3235             {
3236                if(paramDest && paramDest.kind == classType)
3237                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3238                else
3239                   Compiler_Error($"method class should not take an object\n");
3240                return false;
3241             }
3242             paramDest = paramDest.next;
3243          }
3244          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3245          {
3246             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3247             {
3248                if(dest.thisClass)
3249                {
3250                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3251                   {
3252                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3253                      return false;
3254                   }
3255                }
3256                else
3257                {
3258                   // THIS WAS BACKWARDS:
3259                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3260                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3261                   {
3262                      if(owningClassDest)
3263                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3264                      else
3265                         Compiler_Error($"overriding class expected to be derived from method class\n");
3266                      return false;
3267                   }
3268                }
3269                paramSource = paramSource.next;
3270             }
3271             else
3272             {
3273                if(dest.thisClass)
3274                {
3275                   // Source thisClass must be derived from destination thisClass
3276                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3277                   {
3278                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3279                      return false;
3280                   }
3281                }
3282                else
3283                {
3284                   // THIS WAS BACKWARDS TOO??
3285                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3286                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3287                   {
3288                      //if(owningClass)
3289                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3290                      //else
3291                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3292                      return false;
3293                   }
3294                }
3295             }
3296          }
3297
3298
3299          // Source return type must be derived from destination return type
3300          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3301          {
3302             Compiler_Warning($"incompatible return type for function\n");
3303             return false;
3304          }
3305
3306          // Check parameters
3307
3308          for(; paramDest; paramDest = paramDest.next)
3309          {
3310             if(!paramSource)
3311             {
3312                //Compiler_Warning($"not enough parameters\n");
3313                Compiler_Error($"not enough parameters\n");
3314                return false;
3315             }
3316             {
3317                Type paramDestType = paramDest;
3318                Type paramSourceType = paramSource;
3319                Type type = paramDestType;
3320
3321                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3322                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3323                   paramSource.kind != templateType)
3324                {
3325                   int id = 0;
3326                   ClassTemplateParameter curParam = null;
3327                   Class sClass;
3328                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3329                   {
3330                      id = 0;
3331                      if(sClass.templateClass) sClass = sClass.templateClass;
3332                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3333                      {
3334                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3335                         {
3336                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3337                            {
3338                               if(sClass.templateClass) sClass = sClass.templateClass;
3339                               id += sClass.templateParams.count;
3340                            }
3341                            break;
3342                         }
3343                         id++;
3344                      }
3345                      if(curParam) break;
3346                   }
3347
3348                   if(curParam)
3349                   {
3350                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3351                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3352                   }
3353                }
3354
3355                // paramDest must be derived from paramSource
3356                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3357                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3358                {
3359                   char type[1024];
3360                   type[0] = 0;
3361                   PrintType(paramDest, type, false, true);
3362                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3363
3364                   if(paramDestType != paramDest)
3365                      FreeType(paramDestType);
3366                   return false;
3367                }
3368                if(paramDestType != paramDest)
3369                   FreeType(paramDestType);
3370             }
3371
3372             paramSource = paramSource.next;
3373          }
3374          if(paramSource)
3375          {
3376             Compiler_Error($"too many parameters\n");
3377             return false;
3378          }
3379          return true;
3380       }
3381       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3382       {
3383          return true;
3384       }
3385       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3386          (source.kind == arrayType || source.kind == pointerType))
3387       {
3388          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3389             return true;
3390       }
3391    }
3392    return false;
3393 }
3394
3395 static void FreeConvert(Conversion convert)
3396 {
3397    if(convert.resultType)
3398       FreeType(convert.resultType);
3399 }
3400
3401 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3402                               char * string, OldList conversions)
3403 {
3404    BTNamedLink link;
3405
3406    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3407    {
3408       Class _class = link.data;
3409       if(_class.type == enumClass)
3410       {
3411          OldList converts { };
3412          Type type { };
3413          type.kind = classType;
3414
3415          if(!_class.symbol)
3416             _class.symbol = FindClass(_class.fullName);
3417          type._class = _class.symbol;
3418
3419          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3420          {
3421             NamedLink value;
3422             Class enumClass = eSystem_FindClass(privateModule, "enum");
3423             if(enumClass)
3424             {
3425                Class baseClass;
3426                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3427                {
3428                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3429                   for(value = e.values.first; value; value = value.next)
3430                   {
3431                      if(!strcmp(value.name, string))
3432                         break;
3433                   }
3434                   if(value)
3435                   {
3436                      FreeExpContents(sourceExp);
3437                      FreeType(sourceExp.expType);
3438
3439                      sourceExp.isConstant = true;
3440                      sourceExp.expType = MkClassType(baseClass.fullName);
3441                      //if(inCompiler)
3442                      {
3443                         char constant[256];
3444                         sourceExp.type = constantExp;
3445                         if(!strcmp(baseClass.dataTypeString, "int"))
3446                            sprintf(constant, "%d",(int)value.data);
3447                         else
3448                            sprintf(constant, "0x%X",(int)value.data);
3449                         sourceExp.constant = CopyString(constant);
3450                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3451                      }
3452
3453                      while(converts.first)
3454                      {
3455                         Conversion convert = converts.first;
3456                         converts.Remove(convert);
3457                         conversions.Add(convert);
3458                      }
3459                      delete type;
3460                      return true;
3461                   }
3462                }
3463             }
3464          }
3465          if(converts.first)
3466             converts.Free(FreeConvert);
3467          delete type;
3468       }
3469    }
3470    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3471       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3472          return true;
3473    return false;
3474 }
3475
3476 public bool ModuleVisibility(Module searchIn, Module searchFor)
3477 {
3478    SubModule subModule;
3479
3480    if(searchFor == searchIn)
3481       return true;
3482
3483    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3484    {
3485       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3486       {
3487          if(ModuleVisibility(subModule.module, searchFor))
3488             return true;
3489       }
3490    }
3491    return false;
3492 }
3493
3494 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3495 {
3496    Module module;
3497
3498    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3499       return true;
3500    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3501       return true;
3502    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3503       return true;
3504
3505    for(module = mainModule.application.allModules.first; module; module = module.next)
3506    {
3507       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3508          return true;
3509    }
3510    return false;
3511 }
3512
3513 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3514 {
3515    Type source = sourceExp.expType;
3516    Type realDest = dest;
3517    Type backupSourceExpType = null;
3518
3519    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3520       return true;
3521
3522    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3523    {
3524        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3525        {
3526           Class sourceBase, destBase;
3527           for(sourceBase = source._class.registered;
3528               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3529               sourceBase = sourceBase.base);
3530           for(destBase = dest._class.registered;
3531               destBase && destBase.base && destBase.base.type != systemClass;
3532               destBase = destBase.base);
3533           //if(source._class.registered == dest._class.registered)
3534           if(sourceBase == destBase)
3535              return true;
3536        }
3537    }
3538
3539    if(source)
3540    {
3541       OldList * specs;
3542       bool flag = false;
3543       int64 value = MAXINT;
3544
3545       source.refCount++;
3546       dest.refCount++;
3547
3548       if(sourceExp.type == constantExp)
3549       {
3550          if(source.isSigned)
3551             value = strtoll(sourceExp.constant, null, 0);
3552          else
3553             value = strtoull(sourceExp.constant, null, 0);
3554       }
3555       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3556       {
3557          if(source.isSigned)
3558             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3559          else
3560             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3561       }
3562
3563       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3564          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3565       {
3566          FreeType(source);
3567          source = Type { kind = intType, isSigned = false, refCount = 1 };
3568       }
3569
3570       if(dest.kind == classType)
3571       {
3572          Class _class = dest._class ? dest._class.registered : null;
3573
3574          if(_class && _class.type == unitClass)
3575          {
3576             if(source.kind != classType)
3577             {
3578                Type tempType { };
3579                Type tempDest, tempSource;
3580
3581                for(; _class.base.type != systemClass; _class = _class.base);
3582                tempSource = dest;
3583                tempDest = tempType;
3584
3585                tempType.kind = classType;
3586                if(!_class.symbol)
3587                   _class.symbol = FindClass(_class.fullName);
3588
3589                tempType._class = _class.symbol;
3590                tempType.truth = dest.truth;
3591                if(tempType._class)
3592                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3593
3594                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3595                backupSourceExpType = sourceExp.expType;
3596                sourceExp.expType = dest; dest.refCount++;
3597                //sourceExp.expType = MkClassType(_class.fullName);
3598                flag = true;
3599
3600                delete tempType;
3601             }
3602          }
3603
3604
3605          // Why wasn't there something like this?
3606          if(_class && _class.type == bitClass && source.kind != classType)
3607          {
3608             if(!dest._class.registered.dataType)
3609                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3610             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3611             {
3612                FreeType(source);
3613                FreeType(sourceExp.expType);
3614                source = sourceExp.expType = MkClassType(dest._class.string);
3615                source.refCount++;
3616
3617                //source.kind = classType;
3618                //source._class = dest._class;
3619             }
3620          }
3621
3622          // Adding two enumerations
3623          /*
3624          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3625          {
3626             if(!source._class.registered.dataType)
3627                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3628             if(!dest._class.registered.dataType)
3629                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3630
3631             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3632             {
3633                FreeType(source);
3634                source = sourceExp.expType = MkClassType(dest._class.string);
3635                source.refCount++;
3636
3637                //source.kind = classType;
3638                //source._class = dest._class;
3639             }
3640          }*/
3641
3642          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3643          {
3644             OldList * specs = MkList();
3645             Declarator decl;
3646             char string[1024];
3647
3648             ReadString(string, sourceExp.string);
3649             decl = SpecDeclFromString(string, specs, null);
3650
3651             FreeExpContents(sourceExp);
3652             FreeType(sourceExp.expType);
3653
3654             sourceExp.type = classExp;
3655             sourceExp._classExp.specifiers = specs;
3656             sourceExp._classExp.decl = decl;
3657             sourceExp.expType = dest;
3658             dest.refCount++;
3659
3660             FreeType(source);
3661             FreeType(dest);
3662             if(backupSourceExpType) FreeType(backupSourceExpType);
3663             return true;
3664          }
3665       }
3666       else if(source.kind == classType)
3667       {
3668          Class _class = source._class ? source._class.registered : null;
3669
3670          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3671          {
3672             /*
3673             if(dest.kind != classType)
3674             {
3675                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3676                if(!source._class.registered.dataType)
3677                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3678
3679                FreeType(dest);
3680                dest = MkClassType(source._class.string);
3681                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3682                //   dest = MkClassType(source._class.string);
3683             }
3684             */
3685
3686             if(dest.kind != classType)
3687             {
3688                Type tempType { };
3689                Type tempDest, tempSource;
3690
3691                if(!source._class.registered.dataType)
3692                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3693
3694                for(; _class.base.type != systemClass; _class = _class.base);
3695                tempDest = source;
3696                tempSource = tempType;
3697                tempType.kind = classType;
3698                tempType._class = FindClass(_class.fullName);
3699                tempType.truth = source.truth;
3700                tempType.classObjectType = source.classObjectType;
3701
3702                if(tempType._class)
3703                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3704
3705                // PUT THIS BACK TESTING UNITS?
3706                if(conversions.last)
3707                {
3708                   ((Conversion)(conversions.last)).resultType = dest;
3709                   dest.refCount++;
3710                }
3711
3712                FreeType(sourceExp.expType);
3713                sourceExp.expType = MkClassType(_class.fullName);
3714                sourceExp.expType.truth = source.truth;
3715                sourceExp.expType.classObjectType = source.classObjectType;
3716
3717                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3718
3719                if(!sourceExp.destType)
3720                {
3721                   FreeType(sourceExp.destType);
3722                   sourceExp.destType = sourceExp.expType;
3723                   if(sourceExp.expType)
3724                      sourceExp.expType.refCount++;
3725                }
3726                //flag = true;
3727                //source = _class.dataType;
3728
3729
3730                // TOCHECK: TESTING THIS NEW CODE
3731                if(!_class.dataType)
3732                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3733                FreeType(dest);
3734                dest = MkClassType(source._class.string);
3735                dest.truth = source.truth;
3736                dest.classObjectType = source.classObjectType;
3737
3738                FreeType(source);
3739                source = _class.dataType;
3740                source.refCount++;
3741
3742                delete tempType;
3743             }
3744          }
3745       }
3746
3747       if(!flag)
3748       {
3749          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3750          {
3751             FreeType(source);
3752             FreeType(dest);
3753             return true;
3754          }
3755       }
3756
3757       // Implicit Casts
3758       /*
3759       if(source.kind == classType)
3760       {
3761          Class _class = source._class.registered;
3762          if(_class.type == unitClass)
3763          {
3764             if(!_class.dataType)
3765                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3766             source = _class.dataType;
3767          }
3768       }*/
3769
3770       if(dest.kind == classType)
3771       {
3772          Class _class = dest._class ? dest._class.registered : null;
3773          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") ||
3774             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3775          {
3776             if(_class.type == normalClass || _class.type == noHeadClass)
3777             {
3778                Expression newExp { };
3779                *newExp = *sourceExp;
3780                if(sourceExp.destType) sourceExp.destType.refCount++;
3781                if(sourceExp.expType)  sourceExp.expType.refCount++;
3782                sourceExp.type = castExp;
3783                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3784                sourceExp.cast.exp = newExp;
3785                FreeType(sourceExp.expType);
3786                sourceExp.expType = null;
3787                ProcessExpressionType(sourceExp);
3788
3789                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3790                if(!inCompiler)
3791                {
3792                   FreeType(sourceExp.expType);
3793                   sourceExp.expType = dest;
3794                }
3795
3796                FreeType(source);
3797                if(inCompiler) FreeType(dest);
3798
3799                if(backupSourceExpType) FreeType(backupSourceExpType);
3800                return true;
3801             }
3802
3803             if(!_class.dataType)
3804                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3805             FreeType(dest);
3806             dest = _class.dataType;
3807             dest.refCount++;
3808          }
3809
3810          // Accept lower precision types for units, since we want to keep the unit type
3811          if(dest.kind == doubleType &&
3812             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3813              source.kind == charType || source.kind == _BoolType))
3814          {
3815             specs = MkListOne(MkSpecifier(DOUBLE));
3816          }
3817          else if(dest.kind == floatType &&
3818             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3819             source.kind == _BoolType || source.kind == doubleType))
3820          {
3821             specs = MkListOne(MkSpecifier(FLOAT));
3822          }
3823          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3824             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3825          {
3826             specs = MkList();
3827             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3828             ListAdd(specs, MkSpecifier(INT64));
3829          }
3830          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3831             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3832          {
3833             specs = MkList();
3834             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3835             ListAdd(specs, MkSpecifier(INT));
3836          }
3837          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3838             source.kind == floatType || source.kind == doubleType))
3839          {
3840             specs = MkList();
3841             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3842             ListAdd(specs, MkSpecifier(SHORT));
3843          }
3844          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3845             source.kind == floatType || source.kind == doubleType))
3846          {
3847             specs = MkList();
3848             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3849             ListAdd(specs, MkSpecifier(CHAR));
3850          }
3851          else
3852          {
3853             FreeType(source);
3854             FreeType(dest);
3855             if(backupSourceExpType)
3856             {
3857                // Failed to convert: revert previous exp type
3858                if(sourceExp.expType) FreeType(sourceExp.expType);
3859                sourceExp.expType = backupSourceExpType;
3860             }
3861             return false;
3862          }
3863       }
3864       else if(dest.kind == doubleType &&
3865          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3866           source.kind == _BoolType || source.kind == charType))
3867       {
3868          specs = MkListOne(MkSpecifier(DOUBLE));
3869       }
3870       else if(dest.kind == floatType &&
3871          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3872       {
3873          specs = MkListOne(MkSpecifier(FLOAT));
3874       }
3875       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3876          (value == 1 || value == 0))
3877       {
3878          specs = MkList();
3879          ListAdd(specs, MkSpecifier(BOOL));
3880       }
3881       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3882          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3883       {
3884          specs = MkList();
3885          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3886          ListAdd(specs, MkSpecifier(CHAR));
3887       }
3888       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3889          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3890       {
3891          specs = MkList();
3892          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3893          ListAdd(specs, MkSpecifier(SHORT));
3894       }
3895       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3896       {
3897          specs = MkList();
3898          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3899          ListAdd(specs, MkSpecifier(INT));
3900       }
3901       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3902       {
3903          specs = MkList();
3904          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3905          ListAdd(specs, MkSpecifier(INT64));
3906       }
3907       else if(dest.kind == enumType &&
3908          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3909       {
3910          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3911       }
3912       else
3913       {
3914          FreeType(source);
3915          FreeType(dest);
3916          if(backupSourceExpType)
3917          {
3918             // Failed to convert: revert previous exp type
3919             if(sourceExp.expType) FreeType(sourceExp.expType);
3920             sourceExp.expType = backupSourceExpType;
3921          }
3922          return false;
3923       }
3924
3925       if(!flag)
3926       {
3927          Expression newExp { };
3928          *newExp = *sourceExp;
3929          newExp.prev = null;
3930          newExp.next = null;
3931          if(sourceExp.destType) sourceExp.destType.refCount++;
3932          if(sourceExp.expType)  sourceExp.expType.refCount++;
3933
3934          sourceExp.type = castExp;
3935          if(realDest.kind == classType)
3936          {
3937             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3938             FreeList(specs, FreeSpecifier);
3939          }
3940          else
3941             sourceExp.cast.typeName = MkTypeName(specs, null);
3942          if(newExp.type == opExp)
3943          {
3944             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3945          }
3946          else
3947             sourceExp.cast.exp = newExp;
3948
3949          FreeType(sourceExp.expType);
3950          sourceExp.expType = null;
3951          ProcessExpressionType(sourceExp);
3952       }
3953       else
3954          FreeList(specs, FreeSpecifier);
3955
3956       FreeType(dest);
3957       FreeType(source);
3958       if(backupSourceExpType) FreeType(backupSourceExpType);
3959
3960       return true;
3961    }
3962    else
3963    {
3964       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3965       if(sourceExp.type == identifierExp)
3966       {
3967          Identifier id = sourceExp.identifier;
3968          if(dest.kind == classType)
3969          {
3970             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3971             {
3972                Class _class = dest._class.registered;
3973                Class enumClass = eSystem_FindClass(privateModule, "enum");
3974                if(enumClass)
3975                {
3976                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3977                   {
3978                      NamedLink value;
3979                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3980                      for(value = e.values.first; value; value = value.next)
3981                      {
3982                         if(!strcmp(value.name, id.string))
3983                            break;
3984                      }
3985                      if(value)
3986                      {
3987                         FreeExpContents(sourceExp);
3988                         FreeType(sourceExp.expType);
3989
3990                         sourceExp.isConstant = true;
3991                         sourceExp.expType = MkClassType(_class.fullName);
3992                         //if(inCompiler)
3993                         {
3994                            char constant[256];
3995                            sourceExp.type = constantExp;
3996                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3997                               sprintf(constant, "%d", (int) value.data);
3998                            else
3999                               sprintf(constant, "0x%X", (int) value.data);
4000                            sourceExp.constant = CopyString(constant);
4001                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4002                         }
4003                         return true;
4004                      }
4005                   }
4006                }
4007             }
4008          }
4009
4010          // Loop through all enum classes
4011          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4012             return true;
4013       }
4014    }
4015    return false;
4016 }
4017
4018 #define TERTIARY(o, name, m, t, p) \
4019    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4020    {                                                              \
4021       exp.type = constantExp;                                    \
4022       exp.string = p(op1.m ? op2.m : op3.m);                     \
4023       if(!exp.expType) \
4024          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4025       return true;                                                \
4026    }
4027
4028 #define BINARY(o, name, m, t, p) \
4029    static bool name(Expression exp, Operand op1, Operand op2)   \
4030    {                                                              \
4031       t value2 = op2.m;                                           \
4032       exp.type = constantExp;                                    \
4033       exp.string = p(op1.m o value2);                     \
4034       if(!exp.expType) \
4035          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4036       return true;                                                \
4037    }
4038
4039 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4040    static bool name(Expression exp, Operand op1, Operand op2)   \
4041    {                                                              \
4042       t value2 = op2.m;                                           \
4043       exp.type = constantExp;                                    \
4044       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4045       if(!exp.expType) \
4046          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4047       return true;                                                \
4048    }
4049
4050 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4051    static bool name(Expression exp, Operand op1, Operand op2)   \
4052    {                                                              \
4053       t value2 = op2.m;                                           \
4054       exp.type = constantExp;                                    \
4055       exp.string = p(op1.m o value2);             \
4056       if(!exp.expType) \
4057          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4058       return true;                                                \
4059    }
4060
4061 #define UNARY(o, name, m, t, p) \
4062    static bool name(Expression exp, Operand op1)                \
4063    {                                                              \
4064       exp.type = constantExp;                                    \
4065       exp.string = p((t)(o op1.m));                                   \
4066       if(!exp.expType) \
4067          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4068       return true;                                                \
4069    }
4070
4071 #define OPERATOR_ALL(macro, o, name) \
4072    macro(o, Int##name, i, int, PrintInt) \
4073    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4074    macro(o, Int64##name, i64, int64, PrintInt64) \
4075    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4076    macro(o, Short##name, s, short, PrintShort) \
4077    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4078    macro(o, Char##name, c, char, PrintChar) \
4079    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4080    macro(o, Float##name, f, float, PrintFloat) \
4081    macro(o, Double##name, d, double, PrintDouble)
4082
4083 #define OPERATOR_INTTYPES(macro, o, name) \
4084    macro(o, Int##name, i, int, PrintInt) \
4085    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4086    macro(o, Int64##name, i64, int64, PrintInt64) \
4087    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4088    macro(o, Short##name, s, short, PrintShort) \
4089    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4090    macro(o, Char##name, c, char, PrintChar) \
4091    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4092
4093 #define OPERATOR_REALTYPES(macro, o, name) \
4094    macro(o, Float##name, f, float, PrintFloat) \
4095    macro(o, Double##name, d, double, PrintDouble)
4096
4097 // binary arithmetic
4098 OPERATOR_ALL(BINARY, +, Add)
4099 OPERATOR_ALL(BINARY, -, Sub)
4100 OPERATOR_ALL(BINARY, *, Mul)
4101 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4102 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4103 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4104
4105 // unary arithmetic
4106 OPERATOR_ALL(UNARY, -, Neg)
4107
4108 // unary arithmetic increment and decrement
4109 OPERATOR_ALL(UNARY, ++, Inc)
4110 OPERATOR_ALL(UNARY, --, Dec)
4111
4112 // binary arithmetic assignment
4113 OPERATOR_ALL(BINARY, =, Asign)
4114 OPERATOR_ALL(BINARY, +=, AddAsign)
4115 OPERATOR_ALL(BINARY, -=, SubAsign)
4116 OPERATOR_ALL(BINARY, *=, MulAsign)
4117 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4118 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4119 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4120
4121 // binary bitwise
4122 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4123 OPERATOR_INTTYPES(BINARY, |, BitOr)
4124 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4125 OPERATOR_INTTYPES(BINARY, <<, LShift)
4126 OPERATOR_INTTYPES(BINARY, >>, RShift)
4127
4128 // unary bitwise
4129 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4130
4131 // binary bitwise assignment
4132 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4133 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4134 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4135 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4136 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4137
4138 // unary logical negation
4139 OPERATOR_INTTYPES(UNARY, !, Not)
4140
4141 // binary logical equality
4142 OPERATOR_ALL(BINARY, ==, Equ)
4143 OPERATOR_ALL(BINARY, !=, Nqu)
4144
4145 // binary logical
4146 OPERATOR_ALL(BINARY, &&, And)
4147 OPERATOR_ALL(BINARY, ||, Or)
4148
4149 // binary logical relational
4150 OPERATOR_ALL(BINARY, >, Grt)
4151 OPERATOR_ALL(BINARY, <, Sma)
4152 OPERATOR_ALL(BINARY, >=, GrtEqu)
4153 OPERATOR_ALL(BINARY, <=, SmaEqu)
4154
4155 // tertiary condition operator
4156 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4157
4158 //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
4159 #define OPERATOR_TABLE_ALL(name, type) \
4160     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4161                           type##Neg, \
4162                           type##Inc, type##Dec, \
4163                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4164                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4165                           type##BitNot, \
4166                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4167                           type##Not, \
4168                           type##Equ, type##Nqu, \
4169                           type##And, type##Or, \
4170                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4171                         }; \
4172
4173 #define OPERATOR_TABLE_INTTYPES(name, type) \
4174     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4175                           type##Neg, \
4176                           type##Inc, type##Dec, \
4177                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4178                           null, null, null, null, null, \
4179                           null, \
4180                           null, null, null, null, null, \
4181                           null, \
4182                           type##Equ, type##Nqu, \
4183                           type##And, type##Or, \
4184                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4185                         }; \
4186
4187 OPERATOR_TABLE_ALL(int, Int)
4188 OPERATOR_TABLE_ALL(uint, UInt)
4189 OPERATOR_TABLE_ALL(int64, Int64)
4190 OPERATOR_TABLE_ALL(uint64, UInt64)
4191 OPERATOR_TABLE_ALL(short, Short)
4192 OPERATOR_TABLE_ALL(ushort, UShort)
4193 OPERATOR_TABLE_INTTYPES(float, Float)
4194 OPERATOR_TABLE_INTTYPES(double, Double)
4195 OPERATOR_TABLE_ALL(char, Char)
4196 OPERATOR_TABLE_ALL(uchar, UChar)
4197
4198 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4199 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4200 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4201 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4202 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4203 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4204 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4205 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4206
4207 public void ReadString(char * output,  char * string)
4208 {
4209    int len = strlen(string);
4210    int c,d = 0;
4211    bool quoted = false, escaped = false;
4212    for(c = 0; c<len; c++)
4213    {
4214       char ch = string[c];
4215       if(escaped)
4216       {
4217          switch(ch)
4218          {
4219             case 'n': output[d] = '\n'; break;
4220             case 't': output[d] = '\t'; break;
4221             case 'a': output[d] = '\a'; break;
4222             case 'b': output[d] = '\b'; break;
4223             case 'f': output[d] = '\f'; break;
4224             case 'r': output[d] = '\r'; break;
4225             case 'v': output[d] = '\v'; break;
4226             case '\\': output[d] = '\\'; break;
4227             case '\"': output[d] = '\"'; break;
4228             case '\'': output[d] = '\''; break;
4229             default: output[d] = ch;
4230          }
4231          d++;
4232          escaped = false;
4233       }
4234       else
4235       {
4236          if(ch == '\"')
4237             quoted ^= true;
4238          else if(quoted)
4239          {
4240             if(ch == '\\')
4241                escaped = true;
4242             else
4243                output[d++] = ch;
4244          }
4245       }
4246    }
4247    output[d] = '\0';
4248 }
4249
4250 // String Unescape Copy
4251
4252 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4253 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4254 public int UnescapeString(char * d, char * s, int len)
4255 {
4256    int j = 0, k = 0;
4257    char ch;
4258    while(j < len && (ch = s[j]))
4259    {
4260       switch(ch)
4261       {
4262          case '\\':
4263             switch((ch = s[++j]))
4264             {
4265                case 'n': d[k] = '\n'; break;
4266                case 't': d[k] = '\t'; break;
4267                case 'a': d[k] = '\a'; break;
4268                case 'b': d[k] = '\b'; break;
4269                case 'f': d[k] = '\f'; break;
4270                case 'r': d[k] = '\r'; break;
4271                case 'v': d[k] = '\v'; break;
4272                case '\\': d[k] = '\\'; break;
4273                case '\"': d[k] = '\"'; break;
4274                case '\'': d[k] = '\''; break;
4275                default: d[k] = '\\'; d[k] = ch;
4276             }
4277             break;
4278          default:
4279             d[k] = ch;
4280       }
4281       j++, k++;
4282    }
4283    d[k] = '\0';
4284    return k;
4285 }
4286
4287 public char * OffsetEscapedString(char * s, int len, int offset)
4288 {
4289    char ch;
4290    int j = 0, k = 0;
4291    while(j < len && k < offset && (ch = s[j]))
4292    {
4293       if(ch == '\\') ++j;
4294       j++, k++;
4295    }
4296    return (k == offset) ? s + j : null;
4297 }
4298
4299 public Operand GetOperand(Expression exp)
4300 {
4301    Operand op { };
4302    Type type = exp.expType;
4303    if(type)
4304    {
4305       while(type.kind == classType &&
4306          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4307       {
4308          if(!type._class.registered.dataType)
4309             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4310          type = type._class.registered.dataType;
4311
4312       }
4313       if(exp.type == stringExp && op.kind == pointerType)
4314       {
4315          op.ui64 = (uint64)exp.string;
4316          op.kind = pointerType;
4317          op.ops = uint64Ops;
4318       }
4319       else if(exp.isConstant && exp.type == constantExp)
4320       {
4321          op.kind = type.kind;
4322          op.type = exp.expType;
4323
4324          switch(op.kind)
4325          {
4326             case _BoolType:
4327             case charType:
4328             {
4329                if(exp.constant[0] == '\'')
4330                {
4331                   op.c = exp.constant[1];
4332                   op.ops = charOps;
4333                }
4334                else if(type.isSigned)
4335                {
4336                   op.c = (char)strtol(exp.constant, null, 0);
4337                   op.ops = charOps;
4338                }
4339                else
4340                {
4341                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4342                   op.ops = ucharOps;
4343                }
4344                break;
4345             }
4346             case shortType:
4347                if(type.isSigned)
4348                {
4349                   op.s = (short)strtol(exp.constant, null, 0);
4350                   op.ops = shortOps;
4351                }
4352                else
4353                {
4354                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4355                   op.ops = ushortOps;
4356                }
4357                break;
4358             case intType:
4359             case longType:
4360                if(type.isSigned)
4361                {
4362                   op.i = (int)strtol(exp.constant, null, 0);
4363                   op.ops = intOps;
4364                }
4365                else
4366                {
4367                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4368                   op.ops = uintOps;
4369                }
4370                op.kind = intType;
4371                break;
4372             case int64Type:
4373                if(type.isSigned)
4374                {
4375                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4376                   op.ops = int64Ops;
4377                }
4378                else
4379                {
4380                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4381                   op.ops = uint64Ops;
4382                }
4383                op.kind = int64Type;
4384                break;
4385             case intPtrType:
4386                if(type.isSigned)
4387                {
4388                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4389                   op.ops = int64Ops;
4390                }
4391                else
4392                {
4393                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4394                   op.ops = uint64Ops;
4395                }
4396                op.kind = int64Type;
4397                break;
4398             case intSizeType:
4399                if(type.isSigned)
4400                {
4401                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4402                   op.ops = int64Ops;
4403                }
4404                else
4405                {
4406                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4407                   op.ops = uint64Ops;
4408                }
4409                op.kind = int64Type;
4410                break;
4411             case floatType:
4412                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4413                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4414                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4415                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4416                else
4417                   op.f = (float)strtod(exp.constant, null);
4418                op.ops = floatOps;
4419                break;
4420             case doubleType:
4421                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4422                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4423                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4424                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4425                else
4426                   op.d = (double)strtod(exp.constant, null);
4427                op.ops = doubleOps;
4428                break;
4429             //case classType:    For when we have operator overloading...
4430             // Pointer additions
4431             //case functionType:
4432             case arrayType:
4433             case pointerType:
4434             case classType:
4435                op.ui64 = _strtoui64(exp.constant, null, 0);
4436                op.kind = pointerType;
4437                op.ops = uint64Ops;
4438                // op.ptrSize =
4439                break;
4440          }
4441       }
4442    }
4443    return op;
4444 }
4445
4446 static void UnusedFunction()
4447 {
4448    int a;
4449    a.OnGetString(0,0,0);
4450 }
4451 default:
4452 extern int __ecereVMethodID_class_OnGetString;
4453 public:
4454
4455 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4456 {
4457    DataMember dataMember;
4458    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4459    {
4460       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4461          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4462       else
4463       {
4464          Expression exp { };
4465          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4466          Type type;
4467          void * ptr = inst.data + dataMember.offset + offset;
4468          char * result = null;
4469          exp.loc = member.loc = inst.loc;
4470          ((Identifier)member.identifiers->first).loc = inst.loc;
4471
4472          if(!dataMember.dataType)
4473             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4474          type = dataMember.dataType;
4475          if(type.kind == classType)
4476          {
4477             Class _class = type._class.registered;
4478             if(_class.type == enumClass)
4479             {
4480                Class enumClass = eSystem_FindClass(privateModule, "enum");
4481                if(enumClass)
4482                {
4483                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4484                   NamedLink item;
4485                   for(item = e.values.first; item; item = item.next)
4486                   {
4487                      if((int)item.data == *(int *)ptr)
4488                      {
4489                         result = item.name;
4490                         break;
4491                      }
4492                   }
4493                   if(result)
4494                   {
4495                      exp.identifier = MkIdentifier(result);
4496                      exp.type = identifierExp;
4497                      exp.destType = MkClassType(_class.fullName);
4498                      ProcessExpressionType(exp);
4499                   }
4500                }
4501             }
4502             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4503             {
4504                if(!_class.dataType)
4505                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4506                type = _class.dataType;
4507             }
4508          }
4509          if(!result)
4510          {
4511             switch(type.kind)
4512             {
4513                case floatType:
4514                {
4515                   FreeExpContents(exp);
4516
4517                   exp.constant = PrintFloat(*(float*)ptr);
4518                   exp.type = constantExp;
4519                   break;
4520                }
4521                case doubleType:
4522                {
4523                   FreeExpContents(exp);
4524
4525                   exp.constant = PrintDouble(*(double*)ptr);
4526                   exp.type = constantExp;
4527                   break;
4528                }
4529                case intType:
4530                {
4531                   FreeExpContents(exp);
4532
4533                   exp.constant = PrintInt(*(int*)ptr);
4534                   exp.type = constantExp;
4535                   break;
4536                }
4537                case int64Type:
4538                {
4539                   FreeExpContents(exp);
4540
4541                   exp.constant = PrintInt64(*(int64*)ptr);
4542                   exp.type = constantExp;
4543                   break;
4544                }
4545                case intPtrType:
4546                {
4547                   FreeExpContents(exp);
4548                   // TODO: This should probably use proper type
4549                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4550                   exp.type = constantExp;
4551                   break;
4552                }
4553                case intSizeType:
4554                {
4555                   FreeExpContents(exp);
4556                   // TODO: This should probably use proper type
4557                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4558                   exp.type = constantExp;
4559                   break;
4560                }
4561                default:
4562                   Compiler_Error($"Unhandled type populating instance\n");
4563             }
4564          }
4565          ListAdd(memberList, member);
4566       }
4567
4568       if(parentDataMember.type == unionMember)
4569          break;
4570    }
4571 }
4572
4573 void PopulateInstance(Instantiation inst)
4574 {
4575    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4576    Class _class = classSym.registered;
4577    DataMember dataMember;
4578    OldList * memberList = MkList();
4579    // Added this check and ->Add to prevent memory leaks on bad code
4580    if(!inst.members)
4581       inst.members = MkListOne(MkMembersInitList(memberList));
4582    else
4583       inst.members->Add(MkMembersInitList(memberList));
4584    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4585    {
4586       if(!dataMember.isProperty)
4587       {
4588          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4589             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4590          else
4591          {
4592             Expression exp { };
4593             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4594             Type type;
4595             void * ptr = inst.data + dataMember.offset;
4596             char * result = null;
4597
4598             exp.loc = member.loc = inst.loc;
4599             ((Identifier)member.identifiers->first).loc = inst.loc;
4600
4601             if(!dataMember.dataType)
4602                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4603             type = dataMember.dataType;
4604             if(type.kind == classType)
4605             {
4606                Class _class = type._class.registered;
4607                if(_class.type == enumClass)
4608                {
4609                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4610                   if(enumClass)
4611                   {
4612                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4613                      NamedLink item;
4614                      for(item = e.values.first; item; item = item.next)
4615                      {
4616                         if((int)item.data == *(int *)ptr)
4617                         {
4618                            result = item.name;
4619                            break;
4620                         }
4621                      }
4622                   }
4623                   if(result)
4624                   {
4625                      exp.identifier = MkIdentifier(result);
4626                      exp.type = identifierExp;
4627                      exp.destType = MkClassType(_class.fullName);
4628                      ProcessExpressionType(exp);
4629                   }
4630                }
4631                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4632                {
4633                   if(!_class.dataType)
4634                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4635                   type = _class.dataType;
4636                }
4637             }
4638             if(!result)
4639             {
4640                switch(type.kind)
4641                {
4642                   case floatType:
4643                   {
4644                      exp.constant = PrintFloat(*(float*)ptr);
4645                      exp.type = constantExp;
4646                      break;
4647                   }
4648                   case doubleType:
4649                   {
4650                      exp.constant = PrintDouble(*(double*)ptr);
4651                      exp.type = constantExp;
4652                      break;
4653                   }
4654                   case intType:
4655                   {
4656                      exp.constant = PrintInt(*(int*)ptr);
4657                      exp.type = constantExp;
4658                      break;
4659                   }
4660                   case int64Type:
4661                   {
4662                      exp.constant = PrintInt64(*(int64*)ptr);
4663                      exp.type = constantExp;
4664                      break;
4665                   }
4666                   case intPtrType:
4667                   {
4668                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4669                      exp.type = constantExp;
4670                      break;
4671                   }
4672                   default:
4673                      Compiler_Error($"Unhandled type populating instance\n");
4674                }
4675             }
4676             ListAdd(memberList, member);
4677          }
4678       }
4679    }
4680 }
4681
4682 void ComputeInstantiation(Expression exp)
4683 {
4684    Instantiation inst = exp.instance;
4685    MembersInit members;
4686    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4687    Class _class = classSym ? classSym.registered : null;
4688    DataMember curMember = null;
4689    Class curClass = null;
4690    DataMember subMemberStack[256];
4691    int subMemberStackPos = 0;
4692    uint64 bits = 0;
4693
4694    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4695    {
4696       // Don't recompute the instantiation...
4697       // Non Simple classes will have become constants by now
4698       if(inst.data)
4699          return;
4700
4701       if(_class.type == normalClass || _class.type == noHeadClass)
4702       {
4703          inst.data = (byte *)eInstance_New(_class);
4704          if(_class.type == normalClass)
4705             ((Instance)inst.data)._refCount++;
4706       }
4707       else
4708          inst.data = new0 byte[_class.structSize];
4709    }
4710
4711    if(inst.members)
4712    {
4713       for(members = inst.members->first; members; members = members.next)
4714       {
4715          switch(members.type)
4716          {
4717             case dataMembersInit:
4718             {
4719                if(members.dataMembers)
4720                {
4721                   MemberInit member;
4722                   for(member = members.dataMembers->first; member; member = member.next)
4723                   {
4724                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4725                      bool found = false;
4726
4727                      Property prop = null;
4728                      DataMember dataMember = null;
4729                      Method method = null;
4730                      uint dataMemberOffset;
4731
4732                      if(!ident)
4733                      {
4734                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4735                         if(curMember)
4736                         {
4737                            if(curMember.isProperty)
4738                               prop = (Property)curMember;
4739                            else
4740                            {
4741                               dataMember = curMember;
4742
4743                               // CHANGED THIS HERE
4744                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4745
4746                               // 2013/17/29 -- It seems that this was missing here!
4747                               if(_class.type == normalClass)
4748                                  dataMemberOffset += _class.base.structSize;
4749                               // dataMemberOffset = dataMember.offset;
4750                            }
4751                            found = true;
4752                         }
4753                      }
4754                      else
4755                      {
4756                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4757                         if(prop)
4758                         {
4759                            found = true;
4760                            if(prop.memberAccess == publicAccess)
4761                            {
4762                               curMember = (DataMember)prop;
4763                               curClass = prop._class;
4764                            }
4765                         }
4766                         else
4767                         {
4768                            DataMember _subMemberStack[256];
4769                            int _subMemberStackPos = 0;
4770
4771                            // FILL MEMBER STACK
4772                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4773
4774                            if(dataMember)
4775                            {
4776                               found = true;
4777                               if(dataMember.memberAccess == publicAccess)
4778                               {
4779                                  curMember = dataMember;
4780                                  curClass = dataMember._class;
4781                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4782                                  subMemberStackPos = _subMemberStackPos;
4783                               }
4784                            }
4785                         }
4786                      }
4787
4788                      if(found && member.initializer && member.initializer.type == expInitializer)
4789                      {
4790                         Expression value = member.initializer.exp;
4791                         Type type = null;
4792                         bool deepMember = false;
4793                         if(prop)
4794                         {
4795                            type = prop.dataType;
4796                         }
4797                         else if(dataMember)
4798                         {
4799                            if(!dataMember.dataType)
4800                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4801
4802                            type = dataMember.dataType;
4803                         }
4804
4805                         if(ident && ident.next)
4806                         {
4807                            deepMember = true;
4808
4809                            // for(; ident && type; ident = ident.next)
4810                            for(ident = ident.next; ident && type; ident = ident.next)
4811                            {
4812                               if(type.kind == classType)
4813                               {
4814                                  prop = eClass_FindProperty(type._class.registered,
4815                                     ident.string, privateModule);
4816                                  if(prop)
4817                                     type = prop.dataType;
4818                                  else
4819                                  {
4820                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4821                                        ident.string, &dataMemberOffset, privateModule, null, null);
4822                                     if(dataMember)
4823                                        type = dataMember.dataType;
4824                                  }
4825                               }
4826                               else if(type.kind == structType || type.kind == unionType)
4827                               {
4828                                  Type memberType;
4829                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4830                                  {
4831                                     if(!strcmp(memberType.name, ident.string))
4832                                     {
4833                                        type = memberType;
4834                                        break;
4835                                     }
4836                                  }
4837                               }
4838                            }
4839                         }
4840                         if(value)
4841                         {
4842                            FreeType(value.destType);
4843                            value.destType = type;
4844                            if(type) type.refCount++;
4845                            ComputeExpression(value);
4846                         }
4847                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4848                         {
4849                            if(type.kind == classType)
4850                            {
4851                               Class _class = type._class.registered;
4852                               if(_class.type == bitClass || _class.type == unitClass ||
4853                                  _class.type == enumClass)
4854                               {
4855                                  if(!_class.dataType)
4856                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4857                                  type = _class.dataType;
4858                               }
4859                            }
4860
4861                            if(dataMember)
4862                            {
4863                               void * ptr = inst.data + dataMemberOffset;
4864
4865                               if(value.type == constantExp)
4866                               {
4867                                  switch(type.kind)
4868                                  {
4869                                     case intType:
4870                                     {
4871                                        GetInt(value, (int*)ptr);
4872                                        break;
4873                                     }
4874                                     case int64Type:
4875                                     {
4876                                        GetInt64(value, (int64*)ptr);
4877                                        break;
4878                                     }
4879                                     case intPtrType:
4880                                     {
4881                                        GetIntPtr(value, (intptr*)ptr);
4882                                        break;
4883                                     }
4884                                     case intSizeType:
4885                                     {
4886                                        GetIntSize(value, (intsize*)ptr);
4887                                        break;
4888                                     }
4889                                     case floatType:
4890                                     {
4891                                        GetFloat(value, (float*)ptr);
4892                                        break;
4893                                     }
4894                                     case doubleType:
4895                                     {
4896                                        GetDouble(value, (double *)ptr);
4897                                        break;
4898                                     }
4899                                  }
4900                               }
4901                               else if(value.type == instanceExp)
4902                               {
4903                                  if(type.kind == classType)
4904                                  {
4905                                     Class _class = type._class.registered;
4906                                     if(_class.type == structClass)
4907                                     {
4908                                        ComputeTypeSize(type);
4909                                        if(value.instance.data)
4910                                           memcpy(ptr, value.instance.data, type.size);
4911                                     }
4912                                  }
4913                               }
4914                            }
4915                            else if(prop)
4916                            {
4917                               if(value.type == instanceExp && value.instance.data)
4918                               {
4919                                  if(type.kind == classType)
4920                                  {
4921                                     Class _class = type._class.registered;
4922                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4923                                     {
4924                                        void (*Set)(void *, void *) = (void *)prop.Set;
4925                                        Set(inst.data, value.instance.data);
4926                                        PopulateInstance(inst);
4927                                     }
4928                                  }
4929                               }
4930                               else if(value.type == constantExp)
4931                               {
4932                                  switch(type.kind)
4933                                  {
4934                                     case doubleType:
4935                                     {
4936                                        void (*Set)(void *, double) = (void *)prop.Set;
4937                                        Set(inst.data, strtod(value.constant, null) );
4938                                        break;
4939                                     }
4940                                     case floatType:
4941                                     {
4942                                        void (*Set)(void *, float) = (void *)prop.Set;
4943                                        Set(inst.data, (float)(strtod(value.constant, null)));
4944                                        break;
4945                                     }
4946                                     case intType:
4947                                     {
4948                                        void (*Set)(void *, int) = (void *)prop.Set;
4949                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4950                                        break;
4951                                     }
4952                                     case int64Type:
4953                                     {
4954                                        void (*Set)(void *, int64) = (void *)prop.Set;
4955                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4956                                        break;
4957                                     }
4958                                     case intPtrType:
4959                                     {
4960                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4961                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4962                                        break;
4963                                     }
4964                                     case intSizeType:
4965                                     {
4966                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4967                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4968                                        break;
4969                                     }
4970                                  }
4971                               }
4972                               else if(value.type == stringExp)
4973                               {
4974                                  char temp[1024];
4975                                  ReadString(temp, value.string);
4976                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4977                               }
4978                            }
4979                         }
4980                         else if(!deepMember && type && _class.type == unitClass)
4981                         {
4982                            if(prop)
4983                            {
4984                               // Only support converting units to units for now...
4985                               if(value.type == constantExp)
4986                               {
4987                                  if(type.kind == classType)
4988                                  {
4989                                     Class _class = type._class.registered;
4990                                     if(_class.type == unitClass)
4991                                     {
4992                                        if(!_class.dataType)
4993                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4994                                        type = _class.dataType;
4995                                     }
4996                                  }
4997                                  // TODO: Assuming same base type for units...
4998                                  switch(type.kind)
4999                                  {
5000                                     case floatType:
5001                                     {
5002                                        float fValue;
5003                                        float (*Set)(float) = (void *)prop.Set;
5004                                        GetFloat(member.initializer.exp, &fValue);
5005                                        exp.constant = PrintFloat(Set(fValue));
5006                                        exp.type = constantExp;
5007                                        break;
5008                                     }
5009                                     case doubleType:
5010                                     {
5011                                        double dValue;
5012                                        double (*Set)(double) = (void *)prop.Set;
5013                                        GetDouble(member.initializer.exp, &dValue);
5014                                        exp.constant = PrintDouble(Set(dValue));
5015                                        exp.type = constantExp;
5016                                        break;
5017                                     }
5018                                  }
5019                               }
5020                            }
5021                         }
5022                         else if(!deepMember && type && _class.type == bitClass)
5023                         {
5024                            if(prop)
5025                            {
5026                               if(value.type == instanceExp && value.instance.data)
5027                               {
5028                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5029                                  bits = Set(value.instance.data);
5030                               }
5031                               else if(value.type == constantExp)
5032                               {
5033                               }
5034                            }
5035                            else if(dataMember)
5036                            {
5037                               BitMember bitMember = (BitMember) dataMember;
5038                               Type type;
5039                               uint64 part;
5040                               bits = (bits & ~bitMember.mask);
5041                               if(!bitMember.dataType)
5042                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5043                               type = bitMember.dataType;
5044                               if(type.kind == classType && type._class && type._class.registered)
5045                               {
5046                                  if(!type._class.registered.dataType)
5047                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5048                                  type = type._class.registered.dataType;
5049                               }
5050                               switch(type.kind)
5051                               {
5052                                  case _BoolType:
5053                                  case charType:       { byte v; type.isSigned ? GetChar(value, &v) : GetUChar(value, &v); part = (uint64)v; break; }
5054                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, &v) : GetUShort(value, &v); part = (uint64)v; break; }
5055                                  case intType:
5056                                  case longType:       { uint v; type.isSigned ? GetInt(value, &v) : GetUInt(value, &v); part = (uint64)v; break; }
5057                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, &v) : GetUInt64(value, &v); part = (uint64)v; break; }
5058                                  case intPtrType:     { intptr v; type.isSigned ? GetIntPtr(value, &v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5059                                  case intSizeType:    { intsize v; type.isSigned ? GetIntSize(value, &v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5060                               }
5061                               bits += part << bitMember.pos;
5062                            }
5063                         }
5064                      }
5065                      else
5066                      {
5067                         if(_class && _class.type == unitClass)
5068                         {
5069                            ComputeExpression(member.initializer.exp);
5070                            exp.constant = member.initializer.exp.constant;
5071                            exp.type = constantExp;
5072
5073                            member.initializer.exp.constant = null;
5074                         }
5075                      }
5076                   }
5077                }
5078                break;
5079             }
5080          }
5081       }
5082    }
5083    if(_class && _class.type == bitClass)
5084    {
5085       exp.constant = PrintHexUInt(bits);
5086       exp.type = constantExp;
5087    }
5088    if(exp.type != instanceExp)
5089    {
5090       FreeInstance(inst);
5091    }
5092 }
5093
5094 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5095 {
5096    bool result = false;
5097    switch(kind)
5098    {
5099       case shortType:
5100          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5101             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5102          break;
5103       case intType:
5104       case longType:
5105          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5106             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5107          break;
5108       case int64Type:
5109          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5110             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5111             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5112          break;
5113       case floatType:
5114          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5115             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5116             result = GetOpFloat(op, &op.f);
5117          break;
5118       case doubleType:
5119          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5120             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5121             result = GetOpDouble(op, &op.d);
5122          break;
5123       case pointerType:
5124          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5125             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5126             result = GetOpUIntPtr(op, &op.ui64);
5127          break;
5128       case enumType:
5129          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5130             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5131             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5132          break;
5133       case intPtrType:
5134          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5135             result = isSigned ? GetOpIntPtr(op, &op.i64) : GetOpUIntPtr(op, &op.i64);
5136          break;
5137       case intSizeType:
5138          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5139             result = isSigned ? GetOpIntSize(op, &op.ui64) : GetOpUIntSize(op, &op.ui64);
5140          break;
5141    }
5142    return result;
5143 }
5144
5145 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5146 {
5147    if(exp.op.op == SIZEOF)
5148    {
5149       FreeExpContents(exp);
5150       exp.type = constantExp;
5151       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5152    }
5153    else
5154    {
5155       if(!exp.op.exp1)
5156       {
5157          switch(exp.op.op)
5158          {
5159             // unary arithmetic
5160             case '+':
5161             {
5162                // Provide default unary +
5163                Expression exp2 = exp.op.exp2;
5164                exp.op.exp2 = null;
5165                FreeExpContents(exp);
5166                FreeType(exp.expType);
5167                FreeType(exp.destType);
5168                *exp = *exp2;
5169                delete exp2;
5170                break;
5171             }
5172             case '-':
5173                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5174                break;
5175             // unary arithmetic increment and decrement
5176                   //OPERATOR_ALL(UNARY, ++, Inc)
5177                   //OPERATOR_ALL(UNARY, --, Dec)
5178             // unary bitwise
5179             case '~':
5180                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5181                break;
5182             // unary logical negation
5183             case '!':
5184                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5185                break;
5186          }
5187       }
5188       else
5189       {
5190          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5191          {
5192             if(Promote(op2, op1.kind, op1.type.isSigned))
5193                op2.kind = op1.kind, op2.ops = op1.ops;
5194             else if(Promote(op1, op2.kind, op2.type.isSigned))
5195                op1.kind = op2.kind, op1.ops = op2.ops;
5196          }
5197          switch(exp.op.op)
5198          {
5199             // binary arithmetic
5200             case '+':
5201                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5202                break;
5203             case '-':
5204                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5205                break;
5206             case '*':
5207                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5208                break;
5209             case '/':
5210                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5211                break;
5212             case '%':
5213                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5214                break;
5215             // binary arithmetic assignment
5216                   //OPERATOR_ALL(BINARY, =, Asign)
5217                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5218                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5219                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5220                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5221                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5222             // binary bitwise
5223             case '&':
5224                if(exp.op.exp2)
5225                {
5226                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5227                }
5228                break;
5229             case '|':
5230                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5231                break;
5232             case '^':
5233                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5234                break;
5235             case LEFT_OP:
5236                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5237                break;
5238             case RIGHT_OP:
5239                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5240                break;
5241             // binary bitwise assignment
5242                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5243                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5244                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5245                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5246                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5247             // binary logical equality
5248             case EQ_OP:
5249                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5250                break;
5251             case NE_OP:
5252                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5253                break;
5254             // binary logical
5255             case AND_OP:
5256                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5257                break;
5258             case OR_OP:
5259                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5260                break;
5261             // binary logical relational
5262             case '>':
5263                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5264                break;
5265             case '<':
5266                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5267                break;
5268             case GE_OP:
5269                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5270                break;
5271             case LE_OP:
5272                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5273                break;
5274          }
5275       }
5276    }
5277 }
5278
5279 void ComputeExpression(Expression exp)
5280 {
5281    char expString[10240];
5282    expString[0] = '\0';
5283 #ifdef _DEBUG
5284    PrintExpression(exp, expString);
5285 #endif
5286
5287    switch(exp.type)
5288    {
5289       case instanceExp:
5290       {
5291          ComputeInstantiation(exp);
5292          break;
5293       }
5294       /*
5295       case constantExp:
5296          break;
5297       */
5298       case opExp:
5299       {
5300          Expression exp1, exp2 = null;
5301          Operand op1 { };
5302          Operand op2 { };
5303
5304          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5305          if(exp.op.exp2)
5306          {
5307             Expression e = exp.op.exp2;
5308
5309             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5310             {
5311                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5312                {
5313                   if(e.type == extensionCompoundExp)
5314                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5315                   else
5316                      e = e.list->last;
5317                }
5318             }
5319             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5320             {
5321                if(e.type == stringExp && e.string)
5322                {
5323                   char * string = e.string;
5324                   int len = strlen(string);
5325                   char * tmp = new char[len-2+1];
5326                   len = UnescapeString(tmp, string + 1, len - 2);
5327                   delete tmp;
5328                   FreeExpContents(exp);
5329                   exp.type = constantExp;
5330                   exp.constant = PrintUInt(len + 1);
5331                }
5332                else
5333                {
5334                   Type type = e.expType;
5335                   type.refCount++;
5336                   FreeExpContents(exp);
5337                   exp.type = constantExp;
5338                   exp.constant = PrintUInt(ComputeTypeSize(type));
5339                   FreeType(type);
5340                }
5341                break;
5342             }
5343             else
5344                ComputeExpression(exp.op.exp2);
5345          }
5346          if(exp.op.exp1)
5347          {
5348             ComputeExpression(exp.op.exp1);
5349             exp1 = exp.op.exp1;
5350             exp2 = exp.op.exp2;
5351             op1 = GetOperand(exp1);
5352             if(op1.type) op1.type.refCount++;
5353             if(exp2)
5354             {
5355                op2 = GetOperand(exp2);
5356                if(op2.type) op2.type.refCount++;
5357             }
5358          }
5359          else
5360          {
5361             exp1 = exp.op.exp2;
5362             op1 = GetOperand(exp1);
5363             if(op1.type) op1.type.refCount++;
5364          }
5365
5366          CallOperator(exp, exp1, exp2, op1, op2);
5367          /*
5368          switch(exp.op.op)
5369          {
5370             // Unary operators
5371             case '&':
5372                // Also binary
5373                if(exp.op.exp1 && exp.op.exp2)
5374                {
5375                   // Binary And
5376                   if(op1.ops.BitAnd)
5377                   {
5378                      FreeExpContents(exp);
5379                      op1.ops.BitAnd(exp, op1, op2);
5380                   }
5381                }
5382                break;
5383             case '*':
5384                if(exp.op.exp1)
5385                {
5386                   if(op1.ops.Mul)
5387                   {
5388                      FreeExpContents(exp);
5389                      op1.ops.Mul(exp, op1, op2);
5390                   }
5391                }
5392                break;
5393             case '+':
5394                if(exp.op.exp1)
5395                {
5396                   if(op1.ops.Add)
5397                   {
5398                      FreeExpContents(exp);
5399                      op1.ops.Add(exp, op1, op2);
5400                   }
5401                }
5402                else
5403                {
5404                   // Provide default unary +
5405                   Expression exp2 = exp.op.exp2;
5406                   exp.op.exp2 = null;
5407                   FreeExpContents(exp);
5408                   FreeType(exp.expType);
5409                   FreeType(exp.destType);
5410
5411                   *exp = *exp2;
5412                   delete exp2;
5413                }
5414                break;
5415             case '-':
5416                if(exp.op.exp1)
5417                {
5418                   if(op1.ops.Sub)
5419                   {
5420                      FreeExpContents(exp);
5421                      op1.ops.Sub(exp, op1, op2);
5422                   }
5423                }
5424                else
5425                {
5426                   if(op1.ops.Neg)
5427                   {
5428                      FreeExpContents(exp);
5429                      op1.ops.Neg(exp, op1);
5430                   }
5431                }
5432                break;
5433             case '~':
5434                if(op1.ops.BitNot)
5435                {
5436                   FreeExpContents(exp);
5437                   op1.ops.BitNot(exp, op1);
5438                }
5439                break;
5440             case '!':
5441                if(op1.ops.Not)
5442                {
5443                   FreeExpContents(exp);
5444                   op1.ops.Not(exp, op1);
5445                }
5446                break;
5447             // Binary only operators
5448             case '/':
5449                if(op1.ops.Div)
5450                {
5451                   FreeExpContents(exp);
5452                   op1.ops.Div(exp, op1, op2);
5453                }
5454                break;
5455             case '%':
5456                if(op1.ops.Mod)
5457                {
5458                   FreeExpContents(exp);
5459                   op1.ops.Mod(exp, op1, op2);
5460                }
5461                break;
5462             case LEFT_OP:
5463                break;
5464             case RIGHT_OP:
5465                break;
5466             case '<':
5467                if(exp.op.exp1)
5468                {
5469                   if(op1.ops.Sma)
5470                   {
5471                      FreeExpContents(exp);
5472                      op1.ops.Sma(exp, op1, op2);
5473                   }
5474                }
5475                break;
5476             case '>':
5477                if(exp.op.exp1)
5478                {
5479                   if(op1.ops.Grt)
5480                   {
5481                      FreeExpContents(exp);
5482                      op1.ops.Grt(exp, op1, op2);
5483                   }
5484                }
5485                break;
5486             case LE_OP:
5487                if(exp.op.exp1)
5488                {
5489                   if(op1.ops.SmaEqu)
5490                   {
5491                      FreeExpContents(exp);
5492                      op1.ops.SmaEqu(exp, op1, op2);
5493                   }
5494                }
5495                break;
5496             case GE_OP:
5497                if(exp.op.exp1)
5498                {
5499                   if(op1.ops.GrtEqu)
5500                   {
5501                      FreeExpContents(exp);
5502                      op1.ops.GrtEqu(exp, op1, op2);
5503                   }
5504                }
5505                break;
5506             case EQ_OP:
5507                if(exp.op.exp1)
5508                {
5509                   if(op1.ops.Equ)
5510                   {
5511                      FreeExpContents(exp);
5512                      op1.ops.Equ(exp, op1, op2);
5513                   }
5514                }
5515                break;
5516             case NE_OP:
5517                if(exp.op.exp1)
5518                {
5519                   if(op1.ops.Nqu)
5520                   {
5521                      FreeExpContents(exp);
5522                      op1.ops.Nqu(exp, op1, op2);
5523                   }
5524                }
5525                break;
5526             case '|':
5527                if(op1.ops.BitOr)
5528                {
5529                   FreeExpContents(exp);
5530                   op1.ops.BitOr(exp, op1, op2);
5531                }
5532                break;
5533             case '^':
5534                if(op1.ops.BitXor)
5535                {
5536                   FreeExpContents(exp);
5537                   op1.ops.BitXor(exp, op1, op2);
5538                }
5539                break;
5540             case AND_OP:
5541                break;
5542             case OR_OP:
5543                break;
5544             case SIZEOF:
5545                FreeExpContents(exp);
5546                exp.type = constantExp;
5547                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5548                break;
5549          }
5550          */
5551          if(op1.type) FreeType(op1.type);
5552          if(op2.type) FreeType(op2.type);
5553          break;
5554       }
5555       case bracketsExp:
5556       case extensionExpressionExp:
5557       {
5558          Expression e, n;
5559          for(e = exp.list->first; e; e = n)
5560          {
5561             n = e.next;
5562             if(!n)
5563             {
5564                OldList * list = exp.list;
5565                Expression prev = exp.prev;
5566                Expression next = exp.next;
5567                ComputeExpression(e);
5568                //FreeExpContents(exp);
5569                FreeType(exp.expType);
5570                FreeType(exp.destType);
5571                *exp = *e;
5572                exp.prev = prev;
5573                exp.next = next;
5574                delete e;
5575                delete list;
5576             }
5577             else
5578             {
5579                FreeExpression(e);
5580             }
5581          }
5582          break;
5583       }
5584       /*
5585
5586       case ExpIndex:
5587       {
5588          Expression e;
5589          exp.isConstant = true;
5590
5591          ComputeExpression(exp.index.exp);
5592          if(!exp.index.exp.isConstant)
5593             exp.isConstant = false;
5594
5595          for(e = exp.index.index->first; e; e = e.next)
5596          {
5597             ComputeExpression(e);
5598             if(!e.next)
5599             {
5600                // Check if this type is int
5601             }
5602             if(!e.isConstant)
5603                exp.isConstant = false;
5604          }
5605          exp.expType = Dereference(exp.index.exp.expType);
5606          break;
5607       }
5608       */
5609       case memberExp:
5610       {
5611          Expression memberExp = exp.member.exp;
5612          Identifier memberID = exp.member.member;
5613
5614          Type type;
5615          ComputeExpression(exp.member.exp);
5616          type = exp.member.exp.expType;
5617          if(type)
5618          {
5619             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);
5620             Property prop = null;
5621             DataMember member = null;
5622             Class convertTo = null;
5623             if(type.kind == subClassType && exp.member.exp.type == classExp)
5624                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5625
5626             if(!_class)
5627             {
5628                char string[256];
5629                Symbol classSym;
5630                string[0] = '\0';
5631                PrintTypeNoConst(type, string, false, true);
5632                classSym = FindClass(string);
5633                _class = classSym ? classSym.registered : null;
5634             }
5635
5636             if(exp.member.member)
5637             {
5638                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5639                if(!prop)
5640                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5641             }
5642             if(!prop && !member && _class && exp.member.member)
5643             {
5644                Symbol classSym = FindClass(exp.member.member.string);
5645                convertTo = _class;
5646                _class = classSym ? classSym.registered : null;
5647                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5648             }
5649
5650             if(prop)
5651             {
5652                if(prop.compiled)
5653                {
5654                   Type type = prop.dataType;
5655                   // TODO: Assuming same base type for units...
5656                   if(_class.type == unitClass)
5657                   {
5658                      if(type.kind == classType)
5659                      {
5660                         Class _class = type._class.registered;
5661                         if(_class.type == unitClass)
5662                         {
5663                            if(!_class.dataType)
5664                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5665                            type = _class.dataType;
5666                         }
5667                      }
5668                      switch(type.kind)
5669                      {
5670                         case floatType:
5671                         {
5672                            float value;
5673                            float (*Get)(float) = (void *)prop.Get;
5674                            GetFloat(exp.member.exp, &value);
5675                            exp.constant = PrintFloat(Get ? Get(value) : value);
5676                            exp.type = constantExp;
5677                            break;
5678                         }
5679                         case doubleType:
5680                         {
5681                            double value;
5682                            double (*Get)(double);
5683                            GetDouble(exp.member.exp, &value);
5684
5685                            if(convertTo)
5686                               Get = (void *)prop.Set;
5687                            else
5688                               Get = (void *)prop.Get;
5689                            exp.constant = PrintDouble(Get ? Get(value) : value);
5690                            exp.type = constantExp;
5691                            break;
5692                         }
5693                      }
5694                   }
5695                   else
5696                   {
5697                      if(convertTo)
5698                      {
5699                         Expression value = exp.member.exp;
5700                         Type type;
5701                         if(!prop.dataType)
5702                            ProcessPropertyType(prop);
5703
5704                         type = prop.dataType;
5705                         if(!type)
5706                         {
5707                             // printf("Investigate this\n");
5708                         }
5709                         else if(_class.type == structClass)
5710                         {
5711                            switch(type.kind)
5712                            {
5713                               case classType:
5714                               {
5715                                  Class propertyClass = type._class.registered;
5716                                  if(propertyClass.type == structClass && value.type == instanceExp)
5717                                  {
5718                                     void (*Set)(void *, void *) = (void *)prop.Set;
5719                                     exp.instance = Instantiation { };
5720                                     exp.instance.data = new0 byte[_class.structSize];
5721                                     exp.instance._class = MkSpecifierName(_class.fullName);
5722                                     exp.instance.loc = exp.loc;
5723                                     exp.type = instanceExp;
5724                                     Set(exp.instance.data, value.instance.data);
5725                                     PopulateInstance(exp.instance);
5726                                  }
5727                                  break;
5728                               }
5729                               case intType:
5730                               {
5731                                  int intValue;
5732                                  void (*Set)(void *, int) = (void *)prop.Set;
5733
5734                                  exp.instance = Instantiation { };
5735                                  exp.instance.data = new0 byte[_class.structSize];
5736                                  exp.instance._class = MkSpecifierName(_class.fullName);
5737                                  exp.instance.loc = exp.loc;
5738                                  exp.type = instanceExp;
5739
5740                                  GetInt(value, &intValue);
5741
5742                                  Set(exp.instance.data, intValue);
5743                                  PopulateInstance(exp.instance);
5744                                  break;
5745                               }
5746                               case int64Type:
5747                               {
5748                                  int64 intValue;
5749                                  void (*Set)(void *, int64) = (void *)prop.Set;
5750
5751                                  exp.instance = Instantiation { };
5752                                  exp.instance.data = new0 byte[_class.structSize];
5753                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5754                                  exp.instance.loc = exp.loc;
5755                                  exp.type = instanceExp;
5756
5757                                  GetInt64(value, &intValue);
5758
5759                                  Set(exp.instance.data, intValue);
5760                                  PopulateInstance(exp.instance);
5761                                  break;
5762                               }
5763                               case intPtrType:
5764                               {
5765                                  // TOFIX:
5766                                  intptr intValue;
5767                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5768
5769                                  exp.instance = Instantiation { };
5770                                  exp.instance.data = new0 byte[_class.structSize];
5771                                  exp.instance._class = MkSpecifierName(_class.fullName);
5772                                  exp.instance.loc = exp.loc;
5773                                  exp.type = instanceExp;
5774
5775                                  GetIntPtr(value, &intValue);
5776
5777                                  Set(exp.instance.data, intValue);
5778                                  PopulateInstance(exp.instance);
5779                                  break;
5780                               }
5781                               case intSizeType:
5782                               {
5783                                  // TOFIX:
5784                                  intsize intValue;
5785                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5786
5787                                  exp.instance = Instantiation { };
5788                                  exp.instance.data = new0 byte[_class.structSize];
5789                                  exp.instance._class = MkSpecifierName(_class.fullName);
5790                                  exp.instance.loc = exp.loc;
5791                                  exp.type = instanceExp;
5792
5793                                  GetIntSize(value, &intValue);
5794
5795                                  Set(exp.instance.data, intValue);
5796                                  PopulateInstance(exp.instance);
5797                                  break;
5798                               }
5799                               case floatType:
5800                               {
5801                                  float floatValue;
5802                                  void (*Set)(void *, float) = (void *)prop.Set;
5803
5804                                  exp.instance = Instantiation { };
5805                                  exp.instance.data = new0 byte[_class.structSize];
5806                                  exp.instance._class = MkSpecifierName(_class.fullName);
5807                                  exp.instance.loc = exp.loc;
5808                                  exp.type = instanceExp;
5809
5810                                  GetFloat(value, &floatValue);
5811
5812                                  Set(exp.instance.data, floatValue);
5813                                  PopulateInstance(exp.instance);
5814                                  break;
5815                               }
5816                               case doubleType:
5817                               {
5818                                  double doubleValue;
5819                                  void (*Set)(void *, double) = (void *)prop.Set;
5820
5821                                  exp.instance = Instantiation { };
5822                                  exp.instance.data = new0 byte[_class.structSize];
5823                                  exp.instance._class = MkSpecifierName(_class.fullName);
5824                                  exp.instance.loc = exp.loc;
5825                                  exp.type = instanceExp;
5826
5827                                  GetDouble(value, &doubleValue);
5828
5829                                  Set(exp.instance.data, doubleValue);
5830                                  PopulateInstance(exp.instance);
5831                                  break;
5832                               }
5833                            }
5834                         }
5835                         else if(_class.type == bitClass)
5836                         {
5837                            switch(type.kind)
5838                            {
5839                               case classType:
5840                               {
5841                                  Class propertyClass = type._class.registered;
5842                                  if(propertyClass.type == structClass && value.instance.data)
5843                                  {
5844                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5845                                     unsigned int bits = Set(value.instance.data);
5846                                     exp.constant = PrintHexUInt(bits);
5847                                     exp.type = constantExp;
5848                                     break;
5849                                  }
5850                                  else if(_class.type == bitClass)
5851                                  {
5852                                     unsigned int value;
5853                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5854                                     unsigned int bits;
5855
5856                                     GetUInt(exp.member.exp, &value);
5857                                     bits = Set(value);
5858                                     exp.constant = PrintHexUInt(bits);
5859                                     exp.type = constantExp;
5860                                  }
5861                               }
5862                            }
5863                         }
5864                      }
5865                      else
5866                      {
5867                         if(_class.type == bitClass)
5868                         {
5869                            unsigned int value;
5870                            GetUInt(exp.member.exp, &value);
5871
5872                            switch(type.kind)
5873                            {
5874                               case classType:
5875                               {
5876                                  Class _class = type._class.registered;
5877                                  if(_class.type == structClass)
5878                                  {
5879                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5880
5881                                     exp.instance = Instantiation { };
5882                                     exp.instance.data = new0 byte[_class.structSize];
5883                                     exp.instance._class = MkSpecifierName(_class.fullName);
5884                                     exp.instance.loc = exp.loc;
5885                                     //exp.instance.fullSet = true;
5886                                     exp.type = instanceExp;
5887                                     Get(value, exp.instance.data);
5888                                     PopulateInstance(exp.instance);
5889                                  }
5890                                  else if(_class.type == bitClass)
5891                                  {
5892                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5893                                     uint64 bits = Get(value);
5894                                     exp.constant = PrintHexUInt64(bits);
5895                                     exp.type = constantExp;
5896                                  }
5897                                  break;
5898                               }
5899                            }
5900                         }
5901                         else if(_class.type == structClass)
5902                         {
5903                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5904                            switch(type.kind)
5905                            {
5906                               case classType:
5907                               {
5908                                  Class _class = type._class.registered;
5909                                  if(_class.type == structClass && value)
5910                                  {
5911                                     void (*Get)(void *, void *) = (void *)prop.Get;
5912
5913                                     exp.instance = Instantiation { };
5914                                     exp.instance.data = new0 byte[_class.structSize];
5915                                     exp.instance._class = MkSpecifierName(_class.fullName);
5916                                     exp.instance.loc = exp.loc;
5917                                     //exp.instance.fullSet = true;
5918                                     exp.type = instanceExp;
5919                                     Get(value, exp.instance.data);
5920                                     PopulateInstance(exp.instance);
5921                                  }
5922                                  break;
5923                               }
5924                            }
5925                         }
5926                         /*else
5927                         {
5928                            char * value = exp.member.exp.instance.data;
5929                            switch(type.kind)
5930                            {
5931                               case classType:
5932                               {
5933                                  Class _class = type._class.registered;
5934                                  if(_class.type == normalClass)
5935                                  {
5936                                     void *(*Get)(void *) = (void *)prop.Get;
5937
5938                                     exp.instance = Instantiation { };
5939                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5940                                     exp.type = instanceExp;
5941                                     exp.instance.data = Get(value, exp.instance.data);
5942                                  }
5943                                  break;
5944                               }
5945                            }
5946                         }
5947                         */
5948                      }
5949                   }
5950                }
5951                else
5952                {
5953                   exp.isConstant = false;
5954                }
5955             }
5956             else if(member)
5957             {
5958             }
5959          }
5960
5961          if(exp.type != ExpressionType::memberExp)
5962          {
5963             FreeExpression(memberExp);
5964             FreeIdentifier(memberID);
5965          }
5966          break;
5967       }
5968       case typeSizeExp:
5969       {
5970          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5971          FreeExpContents(exp);
5972          exp.constant = PrintUInt(ComputeTypeSize(type));
5973          exp.type = constantExp;
5974          FreeType(type);
5975          break;
5976       }
5977       case classSizeExp:
5978       {
5979          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5980          if(classSym && classSym.registered)
5981          {
5982             if(classSym.registered.fixed)
5983             {
5984                FreeSpecifier(exp._class);
5985                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5986                exp.type = constantExp;
5987             }
5988             else
5989             {
5990                char className[1024];
5991                strcpy(className, "__ecereClass_");
5992                FullClassNameCat(className, classSym.string, true);
5993                MangleClassName(className);
5994
5995                DeclareClass(classSym, className);
5996
5997                FreeExpContents(exp);
5998                exp.type = pointerExp;
5999                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6000                exp.member.member = MkIdentifier("structSize");
6001             }
6002          }
6003          break;
6004       }
6005       case castExp:
6006       //case constantExp:
6007       {
6008          Type type;
6009          Expression e = exp;
6010          if(exp.type == castExp)
6011          {
6012             if(exp.cast.exp)
6013                ComputeExpression(exp.cast.exp);
6014             e = exp.cast.exp;
6015          }
6016          if(e && exp.expType)
6017          {
6018             /*if(exp.destType)
6019                type = exp.destType;
6020             else*/
6021                type = exp.expType;
6022             if(type.kind == classType)
6023             {
6024                Class _class = type._class.registered;
6025                if(_class && (_class.type == unitClass || _class.type == bitClass))
6026                {
6027                   if(!_class.dataType)
6028                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6029                   type = _class.dataType;
6030                }
6031             }
6032
6033             switch(type.kind)
6034             {
6035                case _BoolType:
6036                case charType:
6037                   if(type.isSigned)
6038                   {
6039                      char value = 0;
6040                      if(GetChar(e, &value))
6041                      {
6042                         FreeExpContents(exp);
6043                         exp.constant = PrintChar(value);
6044                         exp.type = constantExp;
6045                      }
6046                   }
6047                   else
6048                   {
6049                      unsigned char value = 0;
6050                      if(GetUChar(e, &value))
6051                      {
6052                         FreeExpContents(exp);
6053                         exp.constant = PrintUChar(value);
6054                         exp.type = constantExp;
6055                      }
6056                   }
6057                   break;
6058                case shortType:
6059                   if(type.isSigned)
6060                   {
6061                      short value = 0;
6062                      if(GetShort(e, &value))
6063                      {
6064                         FreeExpContents(exp);
6065                         exp.constant = PrintShort(value);
6066                         exp.type = constantExp;
6067                      }
6068                   }
6069                   else
6070                   {
6071                      unsigned short value = 0;
6072                      if(GetUShort(e, &value))
6073                      {
6074                         FreeExpContents(exp);
6075                         exp.constant = PrintUShort(value);
6076                         exp.type = constantExp;
6077                      }
6078                   }
6079                   break;
6080                case intType:
6081                   if(type.isSigned)
6082                   {
6083                      int value = 0;
6084                      if(GetInt(e, &value))
6085                      {
6086                         FreeExpContents(exp);
6087                         exp.constant = PrintInt(value);
6088                         exp.type = constantExp;
6089                      }
6090                   }
6091                   else
6092                   {
6093                      unsigned int value = 0;
6094                      if(GetUInt(e, &value))
6095                      {
6096                         FreeExpContents(exp);
6097                         exp.constant = PrintUInt(value);
6098                         exp.type = constantExp;
6099                      }
6100                   }
6101                   break;
6102                case int64Type:
6103                   if(type.isSigned)
6104                   {
6105                      int64 value = 0;
6106                      if(GetInt64(e, &value))
6107                      {
6108                         FreeExpContents(exp);
6109                         exp.constant = PrintInt64(value);
6110                         exp.type = constantExp;
6111                      }
6112                   }
6113                   else
6114                   {
6115                      uint64 value = 0;
6116                      if(GetUInt64(e, &value))
6117                      {
6118                         FreeExpContents(exp);
6119                         exp.constant = PrintUInt64(value);
6120                         exp.type = constantExp;
6121                      }
6122                   }
6123                   break;
6124                case intPtrType:
6125                   if(type.isSigned)
6126                   {
6127                      intptr value = 0;
6128                      if(GetIntPtr(e, &value))
6129                      {
6130                         FreeExpContents(exp);
6131                         exp.constant = PrintInt64((int64)value);
6132                         exp.type = constantExp;
6133                      }
6134                   }
6135                   else
6136                   {
6137                      uintptr value = 0;
6138                      if(GetUIntPtr(e, &value))
6139                      {
6140                         FreeExpContents(exp);
6141                         exp.constant = PrintUInt64((uint64)value);
6142                         exp.type = constantExp;
6143                      }
6144                   }
6145                   break;
6146                case intSizeType:
6147                   if(type.isSigned)
6148                   {
6149                      intsize value = 0;
6150                      if(GetIntSize(e, &value))
6151                      {
6152                         FreeExpContents(exp);
6153                         exp.constant = PrintInt64((int64)value);
6154                         exp.type = constantExp;
6155                      }
6156                   }
6157                   else
6158                   {
6159                      uintsize value = 0;
6160                      if(GetUIntSize(e, &value))
6161                      {
6162                         FreeExpContents(exp);
6163                         exp.constant = PrintUInt64((uint64)value);
6164                         exp.type = constantExp;
6165                      }
6166                   }
6167                   break;
6168                case floatType:
6169                {
6170                   float value = 0;
6171                   if(GetFloat(e, &value))
6172                   {
6173                      FreeExpContents(exp);
6174                      exp.constant = PrintFloat(value);
6175                      exp.type = constantExp;
6176                   }
6177                   break;
6178                }
6179                case doubleType:
6180                {
6181                   double value = 0;
6182                   if(GetDouble(e, &value))
6183                   {
6184                      FreeExpContents(exp);
6185                      exp.constant = PrintDouble(value);
6186                      exp.type = constantExp;
6187                   }
6188                   break;
6189                }
6190             }
6191          }
6192          break;
6193       }
6194       case conditionExp:
6195       {
6196          Operand op1 { };
6197          Operand op2 { };
6198          Operand op3 { };
6199
6200          if(exp.cond.exp)
6201             // Caring only about last expression for now...
6202             ComputeExpression(exp.cond.exp->last);
6203          if(exp.cond.elseExp)
6204             ComputeExpression(exp.cond.elseExp);
6205          if(exp.cond.cond)
6206             ComputeExpression(exp.cond.cond);
6207
6208          op1 = GetOperand(exp.cond.cond);
6209          if(op1.type) op1.type.refCount++;
6210          op2 = GetOperand(exp.cond.exp->last);
6211          if(op2.type) op2.type.refCount++;
6212          op3 = GetOperand(exp.cond.elseExp);
6213          if(op3.type) op3.type.refCount++;
6214
6215          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6216          if(op1.type) FreeType(op1.type);
6217          if(op2.type) FreeType(op2.type);
6218          if(op3.type) FreeType(op3.type);
6219          break;
6220       }
6221    }
6222 }
6223
6224 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
6225 {
6226    bool result = true;
6227    if(destType)
6228    {
6229       OldList converts { };
6230       Conversion convert;
6231
6232       if(destType.kind == voidType)
6233          return false;
6234
6235       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
6236          result = false;
6237       if(converts.count)
6238       {
6239          // for(convert = converts.last; convert; convert = convert.prev)
6240          for(convert = converts.first; convert; convert = convert.next)
6241          {
6242             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6243             if(!empty)
6244             {
6245                Expression newExp { };
6246                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6247
6248                // TODO: Check this...
6249                *newExp = *exp;
6250                newExp.prev = null;
6251                newExp.next = null;
6252                newExp.destType = null;
6253
6254                if(convert.isGet)
6255                {
6256                   // [exp].ColorRGB
6257                   exp.type = memberExp;
6258                   exp.addedThis = true;
6259                   exp.member.exp = newExp;
6260                   FreeType(exp.member.exp.expType);
6261
6262                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6263                   exp.member.exp.expType.classObjectType = objectType;
6264                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6265                   exp.member.memberType = propertyMember;
6266                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6267                   // TESTING THIS... for (int)degrees
6268                   exp.needCast = true;
6269                   if(exp.expType) exp.expType.refCount++;
6270                   ApplyAnyObjectLogic(exp.member.exp);
6271                }
6272                else
6273                {
6274
6275                   /*if(exp.isConstant)
6276                   {
6277                      // Color { ColorRGB = [exp] };
6278                      exp.type = instanceExp;
6279                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6280                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6281                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6282                   }
6283                   else*/
6284                   {
6285                      // If not constant, don't turn it yet into an instantiation
6286                      // (Go through the deep members system first)
6287                      exp.type = memberExp;
6288                      exp.addedThis = true;
6289                      exp.member.exp = newExp;
6290
6291                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6292                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6293                         newExp.expType._class.registered.type == noHeadClass)
6294                      {
6295                         newExp.byReference = true;
6296                      }
6297
6298                      FreeType(exp.member.exp.expType);
6299                      /*exp.member.exp.expType = convert.convert.dataType;
6300                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6301                      exp.member.exp.expType = null;
6302                      if(convert.convert.dataType)
6303                      {
6304                         exp.member.exp.expType = { };
6305                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6306                         exp.member.exp.expType.refCount = 1;
6307                         exp.member.exp.expType.classObjectType = objectType;
6308                         ApplyAnyObjectLogic(exp.member.exp);
6309                      }
6310
6311                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6312                      exp.member.memberType = reverseConversionMember;
6313                      exp.expType = convert.resultType ? convert.resultType :
6314                         MkClassType(convert.convert._class.fullName);
6315                      exp.needCast = true;
6316                      if(convert.resultType) convert.resultType.refCount++;
6317                   }
6318                }
6319             }
6320             else
6321             {
6322                FreeType(exp.expType);
6323                if(convert.isGet)
6324                {
6325                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6326                   exp.needCast = true;
6327                   if(exp.expType) exp.expType.refCount++;
6328                }
6329                else
6330                {
6331                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6332                   exp.needCast = true;
6333                   if(convert.resultType)
6334                      convert.resultType.refCount++;
6335                }
6336             }
6337          }
6338          if(exp.isConstant && inCompiler)
6339             ComputeExpression(exp);
6340
6341          converts.Free(FreeConvert);
6342       }
6343
6344       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6345       {
6346          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6347       }
6348       if(!result && exp.expType && exp.destType)
6349       {
6350          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6351              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6352             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6353             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6354             result = true;
6355       }
6356    }
6357    // if(result) CheckTemplateTypes(exp);
6358    return result;
6359 }
6360
6361 void CheckTemplateTypes(Expression exp)
6362 {
6363    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6364    {
6365       Expression newExp { };
6366       Statement compound;
6367       Context context;
6368       *newExp = *exp;
6369       if(exp.destType) exp.destType.refCount++;
6370       if(exp.expType)  exp.expType.refCount++;
6371       newExp.prev = null;
6372       newExp.next = null;
6373
6374       switch(exp.expType.kind)
6375       {
6376          case doubleType:
6377             if(exp.destType.classObjectType)
6378             {
6379                // We need to pass the address, just pass it along (Undo what was done above)
6380                if(exp.destType) exp.destType.refCount--;
6381                if(exp.expType)  exp.expType.refCount--;
6382                delete newExp;
6383             }
6384             else
6385             {
6386                // If we're looking for value:
6387                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6388                OldList * specs;
6389                OldList * unionDefs = MkList();
6390                OldList * statements = MkList();
6391                context = PushContext();
6392                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6393                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6394                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6395                exp.type = extensionCompoundExp;
6396                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6397                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6398                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6399                exp.compound.compound.context = context;
6400                PopContext(context);
6401             }
6402             break;
6403          default:
6404             exp.type = castExp;
6405             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6406             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6407             break;
6408       }
6409    }
6410    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6411    {
6412       Expression newExp { };
6413       Statement compound;
6414       Context context;
6415       *newExp = *exp;
6416       if(exp.destType) exp.destType.refCount++;
6417       if(exp.expType)  exp.expType.refCount++;
6418       newExp.prev = null;
6419       newExp.next = null;
6420
6421       switch(exp.expType.kind)
6422       {
6423          case doubleType:
6424             if(exp.destType.classObjectType)
6425             {
6426                // We need to pass the address, just pass it along (Undo what was done above)
6427                if(exp.destType) exp.destType.refCount--;
6428                if(exp.expType)  exp.expType.refCount--;
6429                delete newExp;
6430             }
6431             else
6432             {
6433                // If we're looking for value:
6434                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6435                OldList * specs;
6436                OldList * unionDefs = MkList();
6437                OldList * statements = MkList();
6438                context = PushContext();
6439                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6440                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6441                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6442                exp.type = extensionCompoundExp;
6443                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6444                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6445                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6446                exp.compound.compound.context = context;
6447                PopContext(context);
6448             }
6449             break;
6450          case classType:
6451          {
6452             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6453             {
6454                exp.type = bracketsExp;
6455                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6456                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6457                ProcessExpressionType(exp.list->first);
6458                break;
6459             }
6460             else
6461             {
6462                exp.type = bracketsExp;
6463                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6464                newExp.needCast = true;
6465                ProcessExpressionType(exp.list->first);
6466                break;
6467             }
6468          }
6469          default:
6470          {
6471             if(exp.expType.kind == templateType)
6472             {
6473                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6474                if(type)
6475                {
6476                   FreeType(exp.destType);
6477                   FreeType(exp.expType);
6478                   delete newExp;
6479                   break;
6480                }
6481             }
6482             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6483             {
6484                exp.type = opExp;
6485                exp.op.op = '*';
6486                exp.op.exp1 = null;
6487                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6488                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6489             }
6490             else
6491             {
6492                char typeString[1024];
6493                Declarator decl;
6494                OldList * specs = MkList();
6495                typeString[0] = '\0';
6496                PrintType(exp.expType, typeString, false, false);
6497                decl = SpecDeclFromString(typeString, specs, null);
6498
6499                exp.type = castExp;
6500                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6501                exp.cast.typeName = MkTypeName(specs, decl);
6502                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6503                exp.cast.exp.needCast = true;
6504             }
6505             break;
6506          }
6507       }
6508    }
6509 }
6510 // TODO: The Symbol tree should be reorganized by namespaces
6511 // Name Space:
6512 //    - Tree of all symbols within (stored without namespace)
6513 //    - Tree of sub-namespaces
6514
6515 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6516 {
6517    int nsLen = strlen(nameSpace);
6518    Symbol symbol;
6519    // Start at the name space prefix
6520    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6521    {
6522       char * s = symbol.string;
6523       if(!strncmp(s, nameSpace, nsLen))
6524       {
6525          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6526          int c;
6527          char * namePart;
6528          for(c = strlen(s)-1; c >= 0; c--)
6529             if(s[c] == ':')
6530                break;
6531
6532          namePart = s+c+1;
6533          if(!strcmp(namePart, name))
6534          {
6535             // TODO: Error on ambiguity
6536             return symbol;
6537          }
6538       }
6539       else
6540          break;
6541    }
6542    return null;
6543 }
6544
6545 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6546 {
6547    int c;
6548    char nameSpace[1024];
6549    char * namePart;
6550    bool gotColon = false;
6551
6552    nameSpace[0] = '\0';
6553    for(c = strlen(name)-1; c >= 0; c--)
6554       if(name[c] == ':')
6555       {
6556          gotColon = true;
6557          break;
6558       }
6559
6560    namePart = name+c+1;
6561    while(c >= 0 && name[c] == ':') c--;
6562    if(c >= 0)
6563    {
6564       // Try an exact match first
6565       Symbol symbol = (Symbol)tree.FindString(name);
6566       if(symbol)
6567          return symbol;
6568
6569       // Namespace specified
6570       memcpy(nameSpace, name, c + 1);
6571       nameSpace[c+1] = 0;
6572
6573       return ScanWithNameSpace(tree, nameSpace, namePart);
6574    }
6575    else if(gotColon)
6576    {
6577       // Looking for a global symbol, e.g. ::Sleep()
6578       Symbol symbol = (Symbol)tree.FindString(namePart);
6579       return symbol;
6580    }
6581    else
6582    {
6583       // Name only (no namespace specified)
6584       Symbol symbol = (Symbol)tree.FindString(namePart);
6585       if(symbol)
6586          return symbol;
6587       return ScanWithNameSpace(tree, "", namePart);
6588    }
6589    return null;
6590 }
6591
6592 static void ProcessDeclaration(Declaration decl);
6593
6594 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6595 {
6596 #ifdef _DEBUG
6597    //Time startTime = GetTime();
6598 #endif
6599    // Optimize this later? Do this before/less?
6600    Context ctx;
6601    Symbol symbol = null;
6602    // First, check if the identifier is declared inside the function
6603    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6604
6605    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6606    {
6607       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6608       {
6609          symbol = null;
6610          if(thisNameSpace)
6611          {
6612             char curName[1024];
6613             strcpy(curName, thisNameSpace);
6614             strcat(curName, "::");
6615             strcat(curName, name);
6616             // Try to resolve in current namespace first
6617             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6618          }
6619          if(!symbol)
6620             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6621       }
6622       else
6623          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6624
6625       if(symbol || ctx == endContext) break;
6626    }
6627    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6628    {
6629       if(symbol.pointerExternal.type == functionExternal)
6630       {
6631          FunctionDefinition function = symbol.pointerExternal.function;
6632
6633          // Modified this recently...
6634          Context tmpContext = curContext;
6635          curContext = null;
6636          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6637          curContext = tmpContext;
6638
6639          symbol.pointerExternal.symbol = symbol;
6640
6641          // TESTING THIS:
6642          DeclareType(symbol.type, true, true);
6643
6644          ast->Insert(curExternal.prev, symbol.pointerExternal);
6645
6646          symbol.id = curExternal.symbol.idCode;
6647
6648       }
6649       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6650       {
6651          ast->Move(symbol.pointerExternal, curExternal.prev);
6652          symbol.id = curExternal.symbol.idCode;
6653       }
6654    }
6655 #ifdef _DEBUG
6656    //findSymbolTotalTime += GetTime() - startTime;
6657 #endif
6658    return symbol;
6659 }
6660
6661 static void GetTypeSpecs(Type type, OldList * specs)
6662 {
6663    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6664    switch(type.kind)
6665    {
6666       case classType:
6667       {
6668          if(type._class.registered)
6669          {
6670             if(!type._class.registered.dataType)
6671                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6672             GetTypeSpecs(type._class.registered.dataType, specs);
6673          }
6674          break;
6675       }
6676       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6677       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6678       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6679       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6680       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6681       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6682       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6683       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6684       case intType:
6685       default:
6686          ListAdd(specs, MkSpecifier(INT)); break;
6687    }
6688 }
6689
6690 static void PrintArraySize(Type arrayType, char * string)
6691 {
6692    char size[256];
6693    size[0] = '\0';
6694    strcat(size, "[");
6695    if(arrayType.enumClass)
6696       strcat(size, arrayType.enumClass.string);
6697    else if(arrayType.arraySizeExp)
6698       PrintExpression(arrayType.arraySizeExp, size);
6699    strcat(size, "]");
6700    strcat(string, size);
6701 }
6702
6703 // WARNING : This function expects a null terminated string since it recursively concatenate...
6704 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6705 {
6706    if(type)
6707    {
6708       if(printConst && type.constant)
6709          strcat(string, "const ");
6710       switch(type.kind)
6711       {
6712          case classType:
6713          {
6714             Symbol c = type._class;
6715             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6716             //       look into merging with thisclass ?
6717             if(type.classObjectType == typedObject)
6718                strcat(string, "typed_object");
6719             else if(type.classObjectType == anyObject)
6720                strcat(string, "any_object");
6721             else
6722             {
6723                if(c && c.string)
6724                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6725             }
6726             if(type.byReference)
6727                strcat(string, " &");
6728             break;
6729          }
6730          case voidType: strcat(string, "void"); break;
6731          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6732          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6733          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6734          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6735          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6736          case _BoolType: strcat(string, "_Bool"); break;
6737          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6738          case floatType: strcat(string, "float"); break;
6739          case doubleType: strcat(string, "double"); break;
6740          case structType:
6741             if(type.enumName)
6742             {
6743                strcat(string, "struct ");
6744                strcat(string, type.enumName);
6745             }
6746             else if(type.typeName)
6747                strcat(string, type.typeName);
6748             else
6749             {
6750                Type member;
6751                strcat(string, "struct { ");
6752                for(member = type.members.first; member; member = member.next)
6753                {
6754                   PrintType(member, string, true, fullName);
6755                   strcat(string,"; ");
6756                }
6757                strcat(string,"}");
6758             }
6759             break;
6760          case unionType:
6761             if(type.enumName)
6762             {
6763                strcat(string, "union ");
6764                strcat(string, type.enumName);
6765             }
6766             else if(type.typeName)
6767                strcat(string, type.typeName);
6768             else
6769             {
6770                strcat(string, "union ");
6771                strcat(string,"(unnamed)");
6772             }
6773             break;
6774          case enumType:
6775             if(type.enumName)
6776             {
6777                strcat(string, "enum ");
6778                strcat(string, type.enumName);
6779             }
6780             else if(type.typeName)
6781                strcat(string, type.typeName);
6782             else
6783                strcat(string, "int"); // "enum");
6784             break;
6785          case ellipsisType:
6786             strcat(string, "...");
6787             break;
6788          case subClassType:
6789             strcat(string, "subclass(");
6790             strcat(string, type._class ? type._class.string : "int");
6791             strcat(string, ")");
6792             break;
6793          case templateType:
6794             strcat(string, type.templateParameter.identifier.string);
6795             break;
6796          case thisClassType:
6797             strcat(string, "thisclass");
6798             break;
6799          case vaListType:
6800             strcat(string, "__builtin_va_list");
6801             break;
6802       }
6803    }
6804 }
6805
6806 static void PrintName(Type type, char * string, bool fullName)
6807 {
6808    if(type.name && type.name[0])
6809    {
6810       if(fullName)
6811          strcat(string, type.name);
6812       else
6813       {
6814          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6815          if(name) name += 2; else name = type.name;
6816          strcat(string, name);
6817       }
6818    }
6819 }
6820
6821 static void PrintAttribs(Type type, char * string)
6822 {
6823    if(type)
6824    {
6825       if(type.dllExport)   strcat(string, "dllexport ");
6826       if(type.attrStdcall) strcat(string, "stdcall ");
6827    }
6828 }
6829
6830 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6831 {
6832    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6833    {
6834       Type attrType = null;
6835       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6836          PrintAttribs(type, string);
6837       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6838          strcat(string, " const");
6839       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6840       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6841          strcat(string, " (");
6842       if(type.kind == pointerType)
6843       {
6844          if(type.type.kind == functionType || type.type.kind == methodType)
6845             PrintAttribs(type.type, string);
6846       }
6847       if(type.kind == pointerType)
6848       {
6849          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6850             strcat(string, "*");
6851          else
6852             strcat(string, " *");
6853       }
6854       if(printConst && type.constant && type.kind == pointerType)
6855          strcat(string, " const");
6856    }
6857    else
6858       PrintTypeSpecs(type, string, fullName, printConst);
6859 }
6860
6861 static void PostPrintType(Type type, char * string, bool fullName)
6862 {
6863    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6864       strcat(string, ")");
6865    if(type.kind == arrayType)
6866       PrintArraySize(type, string);
6867    else if(type.kind == functionType)
6868    {
6869       Type param;
6870       strcat(string, "(");
6871       for(param = type.params.first; param; param = param.next)
6872       {
6873          PrintType(param, string, true, fullName);
6874          if(param.next) strcat(string, ", ");
6875       }
6876       strcat(string, ")");
6877    }
6878    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6879       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6880 }
6881
6882 // *****
6883 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6884 // *****
6885 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6886 {
6887    PrePrintType(type, string, fullName, null, printConst);
6888
6889    if(type.thisClass || (printName && type.name && type.name[0]))
6890       strcat(string, " ");
6891    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6892    {
6893       Symbol _class = type.thisClass;
6894       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6895       {
6896          if(type.classObjectType == classPointer)
6897             strcat(string, "class");
6898          else
6899             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6900       }
6901       else if(_class && _class.string)
6902       {
6903          String s = _class.string;
6904          if(fullName)
6905             strcat(string, s);
6906          else
6907          {
6908             char * name = RSearchString(s, "::", strlen(s), true, false);
6909             if(name) name += 2; else name = s;
6910             strcat(string, name);
6911          }
6912       }
6913       strcat(string, "::");
6914    }
6915
6916    if(printName && type.name)
6917       PrintName(type, string, fullName);
6918    PostPrintType(type, string, fullName);
6919    if(type.bitFieldCount)
6920    {
6921       char count[100];
6922       sprintf(count, ":%d", type.bitFieldCount);
6923       strcat(string, count);
6924    }
6925 }
6926
6927 void PrintType(Type type, char * string, bool printName, bool fullName)
6928 {
6929    _PrintType(type, string, printName, fullName, true);
6930 }
6931
6932 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6933 {
6934    _PrintType(type, string, printName, fullName, false);
6935 }
6936
6937 static Type FindMember(Type type, char * string)
6938 {
6939    Type memberType;
6940    for(memberType = type.members.first; memberType; memberType = memberType.next)
6941    {
6942       if(!memberType.name)
6943       {
6944          Type subType = FindMember(memberType, string);
6945          if(subType)
6946             return subType;
6947       }
6948       else if(!strcmp(memberType.name, string))
6949          return memberType;
6950    }
6951    return null;
6952 }
6953
6954 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6955 {
6956    Type memberType;
6957    for(memberType = type.members.first; memberType; memberType = memberType.next)
6958    {
6959       if(!memberType.name)
6960       {
6961          Type subType = FindMember(memberType, string);
6962          if(subType)
6963          {
6964             *offset += memberType.offset;
6965             return subType;
6966          }
6967       }
6968       else if(!strcmp(memberType.name, string))
6969       {
6970          *offset += memberType.offset;
6971          return memberType;
6972       }
6973    }
6974    return null;
6975 }
6976
6977 public bool GetParseError() { return parseError; }
6978
6979 Expression ParseExpressionString(char * expression)
6980 {
6981    parseError = false;
6982
6983    fileInput = TempFile { };
6984    fileInput.Write(expression, 1, strlen(expression));
6985    fileInput.Seek(0, start);
6986
6987    echoOn = false;
6988    parsedExpression = null;
6989    resetScanner();
6990    expression_yyparse();
6991    delete fileInput;
6992
6993    return parsedExpression;
6994 }
6995
6996 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6997 {
6998    Identifier id = exp.identifier;
6999    Method method = null;
7000    Property prop = null;
7001    DataMember member = null;
7002    ClassProperty classProp = null;
7003
7004    if(_class && _class.type == enumClass)
7005    {
7006       NamedLink value = null;
7007       Class enumClass = eSystem_FindClass(privateModule, "enum");
7008       if(enumClass)
7009       {
7010          Class baseClass;
7011          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7012          {
7013             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7014             for(value = e.values.first; value; value = value.next)
7015             {
7016                if(!strcmp(value.name, id.string))
7017                   break;
7018             }
7019             if(value)
7020             {
7021                char constant[256];
7022
7023                FreeExpContents(exp);
7024
7025                exp.type = constantExp;
7026                exp.isConstant = true;
7027                if(!strcmp(baseClass.dataTypeString, "int"))
7028                   sprintf(constant, "%d",(int)value.data);
7029                else
7030                   sprintf(constant, "0x%X",(int)value.data);
7031                exp.constant = CopyString(constant);
7032                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7033                exp.expType = MkClassType(baseClass.fullName);
7034                break;
7035             }
7036          }
7037       }
7038       if(value)
7039          return true;
7040    }
7041    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7042    {
7043       ProcessMethodType(method);
7044       exp.expType = Type
7045       {
7046          refCount = 1;
7047          kind = methodType;
7048          method = method;
7049          // Crash here?
7050          // TOCHECK: Put it back to what it was...
7051          // methodClass = _class;
7052          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7053       };
7054       //id._class = null;
7055       return true;
7056    }
7057    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7058    {
7059       if(!prop.dataType)
7060          ProcessPropertyType(prop);
7061       exp.expType = prop.dataType;
7062       if(prop.dataType) prop.dataType.refCount++;
7063       return true;
7064    }
7065    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7066    {
7067       if(!member.dataType)
7068          member.dataType = ProcessTypeString(member.dataTypeString, false);
7069       exp.expType = member.dataType;
7070       if(member.dataType) member.dataType.refCount++;
7071       return true;
7072    }
7073    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7074    {
7075       if(!classProp.dataType)
7076          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7077
7078       if(classProp.constant)
7079       {
7080          FreeExpContents(exp);
7081
7082          exp.isConstant = true;
7083          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7084          {
7085             //char constant[256];
7086             exp.type = stringExp;
7087             exp.constant = QMkString((char *)classProp.Get(_class));
7088          }
7089          else
7090          {
7091             char constant[256];
7092             exp.type = constantExp;
7093             sprintf(constant, "%d", (int)classProp.Get(_class));
7094             exp.constant = CopyString(constant);
7095          }
7096       }
7097       else
7098       {
7099          // TO IMPLEMENT...
7100       }
7101
7102       exp.expType = classProp.dataType;
7103       if(classProp.dataType) classProp.dataType.refCount++;
7104       return true;
7105    }
7106    return false;
7107 }
7108
7109 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7110 {
7111    BinaryTree * tree = &nameSpace.functions;
7112    GlobalData data = (GlobalData)tree->FindString(name);
7113    NameSpace * child;
7114    if(!data)
7115    {
7116       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7117       {
7118          data = ScanGlobalData(child, name);
7119          if(data)
7120             break;
7121       }
7122    }
7123    return data;
7124 }
7125
7126 static GlobalData FindGlobalData(char * name)
7127 {
7128    int start = 0, c;
7129    NameSpace * nameSpace;
7130    nameSpace = globalData;
7131    for(c = 0; name[c]; c++)
7132    {
7133       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7134       {
7135          NameSpace * newSpace;
7136          char * spaceName = new char[c - start + 1];
7137          strncpy(spaceName, name + start, c - start);
7138          spaceName[c-start] = '\0';
7139          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7140          delete spaceName;
7141          if(!newSpace)
7142             return null;
7143          nameSpace = newSpace;
7144          if(name[c] == ':') c++;
7145          start = c+1;
7146       }
7147    }
7148    if(c - start)
7149    {
7150       return ScanGlobalData(nameSpace, name + start);
7151    }
7152    return null;
7153 }
7154
7155 static int definedExpStackPos;
7156 static void * definedExpStack[512];
7157
7158 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7159 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7160 {
7161    Expression prev = checkedExp.prev, next = checkedExp.next;
7162
7163    FreeExpContents(checkedExp);
7164    FreeType(checkedExp.expType);
7165    FreeType(checkedExp.destType);
7166
7167    *checkedExp = *newExp;
7168
7169    delete newExp;
7170
7171    checkedExp.prev = prev;
7172    checkedExp.next = next;
7173 }
7174
7175 void ApplyAnyObjectLogic(Expression e)
7176 {
7177    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7178 #ifdef _DEBUG
7179    char debugExpString[4096];
7180    debugExpString[0] = '\0';
7181    PrintExpression(e, debugExpString);
7182 #endif
7183
7184    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7185    {
7186       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7187       //ellipsisDestType = destType;
7188       if(e && e.expType)
7189       {
7190          Type type = e.expType;
7191          Class _class = null;
7192          //Type destType = e.destType;
7193
7194          if(type.kind == classType && type._class && type._class.registered)
7195          {
7196             _class = type._class.registered;
7197          }
7198          else if(type.kind == subClassType)
7199          {
7200             _class = FindClass("ecere::com::Class").registered;
7201          }
7202          else
7203          {
7204             char string[1024] = "";
7205             Symbol classSym;
7206
7207             PrintTypeNoConst(type, string, false, true);
7208             classSym = FindClass(string);
7209             if(classSym) _class = classSym.registered;
7210          }
7211
7212          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...
7213             (!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))) ||
7214             destType.byReference)))
7215          {
7216             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7217             {
7218                Expression checkedExp = e, newExp;
7219
7220                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7221                {
7222                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7223                   {
7224                      if(checkedExp.type == extensionCompoundExp)
7225                      {
7226                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7227                      }
7228                      else
7229                         checkedExp = checkedExp.list->last;
7230                   }
7231                   else if(checkedExp.type == castExp)
7232                      checkedExp = checkedExp.cast.exp;
7233                }
7234
7235                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7236                {
7237                   newExp = checkedExp.op.exp2;
7238                   checkedExp.op.exp2 = null;
7239                   FreeExpContents(checkedExp);
7240
7241                   if(e.expType && e.expType.passAsTemplate)
7242                   {
7243                      char size[100];
7244                      ComputeTypeSize(e.expType);
7245                      sprintf(size, "%d", e.expType.size);
7246                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7247                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7248                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7249                   }
7250
7251                   ReplaceExpContents(checkedExp, newExp);
7252                   e.byReference = true;
7253                }
7254                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7255                {
7256                   Expression checkedExp, newExp;
7257
7258                   {
7259                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7260                      bool hasAddress =
7261                         e.type == identifierExp ||
7262                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7263                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7264                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7265                         e.type == indexExp;
7266
7267                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7268                      {
7269                         Context context = PushContext();
7270                         Declarator decl;
7271                         OldList * specs = MkList();
7272                         char typeString[1024];
7273                         Expression newExp { };
7274
7275                         typeString[0] = '\0';
7276                         *newExp = *e;
7277
7278                         //if(e.destType) e.destType.refCount++;
7279                         // if(exp.expType) exp.expType.refCount++;
7280                         newExp.prev = null;
7281                         newExp.next = null;
7282                         newExp.expType = null;
7283
7284                         PrintTypeNoConst(e.expType, typeString, false, true);
7285                         decl = SpecDeclFromString(typeString, specs, null);
7286                         newExp.destType = ProcessType(specs, decl);
7287
7288                         curContext = context;
7289
7290                         // We need a current compound for this
7291                         if(curCompound)
7292                         {
7293                            char name[100];
7294                            OldList * stmts = MkList();
7295                            e.type = extensionCompoundExp;
7296                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7297                            if(!curCompound.compound.declarations)
7298                               curCompound.compound.declarations = MkList();
7299                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7300                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7301                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7302                            e.compound = MkCompoundStmt(null, stmts);
7303                         }
7304                         else
7305                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7306
7307                         /*
7308                         e.compound = MkCompoundStmt(
7309                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7310                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7311
7312                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7313                         */
7314
7315                         {
7316                            Type type = e.destType;
7317                            e.destType = { };
7318                            CopyTypeInto(e.destType, type);
7319                            e.destType.refCount = 1;
7320                            e.destType.classObjectType = none;
7321                            FreeType(type);
7322                         }
7323
7324                         e.compound.compound.context = context;
7325                         PopContext(context);
7326                         curContext = context.parent;
7327                      }
7328                   }
7329
7330                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7331                   checkedExp = e;
7332                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7333                   {
7334                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7335                      {
7336                         if(checkedExp.type == extensionCompoundExp)
7337                         {
7338                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7339                         }
7340                         else
7341                            checkedExp = checkedExp.list->last;
7342                      }
7343                      else if(checkedExp.type == castExp)
7344                         checkedExp = checkedExp.cast.exp;
7345                   }
7346                   {
7347                      Expression operand { };
7348                      operand = *checkedExp;
7349                      checkedExp.destType = null;
7350                      checkedExp.expType = null;
7351                      checkedExp.Clear();
7352                      checkedExp.type = opExp;
7353                      checkedExp.op.op = '&';
7354                      checkedExp.op.exp1 = null;
7355                      checkedExp.op.exp2 = operand;
7356
7357                      //newExp = MkExpOp(null, '&', checkedExp);
7358                   }
7359                   //ReplaceExpContents(checkedExp, newExp);
7360                }
7361             }
7362          }
7363       }
7364    }
7365    {
7366       // If expression type is a simple class, make it an address
7367       // FixReference(e, true);
7368    }
7369 //#if 0
7370    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7371       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7372          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7373    {
7374       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"))
7375       {
7376          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7377       }
7378       else
7379       {
7380          Expression thisExp { };
7381
7382          *thisExp = *e;
7383          thisExp.prev = null;
7384          thisExp.next = null;
7385          e.Clear();
7386
7387          e.type = bracketsExp;
7388          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7389          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7390             ((Expression)e.list->first).byReference = true;
7391
7392          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7393          {
7394             e.expType = thisExp.expType;
7395             e.expType.refCount++;
7396          }
7397          else*/
7398          {
7399             e.expType = { };
7400             CopyTypeInto(e.expType, thisExp.expType);
7401             e.expType.byReference = false;
7402             e.expType.refCount = 1;
7403
7404             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7405                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7406             {
7407                e.expType.classObjectType = none;
7408             }
7409          }
7410       }
7411    }
7412 // TOFIX: Try this for a nice IDE crash!
7413 //#endif
7414    // The other way around
7415    else
7416 //#endif
7417    if(destType && e.expType &&
7418          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7419          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7420          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7421    {
7422       if(destType.kind == ellipsisType)
7423       {
7424          Compiler_Error($"Unspecified type\n");
7425       }
7426       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7427       {
7428          bool byReference = e.expType.byReference;
7429          Expression thisExp { };
7430          Declarator decl;
7431          OldList * specs = MkList();
7432          char typeString[1024]; // Watch buffer overruns
7433          Type type;
7434          ClassObjectType backupClassObjectType;
7435          bool backupByReference;
7436
7437          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7438             type = e.expType;
7439          else
7440             type = destType;
7441
7442          backupClassObjectType = type.classObjectType;
7443          backupByReference = type.byReference;
7444
7445          type.classObjectType = none;
7446          type.byReference = false;
7447
7448          typeString[0] = '\0';
7449          PrintType(type, typeString, false, true);
7450          decl = SpecDeclFromString(typeString, specs, null);
7451
7452          type.classObjectType = backupClassObjectType;
7453          type.byReference = backupByReference;
7454
7455          *thisExp = *e;
7456          thisExp.prev = null;
7457          thisExp.next = null;
7458          e.Clear();
7459
7460          if( ( type.kind == classType && type._class && type._class.registered &&
7461                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7462                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7463              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7464              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7465          {
7466             e.type = opExp;
7467             e.op.op = '*';
7468             e.op.exp1 = null;
7469             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7470
7471             e.expType = { };
7472             CopyTypeInto(e.expType, type);
7473             e.expType.byReference = false;
7474             e.expType.refCount = 1;
7475          }
7476          else
7477          {
7478             e.type = castExp;
7479             e.cast.typeName = MkTypeName(specs, decl);
7480             e.cast.exp = thisExp;
7481             e.byReference = true;
7482             e.expType = type;
7483             type.refCount++;
7484          }
7485          e.destType = destType;
7486          destType.refCount++;
7487       }
7488    }
7489 }
7490
7491 void ProcessExpressionType(Expression exp)
7492 {
7493    bool unresolved = false;
7494    Location oldyylloc = yylloc;
7495    bool notByReference = false;
7496 #ifdef _DEBUG
7497    char debugExpString[4096];
7498    debugExpString[0] = '\0';
7499    PrintExpression(exp, debugExpString);
7500 #endif
7501    if(!exp || exp.expType)
7502       return;
7503
7504    //eSystem_Logf("%s\n", expString);
7505
7506    // Testing this here
7507    yylloc = exp.loc;
7508    switch(exp.type)
7509    {
7510       case identifierExp:
7511       {
7512          Identifier id = exp.identifier;
7513          if(!id || !topContext) return;
7514
7515          // DOING THIS LATER NOW...
7516          if(id._class && id._class.name)
7517          {
7518             id.classSym = id._class.symbol; // FindClass(id._class.name);
7519             /* TODO: Name Space Fix ups
7520             if(!id.classSym)
7521                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7522             */
7523          }
7524
7525          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7526          {
7527             exp.expType = ProcessTypeString("Module", true);
7528             break;
7529          }
7530          else */if(strstr(id.string, "__ecereClass") == id.string)
7531          {
7532             exp.expType = ProcessTypeString("ecere::com::Class", true);
7533             break;
7534          }
7535          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7536          {
7537             // Added this here as well
7538             ReplaceClassMembers(exp, thisClass);
7539             if(exp.type != identifierExp)
7540             {
7541                ProcessExpressionType(exp);
7542                break;
7543             }
7544
7545             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7546                break;
7547          }
7548          else
7549          {
7550             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7551             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7552             if(!symbol/* && exp.destType*/)
7553             {
7554                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7555                   break;
7556                else
7557                {
7558                   if(thisClass)
7559                   {
7560                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7561                      if(exp.type != identifierExp)
7562                      {
7563                         ProcessExpressionType(exp);
7564                         break;
7565                      }
7566                   }
7567                   // Static methods called from inside the _class
7568                   else if(currentClass && !id._class)
7569                   {
7570                      if(ResolveIdWithClass(exp, currentClass, true))
7571                         break;
7572                   }
7573                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7574                }
7575             }
7576
7577             // If we manage to resolve this symbol
7578             if(symbol)
7579             {
7580                Type type = symbol.type;
7581                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7582
7583                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7584                {
7585                   Context context = SetupTemplatesContext(_class);
7586                   type = ReplaceThisClassType(_class);
7587                   FinishTemplatesContext(context);
7588                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7589                }
7590
7591                FreeSpecifier(id._class);
7592                id._class = null;
7593                delete id.string;
7594                id.string = CopyString(symbol.string);
7595
7596                id.classSym = null;
7597                exp.expType = type;
7598                if(type)
7599                   type.refCount++;
7600                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7601                   // Add missing cases here... enum Classes...
7602                   exp.isConstant = true;
7603
7604                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7605                if(symbol.isParam || !strcmp(id.string, "this"))
7606                {
7607                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7608                      exp.byReference = true;
7609
7610                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7611                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7612                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7613                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7614                   {
7615                      Identifier id = exp.identifier;
7616                      exp.type = bracketsExp;
7617                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7618                   }*/
7619                }
7620
7621                if(symbol.isIterator)
7622                {
7623                   if(symbol.isIterator == 3)
7624                   {
7625                      exp.type = bracketsExp;
7626                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7627                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7628                      exp.expType = null;
7629                      ProcessExpressionType(exp);
7630                   }
7631                   else if(symbol.isIterator != 4)
7632                   {
7633                      exp.type = memberExp;
7634                      exp.member.exp = MkExpIdentifier(exp.identifier);
7635                      exp.member.exp.expType = exp.expType;
7636                      /*if(symbol.isIterator == 6)
7637                         exp.member.member = MkIdentifier("key");
7638                      else*/
7639                         exp.member.member = MkIdentifier("data");
7640                      exp.expType = null;
7641                      ProcessExpressionType(exp);
7642                   }
7643                }
7644                break;
7645             }
7646             else
7647             {
7648                DefinedExpression definedExp = null;
7649                if(thisNameSpace && !(id._class && !id._class.name))
7650                {
7651                   char name[1024];
7652                   strcpy(name, thisNameSpace);
7653                   strcat(name, "::");
7654                   strcat(name, id.string);
7655                   definedExp = eSystem_FindDefine(privateModule, name);
7656                }
7657                if(!definedExp)
7658                   definedExp = eSystem_FindDefine(privateModule, id.string);
7659                if(definedExp)
7660                {
7661                   int c;
7662                   for(c = 0; c<definedExpStackPos; c++)
7663                      if(definedExpStack[c] == definedExp)
7664                         break;
7665                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7666                   {
7667                      Location backupYylloc = yylloc;
7668                      File backInput = fileInput;
7669                      definedExpStack[definedExpStackPos++] = definedExp;
7670
7671                      fileInput = TempFile { };
7672                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7673                      fileInput.Seek(0, start);
7674
7675                      echoOn = false;
7676                      parsedExpression = null;
7677                      resetScanner();
7678                      expression_yyparse();
7679                      delete fileInput;
7680                      if(backInput)
7681                         fileInput = backInput;
7682
7683                      yylloc = backupYylloc;
7684
7685                      if(parsedExpression)
7686                      {
7687                         FreeIdentifier(id);
7688                         exp.type = bracketsExp;
7689                         exp.list = MkListOne(parsedExpression);
7690                         parsedExpression.loc = yylloc;
7691                         ProcessExpressionType(exp);
7692                         definedExpStackPos--;
7693                         return;
7694                      }
7695                      definedExpStackPos--;
7696                   }
7697                   else
7698                   {
7699                      if(inCompiler)
7700                      {
7701                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7702                      }
7703                   }
7704                }
7705                else
7706                {
7707                   GlobalData data = null;
7708                   if(thisNameSpace && !(id._class && !id._class.name))
7709                   {
7710                      char name[1024];
7711                      strcpy(name, thisNameSpace);
7712                      strcat(name, "::");
7713                      strcat(name, id.string);
7714                      data = FindGlobalData(name);
7715                   }
7716                   if(!data)
7717                      data = FindGlobalData(id.string);
7718                   if(data)
7719                   {
7720                      DeclareGlobalData(data);
7721                      exp.expType = data.dataType;
7722                      if(data.dataType) data.dataType.refCount++;
7723
7724                      delete id.string;
7725                      id.string = CopyString(data.fullName);
7726                      FreeSpecifier(id._class);
7727                      id._class = null;
7728
7729                      break;
7730                   }
7731                   else
7732                   {
7733                      GlobalFunction function = null;
7734                      if(thisNameSpace && !(id._class && !id._class.name))
7735                      {
7736                         char name[1024];
7737                         strcpy(name, thisNameSpace);
7738                         strcat(name, "::");
7739                         strcat(name, id.string);
7740                         function = eSystem_FindFunction(privateModule, name);
7741                      }
7742                      if(!function)
7743                         function = eSystem_FindFunction(privateModule, id.string);
7744                      if(function)
7745                      {
7746                         char name[1024];
7747                         delete id.string;
7748                         id.string = CopyString(function.name);
7749                         name[0] = 0;
7750
7751                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7752                            strcpy(name, "__ecereFunction_");
7753                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7754                         if(DeclareFunction(function, name))
7755                         {
7756                            delete id.string;
7757                            id.string = CopyString(name);
7758                         }
7759                         exp.expType = function.dataType;
7760                         if(function.dataType) function.dataType.refCount++;
7761
7762                         FreeSpecifier(id._class);
7763                         id._class = null;
7764
7765                         break;
7766                      }
7767                   }
7768                }
7769             }
7770          }
7771          unresolved = true;
7772          break;
7773       }
7774       case instanceExp:
7775       {
7776          Class _class;
7777          // Symbol classSym;
7778
7779          if(!exp.instance._class)
7780          {
7781             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7782             {
7783                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7784             }
7785          }
7786
7787          //classSym = FindClass(exp.instance._class.fullName);
7788          //_class = classSym ? classSym.registered : null;
7789
7790          ProcessInstantiationType(exp.instance);
7791          exp.isConstant = exp.instance.isConstant;
7792
7793          /*
7794          if(_class.type == unitClass && _class.base.type != systemClass)
7795          {
7796             {
7797                Type destType = exp.destType;
7798
7799                exp.destType = MkClassType(_class.base.fullName);
7800                exp.expType = MkClassType(_class.fullName);
7801                CheckExpressionType(exp, exp.destType, true);
7802
7803                exp.destType = destType;
7804             }
7805             exp.expType = MkClassType(_class.fullName);
7806          }
7807          else*/
7808          if(exp.instance._class)
7809          {
7810             exp.expType = MkClassType(exp.instance._class.name);
7811             /*if(exp.expType._class && exp.expType._class.registered &&
7812                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7813                exp.expType.byReference = true;*/
7814          }
7815          break;
7816       }
7817       case constantExp:
7818       {
7819          if(!exp.expType)
7820          {
7821             char * constant = exp.constant;
7822             Type type
7823             {
7824                refCount = 1;
7825                constant = true;
7826             };
7827             exp.expType = type;
7828
7829             if(constant[0] == '\'')
7830             {
7831                if((int)((byte *)constant)[1] > 127)
7832                {
7833                   int nb;
7834                   unichar ch = UTF8GetChar(constant + 1, &nb);
7835                   if(nb < 2) ch = constant[1];
7836                   delete constant;
7837                   exp.constant = PrintUInt(ch);
7838                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7839                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7840                   type._class = FindClass("unichar");
7841
7842                   type.isSigned = false;
7843                }
7844                else
7845                {
7846                   type.kind = charType;
7847                   type.isSigned = true;
7848                }
7849             }
7850             else
7851             {
7852                char * dot = strchr(constant, '.');
7853                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7854                char * exponent;
7855                if(isHex)
7856                {
7857                   exponent = strchr(constant, 'p');
7858                   if(!exponent) exponent = strchr(constant, 'P');
7859                }
7860                else
7861                {
7862                   exponent = strchr(constant, 'e');
7863                   if(!exponent) exponent = strchr(constant, 'E');
7864                }
7865
7866                if(dot || exponent)
7867                {
7868                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7869                      type.kind = floatType;
7870                   else
7871                      type.kind = doubleType;
7872                   type.isSigned = true;
7873                }
7874                else
7875                {
7876                   bool isSigned = constant[0] == '-';
7877                   char * endP = null;
7878                   int64 i64 = strtoll(constant, &endP, 0);
7879                   uint64 ui64 = strtoull(constant, &endP, 0);
7880                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
7881                   if(isSigned)
7882                   {
7883                      if(i64 < MININT)
7884                         is64Bit = true;
7885                   }
7886                   else
7887                   {
7888                      if(ui64 > MAXINT)
7889                      {
7890                         if(ui64 > MAXDWORD)
7891                         {
7892                            is64Bit = true;
7893                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7894                               isSigned = true;
7895                         }
7896                      }
7897                      else if(constant[0] != '0' || !constant[1])
7898                         isSigned = true;
7899                   }
7900                   type.kind = is64Bit ? int64Type : intType;
7901                   type.isSigned = isSigned;
7902                }
7903             }
7904             exp.isConstant = true;
7905             if(exp.destType && exp.destType.kind == doubleType)
7906                type.kind = doubleType;
7907             else if(exp.destType && exp.destType.kind == floatType)
7908                type.kind = floatType;
7909             else if(exp.destType && exp.destType.kind == int64Type)
7910                type.kind = int64Type;
7911          }
7912          break;
7913       }
7914       case stringExp:
7915       {
7916          exp.isConstant = true;      // Why wasn't this constant?
7917          exp.expType = Type
7918          {
7919             refCount = 1;
7920             kind = pointerType;
7921             type = Type
7922             {
7923                refCount = 1;
7924                kind = charType;
7925                constant = true;
7926                isSigned = true;
7927             }
7928          };
7929          break;
7930       }
7931       case newExp:
7932       case new0Exp:
7933          ProcessExpressionType(exp._new.size);
7934          exp.expType = Type
7935          {
7936             refCount = 1;
7937             kind = pointerType;
7938             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7939          };
7940          DeclareType(exp.expType.type, false, false);
7941          break;
7942       case renewExp:
7943       case renew0Exp:
7944          ProcessExpressionType(exp._renew.size);
7945          ProcessExpressionType(exp._renew.exp);
7946          exp.expType = Type
7947          {
7948             refCount = 1;
7949             kind = pointerType;
7950             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7951          };
7952          DeclareType(exp.expType.type, false, false);
7953          break;
7954       case opExp:
7955       {
7956          bool assign = false, boolResult = false, boolOps = false;
7957          Type type1 = null, type2 = null;
7958          bool useDestType = false, useSideType = false;
7959          Location oldyylloc = yylloc;
7960          bool useSideUnit = false;
7961
7962          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7963          Type dummy
7964          {
7965             count = 1;
7966             refCount = 1;
7967          };
7968
7969          switch(exp.op.op)
7970          {
7971             // Assignment Operators
7972             case '=':
7973             case MUL_ASSIGN:
7974             case DIV_ASSIGN:
7975             case MOD_ASSIGN:
7976             case ADD_ASSIGN:
7977             case SUB_ASSIGN:
7978             case LEFT_ASSIGN:
7979             case RIGHT_ASSIGN:
7980             case AND_ASSIGN:
7981             case XOR_ASSIGN:
7982             case OR_ASSIGN:
7983                assign = true;
7984                break;
7985             // boolean Operators
7986             case '!':
7987                // Expect boolean operators
7988                //boolOps = true;
7989                //boolResult = true;
7990                break;
7991             case AND_OP:
7992             case OR_OP:
7993                // Expect boolean operands
7994                boolOps = true;
7995                boolResult = true;
7996                break;
7997             // Comparisons
7998             case EQ_OP:
7999             case '<':
8000             case '>':
8001             case LE_OP:
8002             case GE_OP:
8003             case NE_OP:
8004                // Gives boolean result
8005                boolResult = true;
8006                useSideType = true;
8007                break;
8008             case '+':
8009             case '-':
8010                useSideUnit = true;
8011
8012                // Just added these... testing
8013             case '|':
8014             case '&':
8015             case '^':
8016
8017             // DANGER: Verify units
8018             case '/':
8019             case '%':
8020             case '*':
8021
8022                if(exp.op.op != '*' || exp.op.exp1)
8023                {
8024                   useSideType = true;
8025                   useDestType = true;
8026                }
8027                break;
8028
8029             /*// Implement speed etc.
8030             case '*':
8031             case '/':
8032                break;
8033             */
8034          }
8035          if(exp.op.op == '&')
8036          {
8037             // Added this here earlier for Iterator address as key
8038             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8039             {
8040                Identifier id = exp.op.exp2.identifier;
8041                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8042                if(symbol && symbol.isIterator == 2)
8043                {
8044                   exp.type = memberExp;
8045                   exp.member.exp = exp.op.exp2;
8046                   exp.member.member = MkIdentifier("key");
8047                   exp.expType = null;
8048                   exp.op.exp2.expType = symbol.type;
8049                   symbol.type.refCount++;
8050                   ProcessExpressionType(exp);
8051                   FreeType(dummy);
8052                   break;
8053                }
8054                // exp.op.exp2.usage.usageRef = true;
8055             }
8056          }
8057
8058          //dummy.kind = TypeDummy;
8059
8060          if(exp.op.exp1)
8061          {
8062             if(exp.destType && exp.destType.kind == classType &&
8063                exp.destType._class && exp.destType._class.registered && useDestType &&
8064
8065               ((exp.destType._class.registered.type == unitClass && useSideUnit) ||
8066                exp.destType._class.registered.type == enumClass ||
8067                exp.destType._class.registered.type == bitClass
8068                ))
8069
8070               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8071             {
8072                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8073                exp.op.exp1.destType = exp.destType;
8074                if(exp.destType)
8075                   exp.destType.refCount++;
8076             }
8077             else if(!assign)
8078             {
8079                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8080                exp.op.exp1.destType = dummy;
8081                dummy.refCount++;
8082             }
8083
8084             // TESTING THIS HERE...
8085             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8086             ProcessExpressionType(exp.op.exp1);
8087             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8088
8089             // Fix for unit and ++ / --
8090             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8091                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8092             {
8093                exp.op.exp2 = MkExpConstant("1");
8094                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8095                assign = true;
8096             }
8097
8098             if(exp.op.exp1.destType == dummy)
8099             {
8100                FreeType(dummy);
8101                exp.op.exp1.destType = null;
8102             }
8103             type1 = exp.op.exp1.expType;
8104          }
8105
8106          if(exp.op.exp2)
8107          {
8108             char expString[10240];
8109             expString[0] = '\0';
8110             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8111             {
8112                if(exp.op.exp1)
8113                {
8114                   exp.op.exp2.destType = exp.op.exp1.expType;
8115                   if(exp.op.exp1.expType)
8116                      exp.op.exp1.expType.refCount++;
8117                }
8118                else
8119                {
8120                   exp.op.exp2.destType = exp.destType;
8121                   if(exp.destType)
8122                      exp.destType.refCount++;
8123                }
8124
8125                if(type1) type1.refCount++;
8126                exp.expType = type1;
8127             }
8128             else if(assign)
8129             {
8130                if(inCompiler)
8131                   PrintExpression(exp.op.exp2, expString);
8132
8133                if(type1 && type1.kind == pointerType)
8134                {
8135                   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 ||
8136                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8137                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8138                   else if(exp.op.op == '=')
8139                   {
8140                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8141                      exp.op.exp2.destType = type1;
8142                      if(type1)
8143                         type1.refCount++;
8144                   }
8145                }
8146                else
8147                {
8148                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8149                   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/* ||
8150                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8151                   else
8152                   {
8153                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8154                      exp.op.exp2.destType = type1;
8155                      if(type1)
8156                         type1.refCount++;
8157                   }
8158                }
8159                if(type1) type1.refCount++;
8160                exp.expType = type1;
8161             }
8162             else if(exp.destType && exp.destType.kind == classType &&
8163                exp.destType._class && exp.destType._class.registered &&
8164
8165                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) ||
8166                   (exp.destType._class.registered.type == enumClass && useDestType))
8167                   )
8168             {
8169                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8170                exp.op.exp2.destType = exp.destType;
8171                if(exp.destType)
8172                   exp.destType.refCount++;
8173             }
8174             else
8175             {
8176                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8177                exp.op.exp2.destType = dummy;
8178                dummy.refCount++;
8179             }
8180
8181             // TESTING THIS HERE... (DANGEROUS)
8182             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8183                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8184             {
8185                FreeType(exp.op.exp2.destType);
8186                exp.op.exp2.destType = type1;
8187                type1.refCount++;
8188             }
8189             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8190             // Cannot lose the cast on a sizeof
8191             if(exp.op.op == SIZEOF)
8192             {
8193                Expression e = exp.op.exp2;
8194                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8195                {
8196                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8197                   {
8198                      if(e.type == extensionCompoundExp)
8199                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8200                      else
8201                         e = e.list->last;
8202                   }
8203                }
8204                if(e.type == castExp && e.cast.exp)
8205                   e.cast.exp.needCast = true;
8206             }
8207             ProcessExpressionType(exp.op.exp2);
8208             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8209
8210             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8211             {
8212                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)
8213                {
8214                   if(exp.op.op != '=' && type1.type.kind == voidType)
8215                      Compiler_Error($"void *: unknown size\n");
8216                }
8217                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||
8218                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8219                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8220                               exp.op.exp2.expType._class.registered.type == structClass ||
8221                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8222                {
8223                   if(exp.op.op == ADD_ASSIGN)
8224                      Compiler_Error($"cannot add two pointers\n");
8225                }
8226                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8227                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8228                {
8229                   if(exp.op.op == ADD_ASSIGN)
8230                      Compiler_Error($"cannot add two pointers\n");
8231                }
8232                else if(inCompiler)
8233                {
8234                   char type1String[1024];
8235                   char type2String[1024];
8236                   type1String[0] = '\0';
8237                   type2String[0] = '\0';
8238
8239                   PrintType(exp.op.exp2.expType, type1String, false, true);
8240                   PrintType(type1, type2String, false, true);
8241                   ChangeCh(expString, '\n', ' ');
8242                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8243                }
8244             }
8245
8246             if(exp.op.exp2.destType == dummy)
8247             {
8248                FreeType(dummy);
8249                exp.op.exp2.destType = null;
8250             }
8251
8252             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8253             {
8254                type2 = { };
8255                type2.refCount = 1;
8256                CopyTypeInto(type2, exp.op.exp2.expType);
8257                type2.isSigned = true;
8258             }
8259             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8260             {
8261                type2 = { kind = intType };
8262                type2.refCount = 1;
8263                type2.isSigned = true;
8264             }
8265             else
8266             {
8267                type2 = exp.op.exp2.expType;
8268                if(type2) type2.refCount++;
8269             }
8270          }
8271
8272          dummy.kind = voidType;
8273
8274          if(exp.op.op == SIZEOF)
8275          {
8276             exp.expType = Type
8277             {
8278                refCount = 1;
8279                kind = intType;
8280             };
8281             exp.isConstant = true;
8282          }
8283          // Get type of dereferenced pointer
8284          else if(exp.op.op == '*' && !exp.op.exp1)
8285          {
8286             exp.expType = Dereference(type2);
8287             if(type2 && type2.kind == classType)
8288                notByReference = true;
8289          }
8290          else if(exp.op.op == '&' && !exp.op.exp1)
8291             exp.expType = Reference(type2);
8292          else if(!assign)
8293          {
8294             if(boolOps)
8295             {
8296                if(exp.op.exp1)
8297                {
8298                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8299                   exp.op.exp1.destType = MkClassType("bool");
8300                   exp.op.exp1.destType.truth = true;
8301                   if(!exp.op.exp1.expType)
8302                      ProcessExpressionType(exp.op.exp1);
8303                   else
8304                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8305                   FreeType(exp.op.exp1.expType);
8306                   exp.op.exp1.expType = MkClassType("bool");
8307                   exp.op.exp1.expType.truth = true;
8308                }
8309                if(exp.op.exp2)
8310                {
8311                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8312                   exp.op.exp2.destType = MkClassType("bool");
8313                   exp.op.exp2.destType.truth = true;
8314                   if(!exp.op.exp2.expType)
8315                      ProcessExpressionType(exp.op.exp2);
8316                   else
8317                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8318                   FreeType(exp.op.exp2.expType);
8319                   exp.op.exp2.expType = MkClassType("bool");
8320                   exp.op.exp2.expType.truth = true;
8321                }
8322             }
8323             else if(exp.op.exp1 && exp.op.exp2 &&
8324                ((useSideType /*&&
8325                      (useSideUnit ||
8326                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8327                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8328                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8329                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8330             {
8331                if(type1 && type2 &&
8332                   // If either both are class or both are not class
8333                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8334                {
8335                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8336                   exp.op.exp2.destType = type1;
8337                   type1.refCount++;
8338                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8339                   exp.op.exp1.destType = type2;
8340                   type2.refCount++;
8341                   // Warning here for adding Radians + Degrees with no destination type
8342                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8343                      type1._class.registered && type1._class.registered.type == unitClass &&
8344                      type2._class.registered && type2._class.registered.type == unitClass &&
8345                      type1._class.registered != type2._class.registered)
8346                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8347                         type1._class.string, type2._class.string, type1._class.string);
8348
8349                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8350                   {
8351                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8352                      if(argExp)
8353                      {
8354                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8355
8356                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8357                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8358                            exp.op.exp1)));
8359
8360                         ProcessExpressionType(exp.op.exp1);
8361
8362                         if(type2.kind != pointerType)
8363                         {
8364                            ProcessExpressionType(classExp);
8365
8366                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8367                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8368                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8369                                  // noHeadClass
8370                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8371                                     OR_OP,
8372                                  // normalClass
8373                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8374                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8375                                        MkPointer(null, null), null)))),
8376                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8377
8378                            if(!exp.op.exp2.expType)
8379                            {
8380                               if(type2)
8381                                  FreeType(type2);
8382                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8383                               type2.refCount++;
8384                            }
8385
8386                            ProcessExpressionType(exp.op.exp2);
8387                         }
8388                      }
8389                   }
8390
8391                   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)))
8392                   {
8393                      if(type1.kind != classType && type1.type.kind == voidType)
8394                         Compiler_Error($"void *: unknown size\n");
8395                      exp.expType = type1;
8396                      if(type1) type1.refCount++;
8397                   }
8398                   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)))
8399                   {
8400                      if(type2.kind != classType && type2.type.kind == voidType)
8401                         Compiler_Error($"void *: unknown size\n");
8402                      exp.expType = type2;
8403                      if(type2) type2.refCount++;
8404                   }
8405                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8406                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8407                   {
8408                      Compiler_Warning($"different levels of indirection\n");
8409                   }
8410                   else
8411                   {
8412                      bool success = false;
8413                      if(type1.kind == pointerType && type2.kind == pointerType)
8414                      {
8415                         if(exp.op.op == '+')
8416                            Compiler_Error($"cannot add two pointers\n");
8417                         else if(exp.op.op == '-')
8418                         {
8419                            // Pointer Subtraction gives integer
8420                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8421                            {
8422                               exp.expType = Type
8423                               {
8424                                  kind = intType;
8425                                  refCount = 1;
8426                               };
8427                               success = true;
8428
8429                               if(type1.type.kind == templateType)
8430                               {
8431                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8432                                  if(argExp)
8433                                  {
8434                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8435
8436                                     ProcessExpressionType(classExp);
8437
8438                                     exp.type = bracketsExp;
8439                                     exp.list = MkListOne(MkExpOp(
8440                                        MkExpBrackets(MkListOne(MkExpOp(
8441                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8442                                              , exp.op.op,
8443                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8444
8445                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8446
8447                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8448                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8449                                                 // noHeadClass
8450                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8451                                                    OR_OP,
8452                                                 // normalClass
8453                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8454                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8455                                                       MkPointer(null, null), null)))),
8456                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8457
8458
8459                                              ));
8460
8461                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8462                                     FreeType(dummy);
8463                                     return;
8464                                  }
8465                               }
8466                            }
8467                         }
8468                      }
8469
8470                      if(!success && exp.op.exp1.type == constantExp)
8471                      {
8472                         // If first expression is constant, try to match that first
8473                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8474                         {
8475                            if(exp.expType) FreeType(exp.expType);
8476                            exp.expType = exp.op.exp1.destType;
8477                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8478                            success = true;
8479                         }
8480                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8481                         {
8482                            if(exp.expType) FreeType(exp.expType);
8483                            exp.expType = exp.op.exp2.destType;
8484                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8485                            success = true;
8486                         }
8487                      }
8488                      else if(!success)
8489                      {
8490                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8491                         {
8492                            if(exp.expType) FreeType(exp.expType);
8493                            exp.expType = exp.op.exp2.destType;
8494                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8495                            success = true;
8496                         }
8497                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8498                         {
8499                            if(exp.expType) FreeType(exp.expType);
8500                            exp.expType = exp.op.exp1.destType;
8501                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8502                            success = true;
8503                         }
8504                      }
8505                      if(!success)
8506                      {
8507                         char expString1[10240];
8508                         char expString2[10240];
8509                         char type1[1024];
8510                         char type2[1024];
8511                         expString1[0] = '\0';
8512                         expString2[0] = '\0';
8513                         type1[0] = '\0';
8514                         type2[0] = '\0';
8515                         if(inCompiler)
8516                         {
8517                            PrintExpression(exp.op.exp1, expString1);
8518                            ChangeCh(expString1, '\n', ' ');
8519                            PrintExpression(exp.op.exp2, expString2);
8520                            ChangeCh(expString2, '\n', ' ');
8521                            PrintType(exp.op.exp1.expType, type1, false, true);
8522                            PrintType(exp.op.exp2.expType, type2, false, true);
8523                         }
8524
8525                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8526                      }
8527                   }
8528                }
8529                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8530                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8531                {
8532                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8533                   // Convert e.g. / 4 into / 4.0
8534                   exp.op.exp1.destType = type2._class.registered.dataType;
8535                   if(type2._class.registered.dataType)
8536                      type2._class.registered.dataType.refCount++;
8537                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8538                   exp.expType = type2;
8539                   if(type2) type2.refCount++;
8540                }
8541                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8542                {
8543                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8544                   // Convert e.g. / 4 into / 4.0
8545                   exp.op.exp2.destType = type1._class.registered.dataType;
8546                   if(type1._class.registered.dataType)
8547                      type1._class.registered.dataType.refCount++;
8548                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8549                   exp.expType = type1;
8550                   if(type1) type1.refCount++;
8551                }
8552                else if(type1)
8553                {
8554                   bool valid = false;
8555
8556                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8557                   {
8558                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8559
8560                      if(!type1._class.registered.dataType)
8561                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8562                      exp.op.exp2.destType = type1._class.registered.dataType;
8563                      exp.op.exp2.destType.refCount++;
8564
8565                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8566                      if(type2)
8567                         FreeType(type2);
8568                      type2 = exp.op.exp2.destType;
8569                      if(type2) type2.refCount++;
8570
8571                      exp.expType = type2;
8572                      type2.refCount++;
8573                   }
8574
8575                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8576                   {
8577                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8578
8579                      if(!type2._class.registered.dataType)
8580                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8581                      exp.op.exp1.destType = type2._class.registered.dataType;
8582                      exp.op.exp1.destType.refCount++;
8583
8584                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8585                      type1 = exp.op.exp1.destType;
8586                      exp.expType = type1;
8587                      type1.refCount++;
8588                   }
8589
8590                   // TESTING THIS NEW CODE
8591                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8592                   {
8593                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8594                      {
8595                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8596                         {
8597                            if(exp.expType) FreeType(exp.expType);
8598                            exp.expType = exp.op.exp1.expType;
8599                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8600                            valid = true;
8601                         }
8602                      }
8603
8604                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8605                      {
8606                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8607                         {
8608                            if(exp.expType) FreeType(exp.expType);
8609                            exp.expType = exp.op.exp2.expType;
8610                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8611                            valid = true;
8612                         }
8613                      }
8614                   }
8615
8616                   if(!valid)
8617                   {
8618                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8619                      exp.op.exp2.destType = type1;
8620                      type1.refCount++;
8621
8622                      /*
8623                      // Maybe this was meant to be an enum...
8624                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8625                      {
8626                         Type oldType = exp.op.exp2.expType;
8627                         exp.op.exp2.expType = null;
8628                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8629                            FreeType(oldType);
8630                         else
8631                            exp.op.exp2.expType = oldType;
8632                      }
8633                      */
8634
8635                      /*
8636                      // TESTING THIS HERE... LATEST ADDITION
8637                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8638                      {
8639                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8640                         exp.op.exp2.destType = type2._class.registered.dataType;
8641                         if(type2._class.registered.dataType)
8642                            type2._class.registered.dataType.refCount++;
8643                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8644
8645                         //exp.expType = type2._class.registered.dataType; //type2;
8646                         //if(type2) type2.refCount++;
8647                      }
8648
8649                      // TESTING THIS HERE... LATEST ADDITION
8650                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8651                      {
8652                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8653                         exp.op.exp1.destType = type1._class.registered.dataType;
8654                         if(type1._class.registered.dataType)
8655                            type1._class.registered.dataType.refCount++;
8656                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8657                         exp.expType = type1._class.registered.dataType; //type1;
8658                         if(type1) type1.refCount++;
8659                      }
8660                      */
8661
8662                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8663                      {
8664                         if(exp.expType) FreeType(exp.expType);
8665                         exp.expType = exp.op.exp2.destType;
8666                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8667                      }
8668                      else if(type1 && type2)
8669                      {
8670                         char expString1[10240];
8671                         char expString2[10240];
8672                         char type1String[1024];
8673                         char type2String[1024];
8674                         expString1[0] = '\0';
8675                         expString2[0] = '\0';
8676                         type1String[0] = '\0';
8677                         type2String[0] = '\0';
8678                         if(inCompiler)
8679                         {
8680                            PrintExpression(exp.op.exp1, expString1);
8681                            ChangeCh(expString1, '\n', ' ');
8682                            PrintExpression(exp.op.exp2, expString2);
8683                            ChangeCh(expString2, '\n', ' ');
8684                            PrintType(exp.op.exp1.expType, type1String, false, true);
8685                            PrintType(exp.op.exp2.expType, type2String, false, true);
8686                         }
8687
8688                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8689
8690                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8691                         {
8692                            exp.expType = exp.op.exp1.expType;
8693                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8694                         }
8695                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8696                         {
8697                            exp.expType = exp.op.exp2.expType;
8698                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8699                         }
8700                      }
8701                   }
8702                }
8703                else if(type2)
8704                {
8705                   // Maybe this was meant to be an enum...
8706                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8707                   {
8708                      Type oldType = exp.op.exp1.expType;
8709                      exp.op.exp1.expType = null;
8710                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8711                         FreeType(oldType);
8712                      else
8713                         exp.op.exp1.expType = oldType;
8714                   }
8715
8716                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8717                   exp.op.exp1.destType = type2;
8718                   type2.refCount++;
8719                   /*
8720                   // TESTING THIS HERE... LATEST ADDITION
8721                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8722                   {
8723                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8724                      exp.op.exp1.destType = type1._class.registered.dataType;
8725                      if(type1._class.registered.dataType)
8726                         type1._class.registered.dataType.refCount++;
8727                   }
8728
8729                   // TESTING THIS HERE... LATEST ADDITION
8730                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8731                   {
8732                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8733                      exp.op.exp2.destType = type2._class.registered.dataType;
8734                      if(type2._class.registered.dataType)
8735                         type2._class.registered.dataType.refCount++;
8736                   }
8737                   */
8738
8739                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8740                   {
8741                      if(exp.expType) FreeType(exp.expType);
8742                      exp.expType = exp.op.exp1.destType;
8743                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8744                   }
8745                }
8746             }
8747             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8748             {
8749                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8750                {
8751                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8752                   // Convert e.g. / 4 into / 4.0
8753                   exp.op.exp1.destType = type2._class.registered.dataType;
8754                   if(type2._class.registered.dataType)
8755                      type2._class.registered.dataType.refCount++;
8756                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8757                }
8758                if(exp.op.op == '!')
8759                {
8760                   exp.expType = MkClassType("bool");
8761                   exp.expType.truth = true;
8762                }
8763                else
8764                {
8765                   exp.expType = type2;
8766                   if(type2) type2.refCount++;
8767                }
8768             }
8769             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8770             {
8771                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8772                {
8773                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8774                   // Convert e.g. / 4 into / 4.0
8775                   exp.op.exp2.destType = type1._class.registered.dataType;
8776                   if(type1._class.registered.dataType)
8777                      type1._class.registered.dataType.refCount++;
8778                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8779                }
8780                exp.expType = type1;
8781                if(type1) type1.refCount++;
8782             }
8783          }
8784
8785          yylloc = exp.loc;
8786          if(exp.op.exp1 && !exp.op.exp1.expType)
8787          {
8788             char expString[10000];
8789             expString[0] = '\0';
8790             if(inCompiler)
8791             {
8792                PrintExpression(exp.op.exp1, expString);
8793                ChangeCh(expString, '\n', ' ');
8794             }
8795             if(expString[0])
8796                Compiler_Error($"couldn't determine type of %s\n", expString);
8797          }
8798          if(exp.op.exp2 && !exp.op.exp2.expType)
8799          {
8800             char expString[10240];
8801             expString[0] = '\0';
8802             if(inCompiler)
8803             {
8804                PrintExpression(exp.op.exp2, expString);
8805                ChangeCh(expString, '\n', ' ');
8806             }
8807             if(expString[0])
8808                Compiler_Error($"couldn't determine type of %s\n", expString);
8809          }
8810
8811          if(boolResult)
8812          {
8813             FreeType(exp.expType);
8814             exp.expType = MkClassType("bool");
8815             exp.expType.truth = true;
8816          }
8817
8818          if(exp.op.op != SIZEOF)
8819             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8820                (!exp.op.exp2 || exp.op.exp2.isConstant);
8821
8822          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8823          {
8824             DeclareType(exp.op.exp2.expType, false, false);
8825          }
8826
8827          yylloc = oldyylloc;
8828
8829          FreeType(dummy);
8830          if(type2)
8831             FreeType(type2);
8832          break;
8833       }
8834       case bracketsExp:
8835       case extensionExpressionExp:
8836       {
8837          Expression e;
8838          exp.isConstant = true;
8839          for(e = exp.list->first; e; e = e.next)
8840          {
8841             bool inced = false;
8842             if(!e.next)
8843             {
8844                FreeType(e.destType);
8845                e.destType = exp.destType;
8846                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8847             }
8848             ProcessExpressionType(e);
8849             if(inced)
8850                exp.destType.count--;
8851             if(!exp.expType && !e.next)
8852             {
8853                exp.expType = e.expType;
8854                if(e.expType) e.expType.refCount++;
8855             }
8856             if(!e.isConstant)
8857                exp.isConstant = false;
8858          }
8859
8860          // In case a cast became a member...
8861          e = exp.list->first;
8862          if(!e.next && e.type == memberExp)
8863          {
8864             // Preserve prev, next
8865             Expression next = exp.next, prev = exp.prev;
8866
8867
8868             FreeType(exp.expType);
8869             FreeType(exp.destType);
8870             delete exp.list;
8871
8872             *exp = *e;
8873
8874             exp.prev = prev;
8875             exp.next = next;
8876
8877             delete e;
8878
8879             ProcessExpressionType(exp);
8880          }
8881          break;
8882       }
8883       case indexExp:
8884       {
8885          Expression e;
8886          exp.isConstant = true;
8887
8888          ProcessExpressionType(exp.index.exp);
8889          if(!exp.index.exp.isConstant)
8890             exp.isConstant = false;
8891
8892          if(exp.index.exp.expType)
8893          {
8894             Type source = exp.index.exp.expType;
8895             if(source.kind == classType && source._class && source._class.registered)
8896             {
8897                Class _class = source._class.registered;
8898                Class c = _class.templateClass ? _class.templateClass : _class;
8899                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
8900                {
8901                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8902
8903                   if(exp.index.index && exp.index.index->last)
8904                   {
8905                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8906                   }
8907                }
8908             }
8909          }
8910
8911          for(e = exp.index.index->first; e; e = e.next)
8912          {
8913             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8914             {
8915                if(e.destType) FreeType(e.destType);
8916                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8917             }
8918             ProcessExpressionType(e);
8919             if(!e.next)
8920             {
8921                // Check if this type is int
8922             }
8923             if(!e.isConstant)
8924                exp.isConstant = false;
8925          }
8926
8927          if(!exp.expType)
8928             exp.expType = Dereference(exp.index.exp.expType);
8929          if(exp.expType)
8930             DeclareType(exp.expType, false, false);
8931          break;
8932       }
8933       case callExp:
8934       {
8935          Expression e;
8936          Type functionType;
8937          Type methodType = null;
8938          char name[1024];
8939          name[0] = '\0';
8940
8941          if(inCompiler)
8942          {
8943             PrintExpression(exp.call.exp,  name);
8944             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8945             {
8946                //exp.call.exp.expType = null;
8947                PrintExpression(exp.call.exp,  name);
8948             }
8949          }
8950          if(exp.call.exp.type == identifierExp)
8951          {
8952             Expression idExp = exp.call.exp;
8953             Identifier id = idExp.identifier;
8954             if(!strcmp(id.string, "__builtin_frame_address"))
8955             {
8956                exp.expType = ProcessTypeString("void *", true);
8957                if(exp.call.arguments && exp.call.arguments->first)
8958                   ProcessExpressionType(exp.call.arguments->first);
8959                break;
8960             }
8961             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8962             {
8963                exp.expType = ProcessTypeString("int", true);
8964                if(exp.call.arguments && exp.call.arguments->first)
8965                   ProcessExpressionType(exp.call.arguments->first);
8966                break;
8967             }
8968             else if(!strcmp(id.string, "Max") ||
8969                !strcmp(id.string, "Min") ||
8970                !strcmp(id.string, "Sgn") ||
8971                !strcmp(id.string, "Abs"))
8972             {
8973                Expression a = null;
8974                Expression b = null;
8975                Expression tempExp1 = null, tempExp2 = null;
8976                if((!strcmp(id.string, "Max") ||
8977                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8978                {
8979                   a = exp.call.arguments->first;
8980                   b = exp.call.arguments->last;
8981                   tempExp1 = a;
8982                   tempExp2 = b;
8983                }
8984                else if(exp.call.arguments->count == 1)
8985                {
8986                   a = exp.call.arguments->first;
8987                   tempExp1 = a;
8988                }
8989
8990                if(a)
8991                {
8992                   exp.call.arguments->Clear();
8993                   idExp.identifier = null;
8994
8995                   FreeExpContents(exp);
8996
8997                   ProcessExpressionType(a);
8998                   if(b)
8999                      ProcessExpressionType(b);
9000
9001                   exp.type = bracketsExp;
9002                   exp.list = MkList();
9003
9004                   if(a.expType && (!b || b.expType))
9005                   {
9006                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9007                      {
9008                         // Use the simpleStruct name/ids for now...
9009                         if(inCompiler)
9010                         {
9011                            OldList * specs = MkList();
9012                            OldList * decls = MkList();
9013                            Declaration decl;
9014                            char temp1[1024], temp2[1024];
9015
9016                            GetTypeSpecs(a.expType, specs);
9017
9018                            if(a && !a.isConstant && a.type != identifierExp)
9019                            {
9020                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9021                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9022                               tempExp1 = QMkExpId(temp1);
9023                               tempExp1.expType = a.expType;
9024                               if(a.expType)
9025                                  a.expType.refCount++;
9026                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9027                            }
9028                            if(b && !b.isConstant && b.type != identifierExp)
9029                            {
9030                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9031                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9032                               tempExp2 = QMkExpId(temp2);
9033                               tempExp2.expType = b.expType;
9034                               if(b.expType)
9035                                  b.expType.refCount++;
9036                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9037                            }
9038
9039                            decl = MkDeclaration(specs, decls);
9040                            if(!curCompound.compound.declarations)
9041                               curCompound.compound.declarations = MkList();
9042                            curCompound.compound.declarations->Insert(null, decl);
9043                         }
9044                      }
9045                   }
9046
9047                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9048                   {
9049                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9050                      ListAdd(exp.list,
9051                         MkExpCondition(MkExpBrackets(MkListOne(
9052                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9053                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9054                      exp.expType = a.expType;
9055                      if(a.expType)
9056                         a.expType.refCount++;
9057                   }
9058                   else if(!strcmp(id.string, "Abs"))
9059                   {
9060                      ListAdd(exp.list,
9061                         MkExpCondition(MkExpBrackets(MkListOne(
9062                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9063                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9064                      exp.expType = a.expType;
9065                      if(a.expType)
9066                         a.expType.refCount++;
9067                   }
9068                   else if(!strcmp(id.string, "Sgn"))
9069                   {
9070                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9071                      ListAdd(exp.list,
9072                         MkExpCondition(MkExpBrackets(MkListOne(
9073                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9074                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9075                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9076                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9077                      exp.expType = ProcessTypeString("int", false);
9078                   }
9079
9080                   FreeExpression(tempExp1);
9081                   if(tempExp2) FreeExpression(tempExp2);
9082
9083                   FreeIdentifier(id);
9084                   break;
9085                }
9086             }
9087          }
9088
9089          {
9090             Type dummy
9091             {
9092                count = 1;
9093                refCount = 1;
9094             };
9095             if(!exp.call.exp.destType)
9096             {
9097                exp.call.exp.destType = dummy;
9098                dummy.refCount++;
9099             }
9100             ProcessExpressionType(exp.call.exp);
9101             if(exp.call.exp.destType == dummy)
9102             {
9103                FreeType(dummy);
9104                exp.call.exp.destType = null;
9105             }
9106             FreeType(dummy);
9107          }
9108
9109          // Check argument types against parameter types
9110          functionType = exp.call.exp.expType;
9111
9112          if(functionType && functionType.kind == TypeKind::methodType)
9113          {
9114             methodType = functionType;
9115             functionType = methodType.method.dataType;
9116
9117             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9118             // TOCHECK: Instead of doing this here could this be done per param?
9119             if(exp.call.exp.expType.usedClass)
9120             {
9121                char typeString[1024];
9122                typeString[0] = '\0';
9123                {
9124                   Symbol back = functionType.thisClass;
9125                   // Do not output class specifier here (thisclass was added to this)
9126                   functionType.thisClass = null;
9127                   PrintType(functionType, typeString, true, true);
9128                   functionType.thisClass = back;
9129                }
9130                if(strstr(typeString, "thisclass"))
9131                {
9132                   OldList * specs = MkList();
9133                   Declarator decl;
9134                   {
9135                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9136
9137                      decl = SpecDeclFromString(typeString, specs, null);
9138
9139                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9140                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9141                         exp.call.exp.expType.usedClass))
9142                         thisClassParams = false;
9143
9144                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9145                      {
9146                         Class backupThisClass = thisClass;
9147                         thisClass = exp.call.exp.expType.usedClass;
9148                         ProcessDeclarator(decl);
9149                         thisClass = backupThisClass;
9150                      }
9151
9152                      thisClassParams = true;
9153
9154                      functionType = ProcessType(specs, decl);
9155                      functionType.refCount = 0;
9156                      FinishTemplatesContext(context);
9157                   }
9158
9159                   FreeList(specs, FreeSpecifier);
9160                   FreeDeclarator(decl);
9161                 }
9162             }
9163          }
9164          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9165          {
9166             Type type = functionType.type;
9167             if(!functionType.refCount)
9168             {
9169                functionType.type = null;
9170                FreeType(functionType);
9171             }
9172             //methodType = functionType;
9173             functionType = type;
9174          }
9175          if(functionType && functionType.kind != TypeKind::functionType)
9176          {
9177             Compiler_Error($"called object %s is not a function\n", name);
9178          }
9179          else if(functionType)
9180          {
9181             bool emptyParams = false, noParams = false;
9182             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9183             Type type = functionType.params.first;
9184             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9185             int extra = 0;
9186             Location oldyylloc = yylloc;
9187
9188             if(!type) emptyParams = true;
9189
9190             // WORKING ON THIS:
9191             if(functionType.extraParam && e && functionType.thisClass)
9192             {
9193                e.destType = MkClassType(functionType.thisClass.string);
9194                e = e.next;
9195             }
9196
9197             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9198             // Fixed #141 by adding '&& !functionType.extraParam'
9199             if(!functionType.staticMethod && !functionType.extraParam)
9200             {
9201                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9202                   memberExp.member.exp.expType._class)
9203                {
9204                   type = MkClassType(memberExp.member.exp.expType._class.string);
9205                   if(e)
9206                   {
9207                      e.destType = type;
9208                      e = e.next;
9209                      type = functionType.params.first;
9210                   }
9211                   else
9212                      type.refCount = 0;
9213                }
9214                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9215                {
9216                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9217                   type.byReference = functionType.byReference;
9218                   type.typedByReference = functionType.typedByReference;
9219                   if(e)
9220                   {
9221                      // Allow manually passing a class for typed object
9222                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9223                         e = e.next;
9224                      e.destType = type;
9225                      e = e.next;
9226                      type = functionType.params.first;
9227                   }
9228                   else
9229                      type.refCount = 0;
9230                   //extra = 1;
9231                }
9232             }
9233
9234             if(type && type.kind == voidType)
9235             {
9236                noParams = true;
9237                if(!type.refCount) FreeType(type);
9238                type = null;
9239             }
9240
9241             for( ; e; e = e.next)
9242             {
9243                if(!type && !emptyParams)
9244                {
9245                   yylloc = e.loc;
9246                   if(methodType && methodType.methodClass)
9247                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9248                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9249                         noParams ? 0 : functionType.params.count);
9250                   else
9251                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9252                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9253                         noParams ? 0 : functionType.params.count);
9254                   break;
9255                }
9256
9257                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9258                {
9259                   Type templatedType = null;
9260                   Class _class = methodType.usedClass;
9261                   ClassTemplateParameter curParam = null;
9262                   int id = 0;
9263                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9264                   {
9265                      Class sClass;
9266                      for(sClass = _class; sClass; sClass = sClass.base)
9267                      {
9268                         if(sClass.templateClass) sClass = sClass.templateClass;
9269                         id = 0;
9270                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9271                         {
9272                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9273                            {
9274                               Class nextClass;
9275                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9276                               {
9277                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9278                                  id += nextClass.templateParams.count;
9279                               }
9280                               break;
9281                            }
9282                            id++;
9283                         }
9284                         if(curParam) break;
9285                      }
9286                   }
9287                   if(curParam && _class.templateArgs[id].dataTypeString)
9288                   {
9289                      ClassTemplateArgument arg = _class.templateArgs[id];
9290                      {
9291                         Context context = SetupTemplatesContext(_class);
9292
9293                         /*if(!arg.dataType)
9294                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9295                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9296                         FinishTemplatesContext(context);
9297                      }
9298                      e.destType = templatedType;
9299                      if(templatedType)
9300                      {
9301                         templatedType.passAsTemplate = true;
9302                         // templatedType.refCount++;
9303                      }
9304                   }
9305                   else
9306                   {
9307                      e.destType = type;
9308                      if(type) type.refCount++;
9309                   }
9310                }
9311                else
9312                {
9313                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9314                   {
9315                      e.destType = type.prev;
9316                      e.destType.refCount++;
9317                   }
9318                   else
9319                   {
9320                      e.destType = type;
9321                      if(type) type.refCount++;
9322                   }
9323                }
9324                // Don't reach the end for the ellipsis
9325                if(type && type.kind != ellipsisType)
9326                {
9327                   Type next = type.next;
9328                   if(!type.refCount) FreeType(type);
9329                   type = next;
9330                }
9331             }
9332
9333             if(type && type.kind != ellipsisType)
9334             {
9335                if(methodType && methodType.methodClass)
9336                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9337                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9338                      functionType.params.count + extra);
9339                else
9340                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9341                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9342                      functionType.params.count + extra);
9343             }
9344             yylloc = oldyylloc;
9345             if(type && !type.refCount) FreeType(type);
9346          }
9347          else
9348          {
9349             functionType = Type
9350             {
9351                refCount = 0;
9352                kind = TypeKind::functionType;
9353             };
9354
9355             if(exp.call.exp.type == identifierExp)
9356             {
9357                char * string = exp.call.exp.identifier.string;
9358                if(inCompiler)
9359                {
9360                   Symbol symbol;
9361                   Location oldyylloc = yylloc;
9362
9363                   yylloc = exp.call.exp.identifier.loc;
9364                   if(strstr(string, "__builtin_") == string)
9365                   {
9366                      if(exp.destType)
9367                      {
9368                         functionType.returnType = exp.destType;
9369                         exp.destType.refCount++;
9370                      }
9371                   }
9372                   else
9373                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9374                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9375                   globalContext.symbols.Add((BTNode)symbol);
9376                   if(strstr(symbol.string, "::"))
9377                      globalContext.hasNameSpace = true;
9378
9379                   yylloc = oldyylloc;
9380                }
9381             }
9382             else if(exp.call.exp.type == memberExp)
9383             {
9384                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9385                   exp.call.exp.member.member.string);*/
9386             }
9387             else
9388                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9389
9390             if(!functionType.returnType)
9391             {
9392                functionType.returnType = Type
9393                {
9394                   refCount = 1;
9395                   kind = intType;
9396                };
9397             }
9398          }
9399          if(functionType && functionType.kind == TypeKind::functionType)
9400          {
9401             exp.expType = functionType.returnType;
9402
9403             if(functionType.returnType)
9404                functionType.returnType.refCount++;
9405
9406             if(!functionType.refCount)
9407                FreeType(functionType);
9408          }
9409
9410          if(exp.call.arguments)
9411          {
9412             for(e = exp.call.arguments->first; e; e = e.next)
9413             {
9414                Type destType = e.destType;
9415                ProcessExpressionType(e);
9416             }
9417          }
9418          break;
9419       }
9420       case memberExp:
9421       {
9422          Type type;
9423          Location oldyylloc = yylloc;
9424          bool thisPtr;
9425          Expression checkExp = exp.member.exp;
9426          while(checkExp)
9427          {
9428             if(checkExp.type == castExp)
9429                checkExp = checkExp.cast.exp;
9430             else if(checkExp.type == bracketsExp)
9431                checkExp = checkExp.list ? checkExp.list->first : null;
9432             else
9433                break;
9434          }
9435
9436          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9437          exp.thisPtr = thisPtr;
9438
9439          // DOING THIS LATER NOW...
9440          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9441          {
9442             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9443             /* TODO: Name Space Fix ups
9444             if(!exp.member.member.classSym)
9445                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9446             */
9447          }
9448
9449          ProcessExpressionType(exp.member.exp);
9450          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9451             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9452          {
9453             exp.isConstant = false;
9454          }
9455          else
9456             exp.isConstant = exp.member.exp.isConstant;
9457          type = exp.member.exp.expType;
9458
9459          yylloc = exp.loc;
9460
9461          if(type && (type.kind == templateType))
9462          {
9463             Class _class = thisClass ? thisClass : currentClass;
9464             ClassTemplateParameter param = null;
9465             if(_class)
9466             {
9467                for(param = _class.templateParams.first; param; param = param.next)
9468                {
9469                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9470                      break;
9471                }
9472             }
9473             if(param && param.defaultArg.member)
9474             {
9475                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9476                if(argExp)
9477                {
9478                   Expression expMember = exp.member.exp;
9479                   Declarator decl;
9480                   OldList * specs = MkList();
9481                   char thisClassTypeString[1024];
9482
9483                   FreeIdentifier(exp.member.member);
9484
9485                   ProcessExpressionType(argExp);
9486
9487                   {
9488                      char * colon = strstr(param.defaultArg.memberString, "::");
9489                      if(colon)
9490                      {
9491                         char className[1024];
9492                         Class sClass;
9493
9494                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9495                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9496                      }
9497                      else
9498                         strcpy(thisClassTypeString, _class.fullName);
9499                   }
9500
9501                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9502
9503                   exp.expType = ProcessType(specs, decl);
9504                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9505                   {
9506                      Class expClass = exp.expType._class.registered;
9507                      Class cClass = null;
9508                      int c;
9509                      int paramCount = 0;
9510                      int lastParam = -1;
9511
9512                      char templateString[1024];
9513                      ClassTemplateParameter param;
9514                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9515                      for(cClass = expClass; cClass; cClass = cClass.base)
9516                      {
9517                         int p = 0;
9518                         for(param = cClass.templateParams.first; param; param = param.next)
9519                         {
9520                            int id = p;
9521                            Class sClass;
9522                            ClassTemplateArgument arg;
9523                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9524                            arg = expClass.templateArgs[id];
9525
9526                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9527                            {
9528                               ClassTemplateParameter cParam;
9529                               //int p = numParams - sClass.templateParams.count;
9530                               int p = 0;
9531                               Class nextClass;
9532                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9533
9534                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9535                               {
9536                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9537                                  {
9538                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9539                                     {
9540                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9541                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9542                                        break;
9543                                     }
9544                                  }
9545                               }
9546                            }
9547
9548                            {
9549                               char argument[256];
9550                               argument[0] = '\0';
9551                               /*if(arg.name)
9552                               {
9553                                  strcat(argument, arg.name.string);
9554                                  strcat(argument, " = ");
9555                               }*/
9556                               switch(param.type)
9557                               {
9558                                  case expression:
9559                                  {
9560                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9561                                     char expString[1024];
9562                                     OldList * specs = MkList();
9563                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9564                                     Expression exp;
9565                                     char * string = PrintHexUInt64(arg.expression.ui64);
9566                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9567                                     delete string;
9568
9569                                     ProcessExpressionType(exp);
9570                                     ComputeExpression(exp);
9571                                     expString[0] = '\0';
9572                                     PrintExpression(exp, expString);
9573                                     strcat(argument, expString);
9574                                     // delete exp;
9575                                     FreeExpression(exp);
9576                                     break;
9577                                  }
9578                                  case identifier:
9579                                  {
9580                                     strcat(argument, arg.member.name);
9581                                     break;
9582                                  }
9583                                  case TemplateParameterType::type:
9584                                  {
9585                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9586                                     {
9587                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9588                                           strcat(argument, thisClassTypeString);
9589                                        else
9590                                           strcat(argument, arg.dataTypeString);
9591                                     }
9592                                     break;
9593                                  }
9594                               }
9595                               if(argument[0])
9596                               {
9597                                  if(paramCount) strcat(templateString, ", ");
9598                                  if(lastParam != p - 1)
9599                                  {
9600                                     strcat(templateString, param.name);
9601                                     strcat(templateString, " = ");
9602                                  }
9603                                  strcat(templateString, argument);
9604                                  paramCount++;
9605                                  lastParam = p;
9606                               }
9607                               p++;
9608                            }
9609                         }
9610                      }
9611                      {
9612                         int len = strlen(templateString);
9613                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9614                         templateString[len++] = '>';
9615                         templateString[len++] = '\0';
9616                      }
9617                      {
9618                         Context context = SetupTemplatesContext(_class);
9619                         FreeType(exp.expType);
9620                         exp.expType = ProcessTypeString(templateString, false);
9621                         FinishTemplatesContext(context);
9622                      }
9623                   }
9624
9625                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9626                   exp.type = bracketsExp;
9627                   exp.list = MkListOne(MkExpOp(null, '*',
9628                   /*opExp;
9629                   exp.op.op = '*';
9630                   exp.op.exp1 = null;
9631                   exp.op.exp2 = */
9632                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9633                      MkExpBrackets(MkListOne(
9634                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9635                            '+',
9636                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9637                            '+',
9638                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9639
9640                            ));
9641                }
9642             }
9643             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9644                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9645             {
9646                type = ProcessTemplateParameterType(type.templateParameter);
9647             }
9648          }
9649          // TODO: *** This seems to be where we should add method support for all basic types ***
9650          if(type && (type.kind == templateType));
9651          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9652                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9653                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9654                           (type.kind == pointerType && type.type.kind == charType)))
9655          {
9656             Identifier id = exp.member.member;
9657             TypeKind typeKind = type.kind;
9658             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9659             if(typeKind == subClassType && exp.member.exp.type == classExp)
9660             {
9661                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9662                typeKind = classType;
9663             }
9664
9665             if(id)
9666             {
9667                if(typeKind == intType || typeKind == enumType)
9668                   _class = eSystem_FindClass(privateModule, "int");
9669                else if(!_class)
9670                {
9671                   if(type.kind == classType && type._class && type._class.registered)
9672                   {
9673                      _class = type._class.registered;
9674                   }
9675                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9676                   {
9677                      _class = FindClass("char *").registered;
9678                   }
9679                   else if(type.kind == pointerType)
9680                   {
9681                      _class = eSystem_FindClass(privateModule, "uintptr");
9682                      FreeType(exp.expType);
9683                      exp.expType = ProcessTypeString("uintptr", false);
9684                      exp.byReference = true;
9685                   }
9686                   else
9687                   {
9688                      char string[1024] = "";
9689                      Symbol classSym;
9690                      PrintTypeNoConst(type, string, false, true);
9691                      classSym = FindClass(string);
9692                      if(classSym) _class = classSym.registered;
9693                   }
9694                }
9695             }
9696
9697             if(_class && id)
9698             {
9699                /*bool thisPtr =
9700                   (exp.member.exp.type == identifierExp &&
9701                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9702                Property prop = null;
9703                Method method = null;
9704                DataMember member = null;
9705                Property revConvert = null;
9706                ClassProperty classProp = null;
9707
9708                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9709                   exp.member.memberType = propertyMember;
9710
9711                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9712                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9713
9714                if(typeKind != subClassType)
9715                {
9716                   // Prioritize data members over properties for "this"
9717                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9718                   {
9719                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9720                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9721                      {
9722                         prop = eClass_FindProperty(_class, id.string, privateModule);
9723                         if(prop)
9724                            member = null;
9725                      }
9726                      if(!member && !prop)
9727                         prop = eClass_FindProperty(_class, id.string, privateModule);
9728                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9729                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9730                         exp.member.thisPtr = true;
9731                   }
9732                   // Prioritize properties over data members otherwise
9733                   else
9734                   {
9735                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9736                      if(!id.classSym)
9737                      {
9738                         prop = eClass_FindProperty(_class, id.string, null);
9739                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9740                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9741                      }
9742
9743                      if(!prop && !member)
9744                      {
9745                         method = eClass_FindMethod(_class, id.string, null);
9746                         if(!method)
9747                         {
9748                            prop = eClass_FindProperty(_class, id.string, privateModule);
9749                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9750                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9751                         }
9752                      }
9753
9754                      if(member && prop)
9755                      {
9756                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9757                            prop = null;
9758                         else
9759                            member = null;
9760                      }
9761                   }
9762                }
9763                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9764                   method = eClass_FindMethod(_class, id.string, privateModule);
9765                if(!prop && !member && !method)
9766                {
9767                   if(typeKind == subClassType)
9768                   {
9769                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9770                      if(classProp)
9771                      {
9772                         exp.member.memberType = classPropertyMember;
9773                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9774                      }
9775                      else
9776                      {
9777                         // Assume this is a class_data member
9778                         char structName[1024];
9779                         Identifier id = exp.member.member;
9780                         Expression classExp = exp.member.exp;
9781                         type.refCount++;
9782
9783                         FreeType(classExp.expType);
9784                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9785
9786                         strcpy(structName, "__ecereClassData_");
9787                         FullClassNameCat(structName, type._class.string, false);
9788                         exp.type = pointerExp;
9789                         exp.member.member = id;
9790
9791                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9792                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9793                               MkExpBrackets(MkListOne(MkExpOp(
9794                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9795                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9796                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9797                                  )));
9798
9799                         FreeType(type);
9800
9801                         ProcessExpressionType(exp);
9802                         return;
9803                      }
9804                   }
9805                   else
9806                   {
9807                      // Check for reverse conversion
9808                      // (Convert in an instantiation later, so that we can use
9809                      //  deep properties system)
9810                      Symbol classSym = FindClass(id.string);
9811                      if(classSym)
9812                      {
9813                         Class convertClass = classSym.registered;
9814                         if(convertClass)
9815                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9816                      }
9817                   }
9818                }
9819
9820                if(prop)
9821                {
9822                   exp.member.memberType = propertyMember;
9823                   if(!prop.dataType)
9824                      ProcessPropertyType(prop);
9825                   exp.expType = prop.dataType;
9826                   if(prop.dataType) prop.dataType.refCount++;
9827                }
9828                else if(member)
9829                {
9830                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9831                   {
9832                      FreeExpContents(exp);
9833                      exp.type = identifierExp;
9834                      exp.identifier = MkIdentifier("class");
9835                      ProcessExpressionType(exp);
9836                      return;
9837                   }
9838
9839                   exp.member.memberType = dataMember;
9840                   DeclareStruct(_class.fullName, false);
9841                   if(!member.dataType)
9842                   {
9843                      Context context = SetupTemplatesContext(_class);
9844                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9845                      FinishTemplatesContext(context);
9846                   }
9847                   exp.expType = member.dataType;
9848                   if(member.dataType) member.dataType.refCount++;
9849                }
9850                else if(revConvert)
9851                {
9852                   exp.member.memberType = reverseConversionMember;
9853                   exp.expType = MkClassType(revConvert._class.fullName);
9854                }
9855                else if(method)
9856                {
9857                   //if(inCompiler)
9858                   {
9859                      /*if(id._class)
9860                      {
9861                         exp.type = identifierExp;
9862                         exp.identifier = exp.member.member;
9863                      }
9864                      else*/
9865                         exp.member.memberType = methodMember;
9866                   }
9867                   if(!method.dataType)
9868                      ProcessMethodType(method);
9869                   exp.expType = Type
9870                   {
9871                      refCount = 1;
9872                      kind = methodType;
9873                      method = method;
9874                   };
9875
9876                   // Tricky spot here... To use instance versus class virtual table
9877                   // Put it back to what it was... What did we break?
9878
9879                   // Had to put it back for overriding Main of Thread global instance
9880
9881                   //exp.expType.methodClass = _class;
9882                   exp.expType.methodClass = (id && id._class) ? _class : null;
9883
9884                   // Need the actual class used for templated classes
9885                   exp.expType.usedClass = _class;
9886                }
9887                else if(!classProp)
9888                {
9889                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9890                   {
9891                      FreeExpContents(exp);
9892                      exp.type = identifierExp;
9893                      exp.identifier = MkIdentifier("class");
9894                      FreeType(exp.expType);
9895                      exp.expType = MkClassType("ecere::com::Class");
9896                      return;
9897                   }
9898                   yylloc = exp.member.member.loc;
9899                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9900                   if(inCompiler)
9901                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9902                }
9903
9904                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9905                {
9906                   Class tClass;
9907
9908                   tClass = _class;
9909                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9910
9911                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9912                   {
9913                      int id = 0;
9914                      ClassTemplateParameter curParam = null;
9915                      Class sClass;
9916
9917                      for(sClass = tClass; sClass; sClass = sClass.base)
9918                      {
9919                         id = 0;
9920                         if(sClass.templateClass) sClass = sClass.templateClass;
9921                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9922                         {
9923                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9924                            {
9925                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9926                                  id += sClass.templateParams.count;
9927                               break;
9928                            }
9929                            id++;
9930                         }
9931                         if(curParam) break;
9932                      }
9933
9934                      if(curParam && tClass.templateArgs[id].dataTypeString)
9935                      {
9936                         ClassTemplateArgument arg = tClass.templateArgs[id];
9937                         Context context = SetupTemplatesContext(tClass);
9938                         /*if(!arg.dataType)
9939                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9940                         FreeType(exp.expType);
9941                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9942                         if(exp.expType)
9943                         {
9944                            if(exp.expType.kind == thisClassType)
9945                            {
9946                               FreeType(exp.expType);
9947                               exp.expType = ReplaceThisClassType(_class);
9948                            }
9949
9950                            if(tClass.templateClass)
9951                               exp.expType.passAsTemplate = true;
9952                            //exp.expType.refCount++;
9953                            if(!exp.destType)
9954                            {
9955                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9956                               //exp.destType.refCount++;
9957
9958                               if(exp.destType.kind == thisClassType)
9959                               {
9960                                  FreeType(exp.destType);
9961                                  exp.destType = ReplaceThisClassType(_class);
9962                               }
9963                            }
9964                         }
9965                         FinishTemplatesContext(context);
9966                      }
9967                   }
9968                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9969                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9970                   {
9971                      int id = 0;
9972                      ClassTemplateParameter curParam = null;
9973                      Class sClass;
9974
9975                      for(sClass = tClass; sClass; sClass = sClass.base)
9976                      {
9977                         id = 0;
9978                         if(sClass.templateClass) sClass = sClass.templateClass;
9979                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9980                         {
9981                            if(curParam.type == TemplateParameterType::type &&
9982                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9983                            {
9984                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9985                                  id += sClass.templateParams.count;
9986                               break;
9987                            }
9988                            id++;
9989                         }
9990                         if(curParam) break;
9991                      }
9992
9993                      if(curParam)
9994                      {
9995                         ClassTemplateArgument arg = tClass.templateArgs[id];
9996                         Context context = SetupTemplatesContext(tClass);
9997                         Type basicType;
9998                         /*if(!arg.dataType)
9999                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10000
10001                         basicType = ProcessTypeString(arg.dataTypeString, false);
10002                         if(basicType)
10003                         {
10004                            if(basicType.kind == thisClassType)
10005                            {
10006                               FreeType(basicType);
10007                               basicType = ReplaceThisClassType(_class);
10008                            }
10009
10010                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10011                            if(tClass.templateClass)
10012                               basicType.passAsTemplate = true;
10013                            */
10014
10015                            FreeType(exp.expType);
10016
10017                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10018                            //exp.expType.refCount++;
10019                            if(!exp.destType)
10020                            {
10021                               exp.destType = exp.expType;
10022                               exp.destType.refCount++;
10023                            }
10024
10025                            {
10026                               Expression newExp { };
10027                               OldList * specs = MkList();
10028                               Declarator decl;
10029                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10030                               *newExp = *exp;
10031                               if(exp.destType) exp.destType.refCount++;
10032                               if(exp.expType)  exp.expType.refCount++;
10033                               exp.type = castExp;
10034                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10035                               exp.cast.exp = newExp;
10036                               //FreeType(exp.expType);
10037                               //exp.expType = null;
10038                               //ProcessExpressionType(sourceExp);
10039                            }
10040                         }
10041                         FinishTemplatesContext(context);
10042                      }
10043                   }
10044                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10045                   {
10046                      Class expClass = exp.expType._class.registered;
10047                      if(expClass)
10048                      {
10049                         Class cClass = null;
10050                         int c;
10051                         int p = 0;
10052                         int paramCount = 0;
10053                         int lastParam = -1;
10054                         char templateString[1024];
10055                         ClassTemplateParameter param;
10056                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10057                         while(cClass != expClass)
10058                         {
10059                            Class sClass;
10060                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10061                            cClass = sClass;
10062
10063                            for(param = cClass.templateParams.first; param; param = param.next)
10064                            {
10065                               Class cClassCur = null;
10066                               int c;
10067                               int cp = 0;
10068                               ClassTemplateParameter paramCur = null;
10069                               ClassTemplateArgument arg;
10070                               while(cClassCur != tClass && !paramCur)
10071                               {
10072                                  Class sClassCur;
10073                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10074                                  cClassCur = sClassCur;
10075
10076                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10077                                  {
10078                                     if(!strcmp(paramCur.name, param.name))
10079                                     {
10080
10081                                        break;
10082                                     }
10083                                     cp++;
10084                                  }
10085                               }
10086                               if(paramCur && paramCur.type == TemplateParameterType::type)
10087                                  arg = tClass.templateArgs[cp];
10088                               else
10089                                  arg = expClass.templateArgs[p];
10090
10091                               {
10092                                  char argument[256];
10093                                  argument[0] = '\0';
10094                                  /*if(arg.name)
10095                                  {
10096                                     strcat(argument, arg.name.string);
10097                                     strcat(argument, " = ");
10098                                  }*/
10099                                  switch(param.type)
10100                                  {
10101                                     case expression:
10102                                     {
10103                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10104                                        char expString[1024];
10105                                        OldList * specs = MkList();
10106                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10107                                        Expression exp;
10108                                        char * string = PrintHexUInt64(arg.expression.ui64);
10109                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10110                                        delete string;
10111
10112                                        ProcessExpressionType(exp);
10113                                        ComputeExpression(exp);
10114                                        expString[0] = '\0';
10115                                        PrintExpression(exp, expString);
10116                                        strcat(argument, expString);
10117                                        // delete exp;
10118                                        FreeExpression(exp);
10119                                        break;
10120                                     }
10121                                     case identifier:
10122                                     {
10123                                        strcat(argument, arg.member.name);
10124                                        break;
10125                                     }
10126                                     case TemplateParameterType::type:
10127                                     {
10128                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10129                                           strcat(argument, arg.dataTypeString);
10130                                        break;
10131                                     }
10132                                  }
10133                                  if(argument[0])
10134                                  {
10135                                     if(paramCount) strcat(templateString, ", ");
10136                                     if(lastParam != p - 1)
10137                                     {
10138                                        strcat(templateString, param.name);
10139                                        strcat(templateString, " = ");
10140                                     }
10141                                     strcat(templateString, argument);
10142                                     paramCount++;
10143                                     lastParam = p;
10144                                  }
10145                               }
10146                               p++;
10147                            }
10148                         }
10149                         {
10150                            int len = strlen(templateString);
10151                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10152                            templateString[len++] = '>';
10153                            templateString[len++] = '\0';
10154                         }
10155
10156                         FreeType(exp.expType);
10157                         {
10158                            Context context = SetupTemplatesContext(tClass);
10159                            exp.expType = ProcessTypeString(templateString, false);
10160                            FinishTemplatesContext(context);
10161                         }
10162                      }
10163                   }
10164                }
10165             }
10166             else
10167                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10168          }
10169          else if(type && (type.kind == structType || type.kind == unionType))
10170          {
10171             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10172             if(memberType)
10173             {
10174                exp.expType = memberType;
10175                if(memberType)
10176                   memberType.refCount++;
10177             }
10178          }
10179          else
10180          {
10181             char expString[10240];
10182             expString[0] = '\0';
10183             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10184             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10185          }
10186
10187          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10188          {
10189             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10190             {
10191                Identifier id = exp.member.member;
10192                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10193                if(_class)
10194                {
10195                   FreeType(exp.expType);
10196                   exp.expType = ReplaceThisClassType(_class);
10197                }
10198             }
10199          }
10200          yylloc = oldyylloc;
10201          break;
10202       }
10203       // Convert x->y into (*x).y
10204       case pointerExp:
10205       {
10206          Type destType = exp.destType;
10207
10208          // DOING THIS LATER NOW...
10209          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10210          {
10211             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10212             /* TODO: Name Space Fix ups
10213             if(!exp.member.member.classSym)
10214                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10215             */
10216          }
10217
10218          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10219          exp.type = memberExp;
10220          if(destType)
10221             destType.count++;
10222          ProcessExpressionType(exp);
10223          if(destType)
10224             destType.count--;
10225          break;
10226       }
10227       case classSizeExp:
10228       {
10229          //ComputeExpression(exp);
10230
10231          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10232          if(classSym && classSym.registered)
10233          {
10234             if(classSym.registered.type == noHeadClass)
10235             {
10236                char name[1024];
10237                name[0] = '\0';
10238                DeclareStruct(classSym.string, false);
10239                FreeSpecifier(exp._class);
10240                exp.type = typeSizeExp;
10241                FullClassNameCat(name, classSym.string, false);
10242                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10243             }
10244             else
10245             {
10246                if(classSym.registered.fixed)
10247                {
10248                   FreeSpecifier(exp._class);
10249                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10250                   exp.type = constantExp;
10251                }
10252                else
10253                {
10254                   char className[1024];
10255                   strcpy(className, "__ecereClass_");
10256                   FullClassNameCat(className, classSym.string, true);
10257                   MangleClassName(className);
10258
10259                   DeclareClass(classSym, className);
10260
10261                   FreeExpContents(exp);
10262                   exp.type = pointerExp;
10263                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10264                   exp.member.member = MkIdentifier("structSize");
10265                }
10266             }
10267          }
10268
10269          exp.expType = Type
10270          {
10271             refCount = 1;
10272             kind = intType;
10273          };
10274          // exp.isConstant = true;
10275          break;
10276       }
10277       case typeSizeExp:
10278       {
10279          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10280
10281          exp.expType = Type
10282          {
10283             refCount = 1;
10284             kind = intType;
10285          };
10286          exp.isConstant = true;
10287
10288          DeclareType(type, false, false);
10289          FreeType(type);
10290          break;
10291       }
10292       case castExp:
10293       {
10294          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10295          type.count = 1;
10296          FreeType(exp.cast.exp.destType);
10297          exp.cast.exp.destType = type;
10298          type.refCount++;
10299          ProcessExpressionType(exp.cast.exp);
10300          type.count = 0;
10301          exp.expType = type;
10302          //type.refCount++;
10303
10304          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10305          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10306          {
10307             void * prev = exp.prev, * next = exp.next;
10308             Type expType = exp.cast.exp.destType;
10309             Expression castExp = exp.cast.exp;
10310             Type destType = exp.destType;
10311
10312             if(expType) expType.refCount++;
10313
10314             //FreeType(exp.destType);
10315             FreeType(exp.expType);
10316             FreeTypeName(exp.cast.typeName);
10317
10318             *exp = *castExp;
10319             FreeType(exp.expType);
10320             FreeType(exp.destType);
10321
10322             exp.expType = expType;
10323             exp.destType = destType;
10324
10325             delete castExp;
10326
10327             exp.prev = prev;
10328             exp.next = next;
10329
10330          }
10331          else
10332          {
10333             exp.isConstant = exp.cast.exp.isConstant;
10334          }
10335          //FreeType(type);
10336          break;
10337       }
10338       case extensionInitializerExp:
10339       {
10340          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10341          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10342          // ProcessInitializer(exp.initializer.initializer, type);
10343          exp.expType = type;
10344          break;
10345       }
10346       case vaArgExp:
10347       {
10348          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10349          ProcessExpressionType(exp.vaArg.exp);
10350          exp.expType = type;
10351          break;
10352       }
10353       case conditionExp:
10354       {
10355          Expression e;
10356          exp.isConstant = true;
10357
10358          FreeType(exp.cond.cond.destType);
10359          exp.cond.cond.destType = MkClassType("bool");
10360          exp.cond.cond.destType.truth = true;
10361          ProcessExpressionType(exp.cond.cond);
10362          if(!exp.cond.cond.isConstant)
10363             exp.isConstant = false;
10364          for(e = exp.cond.exp->first; e; e = e.next)
10365          {
10366             if(!e.next)
10367             {
10368                FreeType(e.destType);
10369                e.destType = exp.destType;
10370                if(e.destType) e.destType.refCount++;
10371             }
10372             ProcessExpressionType(e);
10373             if(!e.next)
10374             {
10375                exp.expType = e.expType;
10376                if(e.expType) e.expType.refCount++;
10377             }
10378             if(!e.isConstant)
10379                exp.isConstant = false;
10380          }
10381
10382          FreeType(exp.cond.elseExp.destType);
10383          // Added this check if we failed to find an expType
10384          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10385
10386          // Reversed it...
10387          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10388
10389          if(exp.cond.elseExp.destType)
10390             exp.cond.elseExp.destType.refCount++;
10391          ProcessExpressionType(exp.cond.elseExp);
10392
10393          // FIXED THIS: Was done before calling process on elseExp
10394          if(!exp.cond.elseExp.isConstant)
10395             exp.isConstant = false;
10396          break;
10397       }
10398       case extensionCompoundExp:
10399       {
10400          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10401          {
10402             Statement last = exp.compound.compound.statements->last;
10403             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10404             {
10405                ((Expression)last.expressions->last).destType = exp.destType;
10406                if(exp.destType)
10407                   exp.destType.refCount++;
10408             }
10409             ProcessStatement(exp.compound);
10410             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10411             if(exp.expType)
10412                exp.expType.refCount++;
10413          }
10414          break;
10415       }
10416       case classExp:
10417       {
10418          Specifier spec = exp._classExp.specifiers->first;
10419          if(spec && spec.type == nameSpecifier)
10420          {
10421             exp.expType = MkClassType(spec.name);
10422             exp.expType.kind = subClassType;
10423             exp.byReference = true;
10424          }
10425          else
10426          {
10427             exp.expType = MkClassType("ecere::com::Class");
10428             exp.byReference = true;
10429          }
10430          break;
10431       }
10432       case classDataExp:
10433       {
10434          Class _class = thisClass ? thisClass : currentClass;
10435          if(_class)
10436          {
10437             Identifier id = exp.classData.id;
10438             char structName[1024];
10439             Expression classExp;
10440             strcpy(structName, "__ecereClassData_");
10441             FullClassNameCat(structName, _class.fullName, false);
10442             exp.type = pointerExp;
10443             exp.member.member = id;
10444             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10445                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10446             else
10447                classExp = MkExpIdentifier(MkIdentifier("class"));
10448
10449             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10450                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10451                   MkExpBrackets(MkListOne(MkExpOp(
10452                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10453                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10454                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10455                      )));
10456
10457             ProcessExpressionType(exp);
10458             return;
10459          }
10460          break;
10461       }
10462       case arrayExp:
10463       {
10464          Type type = null;
10465          char * typeString = null;
10466          char typeStringBuf[1024];
10467          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10468             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10469          {
10470             Class templateClass = exp.destType._class.registered;
10471             typeString = templateClass.templateArgs[2].dataTypeString;
10472          }
10473          else if(exp.list)
10474          {
10475             // Guess type from expressions in the array
10476             Expression e;
10477             for(e = exp.list->first; e; e = e.next)
10478             {
10479                ProcessExpressionType(e);
10480                if(e.expType)
10481                {
10482                   if(!type) { type = e.expType; type.refCount++; }
10483                   else
10484                   {
10485                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10486                      if(!MatchTypeExpression(e, type, null, false))
10487                      {
10488                         FreeType(type);
10489                         type = e.expType;
10490                         e.expType = null;
10491
10492                         e = exp.list->first;
10493                         ProcessExpressionType(e);
10494                         if(e.expType)
10495                         {
10496                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10497                            if(!MatchTypeExpression(e, type, null, false))
10498                            {
10499                               FreeType(e.expType);
10500                               e.expType = null;
10501                               FreeType(type);
10502                               type = null;
10503                               break;
10504                            }
10505                         }
10506                      }
10507                   }
10508                   if(e.expType)
10509                   {
10510                      FreeType(e.expType);
10511                      e.expType = null;
10512                   }
10513                }
10514             }
10515             if(type)
10516             {
10517                typeStringBuf[0] = '\0';
10518                PrintTypeNoConst(type, typeStringBuf, false, true);
10519                typeString = typeStringBuf;
10520                FreeType(type);
10521                type = null;
10522             }
10523          }
10524          if(typeString)
10525          {
10526             /*
10527             (Container)& (struct BuiltInContainer)
10528             {
10529                ._vTbl = class(BuiltInContainer)._vTbl,
10530                ._class = class(BuiltInContainer),
10531                .refCount = 0,
10532                .data = (int[]){ 1, 7, 3, 4, 5 },
10533                .count = 5,
10534                .type = class(int),
10535             }
10536             */
10537             char templateString[1024];
10538             OldList * initializers = MkList();
10539             OldList * structInitializers = MkList();
10540             OldList * specs = MkList();
10541             Expression expExt;
10542             Declarator decl = SpecDeclFromString(typeString, specs, null);
10543             sprintf(templateString, "Container<%s>", typeString);
10544
10545             if(exp.list)
10546             {
10547                Expression e;
10548                type = ProcessTypeString(typeString, false);
10549                while(e = exp.list->first)
10550                {
10551                   exp.list->Remove(e);
10552                   e.destType = type;
10553                   type.refCount++;
10554                   ProcessExpressionType(e);
10555                   ListAdd(initializers, MkInitializerAssignment(e));
10556                }
10557                FreeType(type);
10558                delete exp.list;
10559             }
10560
10561             DeclareStruct("ecere::com::BuiltInContainer", false);
10562
10563             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10564                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10565             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10566                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10567             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10568                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10569             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10570                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10571                MkInitializerList(initializers))));
10572                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10573             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10574                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10575             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10576                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10577             exp.expType = ProcessTypeString(templateString, false);
10578             exp.type = bracketsExp;
10579             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10580                MkExpOp(null, '&',
10581                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10582                   MkInitializerList(structInitializers)))));
10583             ProcessExpressionType(expExt);
10584          }
10585          else
10586          {
10587             exp.expType = ProcessTypeString("Container", false);
10588             Compiler_Error($"Couldn't determine type of array elements\n");
10589          }
10590          break;
10591       }
10592    }
10593
10594    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10595    {
10596       FreeType(exp.expType);
10597       exp.expType = ReplaceThisClassType(thisClass);
10598    }
10599
10600    // Resolve structures here
10601    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10602    {
10603       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10604       // TODO: Fix members reference...
10605       if(symbol)
10606       {
10607          if(exp.expType.kind != enumType)
10608          {
10609             Type member;
10610             String enumName = CopyString(exp.expType.enumName);
10611
10612             // Fixed a memory leak on self-referencing C structs typedefs
10613             // by instantiating a new type rather than simply copying members
10614             // into exp.expType
10615             FreeType(exp.expType);
10616             exp.expType = Type { };
10617             exp.expType.kind = symbol.type.kind;
10618             exp.expType.refCount++;
10619             exp.expType.enumName = enumName;
10620
10621             exp.expType.members = symbol.type.members;
10622             for(member = symbol.type.members.first; member; member = member.next)
10623                member.refCount++;
10624          }
10625          else
10626          {
10627             NamedLink member;
10628             for(member = symbol.type.members.first; member; member = member.next)
10629             {
10630                NamedLink value { name = CopyString(member.name) };
10631                exp.expType.members.Add(value);
10632             }
10633          }
10634       }
10635    }
10636
10637    yylloc = exp.loc;
10638    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10639    else if(exp.destType && !exp.destType.keepCast)
10640    {
10641       if(!CheckExpressionType(exp, exp.destType, false))
10642       {
10643          if(!exp.destType.count || unresolved)
10644          {
10645             if(!exp.expType)
10646             {
10647                yylloc = exp.loc;
10648                if(exp.destType.kind != ellipsisType)
10649                {
10650                   char type2[1024];
10651                   type2[0] = '\0';
10652                   if(inCompiler)
10653                   {
10654                      char expString[10240];
10655                      expString[0] = '\0';
10656
10657                      PrintType(exp.destType, type2, false, true);
10658
10659                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10660                      if(unresolved)
10661                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10662                      else if(exp.type != dummyExp)
10663                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10664                   }
10665                }
10666                else
10667                {
10668                   char expString[10240] ;
10669                   expString[0] = '\0';
10670                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10671
10672                   if(unresolved)
10673                      Compiler_Error($"unresolved identifier %s\n", expString);
10674                   else if(exp.type != dummyExp)
10675                      Compiler_Error($"couldn't determine type of %s\n", expString);
10676                }
10677             }
10678             else
10679             {
10680                char type1[1024];
10681                char type2[1024];
10682                type1[0] = '\0';
10683                type2[0] = '\0';
10684                if(inCompiler)
10685                {
10686                   PrintType(exp.expType, type1, false, true);
10687                   PrintType(exp.destType, type2, false, true);
10688                }
10689
10690                //CheckExpressionType(exp, exp.destType, false);
10691
10692                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10693                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10694                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10695                else
10696                {
10697                   char expString[10240];
10698                   expString[0] = '\0';
10699                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10700
10701 #ifdef _DEBUG
10702                   CheckExpressionType(exp, exp.destType, false);
10703 #endif
10704                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10705                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10706                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10707
10708                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10709                   FreeType(exp.expType);
10710                   exp.destType.refCount++;
10711                   exp.expType = exp.destType;
10712                }
10713             }
10714          }
10715       }
10716       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10717       {
10718          Expression newExp { };
10719          char typeString[1024];
10720          OldList * specs = MkList();
10721          Declarator decl;
10722
10723          typeString[0] = '\0';
10724
10725          *newExp = *exp;
10726
10727          if(exp.expType)  exp.expType.refCount++;
10728          if(exp.expType)  exp.expType.refCount++;
10729          exp.type = castExp;
10730          newExp.destType = exp.expType;
10731
10732          PrintType(exp.expType, typeString, false, false);
10733          decl = SpecDeclFromString(typeString, specs, null);
10734
10735          exp.cast.typeName = MkTypeName(specs, decl);
10736          exp.cast.exp = newExp;
10737       }
10738    }
10739    else if(unresolved)
10740    {
10741       if(exp.identifier._class && exp.identifier._class.name)
10742          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10743       else if(exp.identifier.string && exp.identifier.string[0])
10744          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10745    }
10746    else if(!exp.expType && exp.type != dummyExp)
10747    {
10748       char expString[10240];
10749       expString[0] = '\0';
10750       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10751       Compiler_Error($"couldn't determine type of %s\n", expString);
10752    }
10753
10754    // Let's try to support any_object & typed_object here:
10755    if(inCompiler)
10756       ApplyAnyObjectLogic(exp);
10757
10758    // Mark nohead classes as by reference, unless we're casting them to an integral type
10759    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10760       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10761          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10762           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10763    {
10764       exp.byReference = true;
10765    }
10766    yylloc = oldyylloc;
10767 }
10768
10769 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10770 {
10771    // THIS CODE WILL FIND NEXT MEMBER...
10772    if(*curMember)
10773    {
10774       *curMember = (*curMember).next;
10775
10776       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10777       {
10778          *curMember = subMemberStack[--(*subMemberStackPos)];
10779          *curMember = (*curMember).next;
10780       }
10781
10782       // SKIP ALL PROPERTIES HERE...
10783       while((*curMember) && (*curMember).isProperty)
10784          *curMember = (*curMember).next;
10785
10786       if(subMemberStackPos)
10787       {
10788          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10789          {
10790             subMemberStack[(*subMemberStackPos)++] = *curMember;
10791
10792             *curMember = (*curMember).members.first;
10793             while(*curMember && (*curMember).isProperty)
10794                *curMember = (*curMember).next;
10795          }
10796       }
10797    }
10798    while(!*curMember)
10799    {
10800       if(!*curMember)
10801       {
10802          if(subMemberStackPos && *subMemberStackPos)
10803          {
10804             *curMember = subMemberStack[--(*subMemberStackPos)];
10805             *curMember = (*curMember).next;
10806          }
10807          else
10808          {
10809             Class lastCurClass = *curClass;
10810
10811             if(*curClass == _class) break;     // REACHED THE END
10812
10813             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10814             *curMember = (*curClass).membersAndProperties.first;
10815          }
10816
10817          while((*curMember) && (*curMember).isProperty)
10818             *curMember = (*curMember).next;
10819          if(subMemberStackPos)
10820          {
10821             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10822             {
10823                subMemberStack[(*subMemberStackPos)++] = *curMember;
10824
10825                *curMember = (*curMember).members.first;
10826                while(*curMember && (*curMember).isProperty)
10827                   *curMember = (*curMember).next;
10828             }
10829          }
10830       }
10831    }
10832 }
10833
10834
10835 static void ProcessInitializer(Initializer init, Type type)
10836 {
10837    switch(init.type)
10838    {
10839       case expInitializer:
10840          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10841          {
10842             // TESTING THIS FOR SHUTTING = 0 WARNING
10843             if(init.exp && !init.exp.destType)
10844             {
10845                FreeType(init.exp.destType);
10846                init.exp.destType = type;
10847                if(type) type.refCount++;
10848             }
10849             if(init.exp)
10850             {
10851                ProcessExpressionType(init.exp);
10852                init.isConstant = init.exp.isConstant;
10853             }
10854             break;
10855          }
10856          else
10857          {
10858             Expression exp = init.exp;
10859             Instantiation inst = exp.instance;
10860             MembersInit members;
10861
10862             init.type = listInitializer;
10863             init.list = MkList();
10864
10865             if(inst.members)
10866             {
10867                for(members = inst.members->first; members; members = members.next)
10868                {
10869                   if(members.type == dataMembersInit)
10870                   {
10871                      MemberInit member;
10872                      for(member = members.dataMembers->first; member; member = member.next)
10873                      {
10874                         ListAdd(init.list, member.initializer);
10875                         member.initializer = null;
10876                      }
10877                   }
10878                   // Discard all MembersInitMethod
10879                }
10880             }
10881             FreeExpression(exp);
10882          }
10883       case listInitializer:
10884       {
10885          Initializer i;
10886          Type initializerType = null;
10887          Class curClass = null;
10888          DataMember curMember = null;
10889          DataMember subMemberStack[256];
10890          int subMemberStackPos = 0;
10891
10892          if(type && type.kind == arrayType)
10893             initializerType = Dereference(type);
10894          else if(type && (type.kind == structType || type.kind == unionType))
10895             initializerType = type.members.first;
10896
10897          for(i = init.list->first; i; i = i.next)
10898          {
10899             if(type && type.kind == classType && type._class && type._class.registered)
10900             {
10901                // 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)
10902                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10903                // TODO: Generate error on initializing a private data member this way from another module...
10904                if(curMember)
10905                {
10906                   if(!curMember.dataType)
10907                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10908                   initializerType = curMember.dataType;
10909                }
10910             }
10911             ProcessInitializer(i, initializerType);
10912             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10913                initializerType = initializerType.next;
10914             if(!i.isConstant)
10915                init.isConstant = false;
10916          }
10917
10918          if(type && type.kind == arrayType)
10919             FreeType(initializerType);
10920
10921          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10922          {
10923             Compiler_Error($"Assigning list initializer to non list\n");
10924          }
10925          break;
10926       }
10927    }
10928 }
10929
10930 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10931 {
10932    switch(spec.type)
10933    {
10934       case baseSpecifier:
10935       {
10936          if(spec.specifier == THISCLASS)
10937          {
10938             if(thisClass)
10939             {
10940                spec.type = nameSpecifier;
10941                spec.name = ReplaceThisClass(thisClass);
10942                spec.symbol = FindClass(spec.name);
10943                ProcessSpecifier(spec, declareStruct);
10944             }
10945          }
10946          break;
10947       }
10948       case nameSpecifier:
10949       {
10950          Symbol symbol = FindType(curContext, spec.name);
10951          if(symbol)
10952             DeclareType(symbol.type, true, true);
10953          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10954             DeclareStruct(spec.name, false);
10955          break;
10956       }
10957       case enumSpecifier:
10958       {
10959          Enumerator e;
10960          if(spec.list)
10961          {
10962             for(e = spec.list->first; e; e = e.next)
10963             {
10964                if(e.exp)
10965                   ProcessExpressionType(e.exp);
10966             }
10967          }
10968          break;
10969       }
10970       case structSpecifier:
10971       case unionSpecifier:
10972       {
10973          if(spec.definitions)
10974          {
10975             ClassDef def;
10976             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10977             //if(symbol)
10978                ProcessClass(spec.definitions, symbol);
10979             /*else
10980             {
10981                for(def = spec.definitions->first; def; def = def.next)
10982                {
10983                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10984                      ProcessDeclaration(def.decl);
10985                }
10986             }*/
10987          }
10988          break;
10989       }
10990       /*
10991       case classSpecifier:
10992       {
10993          Symbol classSym = FindClass(spec.name);
10994          if(classSym && classSym.registered && classSym.registered.type == structClass)
10995             DeclareStruct(spec.name, false);
10996          break;
10997       }
10998       */
10999    }
11000 }
11001
11002
11003 static void ProcessDeclarator(Declarator decl)
11004 {
11005    switch(decl.type)
11006    {
11007       case identifierDeclarator:
11008          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11009          {
11010             FreeSpecifier(decl.identifier._class);
11011             decl.identifier._class = null;
11012          }
11013          break;
11014       case arrayDeclarator:
11015          if(decl.array.exp)
11016             ProcessExpressionType(decl.array.exp);
11017       case structDeclarator:
11018       case bracketsDeclarator:
11019       case functionDeclarator:
11020       case pointerDeclarator:
11021       case extendedDeclarator:
11022       case extendedDeclaratorEnd:
11023          if(decl.declarator)
11024             ProcessDeclarator(decl.declarator);
11025          if(decl.type == functionDeclarator)
11026          {
11027             Identifier id = GetDeclId(decl);
11028             if(id && id._class)
11029             {
11030                TypeName param
11031                {
11032                   qualifiers = MkListOne(id._class);
11033                   declarator = null;
11034                };
11035                if(!decl.function.parameters)
11036                   decl.function.parameters = MkList();
11037                decl.function.parameters->Insert(null, param);
11038                id._class = null;
11039             }
11040             if(decl.function.parameters)
11041             {
11042                TypeName param;
11043
11044                for(param = decl.function.parameters->first; param; param = param.next)
11045                {
11046                   if(param.qualifiers && param.qualifiers->first)
11047                   {
11048                      Specifier spec = param.qualifiers->first;
11049                      if(spec && spec.specifier == TYPED_OBJECT)
11050                      {
11051                         Declarator d = param.declarator;
11052                         TypeName newParam
11053                         {
11054                            qualifiers = MkListOne(MkSpecifier(VOID));
11055                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11056                         };
11057
11058                         FreeList(param.qualifiers, FreeSpecifier);
11059
11060                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11061                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11062
11063                         decl.function.parameters->Insert(param, newParam);
11064                         param = newParam;
11065                      }
11066                      else if(spec && spec.specifier == ANY_OBJECT)
11067                      {
11068                         Declarator d = param.declarator;
11069
11070                         FreeList(param.qualifiers, FreeSpecifier);
11071
11072                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11073                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11074                      }
11075                      else if(spec.specifier == THISCLASS)
11076                      {
11077                         if(thisClass)
11078                         {
11079                            spec.type = nameSpecifier;
11080                            spec.name = ReplaceThisClass(thisClass);
11081                            spec.symbol = FindClass(spec.name);
11082                            ProcessSpecifier(spec, false);
11083                         }
11084                      }
11085                   }
11086
11087                   if(param.declarator)
11088                      ProcessDeclarator(param.declarator);
11089                }
11090             }
11091          }
11092          break;
11093    }
11094 }
11095
11096 static void ProcessDeclaration(Declaration decl)
11097 {
11098    yylloc = decl.loc;
11099    switch(decl.type)
11100    {
11101       case initDeclaration:
11102       {
11103          bool declareStruct = false;
11104          /*
11105          lineNum = decl.pos.line;
11106          column = decl.pos.col;
11107          */
11108
11109          if(decl.declarators)
11110          {
11111             InitDeclarator d;
11112
11113             for(d = decl.declarators->first; d; d = d.next)
11114             {
11115                Type type, subType;
11116                ProcessDeclarator(d.declarator);
11117
11118                type = ProcessType(decl.specifiers, d.declarator);
11119
11120                if(d.initializer)
11121                {
11122                   ProcessInitializer(d.initializer, type);
11123
11124                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11125
11126                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11127                      d.initializer.exp.type == instanceExp)
11128                   {
11129                      if(type.kind == classType && type._class ==
11130                         d.initializer.exp.expType._class)
11131                      {
11132                         Instantiation inst = d.initializer.exp.instance;
11133                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11134
11135                         d.initializer.exp.instance = null;
11136                         if(decl.specifiers)
11137                            FreeList(decl.specifiers, FreeSpecifier);
11138                         FreeList(decl.declarators, FreeInitDeclarator);
11139
11140                         d = null;
11141
11142                         decl.type = instDeclaration;
11143                         decl.inst = inst;
11144                      }
11145                   }
11146                }
11147                for(subType = type; subType;)
11148                {
11149                   if(subType.kind == classType)
11150                   {
11151                      declareStruct = true;
11152                      break;
11153                   }
11154                   else if(subType.kind == pointerType)
11155                      break;
11156                   else if(subType.kind == arrayType)
11157                      subType = subType.arrayType;
11158                   else
11159                      break;
11160                }
11161
11162                FreeType(type);
11163                if(!d) break;
11164             }
11165          }
11166
11167          if(decl.specifiers)
11168          {
11169             Specifier s;
11170             for(s = decl.specifiers->first; s; s = s.next)
11171             {
11172                ProcessSpecifier(s, declareStruct);
11173             }
11174          }
11175          break;
11176       }
11177       case instDeclaration:
11178       {
11179          ProcessInstantiationType(decl.inst);
11180          break;
11181       }
11182       case structDeclaration:
11183       {
11184          Specifier spec;
11185          Declarator d;
11186          bool declareStruct = false;
11187
11188          if(decl.declarators)
11189          {
11190             for(d = decl.declarators->first; d; d = d.next)
11191             {
11192                Type type = ProcessType(decl.specifiers, d.declarator);
11193                Type subType;
11194                ProcessDeclarator(d);
11195                for(subType = type; subType;)
11196                {
11197                   if(subType.kind == classType)
11198                   {
11199                      declareStruct = true;
11200                      break;
11201                   }
11202                   else if(subType.kind == pointerType)
11203                      break;
11204                   else if(subType.kind == arrayType)
11205                      subType = subType.arrayType;
11206                   else
11207                      break;
11208                }
11209                FreeType(type);
11210             }
11211          }
11212          if(decl.specifiers)
11213          {
11214             for(spec = decl.specifiers->first; spec; spec = spec.next)
11215                ProcessSpecifier(spec, declareStruct);
11216          }
11217          break;
11218       }
11219    }
11220 }
11221
11222 static FunctionDefinition curFunction;
11223
11224 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11225 {
11226    char propName[1024], propNameM[1024];
11227    char getName[1024], setName[1024];
11228    OldList * args;
11229
11230    DeclareProperty(prop, setName, getName);
11231
11232    // eInstance_FireWatchers(object, prop);
11233    strcpy(propName, "__ecereProp_");
11234    FullClassNameCat(propName, prop._class.fullName, false);
11235    strcat(propName, "_");
11236    // strcat(propName, prop.name);
11237    FullClassNameCat(propName, prop.name, true);
11238    MangleClassName(propName);
11239
11240    strcpy(propNameM, "__ecerePropM_");
11241    FullClassNameCat(propNameM, prop._class.fullName, false);
11242    strcat(propNameM, "_");
11243    // strcat(propNameM, prop.name);
11244    FullClassNameCat(propNameM, prop.name, true);
11245    MangleClassName(propNameM);
11246
11247    if(prop.isWatchable)
11248    {
11249       args = MkList();
11250       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11251       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11252       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11253
11254       args = MkList();
11255       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11256       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11257       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11258    }
11259
11260
11261    {
11262       args = MkList();
11263       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11264       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11265       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11266
11267       args = MkList();
11268       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11269       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11270       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11271    }
11272
11273    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11274       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11275       curFunction.propSet.fireWatchersDone = true;
11276 }
11277
11278 static void ProcessStatement(Statement stmt)
11279 {
11280    yylloc = stmt.loc;
11281    /*
11282    lineNum = stmt.pos.line;
11283    column = stmt.pos.col;
11284    */
11285    switch(stmt.type)
11286    {
11287       case labeledStmt:
11288          ProcessStatement(stmt.labeled.stmt);
11289          break;
11290       case caseStmt:
11291          // This expression should be constant...
11292          if(stmt.caseStmt.exp)
11293          {
11294             FreeType(stmt.caseStmt.exp.destType);
11295             stmt.caseStmt.exp.destType = curSwitchType;
11296             if(curSwitchType) curSwitchType.refCount++;
11297             ProcessExpressionType(stmt.caseStmt.exp);
11298             ComputeExpression(stmt.caseStmt.exp);
11299          }
11300          if(stmt.caseStmt.stmt)
11301             ProcessStatement(stmt.caseStmt.stmt);
11302          break;
11303       case compoundStmt:
11304       {
11305          if(stmt.compound.context)
11306          {
11307             Declaration decl;
11308             Statement s;
11309
11310             Statement prevCompound = curCompound;
11311             Context prevContext = curContext;
11312
11313             if(!stmt.compound.isSwitch)
11314                curCompound = stmt;
11315             curContext = stmt.compound.context;
11316
11317             if(stmt.compound.declarations)
11318             {
11319                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11320                   ProcessDeclaration(decl);
11321             }
11322             if(stmt.compound.statements)
11323             {
11324                for(s = stmt.compound.statements->first; s; s = s.next)
11325                   ProcessStatement(s);
11326             }
11327
11328             curContext = prevContext;
11329             curCompound = prevCompound;
11330          }
11331          break;
11332       }
11333       case expressionStmt:
11334       {
11335          Expression exp;
11336          if(stmt.expressions)
11337          {
11338             for(exp = stmt.expressions->first; exp; exp = exp.next)
11339                ProcessExpressionType(exp);
11340          }
11341          break;
11342       }
11343       case ifStmt:
11344       {
11345          Expression exp;
11346
11347          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11348          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11349          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11350          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11351          {
11352             ProcessExpressionType(exp);
11353          }
11354          if(stmt.ifStmt.stmt)
11355             ProcessStatement(stmt.ifStmt.stmt);
11356          if(stmt.ifStmt.elseStmt)
11357             ProcessStatement(stmt.ifStmt.elseStmt);
11358          break;
11359       }
11360       case switchStmt:
11361       {
11362          Type oldSwitchType = curSwitchType;
11363          if(stmt.switchStmt.exp)
11364          {
11365             Expression exp;
11366             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11367             {
11368                if(!exp.next)
11369                {
11370                   /*
11371                   Type destType
11372                   {
11373                      kind = intType;
11374                      refCount = 1;
11375                   };
11376                   e.exp.destType = destType;
11377                   */
11378
11379                   ProcessExpressionType(exp);
11380                }
11381                if(!exp.next)
11382                   curSwitchType = exp.expType;
11383             }
11384          }
11385          ProcessStatement(stmt.switchStmt.stmt);
11386          curSwitchType = oldSwitchType;
11387          break;
11388       }
11389       case whileStmt:
11390       {
11391          if(stmt.whileStmt.exp)
11392          {
11393             Expression exp;
11394
11395             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11396             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11397             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11398             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11399             {
11400                ProcessExpressionType(exp);
11401             }
11402          }
11403          if(stmt.whileStmt.stmt)
11404             ProcessStatement(stmt.whileStmt.stmt);
11405          break;
11406       }
11407       case doWhileStmt:
11408       {
11409          if(stmt.doWhile.exp)
11410          {
11411             Expression exp;
11412
11413             if(stmt.doWhile.exp->last)
11414             {
11415                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11416                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11417                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11418             }
11419             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11420             {
11421                ProcessExpressionType(exp);
11422             }
11423          }
11424          if(stmt.doWhile.stmt)
11425             ProcessStatement(stmt.doWhile.stmt);
11426          break;
11427       }
11428       case forStmt:
11429       {
11430          Expression exp;
11431          if(stmt.forStmt.init)
11432             ProcessStatement(stmt.forStmt.init);
11433
11434          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11435          {
11436             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11437             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11438             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11439          }
11440
11441          if(stmt.forStmt.check)
11442             ProcessStatement(stmt.forStmt.check);
11443          if(stmt.forStmt.increment)
11444          {
11445             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11446                ProcessExpressionType(exp);
11447          }
11448
11449          if(stmt.forStmt.stmt)
11450             ProcessStatement(stmt.forStmt.stmt);
11451          break;
11452       }
11453       case forEachStmt:
11454       {
11455          Identifier id = stmt.forEachStmt.id;
11456          OldList * exp = stmt.forEachStmt.exp;
11457          OldList * filter = stmt.forEachStmt.filter;
11458          Statement block = stmt.forEachStmt.stmt;
11459          char iteratorType[1024];
11460          Type source;
11461          Expression e;
11462          bool isBuiltin = exp && exp->last &&
11463             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11464               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11465          Expression arrayExp;
11466          char * typeString = null;
11467          int builtinCount = 0;
11468
11469          for(e = exp ? exp->first : null; e; e = e.next)
11470          {
11471             if(!e.next)
11472             {
11473                FreeType(e.destType);
11474                e.destType = ProcessTypeString("Container", false);
11475             }
11476             if(!isBuiltin || e.next)
11477                ProcessExpressionType(e);
11478          }
11479
11480          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11481          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11482             eClass_IsDerived(source._class.registered, containerClass)))
11483          {
11484             Class _class = source ? source._class.registered : null;
11485             Symbol symbol;
11486             Expression expIt = null;
11487             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11488             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11489             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11490             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11491             stmt.type = compoundStmt;
11492
11493             stmt.compound.context = Context { };
11494             stmt.compound.context.parent = curContext;
11495             curContext = stmt.compound.context;
11496
11497             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11498             {
11499                Class mapClass = eSystem_FindClass(privateModule, "Map");
11500                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11501                isCustomAVLTree = true;
11502                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11503                   isAVLTree = true;
11504                else if(eClass_IsDerived(source._class.registered, mapClass))
11505                   isMap = true;
11506             }
11507             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11508             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11509             {
11510                Class listClass = eSystem_FindClass(privateModule, "List");
11511                isLinkList = true;
11512                isList = eClass_IsDerived(source._class.registered, listClass);
11513             }
11514
11515             if(isArray)
11516             {
11517                Declarator decl;
11518                OldList * specs = MkList();
11519                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11520                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11521                stmt.compound.declarations = MkListOne(
11522                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11523                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11524                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11525                      MkInitializerAssignment(MkExpBrackets(exp))))));
11526             }
11527             else if(isBuiltin)
11528             {
11529                Type type = null;
11530                char typeStringBuf[1024];
11531
11532                // TODO: Merge this code?
11533                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11534                if(((Expression)exp->last).type == castExp)
11535                {
11536                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11537                   if(typeName)
11538                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11539                }
11540
11541                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11542                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11543                   arrayExp.destType._class.registered.templateArgs)
11544                {
11545                   Class templateClass = arrayExp.destType._class.registered;
11546                   typeString = templateClass.templateArgs[2].dataTypeString;
11547                }
11548                else if(arrayExp.list)
11549                {
11550                   // Guess type from expressions in the array
11551                   Expression e;
11552                   for(e = arrayExp.list->first; e; e = e.next)
11553                   {
11554                      ProcessExpressionType(e);
11555                      if(e.expType)
11556                      {
11557                         if(!type) { type = e.expType; type.refCount++; }
11558                         else
11559                         {
11560                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11561                            if(!MatchTypeExpression(e, type, null, false))
11562                            {
11563                               FreeType(type);
11564                               type = e.expType;
11565                               e.expType = null;
11566
11567                               e = arrayExp.list->first;
11568                               ProcessExpressionType(e);
11569                               if(e.expType)
11570                               {
11571                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11572                                  if(!MatchTypeExpression(e, type, null, false))
11573                                  {
11574                                     FreeType(e.expType);
11575                                     e.expType = null;
11576                                     FreeType(type);
11577                                     type = null;
11578                                     break;
11579                                  }
11580                               }
11581                            }
11582                         }
11583                         if(e.expType)
11584                         {
11585                            FreeType(e.expType);
11586                            e.expType = null;
11587                         }
11588                      }
11589                   }
11590                   if(type)
11591                   {
11592                      typeStringBuf[0] = '\0';
11593                      PrintType(type, typeStringBuf, false, true);
11594                      typeString = typeStringBuf;
11595                      FreeType(type);
11596                   }
11597                }
11598                if(typeString)
11599                {
11600                   OldList * initializers = MkList();
11601                   Declarator decl;
11602                   OldList * specs = MkList();
11603                   if(arrayExp.list)
11604                   {
11605                      Expression e;
11606
11607                      builtinCount = arrayExp.list->count;
11608                      type = ProcessTypeString(typeString, false);
11609                      while(e = arrayExp.list->first)
11610                      {
11611                         arrayExp.list->Remove(e);
11612                         e.destType = type;
11613                         type.refCount++;
11614                         ProcessExpressionType(e);
11615                         ListAdd(initializers, MkInitializerAssignment(e));
11616                      }
11617                      FreeType(type);
11618                      delete arrayExp.list;
11619                   }
11620                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11621                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11622                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11623
11624                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11625                      PlugDeclarator(
11626                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11627                         ), MkInitializerList(initializers)))));
11628                   FreeList(exp, FreeExpression);
11629                }
11630                else
11631                {
11632                   arrayExp.expType = ProcessTypeString("Container", false);
11633                   Compiler_Error($"Couldn't determine type of array elements\n");
11634                }
11635
11636                /*
11637                Declarator decl;
11638                OldList * specs = MkList();
11639
11640                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11641                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11642                stmt.compound.declarations = MkListOne(
11643                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11644                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11645                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11646                      MkInitializerAssignment(MkExpBrackets(exp))))));
11647                */
11648             }
11649             else if(isLinkList && !isList)
11650             {
11651                Declarator decl;
11652                OldList * specs = MkList();
11653                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11654                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11655                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11656                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11657                      MkInitializerAssignment(MkExpBrackets(exp))))));
11658             }
11659             /*else if(isCustomAVLTree)
11660             {
11661                Declarator decl;
11662                OldList * specs = MkList();
11663                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11664                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11665                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11666                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11667                      MkInitializerAssignment(MkExpBrackets(exp))))));
11668             }*/
11669             else if(_class.templateArgs)
11670             {
11671                if(isMap)
11672                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11673                else
11674                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11675
11676                stmt.compound.declarations = MkListOne(
11677                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11678                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11679                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11680             }
11681             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11682
11683             if(block)
11684             {
11685                // Reparent sub-contexts in this statement
11686                switch(block.type)
11687                {
11688                   case compoundStmt:
11689                      if(block.compound.context)
11690                         block.compound.context.parent = stmt.compound.context;
11691                      break;
11692                   case ifStmt:
11693                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11694                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11695                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11696                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11697                      break;
11698                   case switchStmt:
11699                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11700                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11701                      break;
11702                   case whileStmt:
11703                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11704                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11705                      break;
11706                   case doWhileStmt:
11707                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11708                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11709                      break;
11710                   case forStmt:
11711                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11712                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11713                      break;
11714                   case forEachStmt:
11715                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11716                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11717                      break;
11718                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11719                   case labeledStmt:
11720                   case caseStmt
11721                   case expressionStmt:
11722                   case gotoStmt:
11723                   case continueStmt:
11724                   case breakStmt
11725                   case returnStmt:
11726                   case asmStmt:
11727                   case badDeclarationStmt:
11728                   case fireWatchersStmt:
11729                   case stopWatchingStmt:
11730                   case watchStmt:
11731                   */
11732                }
11733             }
11734             if(filter)
11735             {
11736                block = MkIfStmt(filter, block, null);
11737             }
11738             if(isArray)
11739             {
11740                stmt.compound.statements = MkListOne(MkForStmt(
11741                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11742                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11743                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11744                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11745                   block));
11746               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11747               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11748               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11749             }
11750             else if(isBuiltin)
11751             {
11752                char count[128];
11753                //OldList * specs = MkList();
11754                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11755
11756                sprintf(count, "%d", builtinCount);
11757
11758                stmt.compound.statements = MkListOne(MkForStmt(
11759                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11760                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11761                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11762                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11763                   block));
11764
11765                /*
11766                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11767                stmt.compound.statements = MkListOne(MkForStmt(
11768                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11769                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11770                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11771                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11772                   block));
11773               */
11774               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11775               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11776               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11777             }
11778             else if(isLinkList && !isList)
11779             {
11780                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11781                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11782                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11783                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11784                {
11785                   stmt.compound.statements = MkListOne(MkForStmt(
11786                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11787                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11788                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11789                      block));
11790                }
11791                else
11792                {
11793                   OldList * specs = MkList();
11794                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11795                   stmt.compound.statements = MkListOne(MkForStmt(
11796                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11797                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11798                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11799                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11800                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11801                      block));
11802                }
11803                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11804                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11805                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11806             }
11807             /*else if(isCustomAVLTree)
11808             {
11809                stmt.compound.statements = MkListOne(MkForStmt(
11810                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11811                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11812                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11813                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11814                   block));
11815
11816                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11817                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11818                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11819             }*/
11820             else
11821             {
11822                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11823                   MkIdentifier("Next")), null)), block));
11824             }
11825             ProcessExpressionType(expIt);
11826             if(stmt.compound.declarations->first)
11827                ProcessDeclaration(stmt.compound.declarations->first);
11828
11829             if(symbol)
11830                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11831
11832             ProcessStatement(stmt);
11833             curContext = stmt.compound.context.parent;
11834             break;
11835          }
11836          else
11837          {
11838             Compiler_Error($"Expression is not a container\n");
11839          }
11840          break;
11841       }
11842       case gotoStmt:
11843          break;
11844       case continueStmt:
11845          break;
11846       case breakStmt:
11847          break;
11848       case returnStmt:
11849       {
11850          Expression exp;
11851          if(stmt.expressions)
11852          {
11853             for(exp = stmt.expressions->first; exp; exp = exp.next)
11854             {
11855                if(!exp.next)
11856                {
11857                   if(curFunction && !curFunction.type)
11858                      curFunction.type = ProcessType(
11859                         curFunction.specifiers, curFunction.declarator);
11860                   FreeType(exp.destType);
11861                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11862                   if(exp.destType) exp.destType.refCount++;
11863                }
11864                ProcessExpressionType(exp);
11865             }
11866          }
11867          break;
11868       }
11869       case badDeclarationStmt:
11870       {
11871          ProcessDeclaration(stmt.decl);
11872          break;
11873       }
11874       case asmStmt:
11875       {
11876          AsmField field;
11877          if(stmt.asmStmt.inputFields)
11878          {
11879             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11880                if(field.expression)
11881                   ProcessExpressionType(field.expression);
11882          }
11883          if(stmt.asmStmt.outputFields)
11884          {
11885             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11886                if(field.expression)
11887                   ProcessExpressionType(field.expression);
11888          }
11889          if(stmt.asmStmt.clobberedFields)
11890          {
11891             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11892             {
11893                if(field.expression)
11894                   ProcessExpressionType(field.expression);
11895             }
11896          }
11897          break;
11898       }
11899       case watchStmt:
11900       {
11901          PropertyWatch propWatch;
11902          OldList * watches = stmt._watch.watches;
11903          Expression object = stmt._watch.object;
11904          Expression watcher = stmt._watch.watcher;
11905          if(watcher)
11906             ProcessExpressionType(watcher);
11907          if(object)
11908             ProcessExpressionType(object);
11909
11910          if(inCompiler)
11911          {
11912             if(watcher || thisClass)
11913             {
11914                External external = curExternal;
11915                Context context = curContext;
11916
11917                stmt.type = expressionStmt;
11918                stmt.expressions = MkList();
11919
11920                curExternal = external.prev;
11921
11922                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11923                {
11924                   ClassFunction func;
11925                   char watcherName[1024];
11926                   Class watcherClass = watcher ?
11927                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11928                   External createdExternal;
11929
11930                   // Create a declaration above
11931                   External externalDecl = MkExternalDeclaration(null);
11932                   ast->Insert(curExternal.prev, externalDecl);
11933
11934                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11935                   if(propWatch.deleteWatch)
11936                      strcat(watcherName, "_delete");
11937                   else
11938                   {
11939                      Identifier propID;
11940                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11941                      {
11942                         strcat(watcherName, "_");
11943                         strcat(watcherName, propID.string);
11944                      }
11945                   }
11946
11947                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11948                   {
11949                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11950                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11951                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11952                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11953                      ProcessClassFunctionBody(func, propWatch.compound);
11954                      propWatch.compound = null;
11955
11956                      //afterExternal = afterExternal ? afterExternal : curExternal;
11957
11958                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11959                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11960                      // TESTING THIS...
11961                      createdExternal.symbol.idCode = external.symbol.idCode;
11962
11963                      curExternal = createdExternal;
11964                      ProcessFunction(createdExternal.function);
11965
11966
11967                      // Create a declaration above
11968                      {
11969                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
11970                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11971                         externalDecl.declaration = decl;
11972                         if(decl.symbol && !decl.symbol.pointerExternal)
11973                            decl.symbol.pointerExternal = externalDecl;
11974                      }
11975
11976                      if(propWatch.deleteWatch)
11977                      {
11978                         OldList * args = MkList();
11979                         ListAdd(args, CopyExpression(object));
11980                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11981                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11982                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11983                      }
11984                      else
11985                      {
11986                         Class _class = object.expType._class.registered;
11987                         Identifier propID;
11988
11989                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11990                         {
11991                            char propName[1024];
11992                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11993                            if(prop)
11994                            {
11995                               char getName[1024], setName[1024];
11996                               OldList * args = MkList();
11997
11998                               DeclareProperty(prop, setName, getName);
11999
12000                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12001                               strcpy(propName, "__ecereProp_");
12002                               FullClassNameCat(propName, prop._class.fullName, false);
12003                               strcat(propName, "_");
12004                               // strcat(propName, prop.name);
12005                               FullClassNameCat(propName, prop.name, true);
12006
12007                               ListAdd(args, CopyExpression(object));
12008                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12009                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12010                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12011
12012                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12013                            }
12014                            else
12015                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12016                         }
12017                      }
12018                   }
12019                   else
12020                      Compiler_Error($"Invalid watched object\n");
12021                }
12022
12023                curExternal = external;
12024                curContext = context;
12025
12026                if(watcher)
12027                   FreeExpression(watcher);
12028                if(object)
12029                   FreeExpression(object);
12030                FreeList(watches, FreePropertyWatch);
12031             }
12032             else
12033                Compiler_Error($"No observer specified and not inside a _class\n");
12034          }
12035          else
12036          {
12037             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12038             {
12039                ProcessStatement(propWatch.compound);
12040             }
12041
12042          }
12043          break;
12044       }
12045       case fireWatchersStmt:
12046       {
12047          OldList * watches = stmt._watch.watches;
12048          Expression object = stmt._watch.object;
12049          Class _class;
12050          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12051          // printf("%X\n", watches);
12052          // printf("%X\n", stmt._watch.watches);
12053          if(object)
12054             ProcessExpressionType(object);
12055
12056          if(inCompiler)
12057          {
12058             _class = object ?
12059                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12060
12061             if(_class)
12062             {
12063                Identifier propID;
12064
12065                stmt.type = expressionStmt;
12066                stmt.expressions = MkList();
12067
12068                // Check if we're inside a property set
12069                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12070                {
12071                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12072                }
12073                else if(!watches)
12074                {
12075                   //Compiler_Error($"No property specified and not inside a property set\n");
12076                }
12077                if(watches)
12078                {
12079                   for(propID = watches->first; propID; propID = propID.next)
12080                   {
12081                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12082                      if(prop)
12083                      {
12084                         CreateFireWatcher(prop, object, stmt);
12085                      }
12086                      else
12087                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12088                   }
12089                }
12090                else
12091                {
12092                   // Fire all properties!
12093                   Property prop;
12094                   Class base;
12095                   for(base = _class; base; base = base.base)
12096                   {
12097                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12098                      {
12099                         if(prop.isProperty && prop.isWatchable)
12100                         {
12101                            CreateFireWatcher(prop, object, stmt);
12102                         }
12103                      }
12104                   }
12105                }
12106
12107                if(object)
12108                   FreeExpression(object);
12109                FreeList(watches, FreeIdentifier);
12110             }
12111             else
12112                Compiler_Error($"Invalid object specified and not inside a class\n");
12113          }
12114          break;
12115       }
12116       case stopWatchingStmt:
12117       {
12118          OldList * watches = stmt._watch.watches;
12119          Expression object = stmt._watch.object;
12120          Expression watcher = stmt._watch.watcher;
12121          Class _class;
12122          if(object)
12123             ProcessExpressionType(object);
12124          if(watcher)
12125             ProcessExpressionType(watcher);
12126          if(inCompiler)
12127          {
12128             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12129
12130             if(watcher || thisClass)
12131             {
12132                if(_class)
12133                {
12134                   Identifier propID;
12135
12136                   stmt.type = expressionStmt;
12137                   stmt.expressions = MkList();
12138
12139                   if(!watches)
12140                   {
12141                      OldList * args;
12142                      // eInstance_StopWatching(object, null, watcher);
12143                      args = MkList();
12144                      ListAdd(args, CopyExpression(object));
12145                      ListAdd(args, MkExpConstant("0"));
12146                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12147                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12148                   }
12149                   else
12150                   {
12151                      for(propID = watches->first; propID; propID = propID.next)
12152                      {
12153                         char propName[1024];
12154                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12155                         if(prop)
12156                         {
12157                            char getName[1024], setName[1024];
12158                            OldList * args = MkList();
12159
12160                            DeclareProperty(prop, setName, getName);
12161
12162                            // eInstance_StopWatching(object, prop, watcher);
12163                            strcpy(propName, "__ecereProp_");
12164                            FullClassNameCat(propName, prop._class.fullName, false);
12165                            strcat(propName, "_");
12166                            // strcat(propName, prop.name);
12167                            FullClassNameCat(propName, prop.name, true);
12168                            MangleClassName(propName);
12169
12170                            ListAdd(args, CopyExpression(object));
12171                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12172                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12173                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12174                         }
12175                         else
12176                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12177                      }
12178                   }
12179
12180                   if(object)
12181                      FreeExpression(object);
12182                   if(watcher)
12183                      FreeExpression(watcher);
12184                   FreeList(watches, FreeIdentifier);
12185                }
12186                else
12187                   Compiler_Error($"Invalid object specified and not inside a class\n");
12188             }
12189             else
12190                Compiler_Error($"No observer specified and not inside a class\n");
12191          }
12192          break;
12193       }
12194    }
12195 }
12196
12197 static void ProcessFunction(FunctionDefinition function)
12198 {
12199    Identifier id = GetDeclId(function.declarator);
12200    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12201    Type type = symbol ? symbol.type : null;
12202    Class oldThisClass = thisClass;
12203    Context oldTopContext = topContext;
12204
12205    yylloc = function.loc;
12206    // Process thisClass
12207
12208    if(type && type.thisClass)
12209    {
12210       Symbol classSym = type.thisClass;
12211       Class _class = type.thisClass.registered;
12212       char className[1024];
12213       char structName[1024];
12214       Declarator funcDecl;
12215       Symbol thisSymbol;
12216
12217       bool typedObject = false;
12218
12219       if(_class && !_class.base)
12220       {
12221          _class = currentClass;
12222          if(_class && !_class.symbol)
12223             _class.symbol = FindClass(_class.fullName);
12224          classSym = _class ? _class.symbol : null;
12225          typedObject = true;
12226       }
12227
12228       thisClass = _class;
12229
12230       if(inCompiler && _class)
12231       {
12232          if(type.kind == functionType)
12233          {
12234             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12235             {
12236                //TypeName param = symbol.type.params.first;
12237                Type param = symbol.type.params.first;
12238                symbol.type.params.Remove(param);
12239                //FreeTypeName(param);
12240                FreeType(param);
12241             }
12242             if(type.classObjectType != classPointer)
12243             {
12244                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12245                symbol.type.staticMethod = true;
12246                symbol.type.thisClass = null;
12247
12248                // HIGH DANGER: VERIFYING THIS...
12249                symbol.type.extraParam = false;
12250             }
12251          }
12252
12253          strcpy(className, "__ecereClass_");
12254          FullClassNameCat(className, _class.fullName, true);
12255
12256          MangleClassName(className);
12257
12258          structName[0] = 0;
12259          FullClassNameCat(structName, _class.fullName, false);
12260
12261          // [class] this
12262
12263
12264          funcDecl = GetFuncDecl(function.declarator);
12265          if(funcDecl)
12266          {
12267             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12268             {
12269                TypeName param = funcDecl.function.parameters->first;
12270                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12271                {
12272                   funcDecl.function.parameters->Remove(param);
12273                   FreeTypeName(param);
12274                }
12275             }
12276
12277             // DANGER: Watch for this... Check if it's a Conversion?
12278             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12279
12280             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12281             if(!function.propertyNoThis)
12282             {
12283                TypeName thisParam;
12284
12285                if(type.classObjectType != classPointer)
12286                {
12287                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12288                   if(!funcDecl.function.parameters)
12289                      funcDecl.function.parameters = MkList();
12290                   funcDecl.function.parameters->Insert(null, thisParam);
12291                }
12292
12293                if(typedObject)
12294                {
12295                   if(type.classObjectType != classPointer)
12296                   {
12297                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12298                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12299                   }
12300
12301                   thisParam = TypeName
12302                   {
12303                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12304                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12305                   };
12306                   funcDecl.function.parameters->Insert(null, thisParam);
12307                }
12308             }
12309          }
12310
12311          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12312          {
12313             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12314             funcDecl = GetFuncDecl(initDecl.declarator);
12315             if(funcDecl)
12316             {
12317                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12318                {
12319                   TypeName param = funcDecl.function.parameters->first;
12320                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12321                   {
12322                      funcDecl.function.parameters->Remove(param);
12323                      FreeTypeName(param);
12324                   }
12325                }
12326
12327                if(type.classObjectType != classPointer)
12328                {
12329                   // DANGER: Watch for this... Check if it's a Conversion?
12330                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12331                   {
12332                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12333
12334                      if(!funcDecl.function.parameters)
12335                         funcDecl.function.parameters = MkList();
12336                      funcDecl.function.parameters->Insert(null, thisParam);
12337                   }
12338                }
12339             }
12340          }
12341       }
12342
12343       // Add this to the context
12344       if(function.body)
12345       {
12346          if(type.classObjectType != classPointer)
12347          {
12348             thisSymbol = Symbol
12349             {
12350                string = CopyString("this");
12351                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12352             };
12353             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12354
12355             if(typedObject && thisSymbol.type)
12356             {
12357                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12358                thisSymbol.type.byReference = type.byReference;
12359                thisSymbol.type.typedByReference = type.byReference;
12360                /*
12361                thisSymbol = Symbol { string = CopyString("class") };
12362                function.body.compound.context.symbols.Add(thisSymbol);
12363                */
12364             }
12365          }
12366       }
12367
12368       // Pointer to class data
12369
12370       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12371       {
12372          DataMember member = null;
12373          {
12374             Class base;
12375             for(base = _class; base && base.type != systemClass; base = base.next)
12376             {
12377                for(member = base.membersAndProperties.first; member; member = member.next)
12378                   if(!member.isProperty)
12379                      break;
12380                if(member)
12381                   break;
12382             }
12383          }
12384          for(member = _class.membersAndProperties.first; member; member = member.next)
12385             if(!member.isProperty)
12386                break;
12387          if(member)
12388          {
12389             char pointerName[1024];
12390
12391             Declaration decl;
12392             Initializer initializer;
12393             Expression exp, bytePtr;
12394
12395             strcpy(pointerName, "__ecerePointer_");
12396             FullClassNameCat(pointerName, _class.fullName, false);
12397             {
12398                char className[1024];
12399                strcpy(className, "__ecereClass_");
12400                FullClassNameCat(className, classSym.string, true);
12401                MangleClassName(className);
12402
12403                // Testing This
12404                DeclareClass(classSym, className);
12405             }
12406
12407             // ((byte *) this)
12408             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12409
12410             if(_class.fixed)
12411             {
12412                char string[256];
12413                sprintf(string, "%d", _class.offset);
12414                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12415             }
12416             else
12417             {
12418                // ([bytePtr] + [className]->offset)
12419                exp = QBrackets(MkExpOp(bytePtr, '+',
12420                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12421             }
12422
12423             // (this ? [exp] : 0)
12424             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12425             exp.expType = Type
12426             {
12427                refCount = 1;
12428                kind = pointerType;
12429                type = Type { refCount = 1, kind = voidType };
12430             };
12431
12432             if(function.body)
12433             {
12434                yylloc = function.body.loc;
12435                // ([structName] *) [exp]
12436                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12437                initializer = MkInitializerAssignment(
12438                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12439
12440                // [structName] * [pointerName] = [initializer];
12441                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12442
12443                {
12444                   Context prevContext = curContext;
12445                   curContext = function.body.compound.context;
12446
12447                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12448                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12449
12450                   curContext = prevContext;
12451                }
12452
12453                // WHY?
12454                decl.symbol = null;
12455
12456                if(!function.body.compound.declarations)
12457                   function.body.compound.declarations = MkList();
12458                function.body.compound.declarations->Insert(null, decl);
12459             }
12460          }
12461       }
12462
12463
12464       // Loop through the function and replace undeclared identifiers
12465       // which are a member of the class (methods, properties or data)
12466       // by "this.[member]"
12467    }
12468    else
12469       thisClass = null;
12470
12471    if(id)
12472    {
12473       FreeSpecifier(id._class);
12474       id._class = null;
12475
12476       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12477       {
12478          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12479          id = GetDeclId(initDecl.declarator);
12480
12481          FreeSpecifier(id._class);
12482          id._class = null;
12483       }
12484    }
12485    if(function.body)
12486       topContext = function.body.compound.context;
12487    {
12488       FunctionDefinition oldFunction = curFunction;
12489       curFunction = function;
12490       if(function.body)
12491          ProcessStatement(function.body);
12492
12493       // If this is a property set and no firewatchers has been done yet, add one here
12494       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12495       {
12496          Statement prevCompound = curCompound;
12497          Context prevContext = curContext;
12498
12499          Statement fireWatchers = MkFireWatchersStmt(null, null);
12500          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12501          ListAdd(function.body.compound.statements, fireWatchers);
12502
12503          curCompound = function.body;
12504          curContext = function.body.compound.context;
12505
12506          ProcessStatement(fireWatchers);
12507
12508          curContext = prevContext;
12509          curCompound = prevCompound;
12510
12511       }
12512
12513       curFunction = oldFunction;
12514    }
12515
12516    if(function.declarator)
12517    {
12518       ProcessDeclarator(function.declarator);
12519    }
12520
12521    topContext = oldTopContext;
12522    thisClass = oldThisClass;
12523 }
12524
12525 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12526 static void ProcessClass(OldList definitions, Symbol symbol)
12527 {
12528    ClassDef def;
12529    External external = curExternal;
12530    Class regClass = symbol ? symbol.registered : null;
12531
12532    // Process all functions
12533    for(def = definitions.first; def; def = def.next)
12534    {
12535       if(def.type == functionClassDef)
12536       {
12537          if(def.function.declarator)
12538             curExternal = def.function.declarator.symbol.pointerExternal;
12539          else
12540             curExternal = external;
12541
12542          ProcessFunction((FunctionDefinition)def.function);
12543       }
12544       else if(def.type == declarationClassDef)
12545       {
12546          if(def.decl.type == instDeclaration)
12547          {
12548             thisClass = regClass;
12549             ProcessInstantiationType(def.decl.inst);
12550             thisClass = null;
12551          }
12552          // Testing this
12553          else
12554          {
12555             Class backThisClass = thisClass;
12556             if(regClass) thisClass = regClass;
12557             ProcessDeclaration(def.decl);
12558             thisClass = backThisClass;
12559          }
12560       }
12561       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12562       {
12563          MemberInit defProperty;
12564
12565          // Add this to the context
12566          Symbol thisSymbol = Symbol
12567          {
12568             string = CopyString("this");
12569             type = regClass ? MkClassType(regClass.fullName) : null;
12570          };
12571          globalContext.symbols.Add((BTNode)thisSymbol);
12572
12573          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12574          {
12575             thisClass = regClass;
12576             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12577             thisClass = null;
12578          }
12579
12580          globalContext.symbols.Remove((BTNode)thisSymbol);
12581          FreeSymbol(thisSymbol);
12582       }
12583       else if(def.type == propertyClassDef && def.propertyDef)
12584       {
12585          PropertyDef prop = def.propertyDef;
12586
12587          // Add this to the context
12588          /*
12589          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12590          globalContext.symbols.Add(thisSymbol);
12591          */
12592
12593          thisClass = regClass;
12594          if(prop.setStmt)
12595          {
12596             if(regClass)
12597             {
12598                Symbol thisSymbol
12599                {
12600                   string = CopyString("this");
12601                   type = MkClassType(regClass.fullName);
12602                };
12603                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12604             }
12605
12606             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12607             ProcessStatement(prop.setStmt);
12608          }
12609          if(prop.getStmt)
12610          {
12611             if(regClass)
12612             {
12613                Symbol thisSymbol
12614                {
12615                   string = CopyString("this");
12616                   type = MkClassType(regClass.fullName);
12617                };
12618                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12619             }
12620
12621             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12622             ProcessStatement(prop.getStmt);
12623          }
12624          if(prop.issetStmt)
12625          {
12626             if(regClass)
12627             {
12628                Symbol thisSymbol
12629                {
12630                   string = CopyString("this");
12631                   type = MkClassType(regClass.fullName);
12632                };
12633                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12634             }
12635
12636             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12637             ProcessStatement(prop.issetStmt);
12638          }
12639
12640          thisClass = null;
12641
12642          /*
12643          globalContext.symbols.Remove(thisSymbol);
12644          FreeSymbol(thisSymbol);
12645          */
12646       }
12647       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12648       {
12649          PropertyWatch propertyWatch = def.propertyWatch;
12650
12651          thisClass = regClass;
12652          if(propertyWatch.compound)
12653          {
12654             Symbol thisSymbol
12655             {
12656                string = CopyString("this");
12657                type = regClass ? MkClassType(regClass.fullName) : null;
12658             };
12659
12660             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12661
12662             curExternal = null;
12663             ProcessStatement(propertyWatch.compound);
12664          }
12665          thisClass = null;
12666       }
12667    }
12668 }
12669
12670 void DeclareFunctionUtil(String s)
12671 {
12672    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12673    if(function)
12674    {
12675       char name[1024];
12676       name[0] = 0;
12677       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12678          strcpy(name, "__ecereFunction_");
12679       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12680       DeclareFunction(function, name);
12681    }
12682 }
12683
12684 void ComputeDataTypes()
12685 {
12686    External external;
12687    External temp { };
12688    External after = null;
12689
12690    currentClass = null;
12691
12692    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12693
12694    for(external = ast->first; external; external = external.next)
12695    {
12696       if(external.type == declarationExternal)
12697       {
12698          Declaration decl = external.declaration;
12699          if(decl)
12700          {
12701             OldList * decls = decl.declarators;
12702             if(decls)
12703             {
12704                InitDeclarator initDecl = decls->first;
12705                if(initDecl)
12706                {
12707                   Declarator declarator = initDecl.declarator;
12708                   if(declarator && declarator.type == identifierDeclarator)
12709                   {
12710                      Identifier id = declarator.identifier;
12711                      if(id && id.string)
12712                      {
12713                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12714                         {
12715                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12716                            after = external;
12717                         }
12718                      }
12719                   }
12720                }
12721             }
12722          }
12723        }
12724    }
12725
12726    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12727    ast->Insert(after, temp);
12728    curExternal = temp;
12729
12730    DeclareFunctionUtil("eSystem_New");
12731    DeclareFunctionUtil("eSystem_New0");
12732    DeclareFunctionUtil("eSystem_Renew");
12733    DeclareFunctionUtil("eSystem_Renew0");
12734    DeclareFunctionUtil("eSystem_Delete");
12735    DeclareFunctionUtil("eClass_GetProperty");
12736    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12737
12738    DeclareStruct("ecere::com::Class", false);
12739    DeclareStruct("ecere::com::Instance", false);
12740    DeclareStruct("ecere::com::Property", false);
12741    DeclareStruct("ecere::com::DataMember", false);
12742    DeclareStruct("ecere::com::Method", false);
12743    DeclareStruct("ecere::com::SerialBuffer", false);
12744    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12745
12746    ast->Remove(temp);
12747
12748    for(external = ast->first; external; external = external.next)
12749    {
12750       afterExternal = curExternal = external;
12751       if(external.type == functionExternal)
12752       {
12753          currentClass = external.function._class;
12754          ProcessFunction(external.function);
12755       }
12756       // There shouldn't be any _class member access here anyways...
12757       else if(external.type == declarationExternal)
12758       {
12759          currentClass = null;
12760          if(external.declaration)
12761             ProcessDeclaration(external.declaration);
12762       }
12763       else if(external.type == classExternal)
12764       {
12765          ClassDefinition _class = external._class;
12766          currentClass = external.symbol.registered;
12767          if(_class.definitions)
12768          {
12769             ProcessClass(_class.definitions, _class.symbol);
12770          }
12771          if(inCompiler)
12772          {
12773             // Free class data...
12774             ast->Remove(external);
12775             delete external;
12776          }
12777       }
12778       else if(external.type == nameSpaceExternal)
12779       {
12780          thisNameSpace = external.id.string;
12781       }
12782    }
12783    currentClass = null;
12784    thisNameSpace = null;
12785    curExternal = null;
12786
12787    delete temp.symbol;
12788    delete temp;
12789 }