compiler/libec: Fixed computation of expressions requiring promotions
[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    sprintf(temp, "%.16ff", result);
301    return CopyString(temp);
302 }
303
304 public char * PrintDouble(double result)
305 {
306    char temp[350];
307    sprintf(temp, "%.16f", result);
308    return CopyString(temp);
309 }
310
311 ////////////////////////////////////////////////////////////////////////
312 ////////////////////////////////////////////////////////////////////////
313
314 //public Operand GetOperand(Expression exp);
315
316 #define GETVALUE(name, t) \
317    public bool GetOp##name(Operand op2, t * value2) \
318    {                                                        \
319       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
320       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
321       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
322       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
323       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
324       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
325       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
326       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
327       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
328       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
329       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
330       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
331       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
332       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
333       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
334       else                                                                          \
335          return false;                                                              \
336       return true;                                                                  \
337    } \
338    public bool Get##name(Expression exp, t * value2) \
339    {                                                        \
340       Operand op2 = GetOperand(exp);                        \
341       return GetOp##name(op2, value2); \
342    }
343
344 // To help the deubugger currently not preprocessing...
345 #define HELP(x) x
346
347 GETVALUE(Int, HELP(int));
348 GETVALUE(UInt, HELP(unsigned int));
349 GETVALUE(Int64, HELP(int64));
350 GETVALUE(UInt64, HELP(uint64));
351 GETVALUE(IntPtr, HELP(intptr));
352 GETVALUE(UIntPtr, HELP(uintptr));
353 GETVALUE(IntSize, HELP(intsize));
354 GETVALUE(UIntSize, HELP(uintsize));
355 GETVALUE(Short, HELP(short));
356 GETVALUE(UShort, HELP(unsigned short));
357 GETVALUE(Char, HELP(char));
358 GETVALUE(UChar, HELP(unsigned char));
359 GETVALUE(Float, HELP(float));
360 GETVALUE(Double, HELP(double));
361
362 void ComputeExpression(Expression exp);
363
364 void ComputeClassMembers(Class _class, bool isMember)
365 {
366    DataMember member = isMember ? (DataMember) _class : null;
367    Context context = isMember ? null : SetupTemplatesContext(_class);
368    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
369                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
370    {
371       int c;
372       int unionMemberOffset = 0;
373       int bitFields = 0;
374
375       /*
376       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
377          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
378       */
379
380       if(member)
381       {
382          member.memberOffset = 0;
383          if(targetBits < sizeof(void *) * 8)
384             member.structAlignment = 0;
385       }
386       else if(targetBits < sizeof(void *) * 8)
387          _class.structAlignment = 0;
388
389       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
390       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
391          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
392
393       if(!member && _class.destructionWatchOffset)
394          _class.memberOffset += sizeof(OldList);
395
396       // To avoid reentrancy...
397       //_class.structSize = -1;
398
399       {
400          DataMember dataMember;
401          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
402          {
403             if(!dataMember.isProperty)
404             {
405                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
406                {
407                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
408                   /*if(!dataMember.dataType)
409                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
410                      */
411                }
412             }
413          }
414       }
415
416       {
417          DataMember dataMember;
418          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
419          {
420             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
421             {
422                if(!isMember && _class.type == bitClass && dataMember.dataType)
423                {
424                   BitMember bitMember = (BitMember) dataMember;
425                   uint64 mask = 0;
426                   int d;
427
428                   ComputeTypeSize(dataMember.dataType);
429
430                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
431                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
432
433                   _class.memberOffset = bitMember.pos + bitMember.size;
434                   for(d = 0; d<bitMember.size; d++)
435                   {
436                      if(d)
437                         mask <<= 1;
438                      mask |= 1;
439                   }
440                   bitMember.mask = mask << bitMember.pos;
441                }
442                else if(dataMember.type == normalMember && dataMember.dataType)
443                {
444                   int size;
445                   int alignment = 0;
446
447                   // Prevent infinite recursion
448                   if(dataMember.dataType.kind != classType ||
449                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
450                      _class.type != structClass)))
451                      ComputeTypeSize(dataMember.dataType);
452
453                   if(dataMember.dataType.bitFieldCount)
454                   {
455                      bitFields += dataMember.dataType.bitFieldCount;
456                      size = 0;
457                   }
458                   else
459                   {
460                      if(bitFields)
461                      {
462                         int size = (bitFields + 7) / 8;
463
464                         if(isMember)
465                         {
466                            // TESTING THIS PADDING CODE
467                            if(alignment)
468                            {
469                               member.structAlignment = Max(member.structAlignment, alignment);
470
471                               if(member.memberOffset % alignment)
472                                  member.memberOffset += alignment - (member.memberOffset % alignment);
473                            }
474
475                            dataMember.offset = member.memberOffset;
476                            if(member.type == unionMember)
477                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
478                            else
479                            {
480                               member.memberOffset += size;
481                            }
482                         }
483                         else
484                         {
485                            // TESTING THIS PADDING CODE
486                            if(alignment)
487                            {
488                               _class.structAlignment = Max(_class.structAlignment, alignment);
489
490                               if(_class.memberOffset % alignment)
491                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
492                            }
493
494                            dataMember.offset = _class.memberOffset;
495                            _class.memberOffset += size;
496                         }
497                         bitFields = 0;
498                      }
499                      size = dataMember.dataType.size;
500                      alignment = dataMember.dataType.alignment;
501                   }
502
503                   if(isMember)
504                   {
505                      // TESTING THIS PADDING CODE
506                      if(alignment)
507                      {
508                         member.structAlignment = Max(member.structAlignment, alignment);
509
510                         if(member.memberOffset % alignment)
511                            member.memberOffset += alignment - (member.memberOffset % alignment);
512                      }
513
514                      dataMember.offset = member.memberOffset;
515                      if(member.type == unionMember)
516                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
517                      else
518                      {
519                         member.memberOffset += size;
520                      }
521                   }
522                   else
523                   {
524                      // TESTING THIS PADDING CODE
525                      if(alignment)
526                      {
527                         _class.structAlignment = Max(_class.structAlignment, alignment);
528
529                         if(_class.memberOffset % alignment)
530                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
531                      }
532
533                      dataMember.offset = _class.memberOffset;
534                      _class.memberOffset += size;
535                   }
536                }
537                else
538                {
539                   int alignment;
540
541                   ComputeClassMembers((Class)dataMember, true);
542                   alignment = dataMember.structAlignment;
543
544                   if(isMember)
545                   {
546                      if(alignment)
547                      {
548                         if(member.memberOffset % alignment)
549                            member.memberOffset += alignment - (member.memberOffset % alignment);
550
551                         member.structAlignment = Max(member.structAlignment, alignment);
552                      }
553                      dataMember.offset = member.memberOffset;
554                      if(member.type == unionMember)
555                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
556                      else
557                         member.memberOffset += dataMember.memberOffset;
558                   }
559                   else
560                   {
561                      if(alignment)
562                      {
563                         if(_class.memberOffset % alignment)
564                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
565                         _class.structAlignment = Max(_class.structAlignment, alignment);
566                      }
567                      dataMember.offset = _class.memberOffset;
568                      _class.memberOffset += dataMember.memberOffset;
569                   }
570                }
571             }
572          }
573          if(bitFields)
574          {
575             int alignment = 0;
576             int size = (bitFields + 7) / 8;
577
578             if(isMember)
579             {
580                // TESTING THIS PADDING CODE
581                if(alignment)
582                {
583                   member.structAlignment = Max(member.structAlignment, alignment);
584
585                   if(member.memberOffset % alignment)
586                      member.memberOffset += alignment - (member.memberOffset % alignment);
587                }
588
589                if(member.type == unionMember)
590                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
591                else
592                {
593                   member.memberOffset += size;
594                }
595             }
596             else
597             {
598                // TESTING THIS PADDING CODE
599                if(alignment)
600                {
601                   _class.structAlignment = Max(_class.structAlignment, alignment);
602
603                   if(_class.memberOffset % alignment)
604                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
605                }
606                _class.memberOffset += size;
607             }
608             bitFields = 0;
609          }
610       }
611       if(member && member.type == unionMember)
612       {
613          member.memberOffset = unionMemberOffset;
614       }
615
616       if(!isMember)
617       {
618          /*if(_class.type == structClass)
619             _class.size = _class.memberOffset;
620          else
621          */
622
623          if(_class.type != bitClass)
624          {
625             int extra = 0;
626             if(_class.structAlignment)
627             {
628                if(_class.memberOffset % _class.structAlignment)
629                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
630             }
631             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
632             if(!member)
633             {
634                Property prop;
635                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
636                {
637                   if(prop.isProperty && prop.isWatchable)
638                   {
639                      prop.watcherOffset = _class.structSize;
640                      _class.structSize += sizeof(OldList);
641                   }
642                }
643             }
644
645             // Fix Derivatives
646             {
647                OldLink derivative;
648                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
649                {
650                   Class deriv = derivative.data;
651
652                   if(deriv.computeSize)
653                   {
654                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
655                      deriv.offset = /*_class.offset + */_class.structSize;
656                      deriv.memberOffset = 0;
657                      // ----------------------
658
659                      deriv.structSize = deriv.offset;
660
661                      ComputeClassMembers(deriv, false);
662                   }
663                }
664             }
665          }
666       }
667    }
668    if(context)
669       FinishTemplatesContext(context);
670 }
671
672 public void ComputeModuleClasses(Module module)
673 {
674    Class _class;
675    OldLink subModule;
676
677    for(subModule = module.modules.first; subModule; subModule = subModule.next)
678       ComputeModuleClasses(subModule.data);
679    for(_class = module.classes.first; _class; _class = _class.next)
680       ComputeClassMembers(_class, false);
681 }
682
683
684 public int ComputeTypeSize(Type type)
685 {
686    uint size = type ? type.size : 0;
687    if(!size && type && !type.computing)
688    {
689       type.computing = true;
690       switch(type.kind)
691       {
692          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
693          case charType: type.alignment = size = sizeof(char); break;
694          case intType: type.alignment = size = sizeof(int); break;
695          case int64Type: type.alignment = size = sizeof(int64); break;
696          case intPtrType: type.alignment = size = targetBits / 8; break;
697          case intSizeType: type.alignment = size = targetBits / 8; break;
698          case longType: type.alignment = size = sizeof(long); break;
699          case shortType: type.alignment = size = sizeof(short); break;
700          case floatType: type.alignment = size = sizeof(float); break;
701          case doubleType: type.alignment = size = sizeof(double); break;
702          case classType:
703          {
704             Class _class = type._class ? type._class.registered : null;
705
706             if(_class && _class.type == structClass)
707             {
708                // Ensure all members are properly registered
709                ComputeClassMembers(_class, false);
710                type.alignment = _class.structAlignment;
711                size = _class.structSize;
712                if(type.alignment && size % type.alignment)
713                   size += type.alignment - (size % type.alignment);
714
715             }
716             else if(_class && (_class.type == unitClass ||
717                    _class.type == enumClass ||
718                    _class.type == bitClass))
719             {
720                if(!_class.dataType)
721                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
722                size = type.alignment = ComputeTypeSize(_class.dataType);
723             }
724             else
725                size = type.alignment = targetBits / 8; // sizeof(Instance *);
726             break;
727          }
728          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
729          case arrayType:
730             if(type.arraySizeExp)
731             {
732                ProcessExpressionType(type.arraySizeExp);
733                ComputeExpression(type.arraySizeExp);
734                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
735                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
736                {
737                   Location oldLoc = yylloc;
738                   // bool isConstant = type.arraySizeExp.isConstant;
739                   char expression[10240];
740                   expression[0] = '\0';
741                   type.arraySizeExp.expType = null;
742                   yylloc = type.arraySizeExp.loc;
743                   if(inCompiler)
744                      PrintExpression(type.arraySizeExp, expression);
745                   Compiler_Error($"Array size not constant int (%s)\n", expression);
746                   yylloc = oldLoc;
747                }
748                GetInt(type.arraySizeExp, &type.arraySize);
749             }
750             else if(type.enumClass)
751             {
752                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
753                {
754                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
755                }
756                else
757                   type.arraySize = 0;
758             }
759             else
760             {
761                // Unimplemented auto size
762                type.arraySize = 0;
763             }
764
765             size = ComputeTypeSize(type.type) * type.arraySize;
766             if(type.type)
767                type.alignment = type.type.alignment;
768
769             break;
770          case structType:
771          {
772             Type member;
773             for(member = type.members.first; member; member = member.next)
774             {
775                uint addSize = ComputeTypeSize(member);
776
777                member.offset = size;
778                if(member.alignment && size % member.alignment)
779                   member.offset += member.alignment - (size % member.alignment);
780                size = member.offset;
781
782                type.alignment = Max(type.alignment, member.alignment);
783                size += addSize;
784             }
785             if(type.alignment && size % type.alignment)
786                size += type.alignment - (size % type.alignment);
787             break;
788          }
789          case unionType:
790          {
791             Type member;
792             for(member = type.members.first; member; member = member.next)
793             {
794                uint addSize = ComputeTypeSize(member);
795
796                member.offset = size;
797                if(member.alignment && size % member.alignment)
798                   member.offset += member.alignment - (size % member.alignment);
799                size = member.offset;
800
801                type.alignment = Max(type.alignment, member.alignment);
802                size = Max(size, addSize);
803             }
804             if(type.alignment && size % type.alignment)
805                size += type.alignment - (size % type.alignment);
806             break;
807          }
808          case templateType:
809          {
810             TemplateParameter param = type.templateParameter;
811             Type baseType = ProcessTemplateParameterType(param);
812             if(baseType)
813             {
814                size = ComputeTypeSize(baseType);
815                type.alignment = baseType.alignment;
816             }
817             else
818                type.alignment = size = sizeof(uint64);
819             break;
820          }
821          case enumType:
822          {
823             type.alignment = size = sizeof(enum { test });
824             break;
825          }
826          case thisClassType:
827          {
828             type.alignment = size = targetBits / 8; //sizeof(void *);
829             break;
830          }
831       }
832       type.size = size;
833       type.computing = false;
834    }
835    return size;
836 }
837
838
839 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
840 {
841    // This function is in need of a major review when implementing private members etc.
842    DataMember topMember = isMember ? (DataMember) _class : null;
843    uint totalSize = 0;
844    uint maxSize = 0;
845    int alignment, size;
846    DataMember member;
847    Context context = isMember ? null : SetupTemplatesContext(_class);
848    if(addedPadding)
849       *addedPadding = false;
850
851    if(!isMember && _class.base)
852    {
853       maxSize = _class.structSize;
854       //if(_class.base.type != systemClass) // Commented out with new Instance _class
855       {
856          // DANGER: Testing this noHeadClass here...
857          if(_class.type == structClass || _class.type == noHeadClass)
858             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
859          else
860          {
861             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
862             if(maxSize > baseSize)
863                maxSize -= baseSize;
864             else
865                maxSize = 0;
866          }
867       }
868    }
869
870    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
871    {
872       if(!member.isProperty)
873       {
874          switch(member.type)
875          {
876             case normalMember:
877             {
878                if(member.dataTypeString)
879                {
880                   OldList * specs = MkList(), * decls = MkList();
881                   Declarator decl;
882
883                   decl = SpecDeclFromString(member.dataTypeString, specs,
884                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
885                   ListAdd(decls, MkStructDeclarator(decl, null));
886                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
887
888                   if(!member.dataType)
889                      member.dataType = ProcessType(specs, decl);
890
891                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
892
893                   {
894                      Type type = ProcessType(specs, decl);
895                      DeclareType(member.dataType, false, false);
896                      FreeType(type);
897                   }
898                   /*
899                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
900                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
901                      DeclareStruct(member.dataType._class.string, false);
902                   */
903
904                   ComputeTypeSize(member.dataType);
905                   size = member.dataType.size;
906                   alignment = member.dataType.alignment;
907
908                   if(alignment)
909                   {
910                      if(totalSize % alignment)
911                         totalSize += alignment - (totalSize % alignment);
912                   }
913                   totalSize += size;
914                }
915                break;
916             }
917             case unionMember:
918             case structMember:
919             {
920                OldList * specs = MkList(), * list = MkList();
921
922                size = 0;
923                AddMembers(list, (Class)member, true, &size, topClass, null);
924                ListAdd(specs,
925                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
926                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
927                alignment = member.structAlignment;
928
929                if(alignment)
930                {
931                   if(totalSize % alignment)
932                      totalSize += alignment - (totalSize % alignment);
933                }
934                totalSize += size;
935                break;
936             }
937          }
938       }
939    }
940    if(retSize)
941    {
942       if(topMember && topMember.type == unionMember)
943          *retSize = Max(*retSize, totalSize);
944       else
945          *retSize += totalSize;
946    }
947    else if(totalSize < maxSize && _class.type != systemClass)
948    {
949       int autoPadding = 0;
950       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
951          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
952       if(totalSize + autoPadding < maxSize)
953       {
954          char sizeString[50];
955          sprintf(sizeString, "%d", maxSize - totalSize);
956          ListAdd(declarations,
957             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
958             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
959          if(addedPadding)
960             *addedPadding = true;
961       }
962    }
963    if(context)
964       FinishTemplatesContext(context);
965    return topMember ? topMember.memberID : _class.memberID;
966 }
967
968 static int DeclareMembers(Class _class, bool isMember)
969 {
970    DataMember topMember = isMember ? (DataMember) _class : null;
971    uint totalSize = 0;
972    DataMember member;
973    Context context = isMember ? null : SetupTemplatesContext(_class);
974
975    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
976       DeclareMembers(_class.base, false);
977
978    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
979    {
980       if(!member.isProperty)
981       {
982          switch(member.type)
983          {
984             case normalMember:
985             {
986                /*
987                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
988                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
989                   DeclareStruct(member.dataType._class.string, false);
990                   */
991                if(!member.dataType && member.dataTypeString)
992                   member.dataType = ProcessTypeString(member.dataTypeString, false);
993                if(member.dataType)
994                   DeclareType(member.dataType, false, false);
995                break;
996             }
997             case unionMember:
998             case structMember:
999             {
1000                DeclareMembers((Class)member, true);
1001                break;
1002             }
1003          }
1004       }
1005    }
1006    if(context)
1007       FinishTemplatesContext(context);
1008
1009    return topMember ? topMember.memberID : _class.memberID;
1010 }
1011
1012 void DeclareStruct(char * name, bool skipNoHead)
1013 {
1014    External external = null;
1015    Symbol classSym = FindClass(name);
1016
1017    if(!inCompiler || !classSym) return;
1018
1019    // We don't need any declaration for bit classes...
1020    if(classSym.registered &&
1021       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1022       return;
1023
1024    /*if(classSym.registered.templateClass)
1025       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1026    */
1027
1028    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1029    {
1030       // Add typedef struct
1031       Declaration decl;
1032       OldList * specifiers, * declarators;
1033       OldList * declarations = null;
1034       char structName[1024];
1035       external = (classSym.registered && classSym.registered.type == structClass) ?
1036          classSym.pointerExternal : classSym.structExternal;
1037
1038       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1039       // Moved this one up because DeclareClass done later will need it
1040
1041       classSym.declaring++;
1042
1043       if(strchr(classSym.string, '<'))
1044       {
1045          if(classSym.registered.templateClass)
1046          {
1047             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1048             classSym.declaring--;
1049          }
1050          return;
1051       }
1052
1053       //if(!skipNoHead)
1054          DeclareMembers(classSym.registered, false);
1055
1056       structName[0] = 0;
1057       FullClassNameCat(structName, name, false);
1058
1059       /*if(!external)
1060          external = MkExternalDeclaration(null);*/
1061
1062       if(!skipNoHead)
1063       {
1064          bool addedPadding = false;
1065          classSym.declaredStructSym = true;
1066
1067          declarations = MkList();
1068
1069          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1070
1071          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1072          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1073
1074          if(!declarations->count || (declarations->count == 1 && addedPadding))
1075          {
1076             FreeList(declarations, FreeClassDef);
1077             declarations = null;
1078          }
1079       }
1080       if(skipNoHead || declarations)
1081       {
1082          if(external && external.declaration)
1083          {
1084             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1085
1086             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1087             {
1088                // TODO: Fix this
1089                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1090
1091                // DANGER
1092                if(classSym.structExternal)
1093                   ast->Move(classSym.structExternal, curExternal.prev);
1094                ast->Move(classSym.pointerExternal, curExternal.prev);
1095
1096                classSym.id = curExternal.symbol.idCode;
1097                classSym.idCode = curExternal.symbol.idCode;
1098                // external = classSym.pointerExternal;
1099                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1100             }
1101          }
1102          else
1103          {
1104             if(!external)
1105                external = MkExternalDeclaration(null);
1106
1107             specifiers = MkList();
1108             declarators = MkList();
1109             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1110
1111             /*
1112             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1113             ListAdd(declarators, MkInitDeclarator(d, null));
1114             */
1115             external.declaration = decl = MkDeclaration(specifiers, declarators);
1116             if(decl.symbol && !decl.symbol.pointerExternal)
1117                decl.symbol.pointerExternal = external;
1118
1119             // For simple classes, keep the declaration as the external to move around
1120             if(classSym.registered && classSym.registered.type == structClass)
1121             {
1122                char className[1024];
1123                strcpy(className, "__ecereClass_");
1124                FullClassNameCat(className, classSym.string, true);
1125                MangleClassName(className);
1126
1127                // Testing This
1128                DeclareClass(classSym, className);
1129
1130                external.symbol = classSym;
1131                classSym.pointerExternal = external;
1132                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1133                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1134             }
1135             else
1136             {
1137                char className[1024];
1138                strcpy(className, "__ecereClass_");
1139                FullClassNameCat(className, classSym.string, true);
1140                MangleClassName(className);
1141
1142                // TOFIX: TESTING THIS...
1143                classSym.structExternal = external;
1144                DeclareClass(classSym, className);
1145                external.symbol = classSym;
1146             }
1147
1148             //if(curExternal)
1149                ast->Insert(curExternal ? curExternal.prev : null, external);
1150          }
1151       }
1152
1153       classSym.declaring--;
1154    }
1155    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1156    {
1157       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1158       // Moved this one up because DeclareClass done later will need it
1159
1160       // TESTING THIS:
1161       classSym.declaring++;
1162
1163       //if(!skipNoHead)
1164       {
1165          if(classSym.registered)
1166             DeclareMembers(classSym.registered, false);
1167       }
1168
1169       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1170       {
1171          // TODO: Fix this
1172          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1173
1174          // DANGER
1175          if(classSym.structExternal)
1176             ast->Move(classSym.structExternal, curExternal.prev);
1177          ast->Move(classSym.pointerExternal, curExternal.prev);
1178
1179          classSym.id = curExternal.symbol.idCode;
1180          classSym.idCode = curExternal.symbol.idCode;
1181          // external = classSym.pointerExternal;
1182          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1183       }
1184
1185       classSym.declaring--;
1186    }
1187    //return external;
1188 }
1189
1190 void DeclareProperty(Property prop, char * setName, char * getName)
1191 {
1192    Symbol symbol = prop.symbol;
1193    char propName[1024];
1194
1195    strcpy(setName, "__ecereProp_");
1196    FullClassNameCat(setName, prop._class.fullName, false);
1197    strcat(setName, "_Set_");
1198    // strcat(setName, prop.name);
1199    FullClassNameCat(setName, prop.name, true);
1200
1201    strcpy(getName, "__ecereProp_");
1202    FullClassNameCat(getName, prop._class.fullName, false);
1203    strcat(getName, "_Get_");
1204    FullClassNameCat(getName, prop.name, true);
1205    // strcat(getName, prop.name);
1206
1207    strcpy(propName, "__ecereProp_");
1208    FullClassNameCat(propName, prop._class.fullName, false);
1209    strcat(propName, "_");
1210    FullClassNameCat(propName, prop.name, true);
1211    // strcat(propName, prop.name);
1212
1213    // To support "char *" property
1214    MangleClassName(getName);
1215    MangleClassName(setName);
1216    MangleClassName(propName);
1217
1218    if(prop._class.type == structClass)
1219       DeclareStruct(prop._class.fullName, false);
1220
1221    if(!symbol || curExternal.symbol.idCode < symbol.id)
1222    {
1223       bool imported = false;
1224       bool dllImport = false;
1225       if(!symbol || symbol._import)
1226       {
1227          if(!symbol)
1228          {
1229             Symbol classSym;
1230             if(!prop._class.symbol)
1231                prop._class.symbol = FindClass(prop._class.fullName);
1232             classSym = prop._class.symbol;
1233             if(classSym && !classSym._import)
1234             {
1235                ModuleImport module;
1236
1237                if(prop._class.module)
1238                   module = FindModule(prop._class.module);
1239                else
1240                   module = mainModule;
1241
1242                classSym._import = ClassImport
1243                {
1244                   name = CopyString(prop._class.fullName);
1245                   isRemote = prop._class.isRemote;
1246                };
1247                module.classes.Add(classSym._import);
1248             }
1249             symbol = prop.symbol = Symbol { };
1250             symbol._import = (ClassImport)PropertyImport
1251             {
1252                name = CopyString(prop.name);
1253                isVirtual = false; //prop.isVirtual;
1254                hasSet = prop.Set ? true : false;
1255                hasGet = prop.Get ? true : false;
1256             };
1257             if(classSym)
1258                classSym._import.properties.Add(symbol._import);
1259          }
1260          imported = true;
1261          if(prop._class.module != privateModule && prop._class.module.importType != staticImport)
1262             dllImport = true;
1263       }
1264
1265       if(!symbol.type)
1266       {
1267          Context context = SetupTemplatesContext(prop._class);
1268          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1269          FinishTemplatesContext(context);
1270       }
1271
1272       // Get
1273       if(prop.Get)
1274       {
1275          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1276          {
1277             Declaration decl;
1278             OldList * specifiers, * declarators;
1279             Declarator d;
1280             OldList * params;
1281             Specifier spec;
1282             External external;
1283             Declarator typeDecl;
1284             bool simple = false;
1285
1286             specifiers = MkList();
1287             declarators = MkList();
1288             params = MkList();
1289
1290             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1291                MkDeclaratorIdentifier(MkIdentifier("this"))));
1292
1293             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1294             //if(imported)
1295             if(dllImport)
1296                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1297
1298             {
1299                Context context = SetupTemplatesContext(prop._class);
1300                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1301                FinishTemplatesContext(context);
1302             }
1303
1304             // Make sure the simple _class's type is declared
1305             for(spec = specifiers->first; spec; spec = spec.next)
1306             {
1307                if(spec.type == nameSpecifier /*SpecifierClass*/)
1308                {
1309                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1310                   {
1311                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1312                      symbol._class = classSym.registered;
1313                      if(classSym.registered && classSym.registered.type == structClass)
1314                      {
1315                         DeclareStruct(spec.name, false);
1316                         simple = true;
1317                      }
1318                   }
1319                }
1320             }
1321
1322             if(!simple)
1323                d = PlugDeclarator(typeDecl, d);
1324             else
1325             {
1326                ListAdd(params, MkTypeName(specifiers,
1327                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1328                specifiers = MkList();
1329             }
1330
1331             d = MkDeclaratorFunction(d, params);
1332
1333             //if(imported)
1334             if(dllImport)
1335                specifiers->Insert(null, MkSpecifier(EXTERN));
1336             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1337                specifiers->Insert(null, MkSpecifier(STATIC));
1338             if(simple)
1339                ListAdd(specifiers, MkSpecifier(VOID));
1340
1341             ListAdd(declarators, MkInitDeclarator(d, null));
1342
1343             decl = MkDeclaration(specifiers, declarators);
1344
1345             external = MkExternalDeclaration(decl);
1346             ast->Insert(curExternal.prev, external);
1347             external.symbol = symbol;
1348             symbol.externalGet = external;
1349
1350             ReplaceThisClassSpecifiers(specifiers, prop._class);
1351
1352             if(typeDecl)
1353                FreeDeclarator(typeDecl);
1354          }
1355          else
1356          {
1357             // Move declaration higher...
1358             ast->Move(symbol.externalGet, curExternal.prev);
1359          }
1360       }
1361
1362       // Set
1363       if(prop.Set)
1364       {
1365          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1366          {
1367             Declaration decl;
1368             OldList * specifiers, * declarators;
1369             Declarator d;
1370             OldList * params;
1371             Specifier spec;
1372             External external;
1373             Declarator typeDecl;
1374
1375             declarators = MkList();
1376             params = MkList();
1377
1378             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1379             if(!prop.conversion || prop._class.type == structClass)
1380             {
1381                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1382                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1383             }
1384
1385             specifiers = MkList();
1386
1387             {
1388                Context context = SetupTemplatesContext(prop._class);
1389                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1390                   MkDeclaratorIdentifier(MkIdentifier("value")));
1391                FinishTemplatesContext(context);
1392             }
1393             ListAdd(params, MkTypeName(specifiers, d));
1394
1395             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1396             //if(imported)
1397             if(dllImport)
1398                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1399             d = MkDeclaratorFunction(d, params);
1400
1401             // Make sure the simple _class's type is declared
1402             for(spec = specifiers->first; spec; spec = spec.next)
1403             {
1404                if(spec.type == nameSpecifier /*SpecifierClass*/)
1405                {
1406                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1407                   {
1408                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1409                      symbol._class = classSym.registered;
1410                      if(classSym.registered && classSym.registered.type == structClass)
1411                         DeclareStruct(spec.name, false);
1412                   }
1413                }
1414             }
1415
1416             ListAdd(declarators, MkInitDeclarator(d, null));
1417
1418             specifiers = MkList();
1419             //if(imported)
1420             if(dllImport)
1421                specifiers->Insert(null, MkSpecifier(EXTERN));
1422             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1423                specifiers->Insert(null, MkSpecifier(STATIC));
1424
1425             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1426             if(!prop.conversion || prop._class.type == structClass)
1427                ListAdd(specifiers, MkSpecifier(VOID));
1428             else
1429                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1430
1431             decl = MkDeclaration(specifiers, declarators);
1432
1433             external = MkExternalDeclaration(decl);
1434             ast->Insert(curExternal.prev, external);
1435             external.symbol = symbol;
1436             symbol.externalSet = external;
1437
1438             ReplaceThisClassSpecifiers(specifiers, prop._class);
1439          }
1440          else
1441          {
1442             // Move declaration higher...
1443             ast->Move(symbol.externalSet, curExternal.prev);
1444          }
1445       }
1446
1447       // Property (for Watchers)
1448       if(!symbol.externalPtr)
1449       {
1450          Declaration decl;
1451          External external;
1452          OldList * specifiers = MkList();
1453
1454          if(imported)
1455             specifiers->Insert(null, MkSpecifier(EXTERN));
1456          else
1457             specifiers->Insert(null, MkSpecifier(STATIC));
1458
1459          ListAdd(specifiers, MkSpecifierName("Property"));
1460
1461          {
1462             OldList * list = MkList();
1463             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1464                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1465
1466             if(!imported)
1467             {
1468                strcpy(propName, "__ecerePropM_");
1469                FullClassNameCat(propName, prop._class.fullName, false);
1470                strcat(propName, "_");
1471                // strcat(propName, prop.name);
1472                FullClassNameCat(propName, prop.name, true);
1473
1474                MangleClassName(propName);
1475
1476                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1477                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1478             }
1479             decl = MkDeclaration(specifiers, list);
1480          }
1481
1482          external = MkExternalDeclaration(decl);
1483          ast->Insert(curExternal.prev, external);
1484          external.symbol = symbol;
1485          symbol.externalPtr = external;
1486       }
1487       else
1488       {
1489          // Move declaration higher...
1490          ast->Move(symbol.externalPtr, curExternal.prev);
1491       }
1492
1493       symbol.id = curExternal.symbol.idCode;
1494    }
1495 }
1496
1497 // ***************** EXPRESSION PROCESSING ***************************
1498 public Type Dereference(Type source)
1499 {
1500    Type type = null;
1501    if(source)
1502    {
1503       if(source.kind == pointerType || source.kind == arrayType)
1504       {
1505          type = source.type;
1506          source.type.refCount++;
1507       }
1508       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1509       {
1510          type = Type
1511          {
1512             kind = charType;
1513             refCount = 1;
1514          };
1515       }
1516       // Support dereferencing of no head classes for now...
1517       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1518       {
1519          type = source;
1520          source.refCount++;
1521       }
1522       else
1523          Compiler_Error($"cannot dereference type\n");
1524    }
1525    return type;
1526 }
1527
1528 static Type Reference(Type source)
1529 {
1530    Type type = null;
1531    if(source)
1532    {
1533       type = Type
1534       {
1535          kind = pointerType;
1536          type = source;
1537          refCount = 1;
1538       };
1539       source.refCount++;
1540    }
1541    return type;
1542 }
1543
1544 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1545 {
1546    Identifier ident = member.identifiers ? member.identifiers->first : null;
1547    bool found = false;
1548    DataMember dataMember = null;
1549    Method method = null;
1550    bool freeType = false;
1551
1552    yylloc = member.loc;
1553
1554    if(!ident)
1555    {
1556       if(curMember)
1557       {
1558          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1559          if(*curMember)
1560          {
1561             found = true;
1562             dataMember = *curMember;
1563          }
1564       }
1565    }
1566    else
1567    {
1568       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1569       DataMember _subMemberStack[256];
1570       int _subMemberStackPos = 0;
1571
1572       // FILL MEMBER STACK
1573       if(!thisMember)
1574          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1575       if(thisMember)
1576       {
1577          dataMember = thisMember;
1578          if(curMember && thisMember.memberAccess == publicAccess)
1579          {
1580             *curMember = thisMember;
1581             *curClass = thisMember._class;
1582             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1583             *subMemberStackPos = _subMemberStackPos;
1584          }
1585          found = true;
1586       }
1587       else
1588       {
1589          // Setting a method
1590          method = eClass_FindMethod(_class, ident.string, privateModule);
1591          if(method && method.type == virtualMethod)
1592             found = true;
1593          else
1594             method = null;
1595       }
1596    }
1597
1598    if(found)
1599    {
1600       Type type = null;
1601       if(dataMember)
1602       {
1603          if(!dataMember.dataType && dataMember.dataTypeString)
1604          {
1605             //Context context = SetupTemplatesContext(dataMember._class);
1606             Context context = SetupTemplatesContext(_class);
1607             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1608             FinishTemplatesContext(context);
1609          }
1610          type = dataMember.dataType;
1611       }
1612       else if(method)
1613       {
1614          // This is for destination type...
1615          if(!method.dataType)
1616             ProcessMethodType(method);
1617          //DeclareMethod(method);
1618          // method.dataType = ((Symbol)method.symbol)->type;
1619          type = method.dataType;
1620       }
1621
1622       if(ident && ident.next)
1623       {
1624          for(ident = ident.next; ident && type; ident = ident.next)
1625          {
1626             if(type.kind == classType)
1627             {
1628                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1629                if(!dataMember)
1630                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1631                if(dataMember)
1632                   type = dataMember.dataType;
1633             }
1634             else if(type.kind == structType || type.kind == unionType)
1635             {
1636                Type memberType;
1637                for(memberType = type.members.first; memberType; memberType = memberType.next)
1638                {
1639                   if(!strcmp(memberType.name, ident.string))
1640                   {
1641                      type = memberType;
1642                      break;
1643                   }
1644                }
1645             }
1646          }
1647       }
1648
1649       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1650       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1651       {
1652          int id = 0;
1653          ClassTemplateParameter curParam = null;
1654          Class sClass;
1655          for(sClass = _class; sClass; sClass = sClass.base)
1656          {
1657             id = 0;
1658             if(sClass.templateClass) sClass = sClass.templateClass;
1659             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1660             {
1661                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1662                {
1663                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1664                   {
1665                      if(sClass.templateClass) sClass = sClass.templateClass;
1666                      id += sClass.templateParams.count;
1667                   }
1668                   break;
1669                }
1670                id++;
1671             }
1672             if(curParam) break;
1673          }
1674
1675          if(curParam)
1676          {
1677             ClassTemplateArgument arg = _class.templateArgs[id];
1678             if(arg.dataTypeString)
1679             {
1680                // FreeType(type);
1681                type = ProcessTypeString(arg.dataTypeString, false);
1682                freeType = true;
1683                if(type && _class.templateClass)
1684                   type.passAsTemplate = true;
1685                if(type)
1686                {
1687                   // type.refCount++;
1688                   /*if(!exp.destType)
1689                   {
1690                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1691                      exp.destType.refCount++;
1692                   }*/
1693                }
1694             }
1695          }
1696       }
1697       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1698       {
1699          Class expClass = type._class.registered;
1700          Class cClass = null;
1701          int c;
1702          int paramCount = 0;
1703          int lastParam = -1;
1704
1705          char templateString[1024];
1706          ClassTemplateParameter param;
1707          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1708          for(cClass = expClass; cClass; cClass = cClass.base)
1709          {
1710             int p = 0;
1711             if(cClass.templateClass) cClass = cClass.templateClass;
1712             for(param = cClass.templateParams.first; param; param = param.next)
1713             {
1714                int id = p;
1715                Class sClass;
1716                ClassTemplateArgument arg;
1717                for(sClass = cClass.base; sClass; sClass = sClass.base)
1718                {
1719                   if(sClass.templateClass) sClass = sClass.templateClass;
1720                   id += sClass.templateParams.count;
1721                }
1722                arg = expClass.templateArgs[id];
1723
1724                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1725                {
1726                   ClassTemplateParameter cParam;
1727                   //int p = numParams - sClass.templateParams.count;
1728                   int p = 0;
1729                   Class nextClass;
1730                   if(sClass.templateClass) sClass = sClass.templateClass;
1731
1732                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1733                   {
1734                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1735                      p += nextClass.templateParams.count;
1736                   }
1737
1738                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1739                   {
1740                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1741                      {
1742                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1743                         {
1744                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1745                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1746                            break;
1747                         }
1748                      }
1749                   }
1750                }
1751
1752                {
1753                   char argument[256];
1754                   argument[0] = '\0';
1755                   /*if(arg.name)
1756                   {
1757                      strcat(argument, arg.name.string);
1758                      strcat(argument, " = ");
1759                   }*/
1760                   switch(param.type)
1761                   {
1762                      case expression:
1763                      {
1764                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1765                         char expString[1024];
1766                         OldList * specs = MkList();
1767                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1768                         Expression exp;
1769                         char * string = PrintHexUInt64(arg.expression.ui64);
1770                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1771                         delete string;
1772
1773                         ProcessExpressionType(exp);
1774                         ComputeExpression(exp);
1775                         expString[0] = '\0';
1776                         PrintExpression(exp, expString);
1777                         strcat(argument, expString);
1778                         //delete exp;
1779                         FreeExpression(exp);
1780                         break;
1781                      }
1782                      case identifier:
1783                      {
1784                         strcat(argument, arg.member.name);
1785                         break;
1786                      }
1787                      case TemplateParameterType::type:
1788                      {
1789                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1790                            strcat(argument, arg.dataTypeString);
1791                         break;
1792                      }
1793                   }
1794                   if(argument[0])
1795                   {
1796                      if(paramCount) strcat(templateString, ", ");
1797                      if(lastParam != p - 1)
1798                      {
1799                         strcat(templateString, param.name);
1800                         strcat(templateString, " = ");
1801                      }
1802                      strcat(templateString, argument);
1803                      paramCount++;
1804                      lastParam = p;
1805                   }
1806                   p++;
1807                }
1808             }
1809          }
1810          {
1811             int len = strlen(templateString);
1812             if(templateString[len-1] == '<')
1813                len--;
1814             else
1815             {
1816                if(templateString[len-1] == '>')
1817                   templateString[len++] = ' ';
1818                templateString[len++] = '>';
1819             }
1820             templateString[len++] = '\0';
1821          }
1822          {
1823             Context context = SetupTemplatesContext(_class);
1824             if(freeType) FreeType(type);
1825             type = ProcessTypeString(templateString, false);
1826             freeType = true;
1827             FinishTemplatesContext(context);
1828          }
1829       }
1830
1831       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1832       {
1833          ProcessExpressionType(member.initializer.exp);
1834          if(!member.initializer.exp.expType)
1835          {
1836             if(inCompiler)
1837             {
1838                char expString[10240];
1839                expString[0] = '\0';
1840                PrintExpression(member.initializer.exp, expString);
1841                ChangeCh(expString, '\n', ' ');
1842                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1843             }
1844          }
1845          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1846          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1847          {
1848             Compiler_Error($"incompatible instance method %s\n", ident.string);
1849          }
1850       }
1851       else if(member.initializer)
1852       {
1853          /*
1854          FreeType(member.exp.destType);
1855          member.exp.destType = type;
1856          if(member.exp.destType)
1857             member.exp.destType.refCount++;
1858          ProcessExpressionType(member.exp);
1859          */
1860
1861          ProcessInitializer(member.initializer, type);
1862       }
1863       if(freeType) FreeType(type);
1864    }
1865    else
1866    {
1867       if(_class && _class.type == unitClass)
1868       {
1869          if(member.initializer)
1870          {
1871             /*
1872             FreeType(member.exp.destType);
1873             member.exp.destType = MkClassType(_class.fullName);
1874             ProcessExpressionType(member.initializer, type);
1875             */
1876             Type type = MkClassType(_class.fullName);
1877             ProcessInitializer(member.initializer, type);
1878             FreeType(type);
1879          }
1880       }
1881       else
1882       {
1883          if(member.initializer)
1884          {
1885             //ProcessExpressionType(member.exp);
1886             ProcessInitializer(member.initializer, null);
1887          }
1888          if(ident)
1889          {
1890             if(method)
1891             {
1892                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1893             }
1894             else if(_class)
1895             {
1896                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1897                if(inCompiler)
1898                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1899             }
1900          }
1901          else if(_class)
1902             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1903       }
1904    }
1905 }
1906
1907 void ProcessInstantiationType(Instantiation inst)
1908 {
1909    yylloc = inst.loc;
1910    if(inst._class)
1911    {
1912       MembersInit members;
1913       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1914       Class _class;
1915
1916       /*if(!inst._class.symbol)
1917          inst._class.symbol = FindClass(inst._class.name);*/
1918       classSym = inst._class.symbol;
1919       _class = classSym ? classSym.registered : null;
1920
1921       // DANGER: Patch for mutex not declaring its struct when not needed
1922       if(!_class || _class.type != noHeadClass)
1923          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1924
1925       afterExternal = afterExternal ? afterExternal : curExternal;
1926
1927       if(inst.exp)
1928          ProcessExpressionType(inst.exp);
1929
1930       inst.isConstant = true;
1931       if(inst.members)
1932       {
1933          DataMember curMember = null;
1934          Class curClass = null;
1935          DataMember subMemberStack[256];
1936          int subMemberStackPos = 0;
1937
1938          for(members = inst.members->first; members; members = members.next)
1939          {
1940             switch(members.type)
1941             {
1942                case methodMembersInit:
1943                {
1944                   char name[1024];
1945                   static uint instMethodID = 0;
1946                   External external = curExternal;
1947                   Context context = curContext;
1948                   Declarator declarator = members.function.declarator;
1949                   Identifier nameID = GetDeclId(declarator);
1950                   char * unmangled = nameID ? nameID.string : null;
1951                   Expression exp;
1952                   External createdExternal = null;
1953
1954                   if(inCompiler)
1955                   {
1956                      char number[16];
1957                      //members.function.dontMangle = true;
1958                      strcpy(name, "__ecereInstMeth_");
1959                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1960                      strcat(name, "_");
1961                      strcat(name, nameID.string);
1962                      strcat(name, "_");
1963                      sprintf(number, "_%08d", instMethodID++);
1964                      strcat(name, number);
1965                      nameID.string = CopyString(name);
1966                   }
1967
1968                   // Do modifications here...
1969                   if(declarator)
1970                   {
1971                      Symbol symbol = declarator.symbol;
1972                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
1973
1974                      if(method && method.type == virtualMethod)
1975                      {
1976                         symbol.method = method;
1977                         ProcessMethodType(method);
1978
1979                         if(!symbol.type.thisClass)
1980                         {
1981                            if(method.dataType.thisClass && currentClass &&
1982                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
1983                            {
1984                               if(!currentClass.symbol)
1985                                  currentClass.symbol = FindClass(currentClass.fullName);
1986                               symbol.type.thisClass = currentClass.symbol;
1987                            }
1988                            else
1989                            {
1990                               if(!_class.symbol)
1991                                  _class.symbol = FindClass(_class.fullName);
1992                               symbol.type.thisClass = _class.symbol;
1993                            }
1994                         }
1995                         // TESTING THIS HERE:
1996                         DeclareType(symbol.type, true, true);
1997
1998                      }
1999                      else if(classSym)
2000                      {
2001                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2002                            unmangled, classSym.string);
2003                      }
2004                   }
2005
2006                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2007                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2008
2009                   if(nameID)
2010                   {
2011                      FreeSpecifier(nameID._class);
2012                      nameID._class = null;
2013                   }
2014
2015                   if(inCompiler)
2016                   {
2017
2018                      Type type = declarator.symbol.type;
2019                      External oldExternal = curExternal;
2020
2021                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2022                      // *** It was commented out for problems such as
2023                      /*
2024                            class VirtualDesktop : Window
2025                            {
2026                               clientSize = Size { };
2027                               Timer timer
2028                               {
2029                                  bool DelayExpired()
2030                                  {
2031                                     clientSize.w;
2032                                     return true;
2033                                  }
2034                               };
2035                            }
2036                      */
2037                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2038
2039                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2040
2041                      /*
2042                      if(strcmp(declarator.symbol.string, name))
2043                      {
2044                         printf("TOCHECK: Look out for this\n");
2045                         delete declarator.symbol.string;
2046                         declarator.symbol.string = CopyString(name);
2047                      }
2048
2049                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2050                      {
2051                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2052                         excludedSymbols->Remove(declarator.symbol);
2053                         globalContext.symbols.Add((BTNode)declarator.symbol);
2054                         if(strstr(declarator.symbol.string), "::")
2055                            globalContext.hasNameSpace = true;
2056
2057                      }
2058                      */
2059
2060                      //curExternal = curExternal.prev;
2061                      //afterExternal = afterExternal->next;
2062
2063                      //ProcessFunction(afterExternal->function);
2064
2065                      //curExternal = afterExternal;
2066                      {
2067                         External externalDecl;
2068                         externalDecl = MkExternalDeclaration(null);
2069                         ast->Insert(oldExternal.prev, externalDecl);
2070
2071                         // Which function does this process?
2072                         if(createdExternal.function)
2073                         {
2074                            ProcessFunction(createdExternal.function);
2075
2076                            //curExternal = oldExternal;
2077
2078                            {
2079                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2080
2081                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2082                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2083
2084                               //externalDecl = MkExternalDeclaration(decl);
2085
2086                               //***** ast->Insert(external.prev, externalDecl);
2087                               //ast->Insert(curExternal.prev, externalDecl);
2088                               externalDecl.declaration = decl;
2089                               if(decl.symbol && !decl.symbol.pointerExternal)
2090                                  decl.symbol.pointerExternal = externalDecl;
2091
2092                               // Trying this out...
2093                               declarator.symbol.pointerExternal = externalDecl;
2094                            }
2095                         }
2096                      }
2097                   }
2098                   else if(declarator)
2099                   {
2100                      curExternal = declarator.symbol.pointerExternal;
2101                      ProcessFunction((FunctionDefinition)members.function);
2102                   }
2103                   curExternal = external;
2104                   curContext = context;
2105
2106                   if(inCompiler)
2107                   {
2108                      FreeClassFunction(members.function);
2109
2110                      // In this pass, turn this into a MemberInitData
2111                      exp = QMkExpId(name);
2112                      members.type = dataMembersInit;
2113                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2114
2115                      delete unmangled;
2116                   }
2117                   break;
2118                }
2119                case dataMembersInit:
2120                {
2121                   if(members.dataMembers && classSym)
2122                   {
2123                      MemberInit member;
2124                      Location oldyyloc = yylloc;
2125                      for(member = members.dataMembers->first; member; member = member.next)
2126                      {
2127                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2128                         if(member.initializer && !member.initializer.isConstant)
2129                            inst.isConstant = false;
2130                      }
2131                      yylloc = oldyyloc;
2132                   }
2133                   break;
2134                }
2135             }
2136          }
2137       }
2138    }
2139 }
2140
2141 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2142 {
2143    // OPTIMIZATIONS: TESTING THIS...
2144    if(inCompiler)
2145    {
2146       if(type.kind == functionType)
2147       {
2148          Type param;
2149          if(declareParams)
2150          {
2151             for(param = type.params.first; param; param = param.next)
2152                DeclareType(param, declarePointers, true);
2153          }
2154          DeclareType(type.returnType, declarePointers, true);
2155       }
2156       else if(type.kind == pointerType && declarePointers)
2157          DeclareType(type.type, declarePointers, false);
2158       else if(type.kind == classType)
2159       {
2160          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2161             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2162       }
2163       else if(type.kind == structType || type.kind == unionType)
2164       {
2165          Type member;
2166          for(member = type.members.first; member; member = member.next)
2167             DeclareType(member, false, false);
2168       }
2169       else if(type.kind == arrayType)
2170          DeclareType(type.arrayType, declarePointers, false);
2171    }
2172 }
2173
2174 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2175 {
2176    ClassTemplateArgument * arg = null;
2177    int id = 0;
2178    ClassTemplateParameter curParam = null;
2179    Class sClass;
2180    for(sClass = _class; sClass; sClass = sClass.base)
2181    {
2182       id = 0;
2183       if(sClass.templateClass) sClass = sClass.templateClass;
2184       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2185       {
2186          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2187          {
2188             for(sClass = sClass.base; sClass; sClass = sClass.base)
2189             {
2190                if(sClass.templateClass) sClass = sClass.templateClass;
2191                id += sClass.templateParams.count;
2192             }
2193             break;
2194          }
2195          id++;
2196       }
2197       if(curParam) break;
2198    }
2199    if(curParam)
2200    {
2201       arg = &_class.templateArgs[id];
2202       if(arg && param.type == type)
2203          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2204    }
2205    return arg;
2206 }
2207
2208 public Context SetupTemplatesContext(Class _class)
2209 {
2210    Context context = PushContext();
2211    context.templateTypesOnly = true;
2212    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2213    {
2214       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2215       for(; param; param = param.next)
2216       {
2217          if(param.type == type && param.identifier)
2218          {
2219             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2220             curContext.templateTypes.Add((BTNode)type);
2221          }
2222       }
2223    }
2224    else if(_class)
2225    {
2226       Class sClass;
2227       for(sClass = _class; sClass; sClass = sClass.base)
2228       {
2229          ClassTemplateParameter p;
2230          for(p = sClass.templateParams.first; p; p = p.next)
2231          {
2232             //OldList * specs = MkList();
2233             //Declarator decl = null;
2234             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2235             if(p.type == type)
2236             {
2237                TemplateParameter param = p.param;
2238                TemplatedType type;
2239                if(!param)
2240                {
2241                   // ADD DATA TYPE HERE...
2242                   p.param = param = TemplateParameter
2243                   {
2244                      identifier = MkIdentifier(p.name), type = p.type,
2245                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2246                   };
2247                }
2248                type = TemplatedType { key = (uintptr)p.name, param = param };
2249                curContext.templateTypes.Add((BTNode)type);
2250             }
2251          }
2252       }
2253    }
2254    return context;
2255 }
2256
2257 public void FinishTemplatesContext(Context context)
2258 {
2259    PopContext(context);
2260    FreeContext(context);
2261    delete context;
2262 }
2263
2264 public void ProcessMethodType(Method method)
2265 {
2266    if(!method.dataType)
2267    {
2268       Context context = SetupTemplatesContext(method._class);
2269
2270       method.dataType = ProcessTypeString(method.dataTypeString, false);
2271
2272       FinishTemplatesContext(context);
2273
2274       if(method.type != virtualMethod && method.dataType)
2275       {
2276          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2277          {
2278             if(!method._class.symbol)
2279                method._class.symbol = FindClass(method._class.fullName);
2280             method.dataType.thisClass = method._class.symbol;
2281          }
2282       }
2283
2284       // Why was this commented out? Working fine without now...
2285
2286       /*
2287       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2288          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2289          */
2290    }
2291
2292    /*
2293    if(type)
2294    {
2295       char * par = strstr(type, "(");
2296       char * classOp = null;
2297       int classOpLen = 0;
2298       if(par)
2299       {
2300          int c;
2301          for(c = par-type-1; c >= 0; c++)
2302          {
2303             if(type[c] == ':' && type[c+1] == ':')
2304             {
2305                classOp = type + c - 1;
2306                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2307                {
2308                   classOp--;
2309                   classOpLen++;
2310                }
2311                break;
2312             }
2313             else if(!isspace(type[c]))
2314                break;
2315          }
2316       }
2317       if(classOp)
2318       {
2319          char temp[1024];
2320          int typeLen = strlen(type);
2321          memcpy(temp, classOp, classOpLen);
2322          temp[classOpLen] = '\0';
2323          if(temp[0])
2324             _class = eSystem_FindClass(module, temp);
2325          else
2326             _class = null;
2327          method.dataTypeString = new char[typeLen - classOpLen + 1];
2328          memcpy(method.dataTypeString, type, classOp - type);
2329          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2330       }
2331       else
2332          method.dataTypeString = type;
2333    }
2334    */
2335 }
2336
2337
2338 public void ProcessPropertyType(Property prop)
2339 {
2340    if(!prop.dataType)
2341    {
2342       Context context = SetupTemplatesContext(prop._class);
2343       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2344       FinishTemplatesContext(context);
2345    }
2346 }
2347
2348 public void DeclareMethod(Method method, char * name)
2349 {
2350    Symbol symbol = method.symbol;
2351    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2352    {
2353       bool imported = false;
2354       bool dllImport = false;
2355
2356       if(!method.dataType)
2357          method.dataType = ProcessTypeString(method.dataTypeString, false);
2358
2359       if(!symbol || symbol._import || method.type == virtualMethod)
2360       {
2361          if(!symbol || method.type == virtualMethod)
2362          {
2363             Symbol classSym;
2364             if(!method._class.symbol)
2365                method._class.symbol = FindClass(method._class.fullName);
2366             classSym = method._class.symbol;
2367             if(!classSym._import)
2368             {
2369                ModuleImport module;
2370
2371                if(method._class.module && method._class.module.name)
2372                   module = FindModule(method._class.module);
2373                else
2374                   module = mainModule;
2375                classSym._import = ClassImport
2376                {
2377                   name = CopyString(method._class.fullName);
2378                   isRemote = method._class.isRemote;
2379                };
2380                module.classes.Add(classSym._import);
2381             }
2382             if(!symbol)
2383             {
2384                symbol = method.symbol = Symbol { };
2385             }
2386             if(!symbol._import)
2387             {
2388                symbol._import = (ClassImport)MethodImport
2389                {
2390                   name = CopyString(method.name);
2391                   isVirtual = method.type == virtualMethod;
2392                };
2393                classSym._import.methods.Add(symbol._import);
2394             }
2395             if(!symbol)
2396             {
2397                // Set the symbol type
2398                /*
2399                if(!type.thisClass)
2400                {
2401                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2402                }
2403                else if(type.thisClass == (void *)-1)
2404                {
2405                   type.thisClass = null;
2406                }
2407                */
2408                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2409                symbol.type = method.dataType;
2410                if(symbol.type) symbol.type.refCount++;
2411             }
2412             /*
2413             if(!method.thisClass || strcmp(method.thisClass, "void"))
2414                symbol.type.params.Insert(null,
2415                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2416             */
2417          }
2418          if(!method.dataType.dllExport)
2419          {
2420             imported = true;
2421             if(method._class.module != privateModule && method._class.module.importType != staticImport)
2422                dllImport = true;
2423          }
2424       }
2425
2426       /* MOVING THIS UP
2427       if(!method.dataType)
2428          method.dataType = ((Symbol)method.symbol).type;
2429          //ProcessMethodType(method);
2430       */
2431
2432       if(method.type != virtualMethod && method.dataType)
2433          DeclareType(method.dataType, true, true);
2434
2435       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2436       {
2437          // We need a declaration here :)
2438          Declaration decl;
2439          OldList * specifiers, * declarators;
2440          Declarator d;
2441          Declarator funcDecl;
2442          External external;
2443
2444          specifiers = MkList();
2445          declarators = MkList();
2446
2447          //if(imported)
2448          if(dllImport)
2449             ListAdd(specifiers, MkSpecifier(EXTERN));
2450          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2451             ListAdd(specifiers, MkSpecifier(STATIC));
2452
2453          if(method.type == virtualMethod)
2454          {
2455             ListAdd(specifiers, MkSpecifier(INT));
2456             d = MkDeclaratorIdentifier(MkIdentifier(name));
2457          }
2458          else
2459          {
2460             d = MkDeclaratorIdentifier(MkIdentifier(name));
2461             //if(imported)
2462             if(dllImport)
2463                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2464             {
2465                Context context = SetupTemplatesContext(method._class);
2466                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2467                FinishTemplatesContext(context);
2468             }
2469             funcDecl = GetFuncDecl(d);
2470
2471             if(dllImport)
2472             {
2473                Specifier spec, next;
2474                for(spec = specifiers->first; spec; spec = next)
2475                {
2476                   next = spec.next;
2477                   if(spec.type == extendedSpecifier)
2478                   {
2479                      specifiers->Remove(spec);
2480                      FreeSpecifier(spec);
2481                   }
2482                }
2483             }
2484
2485             // Add this parameter if not a static method
2486             if(method.dataType && !method.dataType.staticMethod)
2487             {
2488                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2489                {
2490                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2491                   TypeName thisParam = MkTypeName(MkListOne(
2492                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2493                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2494                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2495                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2496
2497                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2498                   {
2499                      TypeName param = funcDecl.function.parameters->first;
2500                      funcDecl.function.parameters->Remove(param);
2501                      FreeTypeName(param);
2502                   }
2503
2504                   if(!funcDecl.function.parameters)
2505                      funcDecl.function.parameters = MkList();
2506                   funcDecl.function.parameters->Insert(null, thisParam);
2507                }
2508             }
2509             // Make sure we don't have empty parameter declarations for static methods...
2510             /*
2511             else if(!funcDecl.function.parameters)
2512             {
2513                funcDecl.function.parameters = MkList();
2514                funcDecl.function.parameters->Insert(null,
2515                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2516             }*/
2517          }
2518          // TESTING THIS:
2519          ProcessDeclarator(d);
2520
2521          ListAdd(declarators, MkInitDeclarator(d, null));
2522
2523          decl = MkDeclaration(specifiers, declarators);
2524
2525          ReplaceThisClassSpecifiers(specifiers, method._class);
2526
2527          // Keep a different symbol for the function definition than the declaration...
2528          if(symbol.pointerExternal)
2529          {
2530             Symbol functionSymbol { };
2531
2532             // Copy symbol
2533             {
2534                *functionSymbol = *symbol;
2535                functionSymbol.string = CopyString(symbol.string);
2536                if(functionSymbol.type)
2537                   functionSymbol.type.refCount++;
2538             }
2539
2540             excludedSymbols->Add(functionSymbol);
2541             symbol.pointerExternal.symbol = functionSymbol;
2542          }
2543          external = MkExternalDeclaration(decl);
2544          if(curExternal)
2545             ast->Insert(curExternal ? curExternal.prev : null, external);
2546          external.symbol = symbol;
2547          symbol.pointerExternal = external;
2548       }
2549       else if(ast)
2550       {
2551          // Move declaration higher...
2552          ast->Move(symbol.pointerExternal, curExternal.prev);
2553       }
2554
2555       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2556    }
2557 }
2558
2559 char * ReplaceThisClass(Class _class)
2560 {
2561    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2562    {
2563       bool first = true;
2564       int p = 0;
2565       ClassTemplateParameter param;
2566       int lastParam = -1;
2567
2568       char className[1024];
2569       strcpy(className, _class.fullName);
2570       for(param = _class.templateParams.first; param; param = param.next)
2571       {
2572          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2573          {
2574             if(first) strcat(className, "<");
2575             if(!first) strcat(className, ", ");
2576             if(lastParam + 1 != p)
2577             {
2578                strcat(className, param.name);
2579                strcat(className, " = ");
2580             }
2581             strcat(className, param.name);
2582             first = false;
2583             lastParam = p;
2584          }
2585          p++;
2586       }
2587       if(!first)
2588       {
2589          int len = strlen(className);
2590          if(className[len-1] == '>') className[len++] = ' ';
2591          className[len++] = '>';
2592          className[len++] = '\0';
2593       }
2594       return CopyString(className);
2595    }
2596    else
2597       return CopyString(_class.fullName);
2598 }
2599
2600 Type ReplaceThisClassType(Class _class)
2601 {
2602    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2603    {
2604       bool first = true;
2605       int p = 0;
2606       ClassTemplateParameter param;
2607       int lastParam = -1;
2608       char className[1024];
2609       strcpy(className, _class.fullName);
2610
2611       for(param = _class.templateParams.first; param; param = param.next)
2612       {
2613          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2614          {
2615             if(first) strcat(className, "<");
2616             if(!first) strcat(className, ", ");
2617             if(lastParam + 1 != p)
2618             {
2619                strcat(className, param.name);
2620                strcat(className, " = ");
2621             }
2622             strcat(className, param.name);
2623             first = false;
2624             lastParam = p;
2625          }
2626          p++;
2627       }
2628       if(!first)
2629       {
2630          int len = strlen(className);
2631          if(className[len-1] == '>') className[len++] = ' ';
2632          className[len++] = '>';
2633          className[len++] = '\0';
2634       }
2635       return MkClassType(className);
2636       //return ProcessTypeString(className, false);
2637    }
2638    else
2639    {
2640       return MkClassType(_class.fullName);
2641       //return ProcessTypeString(_class.fullName, false);
2642    }
2643 }
2644
2645 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2646 {
2647    if(specs != null && _class)
2648    {
2649       Specifier spec;
2650       for(spec = specs.first; spec; spec = spec.next)
2651       {
2652          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2653          {
2654             spec.type = nameSpecifier;
2655             spec.name = ReplaceThisClass(_class);
2656             spec.symbol = FindClass(spec.name); //_class.symbol;
2657          }
2658       }
2659    }
2660 }
2661
2662 // Returns imported or not
2663 bool DeclareFunction(GlobalFunction function, char * name)
2664 {
2665    Symbol symbol = function.symbol;
2666    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2667    {
2668       bool imported = false;
2669       bool dllImport = false;
2670
2671       if(!function.dataType)
2672       {
2673          function.dataType = ProcessTypeString(function.dataTypeString, false);
2674          if(!function.dataType.thisClass)
2675             function.dataType.staticMethod = true;
2676       }
2677
2678       if(inCompiler)
2679       {
2680          if(!symbol)
2681          {
2682             ModuleImport module = FindModule(function.module);
2683             // WARNING: This is not added anywhere...
2684             symbol = function.symbol = Symbol {  };
2685
2686             if(module.name)
2687             {
2688                if(!function.dataType.dllExport)
2689                {
2690                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2691                   module.functions.Add(symbol._import);
2692                }
2693             }
2694             // Set the symbol type
2695             {
2696                symbol.type = ProcessTypeString(function.dataTypeString, false);
2697                if(!symbol.type.thisClass)
2698                   symbol.type.staticMethod = true;
2699             }
2700          }
2701          imported = symbol._import ? true : false;
2702          if(imported && function.module != privateModule && function.module.importType != staticImport)
2703             dllImport = true;
2704       }
2705
2706       DeclareType(function.dataType, true, true);
2707
2708       if(inCompiler)
2709       {
2710          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2711          {
2712             // We need a declaration here :)
2713             Declaration decl;
2714             OldList * specifiers, * declarators;
2715             Declarator d;
2716             Declarator funcDecl;
2717             External external;
2718
2719             specifiers = MkList();
2720             declarators = MkList();
2721
2722             //if(imported)
2723                ListAdd(specifiers, MkSpecifier(EXTERN));
2724             /*
2725             else
2726                ListAdd(specifiers, MkSpecifier(STATIC));
2727             */
2728
2729             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2730             //if(imported)
2731             if(dllImport)
2732                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2733
2734             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2735             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2736             if(function.module.importType == staticImport)
2737             {
2738                Specifier spec;
2739                for(spec = specifiers->first; spec; spec = spec.next)
2740                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2741                   {
2742                      specifiers->Remove(spec);
2743                      FreeSpecifier(spec);
2744                      break;
2745                   }
2746             }
2747
2748             funcDecl = GetFuncDecl(d);
2749
2750             // Make sure we don't have empty parameter declarations for static methods...
2751             if(funcDecl && !funcDecl.function.parameters)
2752             {
2753                funcDecl.function.parameters = MkList();
2754                funcDecl.function.parameters->Insert(null,
2755                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2756             }
2757
2758             ListAdd(declarators, MkInitDeclarator(d, null));
2759
2760             {
2761                Context oldCtx = curContext;
2762                curContext = globalContext;
2763                decl = MkDeclaration(specifiers, declarators);
2764                curContext = oldCtx;
2765             }
2766
2767             // Keep a different symbol for the function definition than the declaration...
2768             if(symbol.pointerExternal)
2769             {
2770                Symbol functionSymbol { };
2771                // Copy symbol
2772                {
2773                   *functionSymbol = *symbol;
2774                   functionSymbol.string = CopyString(symbol.string);
2775                   if(functionSymbol.type)
2776                      functionSymbol.type.refCount++;
2777                }
2778
2779                excludedSymbols->Add(functionSymbol);
2780
2781                symbol.pointerExternal.symbol = functionSymbol;
2782             }
2783             external = MkExternalDeclaration(decl);
2784             if(curExternal)
2785                ast->Insert(curExternal.prev, external);
2786             external.symbol = symbol;
2787             symbol.pointerExternal = external;
2788          }
2789          else
2790          {
2791             // Move declaration higher...
2792             ast->Move(symbol.pointerExternal, curExternal.prev);
2793          }
2794
2795          if(curExternal)
2796             symbol.id = curExternal.symbol.idCode;
2797       }
2798    }
2799    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2800 }
2801
2802 void DeclareGlobalData(GlobalData data)
2803 {
2804    Symbol symbol = data.symbol;
2805    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2806    {
2807       if(inCompiler)
2808       {
2809          if(!symbol)
2810             symbol = data.symbol = Symbol { };
2811       }
2812       if(!data.dataType)
2813          data.dataType = ProcessTypeString(data.dataTypeString, false);
2814       DeclareType(data.dataType, true, true);
2815       if(inCompiler)
2816       {
2817          if(!symbol.pointerExternal)
2818          {
2819             // We need a declaration here :)
2820             Declaration decl;
2821             OldList * specifiers, * declarators;
2822             Declarator d;
2823             External external;
2824
2825             specifiers = MkList();
2826             declarators = MkList();
2827
2828             ListAdd(specifiers, MkSpecifier(EXTERN));
2829             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2830             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2831
2832             ListAdd(declarators, MkInitDeclarator(d, null));
2833
2834             decl = MkDeclaration(specifiers, declarators);
2835             external = MkExternalDeclaration(decl);
2836             if(curExternal)
2837                ast->Insert(curExternal.prev, external);
2838             external.symbol = symbol;
2839             symbol.pointerExternal = external;
2840          }
2841          else
2842          {
2843             // Move declaration higher...
2844             ast->Move(symbol.pointerExternal, curExternal.prev);
2845          }
2846
2847          if(curExternal)
2848             symbol.id = curExternal.symbol.idCode;
2849       }
2850    }
2851 }
2852
2853 class Conversion : struct
2854 {
2855    Conversion prev, next;
2856    Property convert;
2857    bool isGet;
2858    Type resultType;
2859 };
2860
2861 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2862 {
2863    if(source && dest)
2864    {
2865       // Property convert;
2866
2867       if(source.kind == templateType && dest.kind != templateType)
2868       {
2869          Type type = ProcessTemplateParameterType(source.templateParameter);
2870          if(type) source = type;
2871       }
2872
2873       if(dest.kind == templateType && source.kind != templateType)
2874       {
2875          Type type = ProcessTemplateParameterType(dest.templateParameter);
2876          if(type) dest = type;
2877       }
2878
2879       if(dest.classObjectType == typedObject)
2880       {
2881          if(source.classObjectType != anyObject)
2882             return true;
2883          else
2884          {
2885             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2886             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2887             {
2888                return true;
2889             }
2890          }
2891       }
2892       else
2893       {
2894          if(source.classObjectType == anyObject)
2895             return true;
2896          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2897             return true;
2898       }
2899
2900       if((dest.kind == structType && source.kind == structType) ||
2901          (dest.kind == unionType && source.kind == unionType))
2902       {
2903          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2904              (source.members.first && source.members.first == dest.members.first))
2905             return true;
2906       }
2907
2908       if(dest.kind == ellipsisType && source.kind != voidType)
2909          return true;
2910
2911       if(dest.kind == pointerType && dest.type.kind == voidType &&
2912          ((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))
2913          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2914
2915          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2916
2917          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2918          return true;
2919       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2920          ((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))
2921          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2922
2923          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2924
2925          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2926          return true;
2927
2928       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2929       {
2930          if(source._class.registered && source._class.registered.type == unitClass)
2931          {
2932             if(conversions != null)
2933             {
2934                if(source._class.registered == dest._class.registered)
2935                   return true;
2936             }
2937             else
2938             {
2939                Class sourceBase, destBase;
2940                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2941                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2942                if(sourceBase == destBase)
2943                   return true;
2944             }
2945          }
2946          // Don't match enum inheriting from other enum if resolving enumeration values
2947          // TESTING: !dest.classObjectType
2948          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2949             (enumBaseType ||
2950                (!source._class.registered || source._class.registered.type != enumClass) ||
2951                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2952             return true;
2953          else
2954          {
2955             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2956             if(enumBaseType &&
2957                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2958                ((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)
2959             {
2960                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2961                {
2962                   return true;
2963                }
2964             }
2965          }
2966       }
2967
2968       // JUST ADDED THIS...
2969       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2970          return true;
2971
2972       if(doConversion)
2973       {
2974          // Just added this for Straight conversion of ColorAlpha => Color
2975          if(source.kind == classType)
2976          {
2977             Class _class;
2978             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2979             {
2980                Property convert;
2981                for(convert = _class.conversions.first; convert; convert = convert.next)
2982                {
2983                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2984                   {
2985                      Conversion after = (conversions != null) ? conversions.last : null;
2986
2987                      if(!convert.dataType)
2988                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
2989                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
2990                      {
2991                         if(!conversions && !convert.Get)
2992                            return true;
2993                         else if(conversions != null)
2994                         {
2995                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
2996                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
2997                               (dest.kind != classType || dest._class.registered != _class.base))
2998                               return true;
2999                            else
3000                            {
3001                               Conversion conv { convert = convert, isGet = true };
3002                               // conversions.Add(conv);
3003                               conversions.Insert(after, conv);
3004                               return true;
3005                            }
3006                         }
3007                      }
3008                   }
3009                }
3010             }
3011          }
3012
3013          // MOVING THIS??
3014
3015          if(dest.kind == classType)
3016          {
3017             Class _class;
3018             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3019             {
3020                Property convert;
3021                for(convert = _class.conversions.first; convert; convert = convert.next)
3022                {
3023                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3024                   {
3025                      // Conversion after = (conversions != null) ? conversions.last : null;
3026
3027                      if(!convert.dataType)
3028                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3029                      // Just added this equality check to prevent recursion.... Make it safer?
3030                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3031                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3032                      {
3033                         if(!conversions && !convert.Set)
3034                            return true;
3035                         else if(conversions != null)
3036                         {
3037                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3038                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3039                               (source.kind != classType || source._class.registered != _class.base))
3040                               return true;
3041                            else
3042                            {
3043                               // *** Testing this! ***
3044                               Conversion conv { convert = convert };
3045                               conversions.Add(conv);
3046                               //conversions.Insert(after, conv);
3047                               return true;
3048                            }
3049                         }
3050                      }
3051                   }
3052                }
3053             }
3054             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3055             {
3056                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3057                   (source.kind != classType || source._class.registered.type != structClass))
3058                   return true;
3059             }*/
3060
3061             // TESTING THIS... IS THIS OK??
3062             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3063             {
3064                if(!dest._class.registered.dataType)
3065                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3066                // Only support this for classes...
3067                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3068                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3069                {
3070                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3071                   {
3072                      return true;
3073                   }
3074                }
3075             }
3076          }
3077
3078          // Moved this lower
3079          if(source.kind == classType)
3080          {
3081             Class _class;
3082             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3083             {
3084                Property convert;
3085                for(convert = _class.conversions.first; convert; convert = convert.next)
3086                {
3087                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3088                   {
3089                      Conversion after = (conversions != null) ? conversions.last : null;
3090
3091                      if(!convert.dataType)
3092                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3093                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3094                      {
3095                         if(!conversions && !convert.Get)
3096                            return true;
3097                         else if(conversions != null)
3098                         {
3099                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3100                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3101                               (dest.kind != classType || dest._class.registered != _class.base))
3102                               return true;
3103                            else
3104                            {
3105                               Conversion conv { convert = convert, isGet = true };
3106
3107                               // conversions.Add(conv);
3108                               conversions.Insert(after, conv);
3109                               return true;
3110                            }
3111                         }
3112                      }
3113                   }
3114                }
3115             }
3116
3117             // TESTING THIS... IS THIS OK??
3118             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3119             {
3120                if(!source._class.registered.dataType)
3121                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3122                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3123                {
3124                   return true;
3125                }
3126             }
3127          }
3128       }
3129
3130       if(source.kind == classType || source.kind == subClassType)
3131          ;
3132       else if(dest.kind == source.kind &&
3133          (dest.kind != structType && dest.kind != unionType &&
3134           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3135           return true;
3136       // RECENTLY ADDED THESE
3137       else if(dest.kind == doubleType && source.kind == floatType)
3138          return true;
3139       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3140          return true;
3141       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3142          return true;
3143       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3144          return true;
3145       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3146          return true;
3147       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3148          return true;
3149       else if(source.kind == enumType &&
3150          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3151           return true;
3152       else if(dest.kind == enumType &&
3153          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3154           return true;
3155       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3156               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3157       {
3158          Type paramSource, paramDest;
3159
3160          if(dest.kind == methodType)
3161             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3162          if(source.kind == methodType)
3163             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3164
3165          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3166          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3167          if(dest.kind == methodType)
3168             dest = dest.method.dataType;
3169          if(source.kind == methodType)
3170             source = source.method.dataType;
3171
3172          paramSource = source.params.first;
3173          if(paramSource && paramSource.kind == voidType) paramSource = null;
3174          paramDest = dest.params.first;
3175          if(paramDest && paramDest.kind == voidType) paramDest = null;
3176
3177
3178          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3179             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3180          {
3181             // Source thisClass must be derived from destination thisClass
3182             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3183                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3184             {
3185                if(paramDest && paramDest.kind == classType)
3186                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3187                else
3188                   Compiler_Error($"method class should not take an object\n");
3189                return false;
3190             }
3191             paramDest = paramDest.next;
3192          }
3193          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3194          {
3195             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3196             {
3197                if(dest.thisClass)
3198                {
3199                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3200                   {
3201                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3202                      return false;
3203                   }
3204                }
3205                else
3206                {
3207                   // THIS WAS BACKWARDS:
3208                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3209                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3210                   {
3211                      if(owningClassDest)
3212                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3213                      else
3214                         Compiler_Error($"overriding class expected to be derived from method class\n");
3215                      return false;
3216                   }
3217                }
3218                paramSource = paramSource.next;
3219             }
3220             else
3221             {
3222                if(dest.thisClass)
3223                {
3224                   // Source thisClass must be derived from destination thisClass
3225                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3226                   {
3227                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3228                      return false;
3229                   }
3230                }
3231                else
3232                {
3233                   // THIS WAS BACKWARDS TOO??
3234                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3235                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3236                   {
3237                      //if(owningClass)
3238                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3239                      //else
3240                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3241                      return false;
3242                   }
3243                }
3244             }
3245          }
3246
3247
3248          // Source return type must be derived from destination return type
3249          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3250          {
3251             Compiler_Warning($"incompatible return type for function\n");
3252             return false;
3253          }
3254
3255          // Check parameters
3256
3257          for(; paramDest; paramDest = paramDest.next)
3258          {
3259             if(!paramSource)
3260             {
3261                //Compiler_Warning($"not enough parameters\n");
3262                Compiler_Error($"not enough parameters\n");
3263                return false;
3264             }
3265             {
3266                Type paramDestType = paramDest;
3267                Type paramSourceType = paramSource;
3268                Type type = paramDestType;
3269
3270                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3271                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3272                   paramSource.kind != templateType)
3273                {
3274                   int id = 0;
3275                   ClassTemplateParameter curParam = null;
3276                   Class sClass;
3277                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3278                   {
3279                      id = 0;
3280                      if(sClass.templateClass) sClass = sClass.templateClass;
3281                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3282                      {
3283                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3284                         {
3285                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3286                            {
3287                               if(sClass.templateClass) sClass = sClass.templateClass;
3288                               id += sClass.templateParams.count;
3289                            }
3290                            break;
3291                         }
3292                         id++;
3293                      }
3294                      if(curParam) break;
3295                   }
3296
3297                   if(curParam)
3298                   {
3299                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3300                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3301                   }
3302                }
3303
3304                // paramDest must be derived from paramSource
3305                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3306                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3307                {
3308                   char type[1024];
3309                   type[0] = 0;
3310                   PrintType(paramDest, type, false, true);
3311                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3312
3313                   if(paramDestType != paramDest)
3314                      FreeType(paramDestType);
3315                   return false;
3316                }
3317                if(paramDestType != paramDest)
3318                   FreeType(paramDestType);
3319             }
3320
3321             paramSource = paramSource.next;
3322          }
3323          if(paramSource)
3324          {
3325             Compiler_Error($"too many parameters\n");
3326             return false;
3327          }
3328          return true;
3329       }
3330       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3331       {
3332          return true;
3333       }
3334       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3335          (source.kind == arrayType || source.kind == pointerType))
3336       {
3337          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3338             return true;
3339       }
3340    }
3341    return false;
3342 }
3343
3344 static void FreeConvert(Conversion convert)
3345 {
3346    if(convert.resultType)
3347       FreeType(convert.resultType);
3348 }
3349
3350 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3351                               char * string, OldList conversions)
3352 {
3353    BTNamedLink link;
3354
3355    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3356    {
3357       Class _class = link.data;
3358       if(_class.type == enumClass)
3359       {
3360          OldList converts { };
3361          Type type { };
3362          type.kind = classType;
3363
3364          if(!_class.symbol)
3365             _class.symbol = FindClass(_class.fullName);
3366          type._class = _class.symbol;
3367
3368          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3369          {
3370             NamedLink value;
3371             Class enumClass = eSystem_FindClass(privateModule, "enum");
3372             if(enumClass)
3373             {
3374                Class baseClass;
3375                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3376                {
3377                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3378                   for(value = e.values.first; value; value = value.next)
3379                   {
3380                      if(!strcmp(value.name, string))
3381                         break;
3382                   }
3383                   if(value)
3384                   {
3385                      FreeExpContents(sourceExp);
3386                      FreeType(sourceExp.expType);
3387
3388                      sourceExp.isConstant = true;
3389                      sourceExp.expType = MkClassType(baseClass.fullName);
3390                      //if(inCompiler)
3391                      {
3392                         char constant[256];
3393                         sourceExp.type = constantExp;
3394                         if(!strcmp(baseClass.dataTypeString, "int"))
3395                            sprintf(constant, "%d",(int)value.data);
3396                         else
3397                            sprintf(constant, "0x%X",(int)value.data);
3398                         sourceExp.constant = CopyString(constant);
3399                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3400                      }
3401
3402                      while(converts.first)
3403                      {
3404                         Conversion convert = converts.first;
3405                         converts.Remove(convert);
3406                         conversions.Add(convert);
3407                      }
3408                      delete type;
3409                      return true;
3410                   }
3411                }
3412             }
3413          }
3414          if(converts.first)
3415             converts.Free(FreeConvert);
3416          delete type;
3417       }
3418    }
3419    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3420       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3421          return true;
3422    return false;
3423 }
3424
3425 public bool ModuleVisibility(Module searchIn, Module searchFor)
3426 {
3427    SubModule subModule;
3428
3429    if(searchFor == searchIn)
3430       return true;
3431
3432    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3433    {
3434       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3435       {
3436          if(ModuleVisibility(subModule.module, searchFor))
3437             return true;
3438       }
3439    }
3440    return false;
3441 }
3442
3443 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3444 {
3445    Module module;
3446
3447    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3448       return true;
3449    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3450       return true;
3451    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3452       return true;
3453
3454    for(module = mainModule.application.allModules.first; module; module = module.next)
3455    {
3456       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3457          return true;
3458    }
3459    return false;
3460 }
3461
3462 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3463 {
3464    Type source = sourceExp.expType;
3465    Type realDest = dest;
3466    Type backupSourceExpType = null;
3467
3468    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3469       return true;
3470
3471    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3472    {
3473        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3474        {
3475           Class sourceBase, destBase;
3476           for(sourceBase = source._class.registered;
3477               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3478               sourceBase = sourceBase.base);
3479           for(destBase = dest._class.registered;
3480               destBase && destBase.base && destBase.base.type != systemClass;
3481               destBase = destBase.base);
3482           //if(source._class.registered == dest._class.registered)
3483           if(sourceBase == destBase)
3484              return true;
3485        }
3486    }
3487
3488    if(source)
3489    {
3490       OldList * specs;
3491       bool flag = false;
3492       int64 value = MAXINT;
3493
3494       source.refCount++;
3495       dest.refCount++;
3496
3497       if(sourceExp.type == constantExp)
3498       {
3499          if(source.isSigned)
3500             value = strtoll(sourceExp.constant, null, 0);
3501          else
3502             value = strtoull(sourceExp.constant, null, 0);
3503       }
3504       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3505       {
3506          if(source.isSigned)
3507             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3508          else
3509             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3510       }
3511
3512       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3513          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3514       {
3515          FreeType(source);
3516          source = Type { kind = intType, isSigned = false, refCount = 1 };
3517       }
3518
3519       if(dest.kind == classType)
3520       {
3521          Class _class = dest._class ? dest._class.registered : null;
3522
3523          if(_class && _class.type == unitClass)
3524          {
3525             if(source.kind != classType)
3526             {
3527                Type tempType { };
3528                Type tempDest, tempSource;
3529
3530                for(; _class.base.type != systemClass; _class = _class.base);
3531                tempSource = dest;
3532                tempDest = tempType;
3533
3534                tempType.kind = classType;
3535                if(!_class.symbol)
3536                   _class.symbol = FindClass(_class.fullName);
3537
3538                tempType._class = _class.symbol;
3539                tempType.truth = dest.truth;
3540                if(tempType._class)
3541                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3542
3543                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3544                backupSourceExpType = sourceExp.expType;
3545                sourceExp.expType = dest; dest.refCount++;
3546                //sourceExp.expType = MkClassType(_class.fullName);
3547                flag = true;
3548
3549                delete tempType;
3550             }
3551          }
3552
3553
3554          // Why wasn't there something like this?
3555          if(_class && _class.type == bitClass && source.kind != classType)
3556          {
3557             if(!dest._class.registered.dataType)
3558                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3559             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3560             {
3561                FreeType(source);
3562                FreeType(sourceExp.expType);
3563                source = sourceExp.expType = MkClassType(dest._class.string);
3564                source.refCount++;
3565
3566                //source.kind = classType;
3567                //source._class = dest._class;
3568             }
3569          }
3570
3571          // Adding two enumerations
3572          /*
3573          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3574          {
3575             if(!source._class.registered.dataType)
3576                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3577             if(!dest._class.registered.dataType)
3578                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3579
3580             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3581             {
3582                FreeType(source);
3583                source = sourceExp.expType = MkClassType(dest._class.string);
3584                source.refCount++;
3585
3586                //source.kind = classType;
3587                //source._class = dest._class;
3588             }
3589          }*/
3590
3591          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3592          {
3593             OldList * specs = MkList();
3594             Declarator decl;
3595             char string[1024];
3596
3597             ReadString(string, sourceExp.string);
3598             decl = SpecDeclFromString(string, specs, null);
3599
3600             FreeExpContents(sourceExp);
3601             FreeType(sourceExp.expType);
3602
3603             sourceExp.type = classExp;
3604             sourceExp._classExp.specifiers = specs;
3605             sourceExp._classExp.decl = decl;
3606             sourceExp.expType = dest;
3607             dest.refCount++;
3608
3609             FreeType(source);
3610             FreeType(dest);
3611             if(backupSourceExpType) FreeType(backupSourceExpType);
3612             return true;
3613          }
3614       }
3615       else if(source.kind == classType)
3616       {
3617          Class _class = source._class ? source._class.registered : null;
3618
3619          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3620          {
3621             /*
3622             if(dest.kind != classType)
3623             {
3624                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3625                if(!source._class.registered.dataType)
3626                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3627
3628                FreeType(dest);
3629                dest = MkClassType(source._class.string);
3630                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3631                //   dest = MkClassType(source._class.string);
3632             }
3633             */
3634
3635             if(dest.kind != classType)
3636             {
3637                Type tempType { };
3638                Type tempDest, tempSource;
3639
3640                if(!source._class.registered.dataType)
3641                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3642
3643                for(; _class.base.type != systemClass; _class = _class.base);
3644                tempDest = source;
3645                tempSource = tempType;
3646                tempType.kind = classType;
3647                tempType._class = FindClass(_class.fullName);
3648                tempType.truth = source.truth;
3649                tempType.classObjectType = source.classObjectType;
3650
3651                if(tempType._class)
3652                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3653
3654                // PUT THIS BACK TESTING UNITS?
3655                if(conversions.last)
3656                {
3657                   ((Conversion)(conversions.last)).resultType = dest;
3658                   dest.refCount++;
3659                }
3660
3661                FreeType(sourceExp.expType);
3662                sourceExp.expType = MkClassType(_class.fullName);
3663                sourceExp.expType.truth = source.truth;
3664                sourceExp.expType.classObjectType = source.classObjectType;
3665
3666                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3667
3668                if(!sourceExp.destType)
3669                {
3670                   FreeType(sourceExp.destType);
3671                   sourceExp.destType = sourceExp.expType;
3672                   if(sourceExp.expType)
3673                      sourceExp.expType.refCount++;
3674                }
3675                //flag = true;
3676                //source = _class.dataType;
3677
3678
3679                // TOCHECK: TESTING THIS NEW CODE
3680                if(!_class.dataType)
3681                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3682                FreeType(dest);
3683                dest = MkClassType(source._class.string);
3684                dest.truth = source.truth;
3685                dest.classObjectType = source.classObjectType;
3686
3687                FreeType(source);
3688                source = _class.dataType;
3689                source.refCount++;
3690
3691                delete tempType;
3692             }
3693          }
3694       }
3695
3696       if(!flag)
3697       {
3698          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3699          {
3700             FreeType(source);
3701             FreeType(dest);
3702             return true;
3703          }
3704       }
3705
3706       // Implicit Casts
3707       /*
3708       if(source.kind == classType)
3709       {
3710          Class _class = source._class.registered;
3711          if(_class.type == unitClass)
3712          {
3713             if(!_class.dataType)
3714                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3715             source = _class.dataType;
3716          }
3717       }*/
3718
3719       if(dest.kind == classType)
3720       {
3721          Class _class = dest._class ? dest._class.registered : null;
3722          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") ||
3723             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3724          {
3725             if(_class.type == normalClass || _class.type == noHeadClass)
3726             {
3727                Expression newExp { };
3728                *newExp = *sourceExp;
3729                if(sourceExp.destType) sourceExp.destType.refCount++;
3730                if(sourceExp.expType)  sourceExp.expType.refCount++;
3731                sourceExp.type = castExp;
3732                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3733                sourceExp.cast.exp = newExp;
3734                FreeType(sourceExp.expType);
3735                sourceExp.expType = null;
3736                ProcessExpressionType(sourceExp);
3737
3738                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3739                if(!inCompiler)
3740                {
3741                   FreeType(sourceExp.expType);
3742                   sourceExp.expType = dest;
3743                }
3744
3745                FreeType(source);
3746                if(inCompiler) FreeType(dest);
3747
3748                if(backupSourceExpType) FreeType(backupSourceExpType);
3749                return true;
3750             }
3751
3752             if(!_class.dataType)
3753                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3754             FreeType(dest);
3755             dest = _class.dataType;
3756             dest.refCount++;
3757          }
3758
3759          // Accept lower precision types for units, since we want to keep the unit type
3760          if(dest.kind == doubleType &&
3761             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3762              source.kind == charType || source.kind == _BoolType))
3763          {
3764             specs = MkListOne(MkSpecifier(DOUBLE));
3765          }
3766          else if(dest.kind == floatType &&
3767             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3768             source.kind == _BoolType || source.kind == doubleType))
3769          {
3770             specs = MkListOne(MkSpecifier(FLOAT));
3771          }
3772          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3773             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3774          {
3775             specs = MkList();
3776             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3777             ListAdd(specs, MkSpecifier(INT64));
3778          }
3779          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3780             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3781          {
3782             specs = MkList();
3783             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3784             ListAdd(specs, MkSpecifier(INT));
3785          }
3786          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3787             source.kind == floatType || source.kind == doubleType))
3788          {
3789             specs = MkList();
3790             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3791             ListAdd(specs, MkSpecifier(SHORT));
3792          }
3793          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3794             source.kind == floatType || source.kind == doubleType))
3795          {
3796             specs = MkList();
3797             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3798             ListAdd(specs, MkSpecifier(CHAR));
3799          }
3800          else
3801          {
3802             FreeType(source);
3803             FreeType(dest);
3804             if(backupSourceExpType)
3805             {
3806                // Failed to convert: revert previous exp type
3807                if(sourceExp.expType) FreeType(sourceExp.expType);
3808                sourceExp.expType = backupSourceExpType;
3809             }
3810             return false;
3811          }
3812       }
3813       else if(dest.kind == doubleType &&
3814          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3815           source.kind == _BoolType || source.kind == charType))
3816       {
3817          specs = MkListOne(MkSpecifier(DOUBLE));
3818       }
3819       else if(dest.kind == floatType &&
3820          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3821       {
3822          specs = MkListOne(MkSpecifier(FLOAT));
3823       }
3824       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3825          (value == 1 || value == 0))
3826       {
3827          specs = MkList();
3828          ListAdd(specs, MkSpecifier(BOOL));
3829       }
3830       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3831          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3832       {
3833          specs = MkList();
3834          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3835          ListAdd(specs, MkSpecifier(CHAR));
3836       }
3837       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3838          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3839       {
3840          specs = MkList();
3841          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3842          ListAdd(specs, MkSpecifier(SHORT));
3843       }
3844       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3845       {
3846          specs = MkList();
3847          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3848          ListAdd(specs, MkSpecifier(INT));
3849       }
3850       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3851       {
3852          specs = MkList();
3853          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3854          ListAdd(specs, MkSpecifier(INT64));
3855       }
3856       else if(dest.kind == enumType &&
3857          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3858       {
3859          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3860       }
3861       else
3862       {
3863          FreeType(source);
3864          FreeType(dest);
3865          if(backupSourceExpType)
3866          {
3867             // Failed to convert: revert previous exp type
3868             if(sourceExp.expType) FreeType(sourceExp.expType);
3869             sourceExp.expType = backupSourceExpType;
3870          }
3871          return false;
3872       }
3873
3874       if(!flag)
3875       {
3876          Expression newExp { };
3877          *newExp = *sourceExp;
3878          newExp.prev = null;
3879          newExp.next = null;
3880          if(sourceExp.destType) sourceExp.destType.refCount++;
3881          if(sourceExp.expType)  sourceExp.expType.refCount++;
3882
3883          sourceExp.type = castExp;
3884          if(realDest.kind == classType)
3885          {
3886             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3887             FreeList(specs, FreeSpecifier);
3888          }
3889          else
3890             sourceExp.cast.typeName = MkTypeName(specs, null);
3891          if(newExp.type == opExp)
3892          {
3893             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3894          }
3895          else
3896             sourceExp.cast.exp = newExp;
3897
3898          FreeType(sourceExp.expType);
3899          sourceExp.expType = null;
3900          ProcessExpressionType(sourceExp);
3901       }
3902       else
3903          FreeList(specs, FreeSpecifier);
3904
3905       FreeType(dest);
3906       FreeType(source);
3907       if(backupSourceExpType) FreeType(backupSourceExpType);
3908
3909       return true;
3910    }
3911    else
3912    {
3913       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3914       if(sourceExp.type == identifierExp)
3915       {
3916          Identifier id = sourceExp.identifier;
3917          if(dest.kind == classType)
3918          {
3919             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3920             {
3921                Class _class = dest._class.registered;
3922                Class enumClass = eSystem_FindClass(privateModule, "enum");
3923                if(enumClass)
3924                {
3925                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3926                   {
3927                      NamedLink value;
3928                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3929                      for(value = e.values.first; value; value = value.next)
3930                      {
3931                         if(!strcmp(value.name, id.string))
3932                            break;
3933                      }
3934                      if(value)
3935                      {
3936                         FreeExpContents(sourceExp);
3937                         FreeType(sourceExp.expType);
3938
3939                         sourceExp.isConstant = true;
3940                         sourceExp.expType = MkClassType(_class.fullName);
3941                         //if(inCompiler)
3942                         {
3943                            char constant[256];
3944                            sourceExp.type = constantExp;
3945                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3946                               sprintf(constant, "%d", (int) value.data);
3947                            else
3948                               sprintf(constant, "0x%X", (int) value.data);
3949                            sourceExp.constant = CopyString(constant);
3950                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3951                         }
3952                         return true;
3953                      }
3954                   }
3955                }
3956             }
3957          }
3958
3959          // Loop through all enum classes
3960          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3961             return true;
3962       }
3963    }
3964    return false;
3965 }
3966
3967 #define TERTIARY(o, name, m, t, p) \
3968    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
3969    {                                                              \
3970       exp.type = constantExp;                                    \
3971       exp.string = p(op1.m ? op2.m : op3.m);                     \
3972       if(!exp.expType) \
3973          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3974       return true;                                                \
3975    }
3976
3977 #define BINARY(o, name, m, t, p) \
3978    static bool name(Expression exp, Operand op1, Operand op2)   \
3979    {                                                              \
3980       t value2 = op2.m;                                           \
3981       exp.type = constantExp;                                    \
3982       exp.string = p(op1.m o value2);                     \
3983       if(!exp.expType) \
3984          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3985       return true;                                                \
3986    }
3987
3988 #define BINARY_DIVIDE(o, name, m, t, p) \
3989    static bool name(Expression exp, Operand op1, Operand op2)   \
3990    {                                                              \
3991       t value2 = op2.m;                                           \
3992       exp.type = constantExp;                                    \
3993       exp.string = p(value2 ? (op1.m o value2) : 0);             \
3994       if(!exp.expType) \
3995          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3996       return true;                                                \
3997    }
3998
3999 #define UNARY(o, name, m, t, p) \
4000    static bool name(Expression exp, Operand op1)                \
4001    {                                                              \
4002       exp.type = constantExp;                                    \
4003       exp.string = p((t)(o op1.m));                                   \
4004       if(!exp.expType) \
4005          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4006       return true;                                                \
4007    }
4008
4009 #define OPERATOR_ALL(macro, o, name) \
4010    macro(o, Int##name, i, int, PrintInt) \
4011    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4012    macro(o, Int64##name, i64, int64, PrintInt64) \
4013    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4014    macro(o, Short##name, s, short, PrintShort) \
4015    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4016    macro(o, Char##name, c, char, PrintChar) \
4017    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4018    macro(o, Float##name, f, float, PrintFloat) \
4019    macro(o, Double##name, d, double, PrintDouble)
4020
4021 #define OPERATOR_INTTYPES(macro, o, name) \
4022    macro(o, Int##name, i, int, PrintInt) \
4023    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4024    macro(o, Int64##name, i64, int64, PrintInt64) \
4025    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4026    macro(o, Short##name, s, short, PrintShort) \
4027    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4028    macro(o, Char##name, c, char, PrintChar) \
4029    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4030
4031
4032 // binary arithmetic
4033 OPERATOR_ALL(BINARY, +, Add)
4034 OPERATOR_ALL(BINARY, -, Sub)
4035 OPERATOR_ALL(BINARY, *, Mul)
4036 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4037 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4038
4039 // unary arithmetic
4040 OPERATOR_ALL(UNARY, -, Neg)
4041
4042 // unary arithmetic increment and decrement
4043 OPERATOR_ALL(UNARY, ++, Inc)
4044 OPERATOR_ALL(UNARY, --, Dec)
4045
4046 // binary arithmetic assignment
4047 OPERATOR_ALL(BINARY, =, Asign)
4048 OPERATOR_ALL(BINARY, +=, AddAsign)
4049 OPERATOR_ALL(BINARY, -=, SubAsign)
4050 OPERATOR_ALL(BINARY, *=, MulAsign)
4051 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4052 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4053
4054 // binary bitwise
4055 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4056 OPERATOR_INTTYPES(BINARY, |, BitOr)
4057 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4058 OPERATOR_INTTYPES(BINARY, <<, LShift)
4059 OPERATOR_INTTYPES(BINARY, >>, RShift)
4060
4061 // unary bitwise
4062 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4063
4064 // binary bitwise assignment
4065 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4066 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4067 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4068 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4069 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4070
4071 // unary logical negation
4072 OPERATOR_INTTYPES(UNARY, !, Not)
4073
4074 // binary logical equality
4075 OPERATOR_ALL(BINARY, ==, Equ)
4076 OPERATOR_ALL(BINARY, !=, Nqu)
4077
4078 // binary logical
4079 OPERATOR_ALL(BINARY, &&, And)
4080 OPERATOR_ALL(BINARY, ||, Or)
4081
4082 // binary logical relational
4083 OPERATOR_ALL(BINARY, >, Grt)
4084 OPERATOR_ALL(BINARY, <, Sma)
4085 OPERATOR_ALL(BINARY, >=, GrtEqu)
4086 OPERATOR_ALL(BINARY, <=, SmaEqu)
4087
4088 // tertiary condition operator
4089 OPERATOR_ALL(TERTIARY, ?, Cond)
4090
4091 //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
4092 #define OPERATOR_TABLE_ALL(name, type) \
4093     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4094                           type##Neg, \
4095                           type##Inc, type##Dec, \
4096                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4097                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4098                           type##BitNot, \
4099                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4100                           type##Not, \
4101                           type##Equ, type##Nqu, \
4102                           type##And, type##Or, \
4103                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4104                         }; \
4105
4106 #define OPERATOR_TABLE_INTTYPES(name, type) \
4107     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4108                           type##Neg, \
4109                           type##Inc, type##Dec, \
4110                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4111                           null, null, null, null, null, \
4112                           null, \
4113                           null, null, null, null, null, \
4114                           null, \
4115                           type##Equ, type##Nqu, \
4116                           type##And, type##Or, \
4117                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4118                         }; \
4119
4120 OPERATOR_TABLE_ALL(int, Int)
4121 OPERATOR_TABLE_ALL(uint, UInt)
4122 OPERATOR_TABLE_ALL(int64, Int64)
4123 OPERATOR_TABLE_ALL(uint64, UInt64)
4124 OPERATOR_TABLE_ALL(short, Short)
4125 OPERATOR_TABLE_ALL(ushort, UShort)
4126 OPERATOR_TABLE_INTTYPES(float, Float)
4127 OPERATOR_TABLE_INTTYPES(double, Double)
4128 OPERATOR_TABLE_ALL(char, Char)
4129 OPERATOR_TABLE_ALL(uchar, UChar)
4130
4131 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4132 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4133 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4134 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4135 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4136 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4137 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4138 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4139
4140 public void ReadString(char * output,  char * string)
4141 {
4142    int len = strlen(string);
4143    int c,d = 0;
4144    bool quoted = false, escaped = false;
4145    for(c = 0; c<len; c++)
4146    {
4147       char ch = string[c];
4148       if(escaped)
4149       {
4150          switch(ch)
4151          {
4152             case 'n': output[d] = '\n'; break;
4153             case 't': output[d] = '\t'; break;
4154             case 'a': output[d] = '\a'; break;
4155             case 'b': output[d] = '\b'; break;
4156             case 'f': output[d] = '\f'; break;
4157             case 'r': output[d] = '\r'; break;
4158             case 'v': output[d] = '\v'; break;
4159             case '\\': output[d] = '\\'; break;
4160             case '\"': output[d] = '\"'; break;
4161             case '\'': output[d] = '\''; break;
4162             default: output[d] = ch;
4163          }
4164          d++;
4165          escaped = false;
4166       }
4167       else
4168       {
4169          if(ch == '\"')
4170             quoted ^= true;
4171          else if(quoted)
4172          {
4173             if(ch == '\\')
4174                escaped = true;
4175             else
4176                output[d++] = ch;
4177          }
4178       }
4179    }
4180    output[d] = '\0';
4181 }
4182
4183 // String Unescape Copy
4184
4185 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4186 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4187 public int UnescapeString(char * d, char * s, int len)
4188 {
4189    int j = 0, k = 0;
4190    char ch;
4191    while(j < len && (ch = s[j]))
4192    {
4193       switch(ch)
4194       {
4195          case '\\':
4196             switch((ch = s[++j]))
4197             {
4198                case 'n': d[k] = '\n'; break;
4199                case 't': d[k] = '\t'; break;
4200                case 'a': d[k] = '\a'; break;
4201                case 'b': d[k] = '\b'; break;
4202                case 'f': d[k] = '\f'; break;
4203                case 'r': d[k] = '\r'; break;
4204                case 'v': d[k] = '\v'; break;
4205                case '\\': d[k] = '\\'; break;
4206                case '\"': d[k] = '\"'; break;
4207                case '\'': d[k] = '\''; break;
4208                default: d[k] = '\\'; d[k] = ch;
4209             }
4210             break;
4211          default:
4212             d[k] = ch;
4213       }
4214       j++, k++;
4215    }
4216    d[k] = '\0';
4217    return k;
4218 }
4219
4220 public char * OffsetEscapedString(char * s, int len, int offset)
4221 {
4222    char ch;
4223    int j = 0, k = 0;
4224    while(j < len && k < offset && (ch = s[j]))
4225    {
4226       if(ch == '\\') ++j;
4227       j++, k++;
4228    }
4229    return (k == offset) ? s + j : null;
4230 }
4231
4232 public Operand GetOperand(Expression exp)
4233 {
4234    Operand op { };
4235    Type type = exp.expType;
4236    if(type)
4237    {
4238       while(type.kind == classType &&
4239          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4240       {
4241          if(!type._class.registered.dataType)
4242             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4243          type = type._class.registered.dataType;
4244
4245       }
4246       op.kind = type.kind;
4247       op.type = exp.expType;
4248       if(exp.type == stringExp && op.kind == pointerType)
4249       {
4250          op.ui64 = (uint64)exp.string;
4251          op.kind = pointerType;
4252          op.ops = uint64Ops;
4253       }
4254       else if(exp.isConstant && exp.type == constantExp)
4255       {
4256          switch(op.kind)
4257          {
4258             case _BoolType:
4259             case charType:
4260             {
4261                if(exp.constant[0] == '\'')
4262                {
4263                   op.c = exp.constant[1];
4264                   op.ops = charOps;
4265                }
4266                else if(type.isSigned)
4267                {
4268                   op.c = (char)strtol(exp.constant, null, 0);
4269                   op.ops = charOps;
4270                }
4271                else
4272                {
4273                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4274                   op.ops = ucharOps;
4275                }
4276                break;
4277             }
4278             case shortType:
4279                if(type.isSigned)
4280                {
4281                   op.s = (short)strtol(exp.constant, null, 0);
4282                   op.ops = shortOps;
4283                }
4284                else
4285                {
4286                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4287                   op.ops = ushortOps;
4288                }
4289                break;
4290             case intType:
4291             case longType:
4292                if(type.isSigned)
4293                {
4294                   op.i = (int)strtol(exp.constant, null, 0);
4295                   op.ops = intOps;
4296                }
4297                else
4298                {
4299                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4300                   op.ops = uintOps;
4301                }
4302                op.kind = intType;
4303                break;
4304             case int64Type:
4305                if(type.isSigned)
4306                {
4307                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4308                   op.ops = intOps;
4309                }
4310                else
4311                {
4312                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4313                   op.ops = uintOps;
4314                }
4315                op.kind = int64Type;
4316                break;
4317             case intPtrType:
4318                if(type.isSigned)
4319                {
4320                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4321                   op.ops = int64Ops;
4322                }
4323                else
4324                {
4325                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4326                   op.ops = uint64Ops;
4327                }
4328                op.kind = int64Type;
4329                break;
4330             case intSizeType:
4331                if(type.isSigned)
4332                {
4333                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4334                   op.ops = int64Ops;
4335                }
4336                else
4337                {
4338                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4339                   op.ops = uint64Ops;
4340                }
4341                op.kind = int64Type;
4342                break;
4343             case floatType:
4344                op.f = (float)strtod(exp.constant, null);
4345                op.ops = floatOps;
4346                break;
4347             case doubleType:
4348                op.d = (double)strtod(exp.constant, null);
4349                op.ops = doubleOps;
4350                break;
4351             //case classType:    For when we have operator overloading...
4352             // Pointer additions
4353             //case functionType:
4354             case arrayType:
4355             case pointerType:
4356             case classType:
4357                op.ui64 = _strtoui64(exp.constant, null, 0);
4358                op.kind = pointerType;
4359                op.ops = uint64Ops;
4360                // op.ptrSize =
4361                break;
4362          }
4363       }
4364    }
4365    return op;
4366 }
4367
4368 static void UnusedFunction()
4369 {
4370    int a;
4371    a.OnGetString(0,0,0);
4372 }
4373 default:
4374 extern int __ecereVMethodID_class_OnGetString;
4375 public:
4376
4377 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4378 {
4379    DataMember dataMember;
4380    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4381    {
4382       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4383          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4384       else
4385       {
4386          Expression exp { };
4387          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4388          Type type;
4389          void * ptr = inst.data + dataMember.offset + offset;
4390          char * result = null;
4391          exp.loc = member.loc = inst.loc;
4392          ((Identifier)member.identifiers->first).loc = inst.loc;
4393
4394          if(!dataMember.dataType)
4395             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4396          type = dataMember.dataType;
4397          if(type.kind == classType)
4398          {
4399             Class _class = type._class.registered;
4400             if(_class.type == enumClass)
4401             {
4402                Class enumClass = eSystem_FindClass(privateModule, "enum");
4403                if(enumClass)
4404                {
4405                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4406                   NamedLink item;
4407                   for(item = e.values.first; item; item = item.next)
4408                   {
4409                      if((int)item.data == *(int *)ptr)
4410                      {
4411                         result = item.name;
4412                         break;
4413                      }
4414                   }
4415                   if(result)
4416                   {
4417                      exp.identifier = MkIdentifier(result);
4418                      exp.type = identifierExp;
4419                      exp.destType = MkClassType(_class.fullName);
4420                      ProcessExpressionType(exp);
4421                   }
4422                }
4423             }
4424             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4425             {
4426                if(!_class.dataType)
4427                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4428                type = _class.dataType;
4429             }
4430          }
4431          if(!result)
4432          {
4433             switch(type.kind)
4434             {
4435                case floatType:
4436                {
4437                   FreeExpContents(exp);
4438
4439                   exp.constant = PrintFloat(*(float*)ptr);
4440                   exp.type = constantExp;
4441                   break;
4442                }
4443                case doubleType:
4444                {
4445                   FreeExpContents(exp);
4446
4447                   exp.constant = PrintDouble(*(double*)ptr);
4448                   exp.type = constantExp;
4449                   break;
4450                }
4451                case intType:
4452                {
4453                   FreeExpContents(exp);
4454
4455                   exp.constant = PrintInt(*(int*)ptr);
4456                   exp.type = constantExp;
4457                   break;
4458                }
4459                case int64Type:
4460                {
4461                   FreeExpContents(exp);
4462
4463                   exp.constant = PrintInt64(*(int64*)ptr);
4464                   exp.type = constantExp;
4465                   break;
4466                }
4467                case intPtrType:
4468                {
4469                   FreeExpContents(exp);
4470                   // TODO: This should probably use proper type
4471                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4472                   exp.type = constantExp;
4473                   break;
4474                }
4475                case intSizeType:
4476                {
4477                   FreeExpContents(exp);
4478                   // TODO: This should probably use proper type
4479                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4480                   exp.type = constantExp;
4481                   break;
4482                }
4483                default:
4484                   Compiler_Error($"Unhandled type populating instance\n");
4485             }
4486          }
4487          ListAdd(memberList, member);
4488       }
4489
4490       if(parentDataMember.type == unionMember)
4491          break;
4492    }
4493 }
4494
4495 void PopulateInstance(Instantiation inst)
4496 {
4497    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4498    Class _class = classSym.registered;
4499    DataMember dataMember;
4500    OldList * memberList = MkList();
4501    // Added this check and ->Add to prevent memory leaks on bad code
4502    if(!inst.members)
4503       inst.members = MkListOne(MkMembersInitList(memberList));
4504    else
4505       inst.members->Add(MkMembersInitList(memberList));
4506    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4507    {
4508       if(!dataMember.isProperty)
4509       {
4510          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4511             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4512          else
4513          {
4514             Expression exp { };
4515             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4516             Type type;
4517             void * ptr = inst.data + dataMember.offset;
4518             char * result = null;
4519
4520             exp.loc = member.loc = inst.loc;
4521             ((Identifier)member.identifiers->first).loc = inst.loc;
4522
4523             if(!dataMember.dataType)
4524                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4525             type = dataMember.dataType;
4526             if(type.kind == classType)
4527             {
4528                Class _class = type._class.registered;
4529                if(_class.type == enumClass)
4530                {
4531                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4532                   if(enumClass)
4533                   {
4534                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4535                      NamedLink item;
4536                      for(item = e.values.first; item; item = item.next)
4537                      {
4538                         if((int)item.data == *(int *)ptr)
4539                         {
4540                            result = item.name;
4541                            break;
4542                         }
4543                      }
4544                   }
4545                   if(result)
4546                   {
4547                      exp.identifier = MkIdentifier(result);
4548                      exp.type = identifierExp;
4549                      exp.destType = MkClassType(_class.fullName);
4550                      ProcessExpressionType(exp);
4551                   }
4552                }
4553                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4554                {
4555                   if(!_class.dataType)
4556                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4557                   type = _class.dataType;
4558                }
4559             }
4560             if(!result)
4561             {
4562                switch(type.kind)
4563                {
4564                   case floatType:
4565                   {
4566                      exp.constant = PrintFloat(*(float*)ptr);
4567                      exp.type = constantExp;
4568                      break;
4569                   }
4570                   case doubleType:
4571                   {
4572                      exp.constant = PrintDouble(*(double*)ptr);
4573                      exp.type = constantExp;
4574                      break;
4575                   }
4576                   case intType:
4577                   {
4578                      exp.constant = PrintInt(*(int*)ptr);
4579                      exp.type = constantExp;
4580                      break;
4581                   }
4582                   case int64Type:
4583                   {
4584                      exp.constant = PrintInt64(*(int64*)ptr);
4585                      exp.type = constantExp;
4586                      break;
4587                   }
4588                   case intPtrType:
4589                   {
4590                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4591                      exp.type = constantExp;
4592                      break;
4593                   }
4594                   default:
4595                      Compiler_Error($"Unhandled type populating instance\n");
4596                }
4597             }
4598             ListAdd(memberList, member);
4599          }
4600       }
4601    }
4602 }
4603
4604 void ComputeInstantiation(Expression exp)
4605 {
4606    Instantiation inst = exp.instance;
4607    MembersInit members;
4608    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4609    Class _class = classSym ? classSym.registered : null;
4610    DataMember curMember = null;
4611    Class curClass = null;
4612    DataMember subMemberStack[256];
4613    int subMemberStackPos = 0;
4614    uint64 bits = 0;
4615
4616    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4617    {
4618       // Don't recompute the instantiation...
4619       // Non Simple classes will have become constants by now
4620       if(inst.data)
4621          return;
4622
4623       if(_class.type == normalClass || _class.type == noHeadClass)
4624       {
4625          inst.data = (byte *)eInstance_New(_class);
4626          if(_class.type == normalClass)
4627             ((Instance)inst.data)._refCount++;
4628       }
4629       else
4630          inst.data = new0 byte[_class.structSize];
4631    }
4632
4633    if(inst.members)
4634    {
4635       for(members = inst.members->first; members; members = members.next)
4636       {
4637          switch(members.type)
4638          {
4639             case dataMembersInit:
4640             {
4641                if(members.dataMembers)
4642                {
4643                   MemberInit member;
4644                   for(member = members.dataMembers->first; member; member = member.next)
4645                   {
4646                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4647                      bool found = false;
4648
4649                      Property prop = null;
4650                      DataMember dataMember = null;
4651                      Method method = null;
4652                      uint dataMemberOffset;
4653
4654                      if(!ident)
4655                      {
4656                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4657                         if(curMember)
4658                         {
4659                            if(curMember.isProperty)
4660                               prop = (Property)curMember;
4661                            else
4662                            {
4663                               dataMember = curMember;
4664
4665                               // CHANGED THIS HERE
4666                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4667
4668                               // 2013/17/29 -- It seems that this was missing here!
4669                               if(_class.type == normalClass)
4670                                  dataMemberOffset += _class.base.structSize;
4671                               // dataMemberOffset = dataMember.offset;
4672                            }
4673                            found = true;
4674                         }
4675                      }
4676                      else
4677                      {
4678                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4679                         if(prop)
4680                         {
4681                            found = true;
4682                            if(prop.memberAccess == publicAccess)
4683                            {
4684                               curMember = (DataMember)prop;
4685                               curClass = prop._class;
4686                            }
4687                         }
4688                         else
4689                         {
4690                            DataMember _subMemberStack[256];
4691                            int _subMemberStackPos = 0;
4692
4693                            // FILL MEMBER STACK
4694                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4695
4696                            if(dataMember)
4697                            {
4698                               found = true;
4699                               if(dataMember.memberAccess == publicAccess)
4700                               {
4701                                  curMember = dataMember;
4702                                  curClass = dataMember._class;
4703                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4704                                  subMemberStackPos = _subMemberStackPos;
4705                               }
4706                            }
4707                         }
4708                      }
4709
4710                      if(found && member.initializer && member.initializer.type == expInitializer)
4711                      {
4712                         Expression value = member.initializer.exp;
4713                         Type type = null;
4714                         bool deepMember = false;
4715                         if(prop)
4716                         {
4717                            type = prop.dataType;
4718                         }
4719                         else if(dataMember)
4720                         {
4721                            if(!dataMember.dataType)
4722                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4723
4724                            type = dataMember.dataType;
4725                         }
4726
4727                         if(ident && ident.next)
4728                         {
4729                            deepMember = true;
4730
4731                            // for(; ident && type; ident = ident.next)
4732                            for(ident = ident.next; ident && type; ident = ident.next)
4733                            {
4734                               if(type.kind == classType)
4735                               {
4736                                  prop = eClass_FindProperty(type._class.registered,
4737                                     ident.string, privateModule);
4738                                  if(prop)
4739                                     type = prop.dataType;
4740                                  else
4741                                  {
4742                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4743                                        ident.string, &dataMemberOffset, privateModule, null, null);
4744                                     if(dataMember)
4745                                        type = dataMember.dataType;
4746                                  }
4747                               }
4748                               else if(type.kind == structType || type.kind == unionType)
4749                               {
4750                                  Type memberType;
4751                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4752                                  {
4753                                     if(!strcmp(memberType.name, ident.string))
4754                                     {
4755                                        type = memberType;
4756                                        break;
4757                                     }
4758                                  }
4759                               }
4760                            }
4761                         }
4762                         if(value)
4763                         {
4764                            FreeType(value.destType);
4765                            value.destType = type;
4766                            if(type) type.refCount++;
4767                            ComputeExpression(value);
4768                         }
4769                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4770                         {
4771                            if(type.kind == classType)
4772                            {
4773                               Class _class = type._class.registered;
4774                               if(_class.type == bitClass || _class.type == unitClass ||
4775                                  _class.type == enumClass)
4776                               {
4777                                  if(!_class.dataType)
4778                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4779                                  type = _class.dataType;
4780                               }
4781                            }
4782
4783                            if(dataMember)
4784                            {
4785                               void * ptr = inst.data + dataMemberOffset;
4786
4787                               if(value.type == constantExp)
4788                               {
4789                                  switch(type.kind)
4790                                  {
4791                                     case intType:
4792                                     {
4793                                        GetInt(value, (int*)ptr);
4794                                        break;
4795                                     }
4796                                     case int64Type:
4797                                     {
4798                                        GetInt64(value, (int64*)ptr);
4799                                        break;
4800                                     }
4801                                     case intPtrType:
4802                                     {
4803                                        GetIntPtr(value, (intptr*)ptr);
4804                                        break;
4805                                     }
4806                                     case intSizeType:
4807                                     {
4808                                        GetIntSize(value, (intsize*)ptr);
4809                                        break;
4810                                     }
4811                                     case floatType:
4812                                     {
4813                                        GetFloat(value, (float*)ptr);
4814                                        break;
4815                                     }
4816                                     case doubleType:
4817                                     {
4818                                        GetDouble(value, (double *)ptr);
4819                                        break;
4820                                     }
4821                                  }
4822                               }
4823                               else if(value.type == instanceExp)
4824                               {
4825                                  if(type.kind == classType)
4826                                  {
4827                                     Class _class = type._class.registered;
4828                                     if(_class.type == structClass)
4829                                     {
4830                                        ComputeTypeSize(type);
4831                                        if(value.instance.data)
4832                                           memcpy(ptr, value.instance.data, type.size);
4833                                     }
4834                                  }
4835                               }
4836                            }
4837                            else if(prop)
4838                            {
4839                               if(value.type == instanceExp && value.instance.data)
4840                               {
4841                                  if(type.kind == classType)
4842                                  {
4843                                     Class _class = type._class.registered;
4844                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4845                                     {
4846                                        void (*Set)(void *, void *) = (void *)prop.Set;
4847                                        Set(inst.data, value.instance.data);
4848                                        PopulateInstance(inst);
4849                                     }
4850                                  }
4851                               }
4852                               else if(value.type == constantExp)
4853                               {
4854                                  switch(type.kind)
4855                                  {
4856                                     case doubleType:
4857                                     {
4858                                        void (*Set)(void *, double) = (void *)prop.Set;
4859                                        Set(inst.data, strtod(value.constant, null) );
4860                                        break;
4861                                     }
4862                                     case floatType:
4863                                     {
4864                                        void (*Set)(void *, float) = (void *)prop.Set;
4865                                        Set(inst.data, (float)(strtod(value.constant, null)));
4866                                        break;
4867                                     }
4868                                     case intType:
4869                                     {
4870                                        void (*Set)(void *, int) = (void *)prop.Set;
4871                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4872                                        break;
4873                                     }
4874                                     case int64Type:
4875                                     {
4876                                        void (*Set)(void *, int64) = (void *)prop.Set;
4877                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4878                                        break;
4879                                     }
4880                                     case intPtrType:
4881                                     {
4882                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4883                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4884                                        break;
4885                                     }
4886                                     case intSizeType:
4887                                     {
4888                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4889                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4890                                        break;
4891                                     }
4892                                  }
4893                               }
4894                               else if(value.type == stringExp)
4895                               {
4896                                  char temp[1024];
4897                                  ReadString(temp, value.string);
4898                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4899                               }
4900                            }
4901                         }
4902                         else if(!deepMember && type && _class.type == unitClass)
4903                         {
4904                            if(prop)
4905                            {
4906                               // Only support converting units to units for now...
4907                               if(value.type == constantExp)
4908                               {
4909                                  if(type.kind == classType)
4910                                  {
4911                                     Class _class = type._class.registered;
4912                                     if(_class.type == unitClass)
4913                                     {
4914                                        if(!_class.dataType)
4915                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4916                                        type = _class.dataType;
4917                                     }
4918                                  }
4919                                  // TODO: Assuming same base type for units...
4920                                  switch(type.kind)
4921                                  {
4922                                     case floatType:
4923                                     {
4924                                        float fValue;
4925                                        float (*Set)(float) = (void *)prop.Set;
4926                                        GetFloat(member.initializer.exp, &fValue);
4927                                        exp.constant = PrintFloat(Set(fValue));
4928                                        exp.type = constantExp;
4929                                        break;
4930                                     }
4931                                     case doubleType:
4932                                     {
4933                                        double dValue;
4934                                        double (*Set)(double) = (void *)prop.Set;
4935                                        GetDouble(member.initializer.exp, &dValue);
4936                                        exp.constant = PrintDouble(Set(dValue));
4937                                        exp.type = constantExp;
4938                                        break;
4939                                     }
4940                                  }
4941                               }
4942                            }
4943                         }
4944                         else if(!deepMember && type && _class.type == bitClass)
4945                         {
4946                            if(prop)
4947                            {
4948                               if(value.type == instanceExp && value.instance.data)
4949                               {
4950                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4951                                  bits = Set(value.instance.data);
4952                               }
4953                               else if(value.type == constantExp)
4954                               {
4955                               }
4956                            }
4957                            else if(dataMember)
4958                            {
4959                               BitMember bitMember = (BitMember) dataMember;
4960                               Type type;
4961                               int part = 0;
4962                               GetInt(value, &part);
4963                               bits = (bits & ~bitMember.mask);
4964                               if(!bitMember.dataType)
4965                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
4966
4967                               type = bitMember.dataType;
4968
4969                               if(type.kind == classType && type._class && type._class.registered)
4970                               {
4971                                  if(!type._class.registered.dataType)
4972                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4973                                  type = type._class.registered.dataType;
4974                               }
4975
4976                               switch(type.kind)
4977                               {
4978                                  case _BoolType:
4979                                  case charType:
4980                                     if(type.isSigned)
4981                                        bits |= ((char)part << bitMember.pos);
4982                                     else
4983                                        bits |= ((unsigned char)part << bitMember.pos);
4984                                     break;
4985                                  case shortType:
4986                                     if(type.isSigned)
4987                                        bits |= ((short)part << bitMember.pos);
4988                                     else
4989                                        bits |= ((unsigned short)part << bitMember.pos);
4990                                     break;
4991                                  case intType:
4992                                  case longType:
4993                                     if(type.isSigned)
4994                                        bits |= ((int)part << bitMember.pos);
4995                                     else
4996                                        bits |= ((unsigned int)part << bitMember.pos);
4997                                     break;
4998                                  case int64Type:
4999                                     if(type.isSigned)
5000                                        bits |= ((int64)part << bitMember.pos);
5001                                     else
5002                                        bits |= ((uint64)part << bitMember.pos);
5003                                     break;
5004                                  case intPtrType:
5005                                     if(type.isSigned)
5006                                     {
5007                                        bits |= ((intptr)part << bitMember.pos);
5008                                     }
5009                                     else
5010                                     {
5011                                        bits |= ((uintptr)part << bitMember.pos);
5012                                     }
5013                                     break;
5014                                  case intSizeType:
5015                                     if(type.isSigned)
5016                                     {
5017                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
5018                                     }
5019                                     else
5020                                     {
5021                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
5022                                     }
5023                                     break;
5024                               }
5025                            }
5026                         }
5027                      }
5028                      else
5029                      {
5030                         if(_class && _class.type == unitClass)
5031                         {
5032                            ComputeExpression(member.initializer.exp);
5033                            exp.constant = member.initializer.exp.constant;
5034                            exp.type = constantExp;
5035
5036                            member.initializer.exp.constant = null;
5037                         }
5038                      }
5039                   }
5040                }
5041                break;
5042             }
5043          }
5044       }
5045    }
5046    if(_class && _class.type == bitClass)
5047    {
5048       exp.constant = PrintHexUInt(bits);
5049       exp.type = constantExp;
5050    }
5051    if(exp.type != instanceExp)
5052    {
5053       FreeInstance(inst);
5054    }
5055 }
5056
5057 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5058 {
5059    bool result = false;
5060    switch(kind)
5061    {
5062       case shortType:
5063          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5064             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5065          break;
5066       case intType:
5067       case longType:
5068          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5069             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5070          break;
5071       case int64Type:
5072          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5073             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5074          result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5075          break;
5076       case floatType:
5077          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5078             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5079          result = GetOpFloat(op, &op.f);
5080          break;
5081       case doubleType:
5082          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5083             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5084          result = GetOpDouble(op, &op.d);
5085          break;
5086       case pointerType:
5087          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5088             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5089             result = GetOpUIntPtr(op, &op.ui64);
5090          break;
5091       case enumType:
5092          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5093             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5094             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5095          break;
5096       case intPtrType:
5097          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5098             result = isSigned ? GetOpIntPtr(op, &op.i64) : GetOpUIntPtr(op, &op.i64);
5099          break;
5100       case intSizeType:
5101          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5102             result = isSigned ? GetOpIntSize(op, &op.ui64) : GetOpUIntSize(op, &op.ui64);
5103          break;
5104    }
5105    return result;
5106 }
5107
5108 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5109 {
5110    if(exp.op.op == SIZEOF)
5111    {
5112       FreeExpContents(exp);
5113       exp.type = constantExp;
5114       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5115    }
5116    else
5117    {
5118       if(!exp.op.exp1)
5119       {
5120          switch(exp.op.op)
5121          {
5122             // unary arithmetic
5123             case '+':
5124             {
5125                // Provide default unary +
5126                Expression exp2 = exp.op.exp2;
5127                exp.op.exp2 = null;
5128                FreeExpContents(exp);
5129                FreeType(exp.expType);
5130                FreeType(exp.destType);
5131                *exp = *exp2;
5132                delete exp2;
5133                break;
5134             }
5135             case '-':
5136                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5137                break;
5138             // unary arithmetic increment and decrement
5139                   //OPERATOR_ALL(UNARY, ++, Inc)
5140                   //OPERATOR_ALL(UNARY, --, Dec)
5141             // unary bitwise
5142             case '~':
5143                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5144                break;
5145             // unary logical negation
5146             case '!':
5147                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5148                break;
5149          }
5150       }
5151       else
5152       {
5153          if(op1 && op2 && op1.kind != op2.kind)
5154          {
5155             if(Promote(op2, op1.kind, op1.type.isSigned))
5156                op2.kind = op1.kind, op2.ops = op1.ops;
5157             else if(Promote(op1, op2.kind, op2.type.isSigned))
5158                op1.kind = op2.kind, op1.ops = op2.ops;
5159          }
5160          switch(exp.op.op)
5161          {
5162             // binary arithmetic
5163             case '+':
5164                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5165                break;
5166             case '-':
5167                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5168                break;
5169             case '*':
5170                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5171                break;
5172             case '/':
5173                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5174                break;
5175             case '%':
5176                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5177                break;
5178             // binary arithmetic assignment
5179                   //OPERATOR_ALL(BINARY, =, Asign)
5180                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5181                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5182                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5183                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5184                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5185             // binary bitwise
5186             case '&':
5187                if(exp.op.exp2)
5188                {
5189                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5190                }
5191                break;
5192             case '|':
5193                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5194                break;
5195             case '^':
5196                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5197                break;
5198             case LEFT_OP:
5199                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5200                break;
5201             case RIGHT_OP:
5202                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5203                break;
5204             // binary bitwise assignment
5205                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5206                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5207                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5208                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5209                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5210             // binary logical equality
5211             case EQ_OP:
5212                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5213                break;
5214             case NE_OP:
5215                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5216                break;
5217             // binary logical
5218             case AND_OP:
5219                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5220                break;
5221             case OR_OP:
5222                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5223                break;
5224             // binary logical relational
5225             case '>':
5226                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5227                break;
5228             case '<':
5229                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5230                break;
5231             case GE_OP:
5232                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5233                break;
5234             case LE_OP:
5235                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5236                break;
5237          }
5238       }
5239    }
5240 }
5241
5242 void ComputeExpression(Expression exp)
5243 {
5244    char expString[10240];
5245    expString[0] = '\0';
5246 #ifdef _DEBUG
5247    PrintExpression(exp, expString);
5248 #endif
5249
5250    switch(exp.type)
5251    {
5252       case instanceExp:
5253       {
5254          ComputeInstantiation(exp);
5255          break;
5256       }
5257       /*
5258       case constantExp:
5259          break;
5260       */
5261       case opExp:
5262       {
5263          Expression exp1, exp2 = null;
5264          Operand op1 { };
5265          Operand op2 { };
5266
5267          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5268          if(exp.op.exp2)
5269          {
5270             Expression e = exp.op.exp2;
5271
5272             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5273             {
5274                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5275                {
5276                   if(e.type == extensionCompoundExp)
5277                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5278                   else
5279                      e = e.list->last;
5280                }
5281             }
5282             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5283             {
5284                if(e.type == stringExp && e.string)
5285                {
5286                   char * string = e.string;
5287                   int len = strlen(string);
5288                   char * tmp = new char[len-2+1];
5289                   len = UnescapeString(tmp, string + 1, len - 2);
5290                   delete tmp;
5291                   FreeExpContents(exp);
5292                   exp.type = constantExp;
5293                   exp.constant = PrintUInt(len + 1);
5294                }
5295                else
5296                {
5297                   Type type = e.expType;
5298                   type.refCount++;
5299                   FreeExpContents(exp);
5300                   exp.type = constantExp;
5301                   exp.constant = PrintUInt(ComputeTypeSize(type));
5302                   FreeType(type);
5303                }
5304                break;
5305             }
5306             else
5307                ComputeExpression(exp.op.exp2);
5308          }
5309          if(exp.op.exp1)
5310          {
5311             ComputeExpression(exp.op.exp1);
5312             exp1 = exp.op.exp1;
5313             exp2 = exp.op.exp2;
5314             op1 = GetOperand(exp1);
5315             if(op1.type) op1.type.refCount++;
5316             if(exp2)
5317             {
5318                op2 = GetOperand(exp2);
5319                if(op2.type) op2.type.refCount++;
5320             }
5321          }
5322          else
5323          {
5324             exp1 = exp.op.exp2;
5325             op1 = GetOperand(exp1);
5326             if(op1.type) op1.type.refCount++;
5327          }
5328
5329          CallOperator(exp, exp1, exp2, op1, op2);
5330          /*
5331          switch(exp.op.op)
5332          {
5333             // Unary operators
5334             case '&':
5335                // Also binary
5336                if(exp.op.exp1 && exp.op.exp2)
5337                {
5338                   // Binary And
5339                   if(op1.ops.BitAnd)
5340                   {
5341                      FreeExpContents(exp);
5342                      op1.ops.BitAnd(exp, op1, op2);
5343                   }
5344                }
5345                break;
5346             case '*':
5347                if(exp.op.exp1)
5348                {
5349                   if(op1.ops.Mul)
5350                   {
5351                      FreeExpContents(exp);
5352                      op1.ops.Mul(exp, op1, op2);
5353                   }
5354                }
5355                break;
5356             case '+':
5357                if(exp.op.exp1)
5358                {
5359                   if(op1.ops.Add)
5360                   {
5361                      FreeExpContents(exp);
5362                      op1.ops.Add(exp, op1, op2);
5363                   }
5364                }
5365                else
5366                {
5367                   // Provide default unary +
5368                   Expression exp2 = exp.op.exp2;
5369                   exp.op.exp2 = null;
5370                   FreeExpContents(exp);
5371                   FreeType(exp.expType);
5372                   FreeType(exp.destType);
5373
5374                   *exp = *exp2;
5375                   delete exp2;
5376                }
5377                break;
5378             case '-':
5379                if(exp.op.exp1)
5380                {
5381                   if(op1.ops.Sub)
5382                   {
5383                      FreeExpContents(exp);
5384                      op1.ops.Sub(exp, op1, op2);
5385                   }
5386                }
5387                else
5388                {
5389                   if(op1.ops.Neg)
5390                   {
5391                      FreeExpContents(exp);
5392                      op1.ops.Neg(exp, op1);
5393                   }
5394                }
5395                break;
5396             case '~':
5397                if(op1.ops.BitNot)
5398                {
5399                   FreeExpContents(exp);
5400                   op1.ops.BitNot(exp, op1);
5401                }
5402                break;
5403             case '!':
5404                if(op1.ops.Not)
5405                {
5406                   FreeExpContents(exp);
5407                   op1.ops.Not(exp, op1);
5408                }
5409                break;
5410             // Binary only operators
5411             case '/':
5412                if(op1.ops.Div)
5413                {
5414                   FreeExpContents(exp);
5415                   op1.ops.Div(exp, op1, op2);
5416                }
5417                break;
5418             case '%':
5419                if(op1.ops.Mod)
5420                {
5421                   FreeExpContents(exp);
5422                   op1.ops.Mod(exp, op1, op2);
5423                }
5424                break;
5425             case LEFT_OP:
5426                break;
5427             case RIGHT_OP:
5428                break;
5429             case '<':
5430                if(exp.op.exp1)
5431                {
5432                   if(op1.ops.Sma)
5433                   {
5434                      FreeExpContents(exp);
5435                      op1.ops.Sma(exp, op1, op2);
5436                   }
5437                }
5438                break;
5439             case '>':
5440                if(exp.op.exp1)
5441                {
5442                   if(op1.ops.Grt)
5443                   {
5444                      FreeExpContents(exp);
5445                      op1.ops.Grt(exp, op1, op2);
5446                   }
5447                }
5448                break;
5449             case LE_OP:
5450                if(exp.op.exp1)
5451                {
5452                   if(op1.ops.SmaEqu)
5453                   {
5454                      FreeExpContents(exp);
5455                      op1.ops.SmaEqu(exp, op1, op2);
5456                   }
5457                }
5458                break;
5459             case GE_OP:
5460                if(exp.op.exp1)
5461                {
5462                   if(op1.ops.GrtEqu)
5463                   {
5464                      FreeExpContents(exp);
5465                      op1.ops.GrtEqu(exp, op1, op2);
5466                   }
5467                }
5468                break;
5469             case EQ_OP:
5470                if(exp.op.exp1)
5471                {
5472                   if(op1.ops.Equ)
5473                   {
5474                      FreeExpContents(exp);
5475                      op1.ops.Equ(exp, op1, op2);
5476                   }
5477                }
5478                break;
5479             case NE_OP:
5480                if(exp.op.exp1)
5481                {
5482                   if(op1.ops.Nqu)
5483                   {
5484                      FreeExpContents(exp);
5485                      op1.ops.Nqu(exp, op1, op2);
5486                   }
5487                }
5488                break;
5489             case '|':
5490                if(op1.ops.BitOr)
5491                {
5492                   FreeExpContents(exp);
5493                   op1.ops.BitOr(exp, op1, op2);
5494                }
5495                break;
5496             case '^':
5497                if(op1.ops.BitXor)
5498                {
5499                   FreeExpContents(exp);
5500                   op1.ops.BitXor(exp, op1, op2);
5501                }
5502                break;
5503             case AND_OP:
5504                break;
5505             case OR_OP:
5506                break;
5507             case SIZEOF:
5508                FreeExpContents(exp);
5509                exp.type = constantExp;
5510                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5511                break;
5512          }
5513          */
5514          if(op1.type) FreeType(op1.type);
5515          if(op2.type) FreeType(op2.type);
5516          break;
5517       }
5518       case bracketsExp:
5519       case extensionExpressionExp:
5520       {
5521          Expression e, n;
5522          for(e = exp.list->first; e; e = n)
5523          {
5524             n = e.next;
5525             if(!n)
5526             {
5527                OldList * list = exp.list;
5528                ComputeExpression(e);
5529                //FreeExpContents(exp);
5530                FreeType(exp.expType);
5531                FreeType(exp.destType);
5532                *exp = *e;
5533                delete e;
5534                delete list;
5535             }
5536             else
5537             {
5538                FreeExpression(e);
5539             }
5540          }
5541          break;
5542       }
5543       /*
5544
5545       case ExpIndex:
5546       {
5547          Expression e;
5548          exp.isConstant = true;
5549
5550          ComputeExpression(exp.index.exp);
5551          if(!exp.index.exp.isConstant)
5552             exp.isConstant = false;
5553
5554          for(e = exp.index.index->first; e; e = e.next)
5555          {
5556             ComputeExpression(e);
5557             if(!e.next)
5558             {
5559                // Check if this type is int
5560             }
5561             if(!e.isConstant)
5562                exp.isConstant = false;
5563          }
5564          exp.expType = Dereference(exp.index.exp.expType);
5565          break;
5566       }
5567       */
5568       case memberExp:
5569       {
5570          Expression memberExp = exp.member.exp;
5571          Identifier memberID = exp.member.member;
5572
5573          Type type;
5574          ComputeExpression(exp.member.exp);
5575          type = exp.member.exp.expType;
5576          if(type)
5577          {
5578             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);
5579             Property prop = null;
5580             DataMember member = null;
5581             Class convertTo = null;
5582             if(type.kind == subClassType && exp.member.exp.type == classExp)
5583                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5584
5585             if(!_class)
5586             {
5587                char string[256];
5588                Symbol classSym;
5589                string[0] = '\0';
5590                PrintTypeNoConst(type, string, false, true);
5591                classSym = FindClass(string);
5592                _class = classSym ? classSym.registered : null;
5593             }
5594
5595             if(exp.member.member)
5596             {
5597                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5598                if(!prop)
5599                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5600             }
5601             if(!prop && !member && _class && exp.member.member)
5602             {
5603                Symbol classSym = FindClass(exp.member.member.string);
5604                convertTo = _class;
5605                _class = classSym ? classSym.registered : null;
5606                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5607             }
5608
5609             if(prop)
5610             {
5611                if(prop.compiled)
5612                {
5613                   Type type = prop.dataType;
5614                   // TODO: Assuming same base type for units...
5615                   if(_class.type == unitClass)
5616                   {
5617                      if(type.kind == classType)
5618                      {
5619                         Class _class = type._class.registered;
5620                         if(_class.type == unitClass)
5621                         {
5622                            if(!_class.dataType)
5623                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5624                            type = _class.dataType;
5625                         }
5626                      }
5627                      switch(type.kind)
5628                      {
5629                         case floatType:
5630                         {
5631                            float value;
5632                            float (*Get)(float) = (void *)prop.Get;
5633                            GetFloat(exp.member.exp, &value);
5634                            exp.constant = PrintFloat(Get ? Get(value) : value);
5635                            exp.type = constantExp;
5636                            break;
5637                         }
5638                         case doubleType:
5639                         {
5640                            double value;
5641                            double (*Get)(double);
5642                            GetDouble(exp.member.exp, &value);
5643
5644                            if(convertTo)
5645                               Get = (void *)prop.Set;
5646                            else
5647                               Get = (void *)prop.Get;
5648                            exp.constant = PrintDouble(Get ? Get(value) : value);
5649                            exp.type = constantExp;
5650                            break;
5651                         }
5652                      }
5653                   }
5654                   else
5655                   {
5656                      if(convertTo)
5657                      {
5658                         Expression value = exp.member.exp;
5659                         Type type;
5660                         if(!prop.dataType)
5661                            ProcessPropertyType(prop);
5662
5663                         type = prop.dataType;
5664                         if(!type)
5665                         {
5666                             // printf("Investigate this\n");
5667                         }
5668                         else if(_class.type == structClass)
5669                         {
5670                            switch(type.kind)
5671                            {
5672                               case classType:
5673                               {
5674                                  Class propertyClass = type._class.registered;
5675                                  if(propertyClass.type == structClass && value.type == instanceExp)
5676                                  {
5677                                     void (*Set)(void *, void *) = (void *)prop.Set;
5678                                     exp.instance = Instantiation { };
5679                                     exp.instance.data = new0 byte[_class.structSize];
5680                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5681                                     exp.instance.loc = exp.loc;
5682                                     exp.type = instanceExp;
5683                                     Set(exp.instance.data, value.instance.data);
5684                                     PopulateInstance(exp.instance);
5685                                  }
5686                                  break;
5687                               }
5688                               case intType:
5689                               {
5690                                  int intValue;
5691                                  void (*Set)(void *, int) = (void *)prop.Set;
5692
5693                                  exp.instance = Instantiation { };
5694                                  exp.instance.data = new0 byte[_class.structSize];
5695                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5696                                  exp.instance.loc = exp.loc;
5697                                  exp.type = instanceExp;
5698
5699                                  GetInt(value, &intValue);
5700
5701                                  Set(exp.instance.data, intValue);
5702                                  PopulateInstance(exp.instance);
5703                                  break;
5704                               }
5705                               case int64Type:
5706                               {
5707                                  int64 intValue;
5708                                  void (*Set)(void *, int64) = (void *)prop.Set;
5709
5710                                  exp.instance = Instantiation { };
5711                                  exp.instance.data = new0 byte[_class.structSize];
5712                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5713                                  exp.instance.loc = exp.loc;
5714                                  exp.type = instanceExp;
5715
5716                                  GetInt64(value, &intValue);
5717
5718                                  Set(exp.instance.data, intValue);
5719                                  PopulateInstance(exp.instance);
5720                                  break;
5721                               }
5722                               case intPtrType:
5723                               {
5724                                  // TOFIX:
5725                                  intptr intValue;
5726                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5727
5728                                  exp.instance = Instantiation { };
5729                                  exp.instance.data = new0 byte[_class.structSize];
5730                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5731                                  exp.instance.loc = exp.loc;
5732                                  exp.type = instanceExp;
5733
5734                                  GetIntPtr(value, &intValue);
5735
5736                                  Set(exp.instance.data, intValue);
5737                                  PopulateInstance(exp.instance);
5738                                  break;
5739                               }
5740                               case intSizeType:
5741                               {
5742                                  // TOFIX:
5743                                  intsize intValue;
5744                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5745
5746                                  exp.instance = Instantiation { };
5747                                  exp.instance.data = new0 byte[_class.structSize];
5748                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5749                                  exp.instance.loc = exp.loc;
5750                                  exp.type = instanceExp;
5751
5752                                  GetIntSize(value, &intValue);
5753
5754                                  Set(exp.instance.data, intValue);
5755                                  PopulateInstance(exp.instance);
5756                                  break;
5757                               }
5758                               case doubleType:
5759                               {
5760                                  double doubleValue;
5761                                  void (*Set)(void *, double) = (void *)prop.Set;
5762
5763                                  exp.instance = Instantiation { };
5764                                  exp.instance.data = new0 byte[_class.structSize];
5765                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5766                                  exp.instance.loc = exp.loc;
5767                                  exp.type = instanceExp;
5768
5769                                  GetDouble(value, &doubleValue);
5770
5771                                  Set(exp.instance.data, doubleValue);
5772                                  PopulateInstance(exp.instance);
5773                                  break;
5774                               }
5775                            }
5776                         }
5777                         else if(_class.type == bitClass)
5778                         {
5779                            switch(type.kind)
5780                            {
5781                               case classType:
5782                               {
5783                                  Class propertyClass = type._class.registered;
5784                                  if(propertyClass.type == structClass && value.instance.data)
5785                                  {
5786                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5787                                     unsigned int bits = Set(value.instance.data);
5788                                     exp.constant = PrintHexUInt(bits);
5789                                     exp.type = constantExp;
5790                                     break;
5791                                  }
5792                                  else if(_class.type == bitClass)
5793                                  {
5794                                     unsigned int value;
5795                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5796                                     unsigned int bits;
5797
5798                                     GetUInt(exp.member.exp, &value);
5799                                     bits = Set(value);
5800                                     exp.constant = PrintHexUInt(bits);
5801                                     exp.type = constantExp;
5802                                  }
5803                               }
5804                            }
5805                         }
5806                      }
5807                      else
5808                      {
5809                         if(_class.type == bitClass)
5810                         {
5811                            unsigned int value;
5812                            GetUInt(exp.member.exp, &value);
5813
5814                            switch(type.kind)
5815                            {
5816                               case classType:
5817                               {
5818                                  Class _class = type._class.registered;
5819                                  if(_class.type == structClass)
5820                                  {
5821                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5822
5823                                     exp.instance = Instantiation { };
5824                                     exp.instance.data = new0 byte[_class.structSize];
5825                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5826                                     exp.instance.loc = exp.loc;
5827                                     //exp.instance.fullSet = true;
5828                                     exp.type = instanceExp;
5829                                     Get(value, exp.instance.data);
5830                                     PopulateInstance(exp.instance);
5831                                  }
5832                                  else if(_class.type == bitClass)
5833                                  {
5834                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5835                                     uint64 bits = Get(value);
5836                                     exp.constant = PrintHexUInt64(bits);
5837                                     exp.type = constantExp;
5838                                  }
5839                                  break;
5840                               }
5841                            }
5842                         }
5843                         else if(_class.type == structClass)
5844                         {
5845                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5846                            switch(type.kind)
5847                            {
5848                               case classType:
5849                               {
5850                                  Class _class = type._class.registered;
5851                                  if(_class.type == structClass && value)
5852                                  {
5853                                     void (*Get)(void *, void *) = (void *)prop.Get;
5854
5855                                     exp.instance = Instantiation { };
5856                                     exp.instance.data = new0 byte[_class.structSize];
5857                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5858                                     exp.instance.loc = exp.loc;
5859                                     //exp.instance.fullSet = true;
5860                                     exp.type = instanceExp;
5861                                     Get(value, exp.instance.data);
5862                                     PopulateInstance(exp.instance);
5863                                  }
5864                                  break;
5865                               }
5866                            }
5867                         }
5868                         /*else
5869                         {
5870                            char * value = exp.member.exp.instance.data;
5871                            switch(type.kind)
5872                            {
5873                               case classType:
5874                               {
5875                                  Class _class = type._class.registered;
5876                                  if(_class.type == normalClass)
5877                                  {
5878                                     void *(*Get)(void *) = (void *)prop.Get;
5879
5880                                     exp.instance = Instantiation { };
5881                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5882                                     exp.type = instanceExp;
5883                                     exp.instance.data = Get(value, exp.instance.data);
5884                                  }
5885                                  break;
5886                               }
5887                            }
5888                         }
5889                         */
5890                      }
5891                   }
5892                }
5893                else
5894                {
5895                   exp.isConstant = false;
5896                }
5897             }
5898             else if(member)
5899             {
5900             }
5901          }
5902
5903          if(exp.type != ExpressionType::memberExp)
5904          {
5905             FreeExpression(memberExp);
5906             FreeIdentifier(memberID);
5907          }
5908          break;
5909       }
5910       case typeSizeExp:
5911       {
5912          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5913          FreeExpContents(exp);
5914          exp.constant = PrintUInt(ComputeTypeSize(type));
5915          exp.type = constantExp;
5916          FreeType(type);
5917          break;
5918       }
5919       case classSizeExp:
5920       {
5921          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5922          if(classSym && classSym.registered)
5923          {
5924             if(classSym.registered.fixed)
5925             {
5926                FreeSpecifier(exp._class);
5927                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5928                exp.type = constantExp;
5929             }
5930             else
5931             {
5932                char className[1024];
5933                strcpy(className, "__ecereClass_");
5934                FullClassNameCat(className, classSym.string, true);
5935                MangleClassName(className);
5936
5937                DeclareClass(classSym, className);
5938
5939                FreeExpContents(exp);
5940                exp.type = pointerExp;
5941                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5942                exp.member.member = MkIdentifier("structSize");
5943             }
5944          }
5945          break;
5946       }
5947       case castExp:
5948       //case constantExp:
5949       {
5950          Type type;
5951          Expression e = exp;
5952          if(exp.type == castExp)
5953          {
5954             if(exp.cast.exp)
5955                ComputeExpression(exp.cast.exp);
5956             e = exp.cast.exp;
5957          }
5958          if(e && exp.expType)
5959          {
5960             /*if(exp.destType)
5961                type = exp.destType;
5962             else*/
5963                type = exp.expType;
5964             if(type.kind == classType)
5965             {
5966                Class _class = type._class.registered;
5967                if(_class && (_class.type == unitClass || _class.type == bitClass))
5968                {
5969                   if(!_class.dataType)
5970                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5971                   type = _class.dataType;
5972                }
5973             }
5974
5975             switch(type.kind)
5976             {
5977                case _BoolType:
5978                case charType:
5979                   if(type.isSigned)
5980                   {
5981                      char value;
5982                      GetChar(e, &value);
5983                      FreeExpContents(exp);
5984                      exp.constant = PrintChar(value);
5985                      exp.type = constantExp;
5986                   }
5987                   else
5988                   {
5989                      unsigned char value;
5990                      GetUChar(e, &value);
5991                      FreeExpContents(exp);
5992                      exp.constant = PrintUChar(value);
5993                      exp.type = constantExp;
5994                   }
5995                   break;
5996                case shortType:
5997                   if(type.isSigned)
5998                   {
5999                      short value;
6000                      GetShort(e, &value);
6001                      FreeExpContents(exp);
6002                      exp.constant = PrintShort(value);
6003                      exp.type = constantExp;
6004                   }
6005                   else
6006                   {
6007                      unsigned short value;
6008                      GetUShort(e, &value);
6009                      FreeExpContents(exp);
6010                      exp.constant = PrintUShort(value);
6011                      exp.type = constantExp;
6012                   }
6013                   break;
6014                case intType:
6015                   if(type.isSigned)
6016                   {
6017                      int value;
6018                      GetInt(e, &value);
6019                      FreeExpContents(exp);
6020                      exp.constant = PrintInt(value);
6021                      exp.type = constantExp;
6022                   }
6023                   else
6024                   {
6025                      unsigned int value;
6026                      GetUInt(e, &value);
6027                      FreeExpContents(exp);
6028                      exp.constant = PrintUInt(value);
6029                      exp.type = constantExp;
6030                   }
6031                   break;
6032                case int64Type:
6033                   if(type.isSigned)
6034                   {
6035                      int64 value;
6036                      GetInt64(e, &value);
6037                      FreeExpContents(exp);
6038                      exp.constant = PrintInt64(value);
6039                      exp.type = constantExp;
6040                   }
6041                   else
6042                   {
6043                      uint64 value;
6044                      GetUInt64(e, &value);
6045                      FreeExpContents(exp);
6046                      exp.constant = PrintUInt64(value);
6047                      exp.type = constantExp;
6048                   }
6049                   break;
6050                case intPtrType:
6051                   if(type.isSigned)
6052                   {
6053                      intptr value;
6054                      GetIntPtr(e, &value);
6055                      FreeExpContents(exp);
6056                      exp.constant = PrintInt64((int64)value);
6057                      exp.type = constantExp;
6058                   }
6059                   else
6060                   {
6061                      uintptr value;
6062                      GetUIntPtr(e, &value);
6063                      FreeExpContents(exp);
6064                      exp.constant = PrintUInt64((uint64)value);
6065                      exp.type = constantExp;
6066                   }
6067                   break;
6068                case intSizeType:
6069                   if(type.isSigned)
6070                   {
6071                      intsize value;
6072                      GetIntSize(e, &value);
6073                      FreeExpContents(exp);
6074                      exp.constant = PrintInt64((int64)value);
6075                      exp.type = constantExp;
6076                   }
6077                   else
6078                   {
6079                      uintsize value;
6080                      GetUIntSize(e, &value);
6081                      FreeExpContents(exp);
6082                      exp.constant = PrintUInt64((uint64)value);
6083                      exp.type = constantExp;
6084                   }
6085                   break;
6086                case floatType:
6087                {
6088                   float value;
6089                   GetFloat(e, &value);
6090                   FreeExpContents(exp);
6091                   exp.constant = PrintFloat(value);
6092                   exp.type = constantExp;
6093                   break;
6094                }
6095                case doubleType:
6096                {
6097                   double value;
6098                   GetDouble(e, &value);
6099                   FreeExpContents(exp);
6100                   exp.constant = PrintDouble(value);
6101                   exp.type = constantExp;
6102                   break;
6103                }
6104             }
6105          }
6106          break;
6107       }
6108       case conditionExp:
6109       {
6110          Operand op1 { };
6111          Operand op2 { };
6112          Operand op3 { };
6113
6114          if(exp.cond.exp)
6115             // Caring only about last expression for now...
6116             ComputeExpression(exp.cond.exp->last);
6117          if(exp.cond.elseExp)
6118             ComputeExpression(exp.cond.elseExp);
6119          if(exp.cond.cond)
6120             ComputeExpression(exp.cond.cond);
6121
6122          op1 = GetOperand(exp.cond.cond);
6123          if(op1.type) op1.type.refCount++;
6124          op2 = GetOperand(exp.cond.exp->last);
6125          if(op2.type) op2.type.refCount++;
6126          op3 = GetOperand(exp.cond.elseExp);
6127          if(op3.type) op3.type.refCount++;
6128
6129          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6130          if(op1.type) FreeType(op1.type);
6131          if(op2.type) FreeType(op2.type);
6132          if(op3.type) FreeType(op3.type);
6133          break;
6134       }
6135    }
6136 }
6137
6138 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
6139 {
6140    bool result = true;
6141    if(destType)
6142    {
6143       OldList converts { };
6144       Conversion convert;
6145
6146       if(destType.kind == voidType)
6147          return false;
6148
6149       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
6150          result = false;
6151       if(converts.count)
6152       {
6153          // for(convert = converts.last; convert; convert = convert.prev)
6154          for(convert = converts.first; convert; convert = convert.next)
6155          {
6156             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6157             if(!empty)
6158             {
6159                Expression newExp { };
6160                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6161
6162                // TODO: Check this...
6163                *newExp = *exp;
6164                newExp.destType = null;
6165
6166                if(convert.isGet)
6167                {
6168                   // [exp].ColorRGB
6169                   exp.type = memberExp;
6170                   exp.addedThis = true;
6171                   exp.member.exp = newExp;
6172                   FreeType(exp.member.exp.expType);
6173
6174                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6175                   exp.member.exp.expType.classObjectType = objectType;
6176                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6177                   exp.member.memberType = propertyMember;
6178                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6179                   // TESTING THIS... for (int)degrees
6180                   exp.needCast = true;
6181                   if(exp.expType) exp.expType.refCount++;
6182                   ApplyAnyObjectLogic(exp.member.exp);
6183                }
6184                else
6185                {
6186
6187                   /*if(exp.isConstant)
6188                   {
6189                      // Color { ColorRGB = [exp] };
6190                      exp.type = instanceExp;
6191                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6192                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6193                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6194                   }
6195                   else*/
6196                   {
6197                      // If not constant, don't turn it yet into an instantiation
6198                      // (Go through the deep members system first)
6199                      exp.type = memberExp;
6200                      exp.addedThis = true;
6201                      exp.member.exp = newExp;
6202
6203                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6204                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6205                         newExp.expType._class.registered.type == noHeadClass)
6206                      {
6207                         newExp.byReference = true;
6208                      }
6209
6210                      FreeType(exp.member.exp.expType);
6211                      /*exp.member.exp.expType = convert.convert.dataType;
6212                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6213                      exp.member.exp.expType = null;
6214                      if(convert.convert.dataType)
6215                      {
6216                         exp.member.exp.expType = { };
6217                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6218                         exp.member.exp.expType.refCount = 1;
6219                         exp.member.exp.expType.classObjectType = objectType;
6220                         ApplyAnyObjectLogic(exp.member.exp);
6221                      }
6222
6223                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6224                      exp.member.memberType = reverseConversionMember;
6225                      exp.expType = convert.resultType ? convert.resultType :
6226                         MkClassType(convert.convert._class.fullName);
6227                      exp.needCast = true;
6228                      if(convert.resultType) convert.resultType.refCount++;
6229                   }
6230                }
6231             }
6232             else
6233             {
6234                FreeType(exp.expType);
6235                if(convert.isGet)
6236                {
6237                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6238                   exp.needCast = true;
6239                   if(exp.expType) exp.expType.refCount++;
6240                }
6241                else
6242                {
6243                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6244                   exp.needCast = true;
6245                   if(convert.resultType)
6246                      convert.resultType.refCount++;
6247                }
6248             }
6249          }
6250          if(exp.isConstant && inCompiler)
6251             ComputeExpression(exp);
6252
6253          converts.Free(FreeConvert);
6254       }
6255
6256       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6257       {
6258          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6259       }
6260       if(!result && exp.expType && exp.destType)
6261       {
6262          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6263              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6264             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6265             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6266             result = true;
6267       }
6268    }
6269    // if(result) CheckTemplateTypes(exp);
6270    return result;
6271 }
6272
6273 void CheckTemplateTypes(Expression exp)
6274 {
6275    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6276    {
6277       Expression newExp { };
6278       Statement compound;
6279       Context context;
6280       *newExp = *exp;
6281       if(exp.destType) exp.destType.refCount++;
6282       if(exp.expType)  exp.expType.refCount++;
6283       newExp.prev = null;
6284       newExp.next = null;
6285
6286       switch(exp.expType.kind)
6287       {
6288          case doubleType:
6289             if(exp.destType.classObjectType)
6290             {
6291                // We need to pass the address, just pass it along (Undo what was done above)
6292                if(exp.destType) exp.destType.refCount--;
6293                if(exp.expType)  exp.expType.refCount--;
6294                delete newExp;
6295             }
6296             else
6297             {
6298                // If we're looking for value:
6299                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6300                OldList * specs;
6301                OldList * unionDefs = MkList();
6302                OldList * statements = MkList();
6303                context = PushContext();
6304                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6305                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6306                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6307                exp.type = extensionCompoundExp;
6308                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6309                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6310                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6311                exp.compound.compound.context = context;
6312                PopContext(context);
6313             }
6314             break;
6315          default:
6316             exp.type = castExp;
6317             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6318             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6319             break;
6320       }
6321    }
6322    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6323    {
6324       Expression newExp { };
6325       Statement compound;
6326       Context context;
6327       *newExp = *exp;
6328       if(exp.destType) exp.destType.refCount++;
6329       if(exp.expType)  exp.expType.refCount++;
6330       newExp.prev = null;
6331       newExp.next = null;
6332
6333       switch(exp.expType.kind)
6334       {
6335          case doubleType:
6336             if(exp.destType.classObjectType)
6337             {
6338                // We need to pass the address, just pass it along (Undo what was done above)
6339                if(exp.destType) exp.destType.refCount--;
6340                if(exp.expType)  exp.expType.refCount--;
6341                delete newExp;
6342             }
6343             else
6344             {
6345                // If we're looking for value:
6346                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6347                OldList * specs;
6348                OldList * unionDefs = MkList();
6349                OldList * statements = MkList();
6350                context = PushContext();
6351                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6352                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6353                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6354                exp.type = extensionCompoundExp;
6355                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6356                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6357                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6358                exp.compound.compound.context = context;
6359                PopContext(context);
6360             }
6361             break;
6362          case classType:
6363          {
6364             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6365             {
6366                exp.type = bracketsExp;
6367                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6368                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6369                ProcessExpressionType(exp.list->first);
6370                break;
6371             }
6372             else
6373             {
6374                exp.type = bracketsExp;
6375                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6376                newExp.needCast = true;
6377                ProcessExpressionType(exp.list->first);
6378                break;
6379             }
6380          }
6381          default:
6382          {
6383             if(exp.expType.kind == templateType)
6384             {
6385                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6386                if(type)
6387                {
6388                   FreeType(exp.destType);
6389                   FreeType(exp.expType);
6390                   delete newExp;
6391                   break;
6392                }
6393             }
6394             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6395             {
6396                exp.type = opExp;
6397                exp.op.op = '*';
6398                exp.op.exp1 = null;
6399                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6400                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6401             }
6402             else
6403             {
6404                char typeString[1024];
6405                Declarator decl;
6406                OldList * specs = MkList();
6407                typeString[0] = '\0';
6408                PrintType(exp.expType, typeString, false, false);
6409                decl = SpecDeclFromString(typeString, specs, null);
6410
6411                exp.type = castExp;
6412                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6413                exp.cast.typeName = MkTypeName(specs, decl);
6414                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6415                exp.cast.exp.needCast = true;
6416             }
6417             break;
6418          }
6419       }
6420    }
6421 }
6422 // TODO: The Symbol tree should be reorganized by namespaces
6423 // Name Space:
6424 //    - Tree of all symbols within (stored without namespace)
6425 //    - Tree of sub-namespaces
6426
6427 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6428 {
6429    int nsLen = strlen(nameSpace);
6430    Symbol symbol;
6431    // Start at the name space prefix
6432    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6433    {
6434       char * s = symbol.string;
6435       if(!strncmp(s, nameSpace, nsLen))
6436       {
6437          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6438          int c;
6439          char * namePart;
6440          for(c = strlen(s)-1; c >= 0; c--)
6441             if(s[c] == ':')
6442                break;
6443
6444          namePart = s+c+1;
6445          if(!strcmp(namePart, name))
6446          {
6447             // TODO: Error on ambiguity
6448             return symbol;
6449          }
6450       }
6451       else
6452          break;
6453    }
6454    return null;
6455 }
6456
6457 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6458 {
6459    int c;
6460    char nameSpace[1024];
6461    char * namePart;
6462    bool gotColon = false;
6463
6464    nameSpace[0] = '\0';
6465    for(c = strlen(name)-1; c >= 0; c--)
6466       if(name[c] == ':')
6467       {
6468          gotColon = true;
6469          break;
6470       }
6471
6472    namePart = name+c+1;
6473    while(c >= 0 && name[c] == ':') c--;
6474    if(c >= 0)
6475    {
6476       // Try an exact match first
6477       Symbol symbol = (Symbol)tree.FindString(name);
6478       if(symbol)
6479          return symbol;
6480
6481       // Namespace specified
6482       memcpy(nameSpace, name, c + 1);
6483       nameSpace[c+1] = 0;
6484
6485       return ScanWithNameSpace(tree, nameSpace, namePart);
6486    }
6487    else if(gotColon)
6488    {
6489       // Looking for a global symbol, e.g. ::Sleep()
6490       Symbol symbol = (Symbol)tree.FindString(namePart);
6491       return symbol;
6492    }
6493    else
6494    {
6495       // Name only (no namespace specified)
6496       Symbol symbol = (Symbol)tree.FindString(namePart);
6497       if(symbol)
6498          return symbol;
6499       return ScanWithNameSpace(tree, "", namePart);
6500    }
6501    return null;
6502 }
6503
6504 static void ProcessDeclaration(Declaration decl);
6505
6506 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6507 {
6508 #ifdef _DEBUG
6509    //Time startTime = GetTime();
6510 #endif
6511    // Optimize this later? Do this before/less?
6512    Context ctx;
6513    Symbol symbol = null;
6514    // First, check if the identifier is declared inside the function
6515    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6516
6517    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6518    {
6519       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6520       {
6521          symbol = null;
6522          if(thisNameSpace)
6523          {
6524             char curName[1024];
6525             strcpy(curName, thisNameSpace);
6526             strcat(curName, "::");
6527             strcat(curName, name);
6528             // Try to resolve in current namespace first
6529             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6530          }
6531          if(!symbol)
6532             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6533       }
6534       else
6535          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6536
6537       if(symbol || ctx == endContext) break;
6538    }
6539    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6540    {
6541       if(symbol.pointerExternal.type == functionExternal)
6542       {
6543          FunctionDefinition function = symbol.pointerExternal.function;
6544
6545          // Modified this recently...
6546          Context tmpContext = curContext;
6547          curContext = null;
6548          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6549          curContext = tmpContext;
6550
6551          symbol.pointerExternal.symbol = symbol;
6552
6553          // TESTING THIS:
6554          DeclareType(symbol.type, true, true);
6555
6556          ast->Insert(curExternal.prev, symbol.pointerExternal);
6557
6558          symbol.id = curExternal.symbol.idCode;
6559
6560       }
6561       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6562       {
6563          ast->Move(symbol.pointerExternal, curExternal.prev);
6564          symbol.id = curExternal.symbol.idCode;
6565       }
6566    }
6567 #ifdef _DEBUG
6568    //findSymbolTotalTime += GetTime() - startTime;
6569 #endif
6570    return symbol;
6571 }
6572
6573 static void GetTypeSpecs(Type type, OldList * specs)
6574 {
6575    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6576    switch(type.kind)
6577    {
6578       case classType:
6579       {
6580          if(type._class.registered)
6581          {
6582             if(!type._class.registered.dataType)
6583                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6584             GetTypeSpecs(type._class.registered.dataType, specs);
6585          }
6586          break;
6587       }
6588       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6589       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6590       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6591       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6592       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6593       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6594       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6595       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6596       case intType:
6597       default:
6598          ListAdd(specs, MkSpecifier(INT)); break;
6599    }
6600 }
6601
6602 static void PrintArraySize(Type arrayType, char * string)
6603 {
6604    char size[256];
6605    size[0] = '\0';
6606    strcat(size, "[");
6607    if(arrayType.enumClass)
6608       strcat(size, arrayType.enumClass.string);
6609    else if(arrayType.arraySizeExp)
6610       PrintExpression(arrayType.arraySizeExp, size);
6611    strcat(size, "]");
6612    strcat(string, size);
6613 }
6614
6615 // WARNING : This function expects a null terminated string since it recursively concatenate...
6616 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6617 {
6618    if(type)
6619    {
6620       if(printConst && type.constant)
6621          strcat(string, "const ");
6622       switch(type.kind)
6623       {
6624          case classType:
6625          {
6626             Symbol c = type._class;
6627             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6628             //       look into merging with thisclass ?
6629             if(type.classObjectType == typedObject)
6630                strcat(string, "typed_object");
6631             else if(type.classObjectType == anyObject)
6632                strcat(string, "any_object");
6633             else
6634             {
6635                if(c && c.string)
6636                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6637             }
6638             if(type.byReference)
6639                strcat(string, " &");
6640             break;
6641          }
6642          case voidType: strcat(string, "void"); break;
6643          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6644          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6645          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6646          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6647          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6648          case _BoolType: strcat(string, "_Bool"); break;
6649          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6650          case floatType: strcat(string, "float"); break;
6651          case doubleType: strcat(string, "double"); break;
6652          case structType:
6653             if(type.enumName)
6654             {
6655                strcat(string, "struct ");
6656                strcat(string, type.enumName);
6657             }
6658             else if(type.typeName)
6659                strcat(string, type.typeName);
6660             else
6661             {
6662                Type member;
6663                strcat(string, "struct { ");
6664                for(member = type.members.first; member; member = member.next)
6665                {
6666                   PrintType(member, string, true, fullName);
6667                   strcat(string,"; ");
6668                }
6669                strcat(string,"}");
6670             }
6671             break;
6672          case unionType:
6673             if(type.enumName)
6674             {
6675                strcat(string, "union ");
6676                strcat(string, type.enumName);
6677             }
6678             else if(type.typeName)
6679                strcat(string, type.typeName);
6680             else
6681             {
6682                strcat(string, "union ");
6683                strcat(string,"(unnamed)");
6684             }
6685             break;
6686          case enumType:
6687             if(type.enumName)
6688             {
6689                strcat(string, "enum ");
6690                strcat(string, type.enumName);
6691             }
6692             else if(type.typeName)
6693                strcat(string, type.typeName);
6694             else
6695                strcat(string, "int"); // "enum");
6696             break;
6697          case ellipsisType:
6698             strcat(string, "...");
6699             break;
6700          case subClassType:
6701             strcat(string, "subclass(");
6702             strcat(string, type._class ? type._class.string : "int");
6703             strcat(string, ")");
6704             break;
6705          case templateType:
6706             strcat(string, type.templateParameter.identifier.string);
6707             break;
6708          case thisClassType:
6709             strcat(string, "thisclass");
6710             break;
6711          case vaListType:
6712             strcat(string, "__builtin_va_list");
6713             break;
6714       }
6715    }
6716 }
6717
6718 static void PrintName(Type type, char * string, bool fullName)
6719 {
6720    if(type.name && type.name[0])
6721    {
6722       if(fullName)
6723          strcat(string, type.name);
6724       else
6725       {
6726          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6727          if(name) name += 2; else name = type.name;
6728          strcat(string, name);
6729       }
6730    }
6731 }
6732
6733 static void PrintAttribs(Type type, char * string)
6734 {
6735    if(type)
6736    {
6737       if(type.dllExport)   strcat(string, "dllexport ");
6738       if(type.attrStdcall) strcat(string, "stdcall ");
6739    }
6740 }
6741
6742 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6743 {
6744    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6745    {
6746       Type attrType = null;
6747       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6748          PrintAttribs(type, string);
6749       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6750          strcat(string, " const");
6751       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6752       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6753          strcat(string, " (");
6754       if(type.kind == pointerType)
6755       {
6756          if(type.type.kind == functionType || type.type.kind == methodType)
6757             PrintAttribs(type.type, string);
6758       }
6759       if(type.kind == pointerType)
6760       {
6761          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6762             strcat(string, "*");
6763          else
6764             strcat(string, " *");
6765       }
6766       if(printConst && type.constant && type.kind == pointerType)
6767          strcat(string, " const");
6768    }
6769    else
6770       PrintTypeSpecs(type, string, fullName, printConst);
6771 }
6772
6773 static void PostPrintType(Type type, char * string, bool fullName)
6774 {
6775    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6776       strcat(string, ")");
6777    if(type.kind == arrayType)
6778       PrintArraySize(type, string);
6779    else if(type.kind == functionType)
6780    {
6781       Type param;
6782       strcat(string, "(");
6783       for(param = type.params.first; param; param = param.next)
6784       {
6785          PrintType(param, string, true, fullName);
6786          if(param.next) strcat(string, ", ");
6787       }
6788       strcat(string, ")");
6789    }
6790    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6791       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6792 }
6793
6794 // *****
6795 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6796 // *****
6797 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6798 {
6799    PrePrintType(type, string, fullName, null, printConst);
6800
6801    if(type.thisClass || (printName && type.name && type.name[0]))
6802       strcat(string, " ");
6803    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6804    {
6805       Symbol _class = type.thisClass;
6806       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6807       {
6808          if(type.classObjectType == classPointer)
6809             strcat(string, "class");
6810          else
6811             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6812       }
6813       else if(_class && _class.string)
6814       {
6815          String s = _class.string;
6816          if(fullName)
6817             strcat(string, s);
6818          else
6819          {
6820             char * name = RSearchString(s, "::", strlen(s), true, false);
6821             if(name) name += 2; else name = s;
6822             strcat(string, name);
6823          }
6824       }
6825       strcat(string, "::");
6826    }
6827
6828    if(printName && type.name)
6829       PrintName(type, string, fullName);
6830    PostPrintType(type, string, fullName);
6831    if(type.bitFieldCount)
6832    {
6833       char count[100];
6834       sprintf(count, ":%d", type.bitFieldCount);
6835       strcat(string, count);
6836    }
6837 }
6838
6839 void PrintType(Type type, char * string, bool printName, bool fullName)
6840 {
6841    _PrintType(type, string, printName, fullName, true);
6842 }
6843
6844 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6845 {
6846    _PrintType(type, string, printName, fullName, false);
6847 }
6848
6849 static Type FindMember(Type type, char * string)
6850 {
6851    Type memberType;
6852    for(memberType = type.members.first; memberType; memberType = memberType.next)
6853    {
6854       if(!memberType.name)
6855       {
6856          Type subType = FindMember(memberType, string);
6857          if(subType)
6858             return subType;
6859       }
6860       else if(!strcmp(memberType.name, string))
6861          return memberType;
6862    }
6863    return null;
6864 }
6865
6866 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6867 {
6868    Type memberType;
6869    for(memberType = type.members.first; memberType; memberType = memberType.next)
6870    {
6871       if(!memberType.name)
6872       {
6873          Type subType = FindMember(memberType, string);
6874          if(subType)
6875          {
6876             *offset += memberType.offset;
6877             return subType;
6878          }
6879       }
6880       else if(!strcmp(memberType.name, string))
6881       {
6882          *offset += memberType.offset;
6883          return memberType;
6884       }
6885    }
6886    return null;
6887 }
6888
6889 public bool GetParseError() { return parseError; }
6890
6891 Expression ParseExpressionString(char * expression)
6892 {
6893    parseError = false;
6894
6895    fileInput = TempFile { };
6896    fileInput.Write(expression, 1, strlen(expression));
6897    fileInput.Seek(0, start);
6898
6899    echoOn = false;
6900    parsedExpression = null;
6901    resetScanner();
6902    expression_yyparse();
6903    delete fileInput;
6904
6905    return parsedExpression;
6906 }
6907
6908 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6909 {
6910    Identifier id = exp.identifier;
6911    Method method = null;
6912    Property prop = null;
6913    DataMember member = null;
6914    ClassProperty classProp = null;
6915
6916    if(_class && _class.type == enumClass)
6917    {
6918       NamedLink value = null;
6919       Class enumClass = eSystem_FindClass(privateModule, "enum");
6920       if(enumClass)
6921       {
6922          Class baseClass;
6923          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6924          {
6925             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6926             for(value = e.values.first; value; value = value.next)
6927             {
6928                if(!strcmp(value.name, id.string))
6929                   break;
6930             }
6931             if(value)
6932             {
6933                char constant[256];
6934
6935                FreeExpContents(exp);
6936
6937                exp.type = constantExp;
6938                exp.isConstant = true;
6939                if(!strcmp(baseClass.dataTypeString, "int"))
6940                   sprintf(constant, "%d",(int)value.data);
6941                else
6942                   sprintf(constant, "0x%X",(int)value.data);
6943                exp.constant = CopyString(constant);
6944                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
6945                exp.expType = MkClassType(baseClass.fullName);
6946                break;
6947             }
6948          }
6949       }
6950       if(value)
6951          return true;
6952    }
6953    if((method = eClass_FindMethod(_class, id.string, privateModule)))
6954    {
6955       ProcessMethodType(method);
6956       exp.expType = Type
6957       {
6958          refCount = 1;
6959          kind = methodType;
6960          method = method;
6961          // Crash here?
6962          // TOCHECK: Put it back to what it was...
6963          // methodClass = _class;
6964          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
6965       };
6966       //id._class = null;
6967       return true;
6968    }
6969    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
6970    {
6971       if(!prop.dataType)
6972          ProcessPropertyType(prop);
6973       exp.expType = prop.dataType;
6974       if(prop.dataType) prop.dataType.refCount++;
6975       return true;
6976    }
6977    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
6978    {
6979       if(!member.dataType)
6980          member.dataType = ProcessTypeString(member.dataTypeString, false);
6981       exp.expType = member.dataType;
6982       if(member.dataType) member.dataType.refCount++;
6983       return true;
6984    }
6985    else if((classProp = eClass_FindClassProperty(_class, id.string)))
6986    {
6987       if(!classProp.dataType)
6988          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
6989
6990       if(classProp.constant)
6991       {
6992          FreeExpContents(exp);
6993
6994          exp.isConstant = true;
6995          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
6996          {
6997             //char constant[256];
6998             exp.type = stringExp;
6999             exp.constant = QMkString((char *)classProp.Get(_class));
7000          }
7001          else
7002          {
7003             char constant[256];
7004             exp.type = constantExp;
7005             sprintf(constant, "%d", (int)classProp.Get(_class));
7006             exp.constant = CopyString(constant);
7007          }
7008       }
7009       else
7010       {
7011          // TO IMPLEMENT...
7012       }
7013
7014       exp.expType = classProp.dataType;
7015       if(classProp.dataType) classProp.dataType.refCount++;
7016       return true;
7017    }
7018    return false;
7019 }
7020
7021 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7022 {
7023    BinaryTree * tree = &nameSpace.functions;
7024    GlobalData data = (GlobalData)tree->FindString(name);
7025    NameSpace * child;
7026    if(!data)
7027    {
7028       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7029       {
7030          data = ScanGlobalData(child, name);
7031          if(data)
7032             break;
7033       }
7034    }
7035    return data;
7036 }
7037
7038 static GlobalData FindGlobalData(char * name)
7039 {
7040    int start = 0, c;
7041    NameSpace * nameSpace;
7042    nameSpace = globalData;
7043    for(c = 0; name[c]; c++)
7044    {
7045       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7046       {
7047          NameSpace * newSpace;
7048          char * spaceName = new char[c - start + 1];
7049          strncpy(spaceName, name + start, c - start);
7050          spaceName[c-start] = '\0';
7051          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7052          delete spaceName;
7053          if(!newSpace)
7054             return null;
7055          nameSpace = newSpace;
7056          if(name[c] == ':') c++;
7057          start = c+1;
7058       }
7059    }
7060    if(c - start)
7061    {
7062       return ScanGlobalData(nameSpace, name + start);
7063    }
7064    return null;
7065 }
7066
7067 static int definedExpStackPos;
7068 static void * definedExpStack[512];
7069
7070 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7071 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7072 {
7073    Expression prev = checkedExp.prev, next = checkedExp.next;
7074
7075    FreeExpContents(checkedExp);
7076    FreeType(checkedExp.expType);
7077    FreeType(checkedExp.destType);
7078
7079    *checkedExp = *newExp;
7080
7081    delete newExp;
7082
7083    checkedExp.prev = prev;
7084    checkedExp.next = next;
7085 }
7086
7087 void ApplyAnyObjectLogic(Expression e)
7088 {
7089    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7090 #ifdef _DEBUG
7091    char debugExpString[4096];
7092    debugExpString[0] = '\0';
7093    PrintExpression(e, debugExpString);
7094 #endif
7095
7096    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7097    {
7098       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7099       //ellipsisDestType = destType;
7100       if(e && e.expType)
7101       {
7102          Type type = e.expType;
7103          Class _class = null;
7104          //Type destType = e.destType;
7105
7106          if(type.kind == classType && type._class && type._class.registered)
7107          {
7108             _class = type._class.registered;
7109          }
7110          else if(type.kind == subClassType)
7111          {
7112             _class = FindClass("ecere::com::Class").registered;
7113          }
7114          else
7115          {
7116             char string[1024] = "";
7117             Symbol classSym;
7118
7119             PrintTypeNoConst(type, string, false, true);
7120             classSym = FindClass(string);
7121             if(classSym) _class = classSym.registered;
7122          }
7123
7124          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...
7125             (!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))) ||
7126             destType.byReference)))
7127          {
7128             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7129             {
7130                Expression checkedExp = e, newExp;
7131
7132                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7133                {
7134                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7135                   {
7136                      if(checkedExp.type == extensionCompoundExp)
7137                      {
7138                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7139                      }
7140                      else
7141                         checkedExp = checkedExp.list->last;
7142                   }
7143                   else if(checkedExp.type == castExp)
7144                      checkedExp = checkedExp.cast.exp;
7145                }
7146
7147                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7148                {
7149                   newExp = checkedExp.op.exp2;
7150                   checkedExp.op.exp2 = null;
7151                   FreeExpContents(checkedExp);
7152
7153                   if(e.expType && e.expType.passAsTemplate)
7154                   {
7155                      char size[100];
7156                      ComputeTypeSize(e.expType);
7157                      sprintf(size, "%d", e.expType.size);
7158                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7159                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7160                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7161                   }
7162
7163                   ReplaceExpContents(checkedExp, newExp);
7164                   e.byReference = true;
7165                }
7166                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7167                {
7168                   Expression checkedExp, newExp;
7169
7170                   {
7171                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7172                      bool hasAddress =
7173                         e.type == identifierExp ||
7174                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7175                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7176                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7177                         e.type == indexExp;
7178
7179                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7180                      {
7181                         Context context = PushContext();
7182                         Declarator decl;
7183                         OldList * specs = MkList();
7184                         char typeString[1024];
7185                         Expression newExp { };
7186
7187                         typeString[0] = '\0';
7188                         *newExp = *e;
7189
7190                         //if(e.destType) e.destType.refCount++;
7191                         // if(exp.expType) exp.expType.refCount++;
7192                         newExp.prev = null;
7193                         newExp.next = null;
7194                         newExp.expType = null;
7195
7196                         PrintTypeNoConst(e.expType, typeString, false, true);
7197                         decl = SpecDeclFromString(typeString, specs, null);
7198                         newExp.destType = ProcessType(specs, decl);
7199
7200                         curContext = context;
7201
7202                         // We need a current compound for this
7203                         if(curCompound)
7204                         {
7205                            char name[100];
7206                            OldList * stmts = MkList();
7207                            e.type = extensionCompoundExp;
7208                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7209                            if(!curCompound.compound.declarations)
7210                               curCompound.compound.declarations = MkList();
7211                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7212                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7213                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7214                            e.compound = MkCompoundStmt(null, stmts);
7215                         }
7216                         else
7217                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7218
7219                         /*
7220                         e.compound = MkCompoundStmt(
7221                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7222                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7223
7224                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7225                         */
7226
7227                         {
7228                            Type type = e.destType;
7229                            e.destType = { };
7230                            CopyTypeInto(e.destType, type);
7231                            e.destType.refCount = 1;
7232                            e.destType.classObjectType = none;
7233                            FreeType(type);
7234                         }
7235
7236                         e.compound.compound.context = context;
7237                         PopContext(context);
7238                         curContext = context.parent;
7239                      }
7240                   }
7241
7242                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7243                   checkedExp = e;
7244                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7245                   {
7246                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7247                      {
7248                         if(checkedExp.type == extensionCompoundExp)
7249                         {
7250                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7251                         }
7252                         else
7253                            checkedExp = checkedExp.list->last;
7254                      }
7255                      else if(checkedExp.type == castExp)
7256                         checkedExp = checkedExp.cast.exp;
7257                   }
7258                   {
7259                      Expression operand { };
7260                      operand = *checkedExp;
7261                      checkedExp.destType = null;
7262                      checkedExp.expType = null;
7263                      checkedExp.Clear();
7264                      checkedExp.type = opExp;
7265                      checkedExp.op.op = '&';
7266                      checkedExp.op.exp1 = null;
7267                      checkedExp.op.exp2 = operand;
7268
7269                      //newExp = MkExpOp(null, '&', checkedExp);
7270                   }
7271                   //ReplaceExpContents(checkedExp, newExp);
7272                }
7273             }
7274          }
7275       }
7276    }
7277    {
7278       // If expression type is a simple class, make it an address
7279       // FixReference(e, true);
7280    }
7281 //#if 0
7282    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7283       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7284          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7285    {
7286       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"))
7287       {
7288          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7289       }
7290       else
7291       {
7292          Expression thisExp { };
7293
7294          *thisExp = *e;
7295          thisExp.prev = null;
7296          thisExp.next = null;
7297          e.Clear();
7298
7299          e.type = bracketsExp;
7300          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7301          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7302             ((Expression)e.list->first).byReference = true;
7303
7304          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7305          {
7306             e.expType = thisExp.expType;
7307             e.expType.refCount++;
7308          }
7309          else*/
7310          {
7311             e.expType = { };
7312             CopyTypeInto(e.expType, thisExp.expType);
7313             e.expType.byReference = false;
7314             e.expType.refCount = 1;
7315
7316             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7317                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7318             {
7319                e.expType.classObjectType = none;
7320             }
7321          }
7322       }
7323    }
7324 // TOFIX: Try this for a nice IDE crash!
7325 //#endif
7326    // The other way around
7327    else
7328 //#endif
7329    if(destType && e.expType &&
7330          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7331          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7332          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7333    {
7334       if(destType.kind == ellipsisType)
7335       {
7336          Compiler_Error($"Unspecified type\n");
7337       }
7338       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7339       {
7340          bool byReference = e.expType.byReference;
7341          Expression thisExp { };
7342          Declarator decl;
7343          OldList * specs = MkList();
7344          char typeString[1024]; // Watch buffer overruns
7345          Type type;
7346          ClassObjectType backupClassObjectType;
7347          bool backupByReference;
7348
7349          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7350             type = e.expType;
7351          else
7352             type = destType;
7353
7354          backupClassObjectType = type.classObjectType;
7355          backupByReference = type.byReference;
7356
7357          type.classObjectType = none;
7358          type.byReference = false;
7359
7360          typeString[0] = '\0';
7361          PrintType(type, typeString, false, true);
7362          decl = SpecDeclFromString(typeString, specs, null);
7363
7364          type.classObjectType = backupClassObjectType;
7365          type.byReference = backupByReference;
7366
7367          *thisExp = *e;
7368          thisExp.prev = null;
7369          thisExp.next = null;
7370          e.Clear();
7371
7372          if( ( type.kind == classType && type._class && type._class.registered &&
7373                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7374                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7375              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7376              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7377          {
7378             e.type = opExp;
7379             e.op.op = '*';
7380             e.op.exp1 = null;
7381             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7382
7383             e.expType = { };
7384             CopyTypeInto(e.expType, type);
7385             e.expType.byReference = false;
7386             e.expType.refCount = 1;
7387          }
7388          else
7389          {
7390             e.type = castExp;
7391             e.cast.typeName = MkTypeName(specs, decl);
7392             e.cast.exp = thisExp;
7393             e.byReference = true;
7394             e.expType = type;
7395             type.refCount++;
7396          }
7397          e.destType = destType;
7398          destType.refCount++;
7399       }
7400    }
7401 }
7402
7403 void ProcessExpressionType(Expression exp)
7404 {
7405    bool unresolved = false;
7406    Location oldyylloc = yylloc;
7407    bool notByReference = false;
7408 #ifdef _DEBUG
7409    char debugExpString[4096];
7410    debugExpString[0] = '\0';
7411    PrintExpression(exp, debugExpString);
7412 #endif
7413    if(!exp || exp.expType)
7414       return;
7415
7416    //eSystem_Logf("%s\n", expString);
7417
7418    // Testing this here
7419    yylloc = exp.loc;
7420    switch(exp.type)
7421    {
7422       case identifierExp:
7423       {
7424          Identifier id = exp.identifier;
7425          if(!id || !topContext) return;
7426
7427          // DOING THIS LATER NOW...
7428          if(id._class && id._class.name)
7429          {
7430             id.classSym = id._class.symbol; // FindClass(id._class.name);
7431             /* TODO: Name Space Fix ups
7432             if(!id.classSym)
7433                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7434             */
7435          }
7436
7437          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7438          {
7439             exp.expType = ProcessTypeString("Module", true);
7440             break;
7441          }
7442          else */if(strstr(id.string, "__ecereClass") == id.string)
7443          {
7444             exp.expType = ProcessTypeString("ecere::com::Class", true);
7445             break;
7446          }
7447          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7448          {
7449             // Added this here as well
7450             ReplaceClassMembers(exp, thisClass);
7451             if(exp.type != identifierExp)
7452             {
7453                ProcessExpressionType(exp);
7454                break;
7455             }
7456
7457             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7458                break;
7459          }
7460          else
7461          {
7462             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7463             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7464             if(!symbol/* && exp.destType*/)
7465             {
7466                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7467                   break;
7468                else
7469                {
7470                   if(thisClass)
7471                   {
7472                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7473                      if(exp.type != identifierExp)
7474                      {
7475                         ProcessExpressionType(exp);
7476                         break;
7477                      }
7478                   }
7479                   // Static methods called from inside the _class
7480                   else if(currentClass && !id._class)
7481                   {
7482                      if(ResolveIdWithClass(exp, currentClass, true))
7483                         break;
7484                   }
7485                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7486                }
7487             }
7488
7489             // If we manage to resolve this symbol
7490             if(symbol)
7491             {
7492                Type type = symbol.type;
7493                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7494
7495                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7496                {
7497                   Context context = SetupTemplatesContext(_class);
7498                   type = ReplaceThisClassType(_class);
7499                   FinishTemplatesContext(context);
7500                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7501                }
7502
7503                FreeSpecifier(id._class);
7504                id._class = null;
7505                delete id.string;
7506                id.string = CopyString(symbol.string);
7507
7508                id.classSym = null;
7509                exp.expType = type;
7510                if(type)
7511                   type.refCount++;
7512                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7513                   // Add missing cases here... enum Classes...
7514                   exp.isConstant = true;
7515
7516                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7517                if(symbol.isParam || !strcmp(id.string, "this"))
7518                {
7519                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7520                      exp.byReference = true;
7521
7522                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7523                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7524                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7525                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7526                   {
7527                      Identifier id = exp.identifier;
7528                      exp.type = bracketsExp;
7529                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7530                   }*/
7531                }
7532
7533                if(symbol.isIterator)
7534                {
7535                   if(symbol.isIterator == 3)
7536                   {
7537                      exp.type = bracketsExp;
7538                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7539                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7540                      exp.expType = null;
7541                      ProcessExpressionType(exp);
7542                   }
7543                   else if(symbol.isIterator != 4)
7544                   {
7545                      exp.type = memberExp;
7546                      exp.member.exp = MkExpIdentifier(exp.identifier);
7547                      exp.member.exp.expType = exp.expType;
7548                      /*if(symbol.isIterator == 6)
7549                         exp.member.member = MkIdentifier("key");
7550                      else*/
7551                         exp.member.member = MkIdentifier("data");
7552                      exp.expType = null;
7553                      ProcessExpressionType(exp);
7554                   }
7555                }
7556                break;
7557             }
7558             else
7559             {
7560                DefinedExpression definedExp = null;
7561                if(thisNameSpace && !(id._class && !id._class.name))
7562                {
7563                   char name[1024];
7564                   strcpy(name, thisNameSpace);
7565                   strcat(name, "::");
7566                   strcat(name, id.string);
7567                   definedExp = eSystem_FindDefine(privateModule, name);
7568                }
7569                if(!definedExp)
7570                   definedExp = eSystem_FindDefine(privateModule, id.string);
7571                if(definedExp)
7572                {
7573                   int c;
7574                   for(c = 0; c<definedExpStackPos; c++)
7575                      if(definedExpStack[c] == definedExp)
7576                         break;
7577                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7578                   {
7579                      Location backupYylloc = yylloc;
7580                      File backInput = fileInput;
7581                      definedExpStack[definedExpStackPos++] = definedExp;
7582
7583                      fileInput = TempFile { };
7584                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7585                      fileInput.Seek(0, start);
7586
7587                      echoOn = false;
7588                      parsedExpression = null;
7589                      resetScanner();
7590                      expression_yyparse();
7591                      delete fileInput;
7592                      if(backInput)
7593                         fileInput = backInput;
7594
7595                      yylloc = backupYylloc;
7596
7597                      if(parsedExpression)
7598                      {
7599                         FreeIdentifier(id);
7600                         exp.type = bracketsExp;
7601                         exp.list = MkListOne(parsedExpression);
7602                         parsedExpression.loc = yylloc;
7603                         ProcessExpressionType(exp);
7604                         definedExpStackPos--;
7605                         return;
7606                      }
7607                      definedExpStackPos--;
7608                   }
7609                   else
7610                   {
7611                      if(inCompiler)
7612                      {
7613                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7614                      }
7615                   }
7616                }
7617                else
7618                {
7619                   GlobalData data = null;
7620                   if(thisNameSpace && !(id._class && !id._class.name))
7621                   {
7622                      char name[1024];
7623                      strcpy(name, thisNameSpace);
7624                      strcat(name, "::");
7625                      strcat(name, id.string);
7626                      data = FindGlobalData(name);
7627                   }
7628                   if(!data)
7629                      data = FindGlobalData(id.string);
7630                   if(data)
7631                   {
7632                      DeclareGlobalData(data);
7633                      exp.expType = data.dataType;
7634                      if(data.dataType) data.dataType.refCount++;
7635
7636                      delete id.string;
7637                      id.string = CopyString(data.fullName);
7638                      FreeSpecifier(id._class);
7639                      id._class = null;
7640
7641                      break;
7642                   }
7643                   else
7644                   {
7645                      GlobalFunction function = null;
7646                      if(thisNameSpace && !(id._class && !id._class.name))
7647                      {
7648                         char name[1024];
7649                         strcpy(name, thisNameSpace);
7650                         strcat(name, "::");
7651                         strcat(name, id.string);
7652                         function = eSystem_FindFunction(privateModule, name);
7653                      }
7654                      if(!function)
7655                         function = eSystem_FindFunction(privateModule, id.string);
7656                      if(function)
7657                      {
7658                         char name[1024];
7659                         delete id.string;
7660                         id.string = CopyString(function.name);
7661                         name[0] = 0;
7662
7663                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7664                            strcpy(name, "__ecereFunction_");
7665                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7666                         if(DeclareFunction(function, name))
7667                         {
7668                            delete id.string;
7669                            id.string = CopyString(name);
7670                         }
7671                         exp.expType = function.dataType;
7672                         if(function.dataType) function.dataType.refCount++;
7673
7674                         FreeSpecifier(id._class);
7675                         id._class = null;
7676
7677                         break;
7678                      }
7679                   }
7680                }
7681             }
7682          }
7683          unresolved = true;
7684          break;
7685       }
7686       case instanceExp:
7687       {
7688          Class _class;
7689          // Symbol classSym;
7690
7691          if(!exp.instance._class)
7692          {
7693             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7694             {
7695                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7696             }
7697          }
7698
7699          //classSym = FindClass(exp.instance._class.fullName);
7700          //_class = classSym ? classSym.registered : null;
7701
7702          ProcessInstantiationType(exp.instance);
7703          exp.isConstant = exp.instance.isConstant;
7704
7705          /*
7706          if(_class.type == unitClass && _class.base.type != systemClass)
7707          {
7708             {
7709                Type destType = exp.destType;
7710
7711                exp.destType = MkClassType(_class.base.fullName);
7712                exp.expType = MkClassType(_class.fullName);
7713                CheckExpressionType(exp, exp.destType, true);
7714
7715                exp.destType = destType;
7716             }
7717             exp.expType = MkClassType(_class.fullName);
7718          }
7719          else*/
7720          if(exp.instance._class)
7721          {
7722             exp.expType = MkClassType(exp.instance._class.name);
7723             /*if(exp.expType._class && exp.expType._class.registered &&
7724                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7725                exp.expType.byReference = true;*/
7726          }
7727          break;
7728       }
7729       case constantExp:
7730       {
7731          if(!exp.expType)
7732          {
7733             char * constant = exp.constant;
7734             Type type
7735             {
7736                refCount = 1;
7737                constant = true;
7738             };
7739             exp.expType = type;
7740
7741             if(constant[0] == '\'')
7742             {
7743                if((int)((byte *)constant)[1] > 127)
7744                {
7745                   int nb;
7746                   unichar ch = UTF8GetChar(constant + 1, &nb);
7747                   if(nb < 2) ch = constant[1];
7748                   delete constant;
7749                   exp.constant = PrintUInt(ch);
7750                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7751                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7752                   type._class = FindClass("unichar");
7753
7754                   type.isSigned = false;
7755                }
7756                else
7757                {
7758                   type.kind = charType;
7759                   type.isSigned = true;
7760                }
7761             }
7762             else
7763             {
7764                char * dot = strchr(constant, '.');
7765                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7766                char * exponent;
7767                if(isHex)
7768                {
7769                   exponent = strchr(constant, 'p');
7770                   if(!exponent) exponent = strchr(constant, 'P');
7771                }
7772                else
7773                {
7774                   exponent = strchr(constant, 'e');
7775                   if(!exponent) exponent = strchr(constant, 'E');
7776                }
7777
7778                if(dot || exponent)
7779                {
7780                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7781                      type.kind = floatType;
7782                   else
7783                      type.kind = doubleType;
7784                   type.isSigned = true;
7785                }
7786                else
7787                {
7788                   bool isSigned = constant[0] == '-';
7789                   int64 i64 = strtoll(constant, null, 0);
7790                   uint64 ui64 = strtoull(constant, null, 0);
7791                   bool is64Bit = false;
7792                   if(isSigned)
7793                   {
7794                      if(i64 < MININT)
7795                         is64Bit = true;
7796                   }
7797                   else
7798                   {
7799                      if(ui64 > MAXINT)
7800                      {
7801                         if(ui64 > MAXDWORD)
7802                         {
7803                            is64Bit = true;
7804                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7805                               isSigned = true;
7806                         }
7807                      }
7808                      else if(constant[0] != '0' || !constant[1])
7809                         isSigned = true;
7810                   }
7811                   type.kind = is64Bit ? int64Type : intType;
7812                   type.isSigned = isSigned;
7813                }
7814             }
7815             exp.isConstant = true;
7816             if(exp.destType && exp.destType.kind == doubleType)
7817                type.kind = doubleType;
7818             else if(exp.destType && exp.destType.kind == floatType)
7819                type.kind = floatType;
7820             else if(exp.destType && exp.destType.kind == int64Type)
7821                type.kind = int64Type;
7822          }
7823          break;
7824       }
7825       case stringExp:
7826       {
7827          exp.isConstant = true;      // Why wasn't this constant?
7828          exp.expType = Type
7829          {
7830             refCount = 1;
7831             kind = pointerType;
7832             type = Type
7833             {
7834                refCount = 1;
7835                kind = charType;
7836                constant = true;
7837                isSigned = true;
7838             }
7839          };
7840          break;
7841       }
7842       case newExp:
7843       case new0Exp:
7844          ProcessExpressionType(exp._new.size);
7845          exp.expType = Type
7846          {
7847             refCount = 1;
7848             kind = pointerType;
7849             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7850          };
7851          DeclareType(exp.expType.type, false, false);
7852          break;
7853       case renewExp:
7854       case renew0Exp:
7855          ProcessExpressionType(exp._renew.size);
7856          ProcessExpressionType(exp._renew.exp);
7857          exp.expType = Type
7858          {
7859             refCount = 1;
7860             kind = pointerType;
7861             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7862          };
7863          DeclareType(exp.expType.type, false, false);
7864          break;
7865       case opExp:
7866       {
7867          bool assign = false, boolResult = false, boolOps = false;
7868          Type type1 = null, type2 = null;
7869          bool useDestType = false, useSideType = false;
7870          Location oldyylloc = yylloc;
7871          bool useSideUnit = false;
7872
7873          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7874          Type dummy
7875          {
7876             count = 1;
7877             refCount = 1;
7878          };
7879
7880          switch(exp.op.op)
7881          {
7882             // Assignment Operators
7883             case '=':
7884             case MUL_ASSIGN:
7885             case DIV_ASSIGN:
7886             case MOD_ASSIGN:
7887             case ADD_ASSIGN:
7888             case SUB_ASSIGN:
7889             case LEFT_ASSIGN:
7890             case RIGHT_ASSIGN:
7891             case AND_ASSIGN:
7892             case XOR_ASSIGN:
7893             case OR_ASSIGN:
7894                assign = true;
7895                break;
7896             // boolean Operators
7897             case '!':
7898                // Expect boolean operators
7899                //boolOps = true;
7900                //boolResult = true;
7901                break;
7902             case AND_OP:
7903             case OR_OP:
7904                // Expect boolean operands
7905                boolOps = true;
7906                boolResult = true;
7907                break;
7908             // Comparisons
7909             case EQ_OP:
7910             case '<':
7911             case '>':
7912             case LE_OP:
7913             case GE_OP:
7914             case NE_OP:
7915                // Gives boolean result
7916                boolResult = true;
7917                useSideType = true;
7918                break;
7919             case '+':
7920             case '-':
7921                useSideUnit = true;
7922
7923                // Just added these... testing
7924             case '|':
7925             case '&':
7926             case '^':
7927
7928             // DANGER: Verify units
7929             case '/':
7930             case '%':
7931             case '*':
7932
7933                if(exp.op.op != '*' || exp.op.exp1)
7934                {
7935                   useSideType = true;
7936                   useDestType = true;
7937                }
7938                break;
7939
7940             /*// Implement speed etc.
7941             case '*':
7942             case '/':
7943                break;
7944             */
7945          }
7946          if(exp.op.op == '&')
7947          {
7948             // Added this here earlier for Iterator address as key
7949             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
7950             {
7951                Identifier id = exp.op.exp2.identifier;
7952                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7953                if(symbol && symbol.isIterator == 2)
7954                {
7955                   exp.type = memberExp;
7956                   exp.member.exp = exp.op.exp2;
7957                   exp.member.member = MkIdentifier("key");
7958                   exp.expType = null;
7959                   exp.op.exp2.expType = symbol.type;
7960                   symbol.type.refCount++;
7961                   ProcessExpressionType(exp);
7962                   FreeType(dummy);
7963                   break;
7964                }
7965                // exp.op.exp2.usage.usageRef = true;
7966             }
7967          }
7968
7969          //dummy.kind = TypeDummy;
7970
7971          if(exp.op.exp1)
7972          {
7973             if(exp.destType && exp.destType.kind == classType &&
7974                exp.destType._class && exp.destType._class.registered && useDestType &&
7975
7976               ((exp.destType._class.registered.type == unitClass && useSideUnit) ||
7977                exp.destType._class.registered.type == enumClass ||
7978                exp.destType._class.registered.type == bitClass
7979                ))
7980
7981               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
7982             {
7983                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7984                exp.op.exp1.destType = exp.destType;
7985                if(exp.destType)
7986                   exp.destType.refCount++;
7987             }
7988             else if(!assign)
7989             {
7990                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7991                exp.op.exp1.destType = dummy;
7992                dummy.refCount++;
7993             }
7994
7995             // TESTING THIS HERE...
7996             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
7997             ProcessExpressionType(exp.op.exp1);
7998             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
7999
8000             if(exp.op.exp1.destType == dummy)
8001             {
8002                FreeType(dummy);
8003                exp.op.exp1.destType = null;
8004             }
8005             type1 = exp.op.exp1.expType;
8006          }
8007
8008          if(exp.op.exp2)
8009          {
8010             char expString[10240];
8011             expString[0] = '\0';
8012             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8013             {
8014                if(exp.op.exp1)
8015                {
8016                   exp.op.exp2.destType = exp.op.exp1.expType;
8017                   if(exp.op.exp1.expType)
8018                      exp.op.exp1.expType.refCount++;
8019                }
8020                else
8021                {
8022                   exp.op.exp2.destType = exp.destType;
8023                   if(exp.destType)
8024                      exp.destType.refCount++;
8025                }
8026
8027                if(type1) type1.refCount++;
8028                exp.expType = type1;
8029             }
8030             else if(assign)
8031             {
8032                if(inCompiler)
8033                   PrintExpression(exp.op.exp2, expString);
8034
8035                if(type1 && type1.kind == pointerType)
8036                {
8037                   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 ||
8038                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8039                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8040                   else if(exp.op.op == '=')
8041                   {
8042                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8043                      exp.op.exp2.destType = type1;
8044                      if(type1)
8045                         type1.refCount++;
8046                   }
8047                }
8048                else
8049                {
8050                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8051                   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/* ||
8052                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8053                   else
8054                   {
8055                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8056                      exp.op.exp2.destType = type1;
8057                      if(type1)
8058                         type1.refCount++;
8059                   }
8060                }
8061                if(type1) type1.refCount++;
8062                exp.expType = type1;
8063             }
8064             else if(exp.destType && exp.destType.kind == classType &&
8065                exp.destType._class && exp.destType._class.registered &&
8066
8067                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) ||
8068                   (exp.destType._class.registered.type == enumClass && useDestType))
8069                   )
8070             {
8071                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8072                exp.op.exp2.destType = exp.destType;
8073                if(exp.destType)
8074                   exp.destType.refCount++;
8075             }
8076             else
8077             {
8078                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8079                exp.op.exp2.destType = dummy;
8080                dummy.refCount++;
8081             }
8082
8083             // TESTING THIS HERE... (DANGEROUS)
8084             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8085                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8086             {
8087                FreeType(exp.op.exp2.destType);
8088                exp.op.exp2.destType = type1;
8089                type1.refCount++;
8090             }
8091             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8092             // Cannot lose the cast on a sizeof
8093             if(exp.op.op == SIZEOF)
8094             {
8095                Expression e = exp.op.exp2;
8096                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8097                {
8098                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8099                   {
8100                      if(e.type == extensionCompoundExp)
8101                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8102                      else
8103                         e = e.list->last;
8104                   }
8105                }
8106                if(e.type == castExp && e.cast.exp)
8107                   e.cast.exp.needCast = true;
8108             }
8109             ProcessExpressionType(exp.op.exp2);
8110             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8111
8112             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8113             {
8114                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)
8115                {
8116                   if(exp.op.op != '=' && type1.type.kind == voidType)
8117                      Compiler_Error($"void *: unknown size\n");
8118                }
8119                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||
8120                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8121                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8122                               exp.op.exp2.expType._class.registered.type == structClass ||
8123                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8124                {
8125                   if(exp.op.op == ADD_ASSIGN)
8126                      Compiler_Error($"cannot add two pointers\n");
8127                }
8128                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8129                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8130                {
8131                   if(exp.op.op == ADD_ASSIGN)
8132                      Compiler_Error($"cannot add two pointers\n");
8133                }
8134                else if(inCompiler)
8135                {
8136                   char type1String[1024];
8137                   char type2String[1024];
8138                   type1String[0] = '\0';
8139                   type2String[0] = '\0';
8140
8141                   PrintType(exp.op.exp2.expType, type1String, false, true);
8142                   PrintType(type1, type2String, false, true);
8143                   ChangeCh(expString, '\n', ' ');
8144                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8145                }
8146             }
8147
8148             if(exp.op.exp2.destType == dummy)
8149             {
8150                FreeType(dummy);
8151                exp.op.exp2.destType = null;
8152             }
8153
8154             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8155             {
8156                type2 = { };
8157                type2.refCount = 1;
8158                CopyTypeInto(type2, exp.op.exp2.expType);
8159                type2.isSigned = true;
8160             }
8161             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8162             {
8163                type2 = { kind = intType };
8164                type2.refCount = 1;
8165                type2.isSigned = true;
8166             }
8167             else
8168             {
8169                type2 = exp.op.exp2.expType;
8170                if(type2) type2.refCount++;
8171             }
8172          }
8173
8174          dummy.kind = voidType;
8175
8176          if(exp.op.op == SIZEOF)
8177          {
8178             exp.expType = Type
8179             {
8180                refCount = 1;
8181                kind = intType;
8182             };
8183             exp.isConstant = true;
8184          }
8185          // Get type of dereferenced pointer
8186          else if(exp.op.op == '*' && !exp.op.exp1)
8187          {
8188             exp.expType = Dereference(type2);
8189             if(type2 && type2.kind == classType)
8190                notByReference = true;
8191          }
8192          else if(exp.op.op == '&' && !exp.op.exp1)
8193             exp.expType = Reference(type2);
8194          else if(!assign)
8195          {
8196             if(boolOps)
8197             {
8198                if(exp.op.exp1)
8199                {
8200                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8201                   exp.op.exp1.destType = MkClassType("bool");
8202                   exp.op.exp1.destType.truth = true;
8203                   if(!exp.op.exp1.expType)
8204                      ProcessExpressionType(exp.op.exp1);
8205                   else
8206                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8207                   FreeType(exp.op.exp1.expType);
8208                   exp.op.exp1.expType = MkClassType("bool");
8209                   exp.op.exp1.expType.truth = true;
8210                }
8211                if(exp.op.exp2)
8212                {
8213                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8214                   exp.op.exp2.destType = MkClassType("bool");
8215                   exp.op.exp2.destType.truth = true;
8216                   if(!exp.op.exp2.expType)
8217                      ProcessExpressionType(exp.op.exp2);
8218                   else
8219                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8220                   FreeType(exp.op.exp2.expType);
8221                   exp.op.exp2.expType = MkClassType("bool");
8222                   exp.op.exp2.expType.truth = true;
8223                }
8224             }
8225             else if(exp.op.exp1 && exp.op.exp2 &&
8226                ((useSideType /*&&
8227                      (useSideUnit ||
8228                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8229                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8230                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8231                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8232             {
8233                if(type1 && type2 &&
8234                   // If either both are class or both are not class
8235                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8236                {
8237                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8238                   exp.op.exp2.destType = type1;
8239                   type1.refCount++;
8240                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8241                   exp.op.exp1.destType = type2;
8242                   type2.refCount++;
8243                   // Warning here for adding Radians + Degrees with no destination type
8244                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8245                      type1._class.registered && type1._class.registered.type == unitClass &&
8246                      type2._class.registered && type2._class.registered.type == unitClass &&
8247                      type1._class.registered != type2._class.registered)
8248                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8249                         type1._class.string, type2._class.string, type1._class.string);
8250
8251                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8252                   {
8253                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8254                      if(argExp)
8255                      {
8256                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8257
8258                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8259                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8260                            exp.op.exp1)));
8261
8262                         ProcessExpressionType(exp.op.exp1);
8263
8264                         if(type2.kind != pointerType)
8265                         {
8266                            ProcessExpressionType(classExp);
8267
8268                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8269                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8270                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8271                                  // noHeadClass
8272                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8273                                     OR_OP,
8274                                  // normalClass
8275                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8276                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8277                                        MkPointer(null, null), null)))),
8278                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8279
8280                            if(!exp.op.exp2.expType)
8281                            {
8282                               if(type2)
8283                                  FreeType(type2);
8284                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8285                               type2.refCount++;
8286                            }
8287
8288                            ProcessExpressionType(exp.op.exp2);
8289                         }
8290                      }
8291                   }
8292
8293                   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)))
8294                   {
8295                      if(type1.kind != classType && type1.type.kind == voidType)
8296                         Compiler_Error($"void *: unknown size\n");
8297                      exp.expType = type1;
8298                      if(type1) type1.refCount++;
8299                   }
8300                   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)))
8301                   {
8302                      if(type2.kind != classType && type2.type.kind == voidType)
8303                         Compiler_Error($"void *: unknown size\n");
8304                      exp.expType = type2;
8305                      if(type2) type2.refCount++;
8306                   }
8307                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8308                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8309                   {
8310                      Compiler_Warning($"different levels of indirection\n");
8311                   }
8312                   else
8313                   {
8314                      bool success = false;
8315                      if(type1.kind == pointerType && type2.kind == pointerType)
8316                      {
8317                         if(exp.op.op == '+')
8318                            Compiler_Error($"cannot add two pointers\n");
8319                         else if(exp.op.op == '-')
8320                         {
8321                            // Pointer Subtraction gives integer
8322                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8323                            {
8324                               exp.expType = Type
8325                               {
8326                                  kind = intType;
8327                                  refCount = 1;
8328                               };
8329                               success = true;
8330
8331                               if(type1.type.kind == templateType)
8332                               {
8333                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8334                                  if(argExp)
8335                                  {
8336                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8337
8338                                     ProcessExpressionType(classExp);
8339
8340                                     exp.type = bracketsExp;
8341                                     exp.list = MkListOne(MkExpOp(
8342                                        MkExpBrackets(MkListOne(MkExpOp(
8343                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8344                                              , exp.op.op,
8345                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8346
8347                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8348
8349                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8350                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8351                                                 // noHeadClass
8352                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8353                                                    OR_OP,
8354                                                 // normalClass
8355                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8356                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8357                                                       MkPointer(null, null), null)))),
8358                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8359
8360
8361                                              ));
8362
8363                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8364                                     FreeType(dummy);
8365                                     return;
8366                                  }
8367                               }
8368                            }
8369                         }
8370                      }
8371
8372                      if(!success && exp.op.exp1.type == constantExp)
8373                      {
8374                         // If first expression is constant, try to match that first
8375                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8376                         {
8377                            if(exp.expType) FreeType(exp.expType);
8378                            exp.expType = exp.op.exp1.destType;
8379                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8380                            success = true;
8381                         }
8382                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8383                         {
8384                            if(exp.expType) FreeType(exp.expType);
8385                            exp.expType = exp.op.exp2.destType;
8386                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8387                            success = true;
8388                         }
8389                      }
8390                      else if(!success)
8391                      {
8392                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8393                         {
8394                            if(exp.expType) FreeType(exp.expType);
8395                            exp.expType = exp.op.exp2.destType;
8396                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8397                            success = true;
8398                         }
8399                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8400                         {
8401                            if(exp.expType) FreeType(exp.expType);
8402                            exp.expType = exp.op.exp1.destType;
8403                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8404                            success = true;
8405                         }
8406                      }
8407                      if(!success)
8408                      {
8409                         char expString1[10240];
8410                         char expString2[10240];
8411                         char type1[1024];
8412                         char type2[1024];
8413                         expString1[0] = '\0';
8414                         expString2[0] = '\0';
8415                         type1[0] = '\0';
8416                         type2[0] = '\0';
8417                         if(inCompiler)
8418                         {
8419                            PrintExpression(exp.op.exp1, expString1);
8420                            ChangeCh(expString1, '\n', ' ');
8421                            PrintExpression(exp.op.exp2, expString2);
8422                            ChangeCh(expString2, '\n', ' ');
8423                            PrintType(exp.op.exp1.expType, type1, false, true);
8424                            PrintType(exp.op.exp2.expType, type2, false, true);
8425                         }
8426
8427                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8428                      }
8429                   }
8430                }
8431                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8432                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8433                {
8434                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8435                   // Convert e.g. / 4 into / 4.0
8436                   exp.op.exp1.destType = type2._class.registered.dataType;
8437                   if(type2._class.registered.dataType)
8438                      type2._class.registered.dataType.refCount++;
8439                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8440                   exp.expType = type2;
8441                   if(type2) type2.refCount++;
8442                }
8443                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8444                {
8445                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8446                   // Convert e.g. / 4 into / 4.0
8447                   exp.op.exp2.destType = type1._class.registered.dataType;
8448                   if(type1._class.registered.dataType)
8449                      type1._class.registered.dataType.refCount++;
8450                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8451                   exp.expType = type1;
8452                   if(type1) type1.refCount++;
8453                }
8454                else if(type1)
8455                {
8456                   bool valid = false;
8457
8458                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8459                   {
8460                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8461
8462                      if(!type1._class.registered.dataType)
8463                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8464                      exp.op.exp2.destType = type1._class.registered.dataType;
8465                      exp.op.exp2.destType.refCount++;
8466
8467                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8468                      if(type2)
8469                         FreeType(type2);
8470                      type2 = exp.op.exp2.destType;
8471                      if(type2) type2.refCount++;
8472
8473                      exp.expType = type2;
8474                      type2.refCount++;
8475                   }
8476
8477                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8478                   {
8479                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8480
8481                      if(!type2._class.registered.dataType)
8482                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8483                      exp.op.exp1.destType = type2._class.registered.dataType;
8484                      exp.op.exp1.destType.refCount++;
8485
8486                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8487                      type1 = exp.op.exp1.destType;
8488                      exp.expType = type1;
8489                      type1.refCount++;
8490                   }
8491
8492                   // TESTING THIS NEW CODE
8493                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8494                   {
8495                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8496                      {
8497                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8498                         {
8499                            if(exp.expType) FreeType(exp.expType);
8500                            exp.expType = exp.op.exp1.expType;
8501                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8502                            valid = true;
8503                         }
8504                      }
8505
8506                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8507                      {
8508                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8509                         {
8510                            if(exp.expType) FreeType(exp.expType);
8511                            exp.expType = exp.op.exp2.expType;
8512                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8513                            valid = true;
8514                         }
8515                      }
8516                   }
8517
8518                   if(!valid)
8519                   {
8520                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8521                      exp.op.exp2.destType = type1;
8522                      type1.refCount++;
8523
8524                      /*
8525                      // Maybe this was meant to be an enum...
8526                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8527                      {
8528                         Type oldType = exp.op.exp2.expType;
8529                         exp.op.exp2.expType = null;
8530                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8531                            FreeType(oldType);
8532                         else
8533                            exp.op.exp2.expType = oldType;
8534                      }
8535                      */
8536
8537                      /*
8538                      // TESTING THIS HERE... LATEST ADDITION
8539                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8540                      {
8541                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8542                         exp.op.exp2.destType = type2._class.registered.dataType;
8543                         if(type2._class.registered.dataType)
8544                            type2._class.registered.dataType.refCount++;
8545                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8546
8547                         //exp.expType = type2._class.registered.dataType; //type2;
8548                         //if(type2) type2.refCount++;
8549                      }
8550
8551                      // TESTING THIS HERE... LATEST ADDITION
8552                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8553                      {
8554                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8555                         exp.op.exp1.destType = type1._class.registered.dataType;
8556                         if(type1._class.registered.dataType)
8557                            type1._class.registered.dataType.refCount++;
8558                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8559                         exp.expType = type1._class.registered.dataType; //type1;
8560                         if(type1) type1.refCount++;
8561                      }
8562                      */
8563
8564                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8565                      {
8566                         if(exp.expType) FreeType(exp.expType);
8567                         exp.expType = exp.op.exp2.destType;
8568                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8569                      }
8570                      else if(type1 && type2)
8571                      {
8572                         char expString1[10240];
8573                         char expString2[10240];
8574                         char type1String[1024];
8575                         char type2String[1024];
8576                         expString1[0] = '\0';
8577                         expString2[0] = '\0';
8578                         type1String[0] = '\0';
8579                         type2String[0] = '\0';
8580                         if(inCompiler)
8581                         {
8582                            PrintExpression(exp.op.exp1, expString1);
8583                            ChangeCh(expString1, '\n', ' ');
8584                            PrintExpression(exp.op.exp2, expString2);
8585                            ChangeCh(expString2, '\n', ' ');
8586                            PrintType(exp.op.exp1.expType, type1String, false, true);
8587                            PrintType(exp.op.exp2.expType, type2String, false, true);
8588                         }
8589
8590                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8591
8592                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8593                         {
8594                            exp.expType = exp.op.exp1.expType;
8595                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8596                         }
8597                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8598                         {
8599                            exp.expType = exp.op.exp2.expType;
8600                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8601                         }
8602                      }
8603                   }
8604                }
8605                else if(type2)
8606                {
8607                   // Maybe this was meant to be an enum...
8608                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8609                   {
8610                      Type oldType = exp.op.exp1.expType;
8611                      exp.op.exp1.expType = null;
8612                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8613                         FreeType(oldType);
8614                      else
8615                         exp.op.exp1.expType = oldType;
8616                   }
8617
8618                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8619                   exp.op.exp1.destType = type2;
8620                   type2.refCount++;
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                   }
8630
8631                   // TESTING THIS HERE... LATEST ADDITION
8632                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8633                   {
8634                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8635                      exp.op.exp2.destType = type2._class.registered.dataType;
8636                      if(type2._class.registered.dataType)
8637                         type2._class.registered.dataType.refCount++;
8638                   }
8639                   */
8640
8641                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8642                   {
8643                      if(exp.expType) FreeType(exp.expType);
8644                      exp.expType = exp.op.exp1.destType;
8645                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8646                   }
8647                }
8648             }
8649             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8650             {
8651                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8652                {
8653                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8654                   // Convert e.g. / 4 into / 4.0
8655                   exp.op.exp1.destType = type2._class.registered.dataType;
8656                   if(type2._class.registered.dataType)
8657                      type2._class.registered.dataType.refCount++;
8658                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8659                }
8660                if(exp.op.op == '!')
8661                {
8662                   exp.expType = MkClassType("bool");
8663                   exp.expType.truth = true;
8664                }
8665                else
8666                {
8667                   exp.expType = type2;
8668                   if(type2) type2.refCount++;
8669                }
8670             }
8671             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8672             {
8673                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8674                {
8675                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8676                   // Convert e.g. / 4 into / 4.0
8677                   exp.op.exp2.destType = type1._class.registered.dataType;
8678                   if(type1._class.registered.dataType)
8679                      type1._class.registered.dataType.refCount++;
8680                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8681                }
8682                exp.expType = type1;
8683                if(type1) type1.refCount++;
8684             }
8685          }
8686
8687          yylloc = exp.loc;
8688          if(exp.op.exp1 && !exp.op.exp1.expType)
8689          {
8690             char expString[10000];
8691             expString[0] = '\0';
8692             if(inCompiler)
8693             {
8694                PrintExpression(exp.op.exp1, expString);
8695                ChangeCh(expString, '\n', ' ');
8696             }
8697             if(expString[0])
8698                Compiler_Error($"couldn't determine type of %s\n", expString);
8699          }
8700          if(exp.op.exp2 && !exp.op.exp2.expType)
8701          {
8702             char expString[10240];
8703             expString[0] = '\0';
8704             if(inCompiler)
8705             {
8706                PrintExpression(exp.op.exp2, expString);
8707                ChangeCh(expString, '\n', ' ');
8708             }
8709             if(expString[0])
8710                Compiler_Error($"couldn't determine type of %s\n", expString);
8711          }
8712
8713          if(boolResult)
8714          {
8715             FreeType(exp.expType);
8716             exp.expType = MkClassType("bool");
8717             exp.expType.truth = true;
8718          }
8719
8720          if(exp.op.op != SIZEOF)
8721             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8722                (!exp.op.exp2 || exp.op.exp2.isConstant);
8723
8724          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8725          {
8726             DeclareType(exp.op.exp2.expType, false, false);
8727          }
8728
8729          yylloc = oldyylloc;
8730
8731          FreeType(dummy);
8732          if(type2)
8733             FreeType(type2);
8734          break;
8735       }
8736       case bracketsExp:
8737       case extensionExpressionExp:
8738       {
8739          Expression e;
8740          exp.isConstant = true;
8741          for(e = exp.list->first; e; e = e.next)
8742          {
8743             bool inced = false;
8744             if(!e.next)
8745             {
8746                FreeType(e.destType);
8747                e.destType = exp.destType;
8748                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8749             }
8750             ProcessExpressionType(e);
8751             if(inced)
8752                exp.destType.count--;
8753             if(!exp.expType && !e.next)
8754             {
8755                exp.expType = e.expType;
8756                if(e.expType) e.expType.refCount++;
8757             }
8758             if(!e.isConstant)
8759                exp.isConstant = false;
8760          }
8761
8762          // In case a cast became a member...
8763          e = exp.list->first;
8764          if(!e.next && e.type == memberExp)
8765          {
8766             // Preserve prev, next
8767             Expression next = exp.next, prev = exp.prev;
8768
8769
8770             FreeType(exp.expType);
8771             FreeType(exp.destType);
8772             delete exp.list;
8773
8774             *exp = *e;
8775
8776             exp.prev = prev;
8777             exp.next = next;
8778
8779             delete e;
8780
8781             ProcessExpressionType(exp);
8782          }
8783          break;
8784       }
8785       case indexExp:
8786       {
8787          Expression e;
8788          exp.isConstant = true;
8789
8790          ProcessExpressionType(exp.index.exp);
8791          if(!exp.index.exp.isConstant)
8792             exp.isConstant = false;
8793
8794          if(exp.index.exp.expType)
8795          {
8796             Type source = exp.index.exp.expType;
8797             if(source.kind == classType && source._class && source._class.registered)
8798             {
8799                Class _class = source._class.registered;
8800                Class c = _class.templateClass ? _class.templateClass : _class;
8801                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
8802                {
8803                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8804
8805                   if(exp.index.index && exp.index.index->last)
8806                   {
8807                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8808                   }
8809                }
8810             }
8811          }
8812
8813          for(e = exp.index.index->first; e; e = e.next)
8814          {
8815             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8816             {
8817                if(e.destType) FreeType(e.destType);
8818                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8819             }
8820             ProcessExpressionType(e);
8821             if(!e.next)
8822             {
8823                // Check if this type is int
8824             }
8825             if(!e.isConstant)
8826                exp.isConstant = false;
8827          }
8828
8829          if(!exp.expType)
8830             exp.expType = Dereference(exp.index.exp.expType);
8831          if(exp.expType)
8832             DeclareType(exp.expType, false, false);
8833          break;
8834       }
8835       case callExp:
8836       {
8837          Expression e;
8838          Type functionType;
8839          Type methodType = null;
8840          char name[1024];
8841          name[0] = '\0';
8842
8843          if(inCompiler)
8844          {
8845             PrintExpression(exp.call.exp,  name);
8846             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8847             {
8848                //exp.call.exp.expType = null;
8849                PrintExpression(exp.call.exp,  name);
8850             }
8851          }
8852          if(exp.call.exp.type == identifierExp)
8853          {
8854             Expression idExp = exp.call.exp;
8855             Identifier id = idExp.identifier;
8856             if(!strcmp(id.string, "__builtin_frame_address"))
8857             {
8858                exp.expType = ProcessTypeString("void *", true);
8859                if(exp.call.arguments && exp.call.arguments->first)
8860                   ProcessExpressionType(exp.call.arguments->first);
8861                break;
8862             }
8863             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8864             {
8865                exp.expType = ProcessTypeString("int", true);
8866                if(exp.call.arguments && exp.call.arguments->first)
8867                   ProcessExpressionType(exp.call.arguments->first);
8868                break;
8869             }
8870             else if(!strcmp(id.string, "Max") ||
8871                !strcmp(id.string, "Min") ||
8872                !strcmp(id.string, "Sgn") ||
8873                !strcmp(id.string, "Abs"))
8874             {
8875                Expression a = null;
8876                Expression b = null;
8877                Expression tempExp1 = null, tempExp2 = null;
8878                if((!strcmp(id.string, "Max") ||
8879                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8880                {
8881                   a = exp.call.arguments->first;
8882                   b = exp.call.arguments->last;
8883                   tempExp1 = a;
8884                   tempExp2 = b;
8885                }
8886                else if(exp.call.arguments->count == 1)
8887                {
8888                   a = exp.call.arguments->first;
8889                   tempExp1 = a;
8890                }
8891
8892                if(a)
8893                {
8894                   exp.call.arguments->Clear();
8895                   idExp.identifier = null;
8896
8897                   FreeExpContents(exp);
8898
8899                   ProcessExpressionType(a);
8900                   if(b)
8901                      ProcessExpressionType(b);
8902
8903                   exp.type = bracketsExp;
8904                   exp.list = MkList();
8905
8906                   if(a.expType && (!b || b.expType))
8907                   {
8908                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8909                      {
8910                         // Use the simpleStruct name/ids for now...
8911                         if(inCompiler)
8912                         {
8913                            OldList * specs = MkList();
8914                            OldList * decls = MkList();
8915                            Declaration decl;
8916                            char temp1[1024], temp2[1024];
8917
8918                            GetTypeSpecs(a.expType, specs);
8919
8920                            if(a && !a.isConstant && a.type != identifierExp)
8921                            {
8922                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8923                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8924                               tempExp1 = QMkExpId(temp1);
8925                               tempExp1.expType = a.expType;
8926                               if(a.expType)
8927                                  a.expType.refCount++;
8928                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
8929                            }
8930                            if(b && !b.isConstant && b.type != identifierExp)
8931                            {
8932                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
8933                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
8934                               tempExp2 = QMkExpId(temp2);
8935                               tempExp2.expType = b.expType;
8936                               if(b.expType)
8937                                  b.expType.refCount++;
8938                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
8939                            }
8940
8941                            decl = MkDeclaration(specs, decls);
8942                            if(!curCompound.compound.declarations)
8943                               curCompound.compound.declarations = MkList();
8944                            curCompound.compound.declarations->Insert(null, decl);
8945                         }
8946                      }
8947                   }
8948
8949                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
8950                   {
8951                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
8952                      ListAdd(exp.list,
8953                         MkExpCondition(MkExpBrackets(MkListOne(
8954                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
8955                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
8956                      exp.expType = a.expType;
8957                      if(a.expType)
8958                         a.expType.refCount++;
8959                   }
8960                   else if(!strcmp(id.string, "Abs"))
8961                   {
8962                      ListAdd(exp.list,
8963                         MkExpCondition(MkExpBrackets(MkListOne(
8964                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8965                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
8966                      exp.expType = a.expType;
8967                      if(a.expType)
8968                         a.expType.refCount++;
8969                   }
8970                   else if(!strcmp(id.string, "Sgn"))
8971                   {
8972                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
8973                      ListAdd(exp.list,
8974                         MkExpCondition(MkExpBrackets(MkListOne(
8975                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
8976                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
8977                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8978                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
8979                      exp.expType = ProcessTypeString("int", false);
8980                   }
8981
8982                   FreeExpression(tempExp1);
8983                   if(tempExp2) FreeExpression(tempExp2);
8984
8985                   FreeIdentifier(id);
8986                   break;
8987                }
8988             }
8989          }
8990
8991          {
8992             Type dummy
8993             {
8994                count = 1;
8995                refCount = 1;
8996             };
8997             if(!exp.call.exp.destType)
8998             {
8999                exp.call.exp.destType = dummy;
9000                dummy.refCount++;
9001             }
9002             ProcessExpressionType(exp.call.exp);
9003             if(exp.call.exp.destType == dummy)
9004             {
9005                FreeType(dummy);
9006                exp.call.exp.destType = null;
9007             }
9008             FreeType(dummy);
9009          }
9010
9011          // Check argument types against parameter types
9012          functionType = exp.call.exp.expType;
9013
9014          if(functionType && functionType.kind == TypeKind::methodType)
9015          {
9016             methodType = functionType;
9017             functionType = methodType.method.dataType;
9018
9019             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9020             // TOCHECK: Instead of doing this here could this be done per param?
9021             if(exp.call.exp.expType.usedClass)
9022             {
9023                char typeString[1024];
9024                typeString[0] = '\0';
9025                {
9026                   Symbol back = functionType.thisClass;
9027                   // Do not output class specifier here (thisclass was added to this)
9028                   functionType.thisClass = null;
9029                   PrintType(functionType, typeString, true, true);
9030                   functionType.thisClass = back;
9031                }
9032                if(strstr(typeString, "thisclass"))
9033                {
9034                   OldList * specs = MkList();
9035                   Declarator decl;
9036                   {
9037                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9038
9039                      decl = SpecDeclFromString(typeString, specs, null);
9040
9041                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9042                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9043                         exp.call.exp.expType.usedClass))
9044                         thisClassParams = false;
9045
9046                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9047                      {
9048                         Class backupThisClass = thisClass;
9049                         thisClass = exp.call.exp.expType.usedClass;
9050                         ProcessDeclarator(decl);
9051                         thisClass = backupThisClass;
9052                      }
9053
9054                      thisClassParams = true;
9055
9056                      functionType = ProcessType(specs, decl);
9057                      functionType.refCount = 0;
9058                      FinishTemplatesContext(context);
9059                   }
9060
9061                   FreeList(specs, FreeSpecifier);
9062                   FreeDeclarator(decl);
9063                 }
9064             }
9065          }
9066          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9067          {
9068             Type type = functionType.type;
9069             if(!functionType.refCount)
9070             {
9071                functionType.type = null;
9072                FreeType(functionType);
9073             }
9074             //methodType = functionType;
9075             functionType = type;
9076          }
9077          if(functionType && functionType.kind != TypeKind::functionType)
9078          {
9079             Compiler_Error($"called object %s is not a function\n", name);
9080          }
9081          else if(functionType)
9082          {
9083             bool emptyParams = false, noParams = false;
9084             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9085             Type type = functionType.params.first;
9086             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9087             int extra = 0;
9088             Location oldyylloc = yylloc;
9089
9090             if(!type) emptyParams = true;
9091
9092             // WORKING ON THIS:
9093             if(functionType.extraParam && e && functionType.thisClass)
9094             {
9095                e.destType = MkClassType(functionType.thisClass.string);
9096                e = e.next;
9097             }
9098
9099             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9100             // Fixed #141 by adding '&& !functionType.extraParam'
9101             if(!functionType.staticMethod && !functionType.extraParam)
9102             {
9103                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9104                   memberExp.member.exp.expType._class)
9105                {
9106                   type = MkClassType(memberExp.member.exp.expType._class.string);
9107                   if(e)
9108                   {
9109                      e.destType = type;
9110                      e = e.next;
9111                      type = functionType.params.first;
9112                   }
9113                   else
9114                      type.refCount = 0;
9115                }
9116                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9117                {
9118                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9119                   type.byReference = functionType.byReference;
9120                   type.typedByReference = functionType.typedByReference;
9121                   if(e)
9122                   {
9123                      // Allow manually passing a class for typed object
9124                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9125                         e = e.next;
9126                      e.destType = type;
9127                      e = e.next;
9128                      type = functionType.params.first;
9129                   }
9130                   else
9131                      type.refCount = 0;
9132                   //extra = 1;
9133                }
9134             }
9135
9136             if(type && type.kind == voidType)
9137             {
9138                noParams = true;
9139                if(!type.refCount) FreeType(type);
9140                type = null;
9141             }
9142
9143             for( ; e; e = e.next)
9144             {
9145                if(!type && !emptyParams)
9146                {
9147                   yylloc = e.loc;
9148                   if(methodType && methodType.methodClass)
9149                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9150                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9151                         noParams ? 0 : functionType.params.count);
9152                   else
9153                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9154                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9155                         noParams ? 0 : functionType.params.count);
9156                   break;
9157                }
9158
9159                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9160                {
9161                   Type templatedType = null;
9162                   Class _class = methodType.usedClass;
9163                   ClassTemplateParameter curParam = null;
9164                   int id = 0;
9165                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9166                   {
9167                      Class sClass;
9168                      for(sClass = _class; sClass; sClass = sClass.base)
9169                      {
9170                         if(sClass.templateClass) sClass = sClass.templateClass;
9171                         id = 0;
9172                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9173                         {
9174                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9175                            {
9176                               Class nextClass;
9177                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9178                               {
9179                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9180                                  id += nextClass.templateParams.count;
9181                               }
9182                               break;
9183                            }
9184                            id++;
9185                         }
9186                         if(curParam) break;
9187                      }
9188                   }
9189                   if(curParam && _class.templateArgs[id].dataTypeString)
9190                   {
9191                      ClassTemplateArgument arg = _class.templateArgs[id];
9192                      {
9193                         Context context = SetupTemplatesContext(_class);
9194
9195                         /*if(!arg.dataType)
9196                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9197                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9198                         FinishTemplatesContext(context);
9199                      }
9200                      e.destType = templatedType;
9201                      if(templatedType)
9202                      {
9203                         templatedType.passAsTemplate = true;
9204                         // templatedType.refCount++;
9205                      }
9206                   }
9207                   else
9208                   {
9209                      e.destType = type;
9210                      if(type) type.refCount++;
9211                   }
9212                }
9213                else
9214                {
9215                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9216                   {
9217                      e.destType = type.prev;
9218                      e.destType.refCount++;
9219                   }
9220                   else
9221                   {
9222                      e.destType = type;
9223                      if(type) type.refCount++;
9224                   }
9225                }
9226                // Don't reach the end for the ellipsis
9227                if(type && type.kind != ellipsisType)
9228                {
9229                   Type next = type.next;
9230                   if(!type.refCount) FreeType(type);
9231                   type = next;
9232                }
9233             }
9234
9235             if(type && type.kind != ellipsisType)
9236             {
9237                if(methodType && methodType.methodClass)
9238                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9239                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9240                      functionType.params.count + extra);
9241                else
9242                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9243                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9244                      functionType.params.count + extra);
9245             }
9246             yylloc = oldyylloc;
9247             if(type && !type.refCount) FreeType(type);
9248          }
9249          else
9250          {
9251             functionType = Type
9252             {
9253                refCount = 0;
9254                kind = TypeKind::functionType;
9255             };
9256
9257             if(exp.call.exp.type == identifierExp)
9258             {
9259                char * string = exp.call.exp.identifier.string;
9260                if(inCompiler)
9261                {
9262                   Symbol symbol;
9263                   Location oldyylloc = yylloc;
9264
9265                   yylloc = exp.call.exp.identifier.loc;
9266                   if(strstr(string, "__builtin_") == string)
9267                   {
9268                      if(exp.destType)
9269                      {
9270                         functionType.returnType = exp.destType;
9271                         exp.destType.refCount++;
9272                      }
9273                   }
9274                   else
9275                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9276                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9277                   globalContext.symbols.Add((BTNode)symbol);
9278                   if(strstr(symbol.string, "::"))
9279                      globalContext.hasNameSpace = true;
9280
9281                   yylloc = oldyylloc;
9282                }
9283             }
9284             else if(exp.call.exp.type == memberExp)
9285             {
9286                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9287                   exp.call.exp.member.member.string);*/
9288             }
9289             else
9290                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9291
9292             if(!functionType.returnType)
9293             {
9294                functionType.returnType = Type
9295                {
9296                   refCount = 1;
9297                   kind = intType;
9298                };
9299             }
9300          }
9301          if(functionType && functionType.kind == TypeKind::functionType)
9302          {
9303             exp.expType = functionType.returnType;
9304
9305             if(functionType.returnType)
9306                functionType.returnType.refCount++;
9307
9308             if(!functionType.refCount)
9309                FreeType(functionType);
9310          }
9311
9312          if(exp.call.arguments)
9313          {
9314             for(e = exp.call.arguments->first; e; e = e.next)
9315             {
9316                Type destType = e.destType;
9317                ProcessExpressionType(e);
9318             }
9319          }
9320          break;
9321       }
9322       case memberExp:
9323       {
9324          Type type;
9325          Location oldyylloc = yylloc;
9326          bool thisPtr;
9327          Expression checkExp = exp.member.exp;
9328          while(checkExp)
9329          {
9330             if(checkExp.type == castExp)
9331                checkExp = checkExp.cast.exp;
9332             else if(checkExp.type == bracketsExp)
9333                checkExp = checkExp.list ? checkExp.list->first : null;
9334             else
9335                break;
9336          }
9337
9338          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9339          exp.thisPtr = thisPtr;
9340
9341          // DOING THIS LATER NOW...
9342          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9343          {
9344             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9345             /* TODO: Name Space Fix ups
9346             if(!exp.member.member.classSym)
9347                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9348             */
9349          }
9350
9351          ProcessExpressionType(exp.member.exp);
9352          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9353             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9354          {
9355             exp.isConstant = false;
9356          }
9357          else
9358             exp.isConstant = exp.member.exp.isConstant;
9359          type = exp.member.exp.expType;
9360
9361          yylloc = exp.loc;
9362
9363          if(type && (type.kind == templateType))
9364          {
9365             Class _class = thisClass ? thisClass : currentClass;
9366             ClassTemplateParameter param = null;
9367             if(_class)
9368             {
9369                for(param = _class.templateParams.first; param; param = param.next)
9370                {
9371                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9372                      break;
9373                }
9374             }
9375             if(param && param.defaultArg.member)
9376             {
9377                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9378                if(argExp)
9379                {
9380                   Expression expMember = exp.member.exp;
9381                   Declarator decl;
9382                   OldList * specs = MkList();
9383                   char thisClassTypeString[1024];
9384
9385                   FreeIdentifier(exp.member.member);
9386
9387                   ProcessExpressionType(argExp);
9388
9389                   {
9390                      char * colon = strstr(param.defaultArg.memberString, "::");
9391                      if(colon)
9392                      {
9393                         char className[1024];
9394                         Class sClass;
9395
9396                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9397                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9398                      }
9399                      else
9400                         strcpy(thisClassTypeString, _class.fullName);
9401                   }
9402
9403                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9404
9405                   exp.expType = ProcessType(specs, decl);
9406                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9407                   {
9408                      Class expClass = exp.expType._class.registered;
9409                      Class cClass = null;
9410                      int c;
9411                      int paramCount = 0;
9412                      int lastParam = -1;
9413
9414                      char templateString[1024];
9415                      ClassTemplateParameter param;
9416                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9417                      for(cClass = expClass; cClass; cClass = cClass.base)
9418                      {
9419                         int p = 0;
9420                         for(param = cClass.templateParams.first; param; param = param.next)
9421                         {
9422                            int id = p;
9423                            Class sClass;
9424                            ClassTemplateArgument arg;
9425                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9426                            arg = expClass.templateArgs[id];
9427
9428                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9429                            {
9430                               ClassTemplateParameter cParam;
9431                               //int p = numParams - sClass.templateParams.count;
9432                               int p = 0;
9433                               Class nextClass;
9434                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9435
9436                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9437                               {
9438                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9439                                  {
9440                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9441                                     {
9442                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9443                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9444                                        break;
9445                                     }
9446                                  }
9447                               }
9448                            }
9449
9450                            {
9451                               char argument[256];
9452                               argument[0] = '\0';
9453                               /*if(arg.name)
9454                               {
9455                                  strcat(argument, arg.name.string);
9456                                  strcat(argument, " = ");
9457                               }*/
9458                               switch(param.type)
9459                               {
9460                                  case expression:
9461                                  {
9462                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9463                                     char expString[1024];
9464                                     OldList * specs = MkList();
9465                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9466                                     Expression exp;
9467                                     char * string = PrintHexUInt64(arg.expression.ui64);
9468                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9469                                     delete string;
9470
9471                                     ProcessExpressionType(exp);
9472                                     ComputeExpression(exp);
9473                                     expString[0] = '\0';
9474                                     PrintExpression(exp, expString);
9475                                     strcat(argument, expString);
9476                                     // delete exp;
9477                                     FreeExpression(exp);
9478                                     break;
9479                                  }
9480                                  case identifier:
9481                                  {
9482                                     strcat(argument, arg.member.name);
9483                                     break;
9484                                  }
9485                                  case TemplateParameterType::type:
9486                                  {
9487                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9488                                     {
9489                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9490                                           strcat(argument, thisClassTypeString);
9491                                        else
9492                                           strcat(argument, arg.dataTypeString);
9493                                     }
9494                                     break;
9495                                  }
9496                               }
9497                               if(argument[0])
9498                               {
9499                                  if(paramCount) strcat(templateString, ", ");
9500                                  if(lastParam != p - 1)
9501                                  {
9502                                     strcat(templateString, param.name);
9503                                     strcat(templateString, " = ");
9504                                  }
9505                                  strcat(templateString, argument);
9506                                  paramCount++;
9507                                  lastParam = p;
9508                               }
9509                               p++;
9510                            }
9511                         }
9512                      }
9513                      {
9514                         int len = strlen(templateString);
9515                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9516                         templateString[len++] = '>';
9517                         templateString[len++] = '\0';
9518                      }
9519                      {
9520                         Context context = SetupTemplatesContext(_class);
9521                         FreeType(exp.expType);
9522                         exp.expType = ProcessTypeString(templateString, false);
9523                         FinishTemplatesContext(context);
9524                      }
9525                   }
9526
9527                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9528                   exp.type = bracketsExp;
9529                   exp.list = MkListOne(MkExpOp(null, '*',
9530                   /*opExp;
9531                   exp.op.op = '*';
9532                   exp.op.exp1 = null;
9533                   exp.op.exp2 = */
9534                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9535                      MkExpBrackets(MkListOne(
9536                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9537                            '+',
9538                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9539                            '+',
9540                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9541
9542                            ));
9543                }
9544             }
9545             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9546                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9547             {
9548                type = ProcessTemplateParameterType(type.templateParameter);
9549             }
9550          }
9551          // TODO: *** This seems to be where we should add method support for all basic types ***
9552          if(type && (type.kind == templateType));
9553          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9554                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9555                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9556                           (type.kind == pointerType && type.type.kind == charType)))
9557          {
9558             Identifier id = exp.member.member;
9559             TypeKind typeKind = type.kind;
9560             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9561             if(typeKind == subClassType && exp.member.exp.type == classExp)
9562             {
9563                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9564                typeKind = classType;
9565             }
9566
9567             if(id)
9568             {
9569                if(typeKind == intType || typeKind == enumType)
9570                   _class = eSystem_FindClass(privateModule, "int");
9571                else if(!_class)
9572                {
9573                   if(type.kind == classType && type._class && type._class.registered)
9574                   {
9575                      _class = type._class.registered;
9576                   }
9577                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9578                   {
9579                      _class = FindClass("char *").registered;
9580                   }
9581                   else if(type.kind == pointerType)
9582                   {
9583                      _class = eSystem_FindClass(privateModule, "uintptr");
9584                      FreeType(exp.expType);
9585                      exp.expType = ProcessTypeString("uintptr", false);
9586                      exp.byReference = true;
9587                   }
9588                   else
9589                   {
9590                      char string[1024] = "";
9591                      Symbol classSym;
9592                      PrintTypeNoConst(type, string, false, true);
9593                      classSym = FindClass(string);
9594                      if(classSym) _class = classSym.registered;
9595                   }
9596                }
9597             }
9598
9599             if(_class && id)
9600             {
9601                /*bool thisPtr =
9602                   (exp.member.exp.type == identifierExp &&
9603                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9604                Property prop = null;
9605                Method method = null;
9606                DataMember member = null;
9607                Property revConvert = null;
9608                ClassProperty classProp = null;
9609
9610                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9611                   exp.member.memberType = propertyMember;
9612
9613                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9614                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9615
9616                if(typeKind != subClassType)
9617                {
9618                   // Prioritize data members over properties for "this"
9619                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9620                   {
9621                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9622                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9623                      {
9624                         prop = eClass_FindProperty(_class, id.string, privateModule);
9625                         if(prop)
9626                            member = null;
9627                      }
9628                      if(!member && !prop)
9629                         prop = eClass_FindProperty(_class, id.string, privateModule);
9630                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9631                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9632                         exp.member.thisPtr = true;
9633                   }
9634                   // Prioritize properties over data members otherwise
9635                   else
9636                   {
9637                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9638                      if(!id.classSym)
9639                      {
9640                         prop = eClass_FindProperty(_class, id.string, null);
9641                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9642                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9643                      }
9644
9645                      if(!prop && !member)
9646                      {
9647                         method = eClass_FindMethod(_class, id.string, null);
9648                         if(!method)
9649                         {
9650                            prop = eClass_FindProperty(_class, id.string, privateModule);
9651                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9652                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9653                         }
9654                      }
9655
9656                      if(member && prop)
9657                      {
9658                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9659                            prop = null;
9660                         else
9661                            member = null;
9662                      }
9663                   }
9664                }
9665                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9666                   method = eClass_FindMethod(_class, id.string, privateModule);
9667                if(!prop && !member && !method)
9668                {
9669                   if(typeKind == subClassType)
9670                   {
9671                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9672                      if(classProp)
9673                      {
9674                         exp.member.memberType = classPropertyMember;
9675                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9676                      }
9677                      else
9678                      {
9679                         // Assume this is a class_data member
9680                         char structName[1024];
9681                         Identifier id = exp.member.member;
9682                         Expression classExp = exp.member.exp;
9683                         type.refCount++;
9684
9685                         FreeType(classExp.expType);
9686                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9687
9688                         strcpy(structName, "__ecereClassData_");
9689                         FullClassNameCat(structName, type._class.string, false);
9690                         exp.type = pointerExp;
9691                         exp.member.member = id;
9692
9693                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9694                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9695                               MkExpBrackets(MkListOne(MkExpOp(
9696                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9697                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9698                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9699                                  )));
9700
9701                         FreeType(type);
9702
9703                         ProcessExpressionType(exp);
9704                         return;
9705                      }
9706                   }
9707                   else
9708                   {
9709                      // Check for reverse conversion
9710                      // (Convert in an instantiation later, so that we can use
9711                      //  deep properties system)
9712                      Symbol classSym = FindClass(id.string);
9713                      if(classSym)
9714                      {
9715                         Class convertClass = classSym.registered;
9716                         if(convertClass)
9717                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9718                      }
9719                   }
9720                }
9721
9722                if(prop)
9723                {
9724                   exp.member.memberType = propertyMember;
9725                   if(!prop.dataType)
9726                      ProcessPropertyType(prop);
9727                   exp.expType = prop.dataType;
9728                   if(prop.dataType) prop.dataType.refCount++;
9729                }
9730                else if(member)
9731                {
9732                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9733                   {
9734                      FreeExpContents(exp);
9735                      exp.type = identifierExp;
9736                      exp.identifier = MkIdentifier("class");
9737                      ProcessExpressionType(exp);
9738                      return;
9739                   }
9740
9741                   exp.member.memberType = dataMember;
9742                   DeclareStruct(_class.fullName, false);
9743                   if(!member.dataType)
9744                   {
9745                      Context context = SetupTemplatesContext(_class);
9746                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9747                      FinishTemplatesContext(context);
9748                   }
9749                   exp.expType = member.dataType;
9750                   if(member.dataType) member.dataType.refCount++;
9751                }
9752                else if(revConvert)
9753                {
9754                   exp.member.memberType = reverseConversionMember;
9755                   exp.expType = MkClassType(revConvert._class.fullName);
9756                }
9757                else if(method)
9758                {
9759                   //if(inCompiler)
9760                   {
9761                      /*if(id._class)
9762                      {
9763                         exp.type = identifierExp;
9764                         exp.identifier = exp.member.member;
9765                      }
9766                      else*/
9767                         exp.member.memberType = methodMember;
9768                   }
9769                   if(!method.dataType)
9770                      ProcessMethodType(method);
9771                   exp.expType = Type
9772                   {
9773                      refCount = 1;
9774                      kind = methodType;
9775                      method = method;
9776                   };
9777
9778                   // Tricky spot here... To use instance versus class virtual table
9779                   // Put it back to what it was... What did we break?
9780
9781                   // Had to put it back for overriding Main of Thread global instance
9782
9783                   //exp.expType.methodClass = _class;
9784                   exp.expType.methodClass = (id && id._class) ? _class : null;
9785
9786                   // Need the actual class used for templated classes
9787                   exp.expType.usedClass = _class;
9788                }
9789                else if(!classProp)
9790                {
9791                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9792                   {
9793                      FreeExpContents(exp);
9794                      exp.type = identifierExp;
9795                      exp.identifier = MkIdentifier("class");
9796                      FreeType(exp.expType);
9797                      exp.expType = MkClassType("ecere::com::Class");
9798                      return;
9799                   }
9800                   yylloc = exp.member.member.loc;
9801                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9802                   if(inCompiler)
9803                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9804                }
9805
9806                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9807                {
9808                   Class tClass;
9809
9810                   tClass = _class;
9811                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9812
9813                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9814                   {
9815                      int id = 0;
9816                      ClassTemplateParameter curParam = null;
9817                      Class sClass;
9818
9819                      for(sClass = tClass; sClass; sClass = sClass.base)
9820                      {
9821                         id = 0;
9822                         if(sClass.templateClass) sClass = sClass.templateClass;
9823                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9824                         {
9825                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9826                            {
9827                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9828                                  id += sClass.templateParams.count;
9829                               break;
9830                            }
9831                            id++;
9832                         }
9833                         if(curParam) break;
9834                      }
9835
9836                      if(curParam && tClass.templateArgs[id].dataTypeString)
9837                      {
9838                         ClassTemplateArgument arg = tClass.templateArgs[id];
9839                         Context context = SetupTemplatesContext(tClass);
9840                         /*if(!arg.dataType)
9841                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9842                         FreeType(exp.expType);
9843                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9844                         if(exp.expType)
9845                         {
9846                            if(exp.expType.kind == thisClassType)
9847                            {
9848                               FreeType(exp.expType);
9849                               exp.expType = ReplaceThisClassType(_class);
9850                            }
9851
9852                            if(tClass.templateClass)
9853                               exp.expType.passAsTemplate = true;
9854                            //exp.expType.refCount++;
9855                            if(!exp.destType)
9856                            {
9857                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9858                               //exp.destType.refCount++;
9859
9860                               if(exp.destType.kind == thisClassType)
9861                               {
9862                                  FreeType(exp.destType);
9863                                  exp.destType = ReplaceThisClassType(_class);
9864                               }
9865                            }
9866                         }
9867                         FinishTemplatesContext(context);
9868                      }
9869                   }
9870                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9871                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9872                   {
9873                      int id = 0;
9874                      ClassTemplateParameter curParam = null;
9875                      Class sClass;
9876
9877                      for(sClass = tClass; sClass; sClass = sClass.base)
9878                      {
9879                         id = 0;
9880                         if(sClass.templateClass) sClass = sClass.templateClass;
9881                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9882                         {
9883                            if(curParam.type == TemplateParameterType::type &&
9884                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9885                            {
9886                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9887                                  id += sClass.templateParams.count;
9888                               break;
9889                            }
9890                            id++;
9891                         }
9892                         if(curParam) break;
9893                      }
9894
9895                      if(curParam)
9896                      {
9897                         ClassTemplateArgument arg = tClass.templateArgs[id];
9898                         Context context = SetupTemplatesContext(tClass);
9899                         Type basicType;
9900                         /*if(!arg.dataType)
9901                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9902
9903                         basicType = ProcessTypeString(arg.dataTypeString, false);
9904                         if(basicType)
9905                         {
9906                            if(basicType.kind == thisClassType)
9907                            {
9908                               FreeType(basicType);
9909                               basicType = ReplaceThisClassType(_class);
9910                            }
9911
9912                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9913                            if(tClass.templateClass)
9914                               basicType.passAsTemplate = true;
9915                            */
9916
9917                            FreeType(exp.expType);
9918
9919                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9920                            //exp.expType.refCount++;
9921                            if(!exp.destType)
9922                            {
9923                               exp.destType = exp.expType;
9924                               exp.destType.refCount++;
9925                            }
9926
9927                            {
9928                               Expression newExp { };
9929                               OldList * specs = MkList();
9930                               Declarator decl;
9931                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
9932                               *newExp = *exp;
9933                               if(exp.destType) exp.destType.refCount++;
9934                               if(exp.expType)  exp.expType.refCount++;
9935                               exp.type = castExp;
9936                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
9937                               exp.cast.exp = newExp;
9938                               //FreeType(exp.expType);
9939                               //exp.expType = null;
9940                               //ProcessExpressionType(sourceExp);
9941                            }
9942                         }
9943                         FinishTemplatesContext(context);
9944                      }
9945                   }
9946                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
9947                   {
9948                      Class expClass = exp.expType._class.registered;
9949                      if(expClass)
9950                      {
9951                         Class cClass = null;
9952                         int c;
9953                         int p = 0;
9954                         int paramCount = 0;
9955                         int lastParam = -1;
9956                         char templateString[1024];
9957                         ClassTemplateParameter param;
9958                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
9959                         while(cClass != expClass)
9960                         {
9961                            Class sClass;
9962                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
9963                            cClass = sClass;
9964
9965                            for(param = cClass.templateParams.first; param; param = param.next)
9966                            {
9967                               Class cClassCur = null;
9968                               int c;
9969                               int cp = 0;
9970                               ClassTemplateParameter paramCur = null;
9971                               ClassTemplateArgument arg;
9972                               while(cClassCur != tClass && !paramCur)
9973                               {
9974                                  Class sClassCur;
9975                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
9976                                  cClassCur = sClassCur;
9977
9978                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
9979                                  {
9980                                     if(!strcmp(paramCur.name, param.name))
9981                                     {
9982
9983                                        break;
9984                                     }
9985                                     cp++;
9986                                  }
9987                               }
9988                               if(paramCur && paramCur.type == TemplateParameterType::type)
9989                                  arg = tClass.templateArgs[cp];
9990                               else
9991                                  arg = expClass.templateArgs[p];
9992
9993                               {
9994                                  char argument[256];
9995                                  argument[0] = '\0';
9996                                  /*if(arg.name)
9997                                  {
9998                                     strcat(argument, arg.name.string);
9999                                     strcat(argument, " = ");
10000                                  }*/
10001                                  switch(param.type)
10002                                  {
10003                                     case expression:
10004                                     {
10005                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10006                                        char expString[1024];
10007                                        OldList * specs = MkList();
10008                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10009                                        Expression exp;
10010                                        char * string = PrintHexUInt64(arg.expression.ui64);
10011                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10012                                        delete string;
10013
10014                                        ProcessExpressionType(exp);
10015                                        ComputeExpression(exp);
10016                                        expString[0] = '\0';
10017                                        PrintExpression(exp, expString);
10018                                        strcat(argument, expString);
10019                                        // delete exp;
10020                                        FreeExpression(exp);
10021                                        break;
10022                                     }
10023                                     case identifier:
10024                                     {
10025                                        strcat(argument, arg.member.name);
10026                                        break;
10027                                     }
10028                                     case TemplateParameterType::type:
10029                                     {
10030                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10031                                           strcat(argument, arg.dataTypeString);
10032                                        break;
10033                                     }
10034                                  }
10035                                  if(argument[0])
10036                                  {
10037                                     if(paramCount) strcat(templateString, ", ");
10038                                     if(lastParam != p - 1)
10039                                     {
10040                                        strcat(templateString, param.name);
10041                                        strcat(templateString, " = ");
10042                                     }
10043                                     strcat(templateString, argument);
10044                                     paramCount++;
10045                                     lastParam = p;
10046                                  }
10047                               }
10048                               p++;
10049                            }
10050                         }
10051                         {
10052                            int len = strlen(templateString);
10053                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10054                            templateString[len++] = '>';
10055                            templateString[len++] = '\0';
10056                         }
10057
10058                         FreeType(exp.expType);
10059                         {
10060                            Context context = SetupTemplatesContext(tClass);
10061                            exp.expType = ProcessTypeString(templateString, false);
10062                            FinishTemplatesContext(context);
10063                         }
10064                      }
10065                   }
10066                }
10067             }
10068             else
10069                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10070          }
10071          else if(type && (type.kind == structType || type.kind == unionType))
10072          {
10073             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10074             if(memberType)
10075             {
10076                exp.expType = memberType;
10077                if(memberType)
10078                   memberType.refCount++;
10079             }
10080          }
10081          else
10082          {
10083             char expString[10240];
10084             expString[0] = '\0';
10085             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10086             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10087          }
10088
10089          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10090          {
10091             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10092             {
10093                Identifier id = exp.member.member;
10094                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10095                if(_class)
10096                {
10097                   FreeType(exp.expType);
10098                   exp.expType = ReplaceThisClassType(_class);
10099                }
10100             }
10101          }
10102          yylloc = oldyylloc;
10103          break;
10104       }
10105       // Convert x->y into (*x).y
10106       case pointerExp:
10107       {
10108          Type destType = exp.destType;
10109
10110          // DOING THIS LATER NOW...
10111          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10112          {
10113             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10114             /* TODO: Name Space Fix ups
10115             if(!exp.member.member.classSym)
10116                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10117             */
10118          }
10119
10120          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10121          exp.type = memberExp;
10122          if(destType)
10123             destType.count++;
10124          ProcessExpressionType(exp);
10125          if(destType)
10126             destType.count--;
10127          break;
10128       }
10129       case classSizeExp:
10130       {
10131          //ComputeExpression(exp);
10132
10133          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10134          if(classSym && classSym.registered)
10135          {
10136             if(classSym.registered.type == noHeadClass)
10137             {
10138                char name[1024];
10139                name[0] = '\0';
10140                DeclareStruct(classSym.string, false);
10141                FreeSpecifier(exp._class);
10142                exp.type = typeSizeExp;
10143                FullClassNameCat(name, classSym.string, false);
10144                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10145             }
10146             else
10147             {
10148                if(classSym.registered.fixed)
10149                {
10150                   FreeSpecifier(exp._class);
10151                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10152                   exp.type = constantExp;
10153                }
10154                else
10155                {
10156                   char className[1024];
10157                   strcpy(className, "__ecereClass_");
10158                   FullClassNameCat(className, classSym.string, true);
10159                   MangleClassName(className);
10160
10161                   DeclareClass(classSym, className);
10162
10163                   FreeExpContents(exp);
10164                   exp.type = pointerExp;
10165                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10166                   exp.member.member = MkIdentifier("structSize");
10167                }
10168             }
10169          }
10170
10171          exp.expType = Type
10172          {
10173             refCount = 1;
10174             kind = intType;
10175          };
10176          // exp.isConstant = true;
10177          break;
10178       }
10179       case typeSizeExp:
10180       {
10181          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10182
10183          exp.expType = Type
10184          {
10185             refCount = 1;
10186             kind = intType;
10187          };
10188          exp.isConstant = true;
10189
10190          DeclareType(type, false, false);
10191          FreeType(type);
10192          break;
10193       }
10194       case castExp:
10195       {
10196          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10197          type.count = 1;
10198          FreeType(exp.cast.exp.destType);
10199          exp.cast.exp.destType = type;
10200          type.refCount++;
10201          ProcessExpressionType(exp.cast.exp);
10202          type.count = 0;
10203          exp.expType = type;
10204          //type.refCount++;
10205
10206          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10207          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10208          {
10209             void * prev = exp.prev, * next = exp.next;
10210             Type expType = exp.cast.exp.destType;
10211             Expression castExp = exp.cast.exp;
10212             Type destType = exp.destType;
10213
10214             if(expType) expType.refCount++;
10215
10216             //FreeType(exp.destType);
10217             FreeType(exp.expType);
10218             FreeTypeName(exp.cast.typeName);
10219
10220             *exp = *castExp;
10221             FreeType(exp.expType);
10222             FreeType(exp.destType);
10223
10224             exp.expType = expType;
10225             exp.destType = destType;
10226
10227             delete castExp;
10228
10229             exp.prev = prev;
10230             exp.next = next;
10231
10232          }
10233          else
10234          {
10235             exp.isConstant = exp.cast.exp.isConstant;
10236          }
10237          //FreeType(type);
10238          break;
10239       }
10240       case extensionInitializerExp:
10241       {
10242          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10243          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10244          // ProcessInitializer(exp.initializer.initializer, type);
10245          exp.expType = type;
10246          break;
10247       }
10248       case vaArgExp:
10249       {
10250          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10251          ProcessExpressionType(exp.vaArg.exp);
10252          exp.expType = type;
10253          break;
10254       }
10255       case conditionExp:
10256       {
10257          Expression e;
10258          exp.isConstant = true;
10259
10260          FreeType(exp.cond.cond.destType);
10261          exp.cond.cond.destType = MkClassType("bool");
10262          exp.cond.cond.destType.truth = true;
10263          ProcessExpressionType(exp.cond.cond);
10264          if(!exp.cond.cond.isConstant)
10265             exp.isConstant = false;
10266          for(e = exp.cond.exp->first; e; e = e.next)
10267          {
10268             if(!e.next)
10269             {
10270                FreeType(e.destType);
10271                e.destType = exp.destType;
10272                if(e.destType) e.destType.refCount++;
10273             }
10274             ProcessExpressionType(e);
10275             if(!e.next)
10276             {
10277                exp.expType = e.expType;
10278                if(e.expType) e.expType.refCount++;
10279             }
10280             if(!e.isConstant)
10281                exp.isConstant = false;
10282          }
10283
10284          FreeType(exp.cond.elseExp.destType);
10285          // Added this check if we failed to find an expType
10286          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10287
10288          // Reversed it...
10289          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10290
10291          if(exp.cond.elseExp.destType)
10292             exp.cond.elseExp.destType.refCount++;
10293          ProcessExpressionType(exp.cond.elseExp);
10294
10295          // FIXED THIS: Was done before calling process on elseExp
10296          if(!exp.cond.elseExp.isConstant)
10297             exp.isConstant = false;
10298          break;
10299       }
10300       case extensionCompoundExp:
10301       {
10302          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10303          {
10304             Statement last = exp.compound.compound.statements->last;
10305             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10306             {
10307                ((Expression)last.expressions->last).destType = exp.destType;
10308                if(exp.destType)
10309                   exp.destType.refCount++;
10310             }
10311             ProcessStatement(exp.compound);
10312             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10313             if(exp.expType)
10314                exp.expType.refCount++;
10315          }
10316          break;
10317       }
10318       case classExp:
10319       {
10320          Specifier spec = exp._classExp.specifiers->first;
10321          if(spec && spec.type == nameSpecifier)
10322          {
10323             exp.expType = MkClassType(spec.name);
10324             exp.expType.kind = subClassType;
10325             exp.byReference = true;
10326          }
10327          else
10328          {
10329             exp.expType = MkClassType("ecere::com::Class");
10330             exp.byReference = true;
10331          }
10332          break;
10333       }
10334       case classDataExp:
10335       {
10336          Class _class = thisClass ? thisClass : currentClass;
10337          if(_class)
10338          {
10339             Identifier id = exp.classData.id;
10340             char structName[1024];
10341             Expression classExp;
10342             strcpy(structName, "__ecereClassData_");
10343             FullClassNameCat(structName, _class.fullName, false);
10344             exp.type = pointerExp;
10345             exp.member.member = id;
10346             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10347                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10348             else
10349                classExp = MkExpIdentifier(MkIdentifier("class"));
10350
10351             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10352                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10353                   MkExpBrackets(MkListOne(MkExpOp(
10354                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10355                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10356                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10357                      )));
10358
10359             ProcessExpressionType(exp);
10360             return;
10361          }
10362          break;
10363       }
10364       case arrayExp:
10365       {
10366          Type type = null;
10367          char * typeString = null;
10368          char typeStringBuf[1024];
10369          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10370             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10371          {
10372             Class templateClass = exp.destType._class.registered;
10373             typeString = templateClass.templateArgs[2].dataTypeString;
10374          }
10375          else if(exp.list)
10376          {
10377             // Guess type from expressions in the array
10378             Expression e;
10379             for(e = exp.list->first; e; e = e.next)
10380             {
10381                ProcessExpressionType(e);
10382                if(e.expType)
10383                {
10384                   if(!type) { type = e.expType; type.refCount++; }
10385                   else
10386                   {
10387                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10388                      if(!MatchTypeExpression(e, type, null, false))
10389                      {
10390                         FreeType(type);
10391                         type = e.expType;
10392                         e.expType = null;
10393
10394                         e = exp.list->first;
10395                         ProcessExpressionType(e);
10396                         if(e.expType)
10397                         {
10398                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10399                            if(!MatchTypeExpression(e, type, null, false))
10400                            {
10401                               FreeType(e.expType);
10402                               e.expType = null;
10403                               FreeType(type);
10404                               type = null;
10405                               break;
10406                            }
10407                         }
10408                      }
10409                   }
10410                   if(e.expType)
10411                   {
10412                      FreeType(e.expType);
10413                      e.expType = null;
10414                   }
10415                }
10416             }
10417             if(type)
10418             {
10419                typeStringBuf[0] = '\0';
10420                PrintTypeNoConst(type, typeStringBuf, false, true);
10421                typeString = typeStringBuf;
10422                FreeType(type);
10423                type = null;
10424             }
10425          }
10426          if(typeString)
10427          {
10428             /*
10429             (Container)& (struct BuiltInContainer)
10430             {
10431                ._vTbl = class(BuiltInContainer)._vTbl,
10432                ._class = class(BuiltInContainer),
10433                .refCount = 0,
10434                .data = (int[]){ 1, 7, 3, 4, 5 },
10435                .count = 5,
10436                .type = class(int),
10437             }
10438             */
10439             char templateString[1024];
10440             OldList * initializers = MkList();
10441             OldList * structInitializers = MkList();
10442             OldList * specs = MkList();
10443             Expression expExt;
10444             Declarator decl = SpecDeclFromString(typeString, specs, null);
10445             sprintf(templateString, "Container<%s>", typeString);
10446
10447             if(exp.list)
10448             {
10449                Expression e;
10450                type = ProcessTypeString(typeString, false);
10451                while(e = exp.list->first)
10452                {
10453                   exp.list->Remove(e);
10454                   e.destType = type;
10455                   type.refCount++;
10456                   ProcessExpressionType(e);
10457                   ListAdd(initializers, MkInitializerAssignment(e));
10458                }
10459                FreeType(type);
10460                delete exp.list;
10461             }
10462
10463             DeclareStruct("ecere::com::BuiltInContainer", false);
10464
10465             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10466                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10467             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10468                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10469             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10470                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10471             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10472                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10473                MkInitializerList(initializers))));
10474                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10475             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10476                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10477             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10478                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10479             exp.expType = ProcessTypeString(templateString, false);
10480             exp.type = bracketsExp;
10481             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10482                MkExpOp(null, '&',
10483                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10484                   MkInitializerList(structInitializers)))));
10485             ProcessExpressionType(expExt);
10486          }
10487          else
10488          {
10489             exp.expType = ProcessTypeString("Container", false);
10490             Compiler_Error($"Couldn't determine type of array elements\n");
10491          }
10492          break;
10493       }
10494    }
10495
10496    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10497    {
10498       FreeType(exp.expType);
10499       exp.expType = ReplaceThisClassType(thisClass);
10500    }
10501
10502    // Resolve structures here
10503    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10504    {
10505       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10506       // TODO: Fix members reference...
10507       if(symbol)
10508       {
10509          if(exp.expType.kind != enumType)
10510          {
10511             Type member;
10512             String enumName = CopyString(exp.expType.enumName);
10513
10514             // Fixed a memory leak on self-referencing C structs typedefs
10515             // by instantiating a new type rather than simply copying members
10516             // into exp.expType
10517             FreeType(exp.expType);
10518             exp.expType = Type { };
10519             exp.expType.kind = symbol.type.kind;
10520             exp.expType.refCount++;
10521             exp.expType.enumName = enumName;
10522
10523             exp.expType.members = symbol.type.members;
10524             for(member = symbol.type.members.first; member; member = member.next)
10525                member.refCount++;
10526          }
10527          else
10528          {
10529             NamedLink member;
10530             for(member = symbol.type.members.first; member; member = member.next)
10531             {
10532                NamedLink value { name = CopyString(member.name) };
10533                exp.expType.members.Add(value);
10534             }
10535          }
10536       }
10537    }
10538
10539    yylloc = exp.loc;
10540    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10541    else if(exp.destType && !exp.destType.keepCast)
10542    {
10543       if(!CheckExpressionType(exp, exp.destType, false))
10544       {
10545          if(!exp.destType.count || unresolved)
10546          {
10547             if(!exp.expType)
10548             {
10549                yylloc = exp.loc;
10550                if(exp.destType.kind != ellipsisType)
10551                {
10552                   char type2[1024];
10553                   type2[0] = '\0';
10554                   if(inCompiler)
10555                   {
10556                      char expString[10240];
10557                      expString[0] = '\0';
10558
10559                      PrintType(exp.destType, type2, false, true);
10560
10561                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10562                      if(unresolved)
10563                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10564                      else if(exp.type != dummyExp)
10565                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10566                   }
10567                }
10568                else
10569                {
10570                   char expString[10240] ;
10571                   expString[0] = '\0';
10572                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10573
10574                   if(unresolved)
10575                      Compiler_Error($"unresolved identifier %s\n", expString);
10576                   else if(exp.type != dummyExp)
10577                      Compiler_Error($"couldn't determine type of %s\n", expString);
10578                }
10579             }
10580             else
10581             {
10582                char type1[1024];
10583                char type2[1024];
10584                type1[0] = '\0';
10585                type2[0] = '\0';
10586                if(inCompiler)
10587                {
10588                   PrintType(exp.expType, type1, false, true);
10589                   PrintType(exp.destType, type2, false, true);
10590                }
10591
10592                //CheckExpressionType(exp, exp.destType, false);
10593
10594                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10595                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10596                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10597                else
10598                {
10599                   char expString[10240];
10600                   expString[0] = '\0';
10601                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10602
10603 #ifdef _DEBUG
10604                   CheckExpressionType(exp, exp.destType, false);
10605 #endif
10606                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10607                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10608                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10609
10610                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10611                   FreeType(exp.expType);
10612                   exp.destType.refCount++;
10613                   exp.expType = exp.destType;
10614                }
10615             }
10616          }
10617       }
10618       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10619       {
10620          Expression newExp { };
10621          char typeString[1024];
10622          OldList * specs = MkList();
10623          Declarator decl;
10624
10625          typeString[0] = '\0';
10626
10627          *newExp = *exp;
10628
10629          if(exp.expType)  exp.expType.refCount++;
10630          if(exp.expType)  exp.expType.refCount++;
10631          exp.type = castExp;
10632          newExp.destType = exp.expType;
10633
10634          PrintType(exp.expType, typeString, false, false);
10635          decl = SpecDeclFromString(typeString, specs, null);
10636
10637          exp.cast.typeName = MkTypeName(specs, decl);
10638          exp.cast.exp = newExp;
10639       }
10640    }
10641    else if(unresolved)
10642    {
10643       if(exp.identifier._class && exp.identifier._class.name)
10644          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10645       else if(exp.identifier.string && exp.identifier.string[0])
10646          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10647    }
10648    else if(!exp.expType && exp.type != dummyExp)
10649    {
10650       char expString[10240];
10651       expString[0] = '\0';
10652       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10653       Compiler_Error($"couldn't determine type of %s\n", expString);
10654    }
10655
10656    // Let's try to support any_object & typed_object here:
10657    if(inCompiler)
10658       ApplyAnyObjectLogic(exp);
10659
10660    // Mark nohead classes as by reference, unless we're casting them to an integral type
10661    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10662       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10663          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10664           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10665    {
10666       exp.byReference = true;
10667    }
10668    yylloc = oldyylloc;
10669 }
10670
10671 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10672 {
10673    // THIS CODE WILL FIND NEXT MEMBER...
10674    if(*curMember)
10675    {
10676       *curMember = (*curMember).next;
10677
10678       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10679       {
10680          *curMember = subMemberStack[--(*subMemberStackPos)];
10681          *curMember = (*curMember).next;
10682       }
10683
10684       // SKIP ALL PROPERTIES HERE...
10685       while((*curMember) && (*curMember).isProperty)
10686          *curMember = (*curMember).next;
10687
10688       if(subMemberStackPos)
10689       {
10690          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10691          {
10692             subMemberStack[(*subMemberStackPos)++] = *curMember;
10693
10694             *curMember = (*curMember).members.first;
10695             while(*curMember && (*curMember).isProperty)
10696                *curMember = (*curMember).next;
10697          }
10698       }
10699    }
10700    while(!*curMember)
10701    {
10702       if(!*curMember)
10703       {
10704          if(subMemberStackPos && *subMemberStackPos)
10705          {
10706             *curMember = subMemberStack[--(*subMemberStackPos)];
10707             *curMember = (*curMember).next;
10708          }
10709          else
10710          {
10711             Class lastCurClass = *curClass;
10712
10713             if(*curClass == _class) break;     // REACHED THE END
10714
10715             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10716             *curMember = (*curClass).membersAndProperties.first;
10717          }
10718
10719          while((*curMember) && (*curMember).isProperty)
10720             *curMember = (*curMember).next;
10721          if(subMemberStackPos)
10722          {
10723             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10724             {
10725                subMemberStack[(*subMemberStackPos)++] = *curMember;
10726
10727                *curMember = (*curMember).members.first;
10728                while(*curMember && (*curMember).isProperty)
10729                   *curMember = (*curMember).next;
10730             }
10731          }
10732       }
10733    }
10734 }
10735
10736
10737 static void ProcessInitializer(Initializer init, Type type)
10738 {
10739    switch(init.type)
10740    {
10741       case expInitializer:
10742          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10743          {
10744             // TESTING THIS FOR SHUTTING = 0 WARNING
10745             if(init.exp && !init.exp.destType)
10746             {
10747                FreeType(init.exp.destType);
10748                init.exp.destType = type;
10749                if(type) type.refCount++;
10750             }
10751             if(init.exp)
10752             {
10753                ProcessExpressionType(init.exp);
10754                init.isConstant = init.exp.isConstant;
10755             }
10756             break;
10757          }
10758          else
10759          {
10760             Expression exp = init.exp;
10761             Instantiation inst = exp.instance;
10762             MembersInit members;
10763
10764             init.type = listInitializer;
10765             init.list = MkList();
10766
10767             if(inst.members)
10768             {
10769                for(members = inst.members->first; members; members = members.next)
10770                {
10771                   if(members.type == dataMembersInit)
10772                   {
10773                      MemberInit member;
10774                      for(member = members.dataMembers->first; member; member = member.next)
10775                      {
10776                         ListAdd(init.list, member.initializer);
10777                         member.initializer = null;
10778                      }
10779                   }
10780                   // Discard all MembersInitMethod
10781                }
10782             }
10783             FreeExpression(exp);
10784          }
10785       case listInitializer:
10786       {
10787          Initializer i;
10788          Type initializerType = null;
10789          Class curClass = null;
10790          DataMember curMember = null;
10791          DataMember subMemberStack[256];
10792          int subMemberStackPos = 0;
10793
10794          if(type && type.kind == arrayType)
10795             initializerType = Dereference(type);
10796          else if(type && (type.kind == structType || type.kind == unionType))
10797             initializerType = type.members.first;
10798
10799          for(i = init.list->first; i; i = i.next)
10800          {
10801             if(type && type.kind == classType && type._class && type._class.registered)
10802             {
10803                // 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)
10804                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10805                // TODO: Generate error on initializing a private data member this way from another module...
10806                if(curMember)
10807                {
10808                   if(!curMember.dataType)
10809                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10810                   initializerType = curMember.dataType;
10811                }
10812             }
10813             ProcessInitializer(i, initializerType);
10814             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10815                initializerType = initializerType.next;
10816             if(!i.isConstant)
10817                init.isConstant = false;
10818          }
10819
10820          if(type && type.kind == arrayType)
10821             FreeType(initializerType);
10822
10823          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10824          {
10825             Compiler_Error($"Assigning list initializer to non list\n");
10826          }
10827          break;
10828       }
10829    }
10830 }
10831
10832 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10833 {
10834    switch(spec.type)
10835    {
10836       case baseSpecifier:
10837       {
10838          if(spec.specifier == THISCLASS)
10839          {
10840             if(thisClass)
10841             {
10842                spec.type = nameSpecifier;
10843                spec.name = ReplaceThisClass(thisClass);
10844                spec.symbol = FindClass(spec.name);
10845                ProcessSpecifier(spec, declareStruct);
10846             }
10847          }
10848          break;
10849       }
10850       case nameSpecifier:
10851       {
10852          Symbol symbol = FindType(curContext, spec.name);
10853          if(symbol)
10854             DeclareType(symbol.type, true, true);
10855          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10856             DeclareStruct(spec.name, false);
10857          break;
10858       }
10859       case enumSpecifier:
10860       {
10861          Enumerator e;
10862          if(spec.list)
10863          {
10864             for(e = spec.list->first; e; e = e.next)
10865             {
10866                if(e.exp)
10867                   ProcessExpressionType(e.exp);
10868             }
10869          }
10870          break;
10871       }
10872       case structSpecifier:
10873       case unionSpecifier:
10874       {
10875          if(spec.definitions)
10876          {
10877             ClassDef def;
10878             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10879             //if(symbol)
10880                ProcessClass(spec.definitions, symbol);
10881             /*else
10882             {
10883                for(def = spec.definitions->first; def; def = def.next)
10884                {
10885                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10886                      ProcessDeclaration(def.decl);
10887                }
10888             }*/
10889          }
10890          break;
10891       }
10892       /*
10893       case classSpecifier:
10894       {
10895          Symbol classSym = FindClass(spec.name);
10896          if(classSym && classSym.registered && classSym.registered.type == structClass)
10897             DeclareStruct(spec.name, false);
10898          break;
10899       }
10900       */
10901    }
10902 }
10903
10904
10905 static void ProcessDeclarator(Declarator decl)
10906 {
10907    switch(decl.type)
10908    {
10909       case identifierDeclarator:
10910          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10911          {
10912             FreeSpecifier(decl.identifier._class);
10913             decl.identifier._class = null;
10914          }
10915          break;
10916       case arrayDeclarator:
10917          if(decl.array.exp)
10918             ProcessExpressionType(decl.array.exp);
10919       case structDeclarator:
10920       case bracketsDeclarator:
10921       case functionDeclarator:
10922       case pointerDeclarator:
10923       case extendedDeclarator:
10924       case extendedDeclaratorEnd:
10925          if(decl.declarator)
10926             ProcessDeclarator(decl.declarator);
10927          if(decl.type == functionDeclarator)
10928          {
10929             Identifier id = GetDeclId(decl);
10930             if(id && id._class)
10931             {
10932                TypeName param
10933                {
10934                   qualifiers = MkListOne(id._class);
10935                   declarator = null;
10936                };
10937                if(!decl.function.parameters)
10938                   decl.function.parameters = MkList();
10939                decl.function.parameters->Insert(null, param);
10940                id._class = null;
10941             }
10942             if(decl.function.parameters)
10943             {
10944                TypeName param;
10945
10946                for(param = decl.function.parameters->first; param; param = param.next)
10947                {
10948                   if(param.qualifiers && param.qualifiers->first)
10949                   {
10950                      Specifier spec = param.qualifiers->first;
10951                      if(spec && spec.specifier == TYPED_OBJECT)
10952                      {
10953                         Declarator d = param.declarator;
10954                         TypeName newParam
10955                         {
10956                            qualifiers = MkListOne(MkSpecifier(VOID));
10957                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10958                         };
10959
10960                         FreeList(param.qualifiers, FreeSpecifier);
10961
10962                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
10963                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
10964
10965                         decl.function.parameters->Insert(param, newParam);
10966                         param = newParam;
10967                      }
10968                      else if(spec && spec.specifier == ANY_OBJECT)
10969                      {
10970                         Declarator d = param.declarator;
10971
10972                         FreeList(param.qualifiers, FreeSpecifier);
10973
10974                         param.qualifiers = MkListOne(MkSpecifier(VOID));
10975                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10976                      }
10977                      else if(spec.specifier == THISCLASS)
10978                      {
10979                         if(thisClass)
10980                         {
10981                            spec.type = nameSpecifier;
10982                            spec.name = ReplaceThisClass(thisClass);
10983                            spec.symbol = FindClass(spec.name);
10984                            ProcessSpecifier(spec, false);
10985                         }
10986                      }
10987                   }
10988
10989                   if(param.declarator)
10990                      ProcessDeclarator(param.declarator);
10991                }
10992             }
10993          }
10994          break;
10995    }
10996 }
10997
10998 static void ProcessDeclaration(Declaration decl)
10999 {
11000    yylloc = decl.loc;
11001    switch(decl.type)
11002    {
11003       case initDeclaration:
11004       {
11005          bool declareStruct = false;
11006          /*
11007          lineNum = decl.pos.line;
11008          column = decl.pos.col;
11009          */
11010
11011          if(decl.declarators)
11012          {
11013             InitDeclarator d;
11014
11015             for(d = decl.declarators->first; d; d = d.next)
11016             {
11017                Type type, subType;
11018                ProcessDeclarator(d.declarator);
11019
11020                type = ProcessType(decl.specifiers, d.declarator);
11021
11022                if(d.initializer)
11023                {
11024                   ProcessInitializer(d.initializer, type);
11025
11026                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11027
11028                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11029                      d.initializer.exp.type == instanceExp)
11030                   {
11031                      if(type.kind == classType && type._class ==
11032                         d.initializer.exp.expType._class)
11033                      {
11034                         Instantiation inst = d.initializer.exp.instance;
11035                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11036
11037                         d.initializer.exp.instance = null;
11038                         if(decl.specifiers)
11039                            FreeList(decl.specifiers, FreeSpecifier);
11040                         FreeList(decl.declarators, FreeInitDeclarator);
11041
11042                         d = null;
11043
11044                         decl.type = instDeclaration;
11045                         decl.inst = inst;
11046                      }
11047                   }
11048                }
11049                for(subType = type; subType;)
11050                {
11051                   if(subType.kind == classType)
11052                   {
11053                      declareStruct = true;
11054                      break;
11055                   }
11056                   else if(subType.kind == pointerType)
11057                      break;
11058                   else if(subType.kind == arrayType)
11059                      subType = subType.arrayType;
11060                   else
11061                      break;
11062                }
11063
11064                FreeType(type);
11065                if(!d) break;
11066             }
11067          }
11068
11069          if(decl.specifiers)
11070          {
11071             Specifier s;
11072             for(s = decl.specifiers->first; s; s = s.next)
11073             {
11074                ProcessSpecifier(s, declareStruct);
11075             }
11076          }
11077          break;
11078       }
11079       case instDeclaration:
11080       {
11081          ProcessInstantiationType(decl.inst);
11082          break;
11083       }
11084       case structDeclaration:
11085       {
11086          Specifier spec;
11087          Declarator d;
11088          bool declareStruct = false;
11089
11090          if(decl.declarators)
11091          {
11092             for(d = decl.declarators->first; d; d = d.next)
11093             {
11094                Type type = ProcessType(decl.specifiers, d.declarator);
11095                Type subType;
11096                ProcessDeclarator(d);
11097                for(subType = type; subType;)
11098                {
11099                   if(subType.kind == classType)
11100                   {
11101                      declareStruct = true;
11102                      break;
11103                   }
11104                   else if(subType.kind == pointerType)
11105                      break;
11106                   else if(subType.kind == arrayType)
11107                      subType = subType.arrayType;
11108                   else
11109                      break;
11110                }
11111                FreeType(type);
11112             }
11113          }
11114          if(decl.specifiers)
11115          {
11116             for(spec = decl.specifiers->first; spec; spec = spec.next)
11117                ProcessSpecifier(spec, declareStruct);
11118          }
11119          break;
11120       }
11121    }
11122 }
11123
11124 static FunctionDefinition curFunction;
11125
11126 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11127 {
11128    char propName[1024], propNameM[1024];
11129    char getName[1024], setName[1024];
11130    OldList * args;
11131
11132    DeclareProperty(prop, setName, getName);
11133
11134    // eInstance_FireWatchers(object, prop);
11135    strcpy(propName, "__ecereProp_");
11136    FullClassNameCat(propName, prop._class.fullName, false);
11137    strcat(propName, "_");
11138    // strcat(propName, prop.name);
11139    FullClassNameCat(propName, prop.name, true);
11140    MangleClassName(propName);
11141
11142    strcpy(propNameM, "__ecerePropM_");
11143    FullClassNameCat(propNameM, prop._class.fullName, false);
11144    strcat(propNameM, "_");
11145    // strcat(propNameM, prop.name);
11146    FullClassNameCat(propNameM, prop.name, true);
11147    MangleClassName(propNameM);
11148
11149    if(prop.isWatchable)
11150    {
11151       args = MkList();
11152       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11153       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11154       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11155
11156       args = MkList();
11157       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11158       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11159       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11160    }
11161
11162
11163    {
11164       args = MkList();
11165       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11166       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11167       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11168
11169       args = MkList();
11170       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11171       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11172       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11173    }
11174
11175    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11176       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11177       curFunction.propSet.fireWatchersDone = true;
11178 }
11179
11180 static void ProcessStatement(Statement stmt)
11181 {
11182    yylloc = stmt.loc;
11183    /*
11184    lineNum = stmt.pos.line;
11185    column = stmt.pos.col;
11186    */
11187    switch(stmt.type)
11188    {
11189       case labeledStmt:
11190          ProcessStatement(stmt.labeled.stmt);
11191          break;
11192       case caseStmt:
11193          // This expression should be constant...
11194          if(stmt.caseStmt.exp)
11195          {
11196             FreeType(stmt.caseStmt.exp.destType);
11197             stmt.caseStmt.exp.destType = curSwitchType;
11198             if(curSwitchType) curSwitchType.refCount++;
11199             ProcessExpressionType(stmt.caseStmt.exp);
11200             ComputeExpression(stmt.caseStmt.exp);
11201          }
11202          if(stmt.caseStmt.stmt)
11203             ProcessStatement(stmt.caseStmt.stmt);
11204          break;
11205       case compoundStmt:
11206       {
11207          if(stmt.compound.context)
11208          {
11209             Declaration decl;
11210             Statement s;
11211
11212             Statement prevCompound = curCompound;
11213             Context prevContext = curContext;
11214
11215             if(!stmt.compound.isSwitch)
11216                curCompound = stmt;
11217             curContext = stmt.compound.context;
11218
11219             if(stmt.compound.declarations)
11220             {
11221                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11222                   ProcessDeclaration(decl);
11223             }
11224             if(stmt.compound.statements)
11225             {
11226                for(s = stmt.compound.statements->first; s; s = s.next)
11227                   ProcessStatement(s);
11228             }
11229
11230             curContext = prevContext;
11231             curCompound = prevCompound;
11232          }
11233          break;
11234       }
11235       case expressionStmt:
11236       {
11237          Expression exp;
11238          if(stmt.expressions)
11239          {
11240             for(exp = stmt.expressions->first; exp; exp = exp.next)
11241                ProcessExpressionType(exp);
11242          }
11243          break;
11244       }
11245       case ifStmt:
11246       {
11247          Expression exp;
11248
11249          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11250          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11251          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11252          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11253          {
11254             ProcessExpressionType(exp);
11255          }
11256          if(stmt.ifStmt.stmt)
11257             ProcessStatement(stmt.ifStmt.stmt);
11258          if(stmt.ifStmt.elseStmt)
11259             ProcessStatement(stmt.ifStmt.elseStmt);
11260          break;
11261       }
11262       case switchStmt:
11263       {
11264          Type oldSwitchType = curSwitchType;
11265          if(stmt.switchStmt.exp)
11266          {
11267             Expression exp;
11268             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11269             {
11270                if(!exp.next)
11271                {
11272                   /*
11273                   Type destType
11274                   {
11275                      kind = intType;
11276                      refCount = 1;
11277                   };
11278                   e.exp.destType = destType;
11279                   */
11280
11281                   ProcessExpressionType(exp);
11282                }
11283                if(!exp.next)
11284                   curSwitchType = exp.expType;
11285             }
11286          }
11287          ProcessStatement(stmt.switchStmt.stmt);
11288          curSwitchType = oldSwitchType;
11289          break;
11290       }
11291       case whileStmt:
11292       {
11293          if(stmt.whileStmt.exp)
11294          {
11295             Expression exp;
11296
11297             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11298             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11299             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11300             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11301             {
11302                ProcessExpressionType(exp);
11303             }
11304          }
11305          if(stmt.whileStmt.stmt)
11306             ProcessStatement(stmt.whileStmt.stmt);
11307          break;
11308       }
11309       case doWhileStmt:
11310       {
11311          if(stmt.doWhile.exp)
11312          {
11313             Expression exp;
11314
11315             if(stmt.doWhile.exp->last)
11316             {
11317                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11318                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11319                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11320             }
11321             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11322             {
11323                ProcessExpressionType(exp);
11324             }
11325          }
11326          if(stmt.doWhile.stmt)
11327             ProcessStatement(stmt.doWhile.stmt);
11328          break;
11329       }
11330       case forStmt:
11331       {
11332          Expression exp;
11333          if(stmt.forStmt.init)
11334             ProcessStatement(stmt.forStmt.init);
11335
11336          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11337          {
11338             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11339             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11340             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11341          }
11342
11343          if(stmt.forStmt.check)
11344             ProcessStatement(stmt.forStmt.check);
11345          if(stmt.forStmt.increment)
11346          {
11347             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11348                ProcessExpressionType(exp);
11349          }
11350
11351          if(stmt.forStmt.stmt)
11352             ProcessStatement(stmt.forStmt.stmt);
11353          break;
11354       }
11355       case forEachStmt:
11356       {
11357          Identifier id = stmt.forEachStmt.id;
11358          OldList * exp = stmt.forEachStmt.exp;
11359          OldList * filter = stmt.forEachStmt.filter;
11360          Statement block = stmt.forEachStmt.stmt;
11361          char iteratorType[1024];
11362          Type source;
11363          Expression e;
11364          bool isBuiltin = exp && exp->last &&
11365             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11366               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11367          Expression arrayExp;
11368          char * typeString = null;
11369          int builtinCount = 0;
11370
11371          for(e = exp ? exp->first : null; e; e = e.next)
11372          {
11373             if(!e.next)
11374             {
11375                FreeType(e.destType);
11376                e.destType = ProcessTypeString("Container", false);
11377             }
11378             if(!isBuiltin || e.next)
11379                ProcessExpressionType(e);
11380          }
11381
11382          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11383          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11384             eClass_IsDerived(source._class.registered, containerClass)))
11385          {
11386             Class _class = source ? source._class.registered : null;
11387             Symbol symbol;
11388             Expression expIt = null;
11389             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11390             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11391             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11392             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11393             stmt.type = compoundStmt;
11394
11395             stmt.compound.context = Context { };
11396             stmt.compound.context.parent = curContext;
11397             curContext = stmt.compound.context;
11398
11399             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11400             {
11401                Class mapClass = eSystem_FindClass(privateModule, "Map");
11402                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11403                isCustomAVLTree = true;
11404                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11405                   isAVLTree = true;
11406                else if(eClass_IsDerived(source._class.registered, mapClass))
11407                   isMap = true;
11408             }
11409             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11410             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11411             {
11412                Class listClass = eSystem_FindClass(privateModule, "List");
11413                isLinkList = true;
11414                isList = eClass_IsDerived(source._class.registered, listClass);
11415             }
11416
11417             if(isArray)
11418             {
11419                Declarator decl;
11420                OldList * specs = MkList();
11421                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11422                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11423                stmt.compound.declarations = MkListOne(
11424                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11425                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11426                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11427                      MkInitializerAssignment(MkExpBrackets(exp))))));
11428             }
11429             else if(isBuiltin)
11430             {
11431                Type type = null;
11432                char typeStringBuf[1024];
11433
11434                // TODO: Merge this code?
11435                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11436                if(((Expression)exp->last).type == castExp)
11437                {
11438                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11439                   if(typeName)
11440                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11441                }
11442
11443                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11444                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11445                   arrayExp.destType._class.registered.templateArgs)
11446                {
11447                   Class templateClass = arrayExp.destType._class.registered;
11448                   typeString = templateClass.templateArgs[2].dataTypeString;
11449                }
11450                else if(arrayExp.list)
11451                {
11452                   // Guess type from expressions in the array
11453                   Expression e;
11454                   for(e = arrayExp.list->first; e; e = e.next)
11455                   {
11456                      ProcessExpressionType(e);
11457                      if(e.expType)
11458                      {
11459                         if(!type) { type = e.expType; type.refCount++; }
11460                         else
11461                         {
11462                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11463                            if(!MatchTypeExpression(e, type, null, false))
11464                            {
11465                               FreeType(type);
11466                               type = e.expType;
11467                               e.expType = null;
11468
11469                               e = arrayExp.list->first;
11470                               ProcessExpressionType(e);
11471                               if(e.expType)
11472                               {
11473                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11474                                  if(!MatchTypeExpression(e, type, null, false))
11475                                  {
11476                                     FreeType(e.expType);
11477                                     e.expType = null;
11478                                     FreeType(type);
11479                                     type = null;
11480                                     break;
11481                                  }
11482                               }
11483                            }
11484                         }
11485                         if(e.expType)
11486                         {
11487                            FreeType(e.expType);
11488                            e.expType = null;
11489                         }
11490                      }
11491                   }
11492                   if(type)
11493                   {
11494                      typeStringBuf[0] = '\0';
11495                      PrintType(type, typeStringBuf, false, true);
11496                      typeString = typeStringBuf;
11497                      FreeType(type);
11498                   }
11499                }
11500                if(typeString)
11501                {
11502                   OldList * initializers = MkList();
11503                   Declarator decl;
11504                   OldList * specs = MkList();
11505                   if(arrayExp.list)
11506                   {
11507                      Expression e;
11508
11509                      builtinCount = arrayExp.list->count;
11510                      type = ProcessTypeString(typeString, false);
11511                      while(e = arrayExp.list->first)
11512                      {
11513                         arrayExp.list->Remove(e);
11514                         e.destType = type;
11515                         type.refCount++;
11516                         ProcessExpressionType(e);
11517                         ListAdd(initializers, MkInitializerAssignment(e));
11518                      }
11519                      FreeType(type);
11520                      delete arrayExp.list;
11521                   }
11522                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11523                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11524                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11525
11526                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11527                      PlugDeclarator(
11528                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11529                         ), MkInitializerList(initializers)))));
11530                   FreeList(exp, FreeExpression);
11531                }
11532                else
11533                {
11534                   arrayExp.expType = ProcessTypeString("Container", false);
11535                   Compiler_Error($"Couldn't determine type of array elements\n");
11536                }
11537
11538                /*
11539                Declarator decl;
11540                OldList * specs = MkList();
11541
11542                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11543                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11544                stmt.compound.declarations = MkListOne(
11545                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11546                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11547                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11548                      MkInitializerAssignment(MkExpBrackets(exp))))));
11549                */
11550             }
11551             else if(isLinkList && !isList)
11552             {
11553                Declarator decl;
11554                OldList * specs = MkList();
11555                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11556                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11557                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11558                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11559                      MkInitializerAssignment(MkExpBrackets(exp))))));
11560             }
11561             /*else if(isCustomAVLTree)
11562             {
11563                Declarator decl;
11564                OldList * specs = MkList();
11565                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11566                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11567                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11568                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11569                      MkInitializerAssignment(MkExpBrackets(exp))))));
11570             }*/
11571             else if(_class.templateArgs)
11572             {
11573                if(isMap)
11574                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11575                else
11576                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11577
11578                stmt.compound.declarations = MkListOne(
11579                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11580                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11581                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11582             }
11583             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11584
11585             if(block)
11586             {
11587                // Reparent sub-contexts in this statement
11588                switch(block.type)
11589                {
11590                   case compoundStmt:
11591                      if(block.compound.context)
11592                         block.compound.context.parent = stmt.compound.context;
11593                      break;
11594                   case ifStmt:
11595                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11596                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11597                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11598                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11599                      break;
11600                   case switchStmt:
11601                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11602                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11603                      break;
11604                   case whileStmt:
11605                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11606                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11607                      break;
11608                   case doWhileStmt:
11609                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11610                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11611                      break;
11612                   case forStmt:
11613                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11614                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11615                      break;
11616                   case forEachStmt:
11617                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11618                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11619                      break;
11620                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11621                   case labeledStmt:
11622                   case caseStmt
11623                   case expressionStmt:
11624                   case gotoStmt:
11625                   case continueStmt:
11626                   case breakStmt
11627                   case returnStmt:
11628                   case asmStmt:
11629                   case badDeclarationStmt:
11630                   case fireWatchersStmt:
11631                   case stopWatchingStmt:
11632                   case watchStmt:
11633                   */
11634                }
11635             }
11636             if(filter)
11637             {
11638                block = MkIfStmt(filter, block, null);
11639             }
11640             if(isArray)
11641             {
11642                stmt.compound.statements = MkListOne(MkForStmt(
11643                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11644                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11645                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11646                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11647                   block));
11648               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11649               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11650               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11651             }
11652             else if(isBuiltin)
11653             {
11654                char count[128];
11655                //OldList * specs = MkList();
11656                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11657
11658                sprintf(count, "%d", builtinCount);
11659
11660                stmt.compound.statements = MkListOne(MkForStmt(
11661                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11662                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11663                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11664                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11665                   block));
11666
11667                /*
11668                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11669                stmt.compound.statements = MkListOne(MkForStmt(
11670                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11671                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11672                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11673                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11674                   block));
11675               */
11676               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11677               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11678               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11679             }
11680             else if(isLinkList && !isList)
11681             {
11682                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11683                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11684                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11685                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11686                {
11687                   stmt.compound.statements = MkListOne(MkForStmt(
11688                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11689                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11690                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11691                      block));
11692                }
11693                else
11694                {
11695                   OldList * specs = MkList();
11696                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11697                   stmt.compound.statements = MkListOne(MkForStmt(
11698                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11699                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11700                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11701                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11702                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11703                      block));
11704                }
11705                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11706                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11707                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11708             }
11709             /*else if(isCustomAVLTree)
11710             {
11711                stmt.compound.statements = MkListOne(MkForStmt(
11712                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11713                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11714                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11715                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11716                   block));
11717
11718                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11719                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11720                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11721             }*/
11722             else
11723             {
11724                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11725                   MkIdentifier("Next")), null)), block));
11726             }
11727             ProcessExpressionType(expIt);
11728             if(stmt.compound.declarations->first)
11729                ProcessDeclaration(stmt.compound.declarations->first);
11730
11731             if(symbol)
11732                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11733
11734             ProcessStatement(stmt);
11735             curContext = stmt.compound.context.parent;
11736             break;
11737          }
11738          else
11739          {
11740             Compiler_Error($"Expression is not a container\n");
11741          }
11742          break;
11743       }
11744       case gotoStmt:
11745          break;
11746       case continueStmt:
11747          break;
11748       case breakStmt:
11749          break;
11750       case returnStmt:
11751       {
11752          Expression exp;
11753          if(stmt.expressions)
11754          {
11755             for(exp = stmt.expressions->first; exp; exp = exp.next)
11756             {
11757                if(!exp.next)
11758                {
11759                   if(curFunction && !curFunction.type)
11760                      curFunction.type = ProcessType(
11761                         curFunction.specifiers, curFunction.declarator);
11762                   FreeType(exp.destType);
11763                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11764                   if(exp.destType) exp.destType.refCount++;
11765                }
11766                ProcessExpressionType(exp);
11767             }
11768          }
11769          break;
11770       }
11771       case badDeclarationStmt:
11772       {
11773          ProcessDeclaration(stmt.decl);
11774          break;
11775       }
11776       case asmStmt:
11777       {
11778          AsmField field;
11779          if(stmt.asmStmt.inputFields)
11780          {
11781             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11782                if(field.expression)
11783                   ProcessExpressionType(field.expression);
11784          }
11785          if(stmt.asmStmt.outputFields)
11786          {
11787             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11788                if(field.expression)
11789                   ProcessExpressionType(field.expression);
11790          }
11791          if(stmt.asmStmt.clobberedFields)
11792          {
11793             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11794             {
11795                if(field.expression)
11796                   ProcessExpressionType(field.expression);
11797             }
11798          }
11799          break;
11800       }
11801       case watchStmt:
11802       {
11803          PropertyWatch propWatch;
11804          OldList * watches = stmt._watch.watches;
11805          Expression object = stmt._watch.object;
11806          Expression watcher = stmt._watch.watcher;
11807          if(watcher)
11808             ProcessExpressionType(watcher);
11809          if(object)
11810             ProcessExpressionType(object);
11811
11812          if(inCompiler)
11813          {
11814             if(watcher || thisClass)
11815             {
11816                External external = curExternal;
11817                Context context = curContext;
11818
11819                stmt.type = expressionStmt;
11820                stmt.expressions = MkList();
11821
11822                curExternal = external.prev;
11823
11824                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11825                {
11826                   ClassFunction func;
11827                   char watcherName[1024];
11828                   Class watcherClass = watcher ?
11829                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11830                   External createdExternal;
11831
11832                   // Create a declaration above
11833                   External externalDecl = MkExternalDeclaration(null);
11834                   ast->Insert(curExternal.prev, externalDecl);
11835
11836                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11837                   if(propWatch.deleteWatch)
11838                      strcat(watcherName, "_delete");
11839                   else
11840                   {
11841                      Identifier propID;
11842                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11843                      {
11844                         strcat(watcherName, "_");
11845                         strcat(watcherName, propID.string);
11846                      }
11847                   }
11848
11849                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11850                   {
11851                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11852                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11853                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11854                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11855                      ProcessClassFunctionBody(func, propWatch.compound);
11856                      propWatch.compound = null;
11857
11858                      //afterExternal = afterExternal ? afterExternal : curExternal;
11859
11860                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11861                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11862                      // TESTING THIS...
11863                      createdExternal.symbol.idCode = external.symbol.idCode;
11864
11865                      curExternal = createdExternal;
11866                      ProcessFunction(createdExternal.function);
11867
11868
11869                      // Create a declaration above
11870                      {
11871                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
11872                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11873                         externalDecl.declaration = decl;
11874                         if(decl.symbol && !decl.symbol.pointerExternal)
11875                            decl.symbol.pointerExternal = externalDecl;
11876                      }
11877
11878                      if(propWatch.deleteWatch)
11879                      {
11880                         OldList * args = MkList();
11881                         ListAdd(args, CopyExpression(object));
11882                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11883                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11884                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11885                      }
11886                      else
11887                      {
11888                         Class _class = object.expType._class.registered;
11889                         Identifier propID;
11890
11891                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11892                         {
11893                            char propName[1024];
11894                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11895                            if(prop)
11896                            {
11897                               char getName[1024], setName[1024];
11898                               OldList * args = MkList();
11899
11900                               DeclareProperty(prop, setName, getName);
11901
11902                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11903                               strcpy(propName, "__ecereProp_");
11904                               FullClassNameCat(propName, prop._class.fullName, false);
11905                               strcat(propName, "_");
11906                               // strcat(propName, prop.name);
11907                               FullClassNameCat(propName, prop.name, true);
11908
11909                               ListAdd(args, CopyExpression(object));
11910                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11911                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11912                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11913
11914                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11915                            }
11916                            else
11917                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11918                         }
11919                      }
11920                   }
11921                   else
11922                      Compiler_Error($"Invalid watched object\n");
11923                }
11924
11925                curExternal = external;
11926                curContext = context;
11927
11928                if(watcher)
11929                   FreeExpression(watcher);
11930                if(object)
11931                   FreeExpression(object);
11932                FreeList(watches, FreePropertyWatch);
11933             }
11934             else
11935                Compiler_Error($"No observer specified and not inside a _class\n");
11936          }
11937          else
11938          {
11939             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11940             {
11941                ProcessStatement(propWatch.compound);
11942             }
11943
11944          }
11945          break;
11946       }
11947       case fireWatchersStmt:
11948       {
11949          OldList * watches = stmt._watch.watches;
11950          Expression object = stmt._watch.object;
11951          Class _class;
11952          // DEBUGGER BUG: Why doesn't watches evaluate to null??
11953          // printf("%X\n", watches);
11954          // printf("%X\n", stmt._watch.watches);
11955          if(object)
11956             ProcessExpressionType(object);
11957
11958          if(inCompiler)
11959          {
11960             _class = object ?
11961                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
11962
11963             if(_class)
11964             {
11965                Identifier propID;
11966
11967                stmt.type = expressionStmt;
11968                stmt.expressions = MkList();
11969
11970                // Check if we're inside a property set
11971                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11972                {
11973                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
11974                }
11975                else if(!watches)
11976                {
11977                   //Compiler_Error($"No property specified and not inside a property set\n");
11978                }
11979                if(watches)
11980                {
11981                   for(propID = watches->first; propID; propID = propID.next)
11982                   {
11983                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11984                      if(prop)
11985                      {
11986                         CreateFireWatcher(prop, object, stmt);
11987                      }
11988                      else
11989                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
11990                   }
11991                }
11992                else
11993                {
11994                   // Fire all properties!
11995                   Property prop;
11996                   Class base;
11997                   for(base = _class; base; base = base.base)
11998                   {
11999                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12000                      {
12001                         if(prop.isProperty && prop.isWatchable)
12002                         {
12003                            CreateFireWatcher(prop, object, stmt);
12004                         }
12005                      }
12006                   }
12007                }
12008
12009                if(object)
12010                   FreeExpression(object);
12011                FreeList(watches, FreeIdentifier);
12012             }
12013             else
12014                Compiler_Error($"Invalid object specified and not inside a class\n");
12015          }
12016          break;
12017       }
12018       case stopWatchingStmt:
12019       {
12020          OldList * watches = stmt._watch.watches;
12021          Expression object = stmt._watch.object;
12022          Expression watcher = stmt._watch.watcher;
12023          Class _class;
12024          if(object)
12025             ProcessExpressionType(object);
12026          if(watcher)
12027             ProcessExpressionType(watcher);
12028          if(inCompiler)
12029          {
12030             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12031
12032             if(watcher || thisClass)
12033             {
12034                if(_class)
12035                {
12036                   Identifier propID;
12037
12038                   stmt.type = expressionStmt;
12039                   stmt.expressions = MkList();
12040
12041                   if(!watches)
12042                   {
12043                      OldList * args;
12044                      // eInstance_StopWatching(object, null, watcher);
12045                      args = MkList();
12046                      ListAdd(args, CopyExpression(object));
12047                      ListAdd(args, MkExpConstant("0"));
12048                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12049                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12050                   }
12051                   else
12052                   {
12053                      for(propID = watches->first; propID; propID = propID.next)
12054                      {
12055                         char propName[1024];
12056                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12057                         if(prop)
12058                         {
12059                            char getName[1024], setName[1024];
12060                            OldList * args = MkList();
12061
12062                            DeclareProperty(prop, setName, getName);
12063
12064                            // eInstance_StopWatching(object, prop, watcher);
12065                            strcpy(propName, "__ecereProp_");
12066                            FullClassNameCat(propName, prop._class.fullName, false);
12067                            strcat(propName, "_");
12068                            // strcat(propName, prop.name);
12069                            FullClassNameCat(propName, prop.name, true);
12070                            MangleClassName(propName);
12071
12072                            ListAdd(args, CopyExpression(object));
12073                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12074                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12075                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12076                         }
12077                         else
12078                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
12079                      }
12080                   }
12081
12082                   if(object)
12083                      FreeExpression(object);
12084                   if(watcher)
12085                      FreeExpression(watcher);
12086                   FreeList(watches, FreeIdentifier);
12087                }
12088                else
12089                   Compiler_Error($"Invalid object specified and not inside a class\n");
12090             }
12091             else
12092                Compiler_Error($"No observer specified and not inside a class\n");
12093          }
12094          break;
12095       }
12096    }
12097 }
12098
12099 static void ProcessFunction(FunctionDefinition function)
12100 {
12101    Identifier id = GetDeclId(function.declarator);
12102    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12103    Type type = symbol ? symbol.type : null;
12104    Class oldThisClass = thisClass;
12105    Context oldTopContext = topContext;
12106
12107    yylloc = function.loc;
12108    // Process thisClass
12109
12110    if(type && type.thisClass)
12111    {
12112       Symbol classSym = type.thisClass;
12113       Class _class = type.thisClass.registered;
12114       char className[1024];
12115       char structName[1024];
12116       Declarator funcDecl;
12117       Symbol thisSymbol;
12118
12119       bool typedObject = false;
12120
12121       if(_class && !_class.base)
12122       {
12123          _class = currentClass;
12124          if(_class && !_class.symbol)
12125             _class.symbol = FindClass(_class.fullName);
12126          classSym = _class ? _class.symbol : null;
12127          typedObject = true;
12128       }
12129
12130       thisClass = _class;
12131
12132       if(inCompiler && _class)
12133       {
12134          if(type.kind == functionType)
12135          {
12136             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12137             {
12138                //TypeName param = symbol.type.params.first;
12139                Type param = symbol.type.params.first;
12140                symbol.type.params.Remove(param);
12141                //FreeTypeName(param);
12142                FreeType(param);
12143             }
12144             if(type.classObjectType != classPointer)
12145             {
12146                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12147                symbol.type.staticMethod = true;
12148                symbol.type.thisClass = null;
12149
12150                // HIGH DANGER: VERIFYING THIS...
12151                symbol.type.extraParam = false;
12152             }
12153          }
12154
12155          strcpy(className, "__ecereClass_");
12156          FullClassNameCat(className, _class.fullName, true);
12157
12158          MangleClassName(className);
12159
12160          structName[0] = 0;
12161          FullClassNameCat(structName, _class.fullName, false);
12162
12163          // [class] this
12164
12165
12166          funcDecl = GetFuncDecl(function.declarator);
12167          if(funcDecl)
12168          {
12169             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12170             {
12171                TypeName param = funcDecl.function.parameters->first;
12172                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12173                {
12174                   funcDecl.function.parameters->Remove(param);
12175                   FreeTypeName(param);
12176                }
12177             }
12178
12179             // DANGER: Watch for this... Check if it's a Conversion?
12180             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12181
12182             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12183             if(!function.propertyNoThis)
12184             {
12185                TypeName thisParam;
12186
12187                if(type.classObjectType != classPointer)
12188                {
12189                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12190                   if(!funcDecl.function.parameters)
12191                      funcDecl.function.parameters = MkList();
12192                   funcDecl.function.parameters->Insert(null, thisParam);
12193                }
12194
12195                if(typedObject)
12196                {
12197                   if(type.classObjectType != classPointer)
12198                   {
12199                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12200                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12201                   }
12202
12203                   thisParam = TypeName
12204                   {
12205                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12206                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12207                   };
12208                   funcDecl.function.parameters->Insert(null, thisParam);
12209                }
12210             }
12211          }
12212
12213          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12214          {
12215             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12216             funcDecl = GetFuncDecl(initDecl.declarator);
12217             if(funcDecl)
12218             {
12219                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12220                {
12221                   TypeName param = funcDecl.function.parameters->first;
12222                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12223                   {
12224                      funcDecl.function.parameters->Remove(param);
12225                      FreeTypeName(param);
12226                   }
12227                }
12228
12229                if(type.classObjectType != classPointer)
12230                {
12231                   // DANGER: Watch for this... Check if it's a Conversion?
12232                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12233                   {
12234                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12235
12236                      if(!funcDecl.function.parameters)
12237                         funcDecl.function.parameters = MkList();
12238                      funcDecl.function.parameters->Insert(null, thisParam);
12239                   }
12240                }
12241             }
12242          }
12243       }
12244
12245       // Add this to the context
12246       if(function.body)
12247       {
12248          if(type.classObjectType != classPointer)
12249          {
12250             thisSymbol = Symbol
12251             {
12252                string = CopyString("this");
12253                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12254             };
12255             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12256
12257             if(typedObject && thisSymbol.type)
12258             {
12259                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12260                thisSymbol.type.byReference = type.byReference;
12261                thisSymbol.type.typedByReference = type.byReference;
12262                /*
12263                thisSymbol = Symbol { string = CopyString("class") };
12264                function.body.compound.context.symbols.Add(thisSymbol);
12265                */
12266             }
12267          }
12268       }
12269
12270       // Pointer to class data
12271
12272       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12273       {
12274          DataMember member = null;
12275          {
12276             Class base;
12277             for(base = _class; base && base.type != systemClass; base = base.next)
12278             {
12279                for(member = base.membersAndProperties.first; member; member = member.next)
12280                   if(!member.isProperty)
12281                      break;
12282                if(member)
12283                   break;
12284             }
12285          }
12286          for(member = _class.membersAndProperties.first; member; member = member.next)
12287             if(!member.isProperty)
12288                break;
12289          if(member)
12290          {
12291             char pointerName[1024];
12292
12293             Declaration decl;
12294             Initializer initializer;
12295             Expression exp, bytePtr;
12296
12297             strcpy(pointerName, "__ecerePointer_");
12298             FullClassNameCat(pointerName, _class.fullName, false);
12299             {
12300                char className[1024];
12301                strcpy(className, "__ecereClass_");
12302                FullClassNameCat(className, classSym.string, true);
12303                MangleClassName(className);
12304
12305                // Testing This
12306                DeclareClass(classSym, className);
12307             }
12308
12309             // ((byte *) this)
12310             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12311
12312             if(_class.fixed)
12313             {
12314                char string[256];
12315                sprintf(string, "%d", _class.offset);
12316                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12317             }
12318             else
12319             {
12320                // ([bytePtr] + [className]->offset)
12321                exp = QBrackets(MkExpOp(bytePtr, '+',
12322                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12323             }
12324
12325             // (this ? [exp] : 0)
12326             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12327             exp.expType = Type
12328             {
12329                refCount = 1;
12330                kind = pointerType;
12331                type = Type { refCount = 1, kind = voidType };
12332             };
12333
12334             if(function.body)
12335             {
12336                yylloc = function.body.loc;
12337                // ([structName] *) [exp]
12338                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12339                initializer = MkInitializerAssignment(
12340                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12341
12342                // [structName] * [pointerName] = [initializer];
12343                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12344
12345                {
12346                   Context prevContext = curContext;
12347                   curContext = function.body.compound.context;
12348
12349                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12350                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12351
12352                   curContext = prevContext;
12353                }
12354
12355                // WHY?
12356                decl.symbol = null;
12357
12358                if(!function.body.compound.declarations)
12359                   function.body.compound.declarations = MkList();
12360                function.body.compound.declarations->Insert(null, decl);
12361             }
12362          }
12363       }
12364
12365
12366       // Loop through the function and replace undeclared identifiers
12367       // which are a member of the class (methods, properties or data)
12368       // by "this.[member]"
12369    }
12370    else
12371       thisClass = null;
12372
12373    if(id)
12374    {
12375       FreeSpecifier(id._class);
12376       id._class = null;
12377
12378       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12379       {
12380          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12381          id = GetDeclId(initDecl.declarator);
12382
12383          FreeSpecifier(id._class);
12384          id._class = null;
12385       }
12386    }
12387    if(function.body)
12388       topContext = function.body.compound.context;
12389    {
12390       FunctionDefinition oldFunction = curFunction;
12391       curFunction = function;
12392       if(function.body)
12393          ProcessStatement(function.body);
12394
12395       // If this is a property set and no firewatchers has been done yet, add one here
12396       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12397       {
12398          Statement prevCompound = curCompound;
12399          Context prevContext = curContext;
12400
12401          Statement fireWatchers = MkFireWatchersStmt(null, null);
12402          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12403          ListAdd(function.body.compound.statements, fireWatchers);
12404
12405          curCompound = function.body;
12406          curContext = function.body.compound.context;
12407
12408          ProcessStatement(fireWatchers);
12409
12410          curContext = prevContext;
12411          curCompound = prevCompound;
12412
12413       }
12414
12415       curFunction = oldFunction;
12416    }
12417
12418    if(function.declarator)
12419    {
12420       ProcessDeclarator(function.declarator);
12421    }
12422
12423    topContext = oldTopContext;
12424    thisClass = oldThisClass;
12425 }
12426
12427 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12428 static void ProcessClass(OldList definitions, Symbol symbol)
12429 {
12430    ClassDef def;
12431    External external = curExternal;
12432    Class regClass = symbol ? symbol.registered : null;
12433
12434    // Process all functions
12435    for(def = definitions.first; def; def = def.next)
12436    {
12437       if(def.type == functionClassDef)
12438       {
12439          if(def.function.declarator)
12440             curExternal = def.function.declarator.symbol.pointerExternal;
12441          else
12442             curExternal = external;
12443
12444          ProcessFunction((FunctionDefinition)def.function);
12445       }
12446       else if(def.type == declarationClassDef)
12447       {
12448          if(def.decl.type == instDeclaration)
12449          {
12450             thisClass = regClass;
12451             ProcessInstantiationType(def.decl.inst);
12452             thisClass = null;
12453          }
12454          // Testing this
12455          else
12456          {
12457             Class backThisClass = thisClass;
12458             if(regClass) thisClass = regClass;
12459             ProcessDeclaration(def.decl);
12460             thisClass = backThisClass;
12461          }
12462       }
12463       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12464       {
12465          MemberInit defProperty;
12466
12467          // Add this to the context
12468          Symbol thisSymbol = Symbol
12469          {
12470             string = CopyString("this");
12471             type = regClass ? MkClassType(regClass.fullName) : null;
12472          };
12473          globalContext.symbols.Add((BTNode)thisSymbol);
12474
12475          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12476          {
12477             thisClass = regClass;
12478             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12479             thisClass = null;
12480          }
12481
12482          globalContext.symbols.Remove((BTNode)thisSymbol);
12483          FreeSymbol(thisSymbol);
12484       }
12485       else if(def.type == propertyClassDef && def.propertyDef)
12486       {
12487          PropertyDef prop = def.propertyDef;
12488
12489          // Add this to the context
12490          /*
12491          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12492          globalContext.symbols.Add(thisSymbol);
12493          */
12494
12495          thisClass = regClass;
12496          if(prop.setStmt)
12497          {
12498             if(regClass)
12499             {
12500                Symbol thisSymbol
12501                {
12502                   string = CopyString("this");
12503                   type = MkClassType(regClass.fullName);
12504                };
12505                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12506             }
12507
12508             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12509             ProcessStatement(prop.setStmt);
12510          }
12511          if(prop.getStmt)
12512          {
12513             if(regClass)
12514             {
12515                Symbol thisSymbol
12516                {
12517                   string = CopyString("this");
12518                   type = MkClassType(regClass.fullName);
12519                };
12520                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12521             }
12522
12523             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12524             ProcessStatement(prop.getStmt);
12525          }
12526          if(prop.issetStmt)
12527          {
12528             if(regClass)
12529             {
12530                Symbol thisSymbol
12531                {
12532                   string = CopyString("this");
12533                   type = MkClassType(regClass.fullName);
12534                };
12535                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12536             }
12537
12538             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12539             ProcessStatement(prop.issetStmt);
12540          }
12541
12542          thisClass = null;
12543
12544          /*
12545          globalContext.symbols.Remove(thisSymbol);
12546          FreeSymbol(thisSymbol);
12547          */
12548       }
12549       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12550       {
12551          PropertyWatch propertyWatch = def.propertyWatch;
12552
12553          thisClass = regClass;
12554          if(propertyWatch.compound)
12555          {
12556             Symbol thisSymbol
12557             {
12558                string = CopyString("this");
12559                type = regClass ? MkClassType(regClass.fullName) : null;
12560             };
12561
12562             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12563
12564             curExternal = null;
12565             ProcessStatement(propertyWatch.compound);
12566          }
12567          thisClass = null;
12568       }
12569    }
12570 }
12571
12572 void DeclareFunctionUtil(String s)
12573 {
12574    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12575    if(function)
12576    {
12577       char name[1024];
12578       name[0] = 0;
12579       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12580          strcpy(name, "__ecereFunction_");
12581       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12582       DeclareFunction(function, name);
12583    }
12584 }
12585
12586 void ComputeDataTypes()
12587 {
12588    External external;
12589    External temp { };
12590    External after = null;
12591
12592    currentClass = null;
12593
12594    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12595
12596    for(external = ast->first; external; external = external.next)
12597    {
12598       if(external.type == declarationExternal)
12599       {
12600          Declaration decl = external.declaration;
12601          if(decl)
12602          {
12603             OldList * decls = decl.declarators;
12604             if(decls)
12605             {
12606                InitDeclarator initDecl = decls->first;
12607                if(initDecl)
12608                {
12609                   Declarator declarator = initDecl.declarator;
12610                   if(declarator && declarator.type == identifierDeclarator)
12611                   {
12612                      Identifier id = declarator.identifier;
12613                      if(id && id.string)
12614                      {
12615                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12616                         {
12617                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12618                            after = external;
12619                         }
12620                      }
12621                   }
12622                }
12623             }
12624          }
12625        }
12626    }
12627
12628    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12629    ast->Insert(after, temp);
12630    curExternal = temp;
12631
12632    DeclareFunctionUtil("eSystem_New");
12633    DeclareFunctionUtil("eSystem_New0");
12634    DeclareFunctionUtil("eSystem_Renew");
12635    DeclareFunctionUtil("eSystem_Renew0");
12636    DeclareFunctionUtil("eSystem_Delete");
12637    DeclareFunctionUtil("eClass_GetProperty");
12638    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12639
12640    DeclareStruct("ecere::com::Class", false);
12641    DeclareStruct("ecere::com::Instance", false);
12642    DeclareStruct("ecere::com::Property", false);
12643    DeclareStruct("ecere::com::DataMember", false);
12644    DeclareStruct("ecere::com::Method", false);
12645    DeclareStruct("ecere::com::SerialBuffer", false);
12646    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12647
12648    ast->Remove(temp);
12649
12650    for(external = ast->first; external; external = external.next)
12651    {
12652       afterExternal = curExternal = external;
12653       if(external.type == functionExternal)
12654       {
12655          currentClass = external.function._class;
12656          ProcessFunction(external.function);
12657       }
12658       // There shouldn't be any _class member access here anyways...
12659       else if(external.type == declarationExternal)
12660       {
12661          currentClass = null;
12662          ProcessDeclaration(external.declaration);
12663       }
12664       else if(external.type == classExternal)
12665       {
12666          ClassDefinition _class = external._class;
12667          currentClass = external.symbol.registered;
12668          if(_class.definitions)
12669          {
12670             ProcessClass(_class.definitions, _class.symbol);
12671          }
12672          if(inCompiler)
12673          {
12674             // Free class data...
12675             ast->Remove(external);
12676             delete external;
12677          }
12678       }
12679       else if(external.type == nameSpaceExternal)
12680       {
12681          thisNameSpace = external.id.string;
12682       }
12683    }
12684    currentClass = null;
12685    thisNameSpace = null;
12686
12687    delete temp.symbol;
12688    delete temp;
12689 }