ecere; compiler; ide: (#486) Added isNan, isInf, signBit, nan, inf; Improved handling...
[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
51       if(exp)
52          OutputExpression(exp, f);
53       f.Seek(0, start);
54       count = strlen(string);
55       count += f.Read(string + count, 1, 1023);
56       string[count] = '\0';
57       delete f;
58    }
59 }
60
61 Type ProcessTemplateParameterType(TemplateParameter param)
62 {
63    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
64    {
65       // TOFIX: Will need to free this Type
66       if(!param.baseType)
67       {
68          if(param.dataTypeString)
69             param.baseType = ProcessTypeString(param.dataTypeString, false);
70          else
71             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
72       }
73       return param.baseType;
74    }
75    return null;
76 }
77
78 bool NeedCast(Type type1, Type type2)
79 {
80    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
81
82    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
83    {
84       return false;
85    }
86
87    if(type1.kind == type2.kind)
88    {
89       switch(type1.kind)
90       {
91          case _BoolType:
92          case charType:
93          case shortType:
94          case intType:
95          case int64Type:
96          case intPtrType:
97          case intSizeType:
98             if(type1.passAsTemplate && !type2.passAsTemplate)
99                return true;
100             return type1.isSigned != type2.isSigned;
101          case classType:
102             return type1._class != type2._class;
103          case pointerType:
104             return NeedCast(type1.type, type2.type);
105          default:
106             return true; //false; ????
107       }
108    }
109    return true;
110 }
111
112 static void ReplaceClassMembers(Expression exp, Class _class)
113 {
114    if(exp.type == identifierExp && exp.identifier)
115    {
116       Identifier id = exp.identifier;
117       Context ctx;
118       Symbol symbol = null;
119       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
120       {
121          // First, check if the identifier is declared inside the function
122          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
123          {
124             symbol = (Symbol)ctx.symbols.FindString(id.string);
125             if(symbol) break;
126          }
127       }
128
129       // If it is not, check if it is a member of the _class
130       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
131       {
132          Property prop = eClass_FindProperty(_class, id.string, privateModule);
133          Method method = null;
134          DataMember member = null;
135          ClassProperty classProp = null;
136          if(!prop)
137          {
138             method = eClass_FindMethod(_class, id.string, privateModule);
139          }
140          if(!prop && !method)
141             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
142          if(!prop && !method && !member)
143          {
144             classProp = eClass_FindClassProperty(_class, id.string);
145          }
146          if(prop || method || member || classProp)
147          {
148             // Replace by this.[member]
149             exp.type = memberExp;
150             exp.member.member = id;
151             exp.member.memberType = unresolvedMember;
152             exp.member.exp = QMkExpId("this");
153             //exp.member.exp.loc = exp.loc;
154             exp.addedThis = true;
155          }
156          else if(_class && _class.templateParams.first)
157          {
158             Class sClass;
159             for(sClass = _class; sClass; sClass = sClass.base)
160             {
161                if(sClass.templateParams.first)
162                {
163                   ClassTemplateParameter param;
164                   for(param = sClass.templateParams.first; param; param = param.next)
165                   {
166                      if(param.type == expression && !strcmp(param.name, id.string))
167                      {
168                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
169
170                         if(argExp)
171                         {
172                            Declarator decl;
173                            OldList * specs = MkList();
174
175                            FreeIdentifier(exp.member.member);
176
177                            ProcessExpressionType(argExp);
178
179                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
180
181                            exp.expType = ProcessType(specs, decl);
182
183                            // *[expType] *[argExp]
184                            exp.type = bracketsExp;
185                            exp.list = MkListOne(MkExpOp(null, '*',
186                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
187                         }
188                      }
189                   }
190                }
191             }
192          }
193       }
194    }
195 }
196
197 ////////////////////////////////////////////////////////////////////////
198 // PRINTING ////////////////////////////////////////////////////////////
199 ////////////////////////////////////////////////////////////////////////
200
201 public char * PrintInt(int64 result)
202 {
203    char temp[100];
204    if(result > MAXINT64)
205       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
206    else
207       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
208    return CopyString(temp);
209 }
210
211 public char * PrintUInt(uint64 result)
212 {
213    char temp[100];
214    if(result > MAXDWORD)
215       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
216    else if(result > MAXINT)
217       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
218    else
219       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
220    return CopyString(temp);
221 }
222
223 public char * PrintInt64(int64 result)
224 {
225    char temp[100];
226    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
227    return CopyString(temp);
228 }
229
230 public char * PrintUInt64(uint64 result)
231 {
232    char temp[100];
233    if(result > MAXINT64)
234       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
235    else
236       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
237    return CopyString(temp);
238 }
239
240 public char * PrintHexUInt(uint64 result)
241 {
242    char temp[100];
243    if(result > MAXDWORD)
244       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
245    else
246       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
247    return CopyString(temp);
248 }
249
250 public char * PrintHexUInt64(uint64 result)
251 {
252    char temp[100];
253    if(result > MAXDWORD)
254       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
255    else
256       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
257    return CopyString(temp);
258 }
259
260 public char * PrintShort(short result)
261 {
262    char temp[100];
263    sprintf(temp, "%d", (unsigned short)result);
264    return CopyString(temp);
265 }
266
267 public char * PrintUShort(unsigned short result)
268 {
269    char temp[100];
270    if(result > 32767)
271       sprintf(temp, "0x%X", (int)result);
272    else
273       sprintf(temp, "%d", (int)result);
274    return CopyString(temp);
275 }
276
277 public char * PrintChar(char result)
278 {
279    char temp[100];
280    if(result > 0 && isprint(result))
281       sprintf(temp, "'%c'", result);
282    else if(result < 0)
283       sprintf(temp, "%d", (int)result);
284    else
285       //sprintf(temp, "%#X", result);
286       sprintf(temp, "0x%X", (unsigned char)result);
287    return CopyString(temp);
288 }
289
290 public char * PrintUChar(unsigned char result)
291 {
292    char temp[100];
293    sprintf(temp, "0x%X", result);
294    return CopyString(temp);
295 }
296
297 public char * PrintFloat(float result)
298 {
299    char temp[350];
300    if(result.isInf)
301    {
302       if(result.signBit)
303          strcpy(temp, "-inf");
304       else
305          strcpy(temp, "inf");
306    }
307    else if(result.isNan)
308    {
309       if(result.signBit)
310          strcpy(temp, "-nan");
311       else
312          strcpy(temp, "nan");
313    }
314    else
315       sprintf(temp, "%.16ff", result);
316    return CopyString(temp);
317 }
318
319 public char * PrintDouble(double result)
320 {
321    char temp[350];
322    if(result.isInf)
323    {
324       if(result.signBit)
325          strcpy(temp, "-inf");
326       else
327          strcpy(temp, "inf");
328    }
329    else if(result.isNan)
330    {
331       if(result.signBit)
332          strcpy(temp, "-nan");
333       else
334          strcpy(temp, "nan");
335    }
336    else
337       sprintf(temp, "%.16f", result);
338    return CopyString(temp);
339 }
340
341 ////////////////////////////////////////////////////////////////////////
342 ////////////////////////////////////////////////////////////////////////
343
344 //public Operand GetOperand(Expression exp);
345
346 #define GETVALUE(name, t) \
347    public bool GetOp##name(Operand op2, t * value2) \
348    {                                                        \
349       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
350       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
351       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
352       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
353       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
354       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
355       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
356       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
357       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
358       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
359       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
360       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
361       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
362       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
363       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
364       else                                                                          \
365          return false;                                                              \
366       return true;                                                                  \
367    } \
368    public bool Get##name(Expression exp, t * value2) \
369    {                                                        \
370       Operand op2 = GetOperand(exp);                        \
371       return GetOp##name(op2, value2); \
372    }
373
374 // To help the deubugger currently not preprocessing...
375 #define HELP(x) x
376
377 GETVALUE(Int, HELP(int));
378 GETVALUE(UInt, HELP(unsigned int));
379 GETVALUE(Int64, HELP(int64));
380 GETVALUE(UInt64, HELP(uint64));
381 GETVALUE(IntPtr, HELP(intptr));
382 GETVALUE(UIntPtr, HELP(uintptr));
383 GETVALUE(IntSize, HELP(intsize));
384 GETVALUE(UIntSize, HELP(uintsize));
385 GETVALUE(Short, HELP(short));
386 GETVALUE(UShort, HELP(unsigned short));
387 GETVALUE(Char, HELP(char));
388 GETVALUE(UChar, HELP(unsigned char));
389 GETVALUE(Float, HELP(float));
390 GETVALUE(Double, HELP(double));
391
392 void ComputeExpression(Expression exp);
393
394 void ComputeClassMembers(Class _class, bool isMember)
395 {
396    DataMember member = isMember ? (DataMember) _class : null;
397    Context context = isMember ? null : SetupTemplatesContext(_class);
398    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
399                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
400    {
401       int c;
402       int unionMemberOffset = 0;
403       int bitFields = 0;
404
405       /*
406       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
407          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
408       */
409
410       if(member)
411       {
412          member.memberOffset = 0;
413          if(targetBits < sizeof(void *) * 8)
414             member.structAlignment = 0;
415       }
416       else if(targetBits < sizeof(void *) * 8)
417          _class.structAlignment = 0;
418
419       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
420       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
421          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
422
423       if(!member && _class.destructionWatchOffset)
424          _class.memberOffset += sizeof(OldList);
425
426       // To avoid reentrancy...
427       //_class.structSize = -1;
428
429       {
430          DataMember dataMember;
431          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
432          {
433             if(!dataMember.isProperty)
434             {
435                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
436                {
437                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
438                   /*if(!dataMember.dataType)
439                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
440                      */
441                }
442             }
443          }
444       }
445
446       {
447          DataMember dataMember;
448          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
449          {
450             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
451             {
452                if(!isMember && _class.type == bitClass && dataMember.dataType)
453                {
454                   BitMember bitMember = (BitMember) dataMember;
455                   uint64 mask = 0;
456                   int d;
457
458                   ComputeTypeSize(dataMember.dataType);
459
460                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
461                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
462
463                   _class.memberOffset = bitMember.pos + bitMember.size;
464                   for(d = 0; d<bitMember.size; d++)
465                   {
466                      if(d)
467                         mask <<= 1;
468                      mask |= 1;
469                   }
470                   bitMember.mask = mask << bitMember.pos;
471                }
472                else if(dataMember.type == normalMember && dataMember.dataType)
473                {
474                   int size;
475                   int alignment = 0;
476
477                   // Prevent infinite recursion
478                   if(dataMember.dataType.kind != classType ||
479                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
480                      _class.type != structClass)))
481                      ComputeTypeSize(dataMember.dataType);
482
483                   if(dataMember.dataType.bitFieldCount)
484                   {
485                      bitFields += dataMember.dataType.bitFieldCount;
486                      size = 0;
487                   }
488                   else
489                   {
490                      if(bitFields)
491                      {
492                         int size = (bitFields + 7) / 8;
493
494                         if(isMember)
495                         {
496                            // TESTING THIS PADDING CODE
497                            if(alignment)
498                            {
499                               member.structAlignment = Max(member.structAlignment, alignment);
500
501                               if(member.memberOffset % alignment)
502                                  member.memberOffset += alignment - (member.memberOffset % alignment);
503                            }
504
505                            dataMember.offset = member.memberOffset;
506                            if(member.type == unionMember)
507                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
508                            else
509                            {
510                               member.memberOffset += size;
511                            }
512                         }
513                         else
514                         {
515                            // TESTING THIS PADDING CODE
516                            if(alignment)
517                            {
518                               _class.structAlignment = Max(_class.structAlignment, alignment);
519
520                               if(_class.memberOffset % alignment)
521                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
522                            }
523
524                            dataMember.offset = _class.memberOffset;
525                            _class.memberOffset += size;
526                         }
527                         bitFields = 0;
528                      }
529                      size = dataMember.dataType.size;
530                      alignment = dataMember.dataType.alignment;
531                   }
532
533                   if(isMember)
534                   {
535                      // TESTING THIS PADDING CODE
536                      if(alignment)
537                      {
538                         member.structAlignment = Max(member.structAlignment, alignment);
539
540                         if(member.memberOffset % alignment)
541                            member.memberOffset += alignment - (member.memberOffset % alignment);
542                      }
543
544                      dataMember.offset = member.memberOffset;
545                      if(member.type == unionMember)
546                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
547                      else
548                      {
549                         member.memberOffset += size;
550                      }
551                   }
552                   else
553                   {
554                      // TESTING THIS PADDING CODE
555                      if(alignment)
556                      {
557                         _class.structAlignment = Max(_class.structAlignment, alignment);
558
559                         if(_class.memberOffset % alignment)
560                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
561                      }
562
563                      dataMember.offset = _class.memberOffset;
564                      _class.memberOffset += size;
565                   }
566                }
567                else
568                {
569                   int alignment;
570
571                   ComputeClassMembers((Class)dataMember, true);
572                   alignment = dataMember.structAlignment;
573
574                   if(isMember)
575                   {
576                      if(alignment)
577                      {
578                         if(member.memberOffset % alignment)
579                            member.memberOffset += alignment - (member.memberOffset % alignment);
580
581                         member.structAlignment = Max(member.structAlignment, alignment);
582                      }
583                      dataMember.offset = member.memberOffset;
584                      if(member.type == unionMember)
585                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
586                      else
587                         member.memberOffset += dataMember.memberOffset;
588                   }
589                   else
590                   {
591                      if(alignment)
592                      {
593                         if(_class.memberOffset % alignment)
594                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
595                         _class.structAlignment = Max(_class.structAlignment, alignment);
596                      }
597                      dataMember.offset = _class.memberOffset;
598                      _class.memberOffset += dataMember.memberOffset;
599                   }
600                }
601             }
602          }
603          if(bitFields)
604          {
605             int alignment = 0;
606             int size = (bitFields + 7) / 8;
607
608             if(isMember)
609             {
610                // TESTING THIS PADDING CODE
611                if(alignment)
612                {
613                   member.structAlignment = Max(member.structAlignment, alignment);
614
615                   if(member.memberOffset % alignment)
616                      member.memberOffset += alignment - (member.memberOffset % alignment);
617                }
618
619                if(member.type == unionMember)
620                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
621                else
622                {
623                   member.memberOffset += size;
624                }
625             }
626             else
627             {
628                // TESTING THIS PADDING CODE
629                if(alignment)
630                {
631                   _class.structAlignment = Max(_class.structAlignment, alignment);
632
633                   if(_class.memberOffset % alignment)
634                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
635                }
636                _class.memberOffset += size;
637             }
638             bitFields = 0;
639          }
640       }
641       if(member && member.type == unionMember)
642       {
643          member.memberOffset = unionMemberOffset;
644       }
645
646       if(!isMember)
647       {
648          /*if(_class.type == structClass)
649             _class.size = _class.memberOffset;
650          else
651          */
652
653          if(_class.type != bitClass)
654          {
655             int extra = 0;
656             if(_class.structAlignment)
657             {
658                if(_class.memberOffset % _class.structAlignment)
659                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
660             }
661             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
662             if(!member)
663             {
664                Property prop;
665                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
666                {
667                   if(prop.isProperty && prop.isWatchable)
668                   {
669                      prop.watcherOffset = _class.structSize;
670                      _class.structSize += sizeof(OldList);
671                   }
672                }
673             }
674
675             // Fix Derivatives
676             {
677                OldLink derivative;
678                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
679                {
680                   Class deriv = derivative.data;
681
682                   if(deriv.computeSize)
683                   {
684                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
685                      deriv.offset = /*_class.offset + */_class.structSize;
686                      deriv.memberOffset = 0;
687                      // ----------------------
688
689                      deriv.structSize = deriv.offset;
690
691                      ComputeClassMembers(deriv, false);
692                   }
693                }
694             }
695          }
696       }
697    }
698    if(context)
699       FinishTemplatesContext(context);
700 }
701
702 public void ComputeModuleClasses(Module module)
703 {
704    Class _class;
705    OldLink subModule;
706
707    for(subModule = module.modules.first; subModule; subModule = subModule.next)
708       ComputeModuleClasses(subModule.data);
709    for(_class = module.classes.first; _class; _class = _class.next)
710       ComputeClassMembers(_class, false);
711 }
712
713
714 public int ComputeTypeSize(Type type)
715 {
716    uint size = type ? type.size : 0;
717    if(!size && type && !type.computing)
718    {
719       type.computing = true;
720       switch(type.kind)
721       {
722          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
723          case charType: type.alignment = size = sizeof(char); break;
724          case intType: type.alignment = size = sizeof(int); break;
725          case int64Type: type.alignment = size = sizeof(int64); break;
726          case intPtrType: type.alignment = size = targetBits / 8; break;
727          case intSizeType: type.alignment = size = targetBits / 8; break;
728          case longType: type.alignment = size = sizeof(long); break;
729          case shortType: type.alignment = size = sizeof(short); break;
730          case floatType: type.alignment = size = sizeof(float); break;
731          case doubleType: type.alignment = size = sizeof(double); break;
732          case classType:
733          {
734             Class _class = type._class ? type._class.registered : null;
735
736             if(_class && _class.type == structClass)
737             {
738                // Ensure all members are properly registered
739                ComputeClassMembers(_class, false);
740                type.alignment = _class.structAlignment;
741                size = _class.structSize;
742                if(type.alignment && size % type.alignment)
743                   size += type.alignment - (size % type.alignment);
744
745             }
746             else if(_class && (_class.type == unitClass ||
747                    _class.type == enumClass ||
748                    _class.type == bitClass))
749             {
750                if(!_class.dataType)
751                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
752                size = type.alignment = ComputeTypeSize(_class.dataType);
753             }
754             else
755                size = type.alignment = targetBits / 8; // sizeof(Instance *);
756             break;
757          }
758          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
759          case arrayType:
760             if(type.arraySizeExp)
761             {
762                ProcessExpressionType(type.arraySizeExp);
763                ComputeExpression(type.arraySizeExp);
764                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
765                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
766                {
767                   Location oldLoc = yylloc;
768                   // bool isConstant = type.arraySizeExp.isConstant;
769                   char expression[10240];
770                   expression[0] = '\0';
771                   type.arraySizeExp.expType = null;
772                   yylloc = type.arraySizeExp.loc;
773                   if(inCompiler)
774                      PrintExpression(type.arraySizeExp, expression);
775                   Compiler_Error($"Array size not constant int (%s)\n", expression);
776                   yylloc = oldLoc;
777                }
778                GetInt(type.arraySizeExp, &type.arraySize);
779             }
780             else if(type.enumClass)
781             {
782                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
783                {
784                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
785                }
786                else
787                   type.arraySize = 0;
788             }
789             else
790             {
791                // Unimplemented auto size
792                type.arraySize = 0;
793             }
794
795             size = ComputeTypeSize(type.type) * type.arraySize;
796             if(type.type)
797                type.alignment = type.type.alignment;
798
799             break;
800          case structType:
801          {
802             Type member;
803             for(member = type.members.first; member; member = member.next)
804             {
805                uint addSize = ComputeTypeSize(member);
806
807                member.offset = size;
808                if(member.alignment && size % member.alignment)
809                   member.offset += member.alignment - (size % member.alignment);
810                size = member.offset;
811
812                type.alignment = Max(type.alignment, member.alignment);
813                size += addSize;
814             }
815             if(type.alignment && size % type.alignment)
816                size += type.alignment - (size % type.alignment);
817             break;
818          }
819          case unionType:
820          {
821             Type member;
822             for(member = type.members.first; member; member = member.next)
823             {
824                uint addSize = ComputeTypeSize(member);
825
826                member.offset = size;
827                if(member.alignment && size % member.alignment)
828                   member.offset += member.alignment - (size % member.alignment);
829                size = member.offset;
830
831                type.alignment = Max(type.alignment, member.alignment);
832                size = Max(size, addSize);
833             }
834             if(type.alignment && size % type.alignment)
835                size += type.alignment - (size % type.alignment);
836             break;
837          }
838          case templateType:
839          {
840             TemplateParameter param = type.templateParameter;
841             Type baseType = ProcessTemplateParameterType(param);
842             if(baseType)
843             {
844                size = ComputeTypeSize(baseType);
845                type.alignment = baseType.alignment;
846             }
847             else
848                type.alignment = size = sizeof(uint64);
849             break;
850          }
851          case enumType:
852          {
853             type.alignment = size = sizeof(enum { test });
854             break;
855          }
856          case thisClassType:
857          {
858             type.alignment = size = targetBits / 8; //sizeof(void *);
859             break;
860          }
861       }
862       type.size = size;
863       type.computing = false;
864    }
865    return size;
866 }
867
868
869 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
870 {
871    // This function is in need of a major review when implementing private members etc.
872    DataMember topMember = isMember ? (DataMember) _class : null;
873    uint totalSize = 0;
874    uint maxSize = 0;
875    int alignment, size;
876    DataMember member;
877    Context context = isMember ? null : SetupTemplatesContext(_class);
878    if(addedPadding)
879       *addedPadding = false;
880
881    if(!isMember && _class.base)
882    {
883       maxSize = _class.structSize;
884       //if(_class.base.type != systemClass) // Commented out with new Instance _class
885       {
886          // DANGER: Testing this noHeadClass here...
887          if(_class.type == structClass || _class.type == noHeadClass)
888             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
889          else
890          {
891             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
892             if(maxSize > baseSize)
893                maxSize -= baseSize;
894             else
895                maxSize = 0;
896          }
897       }
898    }
899
900    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
901    {
902       if(!member.isProperty)
903       {
904          switch(member.type)
905          {
906             case normalMember:
907             {
908                if(member.dataTypeString)
909                {
910                   OldList * specs = MkList(), * decls = MkList();
911                   Declarator decl;
912
913                   decl = SpecDeclFromString(member.dataTypeString, specs,
914                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
915                   ListAdd(decls, MkStructDeclarator(decl, null));
916                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
917
918                   if(!member.dataType)
919                      member.dataType = ProcessType(specs, decl);
920
921                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
922
923                   {
924                      Type type = ProcessType(specs, decl);
925                      DeclareType(member.dataType, false, false);
926                      FreeType(type);
927                   }
928                   /*
929                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
930                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
931                      DeclareStruct(member.dataType._class.string, false);
932                   */
933
934                   ComputeTypeSize(member.dataType);
935                   size = member.dataType.size;
936                   alignment = member.dataType.alignment;
937
938                   if(alignment)
939                   {
940                      if(totalSize % alignment)
941                         totalSize += alignment - (totalSize % alignment);
942                   }
943                   totalSize += size;
944                }
945                break;
946             }
947             case unionMember:
948             case structMember:
949             {
950                OldList * specs = MkList(), * list = MkList();
951
952                size = 0;
953                AddMembers(list, (Class)member, true, &size, topClass, null);
954                ListAdd(specs,
955                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
956                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
957                alignment = member.structAlignment;
958
959                if(alignment)
960                {
961                   if(totalSize % alignment)
962                      totalSize += alignment - (totalSize % alignment);
963                }
964                totalSize += size;
965                break;
966             }
967          }
968       }
969    }
970    if(retSize)
971    {
972       if(topMember && topMember.type == unionMember)
973          *retSize = Max(*retSize, totalSize);
974       else
975          *retSize += totalSize;
976    }
977    else if(totalSize < maxSize && _class.type != systemClass)
978    {
979       int autoPadding = 0;
980       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
981          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
982       if(totalSize + autoPadding < maxSize)
983       {
984          char sizeString[50];
985          sprintf(sizeString, "%d", maxSize - totalSize);
986          ListAdd(declarations,
987             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
988             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
989          if(addedPadding)
990             *addedPadding = true;
991       }
992    }
993    if(context)
994       FinishTemplatesContext(context);
995    return topMember ? topMember.memberID : _class.memberID;
996 }
997
998 static int DeclareMembers(Class _class, bool isMember)
999 {
1000    DataMember topMember = isMember ? (DataMember) _class : null;
1001    uint totalSize = 0;
1002    DataMember member;
1003    Context context = isMember ? null : SetupTemplatesContext(_class);
1004
1005    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1006       DeclareMembers(_class.base, false);
1007
1008    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1009    {
1010       if(!member.isProperty)
1011       {
1012          switch(member.type)
1013          {
1014             case normalMember:
1015             {
1016                /*
1017                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1018                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1019                   DeclareStruct(member.dataType._class.string, false);
1020                   */
1021                if(!member.dataType && member.dataTypeString)
1022                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1023                if(member.dataType)
1024                   DeclareType(member.dataType, false, false);
1025                break;
1026             }
1027             case unionMember:
1028             case structMember:
1029             {
1030                DeclareMembers((Class)member, true);
1031                break;
1032             }
1033          }
1034       }
1035    }
1036    if(context)
1037       FinishTemplatesContext(context);
1038
1039    return topMember ? topMember.memberID : _class.memberID;
1040 }
1041
1042 void DeclareStruct(char * name, bool skipNoHead)
1043 {
1044    External external = null;
1045    Symbol classSym = FindClass(name);
1046
1047    if(!inCompiler || !classSym) return;
1048
1049    // We don't need any declaration for bit classes...
1050    if(classSym.registered &&
1051       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1052       return;
1053
1054    /*if(classSym.registered.templateClass)
1055       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1056    */
1057
1058    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1059    {
1060       // Add typedef struct
1061       Declaration decl;
1062       OldList * specifiers, * declarators;
1063       OldList * declarations = null;
1064       char structName[1024];
1065       external = (classSym.registered && classSym.registered.type == structClass) ?
1066          classSym.pointerExternal : classSym.structExternal;
1067
1068       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1069       // Moved this one up because DeclareClass done later will need it
1070
1071       classSym.declaring++;
1072
1073       if(strchr(classSym.string, '<'))
1074       {
1075          if(classSym.registered.templateClass)
1076          {
1077             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1078             classSym.declaring--;
1079          }
1080          return;
1081       }
1082
1083       //if(!skipNoHead)
1084          DeclareMembers(classSym.registered, false);
1085
1086       structName[0] = 0;
1087       FullClassNameCat(structName, name, false);
1088
1089       /*if(!external)
1090          external = MkExternalDeclaration(null);*/
1091
1092       if(!skipNoHead)
1093       {
1094          bool addedPadding = false;
1095          classSym.declaredStructSym = true;
1096
1097          declarations = MkList();
1098
1099          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1100
1101          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1102          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1103
1104          if(!declarations->count || (declarations->count == 1 && addedPadding))
1105          {
1106             FreeList(declarations, FreeClassDef);
1107             declarations = null;
1108          }
1109       }
1110       if(skipNoHead || declarations)
1111       {
1112          if(external && external.declaration)
1113          {
1114             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1115
1116             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1117             {
1118                // TODO: Fix this
1119                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1120
1121                // DANGER
1122                if(classSym.structExternal)
1123                   ast->Move(classSym.structExternal, curExternal.prev);
1124                ast->Move(classSym.pointerExternal, curExternal.prev);
1125
1126                classSym.id = curExternal.symbol.idCode;
1127                classSym.idCode = curExternal.symbol.idCode;
1128                // external = classSym.pointerExternal;
1129                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1130             }
1131          }
1132          else
1133          {
1134             if(!external)
1135                external = MkExternalDeclaration(null);
1136
1137             specifiers = MkList();
1138             declarators = MkList();
1139             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1140
1141             /*
1142             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1143             ListAdd(declarators, MkInitDeclarator(d, null));
1144             */
1145             external.declaration = decl = MkDeclaration(specifiers, declarators);
1146             if(decl.symbol && !decl.symbol.pointerExternal)
1147                decl.symbol.pointerExternal = external;
1148
1149             // For simple classes, keep the declaration as the external to move around
1150             if(classSym.registered && classSym.registered.type == structClass)
1151             {
1152                char className[1024];
1153                strcpy(className, "__ecereClass_");
1154                FullClassNameCat(className, classSym.string, true);
1155                MangleClassName(className);
1156
1157                // Testing This
1158                DeclareClass(classSym, className);
1159
1160                external.symbol = classSym;
1161                classSym.pointerExternal = external;
1162                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1163                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1164             }
1165             else
1166             {
1167                char className[1024];
1168                strcpy(className, "__ecereClass_");
1169                FullClassNameCat(className, classSym.string, true);
1170                MangleClassName(className);
1171
1172                // TOFIX: TESTING THIS...
1173                classSym.structExternal = external;
1174                DeclareClass(classSym, className);
1175                external.symbol = classSym;
1176             }
1177
1178             //if(curExternal)
1179                ast->Insert(curExternal ? curExternal.prev : null, external);
1180          }
1181       }
1182
1183       classSym.declaring--;
1184    }
1185    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1186    {
1187       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1188       // Moved this one up because DeclareClass done later will need it
1189
1190       // TESTING THIS:
1191       classSym.declaring++;
1192
1193       //if(!skipNoHead)
1194       {
1195          if(classSym.registered)
1196             DeclareMembers(classSym.registered, false);
1197       }
1198
1199       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1200       {
1201          // TODO: Fix this
1202          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1203
1204          // DANGER
1205          if(classSym.structExternal)
1206             ast->Move(classSym.structExternal, curExternal.prev);
1207          ast->Move(classSym.pointerExternal, curExternal.prev);
1208
1209          classSym.id = curExternal.symbol.idCode;
1210          classSym.idCode = curExternal.symbol.idCode;
1211          // external = classSym.pointerExternal;
1212          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1213       }
1214
1215       classSym.declaring--;
1216    }
1217    //return external;
1218 }
1219
1220 void DeclareProperty(Property prop, char * setName, char * getName)
1221 {
1222    Symbol symbol = prop.symbol;
1223    char propName[1024];
1224
1225    strcpy(setName, "__ecereProp_");
1226    FullClassNameCat(setName, prop._class.fullName, false);
1227    strcat(setName, "_Set_");
1228    // strcat(setName, prop.name);
1229    FullClassNameCat(setName, prop.name, true);
1230
1231    strcpy(getName, "__ecereProp_");
1232    FullClassNameCat(getName, prop._class.fullName, false);
1233    strcat(getName, "_Get_");
1234    FullClassNameCat(getName, prop.name, true);
1235    // strcat(getName, prop.name);
1236
1237    strcpy(propName, "__ecereProp_");
1238    FullClassNameCat(propName, prop._class.fullName, false);
1239    strcat(propName, "_");
1240    FullClassNameCat(propName, prop.name, true);
1241    // strcat(propName, prop.name);
1242
1243    // To support "char *" property
1244    MangleClassName(getName);
1245    MangleClassName(setName);
1246    MangleClassName(propName);
1247
1248    if(prop._class.type == structClass)
1249       DeclareStruct(prop._class.fullName, false);
1250
1251    if(!symbol || curExternal.symbol.idCode < symbol.id)
1252    {
1253       bool imported = false;
1254       bool dllImport = false;
1255       if(!symbol || symbol._import)
1256       {
1257          if(!symbol)
1258          {
1259             Symbol classSym;
1260             if(!prop._class.symbol)
1261                prop._class.symbol = FindClass(prop._class.fullName);
1262             classSym = prop._class.symbol;
1263             if(classSym && !classSym._import)
1264             {
1265                ModuleImport module;
1266
1267                if(prop._class.module)
1268                   module = FindModule(prop._class.module);
1269                else
1270                   module = mainModule;
1271
1272                classSym._import = ClassImport
1273                {
1274                   name = CopyString(prop._class.fullName);
1275                   isRemote = prop._class.isRemote;
1276                };
1277                module.classes.Add(classSym._import);
1278             }
1279             symbol = prop.symbol = Symbol { };
1280             symbol._import = (ClassImport)PropertyImport
1281             {
1282                name = CopyString(prop.name);
1283                isVirtual = false; //prop.isVirtual;
1284                hasSet = prop.Set ? true : false;
1285                hasGet = prop.Get ? true : false;
1286             };
1287             if(classSym)
1288                classSym._import.properties.Add(symbol._import);
1289          }
1290          imported = true;
1291          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1292          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1293             prop._class.module.importType != staticImport)
1294             dllImport = true;
1295       }
1296
1297       if(!symbol.type)
1298       {
1299          Context context = SetupTemplatesContext(prop._class);
1300          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1301          FinishTemplatesContext(context);
1302       }
1303
1304       // Get
1305       if(prop.Get)
1306       {
1307          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1308          {
1309             Declaration decl;
1310             OldList * specifiers, * declarators;
1311             Declarator d;
1312             OldList * params;
1313             Specifier spec;
1314             External external;
1315             Declarator typeDecl;
1316             bool simple = false;
1317
1318             specifiers = MkList();
1319             declarators = MkList();
1320             params = MkList();
1321
1322             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1323                MkDeclaratorIdentifier(MkIdentifier("this"))));
1324
1325             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1326             //if(imported)
1327             if(dllImport)
1328                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1329
1330             {
1331                Context context = SetupTemplatesContext(prop._class);
1332                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1333                FinishTemplatesContext(context);
1334             }
1335
1336             // Make sure the simple _class's type is declared
1337             for(spec = specifiers->first; spec; spec = spec.next)
1338             {
1339                if(spec.type == nameSpecifier /*SpecifierClass*/)
1340                {
1341                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1342                   {
1343                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1344                      symbol._class = classSym.registered;
1345                      if(classSym.registered && classSym.registered.type == structClass)
1346                      {
1347                         DeclareStruct(spec.name, false);
1348                         simple = true;
1349                      }
1350                   }
1351                }
1352             }
1353
1354             if(!simple)
1355                d = PlugDeclarator(typeDecl, d);
1356             else
1357             {
1358                ListAdd(params, MkTypeName(specifiers,
1359                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1360                specifiers = MkList();
1361             }
1362
1363             d = MkDeclaratorFunction(d, params);
1364
1365             //if(imported)
1366             if(dllImport)
1367                specifiers->Insert(null, MkSpecifier(EXTERN));
1368             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1369                specifiers->Insert(null, MkSpecifier(STATIC));
1370             if(simple)
1371                ListAdd(specifiers, MkSpecifier(VOID));
1372
1373             ListAdd(declarators, MkInitDeclarator(d, null));
1374
1375             decl = MkDeclaration(specifiers, declarators);
1376
1377             external = MkExternalDeclaration(decl);
1378             ast->Insert(curExternal.prev, external);
1379             external.symbol = symbol;
1380             symbol.externalGet = external;
1381
1382             ReplaceThisClassSpecifiers(specifiers, prop._class);
1383
1384             if(typeDecl)
1385                FreeDeclarator(typeDecl);
1386          }
1387          else
1388          {
1389             // Move declaration higher...
1390             ast->Move(symbol.externalGet, curExternal.prev);
1391          }
1392       }
1393
1394       // Set
1395       if(prop.Set)
1396       {
1397          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1398          {
1399             Declaration decl;
1400             OldList * specifiers, * declarators;
1401             Declarator d;
1402             OldList * params;
1403             Specifier spec;
1404             External external;
1405             Declarator typeDecl;
1406
1407             declarators = MkList();
1408             params = MkList();
1409
1410             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1411             if(!prop.conversion || prop._class.type == structClass)
1412             {
1413                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1414                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1415             }
1416
1417             specifiers = MkList();
1418
1419             {
1420                Context context = SetupTemplatesContext(prop._class);
1421                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1422                   MkDeclaratorIdentifier(MkIdentifier("value")));
1423                FinishTemplatesContext(context);
1424             }
1425             ListAdd(params, MkTypeName(specifiers, d));
1426
1427             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1428             //if(imported)
1429             if(dllImport)
1430                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1431             d = MkDeclaratorFunction(d, params);
1432
1433             // Make sure the simple _class's type is declared
1434             for(spec = specifiers->first; spec; spec = spec.next)
1435             {
1436                if(spec.type == nameSpecifier /*SpecifierClass*/)
1437                {
1438                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1439                   {
1440                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1441                      symbol._class = classSym.registered;
1442                      if(classSym.registered && classSym.registered.type == structClass)
1443                         DeclareStruct(spec.name, false);
1444                   }
1445                }
1446             }
1447
1448             ListAdd(declarators, MkInitDeclarator(d, null));
1449
1450             specifiers = MkList();
1451             //if(imported)
1452             if(dllImport)
1453                specifiers->Insert(null, MkSpecifier(EXTERN));
1454             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1455                specifiers->Insert(null, MkSpecifier(STATIC));
1456
1457             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1458             if(!prop.conversion || prop._class.type == structClass)
1459                ListAdd(specifiers, MkSpecifier(VOID));
1460             else
1461                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1462
1463             decl = MkDeclaration(specifiers, declarators);
1464
1465             external = MkExternalDeclaration(decl);
1466             ast->Insert(curExternal.prev, external);
1467             external.symbol = symbol;
1468             symbol.externalSet = external;
1469
1470             ReplaceThisClassSpecifiers(specifiers, prop._class);
1471          }
1472          else
1473          {
1474             // Move declaration higher...
1475             ast->Move(symbol.externalSet, curExternal.prev);
1476          }
1477       }
1478
1479       // Property (for Watchers)
1480       if(!symbol.externalPtr)
1481       {
1482          Declaration decl;
1483          External external;
1484          OldList * specifiers = MkList();
1485
1486          if(imported)
1487             specifiers->Insert(null, MkSpecifier(EXTERN));
1488          else
1489             specifiers->Insert(null, MkSpecifier(STATIC));
1490
1491          ListAdd(specifiers, MkSpecifierName("Property"));
1492
1493          {
1494             OldList * list = MkList();
1495             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1496                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1497
1498             if(!imported)
1499             {
1500                strcpy(propName, "__ecerePropM_");
1501                FullClassNameCat(propName, prop._class.fullName, false);
1502                strcat(propName, "_");
1503                // strcat(propName, prop.name);
1504                FullClassNameCat(propName, prop.name, true);
1505
1506                MangleClassName(propName);
1507
1508                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1509                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1510             }
1511             decl = MkDeclaration(specifiers, list);
1512          }
1513
1514          external = MkExternalDeclaration(decl);
1515          ast->Insert(curExternal.prev, external);
1516          external.symbol = symbol;
1517          symbol.externalPtr = external;
1518       }
1519       else
1520       {
1521          // Move declaration higher...
1522          ast->Move(symbol.externalPtr, curExternal.prev);
1523       }
1524
1525       symbol.id = curExternal.symbol.idCode;
1526    }
1527 }
1528
1529 // ***************** EXPRESSION PROCESSING ***************************
1530 public Type Dereference(Type source)
1531 {
1532    Type type = null;
1533    if(source)
1534    {
1535       if(source.kind == pointerType || source.kind == arrayType)
1536       {
1537          type = source.type;
1538          source.type.refCount++;
1539       }
1540       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1541       {
1542          type = Type
1543          {
1544             kind = charType;
1545             refCount = 1;
1546          };
1547       }
1548       // Support dereferencing of no head classes for now...
1549       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1550       {
1551          type = source;
1552          source.refCount++;
1553       }
1554       else
1555          Compiler_Error($"cannot dereference type\n");
1556    }
1557    return type;
1558 }
1559
1560 static Type Reference(Type source)
1561 {
1562    Type type = null;
1563    if(source)
1564    {
1565       type = Type
1566       {
1567          kind = pointerType;
1568          type = source;
1569          refCount = 1;
1570       };
1571       source.refCount++;
1572    }
1573    return type;
1574 }
1575
1576 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1577 {
1578    Identifier ident = member.identifiers ? member.identifiers->first : null;
1579    bool found = false;
1580    DataMember dataMember = null;
1581    Method method = null;
1582    bool freeType = false;
1583
1584    yylloc = member.loc;
1585
1586    if(!ident)
1587    {
1588       if(curMember)
1589       {
1590          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1591          if(*curMember)
1592          {
1593             found = true;
1594             dataMember = *curMember;
1595          }
1596       }
1597    }
1598    else
1599    {
1600       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1601       DataMember _subMemberStack[256];
1602       int _subMemberStackPos = 0;
1603
1604       // FILL MEMBER STACK
1605       if(!thisMember)
1606          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1607       if(thisMember)
1608       {
1609          dataMember = thisMember;
1610          if(curMember && thisMember.memberAccess == publicAccess)
1611          {
1612             *curMember = thisMember;
1613             *curClass = thisMember._class;
1614             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1615             *subMemberStackPos = _subMemberStackPos;
1616          }
1617          found = true;
1618       }
1619       else
1620       {
1621          // Setting a method
1622          method = eClass_FindMethod(_class, ident.string, privateModule);
1623          if(method && method.type == virtualMethod)
1624             found = true;
1625          else
1626             method = null;
1627       }
1628    }
1629
1630    if(found)
1631    {
1632       Type type = null;
1633       if(dataMember)
1634       {
1635          if(!dataMember.dataType && dataMember.dataTypeString)
1636          {
1637             //Context context = SetupTemplatesContext(dataMember._class);
1638             Context context = SetupTemplatesContext(_class);
1639             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1640             FinishTemplatesContext(context);
1641          }
1642          type = dataMember.dataType;
1643       }
1644       else if(method)
1645       {
1646          // This is for destination type...
1647          if(!method.dataType)
1648             ProcessMethodType(method);
1649          //DeclareMethod(method);
1650          // method.dataType = ((Symbol)method.symbol)->type;
1651          type = method.dataType;
1652       }
1653
1654       if(ident && ident.next)
1655       {
1656          for(ident = ident.next; ident && type; ident = ident.next)
1657          {
1658             if(type.kind == classType)
1659             {
1660                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1661                if(!dataMember)
1662                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1663                if(dataMember)
1664                   type = dataMember.dataType;
1665             }
1666             else if(type.kind == structType || type.kind == unionType)
1667             {
1668                Type memberType;
1669                for(memberType = type.members.first; memberType; memberType = memberType.next)
1670                {
1671                   if(!strcmp(memberType.name, ident.string))
1672                   {
1673                      type = memberType;
1674                      break;
1675                   }
1676                }
1677             }
1678          }
1679       }
1680
1681       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1682       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1683       {
1684          int id = 0;
1685          ClassTemplateParameter curParam = null;
1686          Class sClass;
1687          for(sClass = _class; sClass; sClass = sClass.base)
1688          {
1689             id = 0;
1690             if(sClass.templateClass) sClass = sClass.templateClass;
1691             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1692             {
1693                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1694                {
1695                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1696                   {
1697                      if(sClass.templateClass) sClass = sClass.templateClass;
1698                      id += sClass.templateParams.count;
1699                   }
1700                   break;
1701                }
1702                id++;
1703             }
1704             if(curParam) break;
1705          }
1706
1707          if(curParam)
1708          {
1709             ClassTemplateArgument arg = _class.templateArgs[id];
1710             if(arg.dataTypeString)
1711             {
1712                // FreeType(type);
1713                type = ProcessTypeString(arg.dataTypeString, false);
1714                freeType = true;
1715                if(type && _class.templateClass)
1716                   type.passAsTemplate = true;
1717                if(type)
1718                {
1719                   // type.refCount++;
1720                   /*if(!exp.destType)
1721                   {
1722                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1723                      exp.destType.refCount++;
1724                   }*/
1725                }
1726             }
1727          }
1728       }
1729       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1730       {
1731          Class expClass = type._class.registered;
1732          Class cClass = null;
1733          int c;
1734          int paramCount = 0;
1735          int lastParam = -1;
1736
1737          char templateString[1024];
1738          ClassTemplateParameter param;
1739          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1740          for(cClass = expClass; cClass; cClass = cClass.base)
1741          {
1742             int p = 0;
1743             if(cClass.templateClass) cClass = cClass.templateClass;
1744             for(param = cClass.templateParams.first; param; param = param.next)
1745             {
1746                int id = p;
1747                Class sClass;
1748                ClassTemplateArgument arg;
1749                for(sClass = cClass.base; sClass; sClass = sClass.base)
1750                {
1751                   if(sClass.templateClass) sClass = sClass.templateClass;
1752                   id += sClass.templateParams.count;
1753                }
1754                arg = expClass.templateArgs[id];
1755
1756                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1757                {
1758                   ClassTemplateParameter cParam;
1759                   //int p = numParams - sClass.templateParams.count;
1760                   int p = 0;
1761                   Class nextClass;
1762                   if(sClass.templateClass) sClass = sClass.templateClass;
1763
1764                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1765                   {
1766                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1767                      p += nextClass.templateParams.count;
1768                   }
1769
1770                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1771                   {
1772                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1773                      {
1774                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1775                         {
1776                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1777                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1778                            break;
1779                         }
1780                      }
1781                   }
1782                }
1783
1784                {
1785                   char argument[256];
1786                   argument[0] = '\0';
1787                   /*if(arg.name)
1788                   {
1789                      strcat(argument, arg.name.string);
1790                      strcat(argument, " = ");
1791                   }*/
1792                   switch(param.type)
1793                   {
1794                      case expression:
1795                      {
1796                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1797                         char expString[1024];
1798                         OldList * specs = MkList();
1799                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1800                         Expression exp;
1801                         char * string = PrintHexUInt64(arg.expression.ui64);
1802                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1803                         delete string;
1804
1805                         ProcessExpressionType(exp);
1806                         ComputeExpression(exp);
1807                         expString[0] = '\0';
1808                         PrintExpression(exp, expString);
1809                         strcat(argument, expString);
1810                         //delete exp;
1811                         FreeExpression(exp);
1812                         break;
1813                      }
1814                      case identifier:
1815                      {
1816                         strcat(argument, arg.member.name);
1817                         break;
1818                      }
1819                      case TemplateParameterType::type:
1820                      {
1821                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1822                            strcat(argument, arg.dataTypeString);
1823                         break;
1824                      }
1825                   }
1826                   if(argument[0])
1827                   {
1828                      if(paramCount) strcat(templateString, ", ");
1829                      if(lastParam != p - 1)
1830                      {
1831                         strcat(templateString, param.name);
1832                         strcat(templateString, " = ");
1833                      }
1834                      strcat(templateString, argument);
1835                      paramCount++;
1836                      lastParam = p;
1837                   }
1838                   p++;
1839                }
1840             }
1841          }
1842          {
1843             int len = strlen(templateString);
1844             if(templateString[len-1] == '<')
1845                len--;
1846             else
1847             {
1848                if(templateString[len-1] == '>')
1849                   templateString[len++] = ' ';
1850                templateString[len++] = '>';
1851             }
1852             templateString[len++] = '\0';
1853          }
1854          {
1855             Context context = SetupTemplatesContext(_class);
1856             if(freeType) FreeType(type);
1857             type = ProcessTypeString(templateString, false);
1858             freeType = true;
1859             FinishTemplatesContext(context);
1860          }
1861       }
1862
1863       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1864       {
1865          ProcessExpressionType(member.initializer.exp);
1866          if(!member.initializer.exp.expType)
1867          {
1868             if(inCompiler)
1869             {
1870                char expString[10240];
1871                expString[0] = '\0';
1872                PrintExpression(member.initializer.exp, expString);
1873                ChangeCh(expString, '\n', ' ');
1874                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1875             }
1876          }
1877          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1878          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1879          {
1880             Compiler_Error($"incompatible instance method %s\n", ident.string);
1881          }
1882       }
1883       else if(member.initializer)
1884       {
1885          /*
1886          FreeType(member.exp.destType);
1887          member.exp.destType = type;
1888          if(member.exp.destType)
1889             member.exp.destType.refCount++;
1890          ProcessExpressionType(member.exp);
1891          */
1892
1893          ProcessInitializer(member.initializer, type);
1894       }
1895       if(freeType) FreeType(type);
1896    }
1897    else
1898    {
1899       if(_class && _class.type == unitClass)
1900       {
1901          if(member.initializer)
1902          {
1903             /*
1904             FreeType(member.exp.destType);
1905             member.exp.destType = MkClassType(_class.fullName);
1906             ProcessExpressionType(member.initializer, type);
1907             */
1908             Type type = MkClassType(_class.fullName);
1909             ProcessInitializer(member.initializer, type);
1910             FreeType(type);
1911          }
1912       }
1913       else
1914       {
1915          if(member.initializer)
1916          {
1917             //ProcessExpressionType(member.exp);
1918             ProcessInitializer(member.initializer, null);
1919          }
1920          if(ident)
1921          {
1922             if(method)
1923             {
1924                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1925             }
1926             else if(_class)
1927             {
1928                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1929                if(inCompiler)
1930                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1931             }
1932          }
1933          else if(_class)
1934             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1935       }
1936    }
1937 }
1938
1939 void ProcessInstantiationType(Instantiation inst)
1940 {
1941    yylloc = inst.loc;
1942    if(inst._class)
1943    {
1944       MembersInit members;
1945       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1946       Class _class;
1947
1948       /*if(!inst._class.symbol)
1949          inst._class.symbol = FindClass(inst._class.name);*/
1950       classSym = inst._class.symbol;
1951       _class = classSym ? classSym.registered : null;
1952
1953       // DANGER: Patch for mutex not declaring its struct when not needed
1954       if(!_class || _class.type != noHeadClass)
1955          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1956
1957       afterExternal = afterExternal ? afterExternal : curExternal;
1958
1959       if(inst.exp)
1960          ProcessExpressionType(inst.exp);
1961
1962       inst.isConstant = true;
1963       if(inst.members)
1964       {
1965          DataMember curMember = null;
1966          Class curClass = null;
1967          DataMember subMemberStack[256];
1968          int subMemberStackPos = 0;
1969
1970          for(members = inst.members->first; members; members = members.next)
1971          {
1972             switch(members.type)
1973             {
1974                case methodMembersInit:
1975                {
1976                   char name[1024];
1977                   static uint instMethodID = 0;
1978                   External external = curExternal;
1979                   Context context = curContext;
1980                   Declarator declarator = members.function.declarator;
1981                   Identifier nameID = GetDeclId(declarator);
1982                   char * unmangled = nameID ? nameID.string : null;
1983                   Expression exp;
1984                   External createdExternal = null;
1985
1986                   if(inCompiler)
1987                   {
1988                      char number[16];
1989                      //members.function.dontMangle = true;
1990                      strcpy(name, "__ecereInstMeth_");
1991                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1992                      strcat(name, "_");
1993                      strcat(name, nameID.string);
1994                      strcat(name, "_");
1995                      sprintf(number, "_%08d", instMethodID++);
1996                      strcat(name, number);
1997                      nameID.string = CopyString(name);
1998                   }
1999
2000                   // Do modifications here...
2001                   if(declarator)
2002                   {
2003                      Symbol symbol = declarator.symbol;
2004                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2005
2006                      if(method && method.type == virtualMethod)
2007                      {
2008                         symbol.method = method;
2009                         ProcessMethodType(method);
2010
2011                         if(!symbol.type.thisClass)
2012                         {
2013                            if(method.dataType.thisClass && currentClass &&
2014                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2015                            {
2016                               if(!currentClass.symbol)
2017                                  currentClass.symbol = FindClass(currentClass.fullName);
2018                               symbol.type.thisClass = currentClass.symbol;
2019                            }
2020                            else
2021                            {
2022                               if(!_class.symbol)
2023                                  _class.symbol = FindClass(_class.fullName);
2024                               symbol.type.thisClass = _class.symbol;
2025                            }
2026                         }
2027                         // TESTING THIS HERE:
2028                         DeclareType(symbol.type, true, true);
2029
2030                      }
2031                      else if(classSym)
2032                      {
2033                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2034                            unmangled, classSym.string);
2035                      }
2036                   }
2037
2038                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2039                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2040
2041                   if(nameID)
2042                   {
2043                      FreeSpecifier(nameID._class);
2044                      nameID._class = null;
2045                   }
2046
2047                   if(inCompiler)
2048                   {
2049
2050                      Type type = declarator.symbol.type;
2051                      External oldExternal = curExternal;
2052
2053                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2054                      // *** It was commented out for problems such as
2055                      /*
2056                            class VirtualDesktop : Window
2057                            {
2058                               clientSize = Size { };
2059                               Timer timer
2060                               {
2061                                  bool DelayExpired()
2062                                  {
2063                                     clientSize.w;
2064                                     return true;
2065                                  }
2066                               };
2067                            }
2068                      */
2069                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2070
2071                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2072
2073                      /*
2074                      if(strcmp(declarator.symbol.string, name))
2075                      {
2076                         printf("TOCHECK: Look out for this\n");
2077                         delete declarator.symbol.string;
2078                         declarator.symbol.string = CopyString(name);
2079                      }
2080
2081                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2082                      {
2083                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2084                         excludedSymbols->Remove(declarator.symbol);
2085                         globalContext.symbols.Add((BTNode)declarator.symbol);
2086                         if(strstr(declarator.symbol.string), "::")
2087                            globalContext.hasNameSpace = true;
2088
2089                      }
2090                      */
2091
2092                      //curExternal = curExternal.prev;
2093                      //afterExternal = afterExternal->next;
2094
2095                      //ProcessFunction(afterExternal->function);
2096
2097                      //curExternal = afterExternal;
2098                      {
2099                         External externalDecl;
2100                         externalDecl = MkExternalDeclaration(null);
2101                         ast->Insert(oldExternal.prev, externalDecl);
2102
2103                         // Which function does this process?
2104                         if(createdExternal.function)
2105                         {
2106                            ProcessFunction(createdExternal.function);
2107
2108                            //curExternal = oldExternal;
2109
2110                            {
2111                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2112
2113                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2114                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2115
2116                               //externalDecl = MkExternalDeclaration(decl);
2117
2118                               //***** ast->Insert(external.prev, externalDecl);
2119                               //ast->Insert(curExternal.prev, externalDecl);
2120                               externalDecl.declaration = decl;
2121                               if(decl.symbol && !decl.symbol.pointerExternal)
2122                                  decl.symbol.pointerExternal = externalDecl;
2123
2124                               // Trying this out...
2125                               declarator.symbol.pointerExternal = externalDecl;
2126                            }
2127                         }
2128                      }
2129                   }
2130                   else if(declarator)
2131                   {
2132                      curExternal = declarator.symbol.pointerExternal;
2133                      ProcessFunction((FunctionDefinition)members.function);
2134                   }
2135                   curExternal = external;
2136                   curContext = context;
2137
2138                   if(inCompiler)
2139                   {
2140                      FreeClassFunction(members.function);
2141
2142                      // In this pass, turn this into a MemberInitData
2143                      exp = QMkExpId(name);
2144                      members.type = dataMembersInit;
2145                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2146
2147                      delete unmangled;
2148                   }
2149                   break;
2150                }
2151                case dataMembersInit:
2152                {
2153                   if(members.dataMembers && classSym)
2154                   {
2155                      MemberInit member;
2156                      Location oldyyloc = yylloc;
2157                      for(member = members.dataMembers->first; member; member = member.next)
2158                      {
2159                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2160                         if(member.initializer && !member.initializer.isConstant)
2161                            inst.isConstant = false;
2162                      }
2163                      yylloc = oldyyloc;
2164                   }
2165                   break;
2166                }
2167             }
2168          }
2169       }
2170    }
2171 }
2172
2173 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2174 {
2175    // OPTIMIZATIONS: TESTING THIS...
2176    if(inCompiler)
2177    {
2178       if(type.kind == functionType)
2179       {
2180          Type param;
2181          if(declareParams)
2182          {
2183             for(param = type.params.first; param; param = param.next)
2184                DeclareType(param, declarePointers, true);
2185          }
2186          DeclareType(type.returnType, declarePointers, true);
2187       }
2188       else if(type.kind == pointerType && declarePointers)
2189          DeclareType(type.type, declarePointers, false);
2190       else if(type.kind == classType)
2191       {
2192          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2193             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2194       }
2195       else if(type.kind == structType || type.kind == unionType)
2196       {
2197          Type member;
2198          for(member = type.members.first; member; member = member.next)
2199             DeclareType(member, false, false);
2200       }
2201       else if(type.kind == arrayType)
2202          DeclareType(type.arrayType, declarePointers, false);
2203    }
2204 }
2205
2206 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2207 {
2208    ClassTemplateArgument * arg = null;
2209    int id = 0;
2210    ClassTemplateParameter curParam = null;
2211    Class sClass;
2212    for(sClass = _class; sClass; sClass = sClass.base)
2213    {
2214       id = 0;
2215       if(sClass.templateClass) sClass = sClass.templateClass;
2216       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2217       {
2218          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2219          {
2220             for(sClass = sClass.base; sClass; sClass = sClass.base)
2221             {
2222                if(sClass.templateClass) sClass = sClass.templateClass;
2223                id += sClass.templateParams.count;
2224             }
2225             break;
2226          }
2227          id++;
2228       }
2229       if(curParam) break;
2230    }
2231    if(curParam)
2232    {
2233       arg = &_class.templateArgs[id];
2234       if(arg && param.type == type)
2235          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2236    }
2237    return arg;
2238 }
2239
2240 public Context SetupTemplatesContext(Class _class)
2241 {
2242    Context context = PushContext();
2243    context.templateTypesOnly = true;
2244    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2245    {
2246       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2247       for(; param; param = param.next)
2248       {
2249          if(param.type == type && param.identifier)
2250          {
2251             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2252             curContext.templateTypes.Add((BTNode)type);
2253          }
2254       }
2255    }
2256    else if(_class)
2257    {
2258       Class sClass;
2259       for(sClass = _class; sClass; sClass = sClass.base)
2260       {
2261          ClassTemplateParameter p;
2262          for(p = sClass.templateParams.first; p; p = p.next)
2263          {
2264             //OldList * specs = MkList();
2265             //Declarator decl = null;
2266             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2267             if(p.type == type)
2268             {
2269                TemplateParameter param = p.param;
2270                TemplatedType type;
2271                if(!param)
2272                {
2273                   // ADD DATA TYPE HERE...
2274                   p.param = param = TemplateParameter
2275                   {
2276                      identifier = MkIdentifier(p.name), type = p.type,
2277                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2278                   };
2279                }
2280                type = TemplatedType { key = (uintptr)p.name, param = param };
2281                curContext.templateTypes.Add((BTNode)type);
2282             }
2283          }
2284       }
2285    }
2286    return context;
2287 }
2288
2289 public void FinishTemplatesContext(Context context)
2290 {
2291    PopContext(context);
2292    FreeContext(context);
2293    delete context;
2294 }
2295
2296 public void ProcessMethodType(Method method)
2297 {
2298    if(!method.dataType)
2299    {
2300       Context context = SetupTemplatesContext(method._class);
2301
2302       method.dataType = ProcessTypeString(method.dataTypeString, false);
2303
2304       FinishTemplatesContext(context);
2305
2306       if(method.type != virtualMethod && method.dataType)
2307       {
2308          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2309          {
2310             if(!method._class.symbol)
2311                method._class.symbol = FindClass(method._class.fullName);
2312             method.dataType.thisClass = method._class.symbol;
2313          }
2314       }
2315
2316       // Why was this commented out? Working fine without now...
2317
2318       /*
2319       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2320          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2321          */
2322    }
2323
2324    /*
2325    if(type)
2326    {
2327       char * par = strstr(type, "(");
2328       char * classOp = null;
2329       int classOpLen = 0;
2330       if(par)
2331       {
2332          int c;
2333          for(c = par-type-1; c >= 0; c++)
2334          {
2335             if(type[c] == ':' && type[c+1] == ':')
2336             {
2337                classOp = type + c - 1;
2338                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2339                {
2340                   classOp--;
2341                   classOpLen++;
2342                }
2343                break;
2344             }
2345             else if(!isspace(type[c]))
2346                break;
2347          }
2348       }
2349       if(classOp)
2350       {
2351          char temp[1024];
2352          int typeLen = strlen(type);
2353          memcpy(temp, classOp, classOpLen);
2354          temp[classOpLen] = '\0';
2355          if(temp[0])
2356             _class = eSystem_FindClass(module, temp);
2357          else
2358             _class = null;
2359          method.dataTypeString = new char[typeLen - classOpLen + 1];
2360          memcpy(method.dataTypeString, type, classOp - type);
2361          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2362       }
2363       else
2364          method.dataTypeString = type;
2365    }
2366    */
2367 }
2368
2369
2370 public void ProcessPropertyType(Property prop)
2371 {
2372    if(!prop.dataType)
2373    {
2374       Context context = SetupTemplatesContext(prop._class);
2375       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2376       FinishTemplatesContext(context);
2377    }
2378 }
2379
2380 public void DeclareMethod(Method method, char * name)
2381 {
2382    Symbol symbol = method.symbol;
2383    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2384    {
2385       bool imported = false;
2386       bool dllImport = false;
2387
2388       if(!method.dataType)
2389          method.dataType = ProcessTypeString(method.dataTypeString, false);
2390
2391       if(!symbol || symbol._import || method.type == virtualMethod)
2392       {
2393          if(!symbol || method.type == virtualMethod)
2394          {
2395             Symbol classSym;
2396             if(!method._class.symbol)
2397                method._class.symbol = FindClass(method._class.fullName);
2398             classSym = method._class.symbol;
2399             if(!classSym._import)
2400             {
2401                ModuleImport module;
2402
2403                if(method._class.module && method._class.module.name)
2404                   module = FindModule(method._class.module);
2405                else
2406                   module = mainModule;
2407                classSym._import = ClassImport
2408                {
2409                   name = CopyString(method._class.fullName);
2410                   isRemote = method._class.isRemote;
2411                };
2412                module.classes.Add(classSym._import);
2413             }
2414             if(!symbol)
2415             {
2416                symbol = method.symbol = Symbol { };
2417             }
2418             if(!symbol._import)
2419             {
2420                symbol._import = (ClassImport)MethodImport
2421                {
2422                   name = CopyString(method.name);
2423                   isVirtual = method.type == virtualMethod;
2424                };
2425                classSym._import.methods.Add(symbol._import);
2426             }
2427             if(!symbol)
2428             {
2429                // Set the symbol type
2430                /*
2431                if(!type.thisClass)
2432                {
2433                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2434                }
2435                else if(type.thisClass == (void *)-1)
2436                {
2437                   type.thisClass = null;
2438                }
2439                */
2440                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2441                symbol.type = method.dataType;
2442                if(symbol.type) symbol.type.refCount++;
2443             }
2444             /*
2445             if(!method.thisClass || strcmp(method.thisClass, "void"))
2446                symbol.type.params.Insert(null,
2447                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2448             */
2449          }
2450          if(!method.dataType.dllExport)
2451          {
2452             imported = true;
2453             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2454                dllImport = true;
2455          }
2456       }
2457
2458       /* MOVING THIS UP
2459       if(!method.dataType)
2460          method.dataType = ((Symbol)method.symbol).type;
2461          //ProcessMethodType(method);
2462       */
2463
2464       if(method.type != virtualMethod && method.dataType)
2465          DeclareType(method.dataType, true, true);
2466
2467       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2468       {
2469          // We need a declaration here :)
2470          Declaration decl;
2471          OldList * specifiers, * declarators;
2472          Declarator d;
2473          Declarator funcDecl;
2474          External external;
2475
2476          specifiers = MkList();
2477          declarators = MkList();
2478
2479          //if(imported)
2480          if(dllImport)
2481             ListAdd(specifiers, MkSpecifier(EXTERN));
2482          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2483             ListAdd(specifiers, MkSpecifier(STATIC));
2484
2485          if(method.type == virtualMethod)
2486          {
2487             ListAdd(specifiers, MkSpecifier(INT));
2488             d = MkDeclaratorIdentifier(MkIdentifier(name));
2489          }
2490          else
2491          {
2492             d = MkDeclaratorIdentifier(MkIdentifier(name));
2493             //if(imported)
2494             if(dllImport)
2495                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2496             {
2497                Context context = SetupTemplatesContext(method._class);
2498                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2499                FinishTemplatesContext(context);
2500             }
2501             funcDecl = GetFuncDecl(d);
2502
2503             if(dllImport)
2504             {
2505                Specifier spec, next;
2506                for(spec = specifiers->first; spec; spec = next)
2507                {
2508                   next = spec.next;
2509                   if(spec.type == extendedSpecifier)
2510                   {
2511                      specifiers->Remove(spec);
2512                      FreeSpecifier(spec);
2513                   }
2514                }
2515             }
2516
2517             // Add this parameter if not a static method
2518             if(method.dataType && !method.dataType.staticMethod)
2519             {
2520                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2521                {
2522                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2523                   TypeName thisParam = MkTypeName(MkListOne(
2524                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2525                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2526                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2527                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2528
2529                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2530                   {
2531                      TypeName param = funcDecl.function.parameters->first;
2532                      funcDecl.function.parameters->Remove(param);
2533                      FreeTypeName(param);
2534                   }
2535
2536                   if(!funcDecl.function.parameters)
2537                      funcDecl.function.parameters = MkList();
2538                   funcDecl.function.parameters->Insert(null, thisParam);
2539                }
2540             }
2541             // Make sure we don't have empty parameter declarations for static methods...
2542             /*
2543             else if(!funcDecl.function.parameters)
2544             {
2545                funcDecl.function.parameters = MkList();
2546                funcDecl.function.parameters->Insert(null,
2547                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2548             }*/
2549          }
2550          // TESTING THIS:
2551          ProcessDeclarator(d);
2552
2553          ListAdd(declarators, MkInitDeclarator(d, null));
2554
2555          decl = MkDeclaration(specifiers, declarators);
2556
2557          ReplaceThisClassSpecifiers(specifiers, method._class);
2558
2559          // Keep a different symbol for the function definition than the declaration...
2560          if(symbol.pointerExternal)
2561          {
2562             Symbol functionSymbol { };
2563
2564             // Copy symbol
2565             {
2566                *functionSymbol = *symbol;
2567                functionSymbol.string = CopyString(symbol.string);
2568                if(functionSymbol.type)
2569                   functionSymbol.type.refCount++;
2570             }
2571
2572             excludedSymbols->Add(functionSymbol);
2573             symbol.pointerExternal.symbol = functionSymbol;
2574          }
2575          external = MkExternalDeclaration(decl);
2576          if(curExternal)
2577             ast->Insert(curExternal ? curExternal.prev : null, external);
2578          external.symbol = symbol;
2579          symbol.pointerExternal = external;
2580       }
2581       else if(ast)
2582       {
2583          // Move declaration higher...
2584          ast->Move(symbol.pointerExternal, curExternal.prev);
2585       }
2586
2587       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2588    }
2589 }
2590
2591 char * ReplaceThisClass(Class _class)
2592 {
2593    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2594    {
2595       bool first = true;
2596       int p = 0;
2597       ClassTemplateParameter param;
2598       int lastParam = -1;
2599
2600       char className[1024];
2601       strcpy(className, _class.fullName);
2602       for(param = _class.templateParams.first; param; param = param.next)
2603       {
2604          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2605          {
2606             if(first) strcat(className, "<");
2607             if(!first) strcat(className, ", ");
2608             if(lastParam + 1 != p)
2609             {
2610                strcat(className, param.name);
2611                strcat(className, " = ");
2612             }
2613             strcat(className, param.name);
2614             first = false;
2615             lastParam = p;
2616          }
2617          p++;
2618       }
2619       if(!first)
2620       {
2621          int len = strlen(className);
2622          if(className[len-1] == '>') className[len++] = ' ';
2623          className[len++] = '>';
2624          className[len++] = '\0';
2625       }
2626       return CopyString(className);
2627    }
2628    else
2629       return CopyString(_class.fullName);
2630 }
2631
2632 Type ReplaceThisClassType(Class _class)
2633 {
2634    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2635    {
2636       bool first = true;
2637       int p = 0;
2638       ClassTemplateParameter param;
2639       int lastParam = -1;
2640       char className[1024];
2641       strcpy(className, _class.fullName);
2642
2643       for(param = _class.templateParams.first; param; param = param.next)
2644       {
2645          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2646          {
2647             if(first) strcat(className, "<");
2648             if(!first) strcat(className, ", ");
2649             if(lastParam + 1 != p)
2650             {
2651                strcat(className, param.name);
2652                strcat(className, " = ");
2653             }
2654             strcat(className, param.name);
2655             first = false;
2656             lastParam = p;
2657          }
2658          p++;
2659       }
2660       if(!first)
2661       {
2662          int len = strlen(className);
2663          if(className[len-1] == '>') className[len++] = ' ';
2664          className[len++] = '>';
2665          className[len++] = '\0';
2666       }
2667       return MkClassType(className);
2668       //return ProcessTypeString(className, false);
2669    }
2670    else
2671    {
2672       return MkClassType(_class.fullName);
2673       //return ProcessTypeString(_class.fullName, false);
2674    }
2675 }
2676
2677 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2678 {
2679    if(specs != null && _class)
2680    {
2681       Specifier spec;
2682       for(spec = specs.first; spec; spec = spec.next)
2683       {
2684          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2685          {
2686             spec.type = nameSpecifier;
2687             spec.name = ReplaceThisClass(_class);
2688             spec.symbol = FindClass(spec.name); //_class.symbol;
2689          }
2690       }
2691    }
2692 }
2693
2694 // Returns imported or not
2695 bool DeclareFunction(GlobalFunction function, char * name)
2696 {
2697    Symbol symbol = function.symbol;
2698    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2699    {
2700       bool imported = false;
2701       bool dllImport = false;
2702
2703       if(!function.dataType)
2704       {
2705          function.dataType = ProcessTypeString(function.dataTypeString, false);
2706          if(!function.dataType.thisClass)
2707             function.dataType.staticMethod = true;
2708       }
2709
2710       if(inCompiler)
2711       {
2712          if(!symbol)
2713          {
2714             ModuleImport module = FindModule(function.module);
2715             // WARNING: This is not added anywhere...
2716             symbol = function.symbol = Symbol {  };
2717
2718             if(module.name)
2719             {
2720                if(!function.dataType.dllExport)
2721                {
2722                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2723                   module.functions.Add(symbol._import);
2724                }
2725             }
2726             // Set the symbol type
2727             {
2728                symbol.type = ProcessTypeString(function.dataTypeString, false);
2729                if(!symbol.type.thisClass)
2730                   symbol.type.staticMethod = true;
2731             }
2732          }
2733          imported = symbol._import ? true : false;
2734          if(imported && function.module != privateModule && function.module.importType != staticImport)
2735             dllImport = true;
2736       }
2737
2738       DeclareType(function.dataType, true, true);
2739
2740       if(inCompiler)
2741       {
2742          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2743          {
2744             // We need a declaration here :)
2745             Declaration decl;
2746             OldList * specifiers, * declarators;
2747             Declarator d;
2748             Declarator funcDecl;
2749             External external;
2750
2751             specifiers = MkList();
2752             declarators = MkList();
2753
2754             //if(imported)
2755                ListAdd(specifiers, MkSpecifier(EXTERN));
2756             /*
2757             else
2758                ListAdd(specifiers, MkSpecifier(STATIC));
2759             */
2760
2761             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2762             //if(imported)
2763             if(dllImport)
2764                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2765
2766             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2767             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2768             if(function.module.importType == staticImport)
2769             {
2770                Specifier spec;
2771                for(spec = specifiers->first; spec; spec = spec.next)
2772                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2773                   {
2774                      specifiers->Remove(spec);
2775                      FreeSpecifier(spec);
2776                      break;
2777                   }
2778             }
2779
2780             funcDecl = GetFuncDecl(d);
2781
2782             // Make sure we don't have empty parameter declarations for static methods...
2783             if(funcDecl && !funcDecl.function.parameters)
2784             {
2785                funcDecl.function.parameters = MkList();
2786                funcDecl.function.parameters->Insert(null,
2787                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2788             }
2789
2790             ListAdd(declarators, MkInitDeclarator(d, null));
2791
2792             {
2793                Context oldCtx = curContext;
2794                curContext = globalContext;
2795                decl = MkDeclaration(specifiers, declarators);
2796                curContext = oldCtx;
2797             }
2798
2799             // Keep a different symbol for the function definition than the declaration...
2800             if(symbol.pointerExternal)
2801             {
2802                Symbol functionSymbol { };
2803                // Copy symbol
2804                {
2805                   *functionSymbol = *symbol;
2806                   functionSymbol.string = CopyString(symbol.string);
2807                   if(functionSymbol.type)
2808                      functionSymbol.type.refCount++;
2809                }
2810
2811                excludedSymbols->Add(functionSymbol);
2812
2813                symbol.pointerExternal.symbol = functionSymbol;
2814             }
2815             external = MkExternalDeclaration(decl);
2816             if(curExternal)
2817                ast->Insert(curExternal.prev, external);
2818             external.symbol = symbol;
2819             symbol.pointerExternal = external;
2820          }
2821          else
2822          {
2823             // Move declaration higher...
2824             ast->Move(symbol.pointerExternal, curExternal.prev);
2825          }
2826
2827          if(curExternal)
2828             symbol.id = curExternal.symbol.idCode;
2829       }
2830    }
2831    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2832 }
2833
2834 void DeclareGlobalData(GlobalData data)
2835 {
2836    Symbol symbol = data.symbol;
2837    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2838    {
2839       if(inCompiler)
2840       {
2841          if(!symbol)
2842             symbol = data.symbol = Symbol { };
2843       }
2844       if(!data.dataType)
2845          data.dataType = ProcessTypeString(data.dataTypeString, false);
2846       DeclareType(data.dataType, true, true);
2847       if(inCompiler)
2848       {
2849          if(!symbol.pointerExternal)
2850          {
2851             // We need a declaration here :)
2852             Declaration decl;
2853             OldList * specifiers, * declarators;
2854             Declarator d;
2855             External external;
2856
2857             specifiers = MkList();
2858             declarators = MkList();
2859
2860             ListAdd(specifiers, MkSpecifier(EXTERN));
2861             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2862             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2863
2864             ListAdd(declarators, MkInitDeclarator(d, null));
2865
2866             decl = MkDeclaration(specifiers, declarators);
2867             external = MkExternalDeclaration(decl);
2868             if(curExternal)
2869                ast->Insert(curExternal.prev, external);
2870             external.symbol = symbol;
2871             symbol.pointerExternal = external;
2872          }
2873          else
2874          {
2875             // Move declaration higher...
2876             ast->Move(symbol.pointerExternal, curExternal.prev);
2877          }
2878
2879          if(curExternal)
2880             symbol.id = curExternal.symbol.idCode;
2881       }
2882    }
2883 }
2884
2885 class Conversion : struct
2886 {
2887    Conversion prev, next;
2888    Property convert;
2889    bool isGet;
2890    Type resultType;
2891 };
2892
2893 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2894 {
2895    if(source && dest)
2896    {
2897       // Property convert;
2898
2899       if(source.kind == templateType && dest.kind != templateType)
2900       {
2901          Type type = ProcessTemplateParameterType(source.templateParameter);
2902          if(type) source = type;
2903       }
2904
2905       if(dest.kind == templateType && source.kind != templateType)
2906       {
2907          Type type = ProcessTemplateParameterType(dest.templateParameter);
2908          if(type) dest = type;
2909       }
2910
2911       if(dest.classObjectType == typedObject)
2912       {
2913          if(source.classObjectType != anyObject)
2914             return true;
2915          else
2916          {
2917             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2918             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2919             {
2920                return true;
2921             }
2922          }
2923       }
2924       else
2925       {
2926          if(source.classObjectType == anyObject)
2927             return true;
2928          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2929             return true;
2930       }
2931
2932       if((dest.kind == structType && source.kind == structType) ||
2933          (dest.kind == unionType && source.kind == unionType))
2934       {
2935          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2936              (source.members.first && source.members.first == dest.members.first))
2937             return true;
2938       }
2939
2940       if(dest.kind == ellipsisType && source.kind != voidType)
2941          return true;
2942
2943       if(dest.kind == pointerType && dest.type.kind == voidType &&
2944          ((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))
2945          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2946
2947          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2948
2949          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2950          return true;
2951       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2952          ((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))
2953          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2954
2955          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2956
2957          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2958          return true;
2959
2960       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2961       {
2962          if(source._class.registered && source._class.registered.type == unitClass)
2963          {
2964             if(conversions != null)
2965             {
2966                if(source._class.registered == dest._class.registered)
2967                   return true;
2968             }
2969             else
2970             {
2971                Class sourceBase, destBase;
2972                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2973                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2974                if(sourceBase == destBase)
2975                   return true;
2976             }
2977          }
2978          // Don't match enum inheriting from other enum if resolving enumeration values
2979          // TESTING: !dest.classObjectType
2980          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2981             (enumBaseType ||
2982                (!source._class.registered || source._class.registered.type != enumClass) ||
2983                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2984             return true;
2985          else
2986          {
2987             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2988             if(enumBaseType &&
2989                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2990                ((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)
2991             {
2992                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2993                {
2994                   return true;
2995                }
2996             }
2997          }
2998       }
2999
3000       // JUST ADDED THIS...
3001       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3002          return true;
3003
3004       if(doConversion)
3005       {
3006          // Just added this for Straight conversion of ColorAlpha => Color
3007          if(source.kind == classType)
3008          {
3009             Class _class;
3010             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3011             {
3012                Property convert;
3013                for(convert = _class.conversions.first; convert; convert = convert.next)
3014                {
3015                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3016                   {
3017                      Conversion after = (conversions != null) ? conversions.last : null;
3018
3019                      if(!convert.dataType)
3020                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3021                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
3022                      {
3023                         if(!conversions && !convert.Get)
3024                            return true;
3025                         else if(conversions != null)
3026                         {
3027                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3028                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3029                               (dest.kind != classType || dest._class.registered != _class.base))
3030                               return true;
3031                            else
3032                            {
3033                               Conversion conv { convert = convert, isGet = true };
3034                               // conversions.Add(conv);
3035                               conversions.Insert(after, conv);
3036                               return true;
3037                            }
3038                         }
3039                      }
3040                   }
3041                }
3042             }
3043          }
3044
3045          // MOVING THIS??
3046
3047          if(dest.kind == classType)
3048          {
3049             Class _class;
3050             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3051             {
3052                Property convert;
3053                for(convert = _class.conversions.first; convert; convert = convert.next)
3054                {
3055                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3056                   {
3057                      // Conversion after = (conversions != null) ? conversions.last : null;
3058
3059                      if(!convert.dataType)
3060                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3061                      // Just added this equality check to prevent recursion.... Make it safer?
3062                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3063                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3064                      {
3065                         if(!conversions && !convert.Set)
3066                            return true;
3067                         else if(conversions != null)
3068                         {
3069                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3070                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3071                               (source.kind != classType || source._class.registered != _class.base))
3072                               return true;
3073                            else
3074                            {
3075                               // *** Testing this! ***
3076                               Conversion conv { convert = convert };
3077                               conversions.Add(conv);
3078                               //conversions.Insert(after, conv);
3079                               return true;
3080                            }
3081                         }
3082                      }
3083                   }
3084                }
3085             }
3086             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3087             {
3088                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3089                   (source.kind != classType || source._class.registered.type != structClass))
3090                   return true;
3091             }*/
3092
3093             // TESTING THIS... IS THIS OK??
3094             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3095             {
3096                if(!dest._class.registered.dataType)
3097                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3098                // Only support this for classes...
3099                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3100                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3101                {
3102                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3103                   {
3104                      return true;
3105                   }
3106                }
3107             }
3108          }
3109
3110          // Moved this lower
3111          if(source.kind == classType)
3112          {
3113             Class _class;
3114             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3115             {
3116                Property convert;
3117                for(convert = _class.conversions.first; convert; convert = convert.next)
3118                {
3119                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3120                   {
3121                      Conversion after = (conversions != null) ? conversions.last : null;
3122
3123                      if(!convert.dataType)
3124                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3125                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3126                      {
3127                         if(!conversions && !convert.Get)
3128                            return true;
3129                         else if(conversions != null)
3130                         {
3131                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3132                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3133                               (dest.kind != classType || dest._class.registered != _class.base))
3134                               return true;
3135                            else
3136                            {
3137                               Conversion conv { convert = convert, isGet = true };
3138
3139                               // conversions.Add(conv);
3140                               conversions.Insert(after, conv);
3141                               return true;
3142                            }
3143                         }
3144                      }
3145                   }
3146                }
3147             }
3148
3149             // TESTING THIS... IS THIS OK??
3150             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3151             {
3152                if(!source._class.registered.dataType)
3153                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3154                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3155                {
3156                   return true;
3157                }
3158             }
3159          }
3160       }
3161
3162       if(source.kind == classType || source.kind == subClassType)
3163          ;
3164       else if(dest.kind == source.kind &&
3165          (dest.kind != structType && dest.kind != unionType &&
3166           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3167           return true;
3168       // RECENTLY ADDED THESE
3169       else if(dest.kind == doubleType && source.kind == floatType)
3170          return true;
3171       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3172          return true;
3173       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3174          return true;
3175       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3176          return true;
3177       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3178          return true;
3179       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3180          return true;
3181       else if(source.kind == enumType &&
3182          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3183           return true;
3184       else if(dest.kind == enumType &&
3185          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3186           return true;
3187       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3188               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3189       {
3190          Type paramSource, paramDest;
3191
3192          if(dest.kind == methodType)
3193             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3194          if(source.kind == methodType)
3195             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3196
3197          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3198          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3199          if(dest.kind == methodType)
3200             dest = dest.method.dataType;
3201          if(source.kind == methodType)
3202             source = source.method.dataType;
3203
3204          paramSource = source.params.first;
3205          if(paramSource && paramSource.kind == voidType) paramSource = null;
3206          paramDest = dest.params.first;
3207          if(paramDest && paramDest.kind == voidType) paramDest = null;
3208
3209
3210          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3211             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3212          {
3213             // Source thisClass must be derived from destination thisClass
3214             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3215                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3216             {
3217                if(paramDest && paramDest.kind == classType)
3218                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3219                else
3220                   Compiler_Error($"method class should not take an object\n");
3221                return false;
3222             }
3223             paramDest = paramDest.next;
3224          }
3225          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3226          {
3227             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3228             {
3229                if(dest.thisClass)
3230                {
3231                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3232                   {
3233                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3234                      return false;
3235                   }
3236                }
3237                else
3238                {
3239                   // THIS WAS BACKWARDS:
3240                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3241                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3242                   {
3243                      if(owningClassDest)
3244                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3245                      else
3246                         Compiler_Error($"overriding class expected to be derived from method class\n");
3247                      return false;
3248                   }
3249                }
3250                paramSource = paramSource.next;
3251             }
3252             else
3253             {
3254                if(dest.thisClass)
3255                {
3256                   // Source thisClass must be derived from destination thisClass
3257                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3258                   {
3259                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3260                      return false;
3261                   }
3262                }
3263                else
3264                {
3265                   // THIS WAS BACKWARDS TOO??
3266                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3267                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3268                   {
3269                      //if(owningClass)
3270                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3271                      //else
3272                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3273                      return false;
3274                   }
3275                }
3276             }
3277          }
3278
3279
3280          // Source return type must be derived from destination return type
3281          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3282          {
3283             Compiler_Warning($"incompatible return type for function\n");
3284             return false;
3285          }
3286
3287          // Check parameters
3288
3289          for(; paramDest; paramDest = paramDest.next)
3290          {
3291             if(!paramSource)
3292             {
3293                //Compiler_Warning($"not enough parameters\n");
3294                Compiler_Error($"not enough parameters\n");
3295                return false;
3296             }
3297             {
3298                Type paramDestType = paramDest;
3299                Type paramSourceType = paramSource;
3300                Type type = paramDestType;
3301
3302                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3303                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3304                   paramSource.kind != templateType)
3305                {
3306                   int id = 0;
3307                   ClassTemplateParameter curParam = null;
3308                   Class sClass;
3309                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3310                   {
3311                      id = 0;
3312                      if(sClass.templateClass) sClass = sClass.templateClass;
3313                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3314                      {
3315                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3316                         {
3317                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3318                            {
3319                               if(sClass.templateClass) sClass = sClass.templateClass;
3320                               id += sClass.templateParams.count;
3321                            }
3322                            break;
3323                         }
3324                         id++;
3325                      }
3326                      if(curParam) break;
3327                   }
3328
3329                   if(curParam)
3330                   {
3331                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3332                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3333                   }
3334                }
3335
3336                // paramDest must be derived from paramSource
3337                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3338                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3339                {
3340                   char type[1024];
3341                   type[0] = 0;
3342                   PrintType(paramDest, type, false, true);
3343                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3344
3345                   if(paramDestType != paramDest)
3346                      FreeType(paramDestType);
3347                   return false;
3348                }
3349                if(paramDestType != paramDest)
3350                   FreeType(paramDestType);
3351             }
3352
3353             paramSource = paramSource.next;
3354          }
3355          if(paramSource)
3356          {
3357             Compiler_Error($"too many parameters\n");
3358             return false;
3359          }
3360          return true;
3361       }
3362       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3363       {
3364          return true;
3365       }
3366       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3367          (source.kind == arrayType || source.kind == pointerType))
3368       {
3369          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3370             return true;
3371       }
3372    }
3373    return false;
3374 }
3375
3376 static void FreeConvert(Conversion convert)
3377 {
3378    if(convert.resultType)
3379       FreeType(convert.resultType);
3380 }
3381
3382 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3383                               char * string, OldList conversions)
3384 {
3385    BTNamedLink link;
3386
3387    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3388    {
3389       Class _class = link.data;
3390       if(_class.type == enumClass)
3391       {
3392          OldList converts { };
3393          Type type { };
3394          type.kind = classType;
3395
3396          if(!_class.symbol)
3397             _class.symbol = FindClass(_class.fullName);
3398          type._class = _class.symbol;
3399
3400          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3401          {
3402             NamedLink value;
3403             Class enumClass = eSystem_FindClass(privateModule, "enum");
3404             if(enumClass)
3405             {
3406                Class baseClass;
3407                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3408                {
3409                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3410                   for(value = e.values.first; value; value = value.next)
3411                   {
3412                      if(!strcmp(value.name, string))
3413                         break;
3414                   }
3415                   if(value)
3416                   {
3417                      FreeExpContents(sourceExp);
3418                      FreeType(sourceExp.expType);
3419
3420                      sourceExp.isConstant = true;
3421                      sourceExp.expType = MkClassType(baseClass.fullName);
3422                      //if(inCompiler)
3423                      {
3424                         char constant[256];
3425                         sourceExp.type = constantExp;
3426                         if(!strcmp(baseClass.dataTypeString, "int"))
3427                            sprintf(constant, "%d",(int)value.data);
3428                         else
3429                            sprintf(constant, "0x%X",(int)value.data);
3430                         sourceExp.constant = CopyString(constant);
3431                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3432                      }
3433
3434                      while(converts.first)
3435                      {
3436                         Conversion convert = converts.first;
3437                         converts.Remove(convert);
3438                         conversions.Add(convert);
3439                      }
3440                      delete type;
3441                      return true;
3442                   }
3443                }
3444             }
3445          }
3446          if(converts.first)
3447             converts.Free(FreeConvert);
3448          delete type;
3449       }
3450    }
3451    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3452       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3453          return true;
3454    return false;
3455 }
3456
3457 public bool ModuleVisibility(Module searchIn, Module searchFor)
3458 {
3459    SubModule subModule;
3460
3461    if(searchFor == searchIn)
3462       return true;
3463
3464    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3465    {
3466       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3467       {
3468          if(ModuleVisibility(subModule.module, searchFor))
3469             return true;
3470       }
3471    }
3472    return false;
3473 }
3474
3475 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3476 {
3477    Module module;
3478
3479    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3480       return true;
3481    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3482       return true;
3483    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3484       return true;
3485
3486    for(module = mainModule.application.allModules.first; module; module = module.next)
3487    {
3488       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3489          return true;
3490    }
3491    return false;
3492 }
3493
3494 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3495 {
3496    Type source = sourceExp.expType;
3497    Type realDest = dest;
3498    Type backupSourceExpType = null;
3499
3500    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3501       return true;
3502
3503    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3504    {
3505        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3506        {
3507           Class sourceBase, destBase;
3508           for(sourceBase = source._class.registered;
3509               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3510               sourceBase = sourceBase.base);
3511           for(destBase = dest._class.registered;
3512               destBase && destBase.base && destBase.base.type != systemClass;
3513               destBase = destBase.base);
3514           //if(source._class.registered == dest._class.registered)
3515           if(sourceBase == destBase)
3516              return true;
3517        }
3518    }
3519
3520    if(source)
3521    {
3522       OldList * specs;
3523       bool flag = false;
3524       int64 value = MAXINT;
3525
3526       source.refCount++;
3527       dest.refCount++;
3528
3529       if(sourceExp.type == constantExp)
3530       {
3531          if(source.isSigned)
3532             value = strtoll(sourceExp.constant, null, 0);
3533          else
3534             value = strtoull(sourceExp.constant, null, 0);
3535       }
3536       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3537       {
3538          if(source.isSigned)
3539             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3540          else
3541             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3542       }
3543
3544       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3545          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3546       {
3547          FreeType(source);
3548          source = Type { kind = intType, isSigned = false, refCount = 1 };
3549       }
3550
3551       if(dest.kind == classType)
3552       {
3553          Class _class = dest._class ? dest._class.registered : null;
3554
3555          if(_class && _class.type == unitClass)
3556          {
3557             if(source.kind != classType)
3558             {
3559                Type tempType { };
3560                Type tempDest, tempSource;
3561
3562                for(; _class.base.type != systemClass; _class = _class.base);
3563                tempSource = dest;
3564                tempDest = tempType;
3565
3566                tempType.kind = classType;
3567                if(!_class.symbol)
3568                   _class.symbol = FindClass(_class.fullName);
3569
3570                tempType._class = _class.symbol;
3571                tempType.truth = dest.truth;
3572                if(tempType._class)
3573                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3574
3575                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3576                backupSourceExpType = sourceExp.expType;
3577                sourceExp.expType = dest; dest.refCount++;
3578                //sourceExp.expType = MkClassType(_class.fullName);
3579                flag = true;
3580
3581                delete tempType;
3582             }
3583          }
3584
3585
3586          // Why wasn't there something like this?
3587          if(_class && _class.type == bitClass && source.kind != classType)
3588          {
3589             if(!dest._class.registered.dataType)
3590                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3591             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3592             {
3593                FreeType(source);
3594                FreeType(sourceExp.expType);
3595                source = sourceExp.expType = MkClassType(dest._class.string);
3596                source.refCount++;
3597
3598                //source.kind = classType;
3599                //source._class = dest._class;
3600             }
3601          }
3602
3603          // Adding two enumerations
3604          /*
3605          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3606          {
3607             if(!source._class.registered.dataType)
3608                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3609             if(!dest._class.registered.dataType)
3610                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3611
3612             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3613             {
3614                FreeType(source);
3615                source = sourceExp.expType = MkClassType(dest._class.string);
3616                source.refCount++;
3617
3618                //source.kind = classType;
3619                //source._class = dest._class;
3620             }
3621          }*/
3622
3623          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3624          {
3625             OldList * specs = MkList();
3626             Declarator decl;
3627             char string[1024];
3628
3629             ReadString(string, sourceExp.string);
3630             decl = SpecDeclFromString(string, specs, null);
3631
3632             FreeExpContents(sourceExp);
3633             FreeType(sourceExp.expType);
3634
3635             sourceExp.type = classExp;
3636             sourceExp._classExp.specifiers = specs;
3637             sourceExp._classExp.decl = decl;
3638             sourceExp.expType = dest;
3639             dest.refCount++;
3640
3641             FreeType(source);
3642             FreeType(dest);
3643             if(backupSourceExpType) FreeType(backupSourceExpType);
3644             return true;
3645          }
3646       }
3647       else if(source.kind == classType)
3648       {
3649          Class _class = source._class ? source._class.registered : null;
3650
3651          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3652          {
3653             /*
3654             if(dest.kind != classType)
3655             {
3656                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3657                if(!source._class.registered.dataType)
3658                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3659
3660                FreeType(dest);
3661                dest = MkClassType(source._class.string);
3662                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3663                //   dest = MkClassType(source._class.string);
3664             }
3665             */
3666
3667             if(dest.kind != classType)
3668             {
3669                Type tempType { };
3670                Type tempDest, tempSource;
3671
3672                if(!source._class.registered.dataType)
3673                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3674
3675                for(; _class.base.type != systemClass; _class = _class.base);
3676                tempDest = source;
3677                tempSource = tempType;
3678                tempType.kind = classType;
3679                tempType._class = FindClass(_class.fullName);
3680                tempType.truth = source.truth;
3681                tempType.classObjectType = source.classObjectType;
3682
3683                if(tempType._class)
3684                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3685
3686                // PUT THIS BACK TESTING UNITS?
3687                if(conversions.last)
3688                {
3689                   ((Conversion)(conversions.last)).resultType = dest;
3690                   dest.refCount++;
3691                }
3692
3693                FreeType(sourceExp.expType);
3694                sourceExp.expType = MkClassType(_class.fullName);
3695                sourceExp.expType.truth = source.truth;
3696                sourceExp.expType.classObjectType = source.classObjectType;
3697
3698                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3699
3700                if(!sourceExp.destType)
3701                {
3702                   FreeType(sourceExp.destType);
3703                   sourceExp.destType = sourceExp.expType;
3704                   if(sourceExp.expType)
3705                      sourceExp.expType.refCount++;
3706                }
3707                //flag = true;
3708                //source = _class.dataType;
3709
3710
3711                // TOCHECK: TESTING THIS NEW CODE
3712                if(!_class.dataType)
3713                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3714                FreeType(dest);
3715                dest = MkClassType(source._class.string);
3716                dest.truth = source.truth;
3717                dest.classObjectType = source.classObjectType;
3718
3719                FreeType(source);
3720                source = _class.dataType;
3721                source.refCount++;
3722
3723                delete tempType;
3724             }
3725          }
3726       }
3727
3728       if(!flag)
3729       {
3730          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3731          {
3732             FreeType(source);
3733             FreeType(dest);
3734             return true;
3735          }
3736       }
3737
3738       // Implicit Casts
3739       /*
3740       if(source.kind == classType)
3741       {
3742          Class _class = source._class.registered;
3743          if(_class.type == unitClass)
3744          {
3745             if(!_class.dataType)
3746                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3747             source = _class.dataType;
3748          }
3749       }*/
3750
3751       if(dest.kind == classType)
3752       {
3753          Class _class = dest._class ? dest._class.registered : null;
3754          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") ||
3755             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3756          {
3757             if(_class.type == normalClass || _class.type == noHeadClass)
3758             {
3759                Expression newExp { };
3760                *newExp = *sourceExp;
3761                if(sourceExp.destType) sourceExp.destType.refCount++;
3762                if(sourceExp.expType)  sourceExp.expType.refCount++;
3763                sourceExp.type = castExp;
3764                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3765                sourceExp.cast.exp = newExp;
3766                FreeType(sourceExp.expType);
3767                sourceExp.expType = null;
3768                ProcessExpressionType(sourceExp);
3769
3770                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3771                if(!inCompiler)
3772                {
3773                   FreeType(sourceExp.expType);
3774                   sourceExp.expType = dest;
3775                }
3776
3777                FreeType(source);
3778                if(inCompiler) FreeType(dest);
3779
3780                if(backupSourceExpType) FreeType(backupSourceExpType);
3781                return true;
3782             }
3783
3784             if(!_class.dataType)
3785                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3786             FreeType(dest);
3787             dest = _class.dataType;
3788             dest.refCount++;
3789          }
3790
3791          // Accept lower precision types for units, since we want to keep the unit type
3792          if(dest.kind == doubleType &&
3793             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3794              source.kind == charType || source.kind == _BoolType))
3795          {
3796             specs = MkListOne(MkSpecifier(DOUBLE));
3797          }
3798          else if(dest.kind == floatType &&
3799             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3800             source.kind == _BoolType || source.kind == doubleType))
3801          {
3802             specs = MkListOne(MkSpecifier(FLOAT));
3803          }
3804          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3805             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3806          {
3807             specs = MkList();
3808             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3809             ListAdd(specs, MkSpecifier(INT64));
3810          }
3811          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3812             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3813          {
3814             specs = MkList();
3815             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3816             ListAdd(specs, MkSpecifier(INT));
3817          }
3818          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3819             source.kind == floatType || source.kind == doubleType))
3820          {
3821             specs = MkList();
3822             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3823             ListAdd(specs, MkSpecifier(SHORT));
3824          }
3825          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3826             source.kind == floatType || source.kind == doubleType))
3827          {
3828             specs = MkList();
3829             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3830             ListAdd(specs, MkSpecifier(CHAR));
3831          }
3832          else
3833          {
3834             FreeType(source);
3835             FreeType(dest);
3836             if(backupSourceExpType)
3837             {
3838                // Failed to convert: revert previous exp type
3839                if(sourceExp.expType) FreeType(sourceExp.expType);
3840                sourceExp.expType = backupSourceExpType;
3841             }
3842             return false;
3843          }
3844       }
3845       else if(dest.kind == doubleType &&
3846          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3847           source.kind == _BoolType || source.kind == charType))
3848       {
3849          specs = MkListOne(MkSpecifier(DOUBLE));
3850       }
3851       else if(dest.kind == floatType &&
3852          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3853       {
3854          specs = MkListOne(MkSpecifier(FLOAT));
3855       }
3856       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3857          (value == 1 || value == 0))
3858       {
3859          specs = MkList();
3860          ListAdd(specs, MkSpecifier(BOOL));
3861       }
3862       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3863          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3864       {
3865          specs = MkList();
3866          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3867          ListAdd(specs, MkSpecifier(CHAR));
3868       }
3869       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3870          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3871       {
3872          specs = MkList();
3873          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3874          ListAdd(specs, MkSpecifier(SHORT));
3875       }
3876       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3877       {
3878          specs = MkList();
3879          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3880          ListAdd(specs, MkSpecifier(INT));
3881       }
3882       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3883       {
3884          specs = MkList();
3885          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3886          ListAdd(specs, MkSpecifier(INT64));
3887       }
3888       else if(dest.kind == enumType &&
3889          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3890       {
3891          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3892       }
3893       else
3894       {
3895          FreeType(source);
3896          FreeType(dest);
3897          if(backupSourceExpType)
3898          {
3899             // Failed to convert: revert previous exp type
3900             if(sourceExp.expType) FreeType(sourceExp.expType);
3901             sourceExp.expType = backupSourceExpType;
3902          }
3903          return false;
3904       }
3905
3906       if(!flag)
3907       {
3908          Expression newExp { };
3909          *newExp = *sourceExp;
3910          newExp.prev = null;
3911          newExp.next = null;
3912          if(sourceExp.destType) sourceExp.destType.refCount++;
3913          if(sourceExp.expType)  sourceExp.expType.refCount++;
3914
3915          sourceExp.type = castExp;
3916          if(realDest.kind == classType)
3917          {
3918             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3919             FreeList(specs, FreeSpecifier);
3920          }
3921          else
3922             sourceExp.cast.typeName = MkTypeName(specs, null);
3923          if(newExp.type == opExp)
3924          {
3925             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3926          }
3927          else
3928             sourceExp.cast.exp = newExp;
3929
3930          FreeType(sourceExp.expType);
3931          sourceExp.expType = null;
3932          ProcessExpressionType(sourceExp);
3933       }
3934       else
3935          FreeList(specs, FreeSpecifier);
3936
3937       FreeType(dest);
3938       FreeType(source);
3939       if(backupSourceExpType) FreeType(backupSourceExpType);
3940
3941       return true;
3942    }
3943    else
3944    {
3945       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3946       if(sourceExp.type == identifierExp)
3947       {
3948          Identifier id = sourceExp.identifier;
3949          if(dest.kind == classType)
3950          {
3951             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3952             {
3953                Class _class = dest._class.registered;
3954                Class enumClass = eSystem_FindClass(privateModule, "enum");
3955                if(enumClass)
3956                {
3957                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3958                   {
3959                      NamedLink value;
3960                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3961                      for(value = e.values.first; value; value = value.next)
3962                      {
3963                         if(!strcmp(value.name, id.string))
3964                            break;
3965                      }
3966                      if(value)
3967                      {
3968                         FreeExpContents(sourceExp);
3969                         FreeType(sourceExp.expType);
3970
3971                         sourceExp.isConstant = true;
3972                         sourceExp.expType = MkClassType(_class.fullName);
3973                         //if(inCompiler)
3974                         {
3975                            char constant[256];
3976                            sourceExp.type = constantExp;
3977                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3978                               sprintf(constant, "%d", (int) value.data);
3979                            else
3980                               sprintf(constant, "0x%X", (int) value.data);
3981                            sourceExp.constant = CopyString(constant);
3982                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3983                         }
3984                         return true;
3985                      }
3986                   }
3987                }
3988             }
3989          }
3990
3991          // Loop through all enum classes
3992          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3993             return true;
3994       }
3995    }
3996    return false;
3997 }
3998
3999 #define TERTIARY(o, name, m, t, p) \
4000    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4001    {                                                              \
4002       exp.type = constantExp;                                    \
4003       exp.string = p(op1.m ? op2.m : op3.m);                     \
4004       if(!exp.expType) \
4005          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4006       return true;                                                \
4007    }
4008
4009 #define BINARY(o, name, m, t, p) \
4010    static bool name(Expression exp, Operand op1, Operand op2)   \
4011    {                                                              \
4012       t value2 = op2.m;                                           \
4013       exp.type = constantExp;                                    \
4014       exp.string = p(op1.m o value2);                     \
4015       if(!exp.expType) \
4016          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4017       return true;                                                \
4018    }
4019
4020 #define BINARY_DIVIDE(o, name, m, t, p) \
4021    static bool name(Expression exp, Operand op1, Operand op2)   \
4022    {                                                              \
4023       t value2 = op2.m;                                           \
4024       exp.type = constantExp;                                    \
4025       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4026       if(!exp.expType) \
4027          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4028       return true;                                                \
4029    }
4030
4031 #define UNARY(o, name, m, t, p) \
4032    static bool name(Expression exp, Operand op1)                \
4033    {                                                              \
4034       exp.type = constantExp;                                    \
4035       exp.string = p((t)(o op1.m));                                   \
4036       if(!exp.expType) \
4037          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4038       return true;                                                \
4039    }
4040
4041 #define OPERATOR_ALL(macro, o, name) \
4042    macro(o, Int##name, i, int, PrintInt) \
4043    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4044    macro(o, Int64##name, i64, int64, PrintInt64) \
4045    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4046    macro(o, Short##name, s, short, PrintShort) \
4047    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4048    macro(o, Char##name, c, char, PrintChar) \
4049    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4050    macro(o, Float##name, f, float, PrintFloat) \
4051    macro(o, Double##name, d, double, PrintDouble)
4052
4053 #define OPERATOR_INTTYPES(macro, o, name) \
4054    macro(o, Int##name, i, int, PrintInt) \
4055    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4056    macro(o, Int64##name, i64, int64, PrintInt64) \
4057    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4058    macro(o, Short##name, s, short, PrintShort) \
4059    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4060    macro(o, Char##name, c, char, PrintChar) \
4061    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4062
4063
4064 // binary arithmetic
4065 OPERATOR_ALL(BINARY, +, Add)
4066 OPERATOR_ALL(BINARY, -, Sub)
4067 OPERATOR_ALL(BINARY, *, Mul)
4068 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4069 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4070
4071 // unary arithmetic
4072 OPERATOR_ALL(UNARY, -, Neg)
4073
4074 // unary arithmetic increment and decrement
4075 OPERATOR_ALL(UNARY, ++, Inc)
4076 OPERATOR_ALL(UNARY, --, Dec)
4077
4078 // binary arithmetic assignment
4079 OPERATOR_ALL(BINARY, =, Asign)
4080 OPERATOR_ALL(BINARY, +=, AddAsign)
4081 OPERATOR_ALL(BINARY, -=, SubAsign)
4082 OPERATOR_ALL(BINARY, *=, MulAsign)
4083 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4084 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4085
4086 // binary bitwise
4087 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4088 OPERATOR_INTTYPES(BINARY, |, BitOr)
4089 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4090 OPERATOR_INTTYPES(BINARY, <<, LShift)
4091 OPERATOR_INTTYPES(BINARY, >>, RShift)
4092
4093 // unary bitwise
4094 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4095
4096 // binary bitwise assignment
4097 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4098 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4099 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4100 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4101 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4102
4103 // unary logical negation
4104 OPERATOR_INTTYPES(UNARY, !, Not)
4105
4106 // binary logical equality
4107 OPERATOR_ALL(BINARY, ==, Equ)
4108 OPERATOR_ALL(BINARY, !=, Nqu)
4109
4110 // binary logical
4111 OPERATOR_ALL(BINARY, &&, And)
4112 OPERATOR_ALL(BINARY, ||, Or)
4113
4114 // binary logical relational
4115 OPERATOR_ALL(BINARY, >, Grt)
4116 OPERATOR_ALL(BINARY, <, Sma)
4117 OPERATOR_ALL(BINARY, >=, GrtEqu)
4118 OPERATOR_ALL(BINARY, <=, SmaEqu)
4119
4120 // tertiary condition operator
4121 OPERATOR_ALL(TERTIARY, ?, Cond)
4122
4123 //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
4124 #define OPERATOR_TABLE_ALL(name, type) \
4125     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4126                           type##Neg, \
4127                           type##Inc, type##Dec, \
4128                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4129                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4130                           type##BitNot, \
4131                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4132                           type##Not, \
4133                           type##Equ, type##Nqu, \
4134                           type##And, type##Or, \
4135                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4136                         }; \
4137
4138 #define OPERATOR_TABLE_INTTYPES(name, type) \
4139     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4140                           type##Neg, \
4141                           type##Inc, type##Dec, \
4142                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4143                           null, null, null, null, null, \
4144                           null, \
4145                           null, null, null, null, null, \
4146                           null, \
4147                           type##Equ, type##Nqu, \
4148                           type##And, type##Or, \
4149                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4150                         }; \
4151
4152 OPERATOR_TABLE_ALL(int, Int)
4153 OPERATOR_TABLE_ALL(uint, UInt)
4154 OPERATOR_TABLE_ALL(int64, Int64)
4155 OPERATOR_TABLE_ALL(uint64, UInt64)
4156 OPERATOR_TABLE_ALL(short, Short)
4157 OPERATOR_TABLE_ALL(ushort, UShort)
4158 OPERATOR_TABLE_INTTYPES(float, Float)
4159 OPERATOR_TABLE_INTTYPES(double, Double)
4160 OPERATOR_TABLE_ALL(char, Char)
4161 OPERATOR_TABLE_ALL(uchar, UChar)
4162
4163 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4164 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4165 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4166 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4167 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4168 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4169 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4170 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4171
4172 public void ReadString(char * output,  char * string)
4173 {
4174    int len = strlen(string);
4175    int c,d = 0;
4176    bool quoted = false, escaped = false;
4177    for(c = 0; c<len; c++)
4178    {
4179       char ch = string[c];
4180       if(escaped)
4181       {
4182          switch(ch)
4183          {
4184             case 'n': output[d] = '\n'; break;
4185             case 't': output[d] = '\t'; break;
4186             case 'a': output[d] = '\a'; break;
4187             case 'b': output[d] = '\b'; break;
4188             case 'f': output[d] = '\f'; break;
4189             case 'r': output[d] = '\r'; break;
4190             case 'v': output[d] = '\v'; break;
4191             case '\\': output[d] = '\\'; break;
4192             case '\"': output[d] = '\"'; break;
4193             case '\'': output[d] = '\''; break;
4194             default: output[d] = ch;
4195          }
4196          d++;
4197          escaped = false;
4198       }
4199       else
4200       {
4201          if(ch == '\"')
4202             quoted ^= true;
4203          else if(quoted)
4204          {
4205             if(ch == '\\')
4206                escaped = true;
4207             else
4208                output[d++] = ch;
4209          }
4210       }
4211    }
4212    output[d] = '\0';
4213 }
4214
4215 // String Unescape Copy
4216
4217 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4218 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4219 public int UnescapeString(char * d, char * s, int len)
4220 {
4221    int j = 0, k = 0;
4222    char ch;
4223    while(j < len && (ch = s[j]))
4224    {
4225       switch(ch)
4226       {
4227          case '\\':
4228             switch((ch = s[++j]))
4229             {
4230                case 'n': d[k] = '\n'; break;
4231                case 't': d[k] = '\t'; break;
4232                case 'a': d[k] = '\a'; break;
4233                case 'b': d[k] = '\b'; break;
4234                case 'f': d[k] = '\f'; break;
4235                case 'r': d[k] = '\r'; break;
4236                case 'v': d[k] = '\v'; break;
4237                case '\\': d[k] = '\\'; break;
4238                case '\"': d[k] = '\"'; break;
4239                case '\'': d[k] = '\''; break;
4240                default: d[k] = '\\'; d[k] = ch;
4241             }
4242             break;
4243          default:
4244             d[k] = ch;
4245       }
4246       j++, k++;
4247    }
4248    d[k] = '\0';
4249    return k;
4250 }
4251
4252 public char * OffsetEscapedString(char * s, int len, int offset)
4253 {
4254    char ch;
4255    int j = 0, k = 0;
4256    while(j < len && k < offset && (ch = s[j]))
4257    {
4258       if(ch == '\\') ++j;
4259       j++, k++;
4260    }
4261    return (k == offset) ? s + j : null;
4262 }
4263
4264 public Operand GetOperand(Expression exp)
4265 {
4266    Operand op { };
4267    Type type = exp.expType;
4268    if(type)
4269    {
4270       while(type.kind == classType &&
4271          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4272       {
4273          if(!type._class.registered.dataType)
4274             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4275          type = type._class.registered.dataType;
4276
4277       }
4278       if(exp.type == stringExp && op.kind == pointerType)
4279       {
4280          op.ui64 = (uint64)exp.string;
4281          op.kind = pointerType;
4282          op.ops = uint64Ops;
4283       }
4284       else if(exp.isConstant && exp.type == constantExp)
4285       {
4286          op.kind = type.kind;
4287          op.type = exp.expType;
4288
4289          switch(op.kind)
4290          {
4291             case _BoolType:
4292             case charType:
4293             {
4294                if(exp.constant[0] == '\'')
4295                {
4296                   op.c = exp.constant[1];
4297                   op.ops = charOps;
4298                }
4299                else if(type.isSigned)
4300                {
4301                   op.c = (char)strtol(exp.constant, null, 0);
4302                   op.ops = charOps;
4303                }
4304                else
4305                {
4306                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4307                   op.ops = ucharOps;
4308                }
4309                break;
4310             }
4311             case shortType:
4312                if(type.isSigned)
4313                {
4314                   op.s = (short)strtol(exp.constant, null, 0);
4315                   op.ops = shortOps;
4316                }
4317                else
4318                {
4319                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4320                   op.ops = ushortOps;
4321                }
4322                break;
4323             case intType:
4324             case longType:
4325                if(type.isSigned)
4326                {
4327                   op.i = (int)strtol(exp.constant, null, 0);
4328                   op.ops = intOps;
4329                }
4330                else
4331                {
4332                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4333                   op.ops = uintOps;
4334                }
4335                op.kind = intType;
4336                break;
4337             case int64Type:
4338                if(type.isSigned)
4339                {
4340                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4341                   op.ops = intOps;
4342                }
4343                else
4344                {
4345                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4346                   op.ops = uintOps;
4347                }
4348                op.kind = int64Type;
4349                break;
4350             case intPtrType:
4351                if(type.isSigned)
4352                {
4353                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4354                   op.ops = int64Ops;
4355                }
4356                else
4357                {
4358                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4359                   op.ops = uint64Ops;
4360                }
4361                op.kind = int64Type;
4362                break;
4363             case intSizeType:
4364                if(type.isSigned)
4365                {
4366                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4367                   op.ops = int64Ops;
4368                }
4369                else
4370                {
4371                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4372                   op.ops = uint64Ops;
4373                }
4374                op.kind = int64Type;
4375                break;
4376             case floatType:
4377                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4378                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4379                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4380                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4381                else
4382                   op.f = (float)strtod(exp.constant, null);
4383                op.ops = floatOps;
4384                break;
4385             case doubleType:
4386                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4387                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4388                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4389                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4390                else
4391                   op.d = (double)strtod(exp.constant, null);
4392                op.ops = doubleOps;
4393                break;
4394             //case classType:    For when we have operator overloading...
4395             // Pointer additions
4396             //case functionType:
4397             case arrayType:
4398             case pointerType:
4399             case classType:
4400                op.ui64 = _strtoui64(exp.constant, null, 0);
4401                op.kind = pointerType;
4402                op.ops = uint64Ops;
4403                // op.ptrSize =
4404                break;
4405          }
4406       }
4407    }
4408    return op;
4409 }
4410
4411 static void UnusedFunction()
4412 {
4413    int a;
4414    a.OnGetString(0,0,0);
4415 }
4416 default:
4417 extern int __ecereVMethodID_class_OnGetString;
4418 public:
4419
4420 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4421 {
4422    DataMember dataMember;
4423    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4424    {
4425       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4426          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4427       else
4428       {
4429          Expression exp { };
4430          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4431          Type type;
4432          void * ptr = inst.data + dataMember.offset + offset;
4433          char * result = null;
4434          exp.loc = member.loc = inst.loc;
4435          ((Identifier)member.identifiers->first).loc = inst.loc;
4436
4437          if(!dataMember.dataType)
4438             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4439          type = dataMember.dataType;
4440          if(type.kind == classType)
4441          {
4442             Class _class = type._class.registered;
4443             if(_class.type == enumClass)
4444             {
4445                Class enumClass = eSystem_FindClass(privateModule, "enum");
4446                if(enumClass)
4447                {
4448                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4449                   NamedLink item;
4450                   for(item = e.values.first; item; item = item.next)
4451                   {
4452                      if((int)item.data == *(int *)ptr)
4453                      {
4454                         result = item.name;
4455                         break;
4456                      }
4457                   }
4458                   if(result)
4459                   {
4460                      exp.identifier = MkIdentifier(result);
4461                      exp.type = identifierExp;
4462                      exp.destType = MkClassType(_class.fullName);
4463                      ProcessExpressionType(exp);
4464                   }
4465                }
4466             }
4467             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4468             {
4469                if(!_class.dataType)
4470                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4471                type = _class.dataType;
4472             }
4473          }
4474          if(!result)
4475          {
4476             switch(type.kind)
4477             {
4478                case floatType:
4479                {
4480                   FreeExpContents(exp);
4481
4482                   exp.constant = PrintFloat(*(float*)ptr);
4483                   exp.type = constantExp;
4484                   break;
4485                }
4486                case doubleType:
4487                {
4488                   FreeExpContents(exp);
4489
4490                   exp.constant = PrintDouble(*(double*)ptr);
4491                   exp.type = constantExp;
4492                   break;
4493                }
4494                case intType:
4495                {
4496                   FreeExpContents(exp);
4497
4498                   exp.constant = PrintInt(*(int*)ptr);
4499                   exp.type = constantExp;
4500                   break;
4501                }
4502                case int64Type:
4503                {
4504                   FreeExpContents(exp);
4505
4506                   exp.constant = PrintInt64(*(int64*)ptr);
4507                   exp.type = constantExp;
4508                   break;
4509                }
4510                case intPtrType:
4511                {
4512                   FreeExpContents(exp);
4513                   // TODO: This should probably use proper type
4514                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4515                   exp.type = constantExp;
4516                   break;
4517                }
4518                case intSizeType:
4519                {
4520                   FreeExpContents(exp);
4521                   // TODO: This should probably use proper type
4522                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4523                   exp.type = constantExp;
4524                   break;
4525                }
4526                default:
4527                   Compiler_Error($"Unhandled type populating instance\n");
4528             }
4529          }
4530          ListAdd(memberList, member);
4531       }
4532
4533       if(parentDataMember.type == unionMember)
4534          break;
4535    }
4536 }
4537
4538 void PopulateInstance(Instantiation inst)
4539 {
4540    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4541    Class _class = classSym.registered;
4542    DataMember dataMember;
4543    OldList * memberList = MkList();
4544    // Added this check and ->Add to prevent memory leaks on bad code
4545    if(!inst.members)
4546       inst.members = MkListOne(MkMembersInitList(memberList));
4547    else
4548       inst.members->Add(MkMembersInitList(memberList));
4549    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4550    {
4551       if(!dataMember.isProperty)
4552       {
4553          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4554             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4555          else
4556          {
4557             Expression exp { };
4558             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4559             Type type;
4560             void * ptr = inst.data + dataMember.offset;
4561             char * result = null;
4562
4563             exp.loc = member.loc = inst.loc;
4564             ((Identifier)member.identifiers->first).loc = inst.loc;
4565
4566             if(!dataMember.dataType)
4567                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4568             type = dataMember.dataType;
4569             if(type.kind == classType)
4570             {
4571                Class _class = type._class.registered;
4572                if(_class.type == enumClass)
4573                {
4574                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4575                   if(enumClass)
4576                   {
4577                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4578                      NamedLink item;
4579                      for(item = e.values.first; item; item = item.next)
4580                      {
4581                         if((int)item.data == *(int *)ptr)
4582                         {
4583                            result = item.name;
4584                            break;
4585                         }
4586                      }
4587                   }
4588                   if(result)
4589                   {
4590                      exp.identifier = MkIdentifier(result);
4591                      exp.type = identifierExp;
4592                      exp.destType = MkClassType(_class.fullName);
4593                      ProcessExpressionType(exp);
4594                   }
4595                }
4596                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4597                {
4598                   if(!_class.dataType)
4599                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4600                   type = _class.dataType;
4601                }
4602             }
4603             if(!result)
4604             {
4605                switch(type.kind)
4606                {
4607                   case floatType:
4608                   {
4609                      exp.constant = PrintFloat(*(float*)ptr);
4610                      exp.type = constantExp;
4611                      break;
4612                   }
4613                   case doubleType:
4614                   {
4615                      exp.constant = PrintDouble(*(double*)ptr);
4616                      exp.type = constantExp;
4617                      break;
4618                   }
4619                   case intType:
4620                   {
4621                      exp.constant = PrintInt(*(int*)ptr);
4622                      exp.type = constantExp;
4623                      break;
4624                   }
4625                   case int64Type:
4626                   {
4627                      exp.constant = PrintInt64(*(int64*)ptr);
4628                      exp.type = constantExp;
4629                      break;
4630                   }
4631                   case intPtrType:
4632                   {
4633                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4634                      exp.type = constantExp;
4635                      break;
4636                   }
4637                   default:
4638                      Compiler_Error($"Unhandled type populating instance\n");
4639                }
4640             }
4641             ListAdd(memberList, member);
4642          }
4643       }
4644    }
4645 }
4646
4647 void ComputeInstantiation(Expression exp)
4648 {
4649    Instantiation inst = exp.instance;
4650    MembersInit members;
4651    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4652    Class _class = classSym ? classSym.registered : null;
4653    DataMember curMember = null;
4654    Class curClass = null;
4655    DataMember subMemberStack[256];
4656    int subMemberStackPos = 0;
4657    uint64 bits = 0;
4658
4659    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4660    {
4661       // Don't recompute the instantiation...
4662       // Non Simple classes will have become constants by now
4663       if(inst.data)
4664          return;
4665
4666       if(_class.type == normalClass || _class.type == noHeadClass)
4667       {
4668          inst.data = (byte *)eInstance_New(_class);
4669          if(_class.type == normalClass)
4670             ((Instance)inst.data)._refCount++;
4671       }
4672       else
4673          inst.data = new0 byte[_class.structSize];
4674    }
4675
4676    if(inst.members)
4677    {
4678       for(members = inst.members->first; members; members = members.next)
4679       {
4680          switch(members.type)
4681          {
4682             case dataMembersInit:
4683             {
4684                if(members.dataMembers)
4685                {
4686                   MemberInit member;
4687                   for(member = members.dataMembers->first; member; member = member.next)
4688                   {
4689                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4690                      bool found = false;
4691
4692                      Property prop = null;
4693                      DataMember dataMember = null;
4694                      Method method = null;
4695                      uint dataMemberOffset;
4696
4697                      if(!ident)
4698                      {
4699                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4700                         if(curMember)
4701                         {
4702                            if(curMember.isProperty)
4703                               prop = (Property)curMember;
4704                            else
4705                            {
4706                               dataMember = curMember;
4707
4708                               // CHANGED THIS HERE
4709                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4710
4711                               // 2013/17/29 -- It seems that this was missing here!
4712                               if(_class.type == normalClass)
4713                                  dataMemberOffset += _class.base.structSize;
4714                               // dataMemberOffset = dataMember.offset;
4715                            }
4716                            found = true;
4717                         }
4718                      }
4719                      else
4720                      {
4721                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4722                         if(prop)
4723                         {
4724                            found = true;
4725                            if(prop.memberAccess == publicAccess)
4726                            {
4727                               curMember = (DataMember)prop;
4728                               curClass = prop._class;
4729                            }
4730                         }
4731                         else
4732                         {
4733                            DataMember _subMemberStack[256];
4734                            int _subMemberStackPos = 0;
4735
4736                            // FILL MEMBER STACK
4737                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4738
4739                            if(dataMember)
4740                            {
4741                               found = true;
4742                               if(dataMember.memberAccess == publicAccess)
4743                               {
4744                                  curMember = dataMember;
4745                                  curClass = dataMember._class;
4746                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4747                                  subMemberStackPos = _subMemberStackPos;
4748                               }
4749                            }
4750                         }
4751                      }
4752
4753                      if(found && member.initializer && member.initializer.type == expInitializer)
4754                      {
4755                         Expression value = member.initializer.exp;
4756                         Type type = null;
4757                         bool deepMember = false;
4758                         if(prop)
4759                         {
4760                            type = prop.dataType;
4761                         }
4762                         else if(dataMember)
4763                         {
4764                            if(!dataMember.dataType)
4765                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4766
4767                            type = dataMember.dataType;
4768                         }
4769
4770                         if(ident && ident.next)
4771                         {
4772                            deepMember = true;
4773
4774                            // for(; ident && type; ident = ident.next)
4775                            for(ident = ident.next; ident && type; ident = ident.next)
4776                            {
4777                               if(type.kind == classType)
4778                               {
4779                                  prop = eClass_FindProperty(type._class.registered,
4780                                     ident.string, privateModule);
4781                                  if(prop)
4782                                     type = prop.dataType;
4783                                  else
4784                                  {
4785                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4786                                        ident.string, &dataMemberOffset, privateModule, null, null);
4787                                     if(dataMember)
4788                                        type = dataMember.dataType;
4789                                  }
4790                               }
4791                               else if(type.kind == structType || type.kind == unionType)
4792                               {
4793                                  Type memberType;
4794                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4795                                  {
4796                                     if(!strcmp(memberType.name, ident.string))
4797                                     {
4798                                        type = memberType;
4799                                        break;
4800                                     }
4801                                  }
4802                               }
4803                            }
4804                         }
4805                         if(value)
4806                         {
4807                            FreeType(value.destType);
4808                            value.destType = type;
4809                            if(type) type.refCount++;
4810                            ComputeExpression(value);
4811                         }
4812                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4813                         {
4814                            if(type.kind == classType)
4815                            {
4816                               Class _class = type._class.registered;
4817                               if(_class.type == bitClass || _class.type == unitClass ||
4818                                  _class.type == enumClass)
4819                               {
4820                                  if(!_class.dataType)
4821                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4822                                  type = _class.dataType;
4823                               }
4824                            }
4825
4826                            if(dataMember)
4827                            {
4828                               void * ptr = inst.data + dataMemberOffset;
4829
4830                               if(value.type == constantExp)
4831                               {
4832                                  switch(type.kind)
4833                                  {
4834                                     case intType:
4835                                     {
4836                                        GetInt(value, (int*)ptr);
4837                                        break;
4838                                     }
4839                                     case int64Type:
4840                                     {
4841                                        GetInt64(value, (int64*)ptr);
4842                                        break;
4843                                     }
4844                                     case intPtrType:
4845                                     {
4846                                        GetIntPtr(value, (intptr*)ptr);
4847                                        break;
4848                                     }
4849                                     case intSizeType:
4850                                     {
4851                                        GetIntSize(value, (intsize*)ptr);
4852                                        break;
4853                                     }
4854                                     case floatType:
4855                                     {
4856                                        GetFloat(value, (float*)ptr);
4857                                        break;
4858                                     }
4859                                     case doubleType:
4860                                     {
4861                                        GetDouble(value, (double *)ptr);
4862                                        break;
4863                                     }
4864                                  }
4865                               }
4866                               else if(value.type == instanceExp)
4867                               {
4868                                  if(type.kind == classType)
4869                                  {
4870                                     Class _class = type._class.registered;
4871                                     if(_class.type == structClass)
4872                                     {
4873                                        ComputeTypeSize(type);
4874                                        if(value.instance.data)
4875                                           memcpy(ptr, value.instance.data, type.size);
4876                                     }
4877                                  }
4878                               }
4879                            }
4880                            else if(prop)
4881                            {
4882                               if(value.type == instanceExp && value.instance.data)
4883                               {
4884                                  if(type.kind == classType)
4885                                  {
4886                                     Class _class = type._class.registered;
4887                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4888                                     {
4889                                        void (*Set)(void *, void *) = (void *)prop.Set;
4890                                        Set(inst.data, value.instance.data);
4891                                        PopulateInstance(inst);
4892                                     }
4893                                  }
4894                               }
4895                               else if(value.type == constantExp)
4896                               {
4897                                  switch(type.kind)
4898                                  {
4899                                     case doubleType:
4900                                     {
4901                                        void (*Set)(void *, double) = (void *)prop.Set;
4902                                        Set(inst.data, strtod(value.constant, null) );
4903                                        break;
4904                                     }
4905                                     case floatType:
4906                                     {
4907                                        void (*Set)(void *, float) = (void *)prop.Set;
4908                                        Set(inst.data, (float)(strtod(value.constant, null)));
4909                                        break;
4910                                     }
4911                                     case intType:
4912                                     {
4913                                        void (*Set)(void *, int) = (void *)prop.Set;
4914                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4915                                        break;
4916                                     }
4917                                     case int64Type:
4918                                     {
4919                                        void (*Set)(void *, int64) = (void *)prop.Set;
4920                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4921                                        break;
4922                                     }
4923                                     case intPtrType:
4924                                     {
4925                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4926                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4927                                        break;
4928                                     }
4929                                     case intSizeType:
4930                                     {
4931                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4932                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4933                                        break;
4934                                     }
4935                                  }
4936                               }
4937                               else if(value.type == stringExp)
4938                               {
4939                                  char temp[1024];
4940                                  ReadString(temp, value.string);
4941                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4942                               }
4943                            }
4944                         }
4945                         else if(!deepMember && type && _class.type == unitClass)
4946                         {
4947                            if(prop)
4948                            {
4949                               // Only support converting units to units for now...
4950                               if(value.type == constantExp)
4951                               {
4952                                  if(type.kind == classType)
4953                                  {
4954                                     Class _class = type._class.registered;
4955                                     if(_class.type == unitClass)
4956                                     {
4957                                        if(!_class.dataType)
4958                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4959                                        type = _class.dataType;
4960                                     }
4961                                  }
4962                                  // TODO: Assuming same base type for units...
4963                                  switch(type.kind)
4964                                  {
4965                                     case floatType:
4966                                     {
4967                                        float fValue;
4968                                        float (*Set)(float) = (void *)prop.Set;
4969                                        GetFloat(member.initializer.exp, &fValue);
4970                                        exp.constant = PrintFloat(Set(fValue));
4971                                        exp.type = constantExp;
4972                                        break;
4973                                     }
4974                                     case doubleType:
4975                                     {
4976                                        double dValue;
4977                                        double (*Set)(double) = (void *)prop.Set;
4978                                        GetDouble(member.initializer.exp, &dValue);
4979                                        exp.constant = PrintDouble(Set(dValue));
4980                                        exp.type = constantExp;
4981                                        break;
4982                                     }
4983                                  }
4984                               }
4985                            }
4986                         }
4987                         else if(!deepMember && type && _class.type == bitClass)
4988                         {
4989                            if(prop)
4990                            {
4991                               if(value.type == instanceExp && value.instance.data)
4992                               {
4993                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4994                                  bits = Set(value.instance.data);
4995                               }
4996                               else if(value.type == constantExp)
4997                               {
4998                               }
4999                            }
5000                            else if(dataMember)
5001                            {
5002                               BitMember bitMember = (BitMember) dataMember;
5003                               Type type;
5004                               int part = 0;
5005                               GetInt(value, &part);
5006                               bits = (bits & ~bitMember.mask);
5007                               if(!bitMember.dataType)
5008                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5009
5010                               type = bitMember.dataType;
5011
5012                               if(type.kind == classType && type._class && type._class.registered)
5013                               {
5014                                  if(!type._class.registered.dataType)
5015                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5016                                  type = type._class.registered.dataType;
5017                               }
5018
5019                               switch(type.kind)
5020                               {
5021                                  case _BoolType:
5022                                  case charType:
5023                                     if(type.isSigned)
5024                                        bits |= ((char)part << bitMember.pos);
5025                                     else
5026                                        bits |= ((unsigned char)part << bitMember.pos);
5027                                     break;
5028                                  case shortType:
5029                                     if(type.isSigned)
5030                                        bits |= ((short)part << bitMember.pos);
5031                                     else
5032                                        bits |= ((unsigned short)part << bitMember.pos);
5033                                     break;
5034                                  case intType:
5035                                  case longType:
5036                                     if(type.isSigned)
5037                                        bits |= ((int)part << bitMember.pos);
5038                                     else
5039                                        bits |= ((unsigned int)part << bitMember.pos);
5040                                     break;
5041                                  case int64Type:
5042                                     if(type.isSigned)
5043                                        bits |= ((int64)part << bitMember.pos);
5044                                     else
5045                                        bits |= ((uint64)part << bitMember.pos);
5046                                     break;
5047                                  case intPtrType:
5048                                     if(type.isSigned)
5049                                     {
5050                                        bits |= ((intptr)part << bitMember.pos);
5051                                     }
5052                                     else
5053                                     {
5054                                        bits |= ((uintptr)part << bitMember.pos);
5055                                     }
5056                                     break;
5057                                  case intSizeType:
5058                                     if(type.isSigned)
5059                                     {
5060                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
5061                                     }
5062                                     else
5063                                     {
5064                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
5065                                     }
5066                                     break;
5067                               }
5068                            }
5069                         }
5070                      }
5071                      else
5072                      {
5073                         if(_class && _class.type == unitClass)
5074                         {
5075                            ComputeExpression(member.initializer.exp);
5076                            exp.constant = member.initializer.exp.constant;
5077                            exp.type = constantExp;
5078
5079                            member.initializer.exp.constant = null;
5080                         }
5081                      }
5082                   }
5083                }
5084                break;
5085             }
5086          }
5087       }
5088    }
5089    if(_class && _class.type == bitClass)
5090    {
5091       exp.constant = PrintHexUInt(bits);
5092       exp.type = constantExp;
5093    }
5094    if(exp.type != instanceExp)
5095    {
5096       FreeInstance(inst);
5097    }
5098 }
5099
5100 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5101 {
5102    bool result = false;
5103    switch(kind)
5104    {
5105       case shortType:
5106          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5107             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5108          break;
5109       case intType:
5110       case longType:
5111          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5112             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5113          break;
5114       case int64Type:
5115          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5116             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5117          result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5118          break;
5119       case floatType:
5120          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5121             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5122          result = GetOpFloat(op, &op.f);
5123          break;
5124       case doubleType:
5125          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5126             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5127          result = GetOpDouble(op, &op.d);
5128          break;
5129       case pointerType:
5130          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5131             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5132             result = GetOpUIntPtr(op, &op.ui64);
5133          break;
5134       case enumType:
5135          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5136             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5137             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5138          break;
5139       case intPtrType:
5140          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5141             result = isSigned ? GetOpIntPtr(op, &op.i64) : GetOpUIntPtr(op, &op.i64);
5142          break;
5143       case intSizeType:
5144          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5145             result = isSigned ? GetOpIntSize(op, &op.ui64) : GetOpUIntSize(op, &op.ui64);
5146          break;
5147    }
5148    return result;
5149 }
5150
5151 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5152 {
5153    if(exp.op.op == SIZEOF)
5154    {
5155       FreeExpContents(exp);
5156       exp.type = constantExp;
5157       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5158    }
5159    else
5160    {
5161       if(!exp.op.exp1)
5162       {
5163          switch(exp.op.op)
5164          {
5165             // unary arithmetic
5166             case '+':
5167             {
5168                // Provide default unary +
5169                Expression exp2 = exp.op.exp2;
5170                exp.op.exp2 = null;
5171                FreeExpContents(exp);
5172                FreeType(exp.expType);
5173                FreeType(exp.destType);
5174                *exp = *exp2;
5175                delete exp2;
5176                break;
5177             }
5178             case '-':
5179                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5180                break;
5181             // unary arithmetic increment and decrement
5182                   //OPERATOR_ALL(UNARY, ++, Inc)
5183                   //OPERATOR_ALL(UNARY, --, Dec)
5184             // unary bitwise
5185             case '~':
5186                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5187                break;
5188             // unary logical negation
5189             case '!':
5190                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5191                break;
5192          }
5193       }
5194       else
5195       {
5196          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5197          {
5198             if(Promote(op2, op1.kind, op1.type.isSigned))
5199                op2.kind = op1.kind, op2.ops = op1.ops;
5200             else if(Promote(op1, op2.kind, op2.type.isSigned))
5201                op1.kind = op2.kind, op1.ops = op2.ops;
5202          }
5203          switch(exp.op.op)
5204          {
5205             // binary arithmetic
5206             case '+':
5207                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5208                break;
5209             case '-':
5210                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5211                break;
5212             case '*':
5213                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5214                break;
5215             case '/':
5216                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5217                break;
5218             case '%':
5219                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5220                break;
5221             // binary arithmetic assignment
5222                   //OPERATOR_ALL(BINARY, =, Asign)
5223                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5224                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5225                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5226                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5227                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5228             // binary bitwise
5229             case '&':
5230                if(exp.op.exp2)
5231                {
5232                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5233                }
5234                break;
5235             case '|':
5236                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5237                break;
5238             case '^':
5239                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5240                break;
5241             case LEFT_OP:
5242                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5243                break;
5244             case RIGHT_OP:
5245                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5246                break;
5247             // binary bitwise assignment
5248                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5249                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5250                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5251                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5252                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5253             // binary logical equality
5254             case EQ_OP:
5255                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5256                break;
5257             case NE_OP:
5258                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5259                break;
5260             // binary logical
5261             case AND_OP:
5262                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5263                break;
5264             case OR_OP:
5265                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5266                break;
5267             // binary logical relational
5268             case '>':
5269                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5270                break;
5271             case '<':
5272                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5273                break;
5274             case GE_OP:
5275                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5276                break;
5277             case LE_OP:
5278                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5279                break;
5280          }
5281       }
5282    }
5283 }
5284
5285 void ComputeExpression(Expression exp)
5286 {
5287    char expString[10240];
5288    expString[0] = '\0';
5289 #ifdef _DEBUG
5290    PrintExpression(exp, expString);
5291 #endif
5292
5293    switch(exp.type)
5294    {
5295       case instanceExp:
5296       {
5297          ComputeInstantiation(exp);
5298          break;
5299       }
5300       /*
5301       case constantExp:
5302          break;
5303       */
5304       case opExp:
5305       {
5306          Expression exp1, exp2 = null;
5307          Operand op1 { };
5308          Operand op2 { };
5309
5310          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5311          if(exp.op.exp2)
5312          {
5313             Expression e = exp.op.exp2;
5314
5315             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5316             {
5317                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5318                {
5319                   if(e.type == extensionCompoundExp)
5320                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5321                   else
5322                      e = e.list->last;
5323                }
5324             }
5325             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5326             {
5327                if(e.type == stringExp && e.string)
5328                {
5329                   char * string = e.string;
5330                   int len = strlen(string);
5331                   char * tmp = new char[len-2+1];
5332                   len = UnescapeString(tmp, string + 1, len - 2);
5333                   delete tmp;
5334                   FreeExpContents(exp);
5335                   exp.type = constantExp;
5336                   exp.constant = PrintUInt(len + 1);
5337                }
5338                else
5339                {
5340                   Type type = e.expType;
5341                   type.refCount++;
5342                   FreeExpContents(exp);
5343                   exp.type = constantExp;
5344                   exp.constant = PrintUInt(ComputeTypeSize(type));
5345                   FreeType(type);
5346                }
5347                break;
5348             }
5349             else
5350                ComputeExpression(exp.op.exp2);
5351          }
5352          if(exp.op.exp1)
5353          {
5354             ComputeExpression(exp.op.exp1);
5355             exp1 = exp.op.exp1;
5356             exp2 = exp.op.exp2;
5357             op1 = GetOperand(exp1);
5358             if(op1.type) op1.type.refCount++;
5359             if(exp2)
5360             {
5361                op2 = GetOperand(exp2);
5362                if(op2.type) op2.type.refCount++;
5363             }
5364          }
5365          else
5366          {
5367             exp1 = exp.op.exp2;
5368             op1 = GetOperand(exp1);
5369             if(op1.type) op1.type.refCount++;
5370          }
5371
5372          CallOperator(exp, exp1, exp2, op1, op2);
5373          /*
5374          switch(exp.op.op)
5375          {
5376             // Unary operators
5377             case '&':
5378                // Also binary
5379                if(exp.op.exp1 && exp.op.exp2)
5380                {
5381                   // Binary And
5382                   if(op1.ops.BitAnd)
5383                   {
5384                      FreeExpContents(exp);
5385                      op1.ops.BitAnd(exp, op1, op2);
5386                   }
5387                }
5388                break;
5389             case '*':
5390                if(exp.op.exp1)
5391                {
5392                   if(op1.ops.Mul)
5393                   {
5394                      FreeExpContents(exp);
5395                      op1.ops.Mul(exp, op1, op2);
5396                   }
5397                }
5398                break;
5399             case '+':
5400                if(exp.op.exp1)
5401                {
5402                   if(op1.ops.Add)
5403                   {
5404                      FreeExpContents(exp);
5405                      op1.ops.Add(exp, op1, op2);
5406                   }
5407                }
5408                else
5409                {
5410                   // Provide default unary +
5411                   Expression exp2 = exp.op.exp2;
5412                   exp.op.exp2 = null;
5413                   FreeExpContents(exp);
5414                   FreeType(exp.expType);
5415                   FreeType(exp.destType);
5416
5417                   *exp = *exp2;
5418                   delete exp2;
5419                }
5420                break;
5421             case '-':
5422                if(exp.op.exp1)
5423                {
5424                   if(op1.ops.Sub)
5425                   {
5426                      FreeExpContents(exp);
5427                      op1.ops.Sub(exp, op1, op2);
5428                   }
5429                }
5430                else
5431                {
5432                   if(op1.ops.Neg)
5433                   {
5434                      FreeExpContents(exp);
5435                      op1.ops.Neg(exp, op1);
5436                   }
5437                }
5438                break;
5439             case '~':
5440                if(op1.ops.BitNot)
5441                {
5442                   FreeExpContents(exp);
5443                   op1.ops.BitNot(exp, op1);
5444                }
5445                break;
5446             case '!':
5447                if(op1.ops.Not)
5448                {
5449                   FreeExpContents(exp);
5450                   op1.ops.Not(exp, op1);
5451                }
5452                break;
5453             // Binary only operators
5454             case '/':
5455                if(op1.ops.Div)
5456                {
5457                   FreeExpContents(exp);
5458                   op1.ops.Div(exp, op1, op2);
5459                }
5460                break;
5461             case '%':
5462                if(op1.ops.Mod)
5463                {
5464                   FreeExpContents(exp);
5465                   op1.ops.Mod(exp, op1, op2);
5466                }
5467                break;
5468             case LEFT_OP:
5469                break;
5470             case RIGHT_OP:
5471                break;
5472             case '<':
5473                if(exp.op.exp1)
5474                {
5475                   if(op1.ops.Sma)
5476                   {
5477                      FreeExpContents(exp);
5478                      op1.ops.Sma(exp, op1, op2);
5479                   }
5480                }
5481                break;
5482             case '>':
5483                if(exp.op.exp1)
5484                {
5485                   if(op1.ops.Grt)
5486                   {
5487                      FreeExpContents(exp);
5488                      op1.ops.Grt(exp, op1, op2);
5489                   }
5490                }
5491                break;
5492             case LE_OP:
5493                if(exp.op.exp1)
5494                {
5495                   if(op1.ops.SmaEqu)
5496                   {
5497                      FreeExpContents(exp);
5498                      op1.ops.SmaEqu(exp, op1, op2);
5499                   }
5500                }
5501                break;
5502             case GE_OP:
5503                if(exp.op.exp1)
5504                {
5505                   if(op1.ops.GrtEqu)
5506                   {
5507                      FreeExpContents(exp);
5508                      op1.ops.GrtEqu(exp, op1, op2);
5509                   }
5510                }
5511                break;
5512             case EQ_OP:
5513                if(exp.op.exp1)
5514                {
5515                   if(op1.ops.Equ)
5516                   {
5517                      FreeExpContents(exp);
5518                      op1.ops.Equ(exp, op1, op2);
5519                   }
5520                }
5521                break;
5522             case NE_OP:
5523                if(exp.op.exp1)
5524                {
5525                   if(op1.ops.Nqu)
5526                   {
5527                      FreeExpContents(exp);
5528                      op1.ops.Nqu(exp, op1, op2);
5529                   }
5530                }
5531                break;
5532             case '|':
5533                if(op1.ops.BitOr)
5534                {
5535                   FreeExpContents(exp);
5536                   op1.ops.BitOr(exp, op1, op2);
5537                }
5538                break;
5539             case '^':
5540                if(op1.ops.BitXor)
5541                {
5542                   FreeExpContents(exp);
5543                   op1.ops.BitXor(exp, op1, op2);
5544                }
5545                break;
5546             case AND_OP:
5547                break;
5548             case OR_OP:
5549                break;
5550             case SIZEOF:
5551                FreeExpContents(exp);
5552                exp.type = constantExp;
5553                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5554                break;
5555          }
5556          */
5557          if(op1.type) FreeType(op1.type);
5558          if(op2.type) FreeType(op2.type);
5559          break;
5560       }
5561       case bracketsExp:
5562       case extensionExpressionExp:
5563       {
5564          Expression e, n;
5565          for(e = exp.list->first; e; e = n)
5566          {
5567             n = e.next;
5568             if(!n)
5569             {
5570                OldList * list = exp.list;
5571                ComputeExpression(e);
5572                //FreeExpContents(exp);
5573                FreeType(exp.expType);
5574                FreeType(exp.destType);
5575                *exp = *e;
5576                delete e;
5577                delete list;
5578             }
5579             else
5580             {
5581                FreeExpression(e);
5582             }
5583          }
5584          break;
5585       }
5586       /*
5587
5588       case ExpIndex:
5589       {
5590          Expression e;
5591          exp.isConstant = true;
5592
5593          ComputeExpression(exp.index.exp);
5594          if(!exp.index.exp.isConstant)
5595             exp.isConstant = false;
5596
5597          for(e = exp.index.index->first; e; e = e.next)
5598          {
5599             ComputeExpression(e);
5600             if(!e.next)
5601             {
5602                // Check if this type is int
5603             }
5604             if(!e.isConstant)
5605                exp.isConstant = false;
5606          }
5607          exp.expType = Dereference(exp.index.exp.expType);
5608          break;
5609       }
5610       */
5611       case memberExp:
5612       {
5613          Expression memberExp = exp.member.exp;
5614          Identifier memberID = exp.member.member;
5615
5616          Type type;
5617          ComputeExpression(exp.member.exp);
5618          type = exp.member.exp.expType;
5619          if(type)
5620          {
5621             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);
5622             Property prop = null;
5623             DataMember member = null;
5624             Class convertTo = null;
5625             if(type.kind == subClassType && exp.member.exp.type == classExp)
5626                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5627
5628             if(!_class)
5629             {
5630                char string[256];
5631                Symbol classSym;
5632                string[0] = '\0';
5633                PrintTypeNoConst(type, string, false, true);
5634                classSym = FindClass(string);
5635                _class = classSym ? classSym.registered : null;
5636             }
5637
5638             if(exp.member.member)
5639             {
5640                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5641                if(!prop)
5642                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5643             }
5644             if(!prop && !member && _class && exp.member.member)
5645             {
5646                Symbol classSym = FindClass(exp.member.member.string);
5647                convertTo = _class;
5648                _class = classSym ? classSym.registered : null;
5649                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5650             }
5651
5652             if(prop)
5653             {
5654                if(prop.compiled)
5655                {
5656                   Type type = prop.dataType;
5657                   // TODO: Assuming same base type for units...
5658                   if(_class.type == unitClass)
5659                   {
5660                      if(type.kind == classType)
5661                      {
5662                         Class _class = type._class.registered;
5663                         if(_class.type == unitClass)
5664                         {
5665                            if(!_class.dataType)
5666                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5667                            type = _class.dataType;
5668                         }
5669                      }
5670                      switch(type.kind)
5671                      {
5672                         case floatType:
5673                         {
5674                            float value;
5675                            float (*Get)(float) = (void *)prop.Get;
5676                            GetFloat(exp.member.exp, &value);
5677                            exp.constant = PrintFloat(Get ? Get(value) : value);
5678                            exp.type = constantExp;
5679                            break;
5680                         }
5681                         case doubleType:
5682                         {
5683                            double value;
5684                            double (*Get)(double);
5685                            GetDouble(exp.member.exp, &value);
5686
5687                            if(convertTo)
5688                               Get = (void *)prop.Set;
5689                            else
5690                               Get = (void *)prop.Get;
5691                            exp.constant = PrintDouble(Get ? Get(value) : value);
5692                            exp.type = constantExp;
5693                            break;
5694                         }
5695                      }
5696                   }
5697                   else
5698                   {
5699                      if(convertTo)
5700                      {
5701                         Expression value = exp.member.exp;
5702                         Type type;
5703                         if(!prop.dataType)
5704                            ProcessPropertyType(prop);
5705
5706                         type = prop.dataType;
5707                         if(!type)
5708                         {
5709                             // printf("Investigate this\n");
5710                         }
5711                         else if(_class.type == structClass)
5712                         {
5713                            switch(type.kind)
5714                            {
5715                               case classType:
5716                               {
5717                                  Class propertyClass = type._class.registered;
5718                                  if(propertyClass.type == structClass && value.type == instanceExp)
5719                                  {
5720                                     void (*Set)(void *, void *) = (void *)prop.Set;
5721                                     exp.instance = Instantiation { };
5722                                     exp.instance.data = new0 byte[_class.structSize];
5723                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5724                                     exp.instance.loc = exp.loc;
5725                                     exp.type = instanceExp;
5726                                     Set(exp.instance.data, value.instance.data);
5727                                     PopulateInstance(exp.instance);
5728                                  }
5729                                  break;
5730                               }
5731                               case intType:
5732                               {
5733                                  int intValue;
5734                                  void (*Set)(void *, int) = (void *)prop.Set;
5735
5736                                  exp.instance = Instantiation { };
5737                                  exp.instance.data = new0 byte[_class.structSize];
5738                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5739                                  exp.instance.loc = exp.loc;
5740                                  exp.type = instanceExp;
5741
5742                                  GetInt(value, &intValue);
5743
5744                                  Set(exp.instance.data, intValue);
5745                                  PopulateInstance(exp.instance);
5746                                  break;
5747                               }
5748                               case int64Type:
5749                               {
5750                                  int64 intValue;
5751                                  void (*Set)(void *, int64) = (void *)prop.Set;
5752
5753                                  exp.instance = Instantiation { };
5754                                  exp.instance.data = new0 byte[_class.structSize];
5755                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5756                                  exp.instance.loc = exp.loc;
5757                                  exp.type = instanceExp;
5758
5759                                  GetInt64(value, &intValue);
5760
5761                                  Set(exp.instance.data, intValue);
5762                                  PopulateInstance(exp.instance);
5763                                  break;
5764                               }
5765                               case intPtrType:
5766                               {
5767                                  // TOFIX:
5768                                  intptr intValue;
5769                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5770
5771                                  exp.instance = Instantiation { };
5772                                  exp.instance.data = new0 byte[_class.structSize];
5773                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5774                                  exp.instance.loc = exp.loc;
5775                                  exp.type = instanceExp;
5776
5777                                  GetIntPtr(value, &intValue);
5778
5779                                  Set(exp.instance.data, intValue);
5780                                  PopulateInstance(exp.instance);
5781                                  break;
5782                               }
5783                               case intSizeType:
5784                               {
5785                                  // TOFIX:
5786                                  intsize intValue;
5787                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5788
5789                                  exp.instance = Instantiation { };
5790                                  exp.instance.data = new0 byte[_class.structSize];
5791                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5792                                  exp.instance.loc = exp.loc;
5793                                  exp.type = instanceExp;
5794
5795                                  GetIntSize(value, &intValue);
5796
5797                                  Set(exp.instance.data, intValue);
5798                                  PopulateInstance(exp.instance);
5799                                  break;
5800                               }
5801                               case doubleType:
5802                               {
5803                                  double doubleValue;
5804                                  void (*Set)(void *, double) = (void *)prop.Set;
5805
5806                                  exp.instance = Instantiation { };
5807                                  exp.instance.data = new0 byte[_class.structSize];
5808                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5809                                  exp.instance.loc = exp.loc;
5810                                  exp.type = instanceExp;
5811
5812                                  GetDouble(value, &doubleValue);
5813
5814                                  Set(exp.instance.data, doubleValue);
5815                                  PopulateInstance(exp.instance);
5816                                  break;
5817                               }
5818                            }
5819                         }
5820                         else if(_class.type == bitClass)
5821                         {
5822                            switch(type.kind)
5823                            {
5824                               case classType:
5825                               {
5826                                  Class propertyClass = type._class.registered;
5827                                  if(propertyClass.type == structClass && value.instance.data)
5828                                  {
5829                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5830                                     unsigned int bits = Set(value.instance.data);
5831                                     exp.constant = PrintHexUInt(bits);
5832                                     exp.type = constantExp;
5833                                     break;
5834                                  }
5835                                  else if(_class.type == bitClass)
5836                                  {
5837                                     unsigned int value;
5838                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5839                                     unsigned int bits;
5840
5841                                     GetUInt(exp.member.exp, &value);
5842                                     bits = Set(value);
5843                                     exp.constant = PrintHexUInt(bits);
5844                                     exp.type = constantExp;
5845                                  }
5846                               }
5847                            }
5848                         }
5849                      }
5850                      else
5851                      {
5852                         if(_class.type == bitClass)
5853                         {
5854                            unsigned int value;
5855                            GetUInt(exp.member.exp, &value);
5856
5857                            switch(type.kind)
5858                            {
5859                               case classType:
5860                               {
5861                                  Class _class = type._class.registered;
5862                                  if(_class.type == structClass)
5863                                  {
5864                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5865
5866                                     exp.instance = Instantiation { };
5867                                     exp.instance.data = new0 byte[_class.structSize];
5868                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5869                                     exp.instance.loc = exp.loc;
5870                                     //exp.instance.fullSet = true;
5871                                     exp.type = instanceExp;
5872                                     Get(value, exp.instance.data);
5873                                     PopulateInstance(exp.instance);
5874                                  }
5875                                  else if(_class.type == bitClass)
5876                                  {
5877                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5878                                     uint64 bits = Get(value);
5879                                     exp.constant = PrintHexUInt64(bits);
5880                                     exp.type = constantExp;
5881                                  }
5882                                  break;
5883                               }
5884                            }
5885                         }
5886                         else if(_class.type == structClass)
5887                         {
5888                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5889                            switch(type.kind)
5890                            {
5891                               case classType:
5892                               {
5893                                  Class _class = type._class.registered;
5894                                  if(_class.type == structClass && value)
5895                                  {
5896                                     void (*Get)(void *, void *) = (void *)prop.Get;
5897
5898                                     exp.instance = Instantiation { };
5899                                     exp.instance.data = new0 byte[_class.structSize];
5900                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5901                                     exp.instance.loc = exp.loc;
5902                                     //exp.instance.fullSet = true;
5903                                     exp.type = instanceExp;
5904                                     Get(value, exp.instance.data);
5905                                     PopulateInstance(exp.instance);
5906                                  }
5907                                  break;
5908                               }
5909                            }
5910                         }
5911                         /*else
5912                         {
5913                            char * value = exp.member.exp.instance.data;
5914                            switch(type.kind)
5915                            {
5916                               case classType:
5917                               {
5918                                  Class _class = type._class.registered;
5919                                  if(_class.type == normalClass)
5920                                  {
5921                                     void *(*Get)(void *) = (void *)prop.Get;
5922
5923                                     exp.instance = Instantiation { };
5924                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5925                                     exp.type = instanceExp;
5926                                     exp.instance.data = Get(value, exp.instance.data);
5927                                  }
5928                                  break;
5929                               }
5930                            }
5931                         }
5932                         */
5933                      }
5934                   }
5935                }
5936                else
5937                {
5938                   exp.isConstant = false;
5939                }
5940             }
5941             else if(member)
5942             {
5943             }
5944          }
5945
5946          if(exp.type != ExpressionType::memberExp)
5947          {
5948             FreeExpression(memberExp);
5949             FreeIdentifier(memberID);
5950          }
5951          break;
5952       }
5953       case typeSizeExp:
5954       {
5955          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5956          FreeExpContents(exp);
5957          exp.constant = PrintUInt(ComputeTypeSize(type));
5958          exp.type = constantExp;
5959          FreeType(type);
5960          break;
5961       }
5962       case classSizeExp:
5963       {
5964          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5965          if(classSym && classSym.registered)
5966          {
5967             if(classSym.registered.fixed)
5968             {
5969                FreeSpecifier(exp._class);
5970                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5971                exp.type = constantExp;
5972             }
5973             else
5974             {
5975                char className[1024];
5976                strcpy(className, "__ecereClass_");
5977                FullClassNameCat(className, classSym.string, true);
5978                MangleClassName(className);
5979
5980                DeclareClass(classSym, className);
5981
5982                FreeExpContents(exp);
5983                exp.type = pointerExp;
5984                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5985                exp.member.member = MkIdentifier("structSize");
5986             }
5987          }
5988          break;
5989       }
5990       case castExp:
5991       //case constantExp:
5992       {
5993          Type type;
5994          Expression e = exp;
5995          if(exp.type == castExp)
5996          {
5997             if(exp.cast.exp)
5998                ComputeExpression(exp.cast.exp);
5999             e = exp.cast.exp;
6000          }
6001          if(e && exp.expType)
6002          {
6003             /*if(exp.destType)
6004                type = exp.destType;
6005             else*/
6006                type = exp.expType;
6007             if(type.kind == classType)
6008             {
6009                Class _class = type._class.registered;
6010                if(_class && (_class.type == unitClass || _class.type == bitClass))
6011                {
6012                   if(!_class.dataType)
6013                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6014                   type = _class.dataType;
6015                }
6016             }
6017
6018             switch(type.kind)
6019             {
6020                case _BoolType:
6021                case charType:
6022                   if(type.isSigned)
6023                   {
6024                      char value = 0;
6025                      if(GetChar(e, &value))
6026                      {
6027                         FreeExpContents(exp);
6028                         exp.constant = PrintChar(value);
6029                         exp.type = constantExp;
6030                      }
6031                   }
6032                   else
6033                   {
6034                      unsigned char value = 0;
6035                      if(GetUChar(e, &value))
6036                      {
6037                         FreeExpContents(exp);
6038                         exp.constant = PrintUChar(value);
6039                         exp.type = constantExp;
6040                      }
6041                   }
6042                   break;
6043                case shortType:
6044                   if(type.isSigned)
6045                   {
6046                      short value = 0;
6047                      if(GetShort(e, &value))
6048                      {
6049                         FreeExpContents(exp);
6050                         exp.constant = PrintShort(value);
6051                         exp.type = constantExp;
6052                      }
6053                   }
6054                   else
6055                   {
6056                      unsigned short value = 0;
6057                      if(GetUShort(e, &value))
6058                      {
6059                         FreeExpContents(exp);
6060                         exp.constant = PrintUShort(value);
6061                         exp.type = constantExp;
6062                      }
6063                   }
6064                   break;
6065                case intType:
6066                   if(type.isSigned)
6067                   {
6068                      int value = 0;
6069                      if(GetInt(e, &value))
6070                      {
6071                         FreeExpContents(exp);
6072                         exp.constant = PrintInt(value);
6073                         exp.type = constantExp;
6074                      }
6075                   }
6076                   else
6077                   {
6078                      unsigned int value = 0;
6079                      if(GetUInt(e, &value))
6080                      {
6081                         FreeExpContents(exp);
6082                         exp.constant = PrintUInt(value);
6083                         exp.type = constantExp;
6084                      }
6085                   }
6086                   break;
6087                case int64Type:
6088                   if(type.isSigned)
6089                   {
6090                      int64 value = 0;
6091                      if(GetInt64(e, &value))
6092                      {
6093                         FreeExpContents(exp);
6094                         exp.constant = PrintInt64(value);
6095                         exp.type = constantExp;
6096                      }
6097                   }
6098                   else
6099                   {
6100                      uint64 value = 0;
6101                      if(GetUInt64(e, &value))
6102                      {
6103                         FreeExpContents(exp);
6104                         exp.constant = PrintUInt64(value);
6105                         exp.type = constantExp;
6106                      }
6107                   }
6108                   break;
6109                case intPtrType:
6110                   if(type.isSigned)
6111                   {
6112                      intptr value = 0;
6113                      if(GetIntPtr(e, &value))
6114                      {
6115                         FreeExpContents(exp);
6116                         exp.constant = PrintInt64((int64)value);
6117                         exp.type = constantExp;
6118                      }
6119                   }
6120                   else
6121                   {
6122                      uintptr value = 0;
6123                      if(GetUIntPtr(e, &value))
6124                      {
6125                         FreeExpContents(exp);
6126                         exp.constant = PrintUInt64((uint64)value);
6127                         exp.type = constantExp;
6128                      }
6129                   }
6130                   break;
6131                case intSizeType:
6132                   if(type.isSigned)
6133                   {
6134                      intsize value = 0;
6135                      if(GetIntSize(e, &value))
6136                      {
6137                         FreeExpContents(exp);
6138                         exp.constant = PrintInt64((int64)value);
6139                         exp.type = constantExp;
6140                      }
6141                   }
6142                   else
6143                   {
6144                      uintsize value = 0;
6145                      if(GetUIntSize(e, &value))
6146                      {
6147                         FreeExpContents(exp);
6148                         exp.constant = PrintUInt64((uint64)value);
6149                         exp.type = constantExp;
6150                      }
6151                   }
6152                   break;
6153                case floatType:
6154                {
6155                   float value = 0;
6156                   if(GetFloat(e, &value))
6157                   {
6158                      FreeExpContents(exp);
6159                      exp.constant = PrintFloat(value);
6160                      exp.type = constantExp;
6161                   }
6162                   break;
6163                }
6164                case doubleType:
6165                {
6166                   double value = 0;
6167                   if(GetDouble(e, &value))
6168                   {
6169                      FreeExpContents(exp);
6170                      exp.constant = PrintDouble(value);
6171                      exp.type = constantExp;
6172                   }
6173                   break;
6174                }
6175             }
6176          }
6177          break;
6178       }
6179       case conditionExp:
6180       {
6181          Operand op1 { };
6182          Operand op2 { };
6183          Operand op3 { };
6184
6185          if(exp.cond.exp)
6186             // Caring only about last expression for now...
6187             ComputeExpression(exp.cond.exp->last);
6188          if(exp.cond.elseExp)
6189             ComputeExpression(exp.cond.elseExp);
6190          if(exp.cond.cond)
6191             ComputeExpression(exp.cond.cond);
6192
6193          op1 = GetOperand(exp.cond.cond);
6194          if(op1.type) op1.type.refCount++;
6195          op2 = GetOperand(exp.cond.exp->last);
6196          if(op2.type) op2.type.refCount++;
6197          op3 = GetOperand(exp.cond.elseExp);
6198          if(op3.type) op3.type.refCount++;
6199
6200          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6201          if(op1.type) FreeType(op1.type);
6202          if(op2.type) FreeType(op2.type);
6203          if(op3.type) FreeType(op3.type);
6204          break;
6205       }
6206    }
6207 }
6208
6209 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
6210 {
6211    bool result = true;
6212    if(destType)
6213    {
6214       OldList converts { };
6215       Conversion convert;
6216
6217       if(destType.kind == voidType)
6218          return false;
6219
6220       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
6221          result = false;
6222       if(converts.count)
6223       {
6224          // for(convert = converts.last; convert; convert = convert.prev)
6225          for(convert = converts.first; convert; convert = convert.next)
6226          {
6227             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6228             if(!empty)
6229             {
6230                Expression newExp { };
6231                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6232
6233                // TODO: Check this...
6234                *newExp = *exp;
6235                newExp.destType = null;
6236
6237                if(convert.isGet)
6238                {
6239                   // [exp].ColorRGB
6240                   exp.type = memberExp;
6241                   exp.addedThis = true;
6242                   exp.member.exp = newExp;
6243                   FreeType(exp.member.exp.expType);
6244
6245                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6246                   exp.member.exp.expType.classObjectType = objectType;
6247                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6248                   exp.member.memberType = propertyMember;
6249                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6250                   // TESTING THIS... for (int)degrees
6251                   exp.needCast = true;
6252                   if(exp.expType) exp.expType.refCount++;
6253                   ApplyAnyObjectLogic(exp.member.exp);
6254                }
6255                else
6256                {
6257
6258                   /*if(exp.isConstant)
6259                   {
6260                      // Color { ColorRGB = [exp] };
6261                      exp.type = instanceExp;
6262                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6263                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6264                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6265                   }
6266                   else*/
6267                   {
6268                      // If not constant, don't turn it yet into an instantiation
6269                      // (Go through the deep members system first)
6270                      exp.type = memberExp;
6271                      exp.addedThis = true;
6272                      exp.member.exp = newExp;
6273
6274                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6275                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6276                         newExp.expType._class.registered.type == noHeadClass)
6277                      {
6278                         newExp.byReference = true;
6279                      }
6280
6281                      FreeType(exp.member.exp.expType);
6282                      /*exp.member.exp.expType = convert.convert.dataType;
6283                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6284                      exp.member.exp.expType = null;
6285                      if(convert.convert.dataType)
6286                      {
6287                         exp.member.exp.expType = { };
6288                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6289                         exp.member.exp.expType.refCount = 1;
6290                         exp.member.exp.expType.classObjectType = objectType;
6291                         ApplyAnyObjectLogic(exp.member.exp);
6292                      }
6293
6294                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6295                      exp.member.memberType = reverseConversionMember;
6296                      exp.expType = convert.resultType ? convert.resultType :
6297                         MkClassType(convert.convert._class.fullName);
6298                      exp.needCast = true;
6299                      if(convert.resultType) convert.resultType.refCount++;
6300                   }
6301                }
6302             }
6303             else
6304             {
6305                FreeType(exp.expType);
6306                if(convert.isGet)
6307                {
6308                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6309                   exp.needCast = true;
6310                   if(exp.expType) exp.expType.refCount++;
6311                }
6312                else
6313                {
6314                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6315                   exp.needCast = true;
6316                   if(convert.resultType)
6317                      convert.resultType.refCount++;
6318                }
6319             }
6320          }
6321          if(exp.isConstant && inCompiler)
6322             ComputeExpression(exp);
6323
6324          converts.Free(FreeConvert);
6325       }
6326
6327       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6328       {
6329          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6330       }
6331       if(!result && exp.expType && exp.destType)
6332       {
6333          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6334              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6335             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6336             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6337             result = true;
6338       }
6339    }
6340    // if(result) CheckTemplateTypes(exp);
6341    return result;
6342 }
6343
6344 void CheckTemplateTypes(Expression exp)
6345 {
6346    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6347    {
6348       Expression newExp { };
6349       Statement compound;
6350       Context context;
6351       *newExp = *exp;
6352       if(exp.destType) exp.destType.refCount++;
6353       if(exp.expType)  exp.expType.refCount++;
6354       newExp.prev = null;
6355       newExp.next = null;
6356
6357       switch(exp.expType.kind)
6358       {
6359          case doubleType:
6360             if(exp.destType.classObjectType)
6361             {
6362                // We need to pass the address, just pass it along (Undo what was done above)
6363                if(exp.destType) exp.destType.refCount--;
6364                if(exp.expType)  exp.expType.refCount--;
6365                delete newExp;
6366             }
6367             else
6368             {
6369                // If we're looking for value:
6370                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6371                OldList * specs;
6372                OldList * unionDefs = MkList();
6373                OldList * statements = MkList();
6374                context = PushContext();
6375                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6376                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6377                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6378                exp.type = extensionCompoundExp;
6379                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6380                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6381                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6382                exp.compound.compound.context = context;
6383                PopContext(context);
6384             }
6385             break;
6386          default:
6387             exp.type = castExp;
6388             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6389             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6390             break;
6391       }
6392    }
6393    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6394    {
6395       Expression newExp { };
6396       Statement compound;
6397       Context context;
6398       *newExp = *exp;
6399       if(exp.destType) exp.destType.refCount++;
6400       if(exp.expType)  exp.expType.refCount++;
6401       newExp.prev = null;
6402       newExp.next = null;
6403
6404       switch(exp.expType.kind)
6405       {
6406          case doubleType:
6407             if(exp.destType.classObjectType)
6408             {
6409                // We need to pass the address, just pass it along (Undo what was done above)
6410                if(exp.destType) exp.destType.refCount--;
6411                if(exp.expType)  exp.expType.refCount--;
6412                delete newExp;
6413             }
6414             else
6415             {
6416                // If we're looking for value:
6417                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6418                OldList * specs;
6419                OldList * unionDefs = MkList();
6420                OldList * statements = MkList();
6421                context = PushContext();
6422                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6423                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6424                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6425                exp.type = extensionCompoundExp;
6426                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6427                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6428                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6429                exp.compound.compound.context = context;
6430                PopContext(context);
6431             }
6432             break;
6433          case classType:
6434          {
6435             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6436             {
6437                exp.type = bracketsExp;
6438                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6439                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6440                ProcessExpressionType(exp.list->first);
6441                break;
6442             }
6443             else
6444             {
6445                exp.type = bracketsExp;
6446                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6447                newExp.needCast = true;
6448                ProcessExpressionType(exp.list->first);
6449                break;
6450             }
6451          }
6452          default:
6453          {
6454             if(exp.expType.kind == templateType)
6455             {
6456                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6457                if(type)
6458                {
6459                   FreeType(exp.destType);
6460                   FreeType(exp.expType);
6461                   delete newExp;
6462                   break;
6463                }
6464             }
6465             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6466             {
6467                exp.type = opExp;
6468                exp.op.op = '*';
6469                exp.op.exp1 = null;
6470                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6471                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6472             }
6473             else
6474             {
6475                char typeString[1024];
6476                Declarator decl;
6477                OldList * specs = MkList();
6478                typeString[0] = '\0';
6479                PrintType(exp.expType, typeString, false, false);
6480                decl = SpecDeclFromString(typeString, specs, null);
6481
6482                exp.type = castExp;
6483                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6484                exp.cast.typeName = MkTypeName(specs, decl);
6485                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6486                exp.cast.exp.needCast = true;
6487             }
6488             break;
6489          }
6490       }
6491    }
6492 }
6493 // TODO: The Symbol tree should be reorganized by namespaces
6494 // Name Space:
6495 //    - Tree of all symbols within (stored without namespace)
6496 //    - Tree of sub-namespaces
6497
6498 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6499 {
6500    int nsLen = strlen(nameSpace);
6501    Symbol symbol;
6502    // Start at the name space prefix
6503    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6504    {
6505       char * s = symbol.string;
6506       if(!strncmp(s, nameSpace, nsLen))
6507       {
6508          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6509          int c;
6510          char * namePart;
6511          for(c = strlen(s)-1; c >= 0; c--)
6512             if(s[c] == ':')
6513                break;
6514
6515          namePart = s+c+1;
6516          if(!strcmp(namePart, name))
6517          {
6518             // TODO: Error on ambiguity
6519             return symbol;
6520          }
6521       }
6522       else
6523          break;
6524    }
6525    return null;
6526 }
6527
6528 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6529 {
6530    int c;
6531    char nameSpace[1024];
6532    char * namePart;
6533    bool gotColon = false;
6534
6535    nameSpace[0] = '\0';
6536    for(c = strlen(name)-1; c >= 0; c--)
6537       if(name[c] == ':')
6538       {
6539          gotColon = true;
6540          break;
6541       }
6542
6543    namePart = name+c+1;
6544    while(c >= 0 && name[c] == ':') c--;
6545    if(c >= 0)
6546    {
6547       // Try an exact match first
6548       Symbol symbol = (Symbol)tree.FindString(name);
6549       if(symbol)
6550          return symbol;
6551
6552       // Namespace specified
6553       memcpy(nameSpace, name, c + 1);
6554       nameSpace[c+1] = 0;
6555
6556       return ScanWithNameSpace(tree, nameSpace, namePart);
6557    }
6558    else if(gotColon)
6559    {
6560       // Looking for a global symbol, e.g. ::Sleep()
6561       Symbol symbol = (Symbol)tree.FindString(namePart);
6562       return symbol;
6563    }
6564    else
6565    {
6566       // Name only (no namespace specified)
6567       Symbol symbol = (Symbol)tree.FindString(namePart);
6568       if(symbol)
6569          return symbol;
6570       return ScanWithNameSpace(tree, "", namePart);
6571    }
6572    return null;
6573 }
6574
6575 static void ProcessDeclaration(Declaration decl);
6576
6577 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6578 {
6579 #ifdef _DEBUG
6580    //Time startTime = GetTime();
6581 #endif
6582    // Optimize this later? Do this before/less?
6583    Context ctx;
6584    Symbol symbol = null;
6585    // First, check if the identifier is declared inside the function
6586    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6587
6588    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6589    {
6590       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6591       {
6592          symbol = null;
6593          if(thisNameSpace)
6594          {
6595             char curName[1024];
6596             strcpy(curName, thisNameSpace);
6597             strcat(curName, "::");
6598             strcat(curName, name);
6599             // Try to resolve in current namespace first
6600             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6601          }
6602          if(!symbol)
6603             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6604       }
6605       else
6606          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6607
6608       if(symbol || ctx == endContext) break;
6609    }
6610    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6611    {
6612       if(symbol.pointerExternal.type == functionExternal)
6613       {
6614          FunctionDefinition function = symbol.pointerExternal.function;
6615
6616          // Modified this recently...
6617          Context tmpContext = curContext;
6618          curContext = null;
6619          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6620          curContext = tmpContext;
6621
6622          symbol.pointerExternal.symbol = symbol;
6623
6624          // TESTING THIS:
6625          DeclareType(symbol.type, true, true);
6626
6627          ast->Insert(curExternal.prev, symbol.pointerExternal);
6628
6629          symbol.id = curExternal.symbol.idCode;
6630
6631       }
6632       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6633       {
6634          ast->Move(symbol.pointerExternal, curExternal.prev);
6635          symbol.id = curExternal.symbol.idCode;
6636       }
6637    }
6638 #ifdef _DEBUG
6639    //findSymbolTotalTime += GetTime() - startTime;
6640 #endif
6641    return symbol;
6642 }
6643
6644 static void GetTypeSpecs(Type type, OldList * specs)
6645 {
6646    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6647    switch(type.kind)
6648    {
6649       case classType:
6650       {
6651          if(type._class.registered)
6652          {
6653             if(!type._class.registered.dataType)
6654                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6655             GetTypeSpecs(type._class.registered.dataType, specs);
6656          }
6657          break;
6658       }
6659       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6660       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6661       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6662       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6663       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6664       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6665       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6666       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6667       case intType:
6668       default:
6669          ListAdd(specs, MkSpecifier(INT)); break;
6670    }
6671 }
6672
6673 static void PrintArraySize(Type arrayType, char * string)
6674 {
6675    char size[256];
6676    size[0] = '\0';
6677    strcat(size, "[");
6678    if(arrayType.enumClass)
6679       strcat(size, arrayType.enumClass.string);
6680    else if(arrayType.arraySizeExp)
6681       PrintExpression(arrayType.arraySizeExp, size);
6682    strcat(size, "]");
6683    strcat(string, size);
6684 }
6685
6686 // WARNING : This function expects a null terminated string since it recursively concatenate...
6687 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6688 {
6689    if(type)
6690    {
6691       if(printConst && type.constant)
6692          strcat(string, "const ");
6693       switch(type.kind)
6694       {
6695          case classType:
6696          {
6697             Symbol c = type._class;
6698             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6699             //       look into merging with thisclass ?
6700             if(type.classObjectType == typedObject)
6701                strcat(string, "typed_object");
6702             else if(type.classObjectType == anyObject)
6703                strcat(string, "any_object");
6704             else
6705             {
6706                if(c && c.string)
6707                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6708             }
6709             if(type.byReference)
6710                strcat(string, " &");
6711             break;
6712          }
6713          case voidType: strcat(string, "void"); break;
6714          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6715          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6716          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6717          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6718          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6719          case _BoolType: strcat(string, "_Bool"); break;
6720          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6721          case floatType: strcat(string, "float"); break;
6722          case doubleType: strcat(string, "double"); break;
6723          case structType:
6724             if(type.enumName)
6725             {
6726                strcat(string, "struct ");
6727                strcat(string, type.enumName);
6728             }
6729             else if(type.typeName)
6730                strcat(string, type.typeName);
6731             else
6732             {
6733                Type member;
6734                strcat(string, "struct { ");
6735                for(member = type.members.first; member; member = member.next)
6736                {
6737                   PrintType(member, string, true, fullName);
6738                   strcat(string,"; ");
6739                }
6740                strcat(string,"}");
6741             }
6742             break;
6743          case unionType:
6744             if(type.enumName)
6745             {
6746                strcat(string, "union ");
6747                strcat(string, type.enumName);
6748             }
6749             else if(type.typeName)
6750                strcat(string, type.typeName);
6751             else
6752             {
6753                strcat(string, "union ");
6754                strcat(string,"(unnamed)");
6755             }
6756             break;
6757          case enumType:
6758             if(type.enumName)
6759             {
6760                strcat(string, "enum ");
6761                strcat(string, type.enumName);
6762             }
6763             else if(type.typeName)
6764                strcat(string, type.typeName);
6765             else
6766                strcat(string, "int"); // "enum");
6767             break;
6768          case ellipsisType:
6769             strcat(string, "...");
6770             break;
6771          case subClassType:
6772             strcat(string, "subclass(");
6773             strcat(string, type._class ? type._class.string : "int");
6774             strcat(string, ")");
6775             break;
6776          case templateType:
6777             strcat(string, type.templateParameter.identifier.string);
6778             break;
6779          case thisClassType:
6780             strcat(string, "thisclass");
6781             break;
6782          case vaListType:
6783             strcat(string, "__builtin_va_list");
6784             break;
6785       }
6786    }
6787 }
6788
6789 static void PrintName(Type type, char * string, bool fullName)
6790 {
6791    if(type.name && type.name[0])
6792    {
6793       if(fullName)
6794          strcat(string, type.name);
6795       else
6796       {
6797          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6798          if(name) name += 2; else name = type.name;
6799          strcat(string, name);
6800       }
6801    }
6802 }
6803
6804 static void PrintAttribs(Type type, char * string)
6805 {
6806    if(type)
6807    {
6808       if(type.dllExport)   strcat(string, "dllexport ");
6809       if(type.attrStdcall) strcat(string, "stdcall ");
6810    }
6811 }
6812
6813 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6814 {
6815    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6816    {
6817       Type attrType = null;
6818       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6819          PrintAttribs(type, string);
6820       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6821          strcat(string, " const");
6822       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6823       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6824          strcat(string, " (");
6825       if(type.kind == pointerType)
6826       {
6827          if(type.type.kind == functionType || type.type.kind == methodType)
6828             PrintAttribs(type.type, string);
6829       }
6830       if(type.kind == pointerType)
6831       {
6832          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6833             strcat(string, "*");
6834          else
6835             strcat(string, " *");
6836       }
6837       if(printConst && type.constant && type.kind == pointerType)
6838          strcat(string, " const");
6839    }
6840    else
6841       PrintTypeSpecs(type, string, fullName, printConst);
6842 }
6843
6844 static void PostPrintType(Type type, char * string, bool fullName)
6845 {
6846    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6847       strcat(string, ")");
6848    if(type.kind == arrayType)
6849       PrintArraySize(type, string);
6850    else if(type.kind == functionType)
6851    {
6852       Type param;
6853       strcat(string, "(");
6854       for(param = type.params.first; param; param = param.next)
6855       {
6856          PrintType(param, string, true, fullName);
6857          if(param.next) strcat(string, ", ");
6858       }
6859       strcat(string, ")");
6860    }
6861    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6862       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6863 }
6864
6865 // *****
6866 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6867 // *****
6868 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6869 {
6870    PrePrintType(type, string, fullName, null, printConst);
6871
6872    if(type.thisClass || (printName && type.name && type.name[0]))
6873       strcat(string, " ");
6874    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6875    {
6876       Symbol _class = type.thisClass;
6877       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6878       {
6879          if(type.classObjectType == classPointer)
6880             strcat(string, "class");
6881          else
6882             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6883       }
6884       else if(_class && _class.string)
6885       {
6886          String s = _class.string;
6887          if(fullName)
6888             strcat(string, s);
6889          else
6890          {
6891             char * name = RSearchString(s, "::", strlen(s), true, false);
6892             if(name) name += 2; else name = s;
6893             strcat(string, name);
6894          }
6895       }
6896       strcat(string, "::");
6897    }
6898
6899    if(printName && type.name)
6900       PrintName(type, string, fullName);
6901    PostPrintType(type, string, fullName);
6902    if(type.bitFieldCount)
6903    {
6904       char count[100];
6905       sprintf(count, ":%d", type.bitFieldCount);
6906       strcat(string, count);
6907    }
6908 }
6909
6910 void PrintType(Type type, char * string, bool printName, bool fullName)
6911 {
6912    _PrintType(type, string, printName, fullName, true);
6913 }
6914
6915 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6916 {
6917    _PrintType(type, string, printName, fullName, false);
6918 }
6919
6920 static Type FindMember(Type type, char * string)
6921 {
6922    Type memberType;
6923    for(memberType = type.members.first; memberType; memberType = memberType.next)
6924    {
6925       if(!memberType.name)
6926       {
6927          Type subType = FindMember(memberType, string);
6928          if(subType)
6929             return subType;
6930       }
6931       else if(!strcmp(memberType.name, string))
6932          return memberType;
6933    }
6934    return null;
6935 }
6936
6937 Type FindMemberAndOffset(Type type, char * string, uint * offset)
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          {
6947             *offset += memberType.offset;
6948             return subType;
6949          }
6950       }
6951       else if(!strcmp(memberType.name, string))
6952       {
6953          *offset += memberType.offset;
6954          return memberType;
6955       }
6956    }
6957    return null;
6958 }
6959
6960 public bool GetParseError() { return parseError; }
6961
6962 Expression ParseExpressionString(char * expression)
6963 {
6964    parseError = false;
6965
6966    fileInput = TempFile { };
6967    fileInput.Write(expression, 1, strlen(expression));
6968    fileInput.Seek(0, start);
6969
6970    echoOn = false;
6971    parsedExpression = null;
6972    resetScanner();
6973    expression_yyparse();
6974    delete fileInput;
6975
6976    return parsedExpression;
6977 }
6978
6979 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6980 {
6981    Identifier id = exp.identifier;
6982    Method method = null;
6983    Property prop = null;
6984    DataMember member = null;
6985    ClassProperty classProp = null;
6986
6987    if(_class && _class.type == enumClass)
6988    {
6989       NamedLink value = null;
6990       Class enumClass = eSystem_FindClass(privateModule, "enum");
6991       if(enumClass)
6992       {
6993          Class baseClass;
6994          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6995          {
6996             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6997             for(value = e.values.first; value; value = value.next)
6998             {
6999                if(!strcmp(value.name, id.string))
7000                   break;
7001             }
7002             if(value)
7003             {
7004                char constant[256];
7005
7006                FreeExpContents(exp);
7007
7008                exp.type = constantExp;
7009                exp.isConstant = true;
7010                if(!strcmp(baseClass.dataTypeString, "int"))
7011                   sprintf(constant, "%d",(int)value.data);
7012                else
7013                   sprintf(constant, "0x%X",(int)value.data);
7014                exp.constant = CopyString(constant);
7015                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7016                exp.expType = MkClassType(baseClass.fullName);
7017                break;
7018             }
7019          }
7020       }
7021       if(value)
7022          return true;
7023    }
7024    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7025    {
7026       ProcessMethodType(method);
7027       exp.expType = Type
7028       {
7029          refCount = 1;
7030          kind = methodType;
7031          method = method;
7032          // Crash here?
7033          // TOCHECK: Put it back to what it was...
7034          // methodClass = _class;
7035          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7036       };
7037       //id._class = null;
7038       return true;
7039    }
7040    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7041    {
7042       if(!prop.dataType)
7043          ProcessPropertyType(prop);
7044       exp.expType = prop.dataType;
7045       if(prop.dataType) prop.dataType.refCount++;
7046       return true;
7047    }
7048    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7049    {
7050       if(!member.dataType)
7051          member.dataType = ProcessTypeString(member.dataTypeString, false);
7052       exp.expType = member.dataType;
7053       if(member.dataType) member.dataType.refCount++;
7054       return true;
7055    }
7056    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7057    {
7058       if(!classProp.dataType)
7059          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7060
7061       if(classProp.constant)
7062       {
7063          FreeExpContents(exp);
7064
7065          exp.isConstant = true;
7066          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7067          {
7068             //char constant[256];
7069             exp.type = stringExp;
7070             exp.constant = QMkString((char *)classProp.Get(_class));
7071          }
7072          else
7073          {
7074             char constant[256];
7075             exp.type = constantExp;
7076             sprintf(constant, "%d", (int)classProp.Get(_class));
7077             exp.constant = CopyString(constant);
7078          }
7079       }
7080       else
7081       {
7082          // TO IMPLEMENT...
7083       }
7084
7085       exp.expType = classProp.dataType;
7086       if(classProp.dataType) classProp.dataType.refCount++;
7087       return true;
7088    }
7089    return false;
7090 }
7091
7092 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7093 {
7094    BinaryTree * tree = &nameSpace.functions;
7095    GlobalData data = (GlobalData)tree->FindString(name);
7096    NameSpace * child;
7097    if(!data)
7098    {
7099       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7100       {
7101          data = ScanGlobalData(child, name);
7102          if(data)
7103             break;
7104       }
7105    }
7106    return data;
7107 }
7108
7109 static GlobalData FindGlobalData(char * name)
7110 {
7111    int start = 0, c;
7112    NameSpace * nameSpace;
7113    nameSpace = globalData;
7114    for(c = 0; name[c]; c++)
7115    {
7116       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7117       {
7118          NameSpace * newSpace;
7119          char * spaceName = new char[c - start + 1];
7120          strncpy(spaceName, name + start, c - start);
7121          spaceName[c-start] = '\0';
7122          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7123          delete spaceName;
7124          if(!newSpace)
7125             return null;
7126          nameSpace = newSpace;
7127          if(name[c] == ':') c++;
7128          start = c+1;
7129       }
7130    }
7131    if(c - start)
7132    {
7133       return ScanGlobalData(nameSpace, name + start);
7134    }
7135    return null;
7136 }
7137
7138 static int definedExpStackPos;
7139 static void * definedExpStack[512];
7140
7141 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7142 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7143 {
7144    Expression prev = checkedExp.prev, next = checkedExp.next;
7145
7146    FreeExpContents(checkedExp);
7147    FreeType(checkedExp.expType);
7148    FreeType(checkedExp.destType);
7149
7150    *checkedExp = *newExp;
7151
7152    delete newExp;
7153
7154    checkedExp.prev = prev;
7155    checkedExp.next = next;
7156 }
7157
7158 void ApplyAnyObjectLogic(Expression e)
7159 {
7160    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7161 #ifdef _DEBUG
7162    char debugExpString[4096];
7163    debugExpString[0] = '\0';
7164    PrintExpression(e, debugExpString);
7165 #endif
7166
7167    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7168    {
7169       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7170       //ellipsisDestType = destType;
7171       if(e && e.expType)
7172       {
7173          Type type = e.expType;
7174          Class _class = null;
7175          //Type destType = e.destType;
7176
7177          if(type.kind == classType && type._class && type._class.registered)
7178          {
7179             _class = type._class.registered;
7180          }
7181          else if(type.kind == subClassType)
7182          {
7183             _class = FindClass("ecere::com::Class").registered;
7184          }
7185          else
7186          {
7187             char string[1024] = "";
7188             Symbol classSym;
7189
7190             PrintTypeNoConst(type, string, false, true);
7191             classSym = FindClass(string);
7192             if(classSym) _class = classSym.registered;
7193          }
7194
7195          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...
7196             (!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))) ||
7197             destType.byReference)))
7198          {
7199             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7200             {
7201                Expression checkedExp = e, newExp;
7202
7203                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7204                {
7205                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7206                   {
7207                      if(checkedExp.type == extensionCompoundExp)
7208                      {
7209                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7210                      }
7211                      else
7212                         checkedExp = checkedExp.list->last;
7213                   }
7214                   else if(checkedExp.type == castExp)
7215                      checkedExp = checkedExp.cast.exp;
7216                }
7217
7218                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7219                {
7220                   newExp = checkedExp.op.exp2;
7221                   checkedExp.op.exp2 = null;
7222                   FreeExpContents(checkedExp);
7223
7224                   if(e.expType && e.expType.passAsTemplate)
7225                   {
7226                      char size[100];
7227                      ComputeTypeSize(e.expType);
7228                      sprintf(size, "%d", e.expType.size);
7229                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7230                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7231                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7232                   }
7233
7234                   ReplaceExpContents(checkedExp, newExp);
7235                   e.byReference = true;
7236                }
7237                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7238                {
7239                   Expression checkedExp, newExp;
7240
7241                   {
7242                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7243                      bool hasAddress =
7244                         e.type == identifierExp ||
7245                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7246                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7247                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7248                         e.type == indexExp;
7249
7250                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7251                      {
7252                         Context context = PushContext();
7253                         Declarator decl;
7254                         OldList * specs = MkList();
7255                         char typeString[1024];
7256                         Expression newExp { };
7257
7258                         typeString[0] = '\0';
7259                         *newExp = *e;
7260
7261                         //if(e.destType) e.destType.refCount++;
7262                         // if(exp.expType) exp.expType.refCount++;
7263                         newExp.prev = null;
7264                         newExp.next = null;
7265                         newExp.expType = null;
7266
7267                         PrintTypeNoConst(e.expType, typeString, false, true);
7268                         decl = SpecDeclFromString(typeString, specs, null);
7269                         newExp.destType = ProcessType(specs, decl);
7270
7271                         curContext = context;
7272
7273                         // We need a current compound for this
7274                         if(curCompound)
7275                         {
7276                            char name[100];
7277                            OldList * stmts = MkList();
7278                            e.type = extensionCompoundExp;
7279                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7280                            if(!curCompound.compound.declarations)
7281                               curCompound.compound.declarations = MkList();
7282                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7283                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7284                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7285                            e.compound = MkCompoundStmt(null, stmts);
7286                         }
7287                         else
7288                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7289
7290                         /*
7291                         e.compound = MkCompoundStmt(
7292                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7293                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7294
7295                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7296                         */
7297
7298                         {
7299                            Type type = e.destType;
7300                            e.destType = { };
7301                            CopyTypeInto(e.destType, type);
7302                            e.destType.refCount = 1;
7303                            e.destType.classObjectType = none;
7304                            FreeType(type);
7305                         }
7306
7307                         e.compound.compound.context = context;
7308                         PopContext(context);
7309                         curContext = context.parent;
7310                      }
7311                   }
7312
7313                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7314                   checkedExp = e;
7315                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7316                   {
7317                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7318                      {
7319                         if(checkedExp.type == extensionCompoundExp)
7320                         {
7321                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7322                         }
7323                         else
7324                            checkedExp = checkedExp.list->last;
7325                      }
7326                      else if(checkedExp.type == castExp)
7327                         checkedExp = checkedExp.cast.exp;
7328                   }
7329                   {
7330                      Expression operand { };
7331                      operand = *checkedExp;
7332                      checkedExp.destType = null;
7333                      checkedExp.expType = null;
7334                      checkedExp.Clear();
7335                      checkedExp.type = opExp;
7336                      checkedExp.op.op = '&';
7337                      checkedExp.op.exp1 = null;
7338                      checkedExp.op.exp2 = operand;
7339
7340                      //newExp = MkExpOp(null, '&', checkedExp);
7341                   }
7342                   //ReplaceExpContents(checkedExp, newExp);
7343                }
7344             }
7345          }
7346       }
7347    }
7348    {
7349       // If expression type is a simple class, make it an address
7350       // FixReference(e, true);
7351    }
7352 //#if 0
7353    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7354       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7355          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7356    {
7357       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"))
7358       {
7359          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7360       }
7361       else
7362       {
7363          Expression thisExp { };
7364
7365          *thisExp = *e;
7366          thisExp.prev = null;
7367          thisExp.next = null;
7368          e.Clear();
7369
7370          e.type = bracketsExp;
7371          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7372          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7373             ((Expression)e.list->first).byReference = true;
7374
7375          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7376          {
7377             e.expType = thisExp.expType;
7378             e.expType.refCount++;
7379          }
7380          else*/
7381          {
7382             e.expType = { };
7383             CopyTypeInto(e.expType, thisExp.expType);
7384             e.expType.byReference = false;
7385             e.expType.refCount = 1;
7386
7387             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7388                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7389             {
7390                e.expType.classObjectType = none;
7391             }
7392          }
7393       }
7394    }
7395 // TOFIX: Try this for a nice IDE crash!
7396 //#endif
7397    // The other way around
7398    else
7399 //#endif
7400    if(destType && e.expType &&
7401          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7402          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7403          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7404    {
7405       if(destType.kind == ellipsisType)
7406       {
7407          Compiler_Error($"Unspecified type\n");
7408       }
7409       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7410       {
7411          bool byReference = e.expType.byReference;
7412          Expression thisExp { };
7413          Declarator decl;
7414          OldList * specs = MkList();
7415          char typeString[1024]; // Watch buffer overruns
7416          Type type;
7417          ClassObjectType backupClassObjectType;
7418          bool backupByReference;
7419
7420          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7421             type = e.expType;
7422          else
7423             type = destType;
7424
7425          backupClassObjectType = type.classObjectType;
7426          backupByReference = type.byReference;
7427
7428          type.classObjectType = none;
7429          type.byReference = false;
7430
7431          typeString[0] = '\0';
7432          PrintType(type, typeString, false, true);
7433          decl = SpecDeclFromString(typeString, specs, null);
7434
7435          type.classObjectType = backupClassObjectType;
7436          type.byReference = backupByReference;
7437
7438          *thisExp = *e;
7439          thisExp.prev = null;
7440          thisExp.next = null;
7441          e.Clear();
7442
7443          if( ( type.kind == classType && type._class && type._class.registered &&
7444                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7445                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7446              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7447              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7448          {
7449             e.type = opExp;
7450             e.op.op = '*';
7451             e.op.exp1 = null;
7452             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7453
7454             e.expType = { };
7455             CopyTypeInto(e.expType, type);
7456             e.expType.byReference = false;
7457             e.expType.refCount = 1;
7458          }
7459          else
7460          {
7461             e.type = castExp;
7462             e.cast.typeName = MkTypeName(specs, decl);
7463             e.cast.exp = thisExp;
7464             e.byReference = true;
7465             e.expType = type;
7466             type.refCount++;
7467          }
7468          e.destType = destType;
7469          destType.refCount++;
7470       }
7471    }
7472 }
7473
7474 void ProcessExpressionType(Expression exp)
7475 {
7476    bool unresolved = false;
7477    Location oldyylloc = yylloc;
7478    bool notByReference = false;
7479 #ifdef _DEBUG
7480    char debugExpString[4096];
7481    debugExpString[0] = '\0';
7482    PrintExpression(exp, debugExpString);
7483 #endif
7484    if(!exp || exp.expType)
7485       return;
7486
7487    //eSystem_Logf("%s\n", expString);
7488
7489    // Testing this here
7490    yylloc = exp.loc;
7491    switch(exp.type)
7492    {
7493       case identifierExp:
7494       {
7495          Identifier id = exp.identifier;
7496          if(!id || !topContext) return;
7497
7498          // DOING THIS LATER NOW...
7499          if(id._class && id._class.name)
7500          {
7501             id.classSym = id._class.symbol; // FindClass(id._class.name);
7502             /* TODO: Name Space Fix ups
7503             if(!id.classSym)
7504                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7505             */
7506          }
7507
7508          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7509          {
7510             exp.expType = ProcessTypeString("Module", true);
7511             break;
7512          }
7513          else */if(strstr(id.string, "__ecereClass") == id.string)
7514          {
7515             exp.expType = ProcessTypeString("ecere::com::Class", true);
7516             break;
7517          }
7518          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7519          {
7520             // Added this here as well
7521             ReplaceClassMembers(exp, thisClass);
7522             if(exp.type != identifierExp)
7523             {
7524                ProcessExpressionType(exp);
7525                break;
7526             }
7527
7528             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7529                break;
7530          }
7531          else
7532          {
7533             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7534             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7535             if(!symbol/* && exp.destType*/)
7536             {
7537                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7538                   break;
7539                else
7540                {
7541                   if(thisClass)
7542                   {
7543                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7544                      if(exp.type != identifierExp)
7545                      {
7546                         ProcessExpressionType(exp);
7547                         break;
7548                      }
7549                   }
7550                   // Static methods called from inside the _class
7551                   else if(currentClass && !id._class)
7552                   {
7553                      if(ResolveIdWithClass(exp, currentClass, true))
7554                         break;
7555                   }
7556                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7557                }
7558             }
7559
7560             // If we manage to resolve this symbol
7561             if(symbol)
7562             {
7563                Type type = symbol.type;
7564                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7565
7566                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7567                {
7568                   Context context = SetupTemplatesContext(_class);
7569                   type = ReplaceThisClassType(_class);
7570                   FinishTemplatesContext(context);
7571                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7572                }
7573
7574                FreeSpecifier(id._class);
7575                id._class = null;
7576                delete id.string;
7577                id.string = CopyString(symbol.string);
7578
7579                id.classSym = null;
7580                exp.expType = type;
7581                if(type)
7582                   type.refCount++;
7583                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7584                   // Add missing cases here... enum Classes...
7585                   exp.isConstant = true;
7586
7587                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7588                if(symbol.isParam || !strcmp(id.string, "this"))
7589                {
7590                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7591                      exp.byReference = true;
7592
7593                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7594                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7595                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7596                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7597                   {
7598                      Identifier id = exp.identifier;
7599                      exp.type = bracketsExp;
7600                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7601                   }*/
7602                }
7603
7604                if(symbol.isIterator)
7605                {
7606                   if(symbol.isIterator == 3)
7607                   {
7608                      exp.type = bracketsExp;
7609                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7610                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7611                      exp.expType = null;
7612                      ProcessExpressionType(exp);
7613                   }
7614                   else if(symbol.isIterator != 4)
7615                   {
7616                      exp.type = memberExp;
7617                      exp.member.exp = MkExpIdentifier(exp.identifier);
7618                      exp.member.exp.expType = exp.expType;
7619                      /*if(symbol.isIterator == 6)
7620                         exp.member.member = MkIdentifier("key");
7621                      else*/
7622                         exp.member.member = MkIdentifier("data");
7623                      exp.expType = null;
7624                      ProcessExpressionType(exp);
7625                   }
7626                }
7627                break;
7628             }
7629             else
7630             {
7631                DefinedExpression definedExp = null;
7632                if(thisNameSpace && !(id._class && !id._class.name))
7633                {
7634                   char name[1024];
7635                   strcpy(name, thisNameSpace);
7636                   strcat(name, "::");
7637                   strcat(name, id.string);
7638                   definedExp = eSystem_FindDefine(privateModule, name);
7639                }
7640                if(!definedExp)
7641                   definedExp = eSystem_FindDefine(privateModule, id.string);
7642                if(definedExp)
7643                {
7644                   int c;
7645                   for(c = 0; c<definedExpStackPos; c++)
7646                      if(definedExpStack[c] == definedExp)
7647                         break;
7648                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7649                   {
7650                      Location backupYylloc = yylloc;
7651                      File backInput = fileInput;
7652                      definedExpStack[definedExpStackPos++] = definedExp;
7653
7654                      fileInput = TempFile { };
7655                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7656                      fileInput.Seek(0, start);
7657
7658                      echoOn = false;
7659                      parsedExpression = null;
7660                      resetScanner();
7661                      expression_yyparse();
7662                      delete fileInput;
7663                      if(backInput)
7664                         fileInput = backInput;
7665
7666                      yylloc = backupYylloc;
7667
7668                      if(parsedExpression)
7669                      {
7670                         FreeIdentifier(id);
7671                         exp.type = bracketsExp;
7672                         exp.list = MkListOne(parsedExpression);
7673                         parsedExpression.loc = yylloc;
7674                         ProcessExpressionType(exp);
7675                         definedExpStackPos--;
7676                         return;
7677                      }
7678                      definedExpStackPos--;
7679                   }
7680                   else
7681                   {
7682                      if(inCompiler)
7683                      {
7684                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7685                      }
7686                   }
7687                }
7688                else
7689                {
7690                   GlobalData data = null;
7691                   if(thisNameSpace && !(id._class && !id._class.name))
7692                   {
7693                      char name[1024];
7694                      strcpy(name, thisNameSpace);
7695                      strcat(name, "::");
7696                      strcat(name, id.string);
7697                      data = FindGlobalData(name);
7698                   }
7699                   if(!data)
7700                      data = FindGlobalData(id.string);
7701                   if(data)
7702                   {
7703                      DeclareGlobalData(data);
7704                      exp.expType = data.dataType;
7705                      if(data.dataType) data.dataType.refCount++;
7706
7707                      delete id.string;
7708                      id.string = CopyString(data.fullName);
7709                      FreeSpecifier(id._class);
7710                      id._class = null;
7711
7712                      break;
7713                   }
7714                   else
7715                   {
7716                      GlobalFunction function = null;
7717                      if(thisNameSpace && !(id._class && !id._class.name))
7718                      {
7719                         char name[1024];
7720                         strcpy(name, thisNameSpace);
7721                         strcat(name, "::");
7722                         strcat(name, id.string);
7723                         function = eSystem_FindFunction(privateModule, name);
7724                      }
7725                      if(!function)
7726                         function = eSystem_FindFunction(privateModule, id.string);
7727                      if(function)
7728                      {
7729                         char name[1024];
7730                         delete id.string;
7731                         id.string = CopyString(function.name);
7732                         name[0] = 0;
7733
7734                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7735                            strcpy(name, "__ecereFunction_");
7736                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7737                         if(DeclareFunction(function, name))
7738                         {
7739                            delete id.string;
7740                            id.string = CopyString(name);
7741                         }
7742                         exp.expType = function.dataType;
7743                         if(function.dataType) function.dataType.refCount++;
7744
7745                         FreeSpecifier(id._class);
7746                         id._class = null;
7747
7748                         break;
7749                      }
7750                   }
7751                }
7752             }
7753          }
7754          unresolved = true;
7755          break;
7756       }
7757       case instanceExp:
7758       {
7759          Class _class;
7760          // Symbol classSym;
7761
7762          if(!exp.instance._class)
7763          {
7764             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7765             {
7766                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7767             }
7768          }
7769
7770          //classSym = FindClass(exp.instance._class.fullName);
7771          //_class = classSym ? classSym.registered : null;
7772
7773          ProcessInstantiationType(exp.instance);
7774          exp.isConstant = exp.instance.isConstant;
7775
7776          /*
7777          if(_class.type == unitClass && _class.base.type != systemClass)
7778          {
7779             {
7780                Type destType = exp.destType;
7781
7782                exp.destType = MkClassType(_class.base.fullName);
7783                exp.expType = MkClassType(_class.fullName);
7784                CheckExpressionType(exp, exp.destType, true);
7785
7786                exp.destType = destType;
7787             }
7788             exp.expType = MkClassType(_class.fullName);
7789          }
7790          else*/
7791          if(exp.instance._class)
7792          {
7793             exp.expType = MkClassType(exp.instance._class.name);
7794             /*if(exp.expType._class && exp.expType._class.registered &&
7795                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7796                exp.expType.byReference = true;*/
7797          }
7798          break;
7799       }
7800       case constantExp:
7801       {
7802          if(!exp.expType)
7803          {
7804             char * constant = exp.constant;
7805             Type type
7806             {
7807                refCount = 1;
7808                constant = true;
7809             };
7810             exp.expType = type;
7811
7812             if(constant[0] == '\'')
7813             {
7814                if((int)((byte *)constant)[1] > 127)
7815                {
7816                   int nb;
7817                   unichar ch = UTF8GetChar(constant + 1, &nb);
7818                   if(nb < 2) ch = constant[1];
7819                   delete constant;
7820                   exp.constant = PrintUInt(ch);
7821                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7822                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7823                   type._class = FindClass("unichar");
7824
7825                   type.isSigned = false;
7826                }
7827                else
7828                {
7829                   type.kind = charType;
7830                   type.isSigned = true;
7831                }
7832             }
7833             else
7834             {
7835                char * dot = strchr(constant, '.');
7836                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7837                char * exponent;
7838                if(isHex)
7839                {
7840                   exponent = strchr(constant, 'p');
7841                   if(!exponent) exponent = strchr(constant, 'P');
7842                }
7843                else
7844                {
7845                   exponent = strchr(constant, 'e');
7846                   if(!exponent) exponent = strchr(constant, 'E');
7847                }
7848
7849                if(dot || exponent)
7850                {
7851                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7852                      type.kind = floatType;
7853                   else
7854                      type.kind = doubleType;
7855                   type.isSigned = true;
7856                }
7857                else
7858                {
7859                   bool isSigned = constant[0] == '-';
7860                   int64 i64 = strtoll(constant, null, 0);
7861                   uint64 ui64 = strtoull(constant, null, 0);
7862                   bool is64Bit = false;
7863                   if(isSigned)
7864                   {
7865                      if(i64 < MININT)
7866                         is64Bit = true;
7867                   }
7868                   else
7869                   {
7870                      if(ui64 > MAXINT)
7871                      {
7872                         if(ui64 > MAXDWORD)
7873                         {
7874                            is64Bit = true;
7875                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7876                               isSigned = true;
7877                         }
7878                      }
7879                      else if(constant[0] != '0' || !constant[1])
7880                         isSigned = true;
7881                   }
7882                   type.kind = is64Bit ? int64Type : intType;
7883                   type.isSigned = isSigned;
7884                }
7885             }
7886             exp.isConstant = true;
7887             if(exp.destType && exp.destType.kind == doubleType)
7888                type.kind = doubleType;
7889             else if(exp.destType && exp.destType.kind == floatType)
7890                type.kind = floatType;
7891             else if(exp.destType && exp.destType.kind == int64Type)
7892                type.kind = int64Type;
7893          }
7894          break;
7895       }
7896       case stringExp:
7897       {
7898          exp.isConstant = true;      // Why wasn't this constant?
7899          exp.expType = Type
7900          {
7901             refCount = 1;
7902             kind = pointerType;
7903             type = Type
7904             {
7905                refCount = 1;
7906                kind = charType;
7907                constant = true;
7908                isSigned = true;
7909             }
7910          };
7911          break;
7912       }
7913       case newExp:
7914       case new0Exp:
7915          ProcessExpressionType(exp._new.size);
7916          exp.expType = Type
7917          {
7918             refCount = 1;
7919             kind = pointerType;
7920             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7921          };
7922          DeclareType(exp.expType.type, false, false);
7923          break;
7924       case renewExp:
7925       case renew0Exp:
7926          ProcessExpressionType(exp._renew.size);
7927          ProcessExpressionType(exp._renew.exp);
7928          exp.expType = Type
7929          {
7930             refCount = 1;
7931             kind = pointerType;
7932             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7933          };
7934          DeclareType(exp.expType.type, false, false);
7935          break;
7936       case opExp:
7937       {
7938          bool assign = false, boolResult = false, boolOps = false;
7939          Type type1 = null, type2 = null;
7940          bool useDestType = false, useSideType = false;
7941          Location oldyylloc = yylloc;
7942          bool useSideUnit = false;
7943
7944          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7945          Type dummy
7946          {
7947             count = 1;
7948             refCount = 1;
7949          };
7950
7951          switch(exp.op.op)
7952          {
7953             // Assignment Operators
7954             case '=':
7955             case MUL_ASSIGN:
7956             case DIV_ASSIGN:
7957             case MOD_ASSIGN:
7958             case ADD_ASSIGN:
7959             case SUB_ASSIGN:
7960             case LEFT_ASSIGN:
7961             case RIGHT_ASSIGN:
7962             case AND_ASSIGN:
7963             case XOR_ASSIGN:
7964             case OR_ASSIGN:
7965                assign = true;
7966                break;
7967             // boolean Operators
7968             case '!':
7969                // Expect boolean operators
7970                //boolOps = true;
7971                //boolResult = true;
7972                break;
7973             case AND_OP:
7974             case OR_OP:
7975                // Expect boolean operands
7976                boolOps = true;
7977                boolResult = true;
7978                break;
7979             // Comparisons
7980             case EQ_OP:
7981             case '<':
7982             case '>':
7983             case LE_OP:
7984             case GE_OP:
7985             case NE_OP:
7986                // Gives boolean result
7987                boolResult = true;
7988                useSideType = true;
7989                break;
7990             case '+':
7991             case '-':
7992                useSideUnit = true;
7993
7994                // Just added these... testing
7995             case '|':
7996             case '&':
7997             case '^':
7998
7999             // DANGER: Verify units
8000             case '/':
8001             case '%':
8002             case '*':
8003
8004                if(exp.op.op != '*' || exp.op.exp1)
8005                {
8006                   useSideType = true;
8007                   useDestType = true;
8008                }
8009                break;
8010
8011             /*// Implement speed etc.
8012             case '*':
8013             case '/':
8014                break;
8015             */
8016          }
8017          if(exp.op.op == '&')
8018          {
8019             // Added this here earlier for Iterator address as key
8020             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8021             {
8022                Identifier id = exp.op.exp2.identifier;
8023                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8024                if(symbol && symbol.isIterator == 2)
8025                {
8026                   exp.type = memberExp;
8027                   exp.member.exp = exp.op.exp2;
8028                   exp.member.member = MkIdentifier("key");
8029                   exp.expType = null;
8030                   exp.op.exp2.expType = symbol.type;
8031                   symbol.type.refCount++;
8032                   ProcessExpressionType(exp);
8033                   FreeType(dummy);
8034                   break;
8035                }
8036                // exp.op.exp2.usage.usageRef = true;
8037             }
8038          }
8039
8040          //dummy.kind = TypeDummy;
8041
8042          if(exp.op.exp1)
8043          {
8044             if(exp.destType && exp.destType.kind == classType &&
8045                exp.destType._class && exp.destType._class.registered && useDestType &&
8046
8047               ((exp.destType._class.registered.type == unitClass && useSideUnit) ||
8048                exp.destType._class.registered.type == enumClass ||
8049                exp.destType._class.registered.type == bitClass
8050                ))
8051
8052               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8053             {
8054                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8055                exp.op.exp1.destType = exp.destType;
8056                if(exp.destType)
8057                   exp.destType.refCount++;
8058             }
8059             else if(!assign)
8060             {
8061                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8062                exp.op.exp1.destType = dummy;
8063                dummy.refCount++;
8064             }
8065
8066             // TESTING THIS HERE...
8067             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8068             ProcessExpressionType(exp.op.exp1);
8069             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8070
8071             if(exp.op.exp1.destType == dummy)
8072             {
8073                FreeType(dummy);
8074                exp.op.exp1.destType = null;
8075             }
8076             type1 = exp.op.exp1.expType;
8077          }
8078
8079          if(exp.op.exp2)
8080          {
8081             char expString[10240];
8082             expString[0] = '\0';
8083             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8084             {
8085                if(exp.op.exp1)
8086                {
8087                   exp.op.exp2.destType = exp.op.exp1.expType;
8088                   if(exp.op.exp1.expType)
8089                      exp.op.exp1.expType.refCount++;
8090                }
8091                else
8092                {
8093                   exp.op.exp2.destType = exp.destType;
8094                   if(exp.destType)
8095                      exp.destType.refCount++;
8096                }
8097
8098                if(type1) type1.refCount++;
8099                exp.expType = type1;
8100             }
8101             else if(assign)
8102             {
8103                if(inCompiler)
8104                   PrintExpression(exp.op.exp2, expString);
8105
8106                if(type1 && type1.kind == pointerType)
8107                {
8108                   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 ||
8109                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8110                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8111                   else if(exp.op.op == '=')
8112                   {
8113                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8114                      exp.op.exp2.destType = type1;
8115                      if(type1)
8116                         type1.refCount++;
8117                   }
8118                }
8119                else
8120                {
8121                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8122                   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/* ||
8123                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8124                   else
8125                   {
8126                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8127                      exp.op.exp2.destType = type1;
8128                      if(type1)
8129                         type1.refCount++;
8130                   }
8131                }
8132                if(type1) type1.refCount++;
8133                exp.expType = type1;
8134             }
8135             else if(exp.destType && exp.destType.kind == classType &&
8136                exp.destType._class && exp.destType._class.registered &&
8137
8138                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) ||
8139                   (exp.destType._class.registered.type == enumClass && useDestType))
8140                   )
8141             {
8142                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8143                exp.op.exp2.destType = exp.destType;
8144                if(exp.destType)
8145                   exp.destType.refCount++;
8146             }
8147             else
8148             {
8149                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8150                exp.op.exp2.destType = dummy;
8151                dummy.refCount++;
8152             }
8153
8154             // TESTING THIS HERE... (DANGEROUS)
8155             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8156                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8157             {
8158                FreeType(exp.op.exp2.destType);
8159                exp.op.exp2.destType = type1;
8160                type1.refCount++;
8161             }
8162             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8163             // Cannot lose the cast on a sizeof
8164             if(exp.op.op == SIZEOF)
8165             {
8166                Expression e = exp.op.exp2;
8167                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8168                {
8169                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8170                   {
8171                      if(e.type == extensionCompoundExp)
8172                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8173                      else
8174                         e = e.list->last;
8175                   }
8176                }
8177                if(e.type == castExp && e.cast.exp)
8178                   e.cast.exp.needCast = true;
8179             }
8180             ProcessExpressionType(exp.op.exp2);
8181             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8182
8183             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8184             {
8185                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)
8186                {
8187                   if(exp.op.op != '=' && type1.type.kind == voidType)
8188                      Compiler_Error($"void *: unknown size\n");
8189                }
8190                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||
8191                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8192                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8193                               exp.op.exp2.expType._class.registered.type == structClass ||
8194                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8195                {
8196                   if(exp.op.op == ADD_ASSIGN)
8197                      Compiler_Error($"cannot add two pointers\n");
8198                }
8199                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8200                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8201                {
8202                   if(exp.op.op == ADD_ASSIGN)
8203                      Compiler_Error($"cannot add two pointers\n");
8204                }
8205                else if(inCompiler)
8206                {
8207                   char type1String[1024];
8208                   char type2String[1024];
8209                   type1String[0] = '\0';
8210                   type2String[0] = '\0';
8211
8212                   PrintType(exp.op.exp2.expType, type1String, false, true);
8213                   PrintType(type1, type2String, false, true);
8214                   ChangeCh(expString, '\n', ' ');
8215                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8216                }
8217             }
8218
8219             if(exp.op.exp2.destType == dummy)
8220             {
8221                FreeType(dummy);
8222                exp.op.exp2.destType = null;
8223             }
8224
8225             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8226             {
8227                type2 = { };
8228                type2.refCount = 1;
8229                CopyTypeInto(type2, exp.op.exp2.expType);
8230                type2.isSigned = true;
8231             }
8232             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8233             {
8234                type2 = { kind = intType };
8235                type2.refCount = 1;
8236                type2.isSigned = true;
8237             }
8238             else
8239             {
8240                type2 = exp.op.exp2.expType;
8241                if(type2) type2.refCount++;
8242             }
8243          }
8244
8245          dummy.kind = voidType;
8246
8247          if(exp.op.op == SIZEOF)
8248          {
8249             exp.expType = Type
8250             {
8251                refCount = 1;
8252                kind = intType;
8253             };
8254             exp.isConstant = true;
8255          }
8256          // Get type of dereferenced pointer
8257          else if(exp.op.op == '*' && !exp.op.exp1)
8258          {
8259             exp.expType = Dereference(type2);
8260             if(type2 && type2.kind == classType)
8261                notByReference = true;
8262          }
8263          else if(exp.op.op == '&' && !exp.op.exp1)
8264             exp.expType = Reference(type2);
8265          else if(!assign)
8266          {
8267             if(boolOps)
8268             {
8269                if(exp.op.exp1)
8270                {
8271                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8272                   exp.op.exp1.destType = MkClassType("bool");
8273                   exp.op.exp1.destType.truth = true;
8274                   if(!exp.op.exp1.expType)
8275                      ProcessExpressionType(exp.op.exp1);
8276                   else
8277                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8278                   FreeType(exp.op.exp1.expType);
8279                   exp.op.exp1.expType = MkClassType("bool");
8280                   exp.op.exp1.expType.truth = true;
8281                }
8282                if(exp.op.exp2)
8283                {
8284                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8285                   exp.op.exp2.destType = MkClassType("bool");
8286                   exp.op.exp2.destType.truth = true;
8287                   if(!exp.op.exp2.expType)
8288                      ProcessExpressionType(exp.op.exp2);
8289                   else
8290                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8291                   FreeType(exp.op.exp2.expType);
8292                   exp.op.exp2.expType = MkClassType("bool");
8293                   exp.op.exp2.expType.truth = true;
8294                }
8295             }
8296             else if(exp.op.exp1 && exp.op.exp2 &&
8297                ((useSideType /*&&
8298                      (useSideUnit ||
8299                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8300                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8301                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8302                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8303             {
8304                if(type1 && type2 &&
8305                   // If either both are class or both are not class
8306                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8307                {
8308                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8309                   exp.op.exp2.destType = type1;
8310                   type1.refCount++;
8311                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8312                   exp.op.exp1.destType = type2;
8313                   type2.refCount++;
8314                   // Warning here for adding Radians + Degrees with no destination type
8315                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8316                      type1._class.registered && type1._class.registered.type == unitClass &&
8317                      type2._class.registered && type2._class.registered.type == unitClass &&
8318                      type1._class.registered != type2._class.registered)
8319                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8320                         type1._class.string, type2._class.string, type1._class.string);
8321
8322                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8323                   {
8324                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8325                      if(argExp)
8326                      {
8327                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8328
8329                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8330                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8331                            exp.op.exp1)));
8332
8333                         ProcessExpressionType(exp.op.exp1);
8334
8335                         if(type2.kind != pointerType)
8336                         {
8337                            ProcessExpressionType(classExp);
8338
8339                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8340                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8341                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8342                                  // noHeadClass
8343                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8344                                     OR_OP,
8345                                  // normalClass
8346                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8347                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8348                                        MkPointer(null, null), null)))),
8349                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8350
8351                            if(!exp.op.exp2.expType)
8352                            {
8353                               if(type2)
8354                                  FreeType(type2);
8355                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8356                               type2.refCount++;
8357                            }
8358
8359                            ProcessExpressionType(exp.op.exp2);
8360                         }
8361                      }
8362                   }
8363
8364                   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)))
8365                   {
8366                      if(type1.kind != classType && type1.type.kind == voidType)
8367                         Compiler_Error($"void *: unknown size\n");
8368                      exp.expType = type1;
8369                      if(type1) type1.refCount++;
8370                   }
8371                   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)))
8372                   {
8373                      if(type2.kind != classType && type2.type.kind == voidType)
8374                         Compiler_Error($"void *: unknown size\n");
8375                      exp.expType = type2;
8376                      if(type2) type2.refCount++;
8377                   }
8378                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8379                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8380                   {
8381                      Compiler_Warning($"different levels of indirection\n");
8382                   }
8383                   else
8384                   {
8385                      bool success = false;
8386                      if(type1.kind == pointerType && type2.kind == pointerType)
8387                      {
8388                         if(exp.op.op == '+')
8389                            Compiler_Error($"cannot add two pointers\n");
8390                         else if(exp.op.op == '-')
8391                         {
8392                            // Pointer Subtraction gives integer
8393                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8394                            {
8395                               exp.expType = Type
8396                               {
8397                                  kind = intType;
8398                                  refCount = 1;
8399                               };
8400                               success = true;
8401
8402                               if(type1.type.kind == templateType)
8403                               {
8404                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8405                                  if(argExp)
8406                                  {
8407                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8408
8409                                     ProcessExpressionType(classExp);
8410
8411                                     exp.type = bracketsExp;
8412                                     exp.list = MkListOne(MkExpOp(
8413                                        MkExpBrackets(MkListOne(MkExpOp(
8414                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8415                                              , exp.op.op,
8416                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8417
8418                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8419
8420                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8421                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8422                                                 // noHeadClass
8423                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8424                                                    OR_OP,
8425                                                 // normalClass
8426                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8427                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8428                                                       MkPointer(null, null), null)))),
8429                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8430
8431
8432                                              ));
8433
8434                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8435                                     FreeType(dummy);
8436                                     return;
8437                                  }
8438                               }
8439                            }
8440                         }
8441                      }
8442
8443                      if(!success && exp.op.exp1.type == constantExp)
8444                      {
8445                         // If first expression is constant, try to match that first
8446                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8447                         {
8448                            if(exp.expType) FreeType(exp.expType);
8449                            exp.expType = exp.op.exp1.destType;
8450                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8451                            success = true;
8452                         }
8453                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8454                         {
8455                            if(exp.expType) FreeType(exp.expType);
8456                            exp.expType = exp.op.exp2.destType;
8457                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8458                            success = true;
8459                         }
8460                      }
8461                      else if(!success)
8462                      {
8463                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8464                         {
8465                            if(exp.expType) FreeType(exp.expType);
8466                            exp.expType = exp.op.exp2.destType;
8467                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8468                            success = true;
8469                         }
8470                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8471                         {
8472                            if(exp.expType) FreeType(exp.expType);
8473                            exp.expType = exp.op.exp1.destType;
8474                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8475                            success = true;
8476                         }
8477                      }
8478                      if(!success)
8479                      {
8480                         char expString1[10240];
8481                         char expString2[10240];
8482                         char type1[1024];
8483                         char type2[1024];
8484                         expString1[0] = '\0';
8485                         expString2[0] = '\0';
8486                         type1[0] = '\0';
8487                         type2[0] = '\0';
8488                         if(inCompiler)
8489                         {
8490                            PrintExpression(exp.op.exp1, expString1);
8491                            ChangeCh(expString1, '\n', ' ');
8492                            PrintExpression(exp.op.exp2, expString2);
8493                            ChangeCh(expString2, '\n', ' ');
8494                            PrintType(exp.op.exp1.expType, type1, false, true);
8495                            PrintType(exp.op.exp2.expType, type2, false, true);
8496                         }
8497
8498                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8499                      }
8500                   }
8501                }
8502                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8503                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8504                {
8505                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8506                   // Convert e.g. / 4 into / 4.0
8507                   exp.op.exp1.destType = type2._class.registered.dataType;
8508                   if(type2._class.registered.dataType)
8509                      type2._class.registered.dataType.refCount++;
8510                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8511                   exp.expType = type2;
8512                   if(type2) type2.refCount++;
8513                }
8514                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8515                {
8516                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8517                   // Convert e.g. / 4 into / 4.0
8518                   exp.op.exp2.destType = type1._class.registered.dataType;
8519                   if(type1._class.registered.dataType)
8520                      type1._class.registered.dataType.refCount++;
8521                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8522                   exp.expType = type1;
8523                   if(type1) type1.refCount++;
8524                }
8525                else if(type1)
8526                {
8527                   bool valid = false;
8528
8529                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8530                   {
8531                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8532
8533                      if(!type1._class.registered.dataType)
8534                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8535                      exp.op.exp2.destType = type1._class.registered.dataType;
8536                      exp.op.exp2.destType.refCount++;
8537
8538                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8539                      if(type2)
8540                         FreeType(type2);
8541                      type2 = exp.op.exp2.destType;
8542                      if(type2) type2.refCount++;
8543
8544                      exp.expType = type2;
8545                      type2.refCount++;
8546                   }
8547
8548                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8549                   {
8550                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8551
8552                      if(!type2._class.registered.dataType)
8553                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8554                      exp.op.exp1.destType = type2._class.registered.dataType;
8555                      exp.op.exp1.destType.refCount++;
8556
8557                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8558                      type1 = exp.op.exp1.destType;
8559                      exp.expType = type1;
8560                      type1.refCount++;
8561                   }
8562
8563                   // TESTING THIS NEW CODE
8564                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8565                   {
8566                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8567                      {
8568                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8569                         {
8570                            if(exp.expType) FreeType(exp.expType);
8571                            exp.expType = exp.op.exp1.expType;
8572                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8573                            valid = true;
8574                         }
8575                      }
8576
8577                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8578                      {
8579                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8580                         {
8581                            if(exp.expType) FreeType(exp.expType);
8582                            exp.expType = exp.op.exp2.expType;
8583                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8584                            valid = true;
8585                         }
8586                      }
8587                   }
8588
8589                   if(!valid)
8590                   {
8591                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8592                      exp.op.exp2.destType = type1;
8593                      type1.refCount++;
8594
8595                      /*
8596                      // Maybe this was meant to be an enum...
8597                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8598                      {
8599                         Type oldType = exp.op.exp2.expType;
8600                         exp.op.exp2.expType = null;
8601                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8602                            FreeType(oldType);
8603                         else
8604                            exp.op.exp2.expType = oldType;
8605                      }
8606                      */
8607
8608                      /*
8609                      // TESTING THIS HERE... LATEST ADDITION
8610                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8611                      {
8612                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8613                         exp.op.exp2.destType = type2._class.registered.dataType;
8614                         if(type2._class.registered.dataType)
8615                            type2._class.registered.dataType.refCount++;
8616                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8617
8618                         //exp.expType = type2._class.registered.dataType; //type2;
8619                         //if(type2) type2.refCount++;
8620                      }
8621
8622                      // TESTING THIS HERE... LATEST ADDITION
8623                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8624                      {
8625                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8626                         exp.op.exp1.destType = type1._class.registered.dataType;
8627                         if(type1._class.registered.dataType)
8628                            type1._class.registered.dataType.refCount++;
8629                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8630                         exp.expType = type1._class.registered.dataType; //type1;
8631                         if(type1) type1.refCount++;
8632                      }
8633                      */
8634
8635                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8636                      {
8637                         if(exp.expType) FreeType(exp.expType);
8638                         exp.expType = exp.op.exp2.destType;
8639                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8640                      }
8641                      else if(type1 && type2)
8642                      {
8643                         char expString1[10240];
8644                         char expString2[10240];
8645                         char type1String[1024];
8646                         char type2String[1024];
8647                         expString1[0] = '\0';
8648                         expString2[0] = '\0';
8649                         type1String[0] = '\0';
8650                         type2String[0] = '\0';
8651                         if(inCompiler)
8652                         {
8653                            PrintExpression(exp.op.exp1, expString1);
8654                            ChangeCh(expString1, '\n', ' ');
8655                            PrintExpression(exp.op.exp2, expString2);
8656                            ChangeCh(expString2, '\n', ' ');
8657                            PrintType(exp.op.exp1.expType, type1String, false, true);
8658                            PrintType(exp.op.exp2.expType, type2String, false, true);
8659                         }
8660
8661                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8662
8663                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8664                         {
8665                            exp.expType = exp.op.exp1.expType;
8666                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8667                         }
8668                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8669                         {
8670                            exp.expType = exp.op.exp2.expType;
8671                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8672                         }
8673                      }
8674                   }
8675                }
8676                else if(type2)
8677                {
8678                   // Maybe this was meant to be an enum...
8679                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8680                   {
8681                      Type oldType = exp.op.exp1.expType;
8682                      exp.op.exp1.expType = null;
8683                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8684                         FreeType(oldType);
8685                      else
8686                         exp.op.exp1.expType = oldType;
8687                   }
8688
8689                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8690                   exp.op.exp1.destType = type2;
8691                   type2.refCount++;
8692                   /*
8693                   // TESTING THIS HERE... LATEST ADDITION
8694                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8695                   {
8696                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8697                      exp.op.exp1.destType = type1._class.registered.dataType;
8698                      if(type1._class.registered.dataType)
8699                         type1._class.registered.dataType.refCount++;
8700                   }
8701
8702                   // TESTING THIS HERE... LATEST ADDITION
8703                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8704                   {
8705                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8706                      exp.op.exp2.destType = type2._class.registered.dataType;
8707                      if(type2._class.registered.dataType)
8708                         type2._class.registered.dataType.refCount++;
8709                   }
8710                   */
8711
8712                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8713                   {
8714                      if(exp.expType) FreeType(exp.expType);
8715                      exp.expType = exp.op.exp1.destType;
8716                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8717                   }
8718                }
8719             }
8720             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8721             {
8722                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8723                {
8724                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8725                   // Convert e.g. / 4 into / 4.0
8726                   exp.op.exp1.destType = type2._class.registered.dataType;
8727                   if(type2._class.registered.dataType)
8728                      type2._class.registered.dataType.refCount++;
8729                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8730                }
8731                if(exp.op.op == '!')
8732                {
8733                   exp.expType = MkClassType("bool");
8734                   exp.expType.truth = true;
8735                }
8736                else
8737                {
8738                   exp.expType = type2;
8739                   if(type2) type2.refCount++;
8740                }
8741             }
8742             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8743             {
8744                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8745                {
8746                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8747                   // Convert e.g. / 4 into / 4.0
8748                   exp.op.exp2.destType = type1._class.registered.dataType;
8749                   if(type1._class.registered.dataType)
8750                      type1._class.registered.dataType.refCount++;
8751                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8752                }
8753                exp.expType = type1;
8754                if(type1) type1.refCount++;
8755             }
8756          }
8757
8758          yylloc = exp.loc;
8759          if(exp.op.exp1 && !exp.op.exp1.expType)
8760          {
8761             char expString[10000];
8762             expString[0] = '\0';
8763             if(inCompiler)
8764             {
8765                PrintExpression(exp.op.exp1, expString);
8766                ChangeCh(expString, '\n', ' ');
8767             }
8768             if(expString[0])
8769                Compiler_Error($"couldn't determine type of %s\n", expString);
8770          }
8771          if(exp.op.exp2 && !exp.op.exp2.expType)
8772          {
8773             char expString[10240];
8774             expString[0] = '\0';
8775             if(inCompiler)
8776             {
8777                PrintExpression(exp.op.exp2, expString);
8778                ChangeCh(expString, '\n', ' ');
8779             }
8780             if(expString[0])
8781                Compiler_Error($"couldn't determine type of %s\n", expString);
8782          }
8783
8784          if(boolResult)
8785          {
8786             FreeType(exp.expType);
8787             exp.expType = MkClassType("bool");
8788             exp.expType.truth = true;
8789          }
8790
8791          if(exp.op.op != SIZEOF)
8792             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8793                (!exp.op.exp2 || exp.op.exp2.isConstant);
8794
8795          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8796          {
8797             DeclareType(exp.op.exp2.expType, false, false);
8798          }
8799
8800          yylloc = oldyylloc;
8801
8802          FreeType(dummy);
8803          if(type2)
8804             FreeType(type2);
8805          break;
8806       }
8807       case bracketsExp:
8808       case extensionExpressionExp:
8809       {
8810          Expression e;
8811          exp.isConstant = true;
8812          for(e = exp.list->first; e; e = e.next)
8813          {
8814             bool inced = false;
8815             if(!e.next)
8816             {
8817                FreeType(e.destType);
8818                e.destType = exp.destType;
8819                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8820             }
8821             ProcessExpressionType(e);
8822             if(inced)
8823                exp.destType.count--;
8824             if(!exp.expType && !e.next)
8825             {
8826                exp.expType = e.expType;
8827                if(e.expType) e.expType.refCount++;
8828             }
8829             if(!e.isConstant)
8830                exp.isConstant = false;
8831          }
8832
8833          // In case a cast became a member...
8834          e = exp.list->first;
8835          if(!e.next && e.type == memberExp)
8836          {
8837             // Preserve prev, next
8838             Expression next = exp.next, prev = exp.prev;
8839
8840
8841             FreeType(exp.expType);
8842             FreeType(exp.destType);
8843             delete exp.list;
8844
8845             *exp = *e;
8846
8847             exp.prev = prev;
8848             exp.next = next;
8849
8850             delete e;
8851
8852             ProcessExpressionType(exp);
8853          }
8854          break;
8855       }
8856       case indexExp:
8857       {
8858          Expression e;
8859          exp.isConstant = true;
8860
8861          ProcessExpressionType(exp.index.exp);
8862          if(!exp.index.exp.isConstant)
8863             exp.isConstant = false;
8864
8865          if(exp.index.exp.expType)
8866          {
8867             Type source = exp.index.exp.expType;
8868             if(source.kind == classType && source._class && source._class.registered)
8869             {
8870                Class _class = source._class.registered;
8871                Class c = _class.templateClass ? _class.templateClass : _class;
8872                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
8873                {
8874                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8875
8876                   if(exp.index.index && exp.index.index->last)
8877                   {
8878                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8879                   }
8880                }
8881             }
8882          }
8883
8884          for(e = exp.index.index->first; e; e = e.next)
8885          {
8886             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8887             {
8888                if(e.destType) FreeType(e.destType);
8889                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8890             }
8891             ProcessExpressionType(e);
8892             if(!e.next)
8893             {
8894                // Check if this type is int
8895             }
8896             if(!e.isConstant)
8897                exp.isConstant = false;
8898          }
8899
8900          if(!exp.expType)
8901             exp.expType = Dereference(exp.index.exp.expType);
8902          if(exp.expType)
8903             DeclareType(exp.expType, false, false);
8904          break;
8905       }
8906       case callExp:
8907       {
8908          Expression e;
8909          Type functionType;
8910          Type methodType = null;
8911          char name[1024];
8912          name[0] = '\0';
8913
8914          if(inCompiler)
8915          {
8916             PrintExpression(exp.call.exp,  name);
8917             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8918             {
8919                //exp.call.exp.expType = null;
8920                PrintExpression(exp.call.exp,  name);
8921             }
8922          }
8923          if(exp.call.exp.type == identifierExp)
8924          {
8925             Expression idExp = exp.call.exp;
8926             Identifier id = idExp.identifier;
8927             if(!strcmp(id.string, "__builtin_frame_address"))
8928             {
8929                exp.expType = ProcessTypeString("void *", true);
8930                if(exp.call.arguments && exp.call.arguments->first)
8931                   ProcessExpressionType(exp.call.arguments->first);
8932                break;
8933             }
8934             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8935             {
8936                exp.expType = ProcessTypeString("int", true);
8937                if(exp.call.arguments && exp.call.arguments->first)
8938                   ProcessExpressionType(exp.call.arguments->first);
8939                break;
8940             }
8941             else if(!strcmp(id.string, "Max") ||
8942                !strcmp(id.string, "Min") ||
8943                !strcmp(id.string, "Sgn") ||
8944                !strcmp(id.string, "Abs"))
8945             {
8946                Expression a = null;
8947                Expression b = null;
8948                Expression tempExp1 = null, tempExp2 = null;
8949                if((!strcmp(id.string, "Max") ||
8950                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8951                {
8952                   a = exp.call.arguments->first;
8953                   b = exp.call.arguments->last;
8954                   tempExp1 = a;
8955                   tempExp2 = b;
8956                }
8957                else if(exp.call.arguments->count == 1)
8958                {
8959                   a = exp.call.arguments->first;
8960                   tempExp1 = a;
8961                }
8962
8963                if(a)
8964                {
8965                   exp.call.arguments->Clear();
8966                   idExp.identifier = null;
8967
8968                   FreeExpContents(exp);
8969
8970                   ProcessExpressionType(a);
8971                   if(b)
8972                      ProcessExpressionType(b);
8973
8974                   exp.type = bracketsExp;
8975                   exp.list = MkList();
8976
8977                   if(a.expType && (!b || b.expType))
8978                   {
8979                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8980                      {
8981                         // Use the simpleStruct name/ids for now...
8982                         if(inCompiler)
8983                         {
8984                            OldList * specs = MkList();
8985                            OldList * decls = MkList();
8986                            Declaration decl;
8987                            char temp1[1024], temp2[1024];
8988
8989                            GetTypeSpecs(a.expType, specs);
8990
8991                            if(a && !a.isConstant && a.type != identifierExp)
8992                            {
8993                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8994                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8995                               tempExp1 = QMkExpId(temp1);
8996                               tempExp1.expType = a.expType;
8997                               if(a.expType)
8998                                  a.expType.refCount++;
8999                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9000                            }
9001                            if(b && !b.isConstant && b.type != identifierExp)
9002                            {
9003                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9004                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9005                               tempExp2 = QMkExpId(temp2);
9006                               tempExp2.expType = b.expType;
9007                               if(b.expType)
9008                                  b.expType.refCount++;
9009                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9010                            }
9011
9012                            decl = MkDeclaration(specs, decls);
9013                            if(!curCompound.compound.declarations)
9014                               curCompound.compound.declarations = MkList();
9015                            curCompound.compound.declarations->Insert(null, decl);
9016                         }
9017                      }
9018                   }
9019
9020                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9021                   {
9022                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9023                      ListAdd(exp.list,
9024                         MkExpCondition(MkExpBrackets(MkListOne(
9025                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9026                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9027                      exp.expType = a.expType;
9028                      if(a.expType)
9029                         a.expType.refCount++;
9030                   }
9031                   else if(!strcmp(id.string, "Abs"))
9032                   {
9033                      ListAdd(exp.list,
9034                         MkExpCondition(MkExpBrackets(MkListOne(
9035                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9036                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9037                      exp.expType = a.expType;
9038                      if(a.expType)
9039                         a.expType.refCount++;
9040                   }
9041                   else if(!strcmp(id.string, "Sgn"))
9042                   {
9043                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9044                      ListAdd(exp.list,
9045                         MkExpCondition(MkExpBrackets(MkListOne(
9046                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9047                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9048                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9049                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9050                      exp.expType = ProcessTypeString("int", false);
9051                   }
9052
9053                   FreeExpression(tempExp1);
9054                   if(tempExp2) FreeExpression(tempExp2);
9055
9056                   FreeIdentifier(id);
9057                   break;
9058                }
9059             }
9060          }
9061
9062          {
9063             Type dummy
9064             {
9065                count = 1;
9066                refCount = 1;
9067             };
9068             if(!exp.call.exp.destType)
9069             {
9070                exp.call.exp.destType = dummy;
9071                dummy.refCount++;
9072             }
9073             ProcessExpressionType(exp.call.exp);
9074             if(exp.call.exp.destType == dummy)
9075             {
9076                FreeType(dummy);
9077                exp.call.exp.destType = null;
9078             }
9079             FreeType(dummy);
9080          }
9081
9082          // Check argument types against parameter types
9083          functionType = exp.call.exp.expType;
9084
9085          if(functionType && functionType.kind == TypeKind::methodType)
9086          {
9087             methodType = functionType;
9088             functionType = methodType.method.dataType;
9089
9090             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9091             // TOCHECK: Instead of doing this here could this be done per param?
9092             if(exp.call.exp.expType.usedClass)
9093             {
9094                char typeString[1024];
9095                typeString[0] = '\0';
9096                {
9097                   Symbol back = functionType.thisClass;
9098                   // Do not output class specifier here (thisclass was added to this)
9099                   functionType.thisClass = null;
9100                   PrintType(functionType, typeString, true, true);
9101                   functionType.thisClass = back;
9102                }
9103                if(strstr(typeString, "thisclass"))
9104                {
9105                   OldList * specs = MkList();
9106                   Declarator decl;
9107                   {
9108                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9109
9110                      decl = SpecDeclFromString(typeString, specs, null);
9111
9112                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9113                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9114                         exp.call.exp.expType.usedClass))
9115                         thisClassParams = false;
9116
9117                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9118                      {
9119                         Class backupThisClass = thisClass;
9120                         thisClass = exp.call.exp.expType.usedClass;
9121                         ProcessDeclarator(decl);
9122                         thisClass = backupThisClass;
9123                      }
9124
9125                      thisClassParams = true;
9126
9127                      functionType = ProcessType(specs, decl);
9128                      functionType.refCount = 0;
9129                      FinishTemplatesContext(context);
9130                   }
9131
9132                   FreeList(specs, FreeSpecifier);
9133                   FreeDeclarator(decl);
9134                 }
9135             }
9136          }
9137          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9138          {
9139             Type type = functionType.type;
9140             if(!functionType.refCount)
9141             {
9142                functionType.type = null;
9143                FreeType(functionType);
9144             }
9145             //methodType = functionType;
9146             functionType = type;
9147          }
9148          if(functionType && functionType.kind != TypeKind::functionType)
9149          {
9150             Compiler_Error($"called object %s is not a function\n", name);
9151          }
9152          else if(functionType)
9153          {
9154             bool emptyParams = false, noParams = false;
9155             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9156             Type type = functionType.params.first;
9157             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9158             int extra = 0;
9159             Location oldyylloc = yylloc;
9160
9161             if(!type) emptyParams = true;
9162
9163             // WORKING ON THIS:
9164             if(functionType.extraParam && e && functionType.thisClass)
9165             {
9166                e.destType = MkClassType(functionType.thisClass.string);
9167                e = e.next;
9168             }
9169
9170             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9171             // Fixed #141 by adding '&& !functionType.extraParam'
9172             if(!functionType.staticMethod && !functionType.extraParam)
9173             {
9174                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9175                   memberExp.member.exp.expType._class)
9176                {
9177                   type = MkClassType(memberExp.member.exp.expType._class.string);
9178                   if(e)
9179                   {
9180                      e.destType = type;
9181                      e = e.next;
9182                      type = functionType.params.first;
9183                   }
9184                   else
9185                      type.refCount = 0;
9186                }
9187                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9188                {
9189                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9190                   type.byReference = functionType.byReference;
9191                   type.typedByReference = functionType.typedByReference;
9192                   if(e)
9193                   {
9194                      // Allow manually passing a class for typed object
9195                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9196                         e = e.next;
9197                      e.destType = type;
9198                      e = e.next;
9199                      type = functionType.params.first;
9200                   }
9201                   else
9202                      type.refCount = 0;
9203                   //extra = 1;
9204                }
9205             }
9206
9207             if(type && type.kind == voidType)
9208             {
9209                noParams = true;
9210                if(!type.refCount) FreeType(type);
9211                type = null;
9212             }
9213
9214             for( ; e; e = e.next)
9215             {
9216                if(!type && !emptyParams)
9217                {
9218                   yylloc = e.loc;
9219                   if(methodType && methodType.methodClass)
9220                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9221                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9222                         noParams ? 0 : functionType.params.count);
9223                   else
9224                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9225                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9226                         noParams ? 0 : functionType.params.count);
9227                   break;
9228                }
9229
9230                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9231                {
9232                   Type templatedType = null;
9233                   Class _class = methodType.usedClass;
9234                   ClassTemplateParameter curParam = null;
9235                   int id = 0;
9236                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9237                   {
9238                      Class sClass;
9239                      for(sClass = _class; sClass; sClass = sClass.base)
9240                      {
9241                         if(sClass.templateClass) sClass = sClass.templateClass;
9242                         id = 0;
9243                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9244                         {
9245                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9246                            {
9247                               Class nextClass;
9248                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9249                               {
9250                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9251                                  id += nextClass.templateParams.count;
9252                               }
9253                               break;
9254                            }
9255                            id++;
9256                         }
9257                         if(curParam) break;
9258                      }
9259                   }
9260                   if(curParam && _class.templateArgs[id].dataTypeString)
9261                   {
9262                      ClassTemplateArgument arg = _class.templateArgs[id];
9263                      {
9264                         Context context = SetupTemplatesContext(_class);
9265
9266                         /*if(!arg.dataType)
9267                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9268                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9269                         FinishTemplatesContext(context);
9270                      }
9271                      e.destType = templatedType;
9272                      if(templatedType)
9273                      {
9274                         templatedType.passAsTemplate = true;
9275                         // templatedType.refCount++;
9276                      }
9277                   }
9278                   else
9279                   {
9280                      e.destType = type;
9281                      if(type) type.refCount++;
9282                   }
9283                }
9284                else
9285                {
9286                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9287                   {
9288                      e.destType = type.prev;
9289                      e.destType.refCount++;
9290                   }
9291                   else
9292                   {
9293                      e.destType = type;
9294                      if(type) type.refCount++;
9295                   }
9296                }
9297                // Don't reach the end for the ellipsis
9298                if(type && type.kind != ellipsisType)
9299                {
9300                   Type next = type.next;
9301                   if(!type.refCount) FreeType(type);
9302                   type = next;
9303                }
9304             }
9305
9306             if(type && type.kind != ellipsisType)
9307             {
9308                if(methodType && methodType.methodClass)
9309                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9310                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9311                      functionType.params.count + extra);
9312                else
9313                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9314                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9315                      functionType.params.count + extra);
9316             }
9317             yylloc = oldyylloc;
9318             if(type && !type.refCount) FreeType(type);
9319          }
9320          else
9321          {
9322             functionType = Type
9323             {
9324                refCount = 0;
9325                kind = TypeKind::functionType;
9326             };
9327
9328             if(exp.call.exp.type == identifierExp)
9329             {
9330                char * string = exp.call.exp.identifier.string;
9331                if(inCompiler)
9332                {
9333                   Symbol symbol;
9334                   Location oldyylloc = yylloc;
9335
9336                   yylloc = exp.call.exp.identifier.loc;
9337                   if(strstr(string, "__builtin_") == string)
9338                   {
9339                      if(exp.destType)
9340                      {
9341                         functionType.returnType = exp.destType;
9342                         exp.destType.refCount++;
9343                      }
9344                   }
9345                   else
9346                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9347                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9348                   globalContext.symbols.Add((BTNode)symbol);
9349                   if(strstr(symbol.string, "::"))
9350                      globalContext.hasNameSpace = true;
9351
9352                   yylloc = oldyylloc;
9353                }
9354             }
9355             else if(exp.call.exp.type == memberExp)
9356             {
9357                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9358                   exp.call.exp.member.member.string);*/
9359             }
9360             else
9361                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9362
9363             if(!functionType.returnType)
9364             {
9365                functionType.returnType = Type
9366                {
9367                   refCount = 1;
9368                   kind = intType;
9369                };
9370             }
9371          }
9372          if(functionType && functionType.kind == TypeKind::functionType)
9373          {
9374             exp.expType = functionType.returnType;
9375
9376             if(functionType.returnType)
9377                functionType.returnType.refCount++;
9378
9379             if(!functionType.refCount)
9380                FreeType(functionType);
9381          }
9382
9383          if(exp.call.arguments)
9384          {
9385             for(e = exp.call.arguments->first; e; e = e.next)
9386             {
9387                Type destType = e.destType;
9388                ProcessExpressionType(e);
9389             }
9390          }
9391          break;
9392       }
9393       case memberExp:
9394       {
9395          Type type;
9396          Location oldyylloc = yylloc;
9397          bool thisPtr;
9398          Expression checkExp = exp.member.exp;
9399          while(checkExp)
9400          {
9401             if(checkExp.type == castExp)
9402                checkExp = checkExp.cast.exp;
9403             else if(checkExp.type == bracketsExp)
9404                checkExp = checkExp.list ? checkExp.list->first : null;
9405             else
9406                break;
9407          }
9408
9409          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9410          exp.thisPtr = thisPtr;
9411
9412          // DOING THIS LATER NOW...
9413          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9414          {
9415             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9416             /* TODO: Name Space Fix ups
9417             if(!exp.member.member.classSym)
9418                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9419             */
9420          }
9421
9422          ProcessExpressionType(exp.member.exp);
9423          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9424             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9425          {
9426             exp.isConstant = false;
9427          }
9428          else
9429             exp.isConstant = exp.member.exp.isConstant;
9430          type = exp.member.exp.expType;
9431
9432          yylloc = exp.loc;
9433
9434          if(type && (type.kind == templateType))
9435          {
9436             Class _class = thisClass ? thisClass : currentClass;
9437             ClassTemplateParameter param = null;
9438             if(_class)
9439             {
9440                for(param = _class.templateParams.first; param; param = param.next)
9441                {
9442                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9443                      break;
9444                }
9445             }
9446             if(param && param.defaultArg.member)
9447             {
9448                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9449                if(argExp)
9450                {
9451                   Expression expMember = exp.member.exp;
9452                   Declarator decl;
9453                   OldList * specs = MkList();
9454                   char thisClassTypeString[1024];
9455
9456                   FreeIdentifier(exp.member.member);
9457
9458                   ProcessExpressionType(argExp);
9459
9460                   {
9461                      char * colon = strstr(param.defaultArg.memberString, "::");
9462                      if(colon)
9463                      {
9464                         char className[1024];
9465                         Class sClass;
9466
9467                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9468                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9469                      }
9470                      else
9471                         strcpy(thisClassTypeString, _class.fullName);
9472                   }
9473
9474                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9475
9476                   exp.expType = ProcessType(specs, decl);
9477                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9478                   {
9479                      Class expClass = exp.expType._class.registered;
9480                      Class cClass = null;
9481                      int c;
9482                      int paramCount = 0;
9483                      int lastParam = -1;
9484
9485                      char templateString[1024];
9486                      ClassTemplateParameter param;
9487                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9488                      for(cClass = expClass; cClass; cClass = cClass.base)
9489                      {
9490                         int p = 0;
9491                         for(param = cClass.templateParams.first; param; param = param.next)
9492                         {
9493                            int id = p;
9494                            Class sClass;
9495                            ClassTemplateArgument arg;
9496                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9497                            arg = expClass.templateArgs[id];
9498
9499                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9500                            {
9501                               ClassTemplateParameter cParam;
9502                               //int p = numParams - sClass.templateParams.count;
9503                               int p = 0;
9504                               Class nextClass;
9505                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9506
9507                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9508                               {
9509                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9510                                  {
9511                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9512                                     {
9513                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9514                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9515                                        break;
9516                                     }
9517                                  }
9518                               }
9519                            }
9520
9521                            {
9522                               char argument[256];
9523                               argument[0] = '\0';
9524                               /*if(arg.name)
9525                               {
9526                                  strcat(argument, arg.name.string);
9527                                  strcat(argument, " = ");
9528                               }*/
9529                               switch(param.type)
9530                               {
9531                                  case expression:
9532                                  {
9533                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9534                                     char expString[1024];
9535                                     OldList * specs = MkList();
9536                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9537                                     Expression exp;
9538                                     char * string = PrintHexUInt64(arg.expression.ui64);
9539                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9540                                     delete string;
9541
9542                                     ProcessExpressionType(exp);
9543                                     ComputeExpression(exp);
9544                                     expString[0] = '\0';
9545                                     PrintExpression(exp, expString);
9546                                     strcat(argument, expString);
9547                                     // delete exp;
9548                                     FreeExpression(exp);
9549                                     break;
9550                                  }
9551                                  case identifier:
9552                                  {
9553                                     strcat(argument, arg.member.name);
9554                                     break;
9555                                  }
9556                                  case TemplateParameterType::type:
9557                                  {
9558                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9559                                     {
9560                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9561                                           strcat(argument, thisClassTypeString);
9562                                        else
9563                                           strcat(argument, arg.dataTypeString);
9564                                     }
9565                                     break;
9566                                  }
9567                               }
9568                               if(argument[0])
9569                               {
9570                                  if(paramCount) strcat(templateString, ", ");
9571                                  if(lastParam != p - 1)
9572                                  {
9573                                     strcat(templateString, param.name);
9574                                     strcat(templateString, " = ");
9575                                  }
9576                                  strcat(templateString, argument);
9577                                  paramCount++;
9578                                  lastParam = p;
9579                               }
9580                               p++;
9581                            }
9582                         }
9583                      }
9584                      {
9585                         int len = strlen(templateString);
9586                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9587                         templateString[len++] = '>';
9588                         templateString[len++] = '\0';
9589                      }
9590                      {
9591                         Context context = SetupTemplatesContext(_class);
9592                         FreeType(exp.expType);
9593                         exp.expType = ProcessTypeString(templateString, false);
9594                         FinishTemplatesContext(context);
9595                      }
9596                   }
9597
9598                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9599                   exp.type = bracketsExp;
9600                   exp.list = MkListOne(MkExpOp(null, '*',
9601                   /*opExp;
9602                   exp.op.op = '*';
9603                   exp.op.exp1 = null;
9604                   exp.op.exp2 = */
9605                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9606                      MkExpBrackets(MkListOne(
9607                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9608                            '+',
9609                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9610                            '+',
9611                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9612
9613                            ));
9614                }
9615             }
9616             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9617                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9618             {
9619                type = ProcessTemplateParameterType(type.templateParameter);
9620             }
9621          }
9622          // TODO: *** This seems to be where we should add method support for all basic types ***
9623          if(type && (type.kind == templateType));
9624          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9625                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9626                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9627                           (type.kind == pointerType && type.type.kind == charType)))
9628          {
9629             Identifier id = exp.member.member;
9630             TypeKind typeKind = type.kind;
9631             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9632             if(typeKind == subClassType && exp.member.exp.type == classExp)
9633             {
9634                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9635                typeKind = classType;
9636             }
9637
9638             if(id)
9639             {
9640                if(typeKind == intType || typeKind == enumType)
9641                   _class = eSystem_FindClass(privateModule, "int");
9642                else if(!_class)
9643                {
9644                   if(type.kind == classType && type._class && type._class.registered)
9645                   {
9646                      _class = type._class.registered;
9647                   }
9648                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9649                   {
9650                      _class = FindClass("char *").registered;
9651                   }
9652                   else if(type.kind == pointerType)
9653                   {
9654                      _class = eSystem_FindClass(privateModule, "uintptr");
9655                      FreeType(exp.expType);
9656                      exp.expType = ProcessTypeString("uintptr", false);
9657                      exp.byReference = true;
9658                   }
9659                   else
9660                   {
9661                      char string[1024] = "";
9662                      Symbol classSym;
9663                      PrintTypeNoConst(type, string, false, true);
9664                      classSym = FindClass(string);
9665                      if(classSym) _class = classSym.registered;
9666                   }
9667                }
9668             }
9669
9670             if(_class && id)
9671             {
9672                /*bool thisPtr =
9673                   (exp.member.exp.type == identifierExp &&
9674                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9675                Property prop = null;
9676                Method method = null;
9677                DataMember member = null;
9678                Property revConvert = null;
9679                ClassProperty classProp = null;
9680
9681                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9682                   exp.member.memberType = propertyMember;
9683
9684                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9685                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9686
9687                if(typeKind != subClassType)
9688                {
9689                   // Prioritize data members over properties for "this"
9690                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9691                   {
9692                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9693                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9694                      {
9695                         prop = eClass_FindProperty(_class, id.string, privateModule);
9696                         if(prop)
9697                            member = null;
9698                      }
9699                      if(!member && !prop)
9700                         prop = eClass_FindProperty(_class, id.string, privateModule);
9701                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9702                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9703                         exp.member.thisPtr = true;
9704                   }
9705                   // Prioritize properties over data members otherwise
9706                   else
9707                   {
9708                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9709                      if(!id.classSym)
9710                      {
9711                         prop = eClass_FindProperty(_class, id.string, null);
9712                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9713                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9714                      }
9715
9716                      if(!prop && !member)
9717                      {
9718                         method = eClass_FindMethod(_class, id.string, null);
9719                         if(!method)
9720                         {
9721                            prop = eClass_FindProperty(_class, id.string, privateModule);
9722                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9723                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9724                         }
9725                      }
9726
9727                      if(member && prop)
9728                      {
9729                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9730                            prop = null;
9731                         else
9732                            member = null;
9733                      }
9734                   }
9735                }
9736                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9737                   method = eClass_FindMethod(_class, id.string, privateModule);
9738                if(!prop && !member && !method)
9739                {
9740                   if(typeKind == subClassType)
9741                   {
9742                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9743                      if(classProp)
9744                      {
9745                         exp.member.memberType = classPropertyMember;
9746                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9747                      }
9748                      else
9749                      {
9750                         // Assume this is a class_data member
9751                         char structName[1024];
9752                         Identifier id = exp.member.member;
9753                         Expression classExp = exp.member.exp;
9754                         type.refCount++;
9755
9756                         FreeType(classExp.expType);
9757                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9758
9759                         strcpy(structName, "__ecereClassData_");
9760                         FullClassNameCat(structName, type._class.string, false);
9761                         exp.type = pointerExp;
9762                         exp.member.member = id;
9763
9764                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9765                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9766                               MkExpBrackets(MkListOne(MkExpOp(
9767                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9768                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9769                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9770                                  )));
9771
9772                         FreeType(type);
9773
9774                         ProcessExpressionType(exp);
9775                         return;
9776                      }
9777                   }
9778                   else
9779                   {
9780                      // Check for reverse conversion
9781                      // (Convert in an instantiation later, so that we can use
9782                      //  deep properties system)
9783                      Symbol classSym = FindClass(id.string);
9784                      if(classSym)
9785                      {
9786                         Class convertClass = classSym.registered;
9787                         if(convertClass)
9788                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9789                      }
9790                   }
9791                }
9792
9793                if(prop)
9794                {
9795                   exp.member.memberType = propertyMember;
9796                   if(!prop.dataType)
9797                      ProcessPropertyType(prop);
9798                   exp.expType = prop.dataType;
9799                   if(prop.dataType) prop.dataType.refCount++;
9800                }
9801                else if(member)
9802                {
9803                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9804                   {
9805                      FreeExpContents(exp);
9806                      exp.type = identifierExp;
9807                      exp.identifier = MkIdentifier("class");
9808                      ProcessExpressionType(exp);
9809                      return;
9810                   }
9811
9812                   exp.member.memberType = dataMember;
9813                   DeclareStruct(_class.fullName, false);
9814                   if(!member.dataType)
9815                   {
9816                      Context context = SetupTemplatesContext(_class);
9817                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9818                      FinishTemplatesContext(context);
9819                   }
9820                   exp.expType = member.dataType;
9821                   if(member.dataType) member.dataType.refCount++;
9822                }
9823                else if(revConvert)
9824                {
9825                   exp.member.memberType = reverseConversionMember;
9826                   exp.expType = MkClassType(revConvert._class.fullName);
9827                }
9828                else if(method)
9829                {
9830                   //if(inCompiler)
9831                   {
9832                      /*if(id._class)
9833                      {
9834                         exp.type = identifierExp;
9835                         exp.identifier = exp.member.member;
9836                      }
9837                      else*/
9838                         exp.member.memberType = methodMember;
9839                   }
9840                   if(!method.dataType)
9841                      ProcessMethodType(method);
9842                   exp.expType = Type
9843                   {
9844                      refCount = 1;
9845                      kind = methodType;
9846                      method = method;
9847                   };
9848
9849                   // Tricky spot here... To use instance versus class virtual table
9850                   // Put it back to what it was... What did we break?
9851
9852                   // Had to put it back for overriding Main of Thread global instance
9853
9854                   //exp.expType.methodClass = _class;
9855                   exp.expType.methodClass = (id && id._class) ? _class : null;
9856
9857                   // Need the actual class used for templated classes
9858                   exp.expType.usedClass = _class;
9859                }
9860                else if(!classProp)
9861                {
9862                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9863                   {
9864                      FreeExpContents(exp);
9865                      exp.type = identifierExp;
9866                      exp.identifier = MkIdentifier("class");
9867                      FreeType(exp.expType);
9868                      exp.expType = MkClassType("ecere::com::Class");
9869                      return;
9870                   }
9871                   yylloc = exp.member.member.loc;
9872                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9873                   if(inCompiler)
9874                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9875                }
9876
9877                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9878                {
9879                   Class tClass;
9880
9881                   tClass = _class;
9882                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9883
9884                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9885                   {
9886                      int id = 0;
9887                      ClassTemplateParameter curParam = null;
9888                      Class sClass;
9889
9890                      for(sClass = tClass; sClass; sClass = sClass.base)
9891                      {
9892                         id = 0;
9893                         if(sClass.templateClass) sClass = sClass.templateClass;
9894                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9895                         {
9896                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9897                            {
9898                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9899                                  id += sClass.templateParams.count;
9900                               break;
9901                            }
9902                            id++;
9903                         }
9904                         if(curParam) break;
9905                      }
9906
9907                      if(curParam && tClass.templateArgs[id].dataTypeString)
9908                      {
9909                         ClassTemplateArgument arg = tClass.templateArgs[id];
9910                         Context context = SetupTemplatesContext(tClass);
9911                         /*if(!arg.dataType)
9912                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9913                         FreeType(exp.expType);
9914                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9915                         if(exp.expType)
9916                         {
9917                            if(exp.expType.kind == thisClassType)
9918                            {
9919                               FreeType(exp.expType);
9920                               exp.expType = ReplaceThisClassType(_class);
9921                            }
9922
9923                            if(tClass.templateClass)
9924                               exp.expType.passAsTemplate = true;
9925                            //exp.expType.refCount++;
9926                            if(!exp.destType)
9927                            {
9928                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9929                               //exp.destType.refCount++;
9930
9931                               if(exp.destType.kind == thisClassType)
9932                               {
9933                                  FreeType(exp.destType);
9934                                  exp.destType = ReplaceThisClassType(_class);
9935                               }
9936                            }
9937                         }
9938                         FinishTemplatesContext(context);
9939                      }
9940                   }
9941                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9942                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9943                   {
9944                      int id = 0;
9945                      ClassTemplateParameter curParam = null;
9946                      Class sClass;
9947
9948                      for(sClass = tClass; sClass; sClass = sClass.base)
9949                      {
9950                         id = 0;
9951                         if(sClass.templateClass) sClass = sClass.templateClass;
9952                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9953                         {
9954                            if(curParam.type == TemplateParameterType::type &&
9955                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9956                            {
9957                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9958                                  id += sClass.templateParams.count;
9959                               break;
9960                            }
9961                            id++;
9962                         }
9963                         if(curParam) break;
9964                      }
9965
9966                      if(curParam)
9967                      {
9968                         ClassTemplateArgument arg = tClass.templateArgs[id];
9969                         Context context = SetupTemplatesContext(tClass);
9970                         Type basicType;
9971                         /*if(!arg.dataType)
9972                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9973
9974                         basicType = ProcessTypeString(arg.dataTypeString, false);
9975                         if(basicType)
9976                         {
9977                            if(basicType.kind == thisClassType)
9978                            {
9979                               FreeType(basicType);
9980                               basicType = ReplaceThisClassType(_class);
9981                            }
9982
9983                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9984                            if(tClass.templateClass)
9985                               basicType.passAsTemplate = true;
9986                            */
9987
9988                            FreeType(exp.expType);
9989
9990                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9991                            //exp.expType.refCount++;
9992                            if(!exp.destType)
9993                            {
9994                               exp.destType = exp.expType;
9995                               exp.destType.refCount++;
9996                            }
9997
9998                            {
9999                               Expression newExp { };
10000                               OldList * specs = MkList();
10001                               Declarator decl;
10002                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10003                               *newExp = *exp;
10004                               if(exp.destType) exp.destType.refCount++;
10005                               if(exp.expType)  exp.expType.refCount++;
10006                               exp.type = castExp;
10007                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10008                               exp.cast.exp = newExp;
10009                               //FreeType(exp.expType);
10010                               //exp.expType = null;
10011                               //ProcessExpressionType(sourceExp);
10012                            }
10013                         }
10014                         FinishTemplatesContext(context);
10015                      }
10016                   }
10017                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10018                   {
10019                      Class expClass = exp.expType._class.registered;
10020                      if(expClass)
10021                      {
10022                         Class cClass = null;
10023                         int c;
10024                         int p = 0;
10025                         int paramCount = 0;
10026                         int lastParam = -1;
10027                         char templateString[1024];
10028                         ClassTemplateParameter param;
10029                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10030                         while(cClass != expClass)
10031                         {
10032                            Class sClass;
10033                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10034                            cClass = sClass;
10035
10036                            for(param = cClass.templateParams.first; param; param = param.next)
10037                            {
10038                               Class cClassCur = null;
10039                               int c;
10040                               int cp = 0;
10041                               ClassTemplateParameter paramCur = null;
10042                               ClassTemplateArgument arg;
10043                               while(cClassCur != tClass && !paramCur)
10044                               {
10045                                  Class sClassCur;
10046                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10047                                  cClassCur = sClassCur;
10048
10049                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10050                                  {
10051                                     if(!strcmp(paramCur.name, param.name))
10052                                     {
10053
10054                                        break;
10055                                     }
10056                                     cp++;
10057                                  }
10058                               }
10059                               if(paramCur && paramCur.type == TemplateParameterType::type)
10060                                  arg = tClass.templateArgs[cp];
10061                               else
10062                                  arg = expClass.templateArgs[p];
10063
10064                               {
10065                                  char argument[256];
10066                                  argument[0] = '\0';
10067                                  /*if(arg.name)
10068                                  {
10069                                     strcat(argument, arg.name.string);
10070                                     strcat(argument, " = ");
10071                                  }*/
10072                                  switch(param.type)
10073                                  {
10074                                     case expression:
10075                                     {
10076                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10077                                        char expString[1024];
10078                                        OldList * specs = MkList();
10079                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10080                                        Expression exp;
10081                                        char * string = PrintHexUInt64(arg.expression.ui64);
10082                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10083                                        delete string;
10084
10085                                        ProcessExpressionType(exp);
10086                                        ComputeExpression(exp);
10087                                        expString[0] = '\0';
10088                                        PrintExpression(exp, expString);
10089                                        strcat(argument, expString);
10090                                        // delete exp;
10091                                        FreeExpression(exp);
10092                                        break;
10093                                     }
10094                                     case identifier:
10095                                     {
10096                                        strcat(argument, arg.member.name);
10097                                        break;
10098                                     }
10099                                     case TemplateParameterType::type:
10100                                     {
10101                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10102                                           strcat(argument, arg.dataTypeString);
10103                                        break;
10104                                     }
10105                                  }
10106                                  if(argument[0])
10107                                  {
10108                                     if(paramCount) strcat(templateString, ", ");
10109                                     if(lastParam != p - 1)
10110                                     {
10111                                        strcat(templateString, param.name);
10112                                        strcat(templateString, " = ");
10113                                     }
10114                                     strcat(templateString, argument);
10115                                     paramCount++;
10116                                     lastParam = p;
10117                                  }
10118                               }
10119                               p++;
10120                            }
10121                         }
10122                         {
10123                            int len = strlen(templateString);
10124                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10125                            templateString[len++] = '>';
10126                            templateString[len++] = '\0';
10127                         }
10128
10129                         FreeType(exp.expType);
10130                         {
10131                            Context context = SetupTemplatesContext(tClass);
10132                            exp.expType = ProcessTypeString(templateString, false);
10133                            FinishTemplatesContext(context);
10134                         }
10135                      }
10136                   }
10137                }
10138             }
10139             else
10140                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10141          }
10142          else if(type && (type.kind == structType || type.kind == unionType))
10143          {
10144             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10145             if(memberType)
10146             {
10147                exp.expType = memberType;
10148                if(memberType)
10149                   memberType.refCount++;
10150             }
10151          }
10152          else
10153          {
10154             char expString[10240];
10155             expString[0] = '\0';
10156             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10157             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10158          }
10159
10160          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10161          {
10162             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10163             {
10164                Identifier id = exp.member.member;
10165                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10166                if(_class)
10167                {
10168                   FreeType(exp.expType);
10169                   exp.expType = ReplaceThisClassType(_class);
10170                }
10171             }
10172          }
10173          yylloc = oldyylloc;
10174          break;
10175       }
10176       // Convert x->y into (*x).y
10177       case pointerExp:
10178       {
10179          Type destType = exp.destType;
10180
10181          // DOING THIS LATER NOW...
10182          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10183          {
10184             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10185             /* TODO: Name Space Fix ups
10186             if(!exp.member.member.classSym)
10187                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10188             */
10189          }
10190
10191          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10192          exp.type = memberExp;
10193          if(destType)
10194             destType.count++;
10195          ProcessExpressionType(exp);
10196          if(destType)
10197             destType.count--;
10198          break;
10199       }
10200       case classSizeExp:
10201       {
10202          //ComputeExpression(exp);
10203
10204          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10205          if(classSym && classSym.registered)
10206          {
10207             if(classSym.registered.type == noHeadClass)
10208             {
10209                char name[1024];
10210                name[0] = '\0';
10211                DeclareStruct(classSym.string, false);
10212                FreeSpecifier(exp._class);
10213                exp.type = typeSizeExp;
10214                FullClassNameCat(name, classSym.string, false);
10215                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10216             }
10217             else
10218             {
10219                if(classSym.registered.fixed)
10220                {
10221                   FreeSpecifier(exp._class);
10222                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10223                   exp.type = constantExp;
10224                }
10225                else
10226                {
10227                   char className[1024];
10228                   strcpy(className, "__ecereClass_");
10229                   FullClassNameCat(className, classSym.string, true);
10230                   MangleClassName(className);
10231
10232                   DeclareClass(classSym, className);
10233
10234                   FreeExpContents(exp);
10235                   exp.type = pointerExp;
10236                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10237                   exp.member.member = MkIdentifier("structSize");
10238                }
10239             }
10240          }
10241
10242          exp.expType = Type
10243          {
10244             refCount = 1;
10245             kind = intType;
10246          };
10247          // exp.isConstant = true;
10248          break;
10249       }
10250       case typeSizeExp:
10251       {
10252          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10253
10254          exp.expType = Type
10255          {
10256             refCount = 1;
10257             kind = intType;
10258          };
10259          exp.isConstant = true;
10260
10261          DeclareType(type, false, false);
10262          FreeType(type);
10263          break;
10264       }
10265       case castExp:
10266       {
10267          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10268          type.count = 1;
10269          FreeType(exp.cast.exp.destType);
10270          exp.cast.exp.destType = type;
10271          type.refCount++;
10272          ProcessExpressionType(exp.cast.exp);
10273          type.count = 0;
10274          exp.expType = type;
10275          //type.refCount++;
10276
10277          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10278          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10279          {
10280             void * prev = exp.prev, * next = exp.next;
10281             Type expType = exp.cast.exp.destType;
10282             Expression castExp = exp.cast.exp;
10283             Type destType = exp.destType;
10284
10285             if(expType) expType.refCount++;
10286
10287             //FreeType(exp.destType);
10288             FreeType(exp.expType);
10289             FreeTypeName(exp.cast.typeName);
10290
10291             *exp = *castExp;
10292             FreeType(exp.expType);
10293             FreeType(exp.destType);
10294
10295             exp.expType = expType;
10296             exp.destType = destType;
10297
10298             delete castExp;
10299
10300             exp.prev = prev;
10301             exp.next = next;
10302
10303          }
10304          else
10305          {
10306             exp.isConstant = exp.cast.exp.isConstant;
10307          }
10308          //FreeType(type);
10309          break;
10310       }
10311       case extensionInitializerExp:
10312       {
10313          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10314          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10315          // ProcessInitializer(exp.initializer.initializer, type);
10316          exp.expType = type;
10317          break;
10318       }
10319       case vaArgExp:
10320       {
10321          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10322          ProcessExpressionType(exp.vaArg.exp);
10323          exp.expType = type;
10324          break;
10325       }
10326       case conditionExp:
10327       {
10328          Expression e;
10329          exp.isConstant = true;
10330
10331          FreeType(exp.cond.cond.destType);
10332          exp.cond.cond.destType = MkClassType("bool");
10333          exp.cond.cond.destType.truth = true;
10334          ProcessExpressionType(exp.cond.cond);
10335          if(!exp.cond.cond.isConstant)
10336             exp.isConstant = false;
10337          for(e = exp.cond.exp->first; e; e = e.next)
10338          {
10339             if(!e.next)
10340             {
10341                FreeType(e.destType);
10342                e.destType = exp.destType;
10343                if(e.destType) e.destType.refCount++;
10344             }
10345             ProcessExpressionType(e);
10346             if(!e.next)
10347             {
10348                exp.expType = e.expType;
10349                if(e.expType) e.expType.refCount++;
10350             }
10351             if(!e.isConstant)
10352                exp.isConstant = false;
10353          }
10354
10355          FreeType(exp.cond.elseExp.destType);
10356          // Added this check if we failed to find an expType
10357          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10358
10359          // Reversed it...
10360          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10361
10362          if(exp.cond.elseExp.destType)
10363             exp.cond.elseExp.destType.refCount++;
10364          ProcessExpressionType(exp.cond.elseExp);
10365
10366          // FIXED THIS: Was done before calling process on elseExp
10367          if(!exp.cond.elseExp.isConstant)
10368             exp.isConstant = false;
10369          break;
10370       }
10371       case extensionCompoundExp:
10372       {
10373          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10374          {
10375             Statement last = exp.compound.compound.statements->last;
10376             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10377             {
10378                ((Expression)last.expressions->last).destType = exp.destType;
10379                if(exp.destType)
10380                   exp.destType.refCount++;
10381             }
10382             ProcessStatement(exp.compound);
10383             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10384             if(exp.expType)
10385                exp.expType.refCount++;
10386          }
10387          break;
10388       }
10389       case classExp:
10390       {
10391          Specifier spec = exp._classExp.specifiers->first;
10392          if(spec && spec.type == nameSpecifier)
10393          {
10394             exp.expType = MkClassType(spec.name);
10395             exp.expType.kind = subClassType;
10396             exp.byReference = true;
10397          }
10398          else
10399          {
10400             exp.expType = MkClassType("ecere::com::Class");
10401             exp.byReference = true;
10402          }
10403          break;
10404       }
10405       case classDataExp:
10406       {
10407          Class _class = thisClass ? thisClass : currentClass;
10408          if(_class)
10409          {
10410             Identifier id = exp.classData.id;
10411             char structName[1024];
10412             Expression classExp;
10413             strcpy(structName, "__ecereClassData_");
10414             FullClassNameCat(structName, _class.fullName, false);
10415             exp.type = pointerExp;
10416             exp.member.member = id;
10417             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10418                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10419             else
10420                classExp = MkExpIdentifier(MkIdentifier("class"));
10421
10422             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10423                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10424                   MkExpBrackets(MkListOne(MkExpOp(
10425                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10426                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10427                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10428                      )));
10429
10430             ProcessExpressionType(exp);
10431             return;
10432          }
10433          break;
10434       }
10435       case arrayExp:
10436       {
10437          Type type = null;
10438          char * typeString = null;
10439          char typeStringBuf[1024];
10440          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10441             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10442          {
10443             Class templateClass = exp.destType._class.registered;
10444             typeString = templateClass.templateArgs[2].dataTypeString;
10445          }
10446          else if(exp.list)
10447          {
10448             // Guess type from expressions in the array
10449             Expression e;
10450             for(e = exp.list->first; e; e = e.next)
10451             {
10452                ProcessExpressionType(e);
10453                if(e.expType)
10454                {
10455                   if(!type) { type = e.expType; type.refCount++; }
10456                   else
10457                   {
10458                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10459                      if(!MatchTypeExpression(e, type, null, false))
10460                      {
10461                         FreeType(type);
10462                         type = e.expType;
10463                         e.expType = null;
10464
10465                         e = exp.list->first;
10466                         ProcessExpressionType(e);
10467                         if(e.expType)
10468                         {
10469                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10470                            if(!MatchTypeExpression(e, type, null, false))
10471                            {
10472                               FreeType(e.expType);
10473                               e.expType = null;
10474                               FreeType(type);
10475                               type = null;
10476                               break;
10477                            }
10478                         }
10479                      }
10480                   }
10481                   if(e.expType)
10482                   {
10483                      FreeType(e.expType);
10484                      e.expType = null;
10485                   }
10486                }
10487             }
10488             if(type)
10489             {
10490                typeStringBuf[0] = '\0';
10491                PrintTypeNoConst(type, typeStringBuf, false, true);
10492                typeString = typeStringBuf;
10493                FreeType(type);
10494                type = null;
10495             }
10496          }
10497          if(typeString)
10498          {
10499             /*
10500             (Container)& (struct BuiltInContainer)
10501             {
10502                ._vTbl = class(BuiltInContainer)._vTbl,
10503                ._class = class(BuiltInContainer),
10504                .refCount = 0,
10505                .data = (int[]){ 1, 7, 3, 4, 5 },
10506                .count = 5,
10507                .type = class(int),
10508             }
10509             */
10510             char templateString[1024];
10511             OldList * initializers = MkList();
10512             OldList * structInitializers = MkList();
10513             OldList * specs = MkList();
10514             Expression expExt;
10515             Declarator decl = SpecDeclFromString(typeString, specs, null);
10516             sprintf(templateString, "Container<%s>", typeString);
10517
10518             if(exp.list)
10519             {
10520                Expression e;
10521                type = ProcessTypeString(typeString, false);
10522                while(e = exp.list->first)
10523                {
10524                   exp.list->Remove(e);
10525                   e.destType = type;
10526                   type.refCount++;
10527                   ProcessExpressionType(e);
10528                   ListAdd(initializers, MkInitializerAssignment(e));
10529                }
10530                FreeType(type);
10531                delete exp.list;
10532             }
10533
10534             DeclareStruct("ecere::com::BuiltInContainer", false);
10535
10536             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10537                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10538             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10539                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10540             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10541                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10542             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10543                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10544                MkInitializerList(initializers))));
10545                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10546             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10547                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10548             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10549                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10550             exp.expType = ProcessTypeString(templateString, false);
10551             exp.type = bracketsExp;
10552             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10553                MkExpOp(null, '&',
10554                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10555                   MkInitializerList(structInitializers)))));
10556             ProcessExpressionType(expExt);
10557          }
10558          else
10559          {
10560             exp.expType = ProcessTypeString("Container", false);
10561             Compiler_Error($"Couldn't determine type of array elements\n");
10562          }
10563          break;
10564       }
10565    }
10566
10567    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10568    {
10569       FreeType(exp.expType);
10570       exp.expType = ReplaceThisClassType(thisClass);
10571    }
10572
10573    // Resolve structures here
10574    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10575    {
10576       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10577       // TODO: Fix members reference...
10578       if(symbol)
10579       {
10580          if(exp.expType.kind != enumType)
10581          {
10582             Type member;
10583             String enumName = CopyString(exp.expType.enumName);
10584
10585             // Fixed a memory leak on self-referencing C structs typedefs
10586             // by instantiating a new type rather than simply copying members
10587             // into exp.expType
10588             FreeType(exp.expType);
10589             exp.expType = Type { };
10590             exp.expType.kind = symbol.type.kind;
10591             exp.expType.refCount++;
10592             exp.expType.enumName = enumName;
10593
10594             exp.expType.members = symbol.type.members;
10595             for(member = symbol.type.members.first; member; member = member.next)
10596                member.refCount++;
10597          }
10598          else
10599          {
10600             NamedLink member;
10601             for(member = symbol.type.members.first; member; member = member.next)
10602             {
10603                NamedLink value { name = CopyString(member.name) };
10604                exp.expType.members.Add(value);
10605             }
10606          }
10607       }
10608    }
10609
10610    yylloc = exp.loc;
10611    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10612    else if(exp.destType && !exp.destType.keepCast)
10613    {
10614       if(!CheckExpressionType(exp, exp.destType, false))
10615       {
10616          if(!exp.destType.count || unresolved)
10617          {
10618             if(!exp.expType)
10619             {
10620                yylloc = exp.loc;
10621                if(exp.destType.kind != ellipsisType)
10622                {
10623                   char type2[1024];
10624                   type2[0] = '\0';
10625                   if(inCompiler)
10626                   {
10627                      char expString[10240];
10628                      expString[0] = '\0';
10629
10630                      PrintType(exp.destType, type2, false, true);
10631
10632                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10633                      if(unresolved)
10634                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10635                      else if(exp.type != dummyExp)
10636                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10637                   }
10638                }
10639                else
10640                {
10641                   char expString[10240] ;
10642                   expString[0] = '\0';
10643                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10644
10645                   if(unresolved)
10646                      Compiler_Error($"unresolved identifier %s\n", expString);
10647                   else if(exp.type != dummyExp)
10648                      Compiler_Error($"couldn't determine type of %s\n", expString);
10649                }
10650             }
10651             else
10652             {
10653                char type1[1024];
10654                char type2[1024];
10655                type1[0] = '\0';
10656                type2[0] = '\0';
10657                if(inCompiler)
10658                {
10659                   PrintType(exp.expType, type1, false, true);
10660                   PrintType(exp.destType, type2, false, true);
10661                }
10662
10663                //CheckExpressionType(exp, exp.destType, false);
10664
10665                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10666                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10667                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10668                else
10669                {
10670                   char expString[10240];
10671                   expString[0] = '\0';
10672                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10673
10674 #ifdef _DEBUG
10675                   CheckExpressionType(exp, exp.destType, false);
10676 #endif
10677                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10678                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10679                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10680
10681                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10682                   FreeType(exp.expType);
10683                   exp.destType.refCount++;
10684                   exp.expType = exp.destType;
10685                }
10686             }
10687          }
10688       }
10689       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10690       {
10691          Expression newExp { };
10692          char typeString[1024];
10693          OldList * specs = MkList();
10694          Declarator decl;
10695
10696          typeString[0] = '\0';
10697
10698          *newExp = *exp;
10699
10700          if(exp.expType)  exp.expType.refCount++;
10701          if(exp.expType)  exp.expType.refCount++;
10702          exp.type = castExp;
10703          newExp.destType = exp.expType;
10704
10705          PrintType(exp.expType, typeString, false, false);
10706          decl = SpecDeclFromString(typeString, specs, null);
10707
10708          exp.cast.typeName = MkTypeName(specs, decl);
10709          exp.cast.exp = newExp;
10710       }
10711    }
10712    else if(unresolved)
10713    {
10714       if(exp.identifier._class && exp.identifier._class.name)
10715          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10716       else if(exp.identifier.string && exp.identifier.string[0])
10717          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10718    }
10719    else if(!exp.expType && exp.type != dummyExp)
10720    {
10721       char expString[10240];
10722       expString[0] = '\0';
10723       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10724       Compiler_Error($"couldn't determine type of %s\n", expString);
10725    }
10726
10727    // Let's try to support any_object & typed_object here:
10728    if(inCompiler)
10729       ApplyAnyObjectLogic(exp);
10730
10731    // Mark nohead classes as by reference, unless we're casting them to an integral type
10732    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10733       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10734          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10735           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10736    {
10737       exp.byReference = true;
10738    }
10739    yylloc = oldyylloc;
10740 }
10741
10742 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10743 {
10744    // THIS CODE WILL FIND NEXT MEMBER...
10745    if(*curMember)
10746    {
10747       *curMember = (*curMember).next;
10748
10749       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10750       {
10751          *curMember = subMemberStack[--(*subMemberStackPos)];
10752          *curMember = (*curMember).next;
10753       }
10754
10755       // SKIP ALL PROPERTIES HERE...
10756       while((*curMember) && (*curMember).isProperty)
10757          *curMember = (*curMember).next;
10758
10759       if(subMemberStackPos)
10760       {
10761          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10762          {
10763             subMemberStack[(*subMemberStackPos)++] = *curMember;
10764
10765             *curMember = (*curMember).members.first;
10766             while(*curMember && (*curMember).isProperty)
10767                *curMember = (*curMember).next;
10768          }
10769       }
10770    }
10771    while(!*curMember)
10772    {
10773       if(!*curMember)
10774       {
10775          if(subMemberStackPos && *subMemberStackPos)
10776          {
10777             *curMember = subMemberStack[--(*subMemberStackPos)];
10778             *curMember = (*curMember).next;
10779          }
10780          else
10781          {
10782             Class lastCurClass = *curClass;
10783
10784             if(*curClass == _class) break;     // REACHED THE END
10785
10786             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10787             *curMember = (*curClass).membersAndProperties.first;
10788          }
10789
10790          while((*curMember) && (*curMember).isProperty)
10791             *curMember = (*curMember).next;
10792          if(subMemberStackPos)
10793          {
10794             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10795             {
10796                subMemberStack[(*subMemberStackPos)++] = *curMember;
10797
10798                *curMember = (*curMember).members.first;
10799                while(*curMember && (*curMember).isProperty)
10800                   *curMember = (*curMember).next;
10801             }
10802          }
10803       }
10804    }
10805 }
10806
10807
10808 static void ProcessInitializer(Initializer init, Type type)
10809 {
10810    switch(init.type)
10811    {
10812       case expInitializer:
10813          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10814          {
10815             // TESTING THIS FOR SHUTTING = 0 WARNING
10816             if(init.exp && !init.exp.destType)
10817             {
10818                FreeType(init.exp.destType);
10819                init.exp.destType = type;
10820                if(type) type.refCount++;
10821             }
10822             if(init.exp)
10823             {
10824                ProcessExpressionType(init.exp);
10825                init.isConstant = init.exp.isConstant;
10826             }
10827             break;
10828          }
10829          else
10830          {
10831             Expression exp = init.exp;
10832             Instantiation inst = exp.instance;
10833             MembersInit members;
10834
10835             init.type = listInitializer;
10836             init.list = MkList();
10837
10838             if(inst.members)
10839             {
10840                for(members = inst.members->first; members; members = members.next)
10841                {
10842                   if(members.type == dataMembersInit)
10843                   {
10844                      MemberInit member;
10845                      for(member = members.dataMembers->first; member; member = member.next)
10846                      {
10847                         ListAdd(init.list, member.initializer);
10848                         member.initializer = null;
10849                      }
10850                   }
10851                   // Discard all MembersInitMethod
10852                }
10853             }
10854             FreeExpression(exp);
10855          }
10856       case listInitializer:
10857       {
10858          Initializer i;
10859          Type initializerType = null;
10860          Class curClass = null;
10861          DataMember curMember = null;
10862          DataMember subMemberStack[256];
10863          int subMemberStackPos = 0;
10864
10865          if(type && type.kind == arrayType)
10866             initializerType = Dereference(type);
10867          else if(type && (type.kind == structType || type.kind == unionType))
10868             initializerType = type.members.first;
10869
10870          for(i = init.list->first; i; i = i.next)
10871          {
10872             if(type && type.kind == classType && type._class && type._class.registered)
10873             {
10874                // 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)
10875                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10876                // TODO: Generate error on initializing a private data member this way from another module...
10877                if(curMember)
10878                {
10879                   if(!curMember.dataType)
10880                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10881                   initializerType = curMember.dataType;
10882                }
10883             }
10884             ProcessInitializer(i, initializerType);
10885             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10886                initializerType = initializerType.next;
10887             if(!i.isConstant)
10888                init.isConstant = false;
10889          }
10890
10891          if(type && type.kind == arrayType)
10892             FreeType(initializerType);
10893
10894          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10895          {
10896             Compiler_Error($"Assigning list initializer to non list\n");
10897          }
10898          break;
10899       }
10900    }
10901 }
10902
10903 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10904 {
10905    switch(spec.type)
10906    {
10907       case baseSpecifier:
10908       {
10909          if(spec.specifier == THISCLASS)
10910          {
10911             if(thisClass)
10912             {
10913                spec.type = nameSpecifier;
10914                spec.name = ReplaceThisClass(thisClass);
10915                spec.symbol = FindClass(spec.name);
10916                ProcessSpecifier(spec, declareStruct);
10917             }
10918          }
10919          break;
10920       }
10921       case nameSpecifier:
10922       {
10923          Symbol symbol = FindType(curContext, spec.name);
10924          if(symbol)
10925             DeclareType(symbol.type, true, true);
10926          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10927             DeclareStruct(spec.name, false);
10928          break;
10929       }
10930       case enumSpecifier:
10931       {
10932          Enumerator e;
10933          if(spec.list)
10934          {
10935             for(e = spec.list->first; e; e = e.next)
10936             {
10937                if(e.exp)
10938                   ProcessExpressionType(e.exp);
10939             }
10940          }
10941          break;
10942       }
10943       case structSpecifier:
10944       case unionSpecifier:
10945       {
10946          if(spec.definitions)
10947          {
10948             ClassDef def;
10949             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10950             //if(symbol)
10951                ProcessClass(spec.definitions, symbol);
10952             /*else
10953             {
10954                for(def = spec.definitions->first; def; def = def.next)
10955                {
10956                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10957                      ProcessDeclaration(def.decl);
10958                }
10959             }*/
10960          }
10961          break;
10962       }
10963       /*
10964       case classSpecifier:
10965       {
10966          Symbol classSym = FindClass(spec.name);
10967          if(classSym && classSym.registered && classSym.registered.type == structClass)
10968             DeclareStruct(spec.name, false);
10969          break;
10970       }
10971       */
10972    }
10973 }
10974
10975
10976 static void ProcessDeclarator(Declarator decl)
10977 {
10978    switch(decl.type)
10979    {
10980       case identifierDeclarator:
10981          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10982          {
10983             FreeSpecifier(decl.identifier._class);
10984             decl.identifier._class = null;
10985          }
10986          break;
10987       case arrayDeclarator:
10988          if(decl.array.exp)
10989             ProcessExpressionType(decl.array.exp);
10990       case structDeclarator:
10991       case bracketsDeclarator:
10992       case functionDeclarator:
10993       case pointerDeclarator:
10994       case extendedDeclarator:
10995       case extendedDeclaratorEnd:
10996          if(decl.declarator)
10997             ProcessDeclarator(decl.declarator);
10998          if(decl.type == functionDeclarator)
10999          {
11000             Identifier id = GetDeclId(decl);
11001             if(id && id._class)
11002             {
11003                TypeName param
11004                {
11005                   qualifiers = MkListOne(id._class);
11006                   declarator = null;
11007                };
11008                if(!decl.function.parameters)
11009                   decl.function.parameters = MkList();
11010                decl.function.parameters->Insert(null, param);
11011                id._class = null;
11012             }
11013             if(decl.function.parameters)
11014             {
11015                TypeName param;
11016
11017                for(param = decl.function.parameters->first; param; param = param.next)
11018                {
11019                   if(param.qualifiers && param.qualifiers->first)
11020                   {
11021                      Specifier spec = param.qualifiers->first;
11022                      if(spec && spec.specifier == TYPED_OBJECT)
11023                      {
11024                         Declarator d = param.declarator;
11025                         TypeName newParam
11026                         {
11027                            qualifiers = MkListOne(MkSpecifier(VOID));
11028                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11029                         };
11030
11031                         FreeList(param.qualifiers, FreeSpecifier);
11032
11033                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11034                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11035
11036                         decl.function.parameters->Insert(param, newParam);
11037                         param = newParam;
11038                      }
11039                      else if(spec && spec.specifier == ANY_OBJECT)
11040                      {
11041                         Declarator d = param.declarator;
11042
11043                         FreeList(param.qualifiers, FreeSpecifier);
11044
11045                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11046                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11047                      }
11048                      else if(spec.specifier == THISCLASS)
11049                      {
11050                         if(thisClass)
11051                         {
11052                            spec.type = nameSpecifier;
11053                            spec.name = ReplaceThisClass(thisClass);
11054                            spec.symbol = FindClass(spec.name);
11055                            ProcessSpecifier(spec, false);
11056                         }
11057                      }
11058                   }
11059
11060                   if(param.declarator)
11061                      ProcessDeclarator(param.declarator);
11062                }
11063             }
11064          }
11065          break;
11066    }
11067 }
11068
11069 static void ProcessDeclaration(Declaration decl)
11070 {
11071    yylloc = decl.loc;
11072    switch(decl.type)
11073    {
11074       case initDeclaration:
11075       {
11076          bool declareStruct = false;
11077          /*
11078          lineNum = decl.pos.line;
11079          column = decl.pos.col;
11080          */
11081
11082          if(decl.declarators)
11083          {
11084             InitDeclarator d;
11085
11086             for(d = decl.declarators->first; d; d = d.next)
11087             {
11088                Type type, subType;
11089                ProcessDeclarator(d.declarator);
11090
11091                type = ProcessType(decl.specifiers, d.declarator);
11092
11093                if(d.initializer)
11094                {
11095                   ProcessInitializer(d.initializer, type);
11096
11097                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11098
11099                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11100                      d.initializer.exp.type == instanceExp)
11101                   {
11102                      if(type.kind == classType && type._class ==
11103                         d.initializer.exp.expType._class)
11104                      {
11105                         Instantiation inst = d.initializer.exp.instance;
11106                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11107
11108                         d.initializer.exp.instance = null;
11109                         if(decl.specifiers)
11110                            FreeList(decl.specifiers, FreeSpecifier);
11111                         FreeList(decl.declarators, FreeInitDeclarator);
11112
11113                         d = null;
11114
11115                         decl.type = instDeclaration;
11116                         decl.inst = inst;
11117                      }
11118                   }
11119                }
11120                for(subType = type; subType;)
11121                {
11122                   if(subType.kind == classType)
11123                   {
11124                      declareStruct = true;
11125                      break;
11126                   }
11127                   else if(subType.kind == pointerType)
11128                      break;
11129                   else if(subType.kind == arrayType)
11130                      subType = subType.arrayType;
11131                   else
11132                      break;
11133                }
11134
11135                FreeType(type);
11136                if(!d) break;
11137             }
11138          }
11139
11140          if(decl.specifiers)
11141          {
11142             Specifier s;
11143             for(s = decl.specifiers->first; s; s = s.next)
11144             {
11145                ProcessSpecifier(s, declareStruct);
11146             }
11147          }
11148          break;
11149       }
11150       case instDeclaration:
11151       {
11152          ProcessInstantiationType(decl.inst);
11153          break;
11154       }
11155       case structDeclaration:
11156       {
11157          Specifier spec;
11158          Declarator d;
11159          bool declareStruct = false;
11160
11161          if(decl.declarators)
11162          {
11163             for(d = decl.declarators->first; d; d = d.next)
11164             {
11165                Type type = ProcessType(decl.specifiers, d.declarator);
11166                Type subType;
11167                ProcessDeclarator(d);
11168                for(subType = type; subType;)
11169                {
11170                   if(subType.kind == classType)
11171                   {
11172                      declareStruct = true;
11173                      break;
11174                   }
11175                   else if(subType.kind == pointerType)
11176                      break;
11177                   else if(subType.kind == arrayType)
11178                      subType = subType.arrayType;
11179                   else
11180                      break;
11181                }
11182                FreeType(type);
11183             }
11184          }
11185          if(decl.specifiers)
11186          {
11187             for(spec = decl.specifiers->first; spec; spec = spec.next)
11188                ProcessSpecifier(spec, declareStruct);
11189          }
11190          break;
11191       }
11192    }
11193 }
11194
11195 static FunctionDefinition curFunction;
11196
11197 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11198 {
11199    char propName[1024], propNameM[1024];
11200    char getName[1024], setName[1024];
11201    OldList * args;
11202
11203    DeclareProperty(prop, setName, getName);
11204
11205    // eInstance_FireWatchers(object, prop);
11206    strcpy(propName, "__ecereProp_");
11207    FullClassNameCat(propName, prop._class.fullName, false);
11208    strcat(propName, "_");
11209    // strcat(propName, prop.name);
11210    FullClassNameCat(propName, prop.name, true);
11211    MangleClassName(propName);
11212
11213    strcpy(propNameM, "__ecerePropM_");
11214    FullClassNameCat(propNameM, prop._class.fullName, false);
11215    strcat(propNameM, "_");
11216    // strcat(propNameM, prop.name);
11217    FullClassNameCat(propNameM, prop.name, true);
11218    MangleClassName(propNameM);
11219
11220    if(prop.isWatchable)
11221    {
11222       args = MkList();
11223       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11224       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11225       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11226
11227       args = MkList();
11228       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11229       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11230       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11231    }
11232
11233
11234    {
11235       args = MkList();
11236       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11237       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11238       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11239
11240       args = MkList();
11241       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11242       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11243       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11244    }
11245
11246    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11247       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11248       curFunction.propSet.fireWatchersDone = true;
11249 }
11250
11251 static void ProcessStatement(Statement stmt)
11252 {
11253    yylloc = stmt.loc;
11254    /*
11255    lineNum = stmt.pos.line;
11256    column = stmt.pos.col;
11257    */
11258    switch(stmt.type)
11259    {
11260       case labeledStmt:
11261          ProcessStatement(stmt.labeled.stmt);
11262          break;
11263       case caseStmt:
11264          // This expression should be constant...
11265          if(stmt.caseStmt.exp)
11266          {
11267             FreeType(stmt.caseStmt.exp.destType);
11268             stmt.caseStmt.exp.destType = curSwitchType;
11269             if(curSwitchType) curSwitchType.refCount++;
11270             ProcessExpressionType(stmt.caseStmt.exp);
11271             ComputeExpression(stmt.caseStmt.exp);
11272          }
11273          if(stmt.caseStmt.stmt)
11274             ProcessStatement(stmt.caseStmt.stmt);
11275          break;
11276       case compoundStmt:
11277       {
11278          if(stmt.compound.context)
11279          {
11280             Declaration decl;
11281             Statement s;
11282
11283             Statement prevCompound = curCompound;
11284             Context prevContext = curContext;
11285
11286             if(!stmt.compound.isSwitch)
11287                curCompound = stmt;
11288             curContext = stmt.compound.context;
11289
11290             if(stmt.compound.declarations)
11291             {
11292                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11293                   ProcessDeclaration(decl);
11294             }
11295             if(stmt.compound.statements)
11296             {
11297                for(s = stmt.compound.statements->first; s; s = s.next)
11298                   ProcessStatement(s);
11299             }
11300
11301             curContext = prevContext;
11302             curCompound = prevCompound;
11303          }
11304          break;
11305       }
11306       case expressionStmt:
11307       {
11308          Expression exp;
11309          if(stmt.expressions)
11310          {
11311             for(exp = stmt.expressions->first; exp; exp = exp.next)
11312                ProcessExpressionType(exp);
11313          }
11314          break;
11315       }
11316       case ifStmt:
11317       {
11318          Expression exp;
11319
11320          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11321          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11322          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11323          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11324          {
11325             ProcessExpressionType(exp);
11326          }
11327          if(stmt.ifStmt.stmt)
11328             ProcessStatement(stmt.ifStmt.stmt);
11329          if(stmt.ifStmt.elseStmt)
11330             ProcessStatement(stmt.ifStmt.elseStmt);
11331          break;
11332       }
11333       case switchStmt:
11334       {
11335          Type oldSwitchType = curSwitchType;
11336          if(stmt.switchStmt.exp)
11337          {
11338             Expression exp;
11339             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11340             {
11341                if(!exp.next)
11342                {
11343                   /*
11344                   Type destType
11345                   {
11346                      kind = intType;
11347                      refCount = 1;
11348                   };
11349                   e.exp.destType = destType;
11350                   */
11351
11352                   ProcessExpressionType(exp);
11353                }
11354                if(!exp.next)
11355                   curSwitchType = exp.expType;
11356             }
11357          }
11358          ProcessStatement(stmt.switchStmt.stmt);
11359          curSwitchType = oldSwitchType;
11360          break;
11361       }
11362       case whileStmt:
11363       {
11364          if(stmt.whileStmt.exp)
11365          {
11366             Expression exp;
11367
11368             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11369             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11370             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11371             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11372             {
11373                ProcessExpressionType(exp);
11374             }
11375          }
11376          if(stmt.whileStmt.stmt)
11377             ProcessStatement(stmt.whileStmt.stmt);
11378          break;
11379       }
11380       case doWhileStmt:
11381       {
11382          if(stmt.doWhile.exp)
11383          {
11384             Expression exp;
11385
11386             if(stmt.doWhile.exp->last)
11387             {
11388                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11389                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11390                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11391             }
11392             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11393             {
11394                ProcessExpressionType(exp);
11395             }
11396          }
11397          if(stmt.doWhile.stmt)
11398             ProcessStatement(stmt.doWhile.stmt);
11399          break;
11400       }
11401       case forStmt:
11402       {
11403          Expression exp;
11404          if(stmt.forStmt.init)
11405             ProcessStatement(stmt.forStmt.init);
11406
11407          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11408          {
11409             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11410             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11411             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11412          }
11413
11414          if(stmt.forStmt.check)
11415             ProcessStatement(stmt.forStmt.check);
11416          if(stmt.forStmt.increment)
11417          {
11418             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11419                ProcessExpressionType(exp);
11420          }
11421
11422          if(stmt.forStmt.stmt)
11423             ProcessStatement(stmt.forStmt.stmt);
11424          break;
11425       }
11426       case forEachStmt:
11427       {
11428          Identifier id = stmt.forEachStmt.id;
11429          OldList * exp = stmt.forEachStmt.exp;
11430          OldList * filter = stmt.forEachStmt.filter;
11431          Statement block = stmt.forEachStmt.stmt;
11432          char iteratorType[1024];
11433          Type source;
11434          Expression e;
11435          bool isBuiltin = exp && exp->last &&
11436             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11437               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11438          Expression arrayExp;
11439          char * typeString = null;
11440          int builtinCount = 0;
11441
11442          for(e = exp ? exp->first : null; e; e = e.next)
11443          {
11444             if(!e.next)
11445             {
11446                FreeType(e.destType);
11447                e.destType = ProcessTypeString("Container", false);
11448             }
11449             if(!isBuiltin || e.next)
11450                ProcessExpressionType(e);
11451          }
11452
11453          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11454          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11455             eClass_IsDerived(source._class.registered, containerClass)))
11456          {
11457             Class _class = source ? source._class.registered : null;
11458             Symbol symbol;
11459             Expression expIt = null;
11460             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11461             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11462             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11463             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11464             stmt.type = compoundStmt;
11465
11466             stmt.compound.context = Context { };
11467             stmt.compound.context.parent = curContext;
11468             curContext = stmt.compound.context;
11469
11470             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11471             {
11472                Class mapClass = eSystem_FindClass(privateModule, "Map");
11473                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11474                isCustomAVLTree = true;
11475                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11476                   isAVLTree = true;
11477                else if(eClass_IsDerived(source._class.registered, mapClass))
11478                   isMap = true;
11479             }
11480             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11481             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11482             {
11483                Class listClass = eSystem_FindClass(privateModule, "List");
11484                isLinkList = true;
11485                isList = eClass_IsDerived(source._class.registered, listClass);
11486             }
11487
11488             if(isArray)
11489             {
11490                Declarator decl;
11491                OldList * specs = MkList();
11492                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11493                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11494                stmt.compound.declarations = MkListOne(
11495                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11496                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11497                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11498                      MkInitializerAssignment(MkExpBrackets(exp))))));
11499             }
11500             else if(isBuiltin)
11501             {
11502                Type type = null;
11503                char typeStringBuf[1024];
11504
11505                // TODO: Merge this code?
11506                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11507                if(((Expression)exp->last).type == castExp)
11508                {
11509                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11510                   if(typeName)
11511                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11512                }
11513
11514                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11515                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11516                   arrayExp.destType._class.registered.templateArgs)
11517                {
11518                   Class templateClass = arrayExp.destType._class.registered;
11519                   typeString = templateClass.templateArgs[2].dataTypeString;
11520                }
11521                else if(arrayExp.list)
11522                {
11523                   // Guess type from expressions in the array
11524                   Expression e;
11525                   for(e = arrayExp.list->first; e; e = e.next)
11526                   {
11527                      ProcessExpressionType(e);
11528                      if(e.expType)
11529                      {
11530                         if(!type) { type = e.expType; type.refCount++; }
11531                         else
11532                         {
11533                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11534                            if(!MatchTypeExpression(e, type, null, false))
11535                            {
11536                               FreeType(type);
11537                               type = e.expType;
11538                               e.expType = null;
11539
11540                               e = arrayExp.list->first;
11541                               ProcessExpressionType(e);
11542                               if(e.expType)
11543                               {
11544                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11545                                  if(!MatchTypeExpression(e, type, null, false))
11546                                  {
11547                                     FreeType(e.expType);
11548                                     e.expType = null;
11549                                     FreeType(type);
11550                                     type = null;
11551                                     break;
11552                                  }
11553                               }
11554                            }
11555                         }
11556                         if(e.expType)
11557                         {
11558                            FreeType(e.expType);
11559                            e.expType = null;
11560                         }
11561                      }
11562                   }
11563                   if(type)
11564                   {
11565                      typeStringBuf[0] = '\0';
11566                      PrintType(type, typeStringBuf, false, true);
11567                      typeString = typeStringBuf;
11568                      FreeType(type);
11569                   }
11570                }
11571                if(typeString)
11572                {
11573                   OldList * initializers = MkList();
11574                   Declarator decl;
11575                   OldList * specs = MkList();
11576                   if(arrayExp.list)
11577                   {
11578                      Expression e;
11579
11580                      builtinCount = arrayExp.list->count;
11581                      type = ProcessTypeString(typeString, false);
11582                      while(e = arrayExp.list->first)
11583                      {
11584                         arrayExp.list->Remove(e);
11585                         e.destType = type;
11586                         type.refCount++;
11587                         ProcessExpressionType(e);
11588                         ListAdd(initializers, MkInitializerAssignment(e));
11589                      }
11590                      FreeType(type);
11591                      delete arrayExp.list;
11592                   }
11593                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11594                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11595                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11596
11597                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11598                      PlugDeclarator(
11599                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11600                         ), MkInitializerList(initializers)))));
11601                   FreeList(exp, FreeExpression);
11602                }
11603                else
11604                {
11605                   arrayExp.expType = ProcessTypeString("Container", false);
11606                   Compiler_Error($"Couldn't determine type of array elements\n");
11607                }
11608
11609                /*
11610                Declarator decl;
11611                OldList * specs = MkList();
11612
11613                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11614                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11615                stmt.compound.declarations = MkListOne(
11616                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11617                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11618                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11619                      MkInitializerAssignment(MkExpBrackets(exp))))));
11620                */
11621             }
11622             else if(isLinkList && !isList)
11623             {
11624                Declarator decl;
11625                OldList * specs = MkList();
11626                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11627                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11628                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11629                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11630                      MkInitializerAssignment(MkExpBrackets(exp))))));
11631             }
11632             /*else if(isCustomAVLTree)
11633             {
11634                Declarator decl;
11635                OldList * specs = MkList();
11636                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11637                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11638                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11639                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11640                      MkInitializerAssignment(MkExpBrackets(exp))))));
11641             }*/
11642             else if(_class.templateArgs)
11643             {
11644                if(isMap)
11645                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11646                else
11647                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11648
11649                stmt.compound.declarations = MkListOne(
11650                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11651                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11652                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11653             }
11654             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11655
11656             if(block)
11657             {
11658                // Reparent sub-contexts in this statement
11659                switch(block.type)
11660                {
11661                   case compoundStmt:
11662                      if(block.compound.context)
11663                         block.compound.context.parent = stmt.compound.context;
11664                      break;
11665                   case ifStmt:
11666                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11667                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11668                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11669                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11670                      break;
11671                   case switchStmt:
11672                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11673                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11674                      break;
11675                   case whileStmt:
11676                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11677                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11678                      break;
11679                   case doWhileStmt:
11680                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11681                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11682                      break;
11683                   case forStmt:
11684                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11685                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11686                      break;
11687                   case forEachStmt:
11688                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11689                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11690                      break;
11691                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11692                   case labeledStmt:
11693                   case caseStmt
11694                   case expressionStmt:
11695                   case gotoStmt:
11696                   case continueStmt:
11697                   case breakStmt
11698                   case returnStmt:
11699                   case asmStmt:
11700                   case badDeclarationStmt:
11701                   case fireWatchersStmt:
11702                   case stopWatchingStmt:
11703                   case watchStmt:
11704                   */
11705                }
11706             }
11707             if(filter)
11708             {
11709                block = MkIfStmt(filter, block, null);
11710             }
11711             if(isArray)
11712             {
11713                stmt.compound.statements = MkListOne(MkForStmt(
11714                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11715                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11716                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11717                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11718                   block));
11719               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11720               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11721               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11722             }
11723             else if(isBuiltin)
11724             {
11725                char count[128];
11726                //OldList * specs = MkList();
11727                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11728
11729                sprintf(count, "%d", builtinCount);
11730
11731                stmt.compound.statements = MkListOne(MkForStmt(
11732                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11733                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11734                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11735                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11736                   block));
11737
11738                /*
11739                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11740                stmt.compound.statements = MkListOne(MkForStmt(
11741                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11742                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11743                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11744                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11745                   block));
11746               */
11747               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11748               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11749               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11750             }
11751             else if(isLinkList && !isList)
11752             {
11753                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11754                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11755                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11756                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11757                {
11758                   stmt.compound.statements = MkListOne(MkForStmt(
11759                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11760                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11761                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11762                      block));
11763                }
11764                else
11765                {
11766                   OldList * specs = MkList();
11767                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11768                   stmt.compound.statements = MkListOne(MkForStmt(
11769                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11770                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11771                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11772                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11773                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11774                      block));
11775                }
11776                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11777                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11778                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11779             }
11780             /*else if(isCustomAVLTree)
11781             {
11782                stmt.compound.statements = MkListOne(MkForStmt(
11783                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11784                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11785                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11786                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11787                   block));
11788
11789                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11790                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11791                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11792             }*/
11793             else
11794             {
11795                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11796                   MkIdentifier("Next")), null)), block));
11797             }
11798             ProcessExpressionType(expIt);
11799             if(stmt.compound.declarations->first)
11800                ProcessDeclaration(stmt.compound.declarations->first);
11801
11802             if(symbol)
11803                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11804
11805             ProcessStatement(stmt);
11806             curContext = stmt.compound.context.parent;
11807             break;
11808          }
11809          else
11810          {
11811             Compiler_Error($"Expression is not a container\n");
11812          }
11813          break;
11814       }
11815       case gotoStmt:
11816          break;
11817       case continueStmt:
11818          break;
11819       case breakStmt:
11820          break;
11821       case returnStmt:
11822       {
11823          Expression exp;
11824          if(stmt.expressions)
11825          {
11826             for(exp = stmt.expressions->first; exp; exp = exp.next)
11827             {
11828                if(!exp.next)
11829                {
11830                   if(curFunction && !curFunction.type)
11831                      curFunction.type = ProcessType(
11832                         curFunction.specifiers, curFunction.declarator);
11833                   FreeType(exp.destType);
11834                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11835                   if(exp.destType) exp.destType.refCount++;
11836                }
11837                ProcessExpressionType(exp);
11838             }
11839          }
11840          break;
11841       }
11842       case badDeclarationStmt:
11843       {
11844          ProcessDeclaration(stmt.decl);
11845          break;
11846       }
11847       case asmStmt:
11848       {
11849          AsmField field;
11850          if(stmt.asmStmt.inputFields)
11851          {
11852             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11853                if(field.expression)
11854                   ProcessExpressionType(field.expression);
11855          }
11856          if(stmt.asmStmt.outputFields)
11857          {
11858             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11859                if(field.expression)
11860                   ProcessExpressionType(field.expression);
11861          }
11862          if(stmt.asmStmt.clobberedFields)
11863          {
11864             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11865             {
11866                if(field.expression)
11867                   ProcessExpressionType(field.expression);
11868             }
11869          }
11870          break;
11871       }
11872       case watchStmt:
11873       {
11874          PropertyWatch propWatch;
11875          OldList * watches = stmt._watch.watches;
11876          Expression object = stmt._watch.object;
11877          Expression watcher = stmt._watch.watcher;
11878          if(watcher)
11879             ProcessExpressionType(watcher);
11880          if(object)
11881             ProcessExpressionType(object);
11882
11883          if(inCompiler)
11884          {
11885             if(watcher || thisClass)
11886             {
11887                External external = curExternal;
11888                Context context = curContext;
11889
11890                stmt.type = expressionStmt;
11891                stmt.expressions = MkList();
11892
11893                curExternal = external.prev;
11894
11895                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11896                {
11897                   ClassFunction func;
11898                   char watcherName[1024];
11899                   Class watcherClass = watcher ?
11900                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11901                   External createdExternal;
11902
11903                   // Create a declaration above
11904                   External externalDecl = MkExternalDeclaration(null);
11905                   ast->Insert(curExternal.prev, externalDecl);
11906
11907                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11908                   if(propWatch.deleteWatch)
11909                      strcat(watcherName, "_delete");
11910                   else
11911                   {
11912                      Identifier propID;
11913                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11914                      {
11915                         strcat(watcherName, "_");
11916                         strcat(watcherName, propID.string);
11917                      }
11918                   }
11919
11920                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11921                   {
11922                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11923                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11924                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11925                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11926                      ProcessClassFunctionBody(func, propWatch.compound);
11927                      propWatch.compound = null;
11928
11929                      //afterExternal = afterExternal ? afterExternal : curExternal;
11930
11931                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11932                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11933                      // TESTING THIS...
11934                      createdExternal.symbol.idCode = external.symbol.idCode;
11935
11936                      curExternal = createdExternal;
11937                      ProcessFunction(createdExternal.function);
11938
11939
11940                      // Create a declaration above
11941                      {
11942                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
11943                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11944                         externalDecl.declaration = decl;
11945                         if(decl.symbol && !decl.symbol.pointerExternal)
11946                            decl.symbol.pointerExternal = externalDecl;
11947                      }
11948
11949                      if(propWatch.deleteWatch)
11950                      {
11951                         OldList * args = MkList();
11952                         ListAdd(args, CopyExpression(object));
11953                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11954                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11955                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11956                      }
11957                      else
11958                      {
11959                         Class _class = object.expType._class.registered;
11960                         Identifier propID;
11961
11962                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11963                         {
11964                            char propName[1024];
11965                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11966                            if(prop)
11967                            {
11968                               char getName[1024], setName[1024];
11969                               OldList * args = MkList();
11970
11971                               DeclareProperty(prop, setName, getName);
11972
11973                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11974                               strcpy(propName, "__ecereProp_");
11975                               FullClassNameCat(propName, prop._class.fullName, false);
11976                               strcat(propName, "_");
11977                               // strcat(propName, prop.name);
11978                               FullClassNameCat(propName, prop.name, true);
11979
11980                               ListAdd(args, CopyExpression(object));
11981                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11982                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11983                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11984
11985                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11986                            }
11987                            else
11988                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11989                         }
11990                      }
11991                   }
11992                   else
11993                      Compiler_Error($"Invalid watched object\n");
11994                }
11995
11996                curExternal = external;
11997                curContext = context;
11998
11999                if(watcher)
12000                   FreeExpression(watcher);
12001                if(object)
12002                   FreeExpression(object);
12003                FreeList(watches, FreePropertyWatch);
12004             }
12005             else
12006                Compiler_Error($"No observer specified and not inside a _class\n");
12007          }
12008          else
12009          {
12010             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12011             {
12012                ProcessStatement(propWatch.compound);
12013             }
12014
12015          }
12016          break;
12017       }
12018       case fireWatchersStmt:
12019       {
12020          OldList * watches = stmt._watch.watches;
12021          Expression object = stmt._watch.object;
12022          Class _class;
12023          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12024          // printf("%X\n", watches);
12025          // printf("%X\n", stmt._watch.watches);
12026          if(object)
12027             ProcessExpressionType(object);
12028
12029          if(inCompiler)
12030          {
12031             _class = object ?
12032                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12033
12034             if(_class)
12035             {
12036                Identifier propID;
12037
12038                stmt.type = expressionStmt;
12039                stmt.expressions = MkList();
12040
12041                // Check if we're inside a property set
12042                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12043                {
12044                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12045                }
12046                else if(!watches)
12047                {
12048                   //Compiler_Error($"No property specified and not inside a property set\n");
12049                }
12050                if(watches)
12051                {
12052                   for(propID = watches->first; propID; propID = propID.next)
12053                   {
12054                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12055                      if(prop)
12056                      {
12057                         CreateFireWatcher(prop, object, stmt);
12058                      }
12059                      else
12060                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12061                   }
12062                }
12063                else
12064                {
12065                   // Fire all properties!
12066                   Property prop;
12067                   Class base;
12068                   for(base = _class; base; base = base.base)
12069                   {
12070                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12071                      {
12072                         if(prop.isProperty && prop.isWatchable)
12073                         {
12074                            CreateFireWatcher(prop, object, stmt);
12075                         }
12076                      }
12077                   }
12078                }
12079
12080                if(object)
12081                   FreeExpression(object);
12082                FreeList(watches, FreeIdentifier);
12083             }
12084             else
12085                Compiler_Error($"Invalid object specified and not inside a class\n");
12086          }
12087          break;
12088       }
12089       case stopWatchingStmt:
12090       {
12091          OldList * watches = stmt._watch.watches;
12092          Expression object = stmt._watch.object;
12093          Expression watcher = stmt._watch.watcher;
12094          Class _class;
12095          if(object)
12096             ProcessExpressionType(object);
12097          if(watcher)
12098             ProcessExpressionType(watcher);
12099          if(inCompiler)
12100          {
12101             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12102
12103             if(watcher || thisClass)
12104             {
12105                if(_class)
12106                {
12107                   Identifier propID;
12108
12109                   stmt.type = expressionStmt;
12110                   stmt.expressions = MkList();
12111
12112                   if(!watches)
12113                   {
12114                      OldList * args;
12115                      // eInstance_StopWatching(object, null, watcher);
12116                      args = MkList();
12117                      ListAdd(args, CopyExpression(object));
12118                      ListAdd(args, MkExpConstant("0"));
12119                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12120                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12121                   }
12122                   else
12123                   {
12124                      for(propID = watches->first; propID; propID = propID.next)
12125                      {
12126                         char propName[1024];
12127                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12128                         if(prop)
12129                         {
12130                            char getName[1024], setName[1024];
12131                            OldList * args = MkList();
12132
12133                            DeclareProperty(prop, setName, getName);
12134
12135                            // eInstance_StopWatching(object, prop, watcher);
12136                            strcpy(propName, "__ecereProp_");
12137                            FullClassNameCat(propName, prop._class.fullName, false);
12138                            strcat(propName, "_");
12139                            // strcat(propName, prop.name);
12140                            FullClassNameCat(propName, prop.name, true);
12141                            MangleClassName(propName);
12142
12143                            ListAdd(args, CopyExpression(object));
12144                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12145                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12146                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12147                         }
12148                         else
12149                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
12150                      }
12151                   }
12152
12153                   if(object)
12154                      FreeExpression(object);
12155                   if(watcher)
12156                      FreeExpression(watcher);
12157                   FreeList(watches, FreeIdentifier);
12158                }
12159                else
12160                   Compiler_Error($"Invalid object specified and not inside a class\n");
12161             }
12162             else
12163                Compiler_Error($"No observer specified and not inside a class\n");
12164          }
12165          break;
12166       }
12167    }
12168 }
12169
12170 static void ProcessFunction(FunctionDefinition function)
12171 {
12172    Identifier id = GetDeclId(function.declarator);
12173    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12174    Type type = symbol ? symbol.type : null;
12175    Class oldThisClass = thisClass;
12176    Context oldTopContext = topContext;
12177
12178    yylloc = function.loc;
12179    // Process thisClass
12180
12181    if(type && type.thisClass)
12182    {
12183       Symbol classSym = type.thisClass;
12184       Class _class = type.thisClass.registered;
12185       char className[1024];
12186       char structName[1024];
12187       Declarator funcDecl;
12188       Symbol thisSymbol;
12189
12190       bool typedObject = false;
12191
12192       if(_class && !_class.base)
12193       {
12194          _class = currentClass;
12195          if(_class && !_class.symbol)
12196             _class.symbol = FindClass(_class.fullName);
12197          classSym = _class ? _class.symbol : null;
12198          typedObject = true;
12199       }
12200
12201       thisClass = _class;
12202
12203       if(inCompiler && _class)
12204       {
12205          if(type.kind == functionType)
12206          {
12207             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12208             {
12209                //TypeName param = symbol.type.params.first;
12210                Type param = symbol.type.params.first;
12211                symbol.type.params.Remove(param);
12212                //FreeTypeName(param);
12213                FreeType(param);
12214             }
12215             if(type.classObjectType != classPointer)
12216             {
12217                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12218                symbol.type.staticMethod = true;
12219                symbol.type.thisClass = null;
12220
12221                // HIGH DANGER: VERIFYING THIS...
12222                symbol.type.extraParam = false;
12223             }
12224          }
12225
12226          strcpy(className, "__ecereClass_");
12227          FullClassNameCat(className, _class.fullName, true);
12228
12229          MangleClassName(className);
12230
12231          structName[0] = 0;
12232          FullClassNameCat(structName, _class.fullName, false);
12233
12234          // [class] this
12235
12236
12237          funcDecl = GetFuncDecl(function.declarator);
12238          if(funcDecl)
12239          {
12240             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12241             {
12242                TypeName param = funcDecl.function.parameters->first;
12243                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12244                {
12245                   funcDecl.function.parameters->Remove(param);
12246                   FreeTypeName(param);
12247                }
12248             }
12249
12250             // DANGER: Watch for this... Check if it's a Conversion?
12251             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12252
12253             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12254             if(!function.propertyNoThis)
12255             {
12256                TypeName thisParam;
12257
12258                if(type.classObjectType != classPointer)
12259                {
12260                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12261                   if(!funcDecl.function.parameters)
12262                      funcDecl.function.parameters = MkList();
12263                   funcDecl.function.parameters->Insert(null, thisParam);
12264                }
12265
12266                if(typedObject)
12267                {
12268                   if(type.classObjectType != classPointer)
12269                   {
12270                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12271                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12272                   }
12273
12274                   thisParam = TypeName
12275                   {
12276                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12277                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12278                   };
12279                   funcDecl.function.parameters->Insert(null, thisParam);
12280                }
12281             }
12282          }
12283
12284          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12285          {
12286             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12287             funcDecl = GetFuncDecl(initDecl.declarator);
12288             if(funcDecl)
12289             {
12290                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12291                {
12292                   TypeName param = funcDecl.function.parameters->first;
12293                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12294                   {
12295                      funcDecl.function.parameters->Remove(param);
12296                      FreeTypeName(param);
12297                   }
12298                }
12299
12300                if(type.classObjectType != classPointer)
12301                {
12302                   // DANGER: Watch for this... Check if it's a Conversion?
12303                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12304                   {
12305                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12306
12307                      if(!funcDecl.function.parameters)
12308                         funcDecl.function.parameters = MkList();
12309                      funcDecl.function.parameters->Insert(null, thisParam);
12310                   }
12311                }
12312             }
12313          }
12314       }
12315
12316       // Add this to the context
12317       if(function.body)
12318       {
12319          if(type.classObjectType != classPointer)
12320          {
12321             thisSymbol = Symbol
12322             {
12323                string = CopyString("this");
12324                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12325             };
12326             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12327
12328             if(typedObject && thisSymbol.type)
12329             {
12330                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12331                thisSymbol.type.byReference = type.byReference;
12332                thisSymbol.type.typedByReference = type.byReference;
12333                /*
12334                thisSymbol = Symbol { string = CopyString("class") };
12335                function.body.compound.context.symbols.Add(thisSymbol);
12336                */
12337             }
12338          }
12339       }
12340
12341       // Pointer to class data
12342
12343       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12344       {
12345          DataMember member = null;
12346          {
12347             Class base;
12348             for(base = _class; base && base.type != systemClass; base = base.next)
12349             {
12350                for(member = base.membersAndProperties.first; member; member = member.next)
12351                   if(!member.isProperty)
12352                      break;
12353                if(member)
12354                   break;
12355             }
12356          }
12357          for(member = _class.membersAndProperties.first; member; member = member.next)
12358             if(!member.isProperty)
12359                break;
12360          if(member)
12361          {
12362             char pointerName[1024];
12363
12364             Declaration decl;
12365             Initializer initializer;
12366             Expression exp, bytePtr;
12367
12368             strcpy(pointerName, "__ecerePointer_");
12369             FullClassNameCat(pointerName, _class.fullName, false);
12370             {
12371                char className[1024];
12372                strcpy(className, "__ecereClass_");
12373                FullClassNameCat(className, classSym.string, true);
12374                MangleClassName(className);
12375
12376                // Testing This
12377                DeclareClass(classSym, className);
12378             }
12379
12380             // ((byte *) this)
12381             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12382
12383             if(_class.fixed)
12384             {
12385                char string[256];
12386                sprintf(string, "%d", _class.offset);
12387                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12388             }
12389             else
12390             {
12391                // ([bytePtr] + [className]->offset)
12392                exp = QBrackets(MkExpOp(bytePtr, '+',
12393                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12394             }
12395
12396             // (this ? [exp] : 0)
12397             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12398             exp.expType = Type
12399             {
12400                refCount = 1;
12401                kind = pointerType;
12402                type = Type { refCount = 1, kind = voidType };
12403             };
12404
12405             if(function.body)
12406             {
12407                yylloc = function.body.loc;
12408                // ([structName] *) [exp]
12409                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12410                initializer = MkInitializerAssignment(
12411                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12412
12413                // [structName] * [pointerName] = [initializer];
12414                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12415
12416                {
12417                   Context prevContext = curContext;
12418                   curContext = function.body.compound.context;
12419
12420                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12421                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12422
12423                   curContext = prevContext;
12424                }
12425
12426                // WHY?
12427                decl.symbol = null;
12428
12429                if(!function.body.compound.declarations)
12430                   function.body.compound.declarations = MkList();
12431                function.body.compound.declarations->Insert(null, decl);
12432             }
12433          }
12434       }
12435
12436
12437       // Loop through the function and replace undeclared identifiers
12438       // which are a member of the class (methods, properties or data)
12439       // by "this.[member]"
12440    }
12441    else
12442       thisClass = null;
12443
12444    if(id)
12445    {
12446       FreeSpecifier(id._class);
12447       id._class = null;
12448
12449       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12450       {
12451          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12452          id = GetDeclId(initDecl.declarator);
12453
12454          FreeSpecifier(id._class);
12455          id._class = null;
12456       }
12457    }
12458    if(function.body)
12459       topContext = function.body.compound.context;
12460    {
12461       FunctionDefinition oldFunction = curFunction;
12462       curFunction = function;
12463       if(function.body)
12464          ProcessStatement(function.body);
12465
12466       // If this is a property set and no firewatchers has been done yet, add one here
12467       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12468       {
12469          Statement prevCompound = curCompound;
12470          Context prevContext = curContext;
12471
12472          Statement fireWatchers = MkFireWatchersStmt(null, null);
12473          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12474          ListAdd(function.body.compound.statements, fireWatchers);
12475
12476          curCompound = function.body;
12477          curContext = function.body.compound.context;
12478
12479          ProcessStatement(fireWatchers);
12480
12481          curContext = prevContext;
12482          curCompound = prevCompound;
12483
12484       }
12485
12486       curFunction = oldFunction;
12487    }
12488
12489    if(function.declarator)
12490    {
12491       ProcessDeclarator(function.declarator);
12492    }
12493
12494    topContext = oldTopContext;
12495    thisClass = oldThisClass;
12496 }
12497
12498 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12499 static void ProcessClass(OldList definitions, Symbol symbol)
12500 {
12501    ClassDef def;
12502    External external = curExternal;
12503    Class regClass = symbol ? symbol.registered : null;
12504
12505    // Process all functions
12506    for(def = definitions.first; def; def = def.next)
12507    {
12508       if(def.type == functionClassDef)
12509       {
12510          if(def.function.declarator)
12511             curExternal = def.function.declarator.symbol.pointerExternal;
12512          else
12513             curExternal = external;
12514
12515          ProcessFunction((FunctionDefinition)def.function);
12516       }
12517       else if(def.type == declarationClassDef)
12518       {
12519          if(def.decl.type == instDeclaration)
12520          {
12521             thisClass = regClass;
12522             ProcessInstantiationType(def.decl.inst);
12523             thisClass = null;
12524          }
12525          // Testing this
12526          else
12527          {
12528             Class backThisClass = thisClass;
12529             if(regClass) thisClass = regClass;
12530             ProcessDeclaration(def.decl);
12531             thisClass = backThisClass;
12532          }
12533       }
12534       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12535       {
12536          MemberInit defProperty;
12537
12538          // Add this to the context
12539          Symbol thisSymbol = Symbol
12540          {
12541             string = CopyString("this");
12542             type = regClass ? MkClassType(regClass.fullName) : null;
12543          };
12544          globalContext.symbols.Add((BTNode)thisSymbol);
12545
12546          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12547          {
12548             thisClass = regClass;
12549             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12550             thisClass = null;
12551          }
12552
12553          globalContext.symbols.Remove((BTNode)thisSymbol);
12554          FreeSymbol(thisSymbol);
12555       }
12556       else if(def.type == propertyClassDef && def.propertyDef)
12557       {
12558          PropertyDef prop = def.propertyDef;
12559
12560          // Add this to the context
12561          /*
12562          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12563          globalContext.symbols.Add(thisSymbol);
12564          */
12565
12566          thisClass = regClass;
12567          if(prop.setStmt)
12568          {
12569             if(regClass)
12570             {
12571                Symbol thisSymbol
12572                {
12573                   string = CopyString("this");
12574                   type = MkClassType(regClass.fullName);
12575                };
12576                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12577             }
12578
12579             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12580             ProcessStatement(prop.setStmt);
12581          }
12582          if(prop.getStmt)
12583          {
12584             if(regClass)
12585             {
12586                Symbol thisSymbol
12587                {
12588                   string = CopyString("this");
12589                   type = MkClassType(regClass.fullName);
12590                };
12591                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12592             }
12593
12594             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12595             ProcessStatement(prop.getStmt);
12596          }
12597          if(prop.issetStmt)
12598          {
12599             if(regClass)
12600             {
12601                Symbol thisSymbol
12602                {
12603                   string = CopyString("this");
12604                   type = MkClassType(regClass.fullName);
12605                };
12606                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12607             }
12608
12609             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12610             ProcessStatement(prop.issetStmt);
12611          }
12612
12613          thisClass = null;
12614
12615          /*
12616          globalContext.symbols.Remove(thisSymbol);
12617          FreeSymbol(thisSymbol);
12618          */
12619       }
12620       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12621       {
12622          PropertyWatch propertyWatch = def.propertyWatch;
12623
12624          thisClass = regClass;
12625          if(propertyWatch.compound)
12626          {
12627             Symbol thisSymbol
12628             {
12629                string = CopyString("this");
12630                type = regClass ? MkClassType(regClass.fullName) : null;
12631             };
12632
12633             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12634
12635             curExternal = null;
12636             ProcessStatement(propertyWatch.compound);
12637          }
12638          thisClass = null;
12639       }
12640    }
12641 }
12642
12643 void DeclareFunctionUtil(String s)
12644 {
12645    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12646    if(function)
12647    {
12648       char name[1024];
12649       name[0] = 0;
12650       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12651          strcpy(name, "__ecereFunction_");
12652       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12653       DeclareFunction(function, name);
12654    }
12655 }
12656
12657 void ComputeDataTypes()
12658 {
12659    External external;
12660    External temp { };
12661    External after = null;
12662
12663    currentClass = null;
12664
12665    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12666
12667    for(external = ast->first; external; external = external.next)
12668    {
12669       if(external.type == declarationExternal)
12670       {
12671          Declaration decl = external.declaration;
12672          if(decl)
12673          {
12674             OldList * decls = decl.declarators;
12675             if(decls)
12676             {
12677                InitDeclarator initDecl = decls->first;
12678                if(initDecl)
12679                {
12680                   Declarator declarator = initDecl.declarator;
12681                   if(declarator && declarator.type == identifierDeclarator)
12682                   {
12683                      Identifier id = declarator.identifier;
12684                      if(id && id.string)
12685                      {
12686                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12687                         {
12688                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12689                            after = external;
12690                         }
12691                      }
12692                   }
12693                }
12694             }
12695          }
12696        }
12697    }
12698
12699    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12700    ast->Insert(after, temp);
12701    curExternal = temp;
12702
12703    DeclareFunctionUtil("eSystem_New");
12704    DeclareFunctionUtil("eSystem_New0");
12705    DeclareFunctionUtil("eSystem_Renew");
12706    DeclareFunctionUtil("eSystem_Renew0");
12707    DeclareFunctionUtil("eSystem_Delete");
12708    DeclareFunctionUtil("eClass_GetProperty");
12709    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12710
12711    DeclareStruct("ecere::com::Class", false);
12712    DeclareStruct("ecere::com::Instance", false);
12713    DeclareStruct("ecere::com::Property", false);
12714    DeclareStruct("ecere::com::DataMember", false);
12715    DeclareStruct("ecere::com::Method", false);
12716    DeclareStruct("ecere::com::SerialBuffer", false);
12717    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12718
12719    ast->Remove(temp);
12720
12721    for(external = ast->first; external; external = external.next)
12722    {
12723       afterExternal = curExternal = external;
12724       if(external.type == functionExternal)
12725       {
12726          currentClass = external.function._class;
12727          ProcessFunction(external.function);
12728       }
12729       // There shouldn't be any _class member access here anyways...
12730       else if(external.type == declarationExternal)
12731       {
12732          currentClass = null;
12733          ProcessDeclaration(external.declaration);
12734       }
12735       else if(external.type == classExternal)
12736       {
12737          ClassDefinition _class = external._class;
12738          currentClass = external.symbol.registered;
12739          if(_class.definitions)
12740          {
12741             ProcessClass(_class.definitions, _class.symbol);
12742          }
12743          if(inCompiler)
12744          {
12745             // Free class data...
12746             ast->Remove(external);
12747             delete external;
12748          }
12749       }
12750       else if(external.type == nameSpaceExternal)
12751       {
12752          thisNameSpace = external.id.string;
12753       }
12754    }
12755    currentClass = null;
12756    thisNameSpace = null;
12757
12758    delete temp.symbol;
12759    delete temp;
12760 }