ee62a8f60169de4a42972c4f746bb2689c74b659
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50       bool backOutputLineNumbers = outputLineNumbers;
51       outputLineNumbers = false;
52
53       if(exp)
54          OutputExpression(exp, f);
55       f.Seek(0, start);
56       count = strlen(string);
57       count += f.Read(string + count, 1, 1023);
58       string[count] = '\0';
59       delete f;
60
61       outputLineNumbers = backOutputLineNumbers;
62    }
63 }
64
65 Type ProcessTemplateParameterType(TemplateParameter param)
66 {
67    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
68    {
69       // TOFIX: Will need to free this Type
70       if(!param.baseType)
71       {
72          if(param.dataTypeString)
73             param.baseType = ProcessTypeString(param.dataTypeString, false);
74          else
75             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
76       }
77       return param.baseType;
78    }
79    return null;
80 }
81
82 bool NeedCast(Type type1, Type type2)
83 {
84    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
85
86    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
87    {
88       return false;
89    }
90
91    if(type1.kind == type2.kind)
92    {
93       switch(type1.kind)
94       {
95          case _BoolType:
96          case charType:
97          case shortType:
98          case intType:
99          case int64Type:
100          case intPtrType:
101          case intSizeType:
102             if(type1.passAsTemplate && !type2.passAsTemplate)
103                return true;
104             return type1.isSigned != type2.isSigned;
105          case classType:
106             return type1._class != type2._class;
107          case pointerType:
108             return (type1.type && type2.type && type1.type.constant != type2.type.constant) || NeedCast(type1.type, type2.type);
109          default:
110             return true; //false; ????
111       }
112    }
113    return true;
114 }
115
116 static void ReplaceClassMembers(Expression exp, Class _class)
117 {
118    if(exp.type == identifierExp && exp.identifier)
119    {
120       Identifier id = exp.identifier;
121       Context ctx;
122       Symbol symbol = null;
123       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
124       {
125          // First, check if the identifier is declared inside the function
126          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
127          {
128             symbol = (Symbol)ctx.symbols.FindString(id.string);
129             if(symbol) break;
130          }
131       }
132
133       // If it is not, check if it is a member of the _class
134       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
135       {
136          Property prop = eClass_FindProperty(_class, id.string, privateModule);
137          Method method = null;
138          DataMember member = null;
139          ClassProperty classProp = null;
140          if(!prop)
141          {
142             method = eClass_FindMethod(_class, id.string, privateModule);
143          }
144          if(!prop && !method)
145             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
146          if(!prop && !method && !member)
147          {
148             classProp = eClass_FindClassProperty(_class, id.string);
149          }
150          if(prop || method || member || classProp)
151          {
152             // Replace by this.[member]
153             exp.type = memberExp;
154             exp.member.member = id;
155             exp.member.memberType = unresolvedMember;
156             exp.member.exp = QMkExpId("this");
157             //exp.member.exp.loc = exp.loc;
158             exp.addedThis = true;
159          }
160          else if(_class && _class.templateParams.first)
161          {
162             Class sClass;
163             for(sClass = _class; sClass; sClass = sClass.base)
164             {
165                if(sClass.templateParams.first)
166                {
167                   ClassTemplateParameter param;
168                   for(param = sClass.templateParams.first; param; param = param.next)
169                   {
170                      if(param.type == expression && !strcmp(param.name, id.string))
171                      {
172                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
173
174                         if(argExp)
175                         {
176                            Declarator decl;
177                            OldList * specs = MkList();
178
179                            FreeIdentifier(exp.member.member);
180
181                            ProcessExpressionType(argExp);
182
183                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
184
185                            exp.expType = ProcessType(specs, decl);
186
187                            // *[expType] *[argExp]
188                            exp.type = bracketsExp;
189                            exp.list = MkListOne(MkExpOp(null, '*',
190                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
191                         }
192                      }
193                   }
194                }
195             }
196          }
197       }
198    }
199 }
200
201 ////////////////////////////////////////////////////////////////////////
202 // PRINTING ////////////////////////////////////////////////////////////
203 ////////////////////////////////////////////////////////////////////////
204
205 public char * PrintInt(int64 result)
206 {
207    char temp[100];
208    if(result > MAXINT)
209       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
210    else
211       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
212    if(result > MAXINT || result < MININT)
213       strcat(temp, "LL");
214    return CopyString(temp);
215 }
216
217 public char * PrintUInt(uint64 result)
218 {
219    char temp[100];
220    if(result > MAXDWORD)
221       sprintf(temp, FORMAT64HEXLL /*"0x%I64X"*/, result);
222    else if(result > MAXINT)
223       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
224    else
225       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char *  PrintInt64(int64 result)
230 {
231    char temp[100];
232    if(result > MAXINT || result < MININT)
233       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
234    else
235       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
236    return CopyString(temp);
237 }
238
239 public char * PrintUInt64(uint64 result)
240 {
241    char temp[100];
242    if(result > MAXDWORD)
243       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
244    else if(result > MAXINT)
245       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
246    else
247       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
248    return CopyString(temp);
249 }
250
251 public char * PrintHexUInt(uint64 result)
252 {
253    char temp[100];
254    if(result > MAXDWORD)
255       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
256    else
257       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
258    if(result > MAXDWORD)
259       strcat(temp, "LL");
260    return CopyString(temp);
261 }
262
263 public char * PrintHexUInt64(uint64 result)
264 {
265    char temp[100];
266    if(result > MAXDWORD)
267       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
268    else
269       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
270    return CopyString(temp);
271 }
272
273 public char * PrintShort(short result)
274 {
275    char temp[100];
276    sprintf(temp, "%d", (unsigned short)result);
277    return CopyString(temp);
278 }
279
280 public char * PrintUShort(unsigned short result)
281 {
282    char temp[100];
283    if(result > 32767)
284       sprintf(temp, "0x%X", (int)result);
285    else
286       sprintf(temp, "%d", (int)result);
287    return CopyString(temp);
288 }
289
290 public char * PrintChar(char result)
291 {
292    char temp[100];
293    if(result > 0 && isprint(result))
294       sprintf(temp, "'%c'", result);
295    else if(result < 0)
296       sprintf(temp, "%d", (int)result);
297    else
298       //sprintf(temp, "%#X", result);
299       sprintf(temp, "0x%X", (unsigned char)result);
300    return CopyString(temp);
301 }
302
303 public char * PrintUChar(unsigned char result)
304 {
305    char temp[100];
306    sprintf(temp, "0x%X", result);
307    return CopyString(temp);
308 }
309
310 public char * PrintFloat(float result)
311 {
312    char temp[350];
313    if(result.isInf)
314    {
315       if(result.signBit)
316          strcpy(temp, "-inf");
317       else
318          strcpy(temp, "inf");
319    }
320    else if(result.isNan)
321    {
322       if(result.signBit)
323          strcpy(temp, "-nan");
324       else
325          strcpy(temp, "nan");
326    }
327    else
328       sprintf(temp, "%.16ff", result);
329    return CopyString(temp);
330 }
331
332 public char * PrintDouble(double result)
333 {
334    char temp[350];
335    if(result.isInf)
336    {
337       if(result.signBit)
338          strcpy(temp, "-inf");
339       else
340          strcpy(temp, "inf");
341    }
342    else if(result.isNan)
343    {
344       if(result.signBit)
345          strcpy(temp, "-nan");
346       else
347          strcpy(temp, "nan");
348    }
349    else
350       sprintf(temp, "%.16f", result);
351    return CopyString(temp);
352 }
353
354 ////////////////////////////////////////////////////////////////////////
355 ////////////////////////////////////////////////////////////////////////
356
357 //public Operand GetOperand(Expression exp);
358
359 #define GETVALUE(name, t) \
360    public bool GetOp##name(Operand op2, t * value2) \
361    {                                                        \
362       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
363       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
364       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
365       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
366       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
367       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
368       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
369       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
370       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
371       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
372       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
373       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
374       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
375       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
376       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
377       else                                                                          \
378          return false;                                                              \
379       return true;                                                                  \
380    } \
381    public bool Get##name(Expression exp, t * value2) \
382    {                                                        \
383       Operand op2 = GetOperand(exp);                        \
384       return GetOp##name(op2, value2); \
385    }
386
387 // To help the debugger currently not preprocessing...
388 #define HELP(x) x
389
390 GETVALUE(Int, HELP(int));
391 GETVALUE(UInt, HELP(unsigned int));
392 GETVALUE(Int64, HELP(int64));
393 GETVALUE(UInt64, HELP(uint64));
394 GETVALUE(IntPtr, HELP(intptr));
395 GETVALUE(UIntPtr, HELP(uintptr));
396 GETVALUE(IntSize, HELP(intsize));
397 GETVALUE(UIntSize, HELP(uintsize));
398 GETVALUE(Short, HELP(short));
399 GETVALUE(UShort, HELP(unsigned short));
400 GETVALUE(Char, HELP(char));
401 GETVALUE(UChar, HELP(unsigned char));
402 GETVALUE(Float, HELP(float));
403 GETVALUE(Double, HELP(double));
404
405 void ComputeExpression(Expression exp);
406
407 void ComputeClassMembers(Class _class, bool isMember)
408 {
409    DataMember member = isMember ? (DataMember) _class : null;
410    Context context = isMember ? null : SetupTemplatesContext(_class);
411    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
412                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
413    {
414       int unionMemberOffset = 0;
415       int bitFields = 0;
416
417       /*
418       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
419          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
420       */
421
422       if(member)
423       {
424          member.memberOffset = 0;
425          if(targetBits < sizeof(void *) * 8)
426             member.structAlignment = 0;
427       }
428       else if(targetBits < sizeof(void *) * 8)
429          _class.structAlignment = 0;
430
431       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
432       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
433          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
434
435       if(!member && _class.destructionWatchOffset)
436          _class.memberOffset += sizeof(OldList);
437
438       // To avoid reentrancy...
439       //_class.structSize = -1;
440
441       {
442          DataMember dataMember;
443          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
444          {
445             if(!dataMember.isProperty)
446             {
447                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
448                {
449                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
450                   /*if(!dataMember.dataType)
451                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
452                      */
453                }
454             }
455          }
456       }
457
458       {
459          DataMember dataMember;
460          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
461          {
462             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
463             {
464                if(!isMember && _class.type == bitClass && dataMember.dataType)
465                {
466                   BitMember bitMember = (BitMember) dataMember;
467                   uint64 mask = 0;
468                   int d;
469
470                   ComputeTypeSize(dataMember.dataType);
471
472                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
473                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
474
475                   _class.memberOffset = bitMember.pos + bitMember.size;
476                   for(d = 0; d<bitMember.size; d++)
477                   {
478                      if(d)
479                         mask <<= 1;
480                      mask |= 1;
481                   }
482                   bitMember.mask = mask << bitMember.pos;
483                }
484                else if(dataMember.type == normalMember && dataMember.dataType)
485                {
486                   int size;
487                   int alignment = 0;
488
489                   // Prevent infinite recursion
490                   if(dataMember.dataType.kind != classType ||
491                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
492                      _class.type != structClass)))
493                      ComputeTypeSize(dataMember.dataType);
494
495                   if(dataMember.dataType.bitFieldCount)
496                   {
497                      bitFields += dataMember.dataType.bitFieldCount;
498                      size = 0;
499                   }
500                   else
501                   {
502                      if(bitFields)
503                      {
504                         int size = (bitFields + 7) / 8;
505
506                         if(isMember)
507                         {
508                            // TESTING THIS PADDING CODE
509                            if(alignment)
510                            {
511                               member.structAlignment = Max(member.structAlignment, alignment);
512
513                               if(member.memberOffset % alignment)
514                                  member.memberOffset += alignment - (member.memberOffset % alignment);
515                            }
516
517                            dataMember.offset = member.memberOffset;
518                            if(member.type == unionMember)
519                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
520                            else
521                            {
522                               member.memberOffset += size;
523                            }
524                         }
525                         else
526                         {
527                            // TESTING THIS PADDING CODE
528                            if(alignment)
529                            {
530                               _class.structAlignment = Max(_class.structAlignment, alignment);
531
532                               if(_class.memberOffset % alignment)
533                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
534                            }
535
536                            dataMember.offset = _class.memberOffset;
537                            _class.memberOffset += size;
538                         }
539                         bitFields = 0;
540                      }
541                      size = dataMember.dataType.size;
542                      alignment = dataMember.dataType.alignment;
543                   }
544
545                   if(isMember)
546                   {
547                      // TESTING THIS PADDING CODE
548                      if(alignment)
549                      {
550                         member.structAlignment = Max(member.structAlignment, alignment);
551
552                         if(member.memberOffset % alignment)
553                            member.memberOffset += alignment - (member.memberOffset % alignment);
554                      }
555
556                      dataMember.offset = member.memberOffset;
557                      if(member.type == unionMember)
558                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
559                      else
560                      {
561                         member.memberOffset += size;
562                      }
563                   }
564                   else
565                   {
566                      // TESTING THIS PADDING CODE
567                      if(alignment)
568                      {
569                         _class.structAlignment = Max(_class.structAlignment, alignment);
570
571                         if(_class.memberOffset % alignment)
572                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
573                      }
574
575                      dataMember.offset = _class.memberOffset;
576                      _class.memberOffset += size;
577                   }
578                }
579                else
580                {
581                   int alignment;
582
583                   ComputeClassMembers((Class)dataMember, true);
584                   alignment = dataMember.structAlignment;
585
586                   if(isMember)
587                   {
588                      if(alignment)
589                      {
590                         if(member.memberOffset % alignment)
591                            member.memberOffset += alignment - (member.memberOffset % alignment);
592
593                         member.structAlignment = Max(member.structAlignment, alignment);
594                      }
595                      dataMember.offset = member.memberOffset;
596                      if(member.type == unionMember)
597                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
598                      else
599                         member.memberOffset += dataMember.memberOffset;
600                   }
601                   else
602                   {
603                      if(alignment)
604                      {
605                         if(_class.memberOffset % alignment)
606                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
607                         _class.structAlignment = Max(_class.structAlignment, alignment);
608                      }
609                      dataMember.offset = _class.memberOffset;
610                      _class.memberOffset += dataMember.memberOffset;
611                   }
612                }
613             }
614          }
615          if(bitFields)
616          {
617             int alignment = 0;
618             int size = (bitFields + 7) / 8;
619
620             if(isMember)
621             {
622                // TESTING THIS PADDING CODE
623                if(alignment)
624                {
625                   member.structAlignment = Max(member.structAlignment, alignment);
626
627                   if(member.memberOffset % alignment)
628                      member.memberOffset += alignment - (member.memberOffset % alignment);
629                }
630
631                if(member.type == unionMember)
632                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
633                else
634                {
635                   member.memberOffset += size;
636                }
637             }
638             else
639             {
640                // TESTING THIS PADDING CODE
641                if(alignment)
642                {
643                   _class.structAlignment = Max(_class.structAlignment, alignment);
644
645                   if(_class.memberOffset % alignment)
646                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
647                }
648                _class.memberOffset += size;
649             }
650             bitFields = 0;
651          }
652       }
653       if(member && member.type == unionMember)
654       {
655          member.memberOffset = unionMemberOffset;
656       }
657
658       if(!isMember)
659       {
660          /*if(_class.type == structClass)
661             _class.size = _class.memberOffset;
662          else
663          */
664
665          if(_class.type != bitClass)
666          {
667             int extra = 0;
668             if(_class.structAlignment)
669             {
670                if(_class.memberOffset % _class.structAlignment)
671                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
672             }
673             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
674             if(!member)
675             {
676                Property prop;
677                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
678                {
679                   if(prop.isProperty && prop.isWatchable)
680                   {
681                      prop.watcherOffset = _class.structSize;
682                      _class.structSize += sizeof(OldList);
683                   }
684                }
685             }
686
687             // Fix Derivatives
688             {
689                OldLink derivative;
690                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
691                {
692                   Class deriv = derivative.data;
693
694                   if(deriv.computeSize)
695                   {
696                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
697                      deriv.offset = /*_class.offset + */_class.structSize;
698                      deriv.memberOffset = 0;
699                      // ----------------------
700
701                      deriv.structSize = deriv.offset;
702
703                      ComputeClassMembers(deriv, false);
704                   }
705                }
706             }
707          }
708       }
709    }
710    if(context)
711       FinishTemplatesContext(context);
712 }
713
714 public void ComputeModuleClasses(Module module)
715 {
716    Class _class;
717    OldLink subModule;
718
719    for(subModule = module.modules.first; subModule; subModule = subModule.next)
720       ComputeModuleClasses(subModule.data);
721    for(_class = module.classes.first; _class; _class = _class.next)
722       ComputeClassMembers(_class, false);
723 }
724
725
726 public int ComputeTypeSize(Type type)
727 {
728    uint size = type ? type.size : 0;
729    if(!size && type && !type.computing)
730    {
731       type.computing = true;
732       switch(type.kind)
733       {
734          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
735          case charType: type.alignment = size = sizeof(char); break;
736          case intType: type.alignment = size = sizeof(int); break;
737          case int64Type: type.alignment = size = sizeof(int64); break;
738          case intPtrType: type.alignment = size = targetBits / 8; break;
739          case intSizeType: type.alignment = size = targetBits / 8; break;
740          case longType: type.alignment = size = sizeof(long); break;
741          case shortType: type.alignment = size = sizeof(short); break;
742          case floatType: type.alignment = size = sizeof(float); break;
743          case doubleType: type.alignment = size = sizeof(double); break;
744          case classType:
745          {
746             Class _class = type._class ? type._class.registered : null;
747
748             if(_class && _class.type == structClass)
749             {
750                // Ensure all members are properly registered
751                ComputeClassMembers(_class, false);
752                type.alignment = _class.structAlignment;
753                size = _class.structSize;
754                if(type.alignment && size % type.alignment)
755                   size += type.alignment - (size % type.alignment);
756
757             }
758             else if(_class && (_class.type == unitClass ||
759                    _class.type == enumClass ||
760                    _class.type == bitClass))
761             {
762                if(!_class.dataType)
763                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
764                size = type.alignment = ComputeTypeSize(_class.dataType);
765             }
766             else
767                size = type.alignment = targetBits / 8; // sizeof(Instance *);
768             break;
769          }
770          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
771          case arrayType:
772             if(type.arraySizeExp)
773             {
774                ProcessExpressionType(type.arraySizeExp);
775                ComputeExpression(type.arraySizeExp);
776                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
777                   type.arraySizeExp.expType.kind != shortType &&
778                   type.arraySizeExp.expType.kind != charType &&
779                   type.arraySizeExp.expType.kind != longType &&
780                   type.arraySizeExp.expType.kind != int64Type &&
781                   type.arraySizeExp.expType.kind != intSizeType &&
782                   type.arraySizeExp.expType.kind != intPtrType &&
783                   type.arraySizeExp.expType.kind != enumType &&
784                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
785                {
786                   Location oldLoc = yylloc;
787                   // bool isConstant = type.arraySizeExp.isConstant;
788                   char expression[10240];
789                   expression[0] = '\0';
790                   type.arraySizeExp.expType = null;
791                   yylloc = type.arraySizeExp.loc;
792                   if(inCompiler)
793                      PrintExpression(type.arraySizeExp, expression);
794                   Compiler_Error($"Array size not constant int (%s)\n", expression);
795                   yylloc = oldLoc;
796                }
797                GetInt(type.arraySizeExp, &type.arraySize);
798             }
799             else if(type.enumClass)
800             {
801                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
802                {
803                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
804                }
805                else
806                   type.arraySize = 0;
807             }
808             else
809             {
810                // Unimplemented auto size
811                type.arraySize = 0;
812             }
813
814             size = ComputeTypeSize(type.type) * type.arraySize;
815             if(type.type)
816                type.alignment = type.type.alignment;
817
818             break;
819          case structType:
820          {
821             Type member;
822             for(member = type.members.first; member; member = member.next)
823             {
824                uint addSize = ComputeTypeSize(member);
825
826                member.offset = size;
827                if(member.alignment && size % member.alignment)
828                   member.offset += member.alignment - (size % member.alignment);
829                size = member.offset;
830
831                type.alignment = Max(type.alignment, member.alignment);
832                size += addSize;
833             }
834             if(type.alignment && size % type.alignment)
835                size += type.alignment - (size % type.alignment);
836             break;
837          }
838          case unionType:
839          {
840             Type member;
841             for(member = type.members.first; member; member = member.next)
842             {
843                uint addSize = ComputeTypeSize(member);
844
845                member.offset = size;
846                if(member.alignment && size % member.alignment)
847                   member.offset += member.alignment - (size % member.alignment);
848                size = member.offset;
849
850                type.alignment = Max(type.alignment, member.alignment);
851                size = Max(size, addSize);
852             }
853             if(type.alignment && size % type.alignment)
854                size += type.alignment - (size % type.alignment);
855             break;
856          }
857          case templateType:
858          {
859             TemplateParameter param = type.templateParameter;
860             Type baseType = ProcessTemplateParameterType(param);
861             if(baseType)
862             {
863                size = ComputeTypeSize(baseType);
864                type.alignment = baseType.alignment;
865             }
866             else
867                type.alignment = size = sizeof(uint64);
868             break;
869          }
870          case enumType:
871          {
872             type.alignment = size = sizeof(enum { test });
873             break;
874          }
875          case thisClassType:
876          {
877             type.alignment = size = targetBits / 8; //sizeof(void *);
878             break;
879          }
880       }
881       type.size = size;
882       type.computing = false;
883    }
884    return size;
885 }
886
887
888 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
889 {
890    // This function is in need of a major review when implementing private members etc.
891    DataMember topMember = isMember ? (DataMember) _class : null;
892    uint totalSize = 0;
893    uint maxSize = 0;
894    int alignment;
895    uint size;
896    DataMember member;
897    int anonID = 1;
898    Context context = isMember ? null : SetupTemplatesContext(_class);
899    if(addedPadding)
900       *addedPadding = false;
901
902    if(!isMember && _class.base)
903    {
904       maxSize = _class.structSize;
905       //if(_class.base.type != systemClass) // Commented out with new Instance _class
906       {
907          // DANGER: Testing this noHeadClass here...
908          if(_class.type == structClass || _class.type == noHeadClass)
909             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
910          else
911          {
912             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
913             if(maxSize > baseSize)
914                maxSize -= baseSize;
915             else
916                maxSize = 0;
917          }
918       }
919    }
920
921    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
922    {
923       if(!member.isProperty)
924       {
925          switch(member.type)
926          {
927             case normalMember:
928             {
929                if(member.dataTypeString)
930                {
931                   OldList * specs = MkList(), * decls = MkList();
932                   Declarator decl;
933
934                   decl = SpecDeclFromString(member.dataTypeString, specs,
935                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
936                   ListAdd(decls, MkStructDeclarator(decl, null));
937                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
938
939                   if(!member.dataType)
940                      member.dataType = ProcessType(specs, decl);
941
942                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
943
944                   {
945                      Type type = ProcessType(specs, decl);
946                      DeclareType(member.dataType, false, false);
947                      FreeType(type);
948                   }
949                   /*
950                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
951                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
952                      DeclareStruct(member.dataType._class.string, false);
953                   */
954
955                   ComputeTypeSize(member.dataType);
956                   size = member.dataType.size;
957                   alignment = member.dataType.alignment;
958
959                   if(alignment)
960                   {
961                      if(totalSize % alignment)
962                         totalSize += alignment - (totalSize % alignment);
963                   }
964                   totalSize += size;
965                }
966                break;
967             }
968             case unionMember:
969             case structMember:
970             {
971                OldList * specs = MkList(), * list = MkList();
972                char id[100];
973                sprintf(id, "__anon%d", anonID++);
974
975                size = 0;
976                AddMembers(list, (Class)member, true, &size, topClass, null);
977                ListAdd(specs,
978                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
979                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, MkListOne(MkDeclaratorIdentifier(MkIdentifier(id))),null)));
980                alignment = member.structAlignment;
981
982                if(alignment)
983                {
984                   if(totalSize % alignment)
985                      totalSize += alignment - (totalSize % alignment);
986                }
987                totalSize += size;
988                break;
989             }
990          }
991       }
992    }
993    if(retSize)
994    {
995       if(topMember && topMember.type == unionMember)
996          *retSize = Max(*retSize, totalSize);
997       else
998          *retSize += totalSize;
999    }
1000    else if(totalSize < maxSize && _class.type != systemClass)
1001    {
1002       int autoPadding = 0;
1003       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
1004          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
1005       if(totalSize + autoPadding < maxSize)
1006       {
1007          char sizeString[50];
1008          sprintf(sizeString, "%d", maxSize - totalSize);
1009          ListAdd(declarations,
1010             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
1011             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1012          if(addedPadding)
1013             *addedPadding = true;
1014       }
1015    }
1016    if(context)
1017       FinishTemplatesContext(context);
1018    return topMember ? topMember.memberID : _class.memberID;
1019 }
1020
1021 static int DeclareMembers(Class _class, bool isMember)
1022 {
1023    DataMember topMember = isMember ? (DataMember) _class : null;
1024    DataMember member;
1025    Context context = isMember ? null : SetupTemplatesContext(_class);
1026
1027    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1028       DeclareMembers(_class.base, false);
1029
1030    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1031    {
1032       if(!member.isProperty)
1033       {
1034          switch(member.type)
1035          {
1036             case normalMember:
1037             {
1038                /*
1039                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1040                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1041                   DeclareStruct(member.dataType._class.string, false);
1042                   */
1043                if(!member.dataType && member.dataTypeString)
1044                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1045                if(member.dataType)
1046                   DeclareType(member.dataType, false, false);
1047                break;
1048             }
1049             case unionMember:
1050             case structMember:
1051             {
1052                DeclareMembers((Class)member, true);
1053                break;
1054             }
1055          }
1056       }
1057    }
1058    if(context)
1059       FinishTemplatesContext(context);
1060
1061    return topMember ? topMember.memberID : _class.memberID;
1062 }
1063
1064 static void IdentifyAnonStructs(OldList/*<ClassDef>*/ *  definitions)
1065 {
1066    ClassDef def;
1067    int anonID = 1;
1068    for(def = definitions->first; def; def = def.next)
1069    {
1070       if(def.type == declarationClassDef)
1071       {
1072          Declaration decl = def.decl;
1073          if(decl && decl.specifiers)
1074          {
1075             Specifier spec;
1076             bool isStruct = false;
1077             for(spec = decl.specifiers->first; spec; spec = spec.next)
1078             {
1079                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1080                {
1081                   if(spec.definitions)
1082                      IdentifyAnonStructs(spec.definitions);
1083                   isStruct = true;
1084                }
1085             }
1086             if(isStruct)
1087             {
1088                Declarator d = null;
1089                if(decl.declarators)
1090                {
1091                   for(d = decl.declarators->first; d; d = d.next)
1092                   {
1093                      Identifier idDecl = GetDeclId(d);
1094                      if(idDecl)
1095                         break;
1096                   }
1097                }
1098                if(!d)
1099                {
1100                   char id[100];
1101                   sprintf(id, "__anon%d", anonID++);
1102                   if(!decl.declarators)
1103                      decl.declarators = MkList();
1104                   ListAdd(decl.declarators, MkDeclaratorIdentifier(MkIdentifier(id)));
1105                }
1106             }
1107          }
1108       }
1109    }
1110 }
1111
1112 void DeclareStruct(const char * name, bool skipNoHead)
1113 {
1114    External external = null;
1115    Symbol classSym = FindClass(name);
1116
1117    if(!inCompiler || !classSym) return;
1118
1119    // We don't need any declaration for bit classes...
1120    if(classSym.registered &&
1121       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1122       return;
1123
1124    /*if(classSym.registered.templateClass)
1125       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1126    */
1127
1128    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1129    {
1130       // Add typedef struct
1131       Declaration decl;
1132       OldList * specifiers, * declarators;
1133       OldList * declarations = null;
1134       char structName[1024];
1135       Specifier spec = null;
1136       external = (classSym.registered && classSym.registered.type == structClass) ?
1137          classSym.pointerExternal : classSym.structExternal;
1138
1139       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1140       // Moved this one up because DeclareClass done later will need it
1141
1142       classSym.declaring++;
1143
1144       if(strchr(classSym.string, '<'))
1145       {
1146          if(classSym.registered.templateClass)
1147          {
1148             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1149             classSym.declaring--;
1150          }
1151          return;
1152       }
1153
1154       //if(!skipNoHead)
1155          DeclareMembers(classSym.registered, false);
1156
1157       structName[0] = 0;
1158       FullClassNameCat(structName, name, false);
1159
1160       if(external && external.declaration && external.declaration.specifiers)
1161       {
1162          for(spec = external.declaration.specifiers->first; spec; spec = spec.next)
1163          {
1164             if(spec.type == structSpecifier || spec.type == unionSpecifier)
1165                break;
1166          }
1167       }
1168
1169       /*if(!external)
1170          external = MkExternalDeclaration(null);*/
1171
1172       if(!skipNoHead && (!spec || !spec.definitions))
1173       {
1174          bool addedPadding = false;
1175          classSym.declaredStructSym = true;
1176
1177          declarations = MkList();
1178
1179          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1180
1181          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1182          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1183
1184          if(!declarations->count || (declarations->count == 1 && addedPadding))
1185          {
1186             FreeList(declarations, FreeClassDef);
1187             declarations = null;
1188          }
1189       }
1190       if(skipNoHead || declarations)
1191       {
1192          if(spec)
1193          {
1194             if(declarations)
1195                spec.definitions = declarations;
1196
1197             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1198             {
1199                // TODO: Fix this
1200                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1201
1202                // DANGER
1203                if(classSym.structExternal)
1204                   ast->Move(classSym.structExternal, curExternal.prev);
1205                ast->Move(classSym.pointerExternal, curExternal.prev);
1206
1207                classSym.id = curExternal.symbol.idCode;
1208                classSym.idCode = curExternal.symbol.idCode;
1209                // external = classSym.pointerExternal;
1210                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1211             }
1212          }
1213          else
1214          {
1215             if(!external)
1216                external = MkExternalDeclaration(null);
1217
1218             specifiers = MkList();
1219             declarators = MkList();
1220             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1221
1222             /*
1223             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1224             ListAdd(declarators, MkInitDeclarator(d, null));
1225             */
1226             external.declaration = decl = MkDeclaration(specifiers, declarators);
1227             if(decl.symbol && !decl.symbol.pointerExternal)
1228                decl.symbol.pointerExternal = external;
1229
1230             // For simple classes, keep the declaration as the external to move around
1231             if(classSym.registered && classSym.registered.type == structClass)
1232             {
1233                char className[1024];
1234                strcpy(className, "__ecereClass_");
1235                FullClassNameCat(className, classSym.string, true);
1236                //MangleClassName(className);
1237
1238                // Testing This
1239                DeclareClass(classSym, className);
1240
1241                external.symbol = classSym;
1242                classSym.pointerExternal = external;
1243                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1244                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1245             }
1246             else
1247             {
1248                char className[1024];
1249                strcpy(className, "__ecereClass_");
1250                FullClassNameCat(className, classSym.string, true);
1251                //MangleClassName(className);
1252
1253                // TOFIX: TESTING THIS...
1254                classSym.structExternal = external;
1255                DeclareClass(classSym, className);
1256                external.symbol = classSym;
1257             }
1258
1259             //if(curExternal)
1260                ast->Insert(curExternal ? curExternal.prev : null, external);
1261          }
1262       }
1263
1264       classSym.declaring--;
1265    }
1266    else
1267    {
1268       if(classSym.structExternal && classSym.structExternal.declaration && classSym.structExternal.declaration.specifiers)
1269       {
1270          Specifier spec;
1271          for(spec = classSym.structExternal.declaration.specifiers->first; spec; spec = spec.next)
1272          {
1273             IdentifyAnonStructs(spec.definitions);
1274          }
1275       }
1276
1277       if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1278       {
1279          // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1280          // Moved this one up because DeclareClass done later will need it
1281
1282          // TESTING THIS:
1283          classSym.declaring++;
1284
1285          //if(!skipNoHead)
1286          {
1287             if(classSym.registered)
1288                DeclareMembers(classSym.registered, false);
1289          }
1290
1291          if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1292          {
1293             // TODO: Fix this
1294             //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1295
1296             // DANGER
1297             if(classSym.structExternal)
1298                ast->Move(classSym.structExternal, curExternal.prev);
1299             ast->Move(classSym.pointerExternal, curExternal.prev);
1300
1301             classSym.id = curExternal.symbol.idCode;
1302             classSym.idCode = curExternal.symbol.idCode;
1303             // external = classSym.pointerExternal;
1304             // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1305          }
1306
1307          classSym.declaring--;
1308       }
1309    }
1310    //return external;
1311 }
1312
1313 void DeclareProperty(Property prop, char * setName, char * getName)
1314 {
1315    Symbol symbol = prop.symbol;
1316
1317    strcpy(setName, "__ecereProp_");
1318    FullClassNameCat(setName, prop._class.fullName, false);
1319    strcat(setName, "_Set_");
1320    // strcat(setName, prop.name);
1321    FullClassNameCat(setName, prop.name, true);
1322    //MangleClassName(setName);
1323
1324    strcpy(getName, "__ecereProp_");
1325    FullClassNameCat(getName, prop._class.fullName, false);
1326    strcat(getName, "_Get_");
1327    FullClassNameCat(getName, prop.name, true);
1328    // strcat(getName, prop.name);
1329
1330    // To support "char *" property
1331    //MangleClassName(getName);
1332
1333    if(prop._class.type == structClass)
1334       DeclareStruct(prop._class.fullName, false);
1335
1336    if(!symbol || curExternal.symbol.idCode < symbol.id)
1337    {
1338       bool imported = false;
1339       bool dllImport = false;
1340
1341       if(!symbol || symbol._import)
1342       {
1343          if(!symbol)
1344          {
1345             Symbol classSym;
1346             if(!prop._class.symbol)
1347                prop._class.symbol = FindClass(prop._class.fullName);
1348             classSym = prop._class.symbol;
1349             if(classSym && !classSym._import)
1350             {
1351                ModuleImport module;
1352
1353                if(prop._class.module)
1354                   module = FindModule(prop._class.module);
1355                else
1356                   module = mainModule;
1357
1358                classSym._import = ClassImport
1359                {
1360                   name = CopyString(prop._class.fullName);
1361                   isRemote = prop._class.isRemote;
1362                };
1363                module.classes.Add(classSym._import);
1364             }
1365             symbol = prop.symbol = Symbol { };
1366             symbol._import = (ClassImport)PropertyImport
1367             {
1368                name = CopyString(prop.name);
1369                isVirtual = false; //prop.isVirtual;
1370                hasSet = prop.Set ? true : false;
1371                hasGet = prop.Get ? true : false;
1372             };
1373             if(classSym)
1374                classSym._import.properties.Add(symbol._import);
1375          }
1376          imported = true;
1377          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1378          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1379             prop._class.module.importType != staticImport)
1380             dllImport = true;
1381       }
1382
1383       if(!symbol.type)
1384       {
1385          Context context = SetupTemplatesContext(prop._class);
1386          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1387          FinishTemplatesContext(context);
1388       }
1389
1390       // Get
1391       if(prop.Get)
1392       {
1393          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1394          {
1395             Declaration decl;
1396             OldList * specifiers, * declarators;
1397             Declarator d;
1398             OldList * params;
1399             Specifier spec;
1400             External external;
1401             Declarator typeDecl;
1402             bool simple = false;
1403
1404             specifiers = MkList();
1405             declarators = MkList();
1406             params = MkList();
1407
1408             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1409                MkDeclaratorIdentifier(MkIdentifier("this"))));
1410
1411             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1412             //if(imported)
1413             if(dllImport)
1414                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1415
1416             {
1417                Context context = SetupTemplatesContext(prop._class);
1418                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1419                FinishTemplatesContext(context);
1420             }
1421
1422             // Make sure the simple _class's type is declared
1423             for(spec = specifiers->first; spec; spec = spec.next)
1424             {
1425                if(spec.type == nameSpecifier /*SpecifierClass*/)
1426                {
1427                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1428                   {
1429                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1430                      symbol._class = classSym.registered;
1431                      if(classSym.registered && classSym.registered.type == structClass)
1432                      {
1433                         DeclareStruct(spec.name, false);
1434                         simple = true;
1435                      }
1436                   }
1437                }
1438             }
1439
1440             if(!simple)
1441                d = PlugDeclarator(typeDecl, d);
1442             else
1443             {
1444                ListAdd(params, MkTypeName(specifiers,
1445                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1446                specifiers = MkList();
1447             }
1448
1449             d = MkDeclaratorFunction(d, params);
1450
1451             //if(imported)
1452             if(dllImport)
1453                specifiers->Insert(null, MkSpecifier(EXTERN));
1454             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1455                specifiers->Insert(null, MkSpecifier(STATIC));
1456             if(simple)
1457                ListAdd(specifiers, MkSpecifier(VOID));
1458
1459             ListAdd(declarators, MkInitDeclarator(d, null));
1460
1461             decl = MkDeclaration(specifiers, declarators);
1462
1463             external = MkExternalDeclaration(decl);
1464             ast->Insert(curExternal.prev, external);
1465             external.symbol = symbol;
1466             symbol.externalGet = external;
1467
1468             ReplaceThisClassSpecifiers(specifiers, prop._class);
1469
1470             if(typeDecl)
1471                FreeDeclarator(typeDecl);
1472          }
1473          else
1474          {
1475             // Move declaration higher...
1476             ast->Move(symbol.externalGet, curExternal.prev);
1477          }
1478       }
1479
1480       // Set
1481       if(prop.Set)
1482       {
1483          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1484          {
1485             Declaration decl;
1486             OldList * specifiers, * declarators;
1487             Declarator d;
1488             OldList * params;
1489             Specifier spec;
1490             External external;
1491             Declarator typeDecl;
1492
1493             declarators = MkList();
1494             params = MkList();
1495
1496             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1497             if(!prop.conversion || prop._class.type == structClass)
1498             {
1499                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1500                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1501             }
1502
1503             specifiers = MkList();
1504
1505             {
1506                Context context = SetupTemplatesContext(prop._class);
1507                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1508                   MkDeclaratorIdentifier(MkIdentifier("value")));
1509                FinishTemplatesContext(context);
1510             }
1511             if(!strcmp(prop._class.base.fullName, "eda::Row") || !strcmp(prop._class.base.fullName, "eda::Id"))
1512                specifiers->Insert(null, MkSpecifier(CONST));
1513
1514             ListAdd(params, MkTypeName(specifiers, d));
1515
1516             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1517             //if(imported)
1518             if(dllImport)
1519                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1520             d = MkDeclaratorFunction(d, params);
1521
1522             // Make sure the simple _class's type is declared
1523             for(spec = specifiers->first; spec; spec = spec.next)
1524             {
1525                if(spec.type == nameSpecifier /*SpecifierClass*/)
1526                {
1527                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1528                   {
1529                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1530                      symbol._class = classSym.registered;
1531                      if(classSym.registered && classSym.registered.type == structClass)
1532                         DeclareStruct(spec.name, false);
1533                   }
1534                }
1535             }
1536
1537             ListAdd(declarators, MkInitDeclarator(d, null));
1538
1539             specifiers = MkList();
1540             //if(imported)
1541             if(dllImport)
1542                specifiers->Insert(null, MkSpecifier(EXTERN));
1543             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1544                specifiers->Insert(null, MkSpecifier(STATIC));
1545
1546             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1547             if(!prop.conversion || prop._class.type == structClass)
1548                ListAdd(specifiers, MkSpecifier(VOID));
1549             else
1550                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1551
1552             decl = MkDeclaration(specifiers, declarators);
1553
1554             external = MkExternalDeclaration(decl);
1555             ast->Insert(curExternal.prev, external);
1556             external.symbol = symbol;
1557             symbol.externalSet = external;
1558
1559             ReplaceThisClassSpecifiers(specifiers, prop._class);
1560          }
1561          else
1562          {
1563             // Move declaration higher...
1564             ast->Move(symbol.externalSet, curExternal.prev);
1565          }
1566       }
1567
1568       // Property (for Watchers)
1569       if(!symbol.externalPtr)
1570       {
1571          Declaration decl;
1572          External external;
1573          OldList * specifiers = MkList();
1574          char propName[1024];
1575
1576          if(imported)
1577             specifiers->Insert(null, MkSpecifier(EXTERN));
1578          else
1579          {
1580             specifiers->Insert(null, MkSpecifier(STATIC));
1581             specifiers->Add(MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
1582          }
1583
1584          ListAdd(specifiers, MkSpecifierName("Property"));
1585
1586          strcpy(propName, "__ecereProp_");
1587          FullClassNameCat(propName, prop._class.fullName, false);
1588          strcat(propName, "_");
1589          FullClassNameCat(propName, prop.name, true);
1590          // strcat(propName, prop.name);
1591          //MangleClassName(propName);
1592
1593          {
1594             OldList * list = MkList();
1595             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1596                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1597
1598             if(!imported)
1599             {
1600                strcpy(propName, "__ecerePropM_");
1601                FullClassNameCat(propName, prop._class.fullName, false);
1602                strcat(propName, "_");
1603                // strcat(propName, prop.name);
1604                FullClassNameCat(propName, prop.name, true);
1605
1606                //MangleClassName(propName);
1607
1608                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1609                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1610             }
1611             decl = MkDeclaration(specifiers, list);
1612          }
1613
1614          external = MkExternalDeclaration(decl);
1615          ast->Insert(curExternal.prev, external);
1616          external.symbol = symbol;
1617          symbol.externalPtr = external;
1618       }
1619       else
1620       {
1621          // Move declaration higher...
1622          ast->Move(symbol.externalPtr, curExternal.prev);
1623       }
1624
1625       symbol.id = curExternal.symbol.idCode;
1626    }
1627 }
1628
1629 // ***************** EXPRESSION PROCESSING ***************************
1630 public Type Dereference(Type source)
1631 {
1632    Type type = null;
1633    if(source)
1634    {
1635       if(source.kind == pointerType || source.kind == arrayType)
1636       {
1637          type = source.type;
1638          source.type.refCount++;
1639       }
1640       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1641       {
1642          type = Type
1643          {
1644             kind = charType;
1645             refCount = 1;
1646          };
1647       }
1648       // Support dereferencing of no head classes for now...
1649       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1650       {
1651          type = source;
1652          source.refCount++;
1653       }
1654       else
1655          Compiler_Error($"cannot dereference type\n");
1656    }
1657    return type;
1658 }
1659
1660 static Type Reference(Type source)
1661 {
1662    Type type = null;
1663    if(source)
1664    {
1665       type = Type
1666       {
1667          kind = pointerType;
1668          type = source;
1669          refCount = 1;
1670       };
1671       source.refCount++;
1672    }
1673    return type;
1674 }
1675
1676 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1677 {
1678    Identifier ident = member.identifiers ? member.identifiers->first : null;
1679    bool found = false;
1680    DataMember dataMember = null;
1681    Method method = null;
1682    bool freeType = false;
1683
1684    yylloc = member.loc;
1685
1686    if(!ident)
1687    {
1688       if(curMember)
1689       {
1690          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1691          if(*curMember)
1692          {
1693             found = true;
1694             dataMember = *curMember;
1695          }
1696       }
1697    }
1698    else
1699    {
1700       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1701       DataMember _subMemberStack[256];
1702       int _subMemberStackPos = 0;
1703
1704       // FILL MEMBER STACK
1705       if(!thisMember)
1706          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1707       if(thisMember)
1708       {
1709          dataMember = thisMember;
1710          if(curMember && thisMember.memberAccess == publicAccess)
1711          {
1712             *curMember = thisMember;
1713             *curClass = thisMember._class;
1714             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1715             *subMemberStackPos = _subMemberStackPos;
1716          }
1717          found = true;
1718       }
1719       else
1720       {
1721          // Setting a method
1722          method = eClass_FindMethod(_class, ident.string, privateModule);
1723          if(method && method.type == virtualMethod)
1724             found = true;
1725          else
1726             method = null;
1727       }
1728    }
1729
1730    if(found)
1731    {
1732       Type type = null;
1733       if(dataMember)
1734       {
1735          if(!dataMember.dataType && dataMember.dataTypeString)
1736          {
1737             //Context context = SetupTemplatesContext(dataMember._class);
1738             Context context = SetupTemplatesContext(_class);
1739             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1740             FinishTemplatesContext(context);
1741          }
1742          type = dataMember.dataType;
1743       }
1744       else if(method)
1745       {
1746          // This is for destination type...
1747          if(!method.dataType)
1748             ProcessMethodType(method);
1749          //DeclareMethod(method);
1750          // method.dataType = ((Symbol)method.symbol)->type;
1751          type = method.dataType;
1752       }
1753
1754       if(ident && ident.next)
1755       {
1756          for(ident = ident.next; ident && type; ident = ident.next)
1757          {
1758             if(type.kind == classType)
1759             {
1760                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1761                if(!dataMember)
1762                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1763                if(dataMember)
1764                   type = dataMember.dataType;
1765             }
1766             else if(type.kind == structType || type.kind == unionType)
1767             {
1768                Type memberType;
1769                for(memberType = type.members.first; memberType; memberType = memberType.next)
1770                {
1771                   if(!strcmp(memberType.name, ident.string))
1772                   {
1773                      type = memberType;
1774                      break;
1775                   }
1776                }
1777             }
1778          }
1779       }
1780
1781       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1782       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1783       {
1784          int id = 0;
1785          ClassTemplateParameter curParam = null;
1786          Class sClass;
1787          for(sClass = _class; sClass; sClass = sClass.base)
1788          {
1789             id = 0;
1790             if(sClass.templateClass) sClass = sClass.templateClass;
1791             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1792             {
1793                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1794                {
1795                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1796                   {
1797                      if(sClass.templateClass) sClass = sClass.templateClass;
1798                      id += sClass.templateParams.count;
1799                   }
1800                   break;
1801                }
1802                id++;
1803             }
1804             if(curParam) break;
1805          }
1806
1807          if(curParam)
1808          {
1809             ClassTemplateArgument arg = _class.templateArgs[id];
1810             if(arg.dataTypeString)
1811             {
1812                bool constant = type.constant;
1813                // FreeType(type);
1814                type = ProcessTypeString(arg.dataTypeString, false);
1815                if(type.kind == classType && constant) type.constant = true;
1816                else if(type.kind == pointerType)
1817                {
1818                   Type t = type.type;
1819                   while(t.kind == pointerType) t = t.type;
1820                   if(constant) t.constant = constant;
1821                }
1822                freeType = true;
1823                if(type && _class.templateClass)
1824                   type.passAsTemplate = true;
1825                if(type)
1826                {
1827                   // type.refCount++;
1828                   /*if(!exp.destType)
1829                   {
1830                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1831                      exp.destType.refCount++;
1832                   }*/
1833                }
1834             }
1835          }
1836       }
1837       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1838       {
1839          Class expClass = type._class.registered;
1840          Class cClass = null;
1841          int paramCount = 0;
1842          int lastParam = -1;
1843
1844          char templateString[1024];
1845          ClassTemplateParameter param;
1846          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1847          for(cClass = expClass; cClass; cClass = cClass.base)
1848          {
1849             int p = 0;
1850             if(cClass.templateClass) cClass = cClass.templateClass;
1851             for(param = cClass.templateParams.first; param; param = param.next)
1852             {
1853                int id = p;
1854                Class sClass;
1855                ClassTemplateArgument arg;
1856                for(sClass = cClass.base; sClass; sClass = sClass.base)
1857                {
1858                   if(sClass.templateClass) sClass = sClass.templateClass;
1859                   id += sClass.templateParams.count;
1860                }
1861                arg = expClass.templateArgs[id];
1862
1863                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1864                {
1865                   ClassTemplateParameter cParam;
1866                   //int p = numParams - sClass.templateParams.count;
1867                   int p = 0;
1868                   Class nextClass;
1869                   if(sClass.templateClass) sClass = sClass.templateClass;
1870
1871                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1872                   {
1873                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1874                      p += nextClass.templateParams.count;
1875                   }
1876
1877                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1878                   {
1879                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1880                      {
1881                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1882                         {
1883                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1884                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1885                            break;
1886                         }
1887                      }
1888                   }
1889                }
1890
1891                {
1892                   char argument[256];
1893                   argument[0] = '\0';
1894                   /*if(arg.name)
1895                   {
1896                      strcat(argument, arg.name.string);
1897                      strcat(argument, " = ");
1898                   }*/
1899                   switch(param.type)
1900                   {
1901                      case expression:
1902                      {
1903                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1904                         char expString[1024];
1905                         OldList * specs = MkList();
1906                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1907                         Expression exp;
1908                         char * string = PrintHexUInt64(arg.expression.ui64);
1909                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1910                         delete string;
1911
1912                         ProcessExpressionType(exp);
1913                         ComputeExpression(exp);
1914                         expString[0] = '\0';
1915                         PrintExpression(exp, expString);
1916                         strcat(argument, expString);
1917                         //delete exp;
1918                         FreeExpression(exp);
1919                         break;
1920                      }
1921                      case identifier:
1922                      {
1923                         strcat(argument, arg.member.name);
1924                         break;
1925                      }
1926                      case TemplateParameterType::type:
1927                      {
1928                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1929                            strcat(argument, arg.dataTypeString);
1930                         break;
1931                      }
1932                   }
1933                   if(argument[0])
1934                   {
1935                      if(paramCount) strcat(templateString, ", ");
1936                      if(lastParam != p - 1)
1937                      {
1938                         strcat(templateString, param.name);
1939                         strcat(templateString, " = ");
1940                      }
1941                      strcat(templateString, argument);
1942                      paramCount++;
1943                      lastParam = p;
1944                   }
1945                   p++;
1946                }
1947             }
1948          }
1949          {
1950             int len = strlen(templateString);
1951             if(templateString[len-1] == '<')
1952                len--;
1953             else
1954             {
1955                if(templateString[len-1] == '>')
1956                   templateString[len++] = ' ';
1957                templateString[len++] = '>';
1958             }
1959             templateString[len++] = '\0';
1960          }
1961          {
1962             Context context = SetupTemplatesContext(_class);
1963             if(freeType) FreeType(type);
1964             type = ProcessTypeString(templateString, false);
1965             freeType = true;
1966             FinishTemplatesContext(context);
1967          }
1968       }
1969
1970       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1971       {
1972          ProcessExpressionType(member.initializer.exp);
1973          if(!member.initializer.exp.expType)
1974          {
1975             if(inCompiler)
1976             {
1977                char expString[10240];
1978                expString[0] = '\0';
1979                PrintExpression(member.initializer.exp, expString);
1980                ChangeCh(expString, '\n', ' ');
1981                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1982             }
1983          }
1984          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1985          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false, true))
1986          {
1987             Compiler_Error($"incompatible instance method %s\n", ident.string);
1988          }
1989       }
1990       else if(member.initializer)
1991       {
1992          /*
1993          FreeType(member.exp.destType);
1994          member.exp.destType = type;
1995          if(member.exp.destType)
1996             member.exp.destType.refCount++;
1997          ProcessExpressionType(member.exp);
1998          */
1999
2000          ProcessInitializer(member.initializer, type);
2001       }
2002       if(freeType) FreeType(type);
2003    }
2004    else
2005    {
2006       if(_class && _class.type == unitClass)
2007       {
2008          if(member.initializer)
2009          {
2010             /*
2011             FreeType(member.exp.destType);
2012             member.exp.destType = MkClassType(_class.fullName);
2013             ProcessExpressionType(member.initializer, type);
2014             */
2015             Type type = MkClassType(_class.fullName);
2016             ProcessInitializer(member.initializer, type);
2017             FreeType(type);
2018          }
2019       }
2020       else
2021       {
2022          if(member.initializer)
2023          {
2024             //ProcessExpressionType(member.exp);
2025             ProcessInitializer(member.initializer, null);
2026          }
2027          if(ident)
2028          {
2029             if(method)
2030             {
2031                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
2032             }
2033             else if(_class)
2034             {
2035                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
2036                if(inCompiler)
2037                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
2038             }
2039          }
2040          else if(_class)
2041             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
2042       }
2043    }
2044 }
2045
2046 void ProcessInstantiationType(Instantiation inst)
2047 {
2048    yylloc = inst.loc;
2049    if(inst._class)
2050    {
2051       MembersInit members;
2052       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
2053       Class _class;
2054
2055       /*if(!inst._class.symbol)
2056          inst._class.symbol = FindClass(inst._class.name);*/
2057       classSym = inst._class.symbol;
2058       _class = classSym ? classSym.registered : null;
2059
2060       // DANGER: Patch for mutex not declaring its struct when not needed
2061       if(!_class || _class.type != noHeadClass)
2062          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
2063
2064       afterExternal = afterExternal ? afterExternal : curExternal;
2065
2066       if(inst.exp)
2067          ProcessExpressionType(inst.exp);
2068
2069       inst.isConstant = true;
2070       if(inst.members)
2071       {
2072          DataMember curMember = null;
2073          Class curClass = null;
2074          DataMember subMemberStack[256];
2075          int subMemberStackPos = 0;
2076
2077          for(members = inst.members->first; members; members = members.next)
2078          {
2079             switch(members.type)
2080             {
2081                case methodMembersInit:
2082                {
2083                   char name[1024];
2084                   static uint instMethodID = 0;
2085                   External external = curExternal;
2086                   Context context = curContext;
2087                   Declarator declarator = members.function.declarator;
2088                   Identifier nameID = GetDeclId(declarator);
2089                   char * unmangled = nameID ? nameID.string : null;
2090                   Expression exp;
2091                   External createdExternal = null;
2092
2093                   if(inCompiler)
2094                   {
2095                      char number[16];
2096                      //members.function.dontMangle = true;
2097                      strcpy(name, "__ecereInstMeth_");
2098                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2099                      strcat(name, "_");
2100                      strcat(name, nameID.string);
2101                      strcat(name, "_");
2102                      sprintf(number, "_%08d", instMethodID++);
2103                      strcat(name, number);
2104                      nameID.string = CopyString(name);
2105                   }
2106
2107                   // Do modifications here...
2108                   if(declarator)
2109                   {
2110                      Symbol symbol = declarator.symbol;
2111                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2112
2113                      if(method && method.type == virtualMethod)
2114                      {
2115                         symbol.method = method;
2116                         ProcessMethodType(method);
2117
2118                         if(!symbol.type.thisClass)
2119                         {
2120                            if(method.dataType.thisClass && currentClass &&
2121                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2122                            {
2123                               if(!currentClass.symbol)
2124                                  currentClass.symbol = FindClass(currentClass.fullName);
2125                               symbol.type.thisClass = currentClass.symbol;
2126                            }
2127                            else
2128                            {
2129                               if(!_class.symbol)
2130                                  _class.symbol = FindClass(_class.fullName);
2131                               symbol.type.thisClass = _class.symbol;
2132                            }
2133                         }
2134                         // TESTING THIS HERE:
2135                         DeclareType(symbol.type, true, true);
2136
2137                      }
2138                      else if(classSym)
2139                      {
2140                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2141                            unmangled, classSym.string);
2142                      }
2143                   }
2144
2145                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2146                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2147
2148                   if(nameID)
2149                   {
2150                      FreeSpecifier(nameID._class);
2151                      nameID._class = null;
2152                   }
2153
2154                   if(inCompiler)
2155                   {
2156                      //Type type = declarator.symbol.type;
2157                      External oldExternal = curExternal;
2158
2159                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2160                      // *** It was commented out for problems such as
2161                      /*
2162                            class VirtualDesktop : Window
2163                            {
2164                               clientSize = Size { };
2165                               Timer timer
2166                               {
2167                                  bool DelayExpired()
2168                                  {
2169                                     clientSize.w;
2170                                     return true;
2171                                  }
2172                               };
2173                            }
2174                      */
2175                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2176
2177                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2178
2179                      /*
2180                      if(strcmp(declarator.symbol.string, name))
2181                      {
2182                         printf("TOCHECK: Look out for this\n");
2183                         delete declarator.symbol.string;
2184                         declarator.symbol.string = CopyString(name);
2185                      }
2186
2187                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2188                      {
2189                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2190                         excludedSymbols->Remove(declarator.symbol);
2191                         globalContext.symbols.Add((BTNode)declarator.symbol);
2192                         if(strstr(declarator.symbol.string), "::")
2193                            globalContext.hasNameSpace = true;
2194
2195                      }
2196                      */
2197
2198                      //curExternal = curExternal.prev;
2199                      //afterExternal = afterExternal->next;
2200
2201                      //ProcessFunction(afterExternal->function);
2202
2203                      //curExternal = afterExternal;
2204                      {
2205                         External externalDecl;
2206                         externalDecl = MkExternalDeclaration(null);
2207                         ast->Insert(oldExternal.prev, externalDecl);
2208
2209                         // Which function does this process?
2210                         if(createdExternal.function)
2211                         {
2212                            ProcessFunction(createdExternal.function);
2213
2214                            //curExternal = oldExternal;
2215
2216                            {
2217                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2218
2219                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2220                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2221
2222                               //externalDecl = MkExternalDeclaration(decl);
2223
2224                               //***** ast->Insert(external.prev, externalDecl);
2225                               //ast->Insert(curExternal.prev, externalDecl);
2226                               externalDecl.declaration = decl;
2227                               if(decl.symbol && !decl.symbol.pointerExternal)
2228                                  decl.symbol.pointerExternal = externalDecl;
2229
2230                               // Trying this out...
2231                               declarator.symbol.pointerExternal = externalDecl;
2232                            }
2233                         }
2234                      }
2235                   }
2236                   else if(declarator)
2237                   {
2238                      curExternal = declarator.symbol.pointerExternal;
2239                      ProcessFunction((FunctionDefinition)members.function);
2240                   }
2241                   curExternal = external;
2242                   curContext = context;
2243
2244                   if(inCompiler)
2245                   {
2246                      FreeClassFunction(members.function);
2247
2248                      // In this pass, turn this into a MemberInitData
2249                      exp = QMkExpId(name);
2250                      members.type = dataMembersInit;
2251                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2252
2253                      delete unmangled;
2254                   }
2255                   break;
2256                }
2257                case dataMembersInit:
2258                {
2259                   if(members.dataMembers && classSym)
2260                   {
2261                      MemberInit member;
2262                      Location oldyyloc = yylloc;
2263                      for(member = members.dataMembers->first; member; member = member.next)
2264                      {
2265                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2266                         if(member.initializer && !member.initializer.isConstant)
2267                            inst.isConstant = false;
2268                      }
2269                      yylloc = oldyyloc;
2270                   }
2271                   break;
2272                }
2273             }
2274          }
2275       }
2276    }
2277 }
2278
2279 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2280 {
2281    // OPTIMIZATIONS: TESTING THIS...
2282    if(inCompiler)
2283    {
2284       if(type.kind == functionType)
2285       {
2286          Type param;
2287          if(declareParams)
2288          {
2289             for(param = type.params.first; param; param = param.next)
2290                DeclareType(param, declarePointers, true);
2291          }
2292          DeclareType(type.returnType, declarePointers, true);
2293       }
2294       else if(type.kind == pointerType && declarePointers)
2295          DeclareType(type.type, declarePointers, false);
2296       else if(type.kind == classType)
2297       {
2298          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2299             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2300       }
2301       else if(type.kind == structType || type.kind == unionType)
2302       {
2303          Type member;
2304          for(member = type.members.first; member; member = member.next)
2305             DeclareType(member, false, false);
2306       }
2307       else if(type.kind == arrayType)
2308          DeclareType(type.arrayType, declarePointers, false);
2309    }
2310 }
2311
2312 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2313 {
2314    ClassTemplateArgument * arg = null;
2315    int id = 0;
2316    ClassTemplateParameter curParam = null;
2317    Class sClass;
2318    for(sClass = _class; sClass; sClass = sClass.base)
2319    {
2320       id = 0;
2321       if(sClass.templateClass) sClass = sClass.templateClass;
2322       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2323       {
2324          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2325          {
2326             for(sClass = sClass.base; sClass; sClass = sClass.base)
2327             {
2328                if(sClass.templateClass) sClass = sClass.templateClass;
2329                id += sClass.templateParams.count;
2330             }
2331             break;
2332          }
2333          id++;
2334       }
2335       if(curParam) break;
2336    }
2337    if(curParam)
2338    {
2339       arg = &_class.templateArgs[id];
2340       if(arg && param.type == type)
2341          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2342    }
2343    return arg;
2344 }
2345
2346 public Context SetupTemplatesContext(Class _class)
2347 {
2348    Context context = PushContext();
2349    context.templateTypesOnly = true;
2350    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2351    {
2352       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2353       for(; param; param = param.next)
2354       {
2355          if(param.type == type && param.identifier)
2356          {
2357             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2358             curContext.templateTypes.Add((BTNode)type);
2359          }
2360       }
2361    }
2362    else if(_class)
2363    {
2364       Class sClass;
2365       for(sClass = _class; sClass; sClass = sClass.base)
2366       {
2367          ClassTemplateParameter p;
2368          for(p = sClass.templateParams.first; p; p = p.next)
2369          {
2370             //OldList * specs = MkList();
2371             //Declarator decl = null;
2372             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2373             if(p.type == type)
2374             {
2375                TemplateParameter param = p.param;
2376                TemplatedType type;
2377                if(!param)
2378                {
2379                   // ADD DATA TYPE HERE...
2380                   p.param = param = TemplateParameter
2381                   {
2382                      identifier = MkIdentifier(p.name), type = p.type,
2383                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2384                   };
2385                }
2386                type = TemplatedType { key = (uintptr)p.name, param = param };
2387                curContext.templateTypes.Add((BTNode)type);
2388             }
2389          }
2390       }
2391    }
2392    return context;
2393 }
2394
2395 public void FinishTemplatesContext(Context context)
2396 {
2397    PopContext(context);
2398    FreeContext(context);
2399    delete context;
2400 }
2401
2402 public void ProcessMethodType(Method method)
2403 {
2404    if(!method.dataType)
2405    {
2406       Context context = SetupTemplatesContext(method._class);
2407
2408       method.dataType = ProcessTypeString(method.dataTypeString, false);
2409
2410       FinishTemplatesContext(context);
2411
2412       if(method.type != virtualMethod && method.dataType)
2413       {
2414          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2415          {
2416             if(!method._class.symbol)
2417                method._class.symbol = FindClass(method._class.fullName);
2418             method.dataType.thisClass = method._class.symbol;
2419          }
2420       }
2421
2422       // Why was this commented out? Working fine without now...
2423
2424       /*
2425       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2426          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2427          */
2428    }
2429
2430    /*
2431    if(type)
2432    {
2433       char * par = strstr(type, "(");
2434       char * classOp = null;
2435       int classOpLen = 0;
2436       if(par)
2437       {
2438          int c;
2439          for(c = par-type-1; c >= 0; c++)
2440          {
2441             if(type[c] == ':' && type[c+1] == ':')
2442             {
2443                classOp = type + c - 1;
2444                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2445                {
2446                   classOp--;
2447                   classOpLen++;
2448                }
2449                break;
2450             }
2451             else if(!isspace(type[c]))
2452                break;
2453          }
2454       }
2455       if(classOp)
2456       {
2457          char temp[1024];
2458          int typeLen = strlen(type);
2459          memcpy(temp, classOp, classOpLen);
2460          temp[classOpLen] = '\0';
2461          if(temp[0])
2462             _class = eSystem_FindClass(module, temp);
2463          else
2464             _class = null;
2465          method.dataTypeString = new char[typeLen - classOpLen + 1];
2466          memcpy(method.dataTypeString, type, classOp - type);
2467          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2468       }
2469       else
2470          method.dataTypeString = type;
2471    }
2472    */
2473 }
2474
2475
2476 public void ProcessPropertyType(Property prop)
2477 {
2478    if(!prop.dataType)
2479    {
2480       Context context = SetupTemplatesContext(prop._class);
2481       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2482       FinishTemplatesContext(context);
2483    }
2484 }
2485
2486 public void DeclareMethod(Method method, const char * name)
2487 {
2488    Symbol symbol = method.symbol;
2489    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2490    {
2491       //bool imported = false;
2492       bool dllImport = false;
2493
2494       if(!method.dataType)
2495          method.dataType = ProcessTypeString(method.dataTypeString, false);
2496
2497       if(!symbol || symbol._import || method.type == virtualMethod)
2498       {
2499          if(!symbol || method.type == virtualMethod)
2500          {
2501             Symbol classSym;
2502             if(!method._class.symbol)
2503                method._class.symbol = FindClass(method._class.fullName);
2504             classSym = method._class.symbol;
2505             if(!classSym._import)
2506             {
2507                ModuleImport module;
2508
2509                if(method._class.module && method._class.module.name)
2510                   module = FindModule(method._class.module);
2511                else
2512                   module = mainModule;
2513                classSym._import = ClassImport
2514                {
2515                   name = CopyString(method._class.fullName);
2516                   isRemote = method._class.isRemote;
2517                };
2518                module.classes.Add(classSym._import);
2519             }
2520             if(!symbol)
2521             {
2522                symbol = method.symbol = Symbol { };
2523             }
2524             if(!symbol._import)
2525             {
2526                symbol._import = (ClassImport)MethodImport
2527                {
2528                   name = CopyString(method.name);
2529                   isVirtual = method.type == virtualMethod;
2530                };
2531                classSym._import.methods.Add(symbol._import);
2532             }
2533             if(!symbol)
2534             {
2535                // Set the symbol type
2536                /*
2537                if(!type.thisClass)
2538                {
2539                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2540                }
2541                else if(type.thisClass == (void *)-1)
2542                {
2543                   type.thisClass = null;
2544                }
2545                */
2546                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2547                symbol.type = method.dataType;
2548                if(symbol.type) symbol.type.refCount++;
2549             }
2550             /*
2551             if(!method.thisClass || strcmp(method.thisClass, "void"))
2552                symbol.type.params.Insert(null,
2553                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2554             */
2555          }
2556          if(!method.dataType.dllExport)
2557          {
2558             //imported = true;
2559             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2560                dllImport = true;
2561          }
2562       }
2563
2564       /* MOVING THIS UP
2565       if(!method.dataType)
2566          method.dataType = ((Symbol)method.symbol).type;
2567          //ProcessMethodType(method);
2568       */
2569
2570       if(method.type != virtualMethod && method.dataType)
2571          DeclareType(method.dataType, true, true);
2572
2573       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2574       {
2575          // We need a declaration here :)
2576          Declaration decl;
2577          OldList * specifiers, * declarators;
2578          Declarator d;
2579          Declarator funcDecl;
2580          External external;
2581
2582          specifiers = MkList();
2583          declarators = MkList();
2584
2585          //if(imported)
2586          if(dllImport)
2587             ListAdd(specifiers, MkSpecifier(EXTERN));
2588          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2589             ListAdd(specifiers, MkSpecifier(STATIC));
2590
2591          if(method.type == virtualMethod)
2592          {
2593             ListAdd(specifiers, MkSpecifier(INT));
2594             d = MkDeclaratorIdentifier(MkIdentifier(name));
2595          }
2596          else
2597          {
2598             d = MkDeclaratorIdentifier(MkIdentifier(name));
2599             //if(imported)
2600             if(dllImport)
2601                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2602             {
2603                Context context = SetupTemplatesContext(method._class);
2604                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2605                FinishTemplatesContext(context);
2606             }
2607             funcDecl = GetFuncDecl(d);
2608
2609             if(dllImport)
2610             {
2611                Specifier spec, next;
2612                for(spec = specifiers->first; spec; spec = next)
2613                {
2614                   next = spec.next;
2615                   if(spec.type == extendedSpecifier)
2616                   {
2617                      specifiers->Remove(spec);
2618                      FreeSpecifier(spec);
2619                   }
2620                }
2621             }
2622
2623             // Add this parameter if not a static method
2624             if(method.dataType && !method.dataType.staticMethod)
2625             {
2626                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2627                {
2628                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2629                   TypeName thisParam = MkTypeName(MkListOne(
2630                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2631                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2632                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2633                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2634
2635                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2636                   {
2637                      TypeName param = funcDecl.function.parameters->first;
2638                      funcDecl.function.parameters->Remove(param);
2639                      FreeTypeName(param);
2640                   }
2641
2642                   if(!funcDecl.function.parameters)
2643                      funcDecl.function.parameters = MkList();
2644                   funcDecl.function.parameters->Insert(null, thisParam);
2645                }
2646             }
2647             // Make sure we don't have empty parameter declarations for static methods...
2648             /*
2649             else if(!funcDecl.function.parameters)
2650             {
2651                funcDecl.function.parameters = MkList();
2652                funcDecl.function.parameters->Insert(null,
2653                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2654             }*/
2655          }
2656          // TESTING THIS:
2657          ProcessDeclarator(d);
2658
2659          ListAdd(declarators, MkInitDeclarator(d, null));
2660
2661          decl = MkDeclaration(specifiers, declarators);
2662
2663          ReplaceThisClassSpecifiers(specifiers, method._class);
2664
2665          // Keep a different symbol for the function definition than the declaration...
2666          if(symbol.pointerExternal)
2667          {
2668             Symbol functionSymbol { };
2669
2670             // Copy symbol
2671             {
2672                *functionSymbol = *symbol;
2673                functionSymbol.string = CopyString(symbol.string);
2674                if(functionSymbol.type)
2675                   functionSymbol.type.refCount++;
2676             }
2677
2678             excludedSymbols->Add(functionSymbol);
2679             symbol.pointerExternal.symbol = functionSymbol;
2680          }
2681          external = MkExternalDeclaration(decl);
2682          if(curExternal)
2683             ast->Insert(curExternal ? curExternal.prev : null, external);
2684          external.symbol = symbol;
2685          symbol.pointerExternal = external;
2686       }
2687       else if(ast)
2688       {
2689          // Move declaration higher...
2690          ast->Move(symbol.pointerExternal, curExternal.prev);
2691       }
2692
2693       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2694    }
2695 }
2696
2697 char * ReplaceThisClass(Class _class)
2698 {
2699    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2700    {
2701       bool first = true;
2702       int p = 0;
2703       ClassTemplateParameter param;
2704       int lastParam = -1;
2705
2706       char className[1024];
2707       strcpy(className, _class.fullName);
2708       for(param = _class.templateParams.first; param; param = param.next)
2709       {
2710          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2711          {
2712             if(first) strcat(className, "<");
2713             if(!first) strcat(className, ", ");
2714             if(lastParam + 1 != p)
2715             {
2716                strcat(className, param.name);
2717                strcat(className, " = ");
2718             }
2719             strcat(className, param.name);
2720             first = false;
2721             lastParam = p;
2722          }
2723          p++;
2724       }
2725       if(!first)
2726       {
2727          int len = strlen(className);
2728          if(className[len-1] == '>') className[len++] = ' ';
2729          className[len++] = '>';
2730          className[len++] = '\0';
2731       }
2732       return CopyString(className);
2733    }
2734    else
2735       return CopyString(_class.fullName);
2736 }
2737
2738 Type ReplaceThisClassType(Class _class)
2739 {
2740    Type type;
2741    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2742    {
2743       bool first = true;
2744       int p = 0;
2745       ClassTemplateParameter param;
2746       int lastParam = -1;
2747       char className[1024];
2748       strcpy(className, _class.fullName);
2749
2750       for(param = _class.templateParams.first; param; param = param.next)
2751       {
2752          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2753          {
2754             if(first) strcat(className, "<");
2755             if(!first) strcat(className, ", ");
2756             if(lastParam + 1 != p)
2757             {
2758                strcat(className, param.name);
2759                strcat(className, " = ");
2760             }
2761             strcat(className, param.name);
2762             first = false;
2763             lastParam = p;
2764          }
2765          p++;
2766       }
2767       if(!first)
2768       {
2769          int len = strlen(className);
2770          if(className[len-1] == '>') className[len++] = ' ';
2771          className[len++] = '>';
2772          className[len++] = '\0';
2773       }
2774       type = MkClassType(className);
2775       //type = ProcessTypeString(className, false);
2776    }
2777    else
2778    {
2779       type = MkClassType(_class.fullName);
2780       //type = ProcessTypeString(_class.fullName, false);
2781    }
2782    //type.wasThisClass = true;
2783    return type;
2784 }
2785
2786 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2787 {
2788    if(specs != null && _class)
2789    {
2790       Specifier spec;
2791       for(spec = specs.first; spec; spec = spec.next)
2792       {
2793          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2794          {
2795             spec.type = nameSpecifier;
2796             spec.name = ReplaceThisClass(_class);
2797             spec.symbol = FindClass(spec.name); //_class.symbol;
2798          }
2799       }
2800    }
2801 }
2802
2803 // Returns imported or not
2804 bool DeclareFunction(GlobalFunction function, char * name)
2805 {
2806    Symbol symbol = function.symbol;
2807    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2808    {
2809       bool imported = false;
2810       bool dllImport = false;
2811
2812       if(!function.dataType)
2813       {
2814          function.dataType = ProcessTypeString(function.dataTypeString, false);
2815          if(!function.dataType.thisClass)
2816             function.dataType.staticMethod = true;
2817       }
2818
2819       if(inCompiler)
2820       {
2821          if(!symbol)
2822          {
2823             ModuleImport module = FindModule(function.module);
2824             // WARNING: This is not added anywhere...
2825             symbol = function.symbol = Symbol {  };
2826
2827             if(module.name)
2828             {
2829                if(!function.dataType.dllExport)
2830                {
2831                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2832                   module.functions.Add(symbol._import);
2833                }
2834             }
2835             // Set the symbol type
2836             {
2837                symbol.type = ProcessTypeString(function.dataTypeString, false);
2838                if(!symbol.type.thisClass)
2839                   symbol.type.staticMethod = true;
2840             }
2841          }
2842          imported = symbol._import ? true : false;
2843          if(imported && function.module != privateModule && function.module.importType != staticImport)
2844             dllImport = true;
2845       }
2846
2847       DeclareType(function.dataType, true, true);
2848
2849       if(inCompiler)
2850       {
2851          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2852          {
2853             // We need a declaration here :)
2854             Declaration decl;
2855             OldList * specifiers, * declarators;
2856             Declarator d;
2857             Declarator funcDecl;
2858             External external;
2859
2860             specifiers = MkList();
2861             declarators = MkList();
2862
2863             //if(imported)
2864                ListAdd(specifiers, MkSpecifier(EXTERN));
2865             /*
2866             else
2867                ListAdd(specifiers, MkSpecifier(STATIC));
2868             */
2869
2870             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2871             //if(imported)
2872             if(dllImport)
2873                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2874
2875             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2876             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2877             if(function.module.importType == staticImport)
2878             {
2879                Specifier spec;
2880                for(spec = specifiers->first; spec; spec = spec.next)
2881                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2882                   {
2883                      specifiers->Remove(spec);
2884                      FreeSpecifier(spec);
2885                      break;
2886                   }
2887             }
2888
2889             funcDecl = GetFuncDecl(d);
2890
2891             // Make sure we don't have empty parameter declarations for static methods...
2892             if(funcDecl && !funcDecl.function.parameters)
2893             {
2894                funcDecl.function.parameters = MkList();
2895                funcDecl.function.parameters->Insert(null,
2896                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2897             }
2898
2899             ListAdd(declarators, MkInitDeclarator(d, null));
2900
2901             {
2902                Context oldCtx = curContext;
2903                curContext = globalContext;
2904                decl = MkDeclaration(specifiers, declarators);
2905                curContext = oldCtx;
2906             }
2907
2908             // Keep a different symbol for the function definition than the declaration...
2909             if(symbol.pointerExternal)
2910             {
2911                Symbol functionSymbol { };
2912                // Copy symbol
2913                {
2914                   *functionSymbol = *symbol;
2915                   functionSymbol.string = CopyString(symbol.string);
2916                   if(functionSymbol.type)
2917                      functionSymbol.type.refCount++;
2918                }
2919
2920                excludedSymbols->Add(functionSymbol);
2921
2922                symbol.pointerExternal.symbol = functionSymbol;
2923             }
2924             external = MkExternalDeclaration(decl);
2925             if(curExternal)
2926                ast->Insert(curExternal.prev, external);
2927             external.symbol = symbol;
2928             symbol.pointerExternal = external;
2929          }
2930          else
2931          {
2932             // Move declaration higher...
2933             ast->Move(symbol.pointerExternal, curExternal.prev);
2934          }
2935
2936          if(curExternal)
2937             symbol.id = curExternal.symbol.idCode;
2938       }
2939    }
2940    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2941 }
2942
2943 void DeclareGlobalData(GlobalData data)
2944 {
2945    Symbol symbol = data.symbol;
2946    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2947    {
2948       if(inCompiler)
2949       {
2950          if(!symbol)
2951             symbol = data.symbol = Symbol { };
2952       }
2953       if(!data.dataType)
2954          data.dataType = ProcessTypeString(data.dataTypeString, false);
2955       DeclareType(data.dataType, true, true);
2956       if(inCompiler)
2957       {
2958          if(!symbol.pointerExternal)
2959          {
2960             // We need a declaration here :)
2961             Declaration decl;
2962             OldList * specifiers, * declarators;
2963             Declarator d;
2964             External external;
2965
2966             specifiers = MkList();
2967             declarators = MkList();
2968
2969             ListAdd(specifiers, MkSpecifier(EXTERN));
2970             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2971             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2972
2973             ListAdd(declarators, MkInitDeclarator(d, null));
2974
2975             decl = MkDeclaration(specifiers, declarators);
2976             external = MkExternalDeclaration(decl);
2977             if(curExternal)
2978                ast->Insert(curExternal.prev, external);
2979             external.symbol = symbol;
2980             symbol.pointerExternal = external;
2981          }
2982          else
2983          {
2984             // Move declaration higher...
2985             ast->Move(symbol.pointerExternal, curExternal.prev);
2986          }
2987
2988          if(curExternal)
2989             symbol.id = curExternal.symbol.idCode;
2990       }
2991    }
2992 }
2993
2994 class Conversion : struct
2995 {
2996    Conversion prev, next;
2997    Property convert;
2998    bool isGet;
2999    Type resultType;
3000 };
3001
3002 static bool CheckConstCompatibility(Type source, Type dest, bool warn)
3003 {
3004    bool status = true;
3005    if(((source.kind == classType && source._class && source._class.registered) || source.kind == arrayType || source.kind == pointerType) &&
3006       ((dest.kind == classType && dest._class && dest._class.registered) || /*dest.kind == arrayType || */dest.kind == pointerType))
3007    {
3008       Class sourceClass = source.kind == classType ? source._class.registered : null;
3009       Class destClass = dest.kind == classType ? dest._class.registered : null;
3010       if((!sourceClass || (sourceClass && sourceClass.type == normalClass && !sourceClass.structSize)) &&
3011          (!destClass || (destClass && destClass.type == normalClass && !destClass.structSize)))
3012       {
3013          Type sourceType = source, destType = dest;
3014          while((sourceType.kind == pointerType || sourceType.kind == arrayType) && sourceType.type) sourceType = sourceType.type;
3015          while((destType.kind == pointerType || destType.kind == arrayType) && destType.type) destType = destType.type;
3016          if(!destType.constant && sourceType.constant)
3017          {
3018             status = false;
3019             if(warn)
3020                Compiler_Warning($"discarding const qualifier\n");
3021          }
3022       }
3023    }
3024    return status;
3025 }
3026
3027 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams,
3028                        bool isConversionExploration, bool warnConst)
3029 {
3030    if(source && dest)
3031    {
3032       if(warnConst)
3033          CheckConstCompatibility(source, dest, true);
3034       // Property convert;
3035
3036       if(source.kind == templateType && dest.kind != templateType)
3037       {
3038          Type type = ProcessTemplateParameterType(source.templateParameter);
3039          if(type) source = type;
3040       }
3041
3042       if(dest.kind == templateType && source.kind != templateType)
3043       {
3044          Type type = ProcessTemplateParameterType(dest.templateParameter);
3045          if(type) dest = type;
3046       }
3047
3048       if(dest.classObjectType == typedObject && dest.kind != functionType)
3049       {
3050          if(source.classObjectType != anyObject)
3051             return true;
3052          else
3053          {
3054             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
3055             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
3056             {
3057                return true;
3058             }
3059          }
3060       }
3061       else
3062       {
3063          if(source.kind != functionType && source.classObjectType == anyObject)
3064             return true;
3065          if(dest.kind != functionType && dest.classObjectType == anyObject && source.classObjectType != typedObject)
3066             return true;
3067       }
3068
3069       if((dest.kind == structType && source.kind == structType) ||
3070          (dest.kind == unionType && source.kind == unionType))
3071       {
3072          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
3073              (source.members.first && source.members.first == dest.members.first))
3074             return true;
3075       }
3076
3077       if(dest.kind == ellipsisType && source.kind != voidType)
3078          return true;
3079
3080       if(dest.kind == pointerType && dest.type.kind == voidType &&
3081          ((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))
3082          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
3083
3084          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
3085
3086          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
3087          return true;
3088       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
3089          ((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))
3090          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
3091          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
3092
3093          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
3094          return true;
3095
3096       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
3097       {
3098          if(source._class.registered && source._class.registered.type == unitClass)
3099          {
3100             if(conversions != null)
3101             {
3102                if(source._class.registered == dest._class.registered)
3103                   return true;
3104             }
3105             else
3106             {
3107                Class sourceBase, destBase;
3108                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
3109                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
3110                if(sourceBase == destBase)
3111                   return true;
3112             }
3113          }
3114          // Don't match enum inheriting from other enum if resolving enumeration values
3115          // TESTING: !dest.classObjectType
3116          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3117             (enumBaseType ||
3118                (!source._class.registered || source._class.registered.type != enumClass) ||
3119                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3120             return true;
3121          else
3122          {
3123             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3124             if(enumBaseType &&
3125                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3126                ((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)
3127             {
3128                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3129                {
3130                   return true;
3131                }
3132             }
3133          }
3134       }
3135
3136       // JUST ADDED THIS...
3137       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3138          return true;
3139
3140       if(doConversion)
3141       {
3142          // Just added this for Straight conversion of ColorAlpha => Color
3143          if(source.kind == classType)
3144          {
3145             Class _class;
3146             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3147             {
3148                Property convert;
3149                for(convert = _class.conversions.first; convert; convert = convert.next)
3150                {
3151                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3152                   {
3153                      Conversion after = (conversions != null) ? conversions.last : null;
3154
3155                      if(!convert.dataType)
3156                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3157                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3158                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3159                         MatchTypes(convert.dataType, dest, conversions, null, null,
3160                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3161                               convert.dataType.kind == classType, false, true, warnConst))
3162                      {
3163                         if(!conversions && !convert.Get)
3164                            return true;
3165                         else if(conversions != null)
3166                         {
3167                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3168                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3169                               (dest.kind != classType || dest._class.registered != _class.base))
3170                               return true;
3171                            else
3172                            {
3173                               Conversion conv { convert = convert, isGet = true };
3174                               // conversions.Add(conv);
3175                               conversions.Insert(after, conv);
3176
3177                               return true;
3178                            }
3179                         }
3180                      }
3181                   }
3182                }
3183             }
3184          }
3185
3186          // MOVING THIS??
3187
3188          if(dest.kind == classType)
3189          {
3190             Class _class;
3191             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3192             {
3193                Property convert;
3194                for(convert = _class.conversions.first; convert; convert = convert.next)
3195                {
3196                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3197                   {
3198                      Type constType = null;
3199                      bool success = false;
3200                      // Conversion after = (conversions != null) ? conversions.last : null;
3201
3202                      if(!convert.dataType)
3203                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3204
3205                      if(warnConst && convert.dataType.kind == pointerType && convert.dataType.type && dest.constant)
3206                      {
3207                         Type ptrType { };
3208                         constType = { kind = pointerType, refCount = 1, type = ptrType };
3209                         CopyTypeInto(ptrType, convert.dataType.type);
3210                         ptrType.constant = true;
3211                      }
3212
3213                      // Just added this equality check to prevent recursion.... Make it safer?
3214                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3215                      if((constType || convert.dataType != dest) && MatchTypes(source, constType ? constType : convert.dataType, conversions, null, null, true, false /*true*/, false, true, warnConst))
3216                      {
3217                         if(!conversions && !convert.Set)
3218                            success = true;
3219                         else if(conversions != null)
3220                         {
3221                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3222                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3223                               (source.kind != classType || source._class.registered != _class.base))
3224                               success = true;
3225                            else
3226                            {
3227                               // *** Testing this! ***
3228                               Conversion conv { convert = convert };
3229                               conversions.Add(conv);
3230                               //conversions.Insert(after, conv);
3231                               success = true;
3232                            }
3233                         }
3234                      }
3235                      if(constType)
3236                         FreeType(constType);
3237                      if(success)
3238                         return true;
3239                   }
3240                }
3241             }
3242             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3243             {
3244                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3245                   (source.kind != classType || source._class.registered.type != structClass))
3246                   return true;
3247             }*/
3248
3249             // TESTING THIS... IS THIS OK??
3250             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3251             {
3252                if(!dest._class.registered.dataType)
3253                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3254                // Only support this for classes...
3255                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3256                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3257                {
3258                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false, warnConst))
3259                   {
3260                      return true;
3261                   }
3262                }
3263             }
3264          }
3265
3266          // Moved this lower
3267          if(source.kind == classType)
3268          {
3269             Class _class;
3270             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3271             {
3272                Property convert;
3273                for(convert = _class.conversions.first; convert; convert = convert.next)
3274                {
3275                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3276                   {
3277                      Conversion after = (conversions != null) ? conversions.last : null;
3278
3279                      if(!convert.dataType)
3280                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3281                      if(convert.dataType != source &&
3282                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3283                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true, warnConst))
3284                      {
3285                         if(!conversions && !convert.Get)
3286                            return true;
3287                         else if(conversions != null)
3288                         {
3289                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3290                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3291                               (dest.kind != classType || dest._class.registered != _class.base))
3292                               return true;
3293                            else
3294                            {
3295                               Conversion conv { convert = convert, isGet = true };
3296
3297                               // conversions.Add(conv);
3298                               conversions.Insert(after, conv);
3299                               return true;
3300                            }
3301                         }
3302                      }
3303                   }
3304                }
3305             }
3306
3307             // TESTING THIS... IS THIS OK??
3308             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3309             {
3310                if(!source._class.registered.dataType)
3311                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3312                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3313                {
3314                   if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, source._class.registered.dataType.kind == classType, source._class.registered.dataType.kind == classType, false, false, warnConst))
3315                      return true;
3316                   // For bool to be accepted by byte, short, etc.
3317                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false, warnConst))
3318                      return true;
3319                }
3320             }
3321          }
3322       }
3323
3324       if(source.kind == classType || source.kind == subClassType)
3325          ;
3326       else if(dest.kind == source.kind &&
3327          (dest.kind != structType && dest.kind != unionType &&
3328           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3329           return true;
3330       // RECENTLY ADDED THESE
3331       else if(dest.kind == doubleType && source.kind == floatType)
3332          return true;
3333       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3334          return true;
3335       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3336          return true;
3337       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3338          return true;
3339       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3340          return true;
3341       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3342          return true;
3343       else if(source.kind == enumType &&
3344          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3345           return true;
3346       else if(dest.kind == enumType && !isConversionExploration &&
3347          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3348           return true;
3349       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3350               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3351       {
3352          Type paramSource, paramDest;
3353
3354          if(dest.kind == methodType)
3355             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3356          if(source.kind == methodType)
3357             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3358
3359          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3360          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3361          if(dest.kind == methodType)
3362             dest = dest.method.dataType;
3363          if(source.kind == methodType)
3364             source = source.method.dataType;
3365
3366          paramSource = source.params.first;
3367          if(paramSource && paramSource.kind == voidType) paramSource = null;
3368          paramDest = dest.params.first;
3369          if(paramDest && paramDest.kind == voidType) paramDest = null;
3370
3371
3372          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3373             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3374          {
3375             // Source thisClass must be derived from destination thisClass
3376             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3377                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3378             {
3379                if(paramDest && paramDest.kind == classType)
3380                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3381                else
3382                   Compiler_Error($"method class should not take an object\n");
3383                return false;
3384             }
3385             paramDest = paramDest.next;
3386          }
3387          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3388          {
3389             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3390             {
3391                if(dest.thisClass)
3392                {
3393                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3394                   {
3395                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3396                      return false;
3397                   }
3398                }
3399                else
3400                {
3401                   // THIS WAS BACKWARDS:
3402                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3403                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3404                   {
3405                      if(owningClassDest)
3406                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3407                      else
3408                         Compiler_Error($"overriding class expected to be derived from method class\n");
3409                      return false;
3410                   }
3411                }
3412                paramSource = paramSource.next;
3413             }
3414             else
3415             {
3416                if(dest.thisClass)
3417                {
3418                   // Source thisClass must be derived from destination thisClass
3419                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3420                   {
3421                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3422                      return false;
3423                   }
3424                }
3425                else
3426                {
3427                   // THIS WAS BACKWARDS TOO??
3428                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3429                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3430                   {
3431                      //if(owningClass)
3432                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3433                      //else
3434                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3435                      return false;
3436                   }
3437                }
3438             }
3439          }
3440
3441
3442          // Source return type must be derived from destination return type
3443          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false, warnConst))
3444          {
3445             Compiler_Warning($"incompatible return type for function\n");
3446             return false;
3447          }
3448          // The const check is backwards from the MatchTypes above (for derivative classes checks)
3449          else
3450             CheckConstCompatibility(dest.returnType, source.returnType, true);
3451
3452          // Check parameters
3453
3454          for(; paramDest; paramDest = paramDest.next)
3455          {
3456             if(!paramSource)
3457             {
3458                //Compiler_Warning($"not enough parameters\n");
3459                Compiler_Error($"not enough parameters\n");
3460                return false;
3461             }
3462             {
3463                Type paramDestType = paramDest;
3464                Type paramSourceType = paramSource;
3465                Type type = paramDestType;
3466
3467                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3468                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3469                   paramSource.kind != templateType)
3470                {
3471                   int id = 0;
3472                   ClassTemplateParameter curParam = null;
3473                   Class sClass;
3474                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3475                   {
3476                      id = 0;
3477                      if(sClass.templateClass) sClass = sClass.templateClass;
3478                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3479                      {
3480                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3481                         {
3482                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3483                            {
3484                               if(sClass.templateClass) sClass = sClass.templateClass;
3485                               id += sClass.templateParams.count;
3486                            }
3487                            break;
3488                         }
3489                         id++;
3490                      }
3491                      if(curParam) break;
3492                   }
3493
3494                   if(curParam)
3495                   {
3496                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3497                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3498                   }
3499                }
3500
3501                // paramDest must be derived from paramSource
3502                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false, warnConst) &&
3503                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false, warnConst)))
3504                {
3505                   char type[1024];
3506                   type[0] = 0;
3507                   PrintType(paramDest, type, false, true);
3508                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3509
3510                   if(paramDestType != paramDest)
3511                      FreeType(paramDestType);
3512                   return false;
3513                }
3514                if(paramDestType != paramDest)
3515                   FreeType(paramDestType);
3516             }
3517
3518             paramSource = paramSource.next;
3519          }
3520          if(paramSource)
3521          {
3522             Compiler_Error($"too many parameters\n");
3523             return false;
3524          }
3525          return true;
3526       }
3527       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3528       {
3529          return true;
3530       }
3531       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3532          (source.kind == arrayType || source.kind == pointerType))
3533       {
3534          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3535             return true;
3536       }
3537    }
3538    return false;
3539 }
3540
3541 static void FreeConvert(Conversion convert)
3542 {
3543    if(convert.resultType)
3544       FreeType(convert.resultType);
3545 }
3546
3547 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3548                               char * string, OldList conversions)
3549 {
3550    BTNamedLink link;
3551
3552    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3553    {
3554       Class _class = link.data;
3555       if(_class.type == enumClass)
3556       {
3557          OldList converts { };
3558          Type type { };
3559          type.kind = classType;
3560
3561          if(!_class.symbol)
3562             _class.symbol = FindClass(_class.fullName);
3563          type._class = _class.symbol;
3564
3565          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false, false))
3566          {
3567             NamedLink64 value;
3568             Class enumClass = eSystem_FindClass(privateModule, "enum");
3569             if(enumClass)
3570             {
3571                Class baseClass;
3572                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3573                {
3574                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3575                   for(value = e.values.first; value; value = value.next)
3576                   {
3577                      if(!strcmp(value.name, string))
3578                         break;
3579                   }
3580                   if(value)
3581                   {
3582                      FreeExpContents(sourceExp);
3583                      FreeType(sourceExp.expType);
3584
3585                      sourceExp.isConstant = true;
3586                      sourceExp.expType = MkClassType(baseClass.fullName);
3587                      //if(inCompiler)
3588                      {
3589                         char constant[256];
3590                         sourceExp.type = constantExp;
3591                         if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "short") || !strcmp(baseClass.dataTypeString, "char"))
3592                            sprintf(constant, FORMAT64D, value.data);
3593                         else
3594                            sprintf(constant, FORMAT64HEXLL, value.data);
3595                         sourceExp.constant = CopyString(constant);
3596                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3597                      }
3598
3599                      while(converts.first)
3600                      {
3601                         Conversion convert = converts.first;
3602                         converts.Remove(convert);
3603                         conversions.Add(convert);
3604                      }
3605                      delete type;
3606                      return true;
3607                   }
3608                }
3609             }
3610          }
3611          if(converts.first)
3612             converts.Free(FreeConvert);
3613          delete type;
3614       }
3615    }
3616    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3617       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3618          return true;
3619    return false;
3620 }
3621
3622 public bool ModuleVisibility(Module searchIn, Module searchFor)
3623 {
3624    SubModule subModule;
3625
3626    if(searchFor == searchIn)
3627       return true;
3628
3629    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3630    {
3631       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3632       {
3633          if(ModuleVisibility(subModule.module, searchFor))
3634             return true;
3635       }
3636    }
3637    return false;
3638 }
3639
3640 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3641 {
3642    Module module;
3643
3644    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3645       return true;
3646    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3647       return true;
3648    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3649       return true;
3650
3651    for(module = mainModule.application.allModules.first; module; module = module.next)
3652    {
3653       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3654          return true;
3655    }
3656    return false;
3657 }
3658
3659 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3660 {
3661    Type source;
3662    Type realDest = dest;
3663    Type backupSourceExpType = null;
3664    Expression computedExp = sourceExp;
3665    dest.refCount++;
3666
3667    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3668       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3669    {
3670       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3671       ComputeExpression(computedExp /*sourceExp*/);
3672    }
3673
3674    source = sourceExp.expType;
3675
3676    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3677    {
3678       if(computedExp != sourceExp)
3679       {
3680          FreeExpression(computedExp);
3681          computedExp = sourceExp;
3682       }
3683       FreeType(dest);
3684       return true;
3685    }
3686
3687    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3688    {
3689        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3690        {
3691           Class sourceBase, destBase;
3692           for(sourceBase = source._class.registered;
3693               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3694               sourceBase = sourceBase.base);
3695           for(destBase = dest._class.registered;
3696               destBase && destBase.base && destBase.base.type != systemClass;
3697               destBase = destBase.base);
3698           //if(source._class.registered == dest._class.registered)
3699           if(sourceBase == destBase)
3700           {
3701             if(computedExp != sourceExp)
3702             {
3703                FreeExpression(computedExp);
3704                computedExp = sourceExp;
3705             }
3706             FreeType(dest);
3707             return true;
3708          }
3709       }
3710    }
3711
3712    if(source)
3713    {
3714       OldList * specs;
3715       bool flag = false;
3716       int64 value = MAXINT;
3717
3718       source.refCount++;
3719
3720       if(computedExp.type == constantExp)
3721       {
3722          if(source.isSigned)
3723             value = strtoll(computedExp.constant, null, 0);
3724          else
3725             value = strtoull(computedExp.constant, null, 0);
3726       }
3727       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3728       {
3729          if(source.isSigned)
3730             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3731          else
3732             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3733       }
3734       if(computedExp != sourceExp)
3735       {
3736          FreeExpression(computedExp);
3737          computedExp = sourceExp;
3738       }
3739
3740       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3741          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3742       {
3743          FreeType(source);
3744          source = Type { kind = intType, isSigned = false, refCount = 1 };
3745       }
3746
3747       if(dest.kind == classType)
3748       {
3749          Class _class = dest._class ? dest._class.registered : null;
3750
3751          if(_class && _class.type == unitClass)
3752          {
3753             if(source.kind != classType)
3754             {
3755                Type tempType { };
3756                Type tempDest, tempSource;
3757
3758                for(; _class.base.type != systemClass; _class = _class.base);
3759                tempSource = dest;
3760                tempDest = tempType;
3761
3762                tempType.kind = classType;
3763                if(!_class.symbol)
3764                   _class.symbol = FindClass(_class.fullName);
3765
3766                tempType._class = _class.symbol;
3767                tempType.truth = dest.truth;
3768                if(tempType._class)
3769                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3770
3771                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3772                backupSourceExpType = sourceExp.expType;
3773                sourceExp.expType = dest; dest.refCount++;
3774                //sourceExp.expType = MkClassType(_class.fullName);
3775                flag = true;
3776
3777                delete tempType;
3778             }
3779          }
3780
3781
3782          // Why wasn't there something like this?
3783          if(_class && _class.type == bitClass && source.kind != classType)
3784          {
3785             if(!dest._class.registered.dataType)
3786                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3787             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3788             {
3789                FreeType(source);
3790                FreeType(sourceExp.expType);
3791                source = sourceExp.expType = MkClassType(dest._class.string);
3792                source.refCount++;
3793
3794                //source.kind = classType;
3795                //source._class = dest._class;
3796             }
3797          }
3798
3799          // Adding two enumerations
3800          /*
3801          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3802          {
3803             if(!source._class.registered.dataType)
3804                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3805             if(!dest._class.registered.dataType)
3806                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3807
3808             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3809             {
3810                FreeType(source);
3811                source = sourceExp.expType = MkClassType(dest._class.string);
3812                source.refCount++;
3813
3814                //source.kind = classType;
3815                //source._class = dest._class;
3816             }
3817          }*/
3818
3819          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3820          {
3821             OldList * specs = MkList();
3822             Declarator decl;
3823             char string[1024];
3824
3825             ReadString(string, sourceExp.string);
3826             decl = SpecDeclFromString(string, specs, null);
3827
3828             FreeExpContents(sourceExp);
3829             FreeType(sourceExp.expType);
3830
3831             sourceExp.type = classExp;
3832             sourceExp._classExp.specifiers = specs;
3833             sourceExp._classExp.decl = decl;
3834             sourceExp.expType = dest;
3835             dest.refCount++;
3836
3837             FreeType(source);
3838             FreeType(dest);
3839             if(backupSourceExpType) FreeType(backupSourceExpType);
3840             return true;
3841          }
3842       }
3843       else if(source.kind == classType)
3844       {
3845          Class _class = source._class ? source._class.registered : null;
3846
3847          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3848          {
3849             /*
3850             if(dest.kind != classType)
3851             {
3852                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3853                if(!source._class.registered.dataType)
3854                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3855
3856                FreeType(dest);
3857                dest = MkClassType(source._class.string);
3858                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3859                //   dest = MkClassType(source._class.string);
3860             }
3861             */
3862
3863             if(dest.kind != classType)
3864             {
3865                Type tempType { };
3866                Type tempDest, tempSource;
3867
3868                if(!source._class.registered.dataType)
3869                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3870
3871                for(; _class.base.type != systemClass; _class = _class.base);
3872                tempDest = source;
3873                tempSource = tempType;
3874                tempType.kind = classType;
3875                tempType._class = FindClass(_class.fullName);
3876                tempType.truth = source.truth;
3877                tempType.classObjectType = source.classObjectType;
3878
3879                if(tempType._class)
3880                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3881
3882                // PUT THIS BACK TESTING UNITS?
3883                if(conversions.last)
3884                {
3885                   ((Conversion)(conversions.last)).resultType = dest;
3886                   dest.refCount++;
3887                }
3888
3889                FreeType(sourceExp.expType);
3890                sourceExp.expType = MkClassType(_class.fullName);
3891                sourceExp.expType.truth = source.truth;
3892                sourceExp.expType.classObjectType = source.classObjectType;
3893
3894                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3895
3896                if(!sourceExp.destType)
3897                {
3898                   FreeType(sourceExp.destType);
3899                   sourceExp.destType = sourceExp.expType;
3900                   if(sourceExp.expType)
3901                      sourceExp.expType.refCount++;
3902                }
3903                //flag = true;
3904                //source = _class.dataType;
3905
3906
3907                // TOCHECK: TESTING THIS NEW CODE
3908                if(!_class.dataType)
3909                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3910                FreeType(dest);
3911                dest = MkClassType(source._class.string);
3912                dest.truth = source.truth;
3913                dest.classObjectType = source.classObjectType;
3914
3915                FreeType(source);
3916                source = _class.dataType;
3917                source.refCount++;
3918
3919                delete tempType;
3920             }
3921          }
3922       }
3923
3924       if(!flag)
3925       {
3926          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3927          {
3928             FreeType(source);
3929             FreeType(dest);
3930             return true;
3931          }
3932       }
3933
3934       // Implicit Casts
3935       /*
3936       if(source.kind == classType)
3937       {
3938          Class _class = source._class.registered;
3939          if(_class.type == unitClass)
3940          {
3941             if(!_class.dataType)
3942                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3943             source = _class.dataType;
3944          }
3945       }*/
3946
3947       if(dest.kind == classType)
3948       {
3949          Class _class = dest._class ? dest._class.registered : null;
3950          bool fittingValue = false;
3951          if(_class && _class.type == enumClass)
3952          {
3953             Class enumClass = eSystem_FindClass(privateModule, "enum");
3954             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3955             if(c && value >= 0 && value <= c.largest)
3956                fittingValue = true;
3957          }
3958
3959          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3960             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3961          {
3962             if(_class.type == normalClass || _class.type == noHeadClass)
3963             {
3964                Expression newExp { };
3965                *newExp = *sourceExp;
3966                if(sourceExp.destType) sourceExp.destType.refCount++;
3967                if(sourceExp.expType)  sourceExp.expType.refCount++;
3968                sourceExp.type = castExp;
3969                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3970                sourceExp.cast.exp = newExp;
3971                FreeType(sourceExp.expType);
3972                sourceExp.expType = null;
3973                ProcessExpressionType(sourceExp);
3974
3975                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3976                if(!inCompiler)
3977                {
3978                   FreeType(sourceExp.expType);
3979                   sourceExp.expType = dest;
3980                }
3981
3982                FreeType(source);
3983                if(inCompiler) FreeType(dest);
3984
3985                if(backupSourceExpType) FreeType(backupSourceExpType);
3986                return true;
3987             }
3988
3989             if(!_class.dataType)
3990                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3991             FreeType(dest);
3992             dest = _class.dataType;
3993             dest.refCount++;
3994          }
3995
3996          // Accept lower precision types for units, since we want to keep the unit type
3997          if(dest.kind == doubleType &&
3998             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3999              source.kind == charType || source.kind == _BoolType))
4000          {
4001             specs = MkListOne(MkSpecifier(DOUBLE));
4002          }
4003          else if(dest.kind == floatType &&
4004             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
4005             source.kind == _BoolType || source.kind == doubleType))
4006          {
4007             specs = MkListOne(MkSpecifier(FLOAT));
4008          }
4009          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
4010             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4011          {
4012             specs = MkList();
4013             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4014             ListAdd(specs, MkSpecifier(INT64));
4015          }
4016          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
4017             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4018          {
4019             specs = MkList();
4020             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4021             ListAdd(specs, MkSpecifier(INT));
4022          }
4023          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
4024             source.kind == floatType || source.kind == doubleType))
4025          {
4026             specs = MkList();
4027             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4028             ListAdd(specs, MkSpecifier(SHORT));
4029          }
4030          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
4031             source.kind == floatType || source.kind == doubleType))
4032          {
4033             specs = MkList();
4034             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4035             ListAdd(specs, MkSpecifier(CHAR));
4036          }
4037          else
4038          {
4039             FreeType(source);
4040             FreeType(dest);
4041             if(backupSourceExpType)
4042             {
4043                // Failed to convert: revert previous exp type
4044                if(sourceExp.expType) FreeType(sourceExp.expType);
4045                sourceExp.expType = backupSourceExpType;
4046             }
4047             return false;
4048          }
4049       }
4050       else if(dest.kind == doubleType &&
4051          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
4052           source.kind == _BoolType || source.kind == charType))
4053       {
4054          specs = MkListOne(MkSpecifier(DOUBLE));
4055       }
4056       else if(dest.kind == floatType &&
4057          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4058       {
4059          specs = MkListOne(MkSpecifier(FLOAT));
4060       }
4061       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4062          (value == 1 || value == 0))
4063       {
4064          specs = MkList();
4065          ListAdd(specs, MkSpecifier(BOOL));
4066       }
4067       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4068          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
4069       {
4070          specs = MkList();
4071          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4072          ListAdd(specs, MkSpecifier(CHAR));
4073       }
4074       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
4075          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
4076       {
4077          specs = MkList();
4078          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4079          ListAdd(specs, MkSpecifier(SHORT));
4080       }
4081       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
4082       {
4083          specs = MkList();
4084          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4085          ListAdd(specs, MkSpecifier(INT));
4086       }
4087       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
4088       {
4089          specs = MkList();
4090          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4091          ListAdd(specs, MkSpecifier(INT64));
4092       }
4093       else if(dest.kind == enumType &&
4094          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4095       {
4096          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
4097       }
4098       else
4099       {
4100          FreeType(source);
4101          FreeType(dest);
4102          if(backupSourceExpType)
4103          {
4104             // Failed to convert: revert previous exp type
4105             if(sourceExp.expType) FreeType(sourceExp.expType);
4106             sourceExp.expType = backupSourceExpType;
4107          }
4108          return false;
4109       }
4110
4111       if(!flag && !sourceExp.opDestType)
4112       {
4113          Expression newExp { };
4114          *newExp = *sourceExp;
4115          newExp.prev = null;
4116          newExp.next = null;
4117          if(sourceExp.destType) sourceExp.destType.refCount++;
4118          if(sourceExp.expType)  sourceExp.expType.refCount++;
4119
4120          sourceExp.type = castExp;
4121          if(realDest.kind == classType)
4122          {
4123             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
4124             FreeList(specs, FreeSpecifier);
4125          }
4126          else
4127             sourceExp.cast.typeName = MkTypeName(specs, null);
4128          if(newExp.type == opExp)
4129          {
4130             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4131          }
4132          else
4133             sourceExp.cast.exp = newExp;
4134
4135          FreeType(sourceExp.expType);
4136          sourceExp.expType = null;
4137          ProcessExpressionType(sourceExp);
4138       }
4139       else
4140          FreeList(specs, FreeSpecifier);
4141
4142       FreeType(dest);
4143       FreeType(source);
4144       if(backupSourceExpType) FreeType(backupSourceExpType);
4145
4146       return true;
4147    }
4148    else
4149    {
4150       if(computedExp != sourceExp)
4151       {
4152          FreeExpression(computedExp);
4153          computedExp = sourceExp;
4154       }
4155
4156       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4157       if(sourceExp.type == identifierExp)
4158       {
4159          Identifier id = sourceExp.identifier;
4160          if(dest.kind == classType)
4161          {
4162             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4163             {
4164                Class _class = dest._class.registered;
4165                Class enumClass = eSystem_FindClass(privateModule, "enum");
4166                if(enumClass)
4167                {
4168                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4169                   {
4170                      NamedLink64 value;
4171                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4172                      for(value = e.values.first; value; value = value.next)
4173                      {
4174                         if(!strcmp(value.name, id.string))
4175                            break;
4176                      }
4177                      if(value)
4178                      {
4179                         FreeExpContents(sourceExp);
4180                         FreeType(sourceExp.expType);
4181
4182                         sourceExp.isConstant = true;
4183                         sourceExp.expType = MkClassType(_class.fullName);
4184                         //if(inCompiler)
4185                         {
4186                            sourceExp.type = constantExp;
4187                            if(_class.dataTypeString && (!strcmp(_class.dataTypeString, "int") || !strcmp(_class.dataTypeString, "int64") || !strcmp(_class.dataTypeString, "short") || !strcmp(_class.dataTypeString, "char"))) // _class cannot be null here!
4188                               sourceExp.constant = PrintInt64(value.data);
4189                            else
4190                               sourceExp.constant = PrintUInt64(value.data);
4191                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4192                         }
4193                         FreeType(dest);
4194                         return true;
4195                      }
4196                   }
4197                }
4198             }
4199          }
4200
4201          // Loop through all enum classes
4202          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4203          {
4204             FreeType(dest);
4205             return true;
4206          }
4207       }
4208       FreeType(dest);
4209    }
4210    return false;
4211 }
4212
4213 #define TERTIARY(o, name, m, t, p) \
4214    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4215    {                                                              \
4216       exp.type = constantExp;                                    \
4217       exp.string = p(op1.m ? op2.m : op3.m);                     \
4218       if(!exp.expType) \
4219          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4220       return true;                                                \
4221    }
4222
4223 #define BINARY(o, name, m, t, p) \
4224    static bool name(Expression exp, Operand op1, Operand op2)   \
4225    {                                                              \
4226       t value2 = op2.m;                                           \
4227       exp.type = constantExp;                                    \
4228       exp.string = p((t)(op1.m o value2));                     \
4229       if(!exp.expType) \
4230          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4231       return true;                                                \
4232    }
4233
4234 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4235    static bool name(Expression exp, Operand op1, Operand op2)   \
4236    {                                                              \
4237       t value2 = op2.m;                                           \
4238       exp.type = constantExp;                                    \
4239       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4240       if(!exp.expType) \
4241          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4242       return true;                                                \
4243    }
4244
4245 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4246    static bool name(Expression exp, Operand op1, Operand op2)   \
4247    {                                                              \
4248       t value2 = op2.m;                                           \
4249       exp.type = constantExp;                                    \
4250       exp.string = p(op1.m o value2);             \
4251       if(!exp.expType) \
4252          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4253       return true;                                                \
4254    }
4255
4256 #define UNARY(o, name, m, t, p) \
4257    static bool name(Expression exp, Operand op1)                \
4258    {                                                              \
4259       exp.type = constantExp;                                    \
4260       exp.string = p((t)(o op1.m));                                   \
4261       if(!exp.expType) \
4262          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4263       return true;                                                \
4264    }
4265
4266 #define OPERATOR_ALL(macro, o, name) \
4267    macro(o, Int##name, i, int, PrintInt) \
4268    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4269    macro(o, Int64##name, i64, int64, PrintInt64) \
4270    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4271    macro(o, Short##name, s, short, PrintShort) \
4272    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4273    macro(o, Char##name, c, char, PrintChar) \
4274    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4275    macro(o, Float##name, f, float, PrintFloat) \
4276    macro(o, Double##name, d, double, PrintDouble)
4277
4278 #define OPERATOR_INTTYPES(macro, o, name) \
4279    macro(o, Int##name, i, int, PrintInt) \
4280    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4281    macro(o, Int64##name, i64, int64, PrintInt64) \
4282    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4283    macro(o, Short##name, s, short, PrintShort) \
4284    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4285    macro(o, Char##name, c, char, PrintChar) \
4286    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4287
4288 #define OPERATOR_REALTYPES(macro, o, name) \
4289    macro(o, Float##name, f, float, PrintFloat) \
4290    macro(o, Double##name, d, double, PrintDouble)
4291
4292 // binary arithmetic
4293 OPERATOR_ALL(BINARY, +, Add)
4294 OPERATOR_ALL(BINARY, -, Sub)
4295 OPERATOR_ALL(BINARY, *, Mul)
4296 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4297 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4298 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4299
4300 // unary arithmetic
4301 OPERATOR_ALL(UNARY, -, Neg)
4302
4303 // unary arithmetic increment and decrement
4304 OPERATOR_ALL(UNARY, ++, Inc)
4305 OPERATOR_ALL(UNARY, --, Dec)
4306
4307 // binary arithmetic assignment
4308 OPERATOR_ALL(BINARY, =, Asign)
4309 OPERATOR_ALL(BINARY, +=, AddAsign)
4310 OPERATOR_ALL(BINARY, -=, SubAsign)
4311 OPERATOR_ALL(BINARY, *=, MulAsign)
4312 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4313 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4314 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4315
4316 // binary bitwise
4317 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4318 OPERATOR_INTTYPES(BINARY, |, BitOr)
4319 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4320 OPERATOR_INTTYPES(BINARY, <<, LShift)
4321 OPERATOR_INTTYPES(BINARY, >>, RShift)
4322
4323 // unary bitwise
4324 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4325
4326 // binary bitwise assignment
4327 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4328 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4329 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4330 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4331 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4332
4333 // unary logical negation
4334 OPERATOR_INTTYPES(UNARY, !, Not)
4335
4336 // binary logical equality
4337 OPERATOR_ALL(BINARY, ==, Equ)
4338 OPERATOR_ALL(BINARY, !=, Nqu)
4339
4340 // binary logical
4341 OPERATOR_ALL(BINARY, &&, And)
4342 OPERATOR_ALL(BINARY, ||, Or)
4343
4344 // binary logical relational
4345 OPERATOR_ALL(BINARY, >, Grt)
4346 OPERATOR_ALL(BINARY, <, Sma)
4347 OPERATOR_ALL(BINARY, >=, GrtEqu)
4348 OPERATOR_ALL(BINARY, <=, SmaEqu)
4349
4350 // tertiary condition operator
4351 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4352
4353 //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
4354 #define OPERATOR_TABLE_ALL(name, type) \
4355     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4356                           type##Neg, \
4357                           type##Inc, type##Dec, \
4358                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4359                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4360                           type##BitNot, \
4361                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4362                           type##Not, \
4363                           type##Equ, type##Nqu, \
4364                           type##And, type##Or, \
4365                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4366                         }; \
4367
4368 #define OPERATOR_TABLE_INTTYPES(name, type) \
4369     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4370                           type##Neg, \
4371                           type##Inc, type##Dec, \
4372                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4373                           null, null, null, null, null, \
4374                           null, \
4375                           null, null, null, null, null, \
4376                           null, \
4377                           type##Equ, type##Nqu, \
4378                           type##And, type##Or, \
4379                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4380                         }; \
4381
4382 OPERATOR_TABLE_ALL(int, Int)
4383 OPERATOR_TABLE_ALL(uint, UInt)
4384 OPERATOR_TABLE_ALL(int64, Int64)
4385 OPERATOR_TABLE_ALL(uint64, UInt64)
4386 OPERATOR_TABLE_ALL(short, Short)
4387 OPERATOR_TABLE_ALL(ushort, UShort)
4388 OPERATOR_TABLE_INTTYPES(float, Float)
4389 OPERATOR_TABLE_INTTYPES(double, Double)
4390 OPERATOR_TABLE_ALL(char, Char)
4391 OPERATOR_TABLE_ALL(uchar, UChar)
4392
4393 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4394 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4395 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4396 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4397 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4398 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4399 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4400 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4401
4402 public void ReadString(char * output,  char * string)
4403 {
4404    int len = strlen(string);
4405    int c,d = 0;
4406    bool quoted = false, escaped = false;
4407    for(c = 0; c<len; c++)
4408    {
4409       char ch = string[c];
4410       if(escaped)
4411       {
4412          switch(ch)
4413          {
4414             case 'n': output[d] = '\n'; break;
4415             case 't': output[d] = '\t'; break;
4416             case 'a': output[d] = '\a'; break;
4417             case 'b': output[d] = '\b'; break;
4418             case 'f': output[d] = '\f'; break;
4419             case 'r': output[d] = '\r'; break;
4420             case 'v': output[d] = '\v'; break;
4421             case '\\': output[d] = '\\'; break;
4422             case '\"': output[d] = '\"'; break;
4423             case '\'': output[d] = '\''; break;
4424             default: output[d] = ch;
4425          }
4426          d++;
4427          escaped = false;
4428       }
4429       else
4430       {
4431          if(ch == '\"')
4432             quoted ^= true;
4433          else if(quoted)
4434          {
4435             if(ch == '\\')
4436                escaped = true;
4437             else
4438                output[d++] = ch;
4439          }
4440       }
4441    }
4442    output[d] = '\0';
4443 }
4444
4445 // String Unescape Copy
4446
4447 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4448 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4449 public int UnescapeString(char * d, char * s, int len)
4450 {
4451    int j = 0, k = 0;
4452    char ch;
4453    while(j < len && (ch = s[j]))
4454    {
4455       switch(ch)
4456       {
4457          case '\\':
4458             switch((ch = s[++j]))
4459             {
4460                case 'n': d[k] = '\n'; break;
4461                case 't': d[k] = '\t'; break;
4462                case 'a': d[k] = '\a'; break;
4463                case 'b': d[k] = '\b'; break;
4464                case 'f': d[k] = '\f'; break;
4465                case 'r': d[k] = '\r'; break;
4466                case 'v': d[k] = '\v'; break;
4467                case '\\': d[k] = '\\'; break;
4468                case '\"': d[k] = '\"'; break;
4469                case '\'': d[k] = '\''; break;
4470                default: d[k] = '\\'; d[k] = ch;
4471             }
4472             break;
4473          default:
4474             d[k] = ch;
4475       }
4476       j++, k++;
4477    }
4478    d[k] = '\0';
4479    return k;
4480 }
4481
4482 public char * OffsetEscapedString(char * s, int len, int offset)
4483 {
4484    char ch;
4485    int j = 0, k = 0;
4486    while(j < len && k < offset && (ch = s[j]))
4487    {
4488       if(ch == '\\') ++j;
4489       j++, k++;
4490    }
4491    return (k == offset) ? s + j : null;
4492 }
4493
4494 public Operand GetOperand(Expression exp)
4495 {
4496    Operand op { };
4497    Type type = exp.expType;
4498    if(type)
4499    {
4500       while(type.kind == classType &&
4501          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4502       {
4503          if(!type._class.registered.dataType)
4504             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4505          type = type._class.registered.dataType;
4506
4507       }
4508       if(exp.type == stringExp && op.kind == pointerType)
4509       {
4510          op.ui64 = (uint64)exp.string;
4511          op.kind = pointerType;
4512          op.ops = uint64Ops;
4513       }
4514       else if(exp.isConstant && exp.type == constantExp)
4515       {
4516          op.kind = type.kind;
4517          op.type = type;
4518
4519          switch(op.kind)
4520          {
4521             case _BoolType:
4522             case charType:
4523             {
4524                if(exp.constant[0] == '\'')
4525                {
4526                   op.c = exp.constant[1];
4527                   op.ops = charOps;
4528                }
4529                else if(type.isSigned)
4530                {
4531                   op.c = (char)strtol(exp.constant, null, 0);
4532                   op.ops = charOps;
4533                }
4534                else
4535                {
4536                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4537                   op.ops = ucharOps;
4538                }
4539                break;
4540             }
4541             case shortType:
4542                if(type.isSigned)
4543                {
4544                   op.s = (short)strtol(exp.constant, null, 0);
4545                   op.ops = shortOps;
4546                }
4547                else
4548                {
4549                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4550                   op.ops = ushortOps;
4551                }
4552                break;
4553             case intType:
4554             case longType:
4555                if(type.isSigned)
4556                {
4557                   op.i = (int)strtol(exp.constant, null, 0);
4558                   op.ops = intOps;
4559                }
4560                else
4561                {
4562                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4563                   op.ops = uintOps;
4564                }
4565                op.kind = intType;
4566                break;
4567             case int64Type:
4568                if(type.isSigned)
4569                {
4570                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4571                   op.ops = int64Ops;
4572                }
4573                else
4574                {
4575                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4576                   op.ops = uint64Ops;
4577                }
4578                op.kind = int64Type;
4579                break;
4580             case intPtrType:
4581                if(type.isSigned)
4582                {
4583                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4584                   op.ops = int64Ops;
4585                }
4586                else
4587                {
4588                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4589                   op.ops = uint64Ops;
4590                }
4591                op.kind = int64Type;
4592                break;
4593             case intSizeType:
4594                if(type.isSigned)
4595                {
4596                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4597                   op.ops = int64Ops;
4598                }
4599                else
4600                {
4601                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4602                   op.ops = uint64Ops;
4603                }
4604                op.kind = int64Type;
4605                break;
4606             case floatType:
4607                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4608                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4609                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4610                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4611                else
4612                   op.f = (float)strtod(exp.constant, null);
4613                op.ops = floatOps;
4614                break;
4615             case doubleType:
4616                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4617                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4618                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4619                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4620                else
4621                   op.d = (double)strtod(exp.constant, null);
4622                op.ops = doubleOps;
4623                break;
4624             //case classType:    For when we have operator overloading...
4625             // Pointer additions
4626             //case functionType:
4627             case arrayType:
4628             case pointerType:
4629             case classType:
4630                op.ui64 = _strtoui64(exp.constant, null, 0);
4631                op.kind = pointerType;
4632                op.ops = uint64Ops;
4633                // op.ptrSize =
4634                break;
4635          }
4636       }
4637    }
4638    return op;
4639 }
4640
4641 static int64 GetEnumValue(Class _class, void * ptr)
4642 {
4643    int64 v = 0;
4644    switch(_class.typeSize)
4645    {
4646       case 8:
4647          if(!strcmp(_class.dataTypeString, "uint64"))
4648             v = (int64)*(uint64 *)ptr;
4649          else
4650             v = (int64)*(int64 *)ptr;
4651          break;
4652       case 4:
4653          if(!strcmp(_class.dataTypeString, "uint"))
4654             v = (int64)*(uint *)ptr;
4655          else
4656             v = (int64)*(int *)ptr;
4657          break;
4658       case 2:
4659          if(!strcmp(_class.dataTypeString, "uint16"))
4660             v = (int64)*(uint16 *)ptr;
4661          else
4662             v = (int64)*(short *)ptr;
4663          break;
4664       case 1:
4665          if(!strcmp(_class.dataTypeString, "byte"))
4666             v = (int64)*(byte *)ptr;
4667          else
4668             v = (int64)*(char *)ptr;
4669          break;
4670    }
4671    return v;
4672 }
4673
4674 static __attribute__((unused)) void UnusedFunction()
4675 {
4676    int a;
4677    a.OnGetString(0,0,0);
4678 }
4679 default:
4680 extern int __ecereVMethodID_class_OnGetString;
4681 public:
4682
4683 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4684 {
4685    DataMember dataMember;
4686    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4687    {
4688       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4689          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4690       else
4691       {
4692          Expression exp { };
4693          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4694          Type type;
4695          void * ptr = inst.data + dataMember.offset + offset;
4696          char * result = null;
4697          exp.loc = member.loc = inst.loc;
4698          ((Identifier)member.identifiers->first).loc = inst.loc;
4699
4700          if(!dataMember.dataType)
4701             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4702          type = dataMember.dataType;
4703          if(type.kind == classType)
4704          {
4705             Class _class = type._class.registered;
4706             if(_class.type == enumClass)
4707             {
4708                Class enumClass = eSystem_FindClass(privateModule, "enum");
4709                if(enumClass)
4710                {
4711                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4712                   NamedLink64 item;
4713                   for(item = e.values.first; item; item = item.next)
4714                   {
4715                      if(item.data == GetEnumValue(_class, ptr))
4716                      {
4717                         result = item.name;
4718                         break;
4719                      }
4720                   }
4721                   if(result)
4722                   {
4723                      exp.identifier = MkIdentifier(result);
4724                      exp.type = identifierExp;
4725                      exp.destType = MkClassType(_class.fullName);
4726                      ProcessExpressionType(exp);
4727                   }
4728                }
4729             }
4730             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4731             {
4732                if(!_class.dataType)
4733                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4734                type = _class.dataType;
4735             }
4736          }
4737          if(!result)
4738          {
4739             switch(type.kind)
4740             {
4741                case floatType:
4742                {
4743                   FreeExpContents(exp);
4744
4745                   exp.constant = PrintFloat(*(float*)ptr);
4746                   exp.type = constantExp;
4747                   break;
4748                }
4749                case doubleType:
4750                {
4751                   FreeExpContents(exp);
4752
4753                   exp.constant = PrintDouble(*(double*)ptr);
4754                   exp.type = constantExp;
4755                   break;
4756                }
4757                case intType:
4758                {
4759                   FreeExpContents(exp);
4760
4761                   exp.constant = PrintInt(*(int*)ptr);
4762                   exp.type = constantExp;
4763                   break;
4764                }
4765                case int64Type:
4766                {
4767                   FreeExpContents(exp);
4768
4769                   exp.constant = PrintInt64(*(int64*)ptr);
4770                   exp.type = constantExp;
4771                   break;
4772                }
4773                case intPtrType:
4774                {
4775                   FreeExpContents(exp);
4776                   // TODO: This should probably use proper type
4777                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4778                   exp.type = constantExp;
4779                   break;
4780                }
4781                case intSizeType:
4782                {
4783                   FreeExpContents(exp);
4784                   // TODO: This should probably use proper type
4785                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4786                   exp.type = constantExp;
4787                   break;
4788                }
4789                default:
4790                   Compiler_Error($"Unhandled type populating instance\n");
4791             }
4792          }
4793          ListAdd(memberList, member);
4794       }
4795
4796       if(parentDataMember.type == unionMember)
4797          break;
4798    }
4799 }
4800
4801 void PopulateInstance(Instantiation inst)
4802 {
4803    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4804    Class _class = classSym.registered;
4805    DataMember dataMember;
4806    OldList * memberList = MkList();
4807    // Added this check and ->Add to prevent memory leaks on bad code
4808    if(!inst.members)
4809       inst.members = MkListOne(MkMembersInitList(memberList));
4810    else
4811       inst.members->Add(MkMembersInitList(memberList));
4812    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4813    {
4814       if(!dataMember.isProperty)
4815       {
4816          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4817             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4818          else
4819          {
4820             Expression exp { };
4821             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4822             Type type;
4823             void * ptr = inst.data + dataMember.offset;
4824             char * result = null;
4825
4826             exp.loc = member.loc = inst.loc;
4827             ((Identifier)member.identifiers->first).loc = inst.loc;
4828
4829             if(!dataMember.dataType)
4830                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4831             type = dataMember.dataType;
4832             if(type.kind == classType)
4833             {
4834                Class _class = type._class.registered;
4835                if(_class.type == enumClass)
4836                {
4837                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4838                   if(enumClass)
4839                   {
4840                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4841                      NamedLink64 item;
4842                      for(item = e.values.first; item; item = item.next)
4843                      {
4844                         if(item.data == GetEnumValue(_class, ptr))
4845                         {
4846                            result = item.name;
4847                            break;
4848                         }
4849                      }
4850                   }
4851                   if(result)
4852                   {
4853                      exp.identifier = MkIdentifier(result);
4854                      exp.type = identifierExp;
4855                      exp.destType = MkClassType(_class.fullName);
4856                      ProcessExpressionType(exp);
4857                   }
4858                }
4859                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4860                {
4861                   if(!_class.dataType)
4862                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4863                   type = _class.dataType;
4864                }
4865             }
4866             if(!result)
4867             {
4868                switch(type.kind)
4869                {
4870                   case floatType:
4871                   {
4872                      exp.constant = PrintFloat(*(float*)ptr);
4873                      exp.type = constantExp;
4874                      break;
4875                   }
4876                   case doubleType:
4877                   {
4878                      exp.constant = PrintDouble(*(double*)ptr);
4879                      exp.type = constantExp;
4880                      break;
4881                   }
4882                   case intType:
4883                   {
4884                      exp.constant = PrintInt(*(int*)ptr);
4885                      exp.type = constantExp;
4886                      break;
4887                   }
4888                   case int64Type:
4889                   {
4890                      exp.constant = PrintInt64(*(int64*)ptr);
4891                      exp.type = constantExp;
4892                      break;
4893                   }
4894                   case intPtrType:
4895                   {
4896                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4897                      exp.type = constantExp;
4898                      break;
4899                   }
4900                   default:
4901                      Compiler_Error($"Unhandled type populating instance\n");
4902                }
4903             }
4904             ListAdd(memberList, member);
4905          }
4906       }
4907    }
4908 }
4909
4910 void ComputeInstantiation(Expression exp)
4911 {
4912    Instantiation inst = exp.instance;
4913    MembersInit members;
4914    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4915    Class _class = classSym ? classSym.registered : null;
4916    DataMember curMember = null;
4917    Class curClass = null;
4918    DataMember subMemberStack[256];
4919    int subMemberStackPos = 0;
4920    uint64 bits = 0;
4921
4922    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4923    {
4924       // Don't recompute the instantiation...
4925       // Non Simple classes will have become constants by now
4926       if(inst.data)
4927          return;
4928
4929       if(_class.type == normalClass || _class.type == noHeadClass)
4930       {
4931          inst.data = (byte *)eInstance_New(_class);
4932          if(_class.type == normalClass)
4933             ((Instance)inst.data)._refCount++;
4934       }
4935       else
4936          inst.data = new0 byte[_class.structSize];
4937    }
4938
4939    if(inst.members)
4940    {
4941       for(members = inst.members->first; members; members = members.next)
4942       {
4943          switch(members.type)
4944          {
4945             case dataMembersInit:
4946             {
4947                if(members.dataMembers)
4948                {
4949                   MemberInit member;
4950                   for(member = members.dataMembers->first; member; member = member.next)
4951                   {
4952                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4953                      bool found = false;
4954
4955                      Property prop = null;
4956                      DataMember dataMember = null;
4957                      uint dataMemberOffset;
4958
4959                      if(!ident)
4960                      {
4961                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4962                         if(curMember)
4963                         {
4964                            if(curMember.isProperty)
4965                               prop = (Property)curMember;
4966                            else
4967                            {
4968                               dataMember = curMember;
4969
4970                               // CHANGED THIS HERE
4971                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4972
4973                               // 2013/17/29 -- It seems that this was missing here!
4974                               if(_class.type == normalClass)
4975                                  dataMemberOffset += _class.base.structSize;
4976                               // dataMemberOffset = dataMember.offset;
4977                            }
4978                            found = true;
4979                         }
4980                      }
4981                      else
4982                      {
4983                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4984                         if(prop)
4985                         {
4986                            found = true;
4987                            if(prop.memberAccess == publicAccess)
4988                            {
4989                               curMember = (DataMember)prop;
4990                               curClass = prop._class;
4991                            }
4992                         }
4993                         else
4994                         {
4995                            DataMember _subMemberStack[256];
4996                            int _subMemberStackPos = 0;
4997
4998                            // FILL MEMBER STACK
4999                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
5000
5001                            if(dataMember)
5002                            {
5003                               found = true;
5004                               if(dataMember.memberAccess == publicAccess)
5005                               {
5006                                  curMember = dataMember;
5007                                  curClass = dataMember._class;
5008                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
5009                                  subMemberStackPos = _subMemberStackPos;
5010                               }
5011                            }
5012                         }
5013                      }
5014
5015                      if(found && member.initializer && member.initializer.type == expInitializer)
5016                      {
5017                         Expression value = member.initializer.exp;
5018                         Type type = null;
5019                         bool deepMember = false;
5020                         if(prop)
5021                         {
5022                            type = prop.dataType;
5023                         }
5024                         else if(dataMember)
5025                         {
5026                            if(!dataMember.dataType)
5027                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
5028
5029                            type = dataMember.dataType;
5030                         }
5031
5032                         if(ident && ident.next)
5033                         {
5034                            deepMember = true;
5035
5036                            // for(; ident && type; ident = ident.next)
5037                            for(ident = ident.next; ident && type; ident = ident.next)
5038                            {
5039                               if(type.kind == classType)
5040                               {
5041                                  prop = eClass_FindProperty(type._class.registered,
5042                                     ident.string, privateModule);
5043                                  if(prop)
5044                                     type = prop.dataType;
5045                                  else
5046                                  {
5047                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
5048                                        ident.string, &dataMemberOffset, privateModule, null, null);
5049                                     if(dataMember)
5050                                        type = dataMember.dataType;
5051                                  }
5052                               }
5053                               else if(type.kind == structType || type.kind == unionType)
5054                               {
5055                                  Type memberType;
5056                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
5057                                  {
5058                                     if(!strcmp(memberType.name, ident.string))
5059                                     {
5060                                        type = memberType;
5061                                        break;
5062                                     }
5063                                  }
5064                               }
5065                            }
5066                         }
5067                         if(value)
5068                         {
5069                            FreeType(value.destType);
5070                            value.destType = type;
5071                            if(type) type.refCount++;
5072                            ComputeExpression(value);
5073                         }
5074                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
5075                         {
5076                            if(type.kind == classType)
5077                            {
5078                               Class _class = type._class.registered;
5079                               if(_class.type == bitClass || _class.type == unitClass ||
5080                                  _class.type == enumClass)
5081                               {
5082                                  if(!_class.dataType)
5083                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5084                                  type = _class.dataType;
5085                               }
5086                            }
5087
5088                            if(dataMember)
5089                            {
5090                               void * ptr = inst.data + dataMemberOffset;
5091
5092                               if(value.type == constantExp)
5093                               {
5094                                  switch(type.kind)
5095                                  {
5096                                     case intType:
5097                                     {
5098                                        GetInt(value, (int*)ptr);
5099                                        break;
5100                                     }
5101                                     case int64Type:
5102                                     {
5103                                        GetInt64(value, (int64*)ptr);
5104                                        break;
5105                                     }
5106                                     case intPtrType:
5107                                     {
5108                                        GetIntPtr(value, (intptr*)ptr);
5109                                        break;
5110                                     }
5111                                     case intSizeType:
5112                                     {
5113                                        GetIntSize(value, (intsize*)ptr);
5114                                        break;
5115                                     }
5116                                     case floatType:
5117                                     {
5118                                        GetFloat(value, (float*)ptr);
5119                                        break;
5120                                     }
5121                                     case doubleType:
5122                                     {
5123                                        GetDouble(value, (double *)ptr);
5124                                        break;
5125                                     }
5126                                  }
5127                               }
5128                               else if(value.type == instanceExp)
5129                               {
5130                                  if(type.kind == classType)
5131                                  {
5132                                     Class _class = type._class.registered;
5133                                     if(_class.type == structClass)
5134                                     {
5135                                        ComputeTypeSize(type);
5136                                        if(value.instance.data)
5137                                           memcpy(ptr, value.instance.data, type.size);
5138                                     }
5139                                  }
5140                               }
5141                            }
5142                            else if(prop)
5143                            {
5144                               if(value.type == instanceExp && value.instance.data)
5145                               {
5146                                  if(type.kind == classType)
5147                                  {
5148                                     Class _class = type._class.registered;
5149                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5150                                     {
5151                                        void (*Set)(void *, void *) = (void *)prop.Set;
5152                                        Set(inst.data, value.instance.data);
5153                                        PopulateInstance(inst);
5154                                     }
5155                                  }
5156                               }
5157                               else if(value.type == constantExp)
5158                               {
5159                                  switch(type.kind)
5160                                  {
5161                                     case doubleType:
5162                                     {
5163                                        void (*Set)(void *, double) = (void *)prop.Set;
5164                                        Set(inst.data, strtod(value.constant, null) );
5165                                        break;
5166                                     }
5167                                     case floatType:
5168                                     {
5169                                        void (*Set)(void *, float) = (void *)prop.Set;
5170                                        Set(inst.data, (float)(strtod(value.constant, null)));
5171                                        break;
5172                                     }
5173                                     case intType:
5174                                     {
5175                                        void (*Set)(void *, int) = (void *)prop.Set;
5176                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5177                                        break;
5178                                     }
5179                                     case int64Type:
5180                                     {
5181                                        void (*Set)(void *, int64) = (void *)prop.Set;
5182                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5183                                        break;
5184                                     }
5185                                     case intPtrType:
5186                                     {
5187                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5188                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5189                                        break;
5190                                     }
5191                                     case intSizeType:
5192                                     {
5193                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5194                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5195                                        break;
5196                                     }
5197                                  }
5198                               }
5199                               else if(value.type == stringExp)
5200                               {
5201                                  char temp[1024];
5202                                  ReadString(temp, value.string);
5203                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5204                               }
5205                            }
5206                         }
5207                         else if(!deepMember && type && _class.type == unitClass)
5208                         {
5209                            if(prop)
5210                            {
5211                               // Only support converting units to units for now...
5212                               if(value.type == constantExp)
5213                               {
5214                                  if(type.kind == classType)
5215                                  {
5216                                     Class _class = type._class.registered;
5217                                     if(_class.type == unitClass)
5218                                     {
5219                                        if(!_class.dataType)
5220                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5221                                        type = _class.dataType;
5222                                     }
5223                                  }
5224                                  // TODO: Assuming same base type for units...
5225                                  switch(type.kind)
5226                                  {
5227                                     case floatType:
5228                                     {
5229                                        float fValue;
5230                                        float (*Set)(float) = (void *)prop.Set;
5231                                        GetFloat(member.initializer.exp, &fValue);
5232                                        exp.constant = PrintFloat(Set(fValue));
5233                                        exp.type = constantExp;
5234                                        break;
5235                                     }
5236                                     case doubleType:
5237                                     {
5238                                        double dValue;
5239                                        double (*Set)(double) = (void *)prop.Set;
5240                                        GetDouble(member.initializer.exp, &dValue);
5241                                        exp.constant = PrintDouble(Set(dValue));
5242                                        exp.type = constantExp;
5243                                        break;
5244                                     }
5245                                  }
5246                               }
5247                            }
5248                         }
5249                         else if(!deepMember && type && _class.type == bitClass)
5250                         {
5251                            if(prop)
5252                            {
5253                               if(value.type == instanceExp && value.instance.data)
5254                               {
5255                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5256                                  bits = Set(value.instance.data);
5257                               }
5258                               else if(value.type == constantExp)
5259                               {
5260                               }
5261                            }
5262                            else if(dataMember)
5263                            {
5264                               BitMember bitMember = (BitMember) dataMember;
5265                               Type type;
5266                               uint64 part = 0;
5267                               bits = (bits & ~bitMember.mask);
5268                               if(!bitMember.dataType)
5269                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5270                               type = bitMember.dataType;
5271                               if(type.kind == classType && type._class && type._class.registered)
5272                               {
5273                                  if(!type._class.registered.dataType)
5274                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5275                                  type = type._class.registered.dataType;
5276                               }
5277                               switch(type.kind)
5278                               {
5279                                  case _BoolType:
5280                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5281                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (short *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5282                                  case intType:
5283                                  case longType:       { uint v; type.isSigned ? GetInt(value, (int *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5284                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (int64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5285                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (intptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5286                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (intsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5287                               }
5288                               bits |= part << bitMember.pos;
5289                            }
5290                         }
5291                      }
5292                      else
5293                      {
5294                         if(_class && _class.type == unitClass)
5295                         {
5296                            ComputeExpression(member.initializer.exp);
5297                            exp.constant = member.initializer.exp.constant;
5298                            exp.type = constantExp;
5299
5300                            member.initializer.exp.constant = null;
5301                         }
5302                      }
5303                   }
5304                }
5305                break;
5306             }
5307          }
5308       }
5309    }
5310    if(_class && _class.type == bitClass)
5311    {
5312       exp.constant = PrintHexUInt(bits);
5313       exp.type = constantExp;
5314    }
5315    if(exp.type != instanceExp)
5316    {
5317       FreeInstance(inst);
5318    }
5319 }
5320
5321 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5322 {
5323    bool result = false;
5324    switch(kind)
5325    {
5326       case shortType:
5327          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5328             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5329          break;
5330       case intType:
5331       case longType:
5332          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5333             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5334          break;
5335       case int64Type:
5336          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5337             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5338             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5339          break;
5340       case floatType:
5341          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5342             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5343             result = GetOpFloat(op, &op.f);
5344          break;
5345       case doubleType:
5346          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5347             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5348             result = GetOpDouble(op, &op.d);
5349          break;
5350       case pointerType:
5351          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5352             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5353             result = GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5354          break;
5355       case enumType:
5356          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5357             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5358             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5359          break;
5360       case intPtrType:
5361          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5362             result = isSigned ? GetOpInt64 /*GetOpIntPtr*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5363          break;
5364       case intSizeType:
5365          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5366             result = isSigned ? GetOpInt64 /*GetOpIntSize*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntSize*/(op, &op.ui64);
5367          break;
5368    }
5369    return result;
5370 }
5371
5372 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5373 {
5374    if(exp.op.op == SIZEOF)
5375    {
5376       FreeExpContents(exp);
5377       exp.type = constantExp;
5378       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5379    }
5380    else
5381    {
5382       if(!exp.op.exp1)
5383       {
5384          switch(exp.op.op)
5385          {
5386             // unary arithmetic
5387             case '+':
5388             {
5389                // Provide default unary +
5390                Expression exp2 = exp.op.exp2;
5391                exp.op.exp2 = null;
5392                FreeExpContents(exp);
5393                FreeType(exp.expType);
5394                FreeType(exp.destType);
5395                *exp = *exp2;
5396                delete exp2;
5397                break;
5398             }
5399             case '-':
5400                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5401                break;
5402             // unary arithmetic increment and decrement
5403                   //OPERATOR_ALL(UNARY, ++, Inc)
5404                   //OPERATOR_ALL(UNARY, --, Dec)
5405             // unary bitwise
5406             case '~':
5407                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5408                break;
5409             // unary logical negation
5410             case '!':
5411                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5412                break;
5413          }
5414       }
5415       else
5416       {
5417          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5418          {
5419             if(Promote(op2, op1.kind, op1.type.isSigned))
5420                op2.kind = op1.kind, op2.ops = op1.ops;
5421             else if(Promote(op1, op2.kind, op2.type.isSigned))
5422                op1.kind = op2.kind, op1.ops = op2.ops;
5423          }
5424          switch(exp.op.op)
5425          {
5426             // binary arithmetic
5427             case '+':
5428                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5429                break;
5430             case '-':
5431                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5432                break;
5433             case '*':
5434                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5435                break;
5436             case '/':
5437                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5438                break;
5439             case '%':
5440                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5441                break;
5442             // binary arithmetic assignment
5443                   //OPERATOR_ALL(BINARY, =, Asign)
5444                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5445                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5446                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5447                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5448                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5449             // binary bitwise
5450             case '&':
5451                if(exp.op.exp2)
5452                {
5453                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5454                }
5455                break;
5456             case '|':
5457                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5458                break;
5459             case '^':
5460                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5461                break;
5462             case LEFT_OP:
5463                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5464                break;
5465             case RIGHT_OP:
5466                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5467                break;
5468             // binary bitwise assignment
5469                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5470                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5471                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5472                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5473                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5474             // binary logical equality
5475             case EQ_OP:
5476                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5477                break;
5478             case NE_OP:
5479                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5480                break;
5481             // binary logical
5482             case AND_OP:
5483                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5484                break;
5485             case OR_OP:
5486                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5487                break;
5488             // binary logical relational
5489             case '>':
5490                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5491                break;
5492             case '<':
5493                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5494                break;
5495             case GE_OP:
5496                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5497                break;
5498             case LE_OP:
5499                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5500                break;
5501          }
5502       }
5503    }
5504 }
5505
5506 void ComputeExpression(Expression exp)
5507 {
5508 #ifdef _DEBUG
5509    char expString[10240];
5510    expString[0] = '\0';
5511    PrintExpression(exp, expString);
5512 #endif
5513
5514    switch(exp.type)
5515    {
5516       case instanceExp:
5517       {
5518          ComputeInstantiation(exp);
5519          break;
5520       }
5521       /*
5522       case constantExp:
5523          break;
5524       */
5525       case opExp:
5526       {
5527          Expression exp1, exp2 = null;
5528          Operand op1 { };
5529          Operand op2 { };
5530
5531          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5532          if(exp.op.exp2)
5533          {
5534             Expression e = exp.op.exp2;
5535
5536             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5537             {
5538                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5539                {
5540                   if(e.type == extensionCompoundExp)
5541                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5542                   else
5543                      e = e.list->last;
5544                }
5545             }
5546             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5547             {
5548                if(e.type == stringExp && e.string)
5549                {
5550                   char * string = e.string;
5551                   int len = strlen(string);
5552                   char * tmp = new char[len-2+1];
5553                   len = UnescapeString(tmp, string + 1, len - 2);
5554                   delete tmp;
5555                   FreeExpContents(exp);
5556                   exp.type = constantExp;
5557                   exp.constant = PrintUInt(len + 1);
5558                }
5559                else
5560                {
5561                   Type type = e.expType;
5562                   type.refCount++;
5563                   FreeExpContents(exp);
5564                   exp.type = constantExp;
5565                   exp.constant = PrintUInt(ComputeTypeSize(type));
5566                   FreeType(type);
5567                }
5568                break;
5569             }
5570             else
5571                ComputeExpression(exp.op.exp2);
5572          }
5573          if(exp.op.exp1)
5574          {
5575             ComputeExpression(exp.op.exp1);
5576             exp1 = exp.op.exp1;
5577             exp2 = exp.op.exp2;
5578             op1 = GetOperand(exp1);
5579             if(op1.type) op1.type.refCount++;
5580             if(exp2)
5581             {
5582                op2 = GetOperand(exp2);
5583                if(op2.type) op2.type.refCount++;
5584             }
5585          }
5586          else
5587          {
5588             exp1 = exp.op.exp2;
5589             op1 = GetOperand(exp1);
5590             if(op1.type) op1.type.refCount++;
5591          }
5592
5593          CallOperator(exp, exp1, exp2, op1, op2);
5594          /*
5595          switch(exp.op.op)
5596          {
5597             // Unary operators
5598             case '&':
5599                // Also binary
5600                if(exp.op.exp1 && exp.op.exp2)
5601                {
5602                   // Binary And
5603                   if(op1.ops.BitAnd)
5604                   {
5605                      FreeExpContents(exp);
5606                      op1.ops.BitAnd(exp, op1, op2);
5607                   }
5608                }
5609                break;
5610             case '*':
5611                if(exp.op.exp1)
5612                {
5613                   if(op1.ops.Mul)
5614                   {
5615                      FreeExpContents(exp);
5616                      op1.ops.Mul(exp, op1, op2);
5617                   }
5618                }
5619                break;
5620             case '+':
5621                if(exp.op.exp1)
5622                {
5623                   if(op1.ops.Add)
5624                   {
5625                      FreeExpContents(exp);
5626                      op1.ops.Add(exp, op1, op2);
5627                   }
5628                }
5629                else
5630                {
5631                   // Provide default unary +
5632                   Expression exp2 = exp.op.exp2;
5633                   exp.op.exp2 = null;
5634                   FreeExpContents(exp);
5635                   FreeType(exp.expType);
5636                   FreeType(exp.destType);
5637
5638                   *exp = *exp2;
5639                   delete exp2;
5640                }
5641                break;
5642             case '-':
5643                if(exp.op.exp1)
5644                {
5645                   if(op1.ops.Sub)
5646                   {
5647                      FreeExpContents(exp);
5648                      op1.ops.Sub(exp, op1, op2);
5649                   }
5650                }
5651                else
5652                {
5653                   if(op1.ops.Neg)
5654                   {
5655                      FreeExpContents(exp);
5656                      op1.ops.Neg(exp, op1);
5657                   }
5658                }
5659                break;
5660             case '~':
5661                if(op1.ops.BitNot)
5662                {
5663                   FreeExpContents(exp);
5664                   op1.ops.BitNot(exp, op1);
5665                }
5666                break;
5667             case '!':
5668                if(op1.ops.Not)
5669                {
5670                   FreeExpContents(exp);
5671                   op1.ops.Not(exp, op1);
5672                }
5673                break;
5674             // Binary only operators
5675             case '/':
5676                if(op1.ops.Div)
5677                {
5678                   FreeExpContents(exp);
5679                   op1.ops.Div(exp, op1, op2);
5680                }
5681                break;
5682             case '%':
5683                if(op1.ops.Mod)
5684                {
5685                   FreeExpContents(exp);
5686                   op1.ops.Mod(exp, op1, op2);
5687                }
5688                break;
5689             case LEFT_OP:
5690                break;
5691             case RIGHT_OP:
5692                break;
5693             case '<':
5694                if(exp.op.exp1)
5695                {
5696                   if(op1.ops.Sma)
5697                   {
5698                      FreeExpContents(exp);
5699                      op1.ops.Sma(exp, op1, op2);
5700                   }
5701                }
5702                break;
5703             case '>':
5704                if(exp.op.exp1)
5705                {
5706                   if(op1.ops.Grt)
5707                   {
5708                      FreeExpContents(exp);
5709                      op1.ops.Grt(exp, op1, op2);
5710                   }
5711                }
5712                break;
5713             case LE_OP:
5714                if(exp.op.exp1)
5715                {
5716                   if(op1.ops.SmaEqu)
5717                   {
5718                      FreeExpContents(exp);
5719                      op1.ops.SmaEqu(exp, op1, op2);
5720                   }
5721                }
5722                break;
5723             case GE_OP:
5724                if(exp.op.exp1)
5725                {
5726                   if(op1.ops.GrtEqu)
5727                   {
5728                      FreeExpContents(exp);
5729                      op1.ops.GrtEqu(exp, op1, op2);
5730                   }
5731                }
5732                break;
5733             case EQ_OP:
5734                if(exp.op.exp1)
5735                {
5736                   if(op1.ops.Equ)
5737                   {
5738                      FreeExpContents(exp);
5739                      op1.ops.Equ(exp, op1, op2);
5740                   }
5741                }
5742                break;
5743             case NE_OP:
5744                if(exp.op.exp1)
5745                {
5746                   if(op1.ops.Nqu)
5747                   {
5748                      FreeExpContents(exp);
5749                      op1.ops.Nqu(exp, op1, op2);
5750                   }
5751                }
5752                break;
5753             case '|':
5754                if(op1.ops.BitOr)
5755                {
5756                   FreeExpContents(exp);
5757                   op1.ops.BitOr(exp, op1, op2);
5758                }
5759                break;
5760             case '^':
5761                if(op1.ops.BitXor)
5762                {
5763                   FreeExpContents(exp);
5764                   op1.ops.BitXor(exp, op1, op2);
5765                }
5766                break;
5767             case AND_OP:
5768                break;
5769             case OR_OP:
5770                break;
5771             case SIZEOF:
5772                FreeExpContents(exp);
5773                exp.type = constantExp;
5774                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5775                break;
5776          }
5777          */
5778          if(op1.type) FreeType(op1.type);
5779          if(op2.type) FreeType(op2.type);
5780          break;
5781       }
5782       case bracketsExp:
5783       case extensionExpressionExp:
5784       {
5785          Expression e, n;
5786          for(e = exp.list->first; e; e = n)
5787          {
5788             n = e.next;
5789             if(!n)
5790             {
5791                OldList * list = exp.list;
5792                Expression prev = exp.prev;
5793                Expression next = exp.next;
5794                ComputeExpression(e);
5795                //FreeExpContents(exp);
5796                FreeType(exp.expType);
5797                FreeType(exp.destType);
5798                *exp = *e;
5799                exp.prev = prev;
5800                exp.next = next;
5801                delete e;
5802                delete list;
5803             }
5804             else
5805             {
5806                FreeExpression(e);
5807             }
5808          }
5809          break;
5810       }
5811       /*
5812
5813       case ExpIndex:
5814       {
5815          Expression e;
5816          exp.isConstant = true;
5817
5818          ComputeExpression(exp.index.exp);
5819          if(!exp.index.exp.isConstant)
5820             exp.isConstant = false;
5821
5822          for(e = exp.index.index->first; e; e = e.next)
5823          {
5824             ComputeExpression(e);
5825             if(!e.next)
5826             {
5827                // Check if this type is int
5828             }
5829             if(!e.isConstant)
5830                exp.isConstant = false;
5831          }
5832          exp.expType = Dereference(exp.index.exp.expType);
5833          break;
5834       }
5835       */
5836       case memberExp:
5837       {
5838          Expression memberExp = exp.member.exp;
5839          Identifier memberID = exp.member.member;
5840
5841          Type type;
5842          ComputeExpression(exp.member.exp);
5843          type = exp.member.exp.expType;
5844          if(type)
5845          {
5846             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);
5847             Property prop = null;
5848             DataMember member = null;
5849             Class convertTo = null;
5850             if(type.kind == subClassType && exp.member.exp.type == classExp)
5851                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5852
5853             if(!_class)
5854             {
5855                char string[256];
5856                Symbol classSym;
5857                string[0] = '\0';
5858                PrintTypeNoConst(type, string, false, true);
5859                classSym = FindClass(string);
5860                _class = classSym ? classSym.registered : null;
5861             }
5862
5863             if(exp.member.member)
5864             {
5865                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5866                if(!prop)
5867                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5868             }
5869             if(!prop && !member && _class && exp.member.member)
5870             {
5871                Symbol classSym = FindClass(exp.member.member.string);
5872                convertTo = _class;
5873                _class = classSym ? classSym.registered : null;
5874                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5875             }
5876
5877             if(prop)
5878             {
5879                if(prop.compiled)
5880                {
5881                   Type type = prop.dataType;
5882                   // TODO: Assuming same base type for units...
5883                   if(_class.type == unitClass)
5884                   {
5885                      if(type.kind == classType)
5886                      {
5887                         Class _class = type._class.registered;
5888                         if(_class.type == unitClass)
5889                         {
5890                            if(!_class.dataType)
5891                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5892                            type = _class.dataType;
5893                         }
5894                      }
5895                      switch(type.kind)
5896                      {
5897                         case floatType:
5898                         {
5899                            float value;
5900                            float (*Get)(float) = (void *)prop.Get;
5901                            GetFloat(exp.member.exp, &value);
5902                            exp.constant = PrintFloat(Get ? Get(value) : value);
5903                            exp.type = constantExp;
5904                            break;
5905                         }
5906                         case doubleType:
5907                         {
5908                            double value;
5909                            double (*Get)(double);
5910                            GetDouble(exp.member.exp, &value);
5911
5912                            if(convertTo)
5913                               Get = (void *)prop.Set;
5914                            else
5915                               Get = (void *)prop.Get;
5916                            exp.constant = PrintDouble(Get ? Get(value) : value);
5917                            exp.type = constantExp;
5918                            break;
5919                         }
5920                      }
5921                   }
5922                   else
5923                   {
5924                      if(convertTo)
5925                      {
5926                         Expression value = exp.member.exp;
5927                         Type type;
5928                         if(!prop.dataType)
5929                            ProcessPropertyType(prop);
5930
5931                         type = prop.dataType;
5932                         if(!type)
5933                         {
5934                             // printf("Investigate this\n");
5935                         }
5936                         else if(_class.type == structClass)
5937                         {
5938                            switch(type.kind)
5939                            {
5940                               case classType:
5941                               {
5942                                  Class propertyClass = type._class.registered;
5943                                  if(propertyClass.type == structClass && value.type == instanceExp)
5944                                  {
5945                                     void (*Set)(void *, void *) = (void *)prop.Set;
5946                                     exp.instance = Instantiation { };
5947                                     exp.instance.data = new0 byte[_class.structSize];
5948                                     exp.instance._class = MkSpecifierName(_class.fullName);
5949                                     exp.instance.loc = exp.loc;
5950                                     exp.type = instanceExp;
5951                                     Set(exp.instance.data, value.instance.data);
5952                                     PopulateInstance(exp.instance);
5953                                  }
5954                                  break;
5955                               }
5956                               case intType:
5957                               {
5958                                  int intValue;
5959                                  void (*Set)(void *, int) = (void *)prop.Set;
5960
5961                                  exp.instance = Instantiation { };
5962                                  exp.instance.data = new0 byte[_class.structSize];
5963                                  exp.instance._class = MkSpecifierName(_class.fullName);
5964                                  exp.instance.loc = exp.loc;
5965                                  exp.type = instanceExp;
5966
5967                                  GetInt(value, &intValue);
5968
5969                                  Set(exp.instance.data, intValue);
5970                                  PopulateInstance(exp.instance);
5971                                  break;
5972                               }
5973                               case int64Type:
5974                               {
5975                                  int64 intValue;
5976                                  void (*Set)(void *, int64) = (void *)prop.Set;
5977
5978                                  exp.instance = Instantiation { };
5979                                  exp.instance.data = new0 byte[_class.structSize];
5980                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5981                                  exp.instance.loc = exp.loc;
5982                                  exp.type = instanceExp;
5983
5984                                  GetInt64(value, &intValue);
5985
5986                                  Set(exp.instance.data, intValue);
5987                                  PopulateInstance(exp.instance);
5988                                  break;
5989                               }
5990                               case intPtrType:
5991                               {
5992                                  // TOFIX:
5993                                  intptr intValue;
5994                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5995
5996                                  exp.instance = Instantiation { };
5997                                  exp.instance.data = new0 byte[_class.structSize];
5998                                  exp.instance._class = MkSpecifierName(_class.fullName);
5999                                  exp.instance.loc = exp.loc;
6000                                  exp.type = instanceExp;
6001
6002                                  GetIntPtr(value, &intValue);
6003
6004                                  Set(exp.instance.data, intValue);
6005                                  PopulateInstance(exp.instance);
6006                                  break;
6007                               }
6008                               case intSizeType:
6009                               {
6010                                  // TOFIX:
6011                                  intsize intValue;
6012                                  void (*Set)(void *, intsize) = (void *)prop.Set;
6013
6014                                  exp.instance = Instantiation { };
6015                                  exp.instance.data = new0 byte[_class.structSize];
6016                                  exp.instance._class = MkSpecifierName(_class.fullName);
6017                                  exp.instance.loc = exp.loc;
6018                                  exp.type = instanceExp;
6019
6020                                  GetIntSize(value, &intValue);
6021
6022                                  Set(exp.instance.data, intValue);
6023                                  PopulateInstance(exp.instance);
6024                                  break;
6025                               }
6026                               case floatType:
6027                               {
6028                                  float floatValue;
6029                                  void (*Set)(void *, float) = (void *)prop.Set;
6030
6031                                  exp.instance = Instantiation { };
6032                                  exp.instance.data = new0 byte[_class.structSize];
6033                                  exp.instance._class = MkSpecifierName(_class.fullName);
6034                                  exp.instance.loc = exp.loc;
6035                                  exp.type = instanceExp;
6036
6037                                  GetFloat(value, &floatValue);
6038
6039                                  Set(exp.instance.data, floatValue);
6040                                  PopulateInstance(exp.instance);
6041                                  break;
6042                               }
6043                               case doubleType:
6044                               {
6045                                  double doubleValue;
6046                                  void (*Set)(void *, double) = (void *)prop.Set;
6047
6048                                  exp.instance = Instantiation { };
6049                                  exp.instance.data = new0 byte[_class.structSize];
6050                                  exp.instance._class = MkSpecifierName(_class.fullName);
6051                                  exp.instance.loc = exp.loc;
6052                                  exp.type = instanceExp;
6053
6054                                  GetDouble(value, &doubleValue);
6055
6056                                  Set(exp.instance.data, doubleValue);
6057                                  PopulateInstance(exp.instance);
6058                                  break;
6059                               }
6060                            }
6061                         }
6062                         else if(_class.type == bitClass)
6063                         {
6064                            switch(type.kind)
6065                            {
6066                               case classType:
6067                               {
6068                                  Class propertyClass = type._class.registered;
6069                                  if(propertyClass.type == structClass && value.instance.data)
6070                                  {
6071                                     unsigned int (*Set)(void *) = (void *)prop.Set;
6072                                     unsigned int bits = Set(value.instance.data);
6073                                     exp.constant = PrintHexUInt(bits);
6074                                     exp.type = constantExp;
6075                                     break;
6076                                  }
6077                                  else if(_class.type == bitClass)
6078                                  {
6079                                     unsigned int value;
6080                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
6081                                     unsigned int bits;
6082
6083                                     GetUInt(exp.member.exp, &value);
6084                                     bits = Set(value);
6085                                     exp.constant = PrintHexUInt(bits);
6086                                     exp.type = constantExp;
6087                                  }
6088                               }
6089                            }
6090                         }
6091                      }
6092                      else
6093                      {
6094                         if(_class.type == bitClass)
6095                         {
6096                            unsigned int value;
6097                            GetUInt(exp.member.exp, &value);
6098
6099                            switch(type.kind)
6100                            {
6101                               case classType:
6102                               {
6103                                  Class _class = type._class.registered;
6104                                  if(_class.type == structClass)
6105                                  {
6106                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
6107
6108                                     exp.instance = Instantiation { };
6109                                     exp.instance.data = new0 byte[_class.structSize];
6110                                     exp.instance._class = MkSpecifierName(_class.fullName);
6111                                     exp.instance.loc = exp.loc;
6112                                     //exp.instance.fullSet = true;
6113                                     exp.type = instanceExp;
6114                                     Get(value, exp.instance.data);
6115                                     PopulateInstance(exp.instance);
6116                                  }
6117                                  else if(_class.type == bitClass)
6118                                  {
6119                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6120                                     uint64 bits = Get(value);
6121                                     exp.constant = PrintHexUInt64(bits);
6122                                     exp.type = constantExp;
6123                                  }
6124                                  break;
6125                               }
6126                            }
6127                         }
6128                         else if(_class.type == structClass)
6129                         {
6130                            byte * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6131                            switch(type.kind)
6132                            {
6133                               case classType:
6134                               {
6135                                  Class _class = type._class.registered;
6136                                  if(_class.type == structClass && value)
6137                                  {
6138                                     void (*Get)(void *, void *) = (void *)prop.Get;
6139
6140                                     exp.instance = Instantiation { };
6141                                     exp.instance.data = new0 byte[_class.structSize];
6142                                     exp.instance._class = MkSpecifierName(_class.fullName);
6143                                     exp.instance.loc = exp.loc;
6144                                     //exp.instance.fullSet = true;
6145                                     exp.type = instanceExp;
6146                                     Get(value, exp.instance.data);
6147                                     PopulateInstance(exp.instance);
6148                                  }
6149                                  break;
6150                               }
6151                            }
6152                         }
6153                         /*else
6154                         {
6155                            char * value = exp.member.exp.instance.data;
6156                            switch(type.kind)
6157                            {
6158                               case classType:
6159                               {
6160                                  Class _class = type._class.registered;
6161                                  if(_class.type == normalClass)
6162                                  {
6163                                     void *(*Get)(void *) = (void *)prop.Get;
6164
6165                                     exp.instance = Instantiation { };
6166                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6167                                     exp.type = instanceExp;
6168                                     exp.instance.data = Get(value, exp.instance.data);
6169                                  }
6170                                  break;
6171                               }
6172                            }
6173                         }
6174                         */
6175                      }
6176                   }
6177                }
6178                else
6179                {
6180                   exp.isConstant = false;
6181                }
6182             }
6183             else if(member)
6184             {
6185             }
6186          }
6187
6188          if(exp.type != ExpressionType::memberExp)
6189          {
6190             FreeExpression(memberExp);
6191             FreeIdentifier(memberID);
6192          }
6193          break;
6194       }
6195       case typeSizeExp:
6196       {
6197          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6198          FreeExpContents(exp);
6199          exp.constant = PrintUInt(ComputeTypeSize(type));
6200          exp.type = constantExp;
6201          FreeType(type);
6202          break;
6203       }
6204       case classSizeExp:
6205       {
6206          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6207          if(classSym && classSym.registered)
6208          {
6209             if(classSym.registered.fixed)
6210             {
6211                FreeSpecifier(exp._class);
6212                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6213                exp.type = constantExp;
6214             }
6215             else
6216             {
6217                char className[1024];
6218                strcpy(className, "__ecereClass_");
6219                FullClassNameCat(className, classSym.string, true);
6220                //MangleClassName(className);
6221
6222                DeclareClass(classSym, className);
6223
6224                FreeExpContents(exp);
6225                exp.type = pointerExp;
6226                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6227                exp.member.member = MkIdentifier("structSize");
6228             }
6229          }
6230          break;
6231       }
6232       case castExp:
6233       //case constantExp:
6234       {
6235          Type type;
6236          Expression e = exp;
6237          if(exp.type == castExp)
6238          {
6239             if(exp.cast.exp)
6240                ComputeExpression(exp.cast.exp);
6241             e = exp.cast.exp;
6242          }
6243          if(e && exp.expType)
6244          {
6245             /*if(exp.destType)
6246                type = exp.destType;
6247             else*/
6248                type = exp.expType;
6249             if(type.kind == classType)
6250             {
6251                Class _class = type._class.registered;
6252                if(_class && (_class.type == unitClass || _class.type == bitClass))
6253                {
6254                   if(!_class.dataType)
6255                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6256                   type = _class.dataType;
6257                }
6258             }
6259
6260             switch(type.kind)
6261             {
6262                case _BoolType:
6263                case charType:
6264                   if(type.isSigned)
6265                   {
6266                      char value = 0;
6267                      if(GetChar(e, &value))
6268                      {
6269                         FreeExpContents(exp);
6270                         exp.constant = PrintChar(value);
6271                         exp.type = constantExp;
6272                      }
6273                   }
6274                   else
6275                   {
6276                      unsigned char value = 0;
6277                      if(GetUChar(e, &value))
6278                      {
6279                         FreeExpContents(exp);
6280                         exp.constant = PrintUChar(value);
6281                         exp.type = constantExp;
6282                      }
6283                   }
6284                   break;
6285                case shortType:
6286                   if(type.isSigned)
6287                   {
6288                      short value = 0;
6289                      if(GetShort(e, &value))
6290                      {
6291                         FreeExpContents(exp);
6292                         exp.constant = PrintShort(value);
6293                         exp.type = constantExp;
6294                      }
6295                   }
6296                   else
6297                   {
6298                      unsigned short value = 0;
6299                      if(GetUShort(e, &value))
6300                      {
6301                         FreeExpContents(exp);
6302                         exp.constant = PrintUShort(value);
6303                         exp.type = constantExp;
6304                      }
6305                   }
6306                   break;
6307                case intType:
6308                   if(type.isSigned)
6309                   {
6310                      int value = 0;
6311                      if(GetInt(e, &value))
6312                      {
6313                         FreeExpContents(exp);
6314                         exp.constant = PrintInt(value);
6315                         exp.type = constantExp;
6316                      }
6317                   }
6318                   else
6319                   {
6320                      unsigned int value = 0;
6321                      if(GetUInt(e, &value))
6322                      {
6323                         FreeExpContents(exp);
6324                         exp.constant = PrintUInt(value);
6325                         exp.type = constantExp;
6326                      }
6327                   }
6328                   break;
6329                case int64Type:
6330                   if(type.isSigned)
6331                   {
6332                      int64 value = 0;
6333                      if(GetInt64(e, &value))
6334                      {
6335                         FreeExpContents(exp);
6336                         exp.constant = PrintInt64(value);
6337                         exp.type = constantExp;
6338                      }
6339                   }
6340                   else
6341                   {
6342                      uint64 value = 0;
6343                      if(GetUInt64(e, &value))
6344                      {
6345                         FreeExpContents(exp);
6346                         exp.constant = PrintUInt64(value);
6347                         exp.type = constantExp;
6348                      }
6349                   }
6350                   break;
6351                case intPtrType:
6352                   if(type.isSigned)
6353                   {
6354                      intptr value = 0;
6355                      if(GetIntPtr(e, &value))
6356                      {
6357                         FreeExpContents(exp);
6358                         exp.constant = PrintInt64((int64)value);
6359                         exp.type = constantExp;
6360                      }
6361                   }
6362                   else
6363                   {
6364                      uintptr value = 0;
6365                      if(GetUIntPtr(e, &value))
6366                      {
6367                         FreeExpContents(exp);
6368                         exp.constant = PrintUInt64((uint64)value);
6369                         exp.type = constantExp;
6370                      }
6371                   }
6372                   break;
6373                case intSizeType:
6374                   if(type.isSigned)
6375                   {
6376                      intsize value = 0;
6377                      if(GetIntSize(e, &value))
6378                      {
6379                         FreeExpContents(exp);
6380                         exp.constant = PrintInt64((int64)value);
6381                         exp.type = constantExp;
6382                      }
6383                   }
6384                   else
6385                   {
6386                      uintsize value = 0;
6387                      if(GetUIntSize(e, &value))
6388                      {
6389                         FreeExpContents(exp);
6390                         exp.constant = PrintUInt64((uint64)value);
6391                         exp.type = constantExp;
6392                      }
6393                   }
6394                   break;
6395                case floatType:
6396                {
6397                   float value = 0;
6398                   if(GetFloat(e, &value))
6399                   {
6400                      FreeExpContents(exp);
6401                      exp.constant = PrintFloat(value);
6402                      exp.type = constantExp;
6403                   }
6404                   break;
6405                }
6406                case doubleType:
6407                {
6408                   double value = 0;
6409                   if(GetDouble(e, &value))
6410                   {
6411                      FreeExpContents(exp);
6412                      exp.constant = PrintDouble(value);
6413                      exp.type = constantExp;
6414                   }
6415                   break;
6416                }
6417             }
6418          }
6419          break;
6420       }
6421       case conditionExp:
6422       {
6423          Operand op1 { };
6424          Operand op2 { };
6425          Operand op3 { };
6426
6427          if(exp.cond.exp)
6428             // Caring only about last expression for now...
6429             ComputeExpression(exp.cond.exp->last);
6430          if(exp.cond.elseExp)
6431             ComputeExpression(exp.cond.elseExp);
6432          if(exp.cond.cond)
6433             ComputeExpression(exp.cond.cond);
6434
6435          op1 = GetOperand(exp.cond.cond);
6436          if(op1.type) op1.type.refCount++;
6437          op2 = GetOperand(exp.cond.exp->last);
6438          if(op2.type) op2.type.refCount++;
6439          op3 = GetOperand(exp.cond.elseExp);
6440          if(op3.type) op3.type.refCount++;
6441
6442          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6443          if(op1.type) FreeType(op1.type);
6444          if(op2.type) FreeType(op2.type);
6445          if(op3.type) FreeType(op3.type);
6446          break;
6447       }
6448    }
6449 }
6450
6451 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6452 {
6453    bool result = true;
6454    if(destType)
6455    {
6456       OldList converts { };
6457       Conversion convert;
6458
6459       if(destType.kind == voidType)
6460          return false;
6461
6462       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6463          result = false;
6464       if(converts.count)
6465       {
6466          // for(convert = converts.last; convert; convert = convert.prev)
6467          for(convert = converts.first; convert; convert = convert.next)
6468          {
6469             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6470             if(!empty)
6471             {
6472                Expression newExp { };
6473                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6474
6475                // TODO: Check this...
6476                *newExp = *exp;
6477                newExp.prev = null;
6478                newExp.next = null;
6479                newExp.destType = null;
6480
6481                if(convert.isGet)
6482                {
6483                   // [exp].ColorRGB
6484                   exp.type = memberExp;
6485                   exp.addedThis = true;
6486                   exp.member.exp = newExp;
6487                   FreeType(exp.member.exp.expType);
6488
6489                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6490                   exp.member.exp.expType.classObjectType = objectType;
6491                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6492                   exp.member.memberType = propertyMember;
6493                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6494                   // TESTING THIS... for (int)degrees
6495                   exp.needCast = true;
6496                   if(exp.expType) exp.expType.refCount++;
6497                   ApplyAnyObjectLogic(exp.member.exp);
6498                }
6499                else
6500                {
6501
6502                   /*if(exp.isConstant)
6503                   {
6504                      // Color { ColorRGB = [exp] };
6505                      exp.type = instanceExp;
6506                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6507                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6508                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6509                   }
6510                   else*/
6511                   {
6512                      // If not constant, don't turn it yet into an instantiation
6513                      // (Go through the deep members system first)
6514                      exp.type = memberExp;
6515                      exp.addedThis = true;
6516                      exp.member.exp = newExp;
6517
6518                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6519                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6520                         newExp.expType._class.registered.type == noHeadClass)
6521                      {
6522                         newExp.byReference = true;
6523                      }
6524
6525                      FreeType(exp.member.exp.expType);
6526                      /*exp.member.exp.expType = convert.convert.dataType;
6527                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6528                      exp.member.exp.expType = null;
6529                      if(convert.convert.dataType)
6530                      {
6531                         exp.member.exp.expType = { };
6532                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6533                         exp.member.exp.expType.refCount = 1;
6534                         exp.member.exp.expType.classObjectType = objectType;
6535                         ApplyAnyObjectLogic(exp.member.exp);
6536                      }
6537
6538                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6539                      exp.member.memberType = reverseConversionMember;
6540                      exp.expType = convert.resultType ? convert.resultType :
6541                         MkClassType(convert.convert._class.fullName);
6542                      exp.needCast = true;
6543                      if(convert.resultType) convert.resultType.refCount++;
6544                   }
6545                }
6546             }
6547             else
6548             {
6549                FreeType(exp.expType);
6550                if(convert.isGet)
6551                {
6552                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6553                   if(exp.destType.casted)
6554                      exp.needCast = true;
6555                   if(exp.expType) exp.expType.refCount++;
6556                }
6557                else
6558                {
6559                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6560                   if(exp.destType.casted)
6561                      exp.needCast = true;
6562                   if(convert.resultType)
6563                      convert.resultType.refCount++;
6564                }
6565             }
6566          }
6567          if(exp.isConstant && inCompiler)
6568             ComputeExpression(exp);
6569
6570          converts.Free(FreeConvert);
6571       }
6572
6573       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6574       {
6575          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6576       }
6577       if(!result && exp.expType && exp.destType)
6578       {
6579          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6580              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6581             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6582             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6583             result = true;
6584       }
6585    }
6586    // if(result) CheckTemplateTypes(exp);
6587    return result;
6588 }
6589
6590 void CheckTemplateTypes(Expression exp)
6591 {
6592    Expression nbExp = GetNonBracketsExp(exp);
6593    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6594       (nbExp == exp || nbExp.type != castExp))
6595    {
6596       Expression newExp { };
6597       Context context;
6598       *newExp = *exp;
6599       if(exp.destType) exp.destType.refCount++;
6600       if(exp.expType)  exp.expType.refCount++;
6601       newExp.prev = null;
6602       newExp.next = null;
6603
6604       switch(exp.expType.kind)
6605       {
6606          case doubleType:
6607             if(exp.destType.classObjectType)
6608             {
6609                // We need to pass the address, just pass it along (Undo what was done above)
6610                if(exp.destType) exp.destType.refCount--;
6611                if(exp.expType)  exp.expType.refCount--;
6612                delete newExp;
6613             }
6614             else
6615             {
6616                // If we're looking for value:
6617                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6618                OldList * specs;
6619                OldList * unionDefs = MkList();
6620                OldList * statements = MkList();
6621                context = PushContext();
6622                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6623                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6624                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6625                exp.type = extensionCompoundExp;
6626                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6627                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6628                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6629                exp.compound.compound.context = context;
6630                PopContext(context);
6631             }
6632             break;
6633          default:
6634             exp.type = castExp;
6635             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6636             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6637             exp.needCast = true;
6638             break;
6639       }
6640    }
6641    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6642    {
6643       Expression newExp { };
6644       Context context;
6645       *newExp = *exp;
6646       if(exp.destType) exp.destType.refCount++;
6647       if(exp.expType)  exp.expType.refCount++;
6648       newExp.prev = null;
6649       newExp.next = null;
6650
6651       switch(exp.expType.kind)
6652       {
6653          case doubleType:
6654             if(exp.destType.classObjectType)
6655             {
6656                // We need to pass the address, just pass it along (Undo what was done above)
6657                if(exp.destType) exp.destType.refCount--;
6658                if(exp.expType)  exp.expType.refCount--;
6659                delete newExp;
6660             }
6661             else
6662             {
6663                // If we're looking for value:
6664                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6665                OldList * specs;
6666                OldList * unionDefs = MkList();
6667                OldList * statements = MkList();
6668                context = PushContext();
6669                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6670                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6671                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6672                exp.type = extensionCompoundExp;
6673                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6674                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6675                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6676                exp.compound.compound.context = context;
6677                PopContext(context);
6678             }
6679             break;
6680          case classType:
6681          {
6682             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6683             {
6684                exp.type = bracketsExp;
6685                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6686                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6687                ProcessExpressionType(exp.list->first);
6688                break;
6689             }
6690             else
6691             {
6692                exp.type = bracketsExp;
6693                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6694                exp.needTemplateCast = 2;
6695                newExp.needCast = true;
6696                newExp.needTemplateCast = 2;
6697                ProcessExpressionType(exp.list->first);
6698                break;
6699             }
6700          }
6701          default:
6702          {
6703             if(exp.expType.kind == templateType)
6704             {
6705                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6706                if(type)
6707                {
6708                   FreeType(exp.destType);
6709                   FreeType(exp.expType);
6710                   delete newExp;
6711                   break;
6712                }
6713             }
6714             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6715             {
6716                exp.type = opExp;
6717                exp.op.op = '*';
6718                exp.op.exp1 = null;
6719                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6720                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6721             }
6722             else
6723             {
6724                char typeString[1024];
6725                Declarator decl;
6726                OldList * specs = MkList();
6727                typeString[0] = '\0';
6728                PrintType(exp.expType, typeString, false, false);
6729                decl = SpecDeclFromString(typeString, specs, null);
6730
6731                exp.type = castExp;
6732                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6733                exp.cast.typeName = MkTypeName(specs, decl);
6734                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6735                exp.cast.exp.needCast = true;
6736             }
6737             break;
6738          }
6739       }
6740    }
6741 }
6742 // TODO: The Symbol tree should be reorganized by namespaces
6743 // Name Space:
6744 //    - Tree of all symbols within (stored without namespace)
6745 //    - Tree of sub-namespaces
6746
6747 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6748 {
6749    int nsLen = strlen(nameSpace);
6750    Symbol symbol;
6751    // Start at the name space prefix
6752    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6753    {
6754       char * s = symbol.string;
6755       if(!strncmp(s, nameSpace, nsLen))
6756       {
6757          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6758          int c;
6759          char * namePart;
6760          for(c = strlen(s)-1; c >= 0; c--)
6761             if(s[c] == ':')
6762                break;
6763
6764          namePart = s+c+1;
6765          if(!strcmp(namePart, name))
6766          {
6767             // TODO: Error on ambiguity
6768             return symbol;
6769          }
6770       }
6771       else
6772          break;
6773    }
6774    return null;
6775 }
6776
6777 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6778 {
6779    int c;
6780    char nameSpace[1024];
6781    const char * namePart;
6782    bool gotColon = false;
6783
6784    nameSpace[0] = '\0';
6785    for(c = strlen(name)-1; c >= 0; c--)
6786       if(name[c] == ':')
6787       {
6788          gotColon = true;
6789          break;
6790       }
6791
6792    namePart = name+c+1;
6793    while(c >= 0 && name[c] == ':') c--;
6794    if(c >= 0)
6795    {
6796       // Try an exact match first
6797       Symbol symbol = (Symbol)tree.FindString(name);
6798       if(symbol)
6799          return symbol;
6800
6801       // Namespace specified
6802       memcpy(nameSpace, name, c + 1);
6803       nameSpace[c+1] = 0;
6804
6805       return ScanWithNameSpace(tree, nameSpace, namePart);
6806    }
6807    else if(gotColon)
6808    {
6809       // Looking for a global symbol, e.g. ::Sleep()
6810       Symbol symbol = (Symbol)tree.FindString(namePart);
6811       return symbol;
6812    }
6813    else
6814    {
6815       // Name only (no namespace specified)
6816       Symbol symbol = (Symbol)tree.FindString(namePart);
6817       if(symbol)
6818          return symbol;
6819       return ScanWithNameSpace(tree, "", namePart);
6820    }
6821    return null;
6822 }
6823
6824 static void ProcessDeclaration(Declaration decl);
6825
6826 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6827 {
6828 #ifdef _DEBUG
6829    //Time startTime = GetTime();
6830 #endif
6831    // Optimize this later? Do this before/less?
6832    Context ctx;
6833    Symbol symbol = null;
6834    // First, check if the identifier is declared inside the function
6835    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6836
6837    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6838    {
6839       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6840       {
6841          symbol = null;
6842          if(thisNameSpace)
6843          {
6844             char curName[1024];
6845             strcpy(curName, thisNameSpace);
6846             strcat(curName, "::");
6847             strcat(curName, name);
6848             // Try to resolve in current namespace first
6849             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6850          }
6851          if(!symbol)
6852             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6853       }
6854       else
6855          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6856
6857       if(symbol || ctx == endContext) break;
6858    }
6859    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6860    {
6861       if(symbol.pointerExternal.type == functionExternal)
6862       {
6863          FunctionDefinition function = symbol.pointerExternal.function;
6864
6865          // Modified this recently...
6866          Context tmpContext = curContext;
6867          curContext = null;
6868          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6869          curContext = tmpContext;
6870
6871          symbol.pointerExternal.symbol = symbol;
6872
6873          // TESTING THIS:
6874          DeclareType(symbol.type, true, true);
6875
6876          ast->Insert(curExternal.prev, symbol.pointerExternal);
6877
6878          symbol.id = curExternal.symbol.idCode;
6879
6880       }
6881       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6882       {
6883          ast->Move(symbol.pointerExternal, curExternal.prev);
6884          symbol.id = curExternal.symbol.idCode;
6885       }
6886    }
6887 #ifdef _DEBUG
6888    //findSymbolTotalTime += GetTime() - startTime;
6889 #endif
6890    return symbol;
6891 }
6892
6893 static void GetTypeSpecs(Type type, OldList * specs)
6894 {
6895    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6896    switch(type.kind)
6897    {
6898       case classType:
6899       {
6900          if(type._class.registered)
6901          {
6902             if(!type._class.registered.dataType)
6903                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6904             GetTypeSpecs(type._class.registered.dataType, specs);
6905          }
6906          break;
6907       }
6908       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6909       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6910       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6911       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6912       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6913       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6914       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6915       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6916       case intType:
6917       default:
6918          ListAdd(specs, MkSpecifier(INT)); break;
6919    }
6920 }
6921
6922 static void PrintArraySize(Type arrayType, char * string)
6923 {
6924    char size[256];
6925    size[0] = '\0';
6926    strcat(size, "[");
6927    if(arrayType.enumClass)
6928       strcat(size, arrayType.enumClass.string);
6929    else if(arrayType.arraySizeExp)
6930       PrintExpression(arrayType.arraySizeExp, size);
6931    strcat(size, "]");
6932    strcat(string, size);
6933 }
6934
6935 // WARNING : This function expects a null terminated string since it recursively concatenate...
6936 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6937 {
6938    if(type)
6939    {
6940       if(printConst && type.constant)
6941          strcat(string, "const ");
6942       switch(type.kind)
6943       {
6944          case classType:
6945          {
6946             Symbol c = type._class;
6947             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6948             //       look into merging with thisclass ?
6949             if(type.classObjectType == typedObject)
6950                strcat(string, "typed_object");
6951             else if(type.classObjectType == anyObject)
6952                strcat(string, "any_object");
6953             else
6954             {
6955                if(c && c.string)
6956                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6957             }
6958             if(type.byReference)
6959                strcat(string, " &");
6960             break;
6961          }
6962          case voidType: strcat(string, "void"); break;
6963          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6964          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6965          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6966          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6967          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6968          case _BoolType: strcat(string, "_Bool"); break;
6969          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6970          case floatType: strcat(string, "float"); break;
6971          case doubleType: strcat(string, "double"); break;
6972          case structType:
6973             if(type.enumName)
6974             {
6975                strcat(string, "struct ");
6976                strcat(string, type.enumName);
6977             }
6978             else if(type.typeName)
6979                strcat(string, type.typeName);
6980             else
6981             {
6982                Type member;
6983                strcat(string, "struct { ");
6984                for(member = type.members.first; member; member = member.next)
6985                {
6986                   PrintType(member, string, true, fullName);
6987                   strcat(string,"; ");
6988                }
6989                strcat(string,"}");
6990             }
6991             break;
6992          case unionType:
6993             if(type.enumName)
6994             {
6995                strcat(string, "union ");
6996                strcat(string, type.enumName);
6997             }
6998             else if(type.typeName)
6999                strcat(string, type.typeName);
7000             else
7001             {
7002                strcat(string, "union ");
7003                strcat(string,"(unnamed)");
7004             }
7005             break;
7006          case enumType:
7007             if(type.enumName)
7008             {
7009                strcat(string, "enum ");
7010                strcat(string, type.enumName);
7011             }
7012             else if(type.typeName)
7013                strcat(string, type.typeName);
7014             else
7015                strcat(string, "int"); // "enum");
7016             break;
7017          case ellipsisType:
7018             strcat(string, "...");
7019             break;
7020          case subClassType:
7021             strcat(string, "subclass(");
7022             strcat(string, type._class ? type._class.string : "int");
7023             strcat(string, ")");
7024             break;
7025          case templateType:
7026             strcat(string, type.templateParameter.identifier.string);
7027             break;
7028          case thisClassType:
7029             strcat(string, "thisclass");
7030             break;
7031          case vaListType:
7032             strcat(string, "__builtin_va_list");
7033             break;
7034       }
7035    }
7036 }
7037
7038 static void PrintName(Type type, char * string, bool fullName)
7039 {
7040    if(type.name && type.name[0])
7041    {
7042       if(fullName)
7043          strcat(string, type.name);
7044       else
7045       {
7046          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
7047          if(name) name += 2; else name = type.name;
7048          strcat(string, name);
7049       }
7050    }
7051 }
7052
7053 static void PrintAttribs(Type type, char * string)
7054 {
7055    if(type)
7056    {
7057       if(type.dllExport)   strcat(string, "dllexport ");
7058       if(type.attrStdcall) strcat(string, "stdcall ");
7059    }
7060 }
7061
7062 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
7063 {
7064    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7065    {
7066       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7067          PrintAttribs(type, string);
7068       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7069          strcat(string, " const");
7070       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7071       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7072          strcat(string, " (");
7073       if(type.kind == pointerType)
7074       {
7075          if(type.type.kind == functionType || type.type.kind == methodType)
7076             PrintAttribs(type.type, string);
7077       }
7078       if(type.kind == pointerType)
7079       {
7080          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7081             strcat(string, "*");
7082          else
7083             strcat(string, " *");
7084       }
7085       if(printConst && type.constant && type.kind == pointerType)
7086          strcat(string, " const");
7087    }
7088    else
7089       PrintTypeSpecs(type, string, fullName, printConst);
7090 }
7091
7092 static void PostPrintType(Type type, char * string, bool fullName)
7093 {
7094    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7095       strcat(string, ")");
7096    if(type.kind == arrayType)
7097       PrintArraySize(type, string);
7098    else if(type.kind == functionType)
7099    {
7100       Type param;
7101       strcat(string, "(");
7102       for(param = type.params.first; param; param = param.next)
7103       {
7104          PrintType(param, string, true, fullName);
7105          if(param.next) strcat(string, ", ");
7106       }
7107       strcat(string, ")");
7108    }
7109    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7110       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7111 }
7112
7113 // *****
7114 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7115 // *****
7116 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7117 {
7118    PrePrintType(type, string, fullName, null, printConst);
7119
7120    if(type.thisClass || (printName && type.name && type.name[0]))
7121       strcat(string, " ");
7122    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7123    {
7124       Symbol _class = type.thisClass;
7125       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7126       {
7127          if(type.classObjectType == classPointer)
7128             strcat(string, "class");
7129          else
7130             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7131       }
7132       else if(_class && _class.string)
7133       {
7134          String s = _class.string;
7135          if(fullName)
7136             strcat(string, s);
7137          else
7138          {
7139             char * name = RSearchString(s, "::", strlen(s), true, false);
7140             if(name) name += 2; else name = s;
7141             strcat(string, name);
7142          }
7143       }
7144       strcat(string, "::");
7145    }
7146
7147    if(printName && type.name)
7148       PrintName(type, string, fullName);
7149    PostPrintType(type, string, fullName);
7150    if(type.bitFieldCount)
7151    {
7152       char count[100];
7153       sprintf(count, ":%d", type.bitFieldCount);
7154       strcat(string, count);
7155    }
7156 }
7157
7158 void PrintType(Type type, char * string, bool printName, bool fullName)
7159 {
7160    _PrintType(type, string, printName, fullName, true);
7161 }
7162
7163 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7164 {
7165    _PrintType(type, string, printName, fullName, false);
7166 }
7167
7168 static Type FindMember(Type type, char * string)
7169 {
7170    Type memberType;
7171    for(memberType = type.members.first; memberType; memberType = memberType.next)
7172    {
7173       if(!memberType.name)
7174       {
7175          Type subType = FindMember(memberType, string);
7176          if(subType)
7177             return subType;
7178       }
7179       else if(!strcmp(memberType.name, string))
7180          return memberType;
7181    }
7182    return null;
7183 }
7184
7185 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7186 {
7187    Type memberType;
7188    for(memberType = type.members.first; memberType; memberType = memberType.next)
7189    {
7190       if(!memberType.name)
7191       {
7192          Type subType = FindMember(memberType, string);
7193          if(subType)
7194          {
7195             *offset += memberType.offset;
7196             return subType;
7197          }
7198       }
7199       else if(!strcmp(memberType.name, string))
7200       {
7201          *offset += memberType.offset;
7202          return memberType;
7203       }
7204    }
7205    return null;
7206 }
7207
7208 public bool GetParseError() { return parseError; }
7209
7210 Expression ParseExpressionString(char * expression)
7211 {
7212    parseError = false;
7213
7214    fileInput = TempFile { };
7215    fileInput.Write(expression, 1, strlen(expression));
7216    fileInput.Seek(0, start);
7217
7218    echoOn = false;
7219    parsedExpression = null;
7220    resetScanner();
7221    expression_yyparse();
7222    delete fileInput;
7223
7224    return parsedExpression;
7225 }
7226
7227 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7228 {
7229    Identifier id = exp.identifier;
7230    Method method = null;
7231    Property prop = null;
7232    DataMember member = null;
7233    ClassProperty classProp = null;
7234
7235    if(_class && _class.type == enumClass)
7236    {
7237       NamedLink64 value = null;
7238       Class enumClass = eSystem_FindClass(privateModule, "enum");
7239       if(enumClass)
7240       {
7241          Class baseClass;
7242          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7243          {
7244             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7245             for(value = e.values.first; value; value = value.next)
7246             {
7247                if(!strcmp(value.name, id.string))
7248                   break;
7249             }
7250             if(value)
7251             {
7252                char constant[256];
7253
7254                FreeExpContents(exp);
7255
7256                exp.type = constantExp;
7257                exp.isConstant = true;
7258                if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "char") || !strcmp(baseClass.dataTypeString, "short"))
7259                   sprintf(constant, FORMAT64D, value.data);
7260                else
7261                   sprintf(constant, FORMAT64HEX, value.data);
7262                exp.constant = CopyString(constant);
7263                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7264                exp.expType = MkClassType(baseClass.fullName);
7265                break;
7266             }
7267          }
7268       }
7269       if(value)
7270          return true;
7271    }
7272    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7273    {
7274       ProcessMethodType(method);
7275       exp.expType = Type
7276       {
7277          refCount = 1;
7278          kind = methodType;
7279          method = method;
7280          // Crash here?
7281          // TOCHECK: Put it back to what it was...
7282          // methodClass = _class;
7283          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7284       };
7285       //id._class = null;
7286       return true;
7287    }
7288    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7289    {
7290       if(!prop.dataType)
7291          ProcessPropertyType(prop);
7292       exp.expType = prop.dataType;
7293       if(prop.dataType) prop.dataType.refCount++;
7294       return true;
7295    }
7296    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7297    {
7298       if(!member.dataType)
7299          member.dataType = ProcessTypeString(member.dataTypeString, false);
7300       exp.expType = member.dataType;
7301       if(member.dataType) member.dataType.refCount++;
7302       return true;
7303    }
7304    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7305    {
7306       if(!classProp.dataType)
7307          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7308
7309       if(classProp.constant)
7310       {
7311          FreeExpContents(exp);
7312
7313          exp.isConstant = true;
7314          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7315          {
7316             //char constant[256];
7317             exp.type = stringExp;
7318             exp.constant = QMkString((char *)classProp.Get(_class));
7319          }
7320          else
7321          {
7322             char constant[256];
7323             exp.type = constantExp;
7324             sprintf(constant, "%d", (int)classProp.Get(_class));
7325             exp.constant = CopyString(constant);
7326          }
7327       }
7328       else
7329       {
7330          // TO IMPLEMENT...
7331       }
7332
7333       exp.expType = classProp.dataType;
7334       if(classProp.dataType) classProp.dataType.refCount++;
7335       return true;
7336    }
7337    return false;
7338 }
7339
7340 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7341 {
7342    BinaryTree * tree = &nameSpace.functions;
7343    GlobalData data = (GlobalData)tree->FindString(name);
7344    NameSpace * child;
7345    if(!data)
7346    {
7347       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7348       {
7349          data = ScanGlobalData(child, name);
7350          if(data)
7351             break;
7352       }
7353    }
7354    return data;
7355 }
7356
7357 static GlobalData FindGlobalData(char * name)
7358 {
7359    int start = 0, c;
7360    NameSpace * nameSpace;
7361    nameSpace = globalData;
7362    for(c = 0; name[c]; c++)
7363    {
7364       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7365       {
7366          NameSpace * newSpace;
7367          char * spaceName = new char[c - start + 1];
7368          strncpy(spaceName, name + start, c - start);
7369          spaceName[c-start] = '\0';
7370          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7371          delete spaceName;
7372          if(!newSpace)
7373             return null;
7374          nameSpace = newSpace;
7375          if(name[c] == ':') c++;
7376          start = c+1;
7377       }
7378    }
7379    if(c - start)
7380    {
7381       return ScanGlobalData(nameSpace, name + start);
7382    }
7383    return null;
7384 }
7385
7386 static int definedExpStackPos;
7387 static void * definedExpStack[512];
7388
7389 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7390 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7391 {
7392    Expression prev = checkedExp.prev, next = checkedExp.next;
7393
7394    FreeExpContents(checkedExp);
7395    FreeType(checkedExp.expType);
7396    FreeType(checkedExp.destType);
7397
7398    *checkedExp = *newExp;
7399
7400    delete newExp;
7401
7402    checkedExp.prev = prev;
7403    checkedExp.next = next;
7404 }
7405
7406 void ApplyAnyObjectLogic(Expression e)
7407 {
7408    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7409 #ifdef _DEBUG
7410    char debugExpString[4096];
7411    debugExpString[0] = '\0';
7412    PrintExpression(e, debugExpString);
7413 #endif
7414
7415    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7416    {
7417       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7418       //ellipsisDestType = destType;
7419       if(e && e.expType)
7420       {
7421          Type type = e.expType;
7422          Class _class = null;
7423          //Type destType = e.destType;
7424
7425          if(type.kind == classType && type._class && type._class.registered)
7426          {
7427             _class = type._class.registered;
7428          }
7429          else if(type.kind == subClassType)
7430          {
7431             _class = FindClass("ecere::com::Class").registered;
7432          }
7433          else
7434          {
7435             char string[1024] = "";
7436             Symbol classSym;
7437
7438             PrintTypeNoConst(type, string, false, true);
7439             classSym = FindClass(string);
7440             if(classSym) _class = classSym.registered;
7441          }
7442
7443          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...
7444             (!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))) ||
7445             destType.byReference)))
7446          {
7447             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7448             {
7449                Expression checkedExp = e, newExp;
7450
7451                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7452                {
7453                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7454                   {
7455                      if(checkedExp.type == extensionCompoundExp)
7456                      {
7457                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7458                      }
7459                      else
7460                         checkedExp = checkedExp.list->last;
7461                   }
7462                   else if(checkedExp.type == castExp)
7463                      checkedExp = checkedExp.cast.exp;
7464                }
7465
7466                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7467                {
7468                   newExp = checkedExp.op.exp2;
7469                   checkedExp.op.exp2 = null;
7470                   FreeExpContents(checkedExp);
7471
7472                   if(e.expType && e.expType.passAsTemplate)
7473                   {
7474                      char size[100];
7475                      ComputeTypeSize(e.expType);
7476                      sprintf(size, "%d", e.expType.size);
7477                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7478                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7479                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7480                   }
7481
7482                   ReplaceExpContents(checkedExp, newExp);
7483                   e.byReference = true;
7484                }
7485                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7486                {
7487                   Expression checkedExp; //, newExp;
7488
7489                   {
7490                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7491                      bool hasAddress =
7492                         e.type == identifierExp ||
7493                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7494                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7495                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7496                         e.type == indexExp;
7497
7498                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7499                      {
7500                         Context context = PushContext();
7501                         Declarator decl;
7502                         OldList * specs = MkList();
7503                         char typeString[1024];
7504                         Expression newExp { };
7505
7506                         typeString[0] = '\0';
7507                         *newExp = *e;
7508
7509                         //if(e.destType) e.destType.refCount++;
7510                         // if(exp.expType) exp.expType.refCount++;
7511                         newExp.prev = null;
7512                         newExp.next = null;
7513                         newExp.expType = null;
7514
7515                         PrintTypeNoConst(e.expType, typeString, false, true);
7516                         decl = SpecDeclFromString(typeString, specs, null);
7517                         newExp.destType = ProcessType(specs, decl);
7518
7519                         curContext = context;
7520
7521                         // We need a current compound for this
7522                         if(curCompound)
7523                         {
7524                            char name[100];
7525                            OldList * stmts = MkList();
7526                            e.type = extensionCompoundExp;
7527                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7528                            if(!curCompound.compound.declarations)
7529                               curCompound.compound.declarations = MkList();
7530                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7531                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7532                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7533                            e.compound = MkCompoundStmt(null, stmts);
7534                         }
7535                         else
7536                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7537
7538                         /*
7539                         e.compound = MkCompoundStmt(
7540                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7541                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7542
7543                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7544                         */
7545
7546                         {
7547                            Type type = e.destType;
7548                            e.destType = { };
7549                            CopyTypeInto(e.destType, type);
7550                            e.destType.refCount = 1;
7551                            e.destType.classObjectType = none;
7552                            FreeType(type);
7553                         }
7554
7555                         e.compound.compound.context = context;
7556                         PopContext(context);
7557                         curContext = context.parent;
7558                      }
7559                   }
7560
7561                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7562                   checkedExp = e;
7563                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7564                   {
7565                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7566                      {
7567                         if(checkedExp.type == extensionCompoundExp)
7568                         {
7569                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7570                         }
7571                         else
7572                            checkedExp = checkedExp.list->last;
7573                      }
7574                      else if(checkedExp.type == castExp)
7575                         checkedExp = checkedExp.cast.exp;
7576                   }
7577                   {
7578                      Expression operand { };
7579                      operand = *checkedExp;
7580                      checkedExp.Clear();
7581                      checkedExp.destType = ProcessTypeString("void *", false);
7582                      checkedExp.expType = checkedExp.destType;
7583                      checkedExp.destType.refCount++;
7584
7585                      checkedExp.type = opExp;
7586                      checkedExp.op.op = '&';
7587                      checkedExp.op.exp1 = null;
7588                      checkedExp.op.exp2 = operand;
7589
7590                      //newExp = MkExpOp(null, '&', checkedExp);
7591                   }
7592                   //ReplaceExpContents(checkedExp, newExp);
7593                }
7594             }
7595          }
7596       }
7597    }
7598    {
7599       // If expression type is a simple class, make it an address
7600       // FixReference(e, true);
7601    }
7602 //#if 0
7603    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7604       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7605          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7606    {
7607       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"))
7608       {
7609          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7610       }
7611       else
7612       {
7613          Expression thisExp { };
7614
7615          *thisExp = *e;
7616          thisExp.prev = null;
7617          thisExp.next = null;
7618          e.Clear();
7619
7620          e.type = bracketsExp;
7621          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7622          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7623             ((Expression)e.list->first).byReference = true;
7624
7625          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7626          {
7627             e.expType = thisExp.expType;
7628             e.expType.refCount++;
7629          }
7630          else*/
7631          {
7632             e.expType = { };
7633             CopyTypeInto(e.expType, thisExp.expType);
7634             e.expType.byReference = false;
7635             e.expType.refCount = 1;
7636
7637             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7638                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7639             {
7640                e.expType.classObjectType = none;
7641             }
7642          }
7643       }
7644    }
7645 // TOFIX: Try this for a nice IDE crash!
7646 //#endif
7647    // The other way around
7648    else
7649 //#endif
7650    if(destType && e.expType &&
7651          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7652          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7653          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7654    {
7655       if(destType.kind == ellipsisType)
7656       {
7657          Compiler_Error($"Unspecified type\n");
7658       }
7659       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7660       {
7661          bool byReference = e.expType.byReference;
7662          Expression thisExp { };
7663          Declarator decl;
7664          OldList * specs = MkList();
7665          char typeString[1024]; // Watch buffer overruns
7666          Type type;
7667          ClassObjectType backupClassObjectType;
7668          bool backupByReference;
7669
7670          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7671             type = e.expType;
7672          else
7673             type = destType;
7674
7675          backupClassObjectType = type.classObjectType;
7676          backupByReference = type.byReference;
7677
7678          type.classObjectType = none;
7679          type.byReference = false;
7680
7681          typeString[0] = '\0';
7682          PrintType(type, typeString, false, true);
7683          decl = SpecDeclFromString(typeString, specs, null);
7684
7685          type.classObjectType = backupClassObjectType;
7686          type.byReference = backupByReference;
7687
7688          *thisExp = *e;
7689          thisExp.prev = null;
7690          thisExp.next = null;
7691          e.Clear();
7692
7693          if( ( type.kind == classType && type._class && type._class.registered &&
7694                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7695                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7696              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7697              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7698          {
7699             e.type = opExp;
7700             e.op.op = '*';
7701             e.op.exp1 = null;
7702             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7703
7704             e.expType = { };
7705             CopyTypeInto(e.expType, type);
7706             e.expType.byReference = false;
7707             e.expType.refCount = 1;
7708          }
7709          else
7710          {
7711             e.type = castExp;
7712             e.cast.typeName = MkTypeName(specs, decl);
7713             e.cast.exp = thisExp;
7714             e.byReference = true;
7715             e.expType = type;
7716             type.refCount++;
7717          }
7718          e.destType = destType;
7719          destType.refCount++;
7720       }
7721    }
7722 }
7723
7724 void ApplyLocation(Expression exp, Location loc)
7725 {
7726    exp.loc = loc;
7727    switch(exp.type)
7728    {
7729       case opExp:
7730          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7731          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7732          break;
7733       case bracketsExp:
7734          if(exp.list)
7735          {
7736             Expression e;
7737             for(e = exp.list->first; e; e = e.next)
7738                ApplyLocation(e, loc);
7739          }
7740          break;
7741       case indexExp:
7742          if(exp.index.index)
7743          {
7744             Expression e;
7745             for(e = exp.index.index->first; e; e = e.next)
7746                ApplyLocation(e, loc);
7747          }
7748          if(exp.index.exp)
7749             ApplyLocation(exp.index.exp, loc);
7750          break;
7751       case callExp:
7752          if(exp.call.arguments)
7753          {
7754             Expression arg;
7755             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7756                ApplyLocation(arg, loc);
7757          }
7758          if(exp.call.exp)
7759             ApplyLocation(exp.call.exp, loc);
7760          break;
7761       case memberExp:
7762       case pointerExp:
7763          if(exp.member.exp)
7764             ApplyLocation(exp.member.exp, loc);
7765          break;
7766       case castExp:
7767          if(exp.cast.exp)
7768             ApplyLocation(exp.cast.exp, loc);
7769          break;
7770       case conditionExp:
7771          if(exp.cond.exp)
7772          {
7773             Expression e;
7774             for(e = exp.cond.exp->first; e; e = e.next)
7775                ApplyLocation(e, loc);
7776          }
7777          if(exp.cond.cond)
7778             ApplyLocation(exp.cond.cond, loc);
7779          if(exp.cond.elseExp)
7780             ApplyLocation(exp.cond.elseExp, loc);
7781          break;
7782       case vaArgExp:
7783          if(exp.vaArg.exp)
7784             ApplyLocation(exp.vaArg.exp, loc);
7785          break;
7786       default:
7787          break;
7788    }
7789 }
7790
7791 void ProcessExpressionType(Expression exp)
7792 {
7793    bool unresolved = false;
7794    Location oldyylloc = yylloc;
7795    bool notByReference = false;
7796 #ifdef _DEBUG
7797    char debugExpString[4096];
7798    debugExpString[0] = '\0';
7799    PrintExpression(exp, debugExpString);
7800 #endif
7801    if(!exp || exp.expType)
7802       return;
7803
7804    //eSystem_Logf("%s\n", expString);
7805
7806    // Testing this here
7807    yylloc = exp.loc;
7808    switch(exp.type)
7809    {
7810       case identifierExp:
7811       {
7812          Identifier id = exp.identifier;
7813          if(!id || !topContext) return;
7814
7815          // DOING THIS LATER NOW...
7816          if(id._class && id._class.name)
7817          {
7818             id.classSym = id._class.symbol; // FindClass(id._class.name);
7819             /* TODO: Name Space Fix ups
7820             if(!id.classSym)
7821                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7822             */
7823          }
7824
7825          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7826          {
7827             exp.expType = ProcessTypeString("Module", true);
7828             break;
7829          }
7830          else */
7831          if(!strcmp(id.string, "__runtimePlatform"))
7832          {
7833             exp.expType = ProcessTypeString("ecere::com::Platform", true);
7834             break;
7835          }
7836          else if(strstr(id.string, "__ecereClass") == id.string)
7837          {
7838             exp.expType = ProcessTypeString("ecere::com::Class", true);
7839             break;
7840          }
7841          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7842          {
7843             // Added this here as well
7844             ReplaceClassMembers(exp, thisClass);
7845             if(exp.type != identifierExp)
7846             {
7847                ProcessExpressionType(exp);
7848                break;
7849             }
7850
7851             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7852                break;
7853          }
7854          else
7855          {
7856             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7857             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7858             if(!symbol/* && exp.destType*/)
7859             {
7860                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7861                   break;
7862                else
7863                {
7864                   if(thisClass)
7865                   {
7866                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7867                      if(exp.type != identifierExp)
7868                      {
7869                         ProcessExpressionType(exp);
7870                         break;
7871                      }
7872                   }
7873                   // Static methods called from inside the _class
7874                   else if(currentClass && !id._class)
7875                   {
7876                      if(ResolveIdWithClass(exp, currentClass, true))
7877                         break;
7878                   }
7879                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7880                }
7881             }
7882
7883             // If we manage to resolve this symbol
7884             if(symbol)
7885             {
7886                Type type = symbol.type;
7887                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7888
7889                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7890                {
7891                   Context context = SetupTemplatesContext(_class);
7892                   type = ReplaceThisClassType(_class);
7893                   FinishTemplatesContext(context);
7894                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7895                }
7896
7897                FreeSpecifier(id._class);
7898                id._class = null;
7899                delete id.string;
7900                id.string = CopyString(symbol.string);
7901
7902                id.classSym = null;
7903                exp.expType = type;
7904                if(type)
7905                   type.refCount++;
7906
7907                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7908                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7909                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7910                   // Add missing cases here... enum Classes...
7911                   exp.isConstant = true;
7912
7913                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7914                if(symbol.isParam || !strcmp(id.string, "this"))
7915                {
7916                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7917                      exp.byReference = true;
7918
7919                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7920                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7921                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7922                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7923                   {
7924                      Identifier id = exp.identifier;
7925                      exp.type = bracketsExp;
7926                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7927                   }*/
7928                }
7929
7930                if(symbol.isIterator)
7931                {
7932                   if(symbol.isIterator == 3)
7933                   {
7934                      exp.type = bracketsExp;
7935                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7936                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7937                      exp.expType = null;
7938                      ProcessExpressionType(exp);
7939                   }
7940                   else if(symbol.isIterator != 4)
7941                   {
7942                      exp.type = memberExp;
7943                      exp.member.exp = MkExpIdentifier(exp.identifier);
7944                      exp.member.exp.expType = exp.expType;
7945                      /*if(symbol.isIterator == 6)
7946                         exp.member.member = MkIdentifier("key");
7947                      else*/
7948                         exp.member.member = MkIdentifier("data");
7949                      exp.expType = null;
7950                      ProcessExpressionType(exp);
7951                   }
7952                }
7953                break;
7954             }
7955             else
7956             {
7957                DefinedExpression definedExp = null;
7958                if(thisNameSpace && !(id._class && !id._class.name))
7959                {
7960                   char name[1024];
7961                   strcpy(name, thisNameSpace);
7962                   strcat(name, "::");
7963                   strcat(name, id.string);
7964                   definedExp = eSystem_FindDefine(privateModule, name);
7965                }
7966                if(!definedExp)
7967                   definedExp = eSystem_FindDefine(privateModule, id.string);
7968                if(definedExp)
7969                {
7970                   int c;
7971                   for(c = 0; c<definedExpStackPos; c++)
7972                      if(definedExpStack[c] == definedExp)
7973                         break;
7974                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7975                   {
7976                      Location backupYylloc = yylloc;
7977                      File backInput = fileInput;
7978                      definedExpStack[definedExpStackPos++] = definedExp;
7979
7980                      fileInput = TempFile { };
7981                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7982                      fileInput.Seek(0, start);
7983
7984                      echoOn = false;
7985                      parsedExpression = null;
7986                      resetScanner();
7987                      expression_yyparse();
7988                      delete fileInput;
7989                      if(backInput)
7990                         fileInput = backInput;
7991
7992                      yylloc = backupYylloc;
7993
7994                      if(parsedExpression)
7995                      {
7996                         FreeIdentifier(id);
7997                         exp.type = bracketsExp;
7998                         exp.list = MkListOne(parsedExpression);
7999                         ApplyLocation(parsedExpression, yylloc);
8000                         ProcessExpressionType(exp);
8001                         definedExpStackPos--;
8002                         return;
8003                      }
8004                      definedExpStackPos--;
8005                   }
8006                   else
8007                   {
8008                      if(inCompiler)
8009                      {
8010                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
8011                      }
8012                   }
8013                }
8014                else
8015                {
8016                   GlobalData data = null;
8017                   if(thisNameSpace && !(id._class && !id._class.name))
8018                   {
8019                      char name[1024];
8020                      strcpy(name, thisNameSpace);
8021                      strcat(name, "::");
8022                      strcat(name, id.string);
8023                      data = FindGlobalData(name);
8024                   }
8025                   if(!data)
8026                      data = FindGlobalData(id.string);
8027                   if(data)
8028                   {
8029                      DeclareGlobalData(data);
8030                      exp.expType = data.dataType;
8031                      if(data.dataType) data.dataType.refCount++;
8032
8033                      delete id.string;
8034                      id.string = CopyString(data.fullName);
8035                      FreeSpecifier(id._class);
8036                      id._class = null;
8037
8038                      break;
8039                   }
8040                   else
8041                   {
8042                      GlobalFunction function = null;
8043                      if(thisNameSpace && !(id._class && !id._class.name))
8044                      {
8045                         char name[1024];
8046                         strcpy(name, thisNameSpace);
8047                         strcat(name, "::");
8048                         strcat(name, id.string);
8049                         function = eSystem_FindFunction(privateModule, name);
8050                      }
8051                      if(!function)
8052                         function = eSystem_FindFunction(privateModule, id.string);
8053                      if(function)
8054                      {
8055                         char name[1024];
8056                         delete id.string;
8057                         id.string = CopyString(function.name);
8058                         name[0] = 0;
8059
8060                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8061                            strcpy(name, "__ecereFunction_");
8062                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8063                         if(DeclareFunction(function, name))
8064                         {
8065                            delete id.string;
8066                            id.string = CopyString(name);
8067                         }
8068                         exp.expType = function.dataType;
8069                         if(function.dataType) function.dataType.refCount++;
8070
8071                         FreeSpecifier(id._class);
8072                         id._class = null;
8073
8074                         break;
8075                      }
8076                   }
8077                }
8078             }
8079          }
8080          unresolved = true;
8081          break;
8082       }
8083       case instanceExp:
8084       {
8085          // Class _class;
8086          // Symbol classSym;
8087
8088          if(!exp.instance._class)
8089          {
8090             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8091             {
8092                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8093             }
8094          }
8095
8096          //classSym = FindClass(exp.instance._class.fullName);
8097          //_class = classSym ? classSym.registered : null;
8098
8099          ProcessInstantiationType(exp.instance);
8100
8101          exp.isConstant = exp.instance.isConstant;
8102
8103          /*
8104          if(_class.type == unitClass && _class.base.type != systemClass)
8105          {
8106             {
8107                Type destType = exp.destType;
8108
8109                exp.destType = MkClassType(_class.base.fullName);
8110                exp.expType = MkClassType(_class.fullName);
8111                CheckExpressionType(exp, exp.destType, true);
8112
8113                exp.destType = destType;
8114             }
8115             exp.expType = MkClassType(_class.fullName);
8116          }
8117          else*/
8118          if(exp.instance._class)
8119          {
8120             exp.expType = MkClassType(exp.instance._class.name);
8121             /*if(exp.expType._class && exp.expType._class.registered &&
8122                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8123                exp.expType.byReference = true;*/
8124          }
8125          break;
8126       }
8127       case constantExp:
8128       {
8129          if(!exp.expType)
8130          {
8131             char * constant = exp.constant;
8132             Type type
8133             {
8134                refCount = 1;
8135                constant = true;
8136             };
8137             exp.expType = type;
8138
8139             if(constant[0] == '\'')
8140             {
8141                if((int)((byte *)constant)[1] > 127)
8142                {
8143                   int nb;
8144                   unichar ch = UTF8GetChar(constant + 1, &nb);
8145                   if(nb < 2) ch = constant[1];
8146                   delete constant;
8147                   exp.constant = PrintUInt(ch);
8148                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8149                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8150                   type._class = FindClass("unichar");
8151
8152                   type.isSigned = false;
8153                }
8154                else
8155                {
8156                   type.kind = charType;
8157                   type.isSigned = true;
8158                }
8159             }
8160             else
8161             {
8162                char * dot = strchr(constant, '.');
8163                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8164                char * exponent;
8165                if(isHex)
8166                {
8167                   exponent = strchr(constant, 'p');
8168                   if(!exponent) exponent = strchr(constant, 'P');
8169                }
8170                else
8171                {
8172                   exponent = strchr(constant, 'e');
8173                   if(!exponent) exponent = strchr(constant, 'E');
8174                }
8175
8176                if(dot || exponent)
8177                {
8178                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8179                      type.kind = floatType;
8180                   else
8181                      type.kind = doubleType;
8182                   type.isSigned = true;
8183                }
8184                else
8185                {
8186                   bool isSigned = constant[0] == '-';
8187                   char * endP = null;
8188                   int64 i64 = strtoll(constant, &endP, 0);
8189                   uint64 ui64 = strtoull(constant, &endP, 0);
8190                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
8191                   if(isSigned)
8192                   {
8193                      if(i64 < MININT)
8194                         is64Bit = true;
8195                   }
8196                   else
8197                   {
8198                      if(ui64 > MAXINT)
8199                      {
8200                         if(ui64 > MAXDWORD)
8201                         {
8202                            is64Bit = true;
8203                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8204                               isSigned = true;
8205                         }
8206                      }
8207                      else if(constant[0] != '0' || !constant[1])
8208                         isSigned = true;
8209                   }
8210                   type.kind = is64Bit ? int64Type : intType;
8211                   type.isSigned = isSigned;
8212                }
8213             }
8214             exp.isConstant = true;
8215             if(exp.destType && exp.destType.kind == doubleType)
8216                type.kind = doubleType;
8217             else if(exp.destType && exp.destType.kind == floatType)
8218                type.kind = floatType;
8219             else if(exp.destType && exp.destType.kind == int64Type)
8220                type.kind = int64Type;
8221          }
8222          break;
8223       }
8224       case stringExp:
8225       {
8226          exp.isConstant = true;      // Why wasn't this constant?
8227          exp.expType = Type
8228          {
8229             refCount = 1;
8230             kind = pointerType;
8231             type = Type
8232             {
8233                refCount = 1;
8234                kind = charType;
8235                constant = true;
8236                isSigned = true;
8237             }
8238          };
8239          break;
8240       }
8241       case newExp:
8242       case new0Exp:
8243          ProcessExpressionType(exp._new.size);
8244          exp.expType = Type
8245          {
8246             refCount = 1;
8247             kind = pointerType;
8248             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8249          };
8250          DeclareType(exp.expType.type, false, false);
8251          break;
8252       case renewExp:
8253       case renew0Exp:
8254          ProcessExpressionType(exp._renew.size);
8255          ProcessExpressionType(exp._renew.exp);
8256          exp.expType = Type
8257          {
8258             refCount = 1;
8259             kind = pointerType;
8260             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8261          };
8262          DeclareType(exp.expType.type, false, false);
8263          break;
8264       case opExp:
8265       {
8266          bool assign = false, boolResult = false, boolOps = false;
8267          Type type1 = null, type2 = null;
8268          bool useDestType = false, useSideType = false;
8269          Location oldyylloc = yylloc;
8270          bool useSideUnit = false;
8271          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8272
8273          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8274          Type dummy
8275          {
8276             count = 1;
8277             refCount = 1;
8278          };
8279
8280          switch(exp.op.op)
8281          {
8282             // Assignment Operators
8283             case '=':
8284             case MUL_ASSIGN:
8285             case DIV_ASSIGN:
8286             case MOD_ASSIGN:
8287             case ADD_ASSIGN:
8288             case SUB_ASSIGN:
8289             case LEFT_ASSIGN:
8290             case RIGHT_ASSIGN:
8291             case AND_ASSIGN:
8292             case XOR_ASSIGN:
8293             case OR_ASSIGN:
8294                assign = true;
8295                break;
8296             // boolean Operators
8297             case '!':
8298                // Expect boolean operators
8299                //boolOps = true;
8300                //boolResult = true;
8301                break;
8302             case AND_OP:
8303             case OR_OP:
8304                // Expect boolean operands
8305                boolOps = true;
8306                boolResult = true;
8307                break;
8308             // Comparisons
8309             case EQ_OP:
8310             case '<':
8311             case '>':
8312             case LE_OP:
8313             case GE_OP:
8314             case NE_OP:
8315                // Gives boolean result
8316                boolResult = true;
8317                useSideType = true;
8318                break;
8319             case '+':
8320             case '-':
8321                useSideUnit = true;
8322                useSideType = true;
8323                useDestType = true;
8324                break;
8325
8326             case LEFT_OP:
8327             case RIGHT_OP:
8328                useSideType = true;
8329                useDestType = true;
8330                break;
8331
8332             case '|':
8333             case '^':
8334                useSideType = true;
8335                useDestType = true;
8336                break;
8337
8338             case '/':
8339             case '%':
8340                useSideType = true;
8341                useDestType = true;
8342                break;
8343             case '&':
8344             case '*':
8345                if(exp.op.exp1)
8346                {
8347                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8348                   useSideType = true;
8349                   useDestType = true;
8350                }
8351                break;
8352
8353             /*// Implement speed etc.
8354             case '*':
8355             case '/':
8356                break;
8357             */
8358          }
8359          if(exp.op.op == '&')
8360          {
8361             // Added this here earlier for Iterator address as key
8362             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8363             {
8364                Identifier id = exp.op.exp2.identifier;
8365                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8366                if(symbol && symbol.isIterator == 2)
8367                {
8368                   exp.type = memberExp;
8369                   exp.member.exp = exp.op.exp2;
8370                   exp.member.member = MkIdentifier("key");
8371                   exp.expType = null;
8372                   exp.op.exp2.expType = symbol.type;
8373                   symbol.type.refCount++;
8374                   ProcessExpressionType(exp);
8375                   FreeType(dummy);
8376                   break;
8377                }
8378                // exp.op.exp2.usage.usageRef = true;
8379             }
8380          }
8381
8382          //dummy.kind = TypeDummy;
8383          if(exp.op.exp1)
8384          {
8385             // Added this check here to use the dest type only for units derived from the base unit
8386             // So that untyped units will use the side unit as opposed to the untyped destination unit
8387             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8388             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8389                useDestType = false;
8390
8391             if(destClass && useDestType &&
8392               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8393
8394               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8395             {
8396                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8397                exp.op.exp1.destType = exp.destType;
8398                exp.op.exp1.opDestType = true;
8399                if(exp.destType)
8400                   exp.destType.refCount++;
8401             }
8402             else if(!assign)
8403             {
8404                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8405                exp.op.exp1.destType = dummy;
8406                dummy.refCount++;
8407             }
8408
8409             // TESTING THIS HERE...
8410             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8411                ProcessExpressionType(exp.op.exp1);
8412             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8413
8414             exp.op.exp1.opDestType = false;
8415
8416             // Fix for unit and ++ / --
8417             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8418                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8419             {
8420                exp.op.exp2 = MkExpConstant("1");
8421                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8422                assign = true;
8423             }
8424
8425             if(exp.op.exp1.destType == dummy)
8426             {
8427                FreeType(dummy);
8428                exp.op.exp1.destType = null;
8429             }
8430             type1 = exp.op.exp1.expType;
8431          }
8432
8433          if(exp.op.exp2)
8434          {
8435             char expString[10240];
8436             expString[0] = '\0';
8437             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8438             {
8439                if(exp.op.exp1)
8440                {
8441                   exp.op.exp2.destType = exp.op.exp1.expType;
8442                   if(exp.op.exp1.expType)
8443                      exp.op.exp1.expType.refCount++;
8444                }
8445                else
8446                {
8447                   exp.op.exp2.destType = exp.destType;
8448                   if(!exp.op.exp1 || exp.op.op != '&')
8449                      exp.op.exp2.opDestType = true;
8450                   if(exp.destType)
8451                      exp.destType.refCount++;
8452                }
8453
8454                if(type1) type1.refCount++;
8455                exp.expType = type1;
8456             }
8457             else if(assign)
8458             {
8459                if(inCompiler)
8460                   PrintExpression(exp.op.exp2, expString);
8461
8462                if(type1 && type1.kind == pointerType)
8463                {
8464                   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 ||
8465                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8466                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8467                   else if(exp.op.op == '=')
8468                   {
8469                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8470                      exp.op.exp2.destType = type1;
8471                      if(type1)
8472                         type1.refCount++;
8473                   }
8474                }
8475                else
8476                {
8477                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8478                   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/* ||
8479                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8480                   else
8481                   {
8482                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8483                      exp.op.exp2.destType = type1;
8484                      if(type1)
8485                         type1.refCount++;
8486                   }
8487                }
8488                if(type1) type1.refCount++;
8489                exp.expType = type1;
8490             }
8491             else if(destClass &&
8492                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8493                   (destClass.type == enumClass && useDestType)))
8494             {
8495                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8496                exp.op.exp2.destType = exp.destType;
8497                if(exp.op.op != '&')
8498                   exp.op.exp2.opDestType = true;
8499                if(exp.destType)
8500                   exp.destType.refCount++;
8501             }
8502             else
8503             {
8504                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8505                exp.op.exp2.destType = dummy;
8506                dummy.refCount++;
8507             }
8508
8509             // TESTING THIS HERE... (DANGEROUS)
8510             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8511                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8512             {
8513                FreeType(exp.op.exp2.destType);
8514                exp.op.exp2.destType = type1;
8515                type1.refCount++;
8516             }
8517             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8518             // Cannot lose the cast on a sizeof
8519             if(exp.op.op == SIZEOF)
8520             {
8521                Expression e = exp.op.exp2;
8522                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8523                {
8524                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8525                   {
8526                      if(e.type == extensionCompoundExp)
8527                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8528                      else
8529                         e = e.list->last;
8530                   }
8531                }
8532                if(e.type == castExp && e.cast.exp)
8533                   e.cast.exp.needCast = true;
8534             }
8535             ProcessExpressionType(exp.op.exp2);
8536             exp.op.exp2.opDestType = false;
8537             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8538
8539             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8540             {
8541                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)
8542                {
8543                   if(exp.op.op != '=' && type1.type.kind == voidType)
8544                      Compiler_Error($"void *: unknown size\n");
8545                }
8546                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||
8547                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8548                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8549                               exp.op.exp2.expType._class.registered.type == structClass ||
8550                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8551                {
8552                   if(exp.op.op == ADD_ASSIGN)
8553                      Compiler_Error($"cannot add two pointers\n");
8554                }
8555                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8556                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8557                {
8558                   if(exp.op.op == ADD_ASSIGN)
8559                      Compiler_Error($"cannot add two pointers\n");
8560                }
8561                else if(inCompiler)
8562                {
8563                   char type1String[1024];
8564                   char type2String[1024];
8565                   type1String[0] = '\0';
8566                   type2String[0] = '\0';
8567
8568                   PrintType(exp.op.exp2.expType, type1String, false, true);
8569                   PrintType(type1, type2String, false, true);
8570                   ChangeCh(expString, '\n', ' ');
8571                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8572                }
8573             }
8574
8575             if(exp.op.exp2.destType == dummy)
8576             {
8577                FreeType(dummy);
8578                exp.op.exp2.destType = null;
8579             }
8580
8581             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8582             {
8583                type2 = { };
8584                type2.refCount = 1;
8585                CopyTypeInto(type2, exp.op.exp2.expType);
8586                type2.isSigned = true;
8587             }
8588             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8589             {
8590                type2 = { kind = intType };
8591                type2.refCount = 1;
8592                type2.isSigned = true;
8593             }
8594             else
8595             {
8596                type2 = exp.op.exp2.expType;
8597                if(type2) type2.refCount++;
8598             }
8599          }
8600
8601          dummy.kind = voidType;
8602
8603          if(exp.op.op == SIZEOF)
8604          {
8605             exp.expType = Type
8606             {
8607                refCount = 1;
8608                kind = intSizeType;
8609             };
8610             exp.isConstant = true;
8611          }
8612          // Get type of dereferenced pointer
8613          else if(exp.op.op == '*' && !exp.op.exp1)
8614          {
8615             exp.expType = Dereference(type2);
8616             if(type2 && type2.kind == classType)
8617                notByReference = true;
8618          }
8619          else if(exp.op.op == '&' && !exp.op.exp1)
8620             exp.expType = Reference(type2);
8621          else if(!assign)
8622          {
8623             if(boolOps)
8624             {
8625                if(exp.op.exp1)
8626                {
8627                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8628                   exp.op.exp1.destType = MkClassType("bool");
8629                   exp.op.exp1.destType.truth = true;
8630                   if(!exp.op.exp1.expType)
8631                      ProcessExpressionType(exp.op.exp1);
8632                   else
8633                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8634                   FreeType(exp.op.exp1.expType);
8635                   exp.op.exp1.expType = MkClassType("bool");
8636                   exp.op.exp1.expType.truth = true;
8637                }
8638                if(exp.op.exp2)
8639                {
8640                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8641                   exp.op.exp2.destType = MkClassType("bool");
8642                   exp.op.exp2.destType.truth = true;
8643                   if(!exp.op.exp2.expType)
8644                      ProcessExpressionType(exp.op.exp2);
8645                   else
8646                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8647                   FreeType(exp.op.exp2.expType);
8648                   exp.op.exp2.expType = MkClassType("bool");
8649                   exp.op.exp2.expType.truth = true;
8650                }
8651             }
8652             else if(exp.op.exp1 && exp.op.exp2 &&
8653                ((useSideType /*&&
8654                      (useSideUnit ||
8655                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8656                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8657                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8658                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8659             {
8660                if(type1 && type2 &&
8661                   // If either both are class or both are not class
8662                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8663                {
8664                   // Added this check for enum subtraction to result in an int type:
8665                   if(exp.op.op == '-' &&
8666                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8667                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8668                   {
8669                      Type intType;
8670                      if(!type1._class.registered.dataType)
8671                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8672                      if(!type2._class.registered.dataType)
8673                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8674
8675                      intType = ProcessTypeString(
8676                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8677
8678                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8679                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8680                      exp.op.exp1.destType = intType;
8681                      exp.op.exp2.destType = intType;
8682                      intType.refCount++;
8683                   }
8684                   else
8685                   {
8686                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8687                      exp.op.exp2.destType = type1;
8688                      type1.refCount++;
8689                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8690                      exp.op.exp1.destType = type2;
8691                      type2.refCount++;
8692                   }
8693
8694                   // Warning here for adding Radians + Degrees with no destination type
8695                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8696                      type1._class.registered && type1._class.registered.type == unitClass &&
8697                      type2._class.registered && type2._class.registered.type == unitClass &&
8698                      type1._class.registered != type2._class.registered)
8699                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8700                         type1._class.string, type2._class.string, type1._class.string);
8701
8702                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8703                   {
8704                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8705                      if(argExp)
8706                      {
8707                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8708
8709                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8710                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8711                            exp.op.exp1)));
8712
8713                         ProcessExpressionType(exp.op.exp1);
8714
8715                         if(type2.kind != pointerType)
8716                         {
8717                            ProcessExpressionType(classExp);
8718
8719                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8720
8721                            if(!exp.op.exp2.expType)
8722                            {
8723                               if(type2)
8724                                  FreeType(type2);
8725                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8726                               type2.refCount++;
8727                            }
8728
8729                            ProcessExpressionType(exp.op.exp2);
8730                         }
8731                      }
8732                   }
8733
8734                   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)))
8735                   {
8736                      if(type1.kind != classType && type1.type.kind == voidType)
8737                         Compiler_Error($"void *: unknown size\n");
8738                      exp.expType = type1;
8739                      if(type1) type1.refCount++;
8740                   }
8741                   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)))
8742                   {
8743                      if(type2.kind != classType && type2.type.kind == voidType)
8744                         Compiler_Error($"void *: unknown size\n");
8745                      exp.expType = type2;
8746                      if(type2) type2.refCount++;
8747                   }
8748                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8749                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8750                   {
8751                      Compiler_Warning($"different levels of indirection\n");
8752                   }
8753                   else
8754                   {
8755                      bool success = false;
8756                      if(type1.kind == pointerType && type2.kind == pointerType)
8757                      {
8758                         if(exp.op.op == '+')
8759                            Compiler_Error($"cannot add two pointers\n");
8760                         else if(exp.op.op == '-')
8761                         {
8762                            // Pointer Subtraction gives integer
8763                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8764                            {
8765                               exp.expType = Type
8766                               {
8767                                  kind = intType;
8768                                  refCount = 1;
8769                               };
8770                               success = true;
8771
8772                               if(type1.type.kind == templateType)
8773                               {
8774                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8775                                  if(argExp)
8776                                  {
8777                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8778
8779                                     ProcessExpressionType(classExp);
8780
8781                                     exp.type = bracketsExp;
8782                                     exp.list = MkListOne(MkExpOp(
8783                                        MkExpBrackets(MkListOne(MkExpOp(
8784                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8785                                              , exp.op.op,
8786                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8787                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8788
8789                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8790                                     FreeType(dummy);
8791                                     return;
8792                                  }
8793                               }
8794                            }
8795                         }
8796                      }
8797
8798                      if(!success && exp.op.exp1.type == constantExp)
8799                      {
8800                         // If first expression is constant, try to match that first
8801                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8802                         {
8803                            if(exp.expType) FreeType(exp.expType);
8804                            exp.expType = exp.op.exp1.destType;
8805                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8806                            success = true;
8807                         }
8808                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8809                         {
8810                            if(exp.expType) FreeType(exp.expType);
8811                            exp.expType = exp.op.exp2.destType;
8812                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8813                            success = true;
8814                         }
8815                      }
8816                      else if(!success)
8817                      {
8818                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8819                         {
8820                            if(exp.expType) FreeType(exp.expType);
8821                            exp.expType = exp.op.exp2.destType;
8822                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8823                            success = true;
8824                         }
8825                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8826                         {
8827                            if(exp.expType) FreeType(exp.expType);
8828                            exp.expType = exp.op.exp1.destType;
8829                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8830                            success = true;
8831                         }
8832                      }
8833                      if(!success)
8834                      {
8835                         char expString1[10240];
8836                         char expString2[10240];
8837                         char type1[1024];
8838                         char type2[1024];
8839                         expString1[0] = '\0';
8840                         expString2[0] = '\0';
8841                         type1[0] = '\0';
8842                         type2[0] = '\0';
8843                         if(inCompiler)
8844                         {
8845                            PrintExpression(exp.op.exp1, expString1);
8846                            ChangeCh(expString1, '\n', ' ');
8847                            PrintExpression(exp.op.exp2, expString2);
8848                            ChangeCh(expString2, '\n', ' ');
8849                            PrintType(exp.op.exp1.expType, type1, false, true);
8850                            PrintType(exp.op.exp2.expType, type2, false, true);
8851                         }
8852
8853                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8854                      }
8855                   }
8856                }
8857                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8858                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8859                {
8860                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8861                   // Convert e.g. / 4 into / 4.0
8862                   exp.op.exp1.destType = type2._class.registered.dataType;
8863                   if(type2._class.registered.dataType)
8864                      type2._class.registered.dataType.refCount++;
8865                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8866                   exp.expType = type2;
8867                   if(type2) type2.refCount++;
8868                }
8869                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8870                {
8871                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8872                   // Convert e.g. / 4 into / 4.0
8873                   exp.op.exp2.destType = type1._class.registered.dataType;
8874                   if(type1._class.registered.dataType)
8875                      type1._class.registered.dataType.refCount++;
8876                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8877                   exp.expType = type1;
8878                   if(type1) type1.refCount++;
8879                }
8880                else if(type1)
8881                {
8882                   bool valid = false;
8883
8884                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8885                   {
8886                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8887
8888                      if(!type1._class.registered.dataType)
8889                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8890                      exp.op.exp2.destType = type1._class.registered.dataType;
8891                      exp.op.exp2.destType.refCount++;
8892
8893                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8894                      if(type2)
8895                         FreeType(type2);
8896                      type2 = exp.op.exp2.destType;
8897                      if(type2) type2.refCount++;
8898
8899                      exp.expType = type2;
8900                      type2.refCount++;
8901                   }
8902
8903                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8904                   {
8905                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8906
8907                      if(!type2._class.registered.dataType)
8908                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8909                      exp.op.exp1.destType = type2._class.registered.dataType;
8910                      exp.op.exp1.destType.refCount++;
8911
8912                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8913                      type1 = exp.op.exp1.destType;
8914                      exp.expType = type1;
8915                      type1.refCount++;
8916                   }
8917
8918                   // TESTING THIS NEW CODE
8919                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8920                   {
8921                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8922                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8923                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8924                      {
8925                         // Convert the enum to an int instead for these operators
8926                         if(op1IsEnum && exp.op.exp2.expType)
8927                         {
8928                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8929                            {
8930                               if(exp.expType) FreeType(exp.expType);
8931                               exp.expType = exp.op.exp2.expType;
8932                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8933                               valid = true;
8934                            }
8935                         }
8936                         else if(op2IsEnum && exp.op.exp1.expType)
8937                         {
8938                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8939                            {
8940                               if(exp.expType) FreeType(exp.expType);
8941                               exp.expType = exp.op.exp1.expType;
8942                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8943                               valid = true;
8944                            }
8945                         }
8946                      }
8947                      else
8948                      {
8949                         if(op1IsEnum && exp.op.exp2.expType)
8950                         {
8951                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8952                            {
8953                               if(exp.expType) FreeType(exp.expType);
8954                               exp.expType = exp.op.exp1.expType;
8955                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8956                               valid = true;
8957                            }
8958                         }
8959                         else if(op2IsEnum && exp.op.exp1.expType)
8960                         {
8961                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8962                            {
8963                               if(exp.expType) FreeType(exp.expType);
8964                               exp.expType = exp.op.exp2.expType;
8965                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8966                               valid = true;
8967                            }
8968                         }
8969                      }
8970                   }
8971
8972                   if(!valid)
8973                   {
8974                      // Added this first part of the if here to handle  5 + Degrees { 5 } with either a base unit dest or not a unit dest type
8975                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8976                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8977                      {
8978                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8979                         exp.op.exp1.destType = type2;
8980                         type2.refCount++;
8981
8982                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8983                         {
8984                            if(exp.expType) FreeType(exp.expType);
8985                            exp.expType = exp.op.exp1.destType;
8986                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8987                         }
8988                      }
8989                      else
8990                      {
8991                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8992                         exp.op.exp2.destType = type1;
8993                         type1.refCount++;
8994
8995                      /*
8996                      // Maybe this was meant to be an enum...
8997                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8998                      {
8999                         Type oldType = exp.op.exp2.expType;
9000                         exp.op.exp2.expType = null;
9001                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
9002                            FreeType(oldType);
9003                         else
9004                            exp.op.exp2.expType = oldType;
9005                      }
9006                      */
9007
9008                      /*
9009                      // TESTING THIS HERE... LATEST ADDITION
9010                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9011                      {
9012                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9013                         exp.op.exp2.destType = type2._class.registered.dataType;
9014                         if(type2._class.registered.dataType)
9015                            type2._class.registered.dataType.refCount++;
9016                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
9017
9018                         //exp.expType = type2._class.registered.dataType; //type2;
9019                         //if(type2) type2.refCount++;
9020                      }
9021
9022                      // TESTING THIS HERE... LATEST ADDITION
9023                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9024                      {
9025                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9026                         exp.op.exp1.destType = type1._class.registered.dataType;
9027                         if(type1._class.registered.dataType)
9028                            type1._class.registered.dataType.refCount++;
9029                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
9030                         exp.expType = type1._class.registered.dataType; //type1;
9031                         if(type1) type1.refCount++;
9032                      }
9033                      */
9034
9035                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
9036                         {
9037                            if(exp.expType) FreeType(exp.expType);
9038                            exp.expType = exp.op.exp2.destType;
9039                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
9040                         }
9041                         else if(type1 && type2)
9042                         {
9043                            char expString1[10240];
9044                            char expString2[10240];
9045                            char type1String[1024];
9046                            char type2String[1024];
9047                            expString1[0] = '\0';
9048                            expString2[0] = '\0';
9049                            type1String[0] = '\0';
9050                            type2String[0] = '\0';
9051                            if(inCompiler)
9052                            {
9053                               PrintExpression(exp.op.exp1, expString1);
9054                               ChangeCh(expString1, '\n', ' ');
9055                               PrintExpression(exp.op.exp2, expString2);
9056                               ChangeCh(expString2, '\n', ' ');
9057                               PrintType(exp.op.exp1.expType, type1String, false, true);
9058                               PrintType(exp.op.exp2.expType, type2String, false, true);
9059                            }
9060
9061                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9062
9063                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9064                            {
9065                               exp.expType = exp.op.exp1.expType;
9066                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9067                            }
9068                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9069                            {
9070                               exp.expType = exp.op.exp2.expType;
9071                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9072                            }
9073                         }
9074                      }
9075                   }
9076                }
9077                else if(type2)
9078                {
9079                   // Maybe this was meant to be an enum...
9080                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9081                   {
9082                      Type oldType = exp.op.exp1.expType;
9083                      exp.op.exp1.expType = null;
9084                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9085                         FreeType(oldType);
9086                      else
9087                         exp.op.exp1.expType = oldType;
9088                   }
9089
9090                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9091                   exp.op.exp1.destType = type2;
9092                   type2.refCount++;
9093                   /*
9094                   // TESTING THIS HERE... LATEST ADDITION
9095                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9096                   {
9097                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9098                      exp.op.exp1.destType = type1._class.registered.dataType;
9099                      if(type1._class.registered.dataType)
9100                         type1._class.registered.dataType.refCount++;
9101                   }
9102
9103                   // TESTING THIS HERE... LATEST ADDITION
9104                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9105                   {
9106                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9107                      exp.op.exp2.destType = type2._class.registered.dataType;
9108                      if(type2._class.registered.dataType)
9109                         type2._class.registered.dataType.refCount++;
9110                   }
9111                   */
9112
9113                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9114                   {
9115                      if(exp.expType) FreeType(exp.expType);
9116                      exp.expType = exp.op.exp1.destType;
9117                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9118                   }
9119                }
9120             }
9121             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9122             {
9123                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9124                {
9125                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9126                   // Convert e.g. / 4 into / 4.0
9127                   exp.op.exp1.destType = type2._class.registered.dataType;
9128                   if(type2._class.registered.dataType)
9129                      type2._class.registered.dataType.refCount++;
9130                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9131                }
9132                if(exp.op.op == '!')
9133                {
9134                   exp.expType = MkClassType("bool");
9135                   exp.expType.truth = true;
9136                }
9137                else
9138                {
9139                   exp.expType = type2;
9140                   if(type2) type2.refCount++;
9141                }
9142             }
9143             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9144             {
9145                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9146                {
9147                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9148                   // Convert e.g. / 4 into / 4.0
9149                   exp.op.exp2.destType = type1._class.registered.dataType;
9150                   if(type1._class.registered.dataType)
9151                      type1._class.registered.dataType.refCount++;
9152                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9153                }
9154                exp.expType = type1;
9155                if(type1) type1.refCount++;
9156             }
9157          }
9158
9159          yylloc = exp.loc;
9160          if(exp.op.exp1 && !exp.op.exp1.expType)
9161          {
9162             char expString[10000];
9163             expString[0] = '\0';
9164             if(inCompiler)
9165             {
9166                PrintExpression(exp.op.exp1, expString);
9167                ChangeCh(expString, '\n', ' ');
9168             }
9169             if(expString[0])
9170                Compiler_Error($"couldn't determine type of %s\n", expString);
9171          }
9172          if(exp.op.exp2 && !exp.op.exp2.expType)
9173          {
9174             char expString[10240];
9175             expString[0] = '\0';
9176             if(inCompiler)
9177             {
9178                PrintExpression(exp.op.exp2, expString);
9179                ChangeCh(expString, '\n', ' ');
9180             }
9181             if(expString[0])
9182                Compiler_Error($"couldn't determine type of %s\n", expString);
9183          }
9184
9185          if(boolResult)
9186          {
9187             FreeType(exp.expType);
9188             exp.expType = MkClassType("bool");
9189             exp.expType.truth = true;
9190          }
9191
9192          if(exp.op.op != SIZEOF)
9193             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9194                (!exp.op.exp2 || exp.op.exp2.isConstant);
9195
9196          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9197          {
9198             DeclareType(exp.op.exp2.expType, false, false);
9199          }
9200
9201          if(exp.op.op == DELETE && exp.op.exp2 && exp.op.exp2.expType && exp.op.exp2.expType.specConst)
9202             Compiler_Warning($"deleting const qualified object\n");
9203
9204          yylloc = oldyylloc;
9205
9206          FreeType(dummy);
9207          if(type2)
9208             FreeType(type2);
9209          break;
9210       }
9211       case bracketsExp:
9212       case extensionExpressionExp:
9213       {
9214          Expression e;
9215          exp.isConstant = true;
9216          for(e = exp.list->first; e; e = e.next)
9217          {
9218             bool inced = false;
9219             if(!e.next)
9220             {
9221                FreeType(e.destType);
9222                e.opDestType = exp.opDestType;
9223                e.destType = exp.destType;
9224                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9225             }
9226             ProcessExpressionType(e);
9227             if(inced)
9228                exp.destType.count--;
9229             if(!exp.expType && !e.next)
9230             {
9231                exp.expType = e.expType;
9232                if(e.expType) e.expType.refCount++;
9233             }
9234             if(!e.isConstant)
9235                exp.isConstant = false;
9236          }
9237
9238          // In case a cast became a member...
9239          e = exp.list->first;
9240          if(!e.next && e.type == memberExp)
9241          {
9242             // Preserve prev, next
9243             Expression next = exp.next, prev = exp.prev;
9244
9245
9246             FreeType(exp.expType);
9247             FreeType(exp.destType);
9248             delete exp.list;
9249
9250             *exp = *e;
9251
9252             exp.prev = prev;
9253             exp.next = next;
9254
9255             delete e;
9256
9257             ProcessExpressionType(exp);
9258          }
9259          break;
9260       }
9261       case indexExp:
9262       {
9263          Expression e;
9264          exp.isConstant = true;
9265
9266          ProcessExpressionType(exp.index.exp);
9267          if(!exp.index.exp.isConstant)
9268             exp.isConstant = false;
9269
9270          if(exp.index.exp.expType)
9271          {
9272             Type source = exp.index.exp.expType;
9273             if(source.kind == classType && source._class && source._class.registered)
9274             {
9275                Class _class = source._class.registered;
9276                Class c = _class.templateClass ? _class.templateClass : _class;
9277                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9278                {
9279                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9280
9281                   if(exp.index.index && exp.index.index->last)
9282                   {
9283                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9284
9285                      if(type.kind == classType) type.constant = true;
9286                      else if(type.kind == pointerType)
9287                      {
9288                         Type t = type;
9289                         while(t.kind == pointerType) t = t.type;
9290                         t.constant = true;
9291                      }
9292
9293                      ((Expression)exp.index.index->last).destType = type;
9294                   }
9295                }
9296             }
9297          }
9298
9299          for(e = exp.index.index->first; e; e = e.next)
9300          {
9301             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9302             {
9303                if(e.destType) FreeType(e.destType);
9304                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9305             }
9306             ProcessExpressionType(e);
9307             if(!e.next)
9308             {
9309                // Check if this type is int
9310             }
9311             if(!e.isConstant)
9312                exp.isConstant = false;
9313          }
9314
9315          if(!exp.expType)
9316             exp.expType = Dereference(exp.index.exp.expType);
9317          if(exp.expType)
9318             DeclareType(exp.expType, false, false);
9319          break;
9320       }
9321       case callExp:
9322       {
9323          Expression e;
9324          Type functionType;
9325          Type methodType = null;
9326          char name[1024];
9327          name[0] = '\0';
9328
9329          if(inCompiler)
9330          {
9331             PrintExpression(exp.call.exp,  name);
9332             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9333             {
9334                //exp.call.exp.expType = null;
9335                PrintExpression(exp.call.exp,  name);
9336             }
9337          }
9338          if(exp.call.exp.type == identifierExp)
9339          {
9340             Expression idExp = exp.call.exp;
9341             Identifier id = idExp.identifier;
9342             if(!strcmp(id.string, "__builtin_frame_address"))
9343             {
9344                exp.expType = ProcessTypeString("void *", true);
9345                if(exp.call.arguments && exp.call.arguments->first)
9346                   ProcessExpressionType(exp.call.arguments->first);
9347                break;
9348             }
9349             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9350             {
9351                exp.expType = ProcessTypeString("int", true);
9352                if(exp.call.arguments && exp.call.arguments->first)
9353                   ProcessExpressionType(exp.call.arguments->first);
9354                break;
9355             }
9356             else if(!strcmp(id.string, "Max") ||
9357                !strcmp(id.string, "Min") ||
9358                !strcmp(id.string, "Sgn") ||
9359                !strcmp(id.string, "Abs"))
9360             {
9361                Expression a = null;
9362                Expression b = null;
9363                Expression tempExp1 = null, tempExp2 = null;
9364                if((!strcmp(id.string, "Max") ||
9365                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9366                {
9367                   a = exp.call.arguments->first;
9368                   b = exp.call.arguments->last;
9369                   tempExp1 = a;
9370                   tempExp2 = b;
9371                }
9372                else if(exp.call.arguments->count == 1)
9373                {
9374                   a = exp.call.arguments->first;
9375                   tempExp1 = a;
9376                }
9377
9378                if(a)
9379                {
9380                   exp.call.arguments->Clear();
9381                   idExp.identifier = null;
9382
9383                   FreeExpContents(exp);
9384
9385                   ProcessExpressionType(a);
9386                   if(b)
9387                      ProcessExpressionType(b);
9388
9389                   exp.type = bracketsExp;
9390                   exp.list = MkList();
9391
9392                   if(a.expType && (!b || b.expType))
9393                   {
9394                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9395                      {
9396                         // Use the simpleStruct name/ids for now...
9397                         if(inCompiler)
9398                         {
9399                            OldList * specs = MkList();
9400                            OldList * decls = MkList();
9401                            Declaration decl;
9402                            char temp1[1024], temp2[1024];
9403
9404                            GetTypeSpecs(a.expType, specs);
9405
9406                            if(a && !a.isConstant && a.type != identifierExp)
9407                            {
9408                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9409                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9410                               tempExp1 = QMkExpId(temp1);
9411                               tempExp1.expType = a.expType;
9412                               if(a.expType)
9413                                  a.expType.refCount++;
9414                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9415                            }
9416                            if(b && !b.isConstant && b.type != identifierExp)
9417                            {
9418                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9419                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9420                               tempExp2 = QMkExpId(temp2);
9421                               tempExp2.expType = b.expType;
9422                               if(b.expType)
9423                                  b.expType.refCount++;
9424                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9425                            }
9426
9427                            decl = MkDeclaration(specs, decls);
9428                            if(!curCompound.compound.declarations)
9429                               curCompound.compound.declarations = MkList();
9430                            curCompound.compound.declarations->Insert(null, decl);
9431                         }
9432                      }
9433                   }
9434
9435                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9436                   {
9437                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9438                      ListAdd(exp.list,
9439                         MkExpCondition(MkExpBrackets(MkListOne(
9440                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9441                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9442                      exp.expType = a.expType;
9443                      if(a.expType)
9444                         a.expType.refCount++;
9445                   }
9446                   else if(!strcmp(id.string, "Abs"))
9447                   {
9448                      ListAdd(exp.list,
9449                         MkExpCondition(MkExpBrackets(MkListOne(
9450                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9451                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9452                      exp.expType = a.expType;
9453                      if(a.expType)
9454                         a.expType.refCount++;
9455                   }
9456                   else if(!strcmp(id.string, "Sgn"))
9457                   {
9458                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9459                      ListAdd(exp.list,
9460                         MkExpCondition(MkExpBrackets(MkListOne(
9461                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9462                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9463                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9464                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9465                      exp.expType = ProcessTypeString("int", false);
9466                   }
9467
9468                   FreeExpression(tempExp1);
9469                   if(tempExp2) FreeExpression(tempExp2);
9470
9471                   FreeIdentifier(id);
9472                   break;
9473                }
9474             }
9475          }
9476
9477          {
9478             Type dummy
9479             {
9480                count = 1;
9481                refCount = 1;
9482             };
9483             if(!exp.call.exp.destType)
9484             {
9485                exp.call.exp.destType = dummy;
9486                dummy.refCount++;
9487             }
9488             ProcessExpressionType(exp.call.exp);
9489             if(exp.call.exp.destType == dummy)
9490             {
9491                FreeType(dummy);
9492                exp.call.exp.destType = null;
9493             }
9494             FreeType(dummy);
9495          }
9496
9497          // Check argument types against parameter types
9498          functionType = exp.call.exp.expType;
9499
9500          if(functionType && functionType.kind == TypeKind::methodType)
9501          {
9502             methodType = functionType;
9503             functionType = methodType.method.dataType;
9504
9505             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9506             // TOCHECK: Instead of doing this here could this be done per param?
9507             if(exp.call.exp.expType.usedClass)
9508             {
9509                char typeString[1024];
9510                typeString[0] = '\0';
9511                {
9512                   Symbol back = functionType.thisClass;
9513                   // Do not output class specifier here (thisclass was added to this)
9514                   functionType.thisClass = null;
9515                   PrintType(functionType, typeString, true, true);
9516                   functionType.thisClass = back;
9517                }
9518                if(strstr(typeString, "thisclass"))
9519                {
9520                   OldList * specs = MkList();
9521                   Declarator decl;
9522                   {
9523                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9524
9525                      decl = SpecDeclFromString(typeString, specs, null);
9526
9527                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9528                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9529                         exp.call.exp.expType.usedClass))
9530                         thisClassParams = false;
9531
9532                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9533                      {
9534                         Class backupThisClass = thisClass;
9535                         thisClass = exp.call.exp.expType.usedClass;
9536                         ProcessDeclarator(decl);
9537                         thisClass = backupThisClass;
9538                      }
9539
9540                      thisClassParams = true;
9541
9542                      functionType = ProcessType(specs, decl);
9543                      functionType.refCount = 0;
9544                      FinishTemplatesContext(context);
9545
9546                      // Mark parameters that were 'thisclass'
9547                      /*{
9548                         Type p, op;
9549                         for(p = functionType.params.first, op = methodType.method.dataType.params.first; p && op; p = p.next, op = op.next)
9550                            p.wasThisClass = op.kind == thisClassType;
9551                      }*/
9552                   }
9553
9554                   FreeList(specs, FreeSpecifier);
9555                   FreeDeclarator(decl);
9556                 }
9557             }
9558          }
9559          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9560          {
9561             Type type = functionType.type;
9562             if(!functionType.refCount)
9563             {
9564                functionType.type = null;
9565                FreeType(functionType);
9566             }
9567             //methodType = functionType;
9568             functionType = type;
9569          }
9570          if(functionType && functionType.kind != TypeKind::functionType)
9571          {
9572             Compiler_Error($"called object %s is not a function\n", name);
9573          }
9574          else if(functionType)
9575          {
9576             bool emptyParams = false, noParams = false;
9577             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9578             Type type = functionType.params.first;
9579             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9580             int extra = 0;
9581             Location oldyylloc = yylloc;
9582
9583             if(!type) emptyParams = true;
9584
9585             // WORKING ON THIS:
9586             if(functionType.extraParam && e && functionType.thisClass)
9587             {
9588                e.destType = MkClassType(functionType.thisClass.string);
9589                e = e.next;
9590             }
9591
9592             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9593             // Fixed #141 by adding '&& !functionType.extraParam'
9594             if(!functionType.staticMethod && !functionType.extraParam)
9595             {
9596                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9597                   memberExp.member.exp.expType._class)
9598                {
9599                   type = MkClassType(memberExp.member.exp.expType._class.string);
9600                   if(e)
9601                   {
9602                      e.destType = type;
9603                      e = e.next;
9604                      type = functionType.params.first;
9605                   }
9606                   else
9607                      type.refCount = 0;
9608                }
9609                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9610                {
9611                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9612                   type.byReference = functionType.byReference;
9613                   type.typedByReference = functionType.typedByReference;
9614                   if(e)
9615                   {
9616                      // Allow manually passing a class for typed object
9617                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9618                         e = e.next;
9619                      e.destType = type;
9620                      e = e.next;
9621                      type = functionType.params.first;
9622                   }
9623                   else
9624                      type.refCount = 0;
9625                   //extra = 1;
9626                }
9627             }
9628
9629             if(type && type.kind == voidType)
9630             {
9631                noParams = true;
9632                if(!type.refCount) FreeType(type);
9633                type = null;
9634             }
9635
9636             for( ; e; e = e.next)
9637             {
9638                if(!type && !emptyParams)
9639                {
9640                   yylloc = e.loc;
9641                   if(methodType && methodType.methodClass)
9642                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9643                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9644                         noParams ? 0 : functionType.params.count);
9645                   else
9646                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9647                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9648                         noParams ? 0 : functionType.params.count);
9649                   break;
9650                }
9651
9652                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9653                {
9654                   Type templatedType = null;
9655                   Class _class = methodType.usedClass;
9656                   ClassTemplateParameter curParam = null;
9657                   int id = 0;
9658                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9659                   {
9660                      Class sClass;
9661                      for(sClass = _class; sClass; sClass = sClass.base)
9662                      {
9663                         if(sClass.templateClass) sClass = sClass.templateClass;
9664                         id = 0;
9665                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9666                         {
9667                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9668                            {
9669                               Class nextClass;
9670                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9671                               {
9672                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9673                                  id += nextClass.templateParams.count;
9674                               }
9675                               break;
9676                            }
9677                            id++;
9678                         }
9679                         if(curParam) break;
9680                      }
9681                   }
9682                   if(curParam && _class.templateArgs[id].dataTypeString)
9683                   {
9684                      bool constant = type.constant;
9685                      ClassTemplateArgument arg = _class.templateArgs[id];
9686                      {
9687                         Context context = SetupTemplatesContext(_class);
9688
9689                         /*if(!arg.dataType)
9690                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9691                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9692                         FinishTemplatesContext(context);
9693                      }
9694
9695                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9696                      else if(templatedType.kind == pointerType)
9697                      {
9698                         Type t = templatedType.type;
9699                         while(t.kind == pointerType) t = t.type;
9700                         if(constant) t.constant = constant;
9701                      }
9702
9703                      e.destType = templatedType;
9704                      if(templatedType)
9705                      {
9706                         templatedType.passAsTemplate = true;
9707                         // templatedType.refCount++;
9708                      }
9709                   }
9710                   else
9711                   {
9712                      e.destType = type;
9713                      if(type) type.refCount++;
9714                   }
9715                }
9716                else
9717                {
9718                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9719                   {
9720                      e.destType = type.prev;
9721                      e.destType.refCount++;
9722                   }
9723                   else
9724                   {
9725                      e.destType = type;
9726                      if(type) type.refCount++;
9727                   }
9728                }
9729                // Don't reach the end for the ellipsis
9730                if(type && type.kind != ellipsisType)
9731                {
9732                   Type next = type.next;
9733                   if(!type.refCount) FreeType(type);
9734                   type = next;
9735                }
9736             }
9737
9738             if(type && type.kind != ellipsisType)
9739             {
9740                if(methodType && methodType.methodClass)
9741                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9742                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9743                      functionType.params.count + extra);
9744                else
9745                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9746                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9747                      functionType.params.count + extra);
9748             }
9749             yylloc = oldyylloc;
9750             if(type && !type.refCount) FreeType(type);
9751          }
9752          else
9753          {
9754             functionType = Type
9755             {
9756                refCount = 0;
9757                kind = TypeKind::functionType;
9758             };
9759
9760             if(exp.call.exp.type == identifierExp)
9761             {
9762                char * string = exp.call.exp.identifier.string;
9763                if(inCompiler)
9764                {
9765                   Symbol symbol;
9766                   Location oldyylloc = yylloc;
9767
9768                   yylloc = exp.call.exp.identifier.loc;
9769                   if(strstr(string, "__builtin_") == string)
9770                   {
9771                      if(exp.destType)
9772                      {
9773                         functionType.returnType = exp.destType;
9774                         exp.destType.refCount++;
9775                      }
9776                   }
9777                   else
9778                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9779                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9780                   globalContext.symbols.Add((BTNode)symbol);
9781                   if(strstr(symbol.string, "::"))
9782                      globalContext.hasNameSpace = true;
9783
9784                   yylloc = oldyylloc;
9785                }
9786             }
9787             else if(exp.call.exp.type == memberExp)
9788             {
9789                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9790                   exp.call.exp.member.member.string);*/
9791             }
9792             else
9793                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9794
9795             if(!functionType.returnType)
9796             {
9797                functionType.returnType = Type
9798                {
9799                   refCount = 1;
9800                   kind = intType;
9801                };
9802             }
9803          }
9804          if(functionType && functionType.kind == TypeKind::functionType)
9805          {
9806             exp.expType = functionType.returnType;
9807
9808             if(functionType.returnType)
9809                functionType.returnType.refCount++;
9810
9811             if(!functionType.refCount)
9812                FreeType(functionType);
9813          }
9814
9815          if(exp.call.arguments)
9816          {
9817             for(e = exp.call.arguments->first; e; e = e.next)
9818                ProcessExpressionType(e);
9819          }
9820          break;
9821       }
9822       case memberExp:
9823       {
9824          Type type;
9825          Location oldyylloc = yylloc;
9826          bool thisPtr;
9827          Expression checkExp = exp.member.exp;
9828          while(checkExp)
9829          {
9830             if(checkExp.type == castExp)
9831                checkExp = checkExp.cast.exp;
9832             else if(checkExp.type == bracketsExp)
9833                checkExp = checkExp.list ? checkExp.list->first : null;
9834             else
9835                break;
9836          }
9837
9838          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9839          exp.thisPtr = thisPtr;
9840
9841          // DOING THIS LATER NOW...
9842          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9843          {
9844             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9845             /* TODO: Name Space Fix ups
9846             if(!exp.member.member.classSym)
9847                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9848             */
9849          }
9850
9851          ProcessExpressionType(exp.member.exp);
9852          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9853             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9854          {
9855             exp.isConstant = false;
9856          }
9857          else
9858             exp.isConstant = exp.member.exp.isConstant;
9859          type = exp.member.exp.expType;
9860
9861          yylloc = exp.loc;
9862
9863          if(type && (type.kind == templateType))
9864          {
9865             Class _class = thisClass ? thisClass : currentClass;
9866             ClassTemplateParameter param = null;
9867             if(_class)
9868             {
9869                for(param = _class.templateParams.first; param; param = param.next)
9870                {
9871                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9872                      break;
9873                }
9874             }
9875             if(param && param.defaultArg.member)
9876             {
9877                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9878                if(argExp)
9879                {
9880                   Expression expMember = exp.member.exp;
9881                   Declarator decl;
9882                   OldList * specs = MkList();
9883                   char thisClassTypeString[1024];
9884
9885                   FreeIdentifier(exp.member.member);
9886
9887                   ProcessExpressionType(argExp);
9888
9889                   {
9890                      char * colon = strstr(param.defaultArg.memberString, "::");
9891                      if(colon)
9892                      {
9893                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9894                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9895                      }
9896                      else
9897                         strcpy(thisClassTypeString, _class.fullName);
9898                   }
9899
9900                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9901
9902                   exp.expType = ProcessType(specs, decl);
9903                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9904                   {
9905                      Class expClass = exp.expType._class.registered;
9906                      Class cClass = null;
9907                      int paramCount = 0;
9908                      int lastParam = -1;
9909
9910                      char templateString[1024];
9911                      ClassTemplateParameter param;
9912                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9913                      for(cClass = expClass; cClass; cClass = cClass.base)
9914                      {
9915                         int p = 0;
9916                         for(param = cClass.templateParams.first; param; param = param.next)
9917                         {
9918                            int id = p;
9919                            Class sClass;
9920                            ClassTemplateArgument arg;
9921                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9922                            arg = expClass.templateArgs[id];
9923
9924                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9925                            {
9926                               ClassTemplateParameter cParam;
9927                               //int p = numParams - sClass.templateParams.count;
9928                               int p = 0;
9929                               Class nextClass;
9930                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9931
9932                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9933                               {
9934                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9935                                  {
9936                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9937                                     {
9938                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9939                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9940                                        break;
9941                                     }
9942                                  }
9943                               }
9944                            }
9945
9946                            {
9947                               char argument[256];
9948                               argument[0] = '\0';
9949                               /*if(arg.name)
9950                               {
9951                                  strcat(argument, arg.name.string);
9952                                  strcat(argument, " = ");
9953                               }*/
9954                               switch(param.type)
9955                               {
9956                                  case expression:
9957                                  {
9958                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9959                                     char expString[1024];
9960                                     OldList * specs = MkList();
9961                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9962                                     Expression exp;
9963                                     char * string = PrintHexUInt64(arg.expression.ui64);
9964                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9965                                     delete string;
9966
9967                                     ProcessExpressionType(exp);
9968                                     ComputeExpression(exp);
9969                                     expString[0] = '\0';
9970                                     PrintExpression(exp, expString);
9971                                     strcat(argument, expString);
9972                                     // delete exp;
9973                                     FreeExpression(exp);
9974                                     break;
9975                                  }
9976                                  case identifier:
9977                                  {
9978                                     strcat(argument, arg.member.name);
9979                                     break;
9980                                  }
9981                                  case TemplateParameterType::type:
9982                                  {
9983                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9984                                     {
9985                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9986                                           strcat(argument, thisClassTypeString);
9987                                        else
9988                                           strcat(argument, arg.dataTypeString);
9989                                     }
9990                                     break;
9991                                  }
9992                               }
9993                               if(argument[0])
9994                               {
9995                                  if(paramCount) strcat(templateString, ", ");
9996                                  if(lastParam != p - 1)
9997                                  {
9998                                     strcat(templateString, param.name);
9999                                     strcat(templateString, " = ");
10000                                  }
10001                                  strcat(templateString, argument);
10002                                  paramCount++;
10003                                  lastParam = p;
10004                               }
10005                               p++;
10006                            }
10007                         }
10008                      }
10009                      {
10010                         int len = strlen(templateString);
10011                         if(templateString[len-1] == '>') templateString[len++] = ' ';
10012                         templateString[len++] = '>';
10013                         templateString[len++] = '\0';
10014                      }
10015                      {
10016                         Context context = SetupTemplatesContext(_class);
10017                         FreeType(exp.expType);
10018                         exp.expType = ProcessTypeString(templateString, false);
10019                         FinishTemplatesContext(context);
10020                      }
10021                   }
10022
10023                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
10024                   exp.type = bracketsExp;
10025                   exp.list = MkListOne(MkExpOp(null, '*',
10026                   /*opExp;
10027                   exp.op.op = '*';
10028                   exp.op.exp1 = null;
10029                   exp.op.exp2 = */
10030                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
10031                      MkExpBrackets(MkListOne(
10032                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
10033                            '+',
10034                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
10035                            '+',
10036                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
10037
10038                            ));
10039                }
10040             }
10041             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
10042                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
10043             {
10044                type = ProcessTemplateParameterType(type.templateParameter);
10045             }
10046          }
10047          // TODO: *** This seems to be where we should add method support for all basic types ***
10048          if(type && (type.kind == templateType));
10049          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
10050                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
10051                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
10052                           (type.kind == pointerType && type.type.kind == charType)))
10053          {
10054             Identifier id = exp.member.member;
10055             TypeKind typeKind = type.kind;
10056             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10057             if(typeKind == subClassType && exp.member.exp.type == classExp)
10058             {
10059                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10060                typeKind = classType;
10061             }
10062
10063             if(id)
10064             {
10065                if(typeKind == intType || typeKind == enumType)
10066                   _class = eSystem_FindClass(privateModule, "int");
10067                else if(!_class)
10068                {
10069                   if(type.kind == classType && type._class && type._class.registered)
10070                   {
10071                      _class = type._class.registered;
10072                   }
10073                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10074                   {
10075                      _class = FindClass("char *").registered;
10076                   }
10077                   else if(type.kind == pointerType)
10078                   {
10079                      _class = eSystem_FindClass(privateModule, "uintptr");
10080                      FreeType(exp.expType);
10081                      exp.expType = ProcessTypeString("uintptr", false);
10082                      exp.byReference = true;
10083                   }
10084                   else
10085                   {
10086                      char string[1024] = "";
10087                      Symbol classSym;
10088                      PrintTypeNoConst(type, string, false, true);
10089                      classSym = FindClass(string);
10090                      if(classSym) _class = classSym.registered;
10091                   }
10092                }
10093             }
10094
10095             if(_class && id)
10096             {
10097                /*bool thisPtr =
10098                   (exp.member.exp.type == identifierExp &&
10099                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10100                Property prop = null;
10101                Method method = null;
10102                DataMember member = null;
10103                Property revConvert = null;
10104                ClassProperty classProp = null;
10105
10106                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10107                   exp.member.memberType = propertyMember;
10108
10109                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10110                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10111
10112                if(typeKind != subClassType)
10113                {
10114                   // Prioritize data members over properties for "this"
10115                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10116                   {
10117                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10118                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10119                      {
10120                         prop = eClass_FindProperty(_class, id.string, privateModule);
10121                         if(prop)
10122                            member = null;
10123                      }
10124                      if(!member && !prop)
10125                         prop = eClass_FindProperty(_class, id.string, privateModule);
10126                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10127                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10128                         exp.member.thisPtr = true;
10129                   }
10130                   // Prioritize properties over data members otherwise
10131                   else
10132                   {
10133                      bool useMemberForNonConst = false;
10134                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10135                      if(!id.classSym)
10136                      {
10137                         prop = eClass_FindProperty(_class, id.string, null);
10138
10139                         useMemberForNonConst = prop && exp.destType &&
10140                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10141                               !strncmp(prop.dataTypeString, "const ", 6);
10142
10143                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10144                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10145                      }
10146
10147                      if((!prop || useMemberForNonConst) && !member)
10148                      {
10149                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10150                         if(!method)
10151                         {
10152                            prop = eClass_FindProperty(_class, id.string, privateModule);
10153
10154                            useMemberForNonConst |= prop && exp.destType &&
10155                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10156                                  !strncmp(prop.dataTypeString, "const ", 6);
10157
10158                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10159                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10160                         }
10161                      }
10162
10163                      if(member && prop)
10164                      {
10165                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10166                            prop = null;
10167                         else
10168                            member = null;
10169                      }
10170                   }
10171                }
10172                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10173                   method = eClass_FindMethod(_class, id.string, privateModule);
10174                if(!prop && !member && !method)
10175                {
10176                   if(typeKind == subClassType)
10177                   {
10178                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10179                      if(classProp)
10180                      {
10181                         exp.member.memberType = classPropertyMember;
10182                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10183                      }
10184                      else
10185                      {
10186                         // Assume this is a class_data member
10187                         char structName[1024];
10188                         Identifier id = exp.member.member;
10189                         Expression classExp = exp.member.exp;
10190                         type.refCount++;
10191
10192                         FreeType(classExp.expType);
10193                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10194
10195                         strcpy(structName, "__ecereClassData_");
10196                         FullClassNameCat(structName, type._class.string, false);
10197                         exp.type = pointerExp;
10198                         exp.member.member = id;
10199
10200                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10201                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10202                               MkExpBrackets(MkListOne(MkExpOp(
10203                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10204                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10205                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10206                                  )));
10207
10208                         FreeType(type);
10209
10210                         ProcessExpressionType(exp);
10211                         return;
10212                      }
10213                   }
10214                   else
10215                   {
10216                      // Check for reverse conversion
10217                      // (Convert in an instantiation later, so that we can use
10218                      //  deep properties system)
10219                      Symbol classSym = FindClass(id.string);
10220                      if(classSym)
10221                      {
10222                         Class convertClass = classSym.registered;
10223                         if(convertClass)
10224                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10225                      }
10226                   }
10227                }
10228
10229                //if(!exp.member.exp.destType)
10230                if(exp.member.exp.destType)
10231                   FreeType(exp.member.exp.destType);
10232                {
10233                   if(method && !method._class.symbol)
10234                      method._class.symbol = FindClass(method._class.fullName);
10235                   if(prop && !prop._class.symbol)
10236                      prop._class.symbol = FindClass(prop._class.fullName);
10237
10238                   exp.member.exp.destType = Type
10239                   {
10240                      refCount = 1;
10241                      kind = classType;
10242                      _class = prop ? prop._class.symbol : method ? method._class.symbol : _class.symbol;
10243                      // wasThisClass = type ? type.wasThisClass : false;
10244                   };
10245                }
10246
10247                if(prop)
10248                {
10249                   exp.member.memberType = propertyMember;
10250                   if(!prop.dataType)
10251                      ProcessPropertyType(prop);
10252                   exp.expType = prop.dataType;
10253                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10254                   {
10255                      Type type { };
10256                      CopyTypeInto(type, exp.expType);
10257                      type.refCount = 1;
10258                      type.constant = true;
10259                      exp.expType = type;
10260                   }
10261                   else if(prop.dataType)
10262                      prop.dataType.refCount++;
10263                }
10264                else if(member)
10265                {
10266                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10267                   {
10268                      FreeExpContents(exp);
10269                      exp.type = identifierExp;
10270                      exp.identifier = MkIdentifier("class");
10271                      ProcessExpressionType(exp);
10272                      return;
10273                   }
10274
10275                   exp.member.memberType = dataMember;
10276                   DeclareStruct(_class.fullName, false);
10277                   if(!member.dataType)
10278                   {
10279                      Context context = SetupTemplatesContext(_class);
10280                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10281                      FinishTemplatesContext(context);
10282                   }
10283                   exp.expType = member.dataType;
10284                   if(member.dataType) member.dataType.refCount++;
10285                }
10286                else if(revConvert)
10287                {
10288                   exp.member.memberType = reverseConversionMember;
10289                   exp.expType = MkClassType(revConvert._class.fullName);
10290                }
10291                else if(method)
10292                {
10293                   //if(inCompiler)
10294                   {
10295                      /*if(id._class)
10296                      {
10297                         exp.type = identifierExp;
10298                         exp.identifier = exp.member.member;
10299                      }
10300                      else*/
10301                         exp.member.memberType = methodMember;
10302                   }
10303                   if(!method.dataType)
10304                      ProcessMethodType(method);
10305                   exp.expType = Type
10306                   {
10307                      refCount = 1;
10308                      kind = methodType;
10309                      method = method;
10310                   };
10311
10312                   // Tricky spot here... To use instance versus class virtual table
10313                   // Put it back to what it was... What did we break?
10314
10315                   // Had to put it back for overriding Main of Thread global instance
10316
10317                   //exp.expType.methodClass = _class;
10318                   exp.expType.methodClass = (id && id._class) ? _class : null;
10319
10320                   // Need the actual class used for templated classes
10321                   exp.expType.usedClass = _class;
10322                }
10323                else if(!classProp)
10324                {
10325                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10326                   {
10327                      FreeExpContents(exp);
10328                      exp.type = identifierExp;
10329                      exp.identifier = MkIdentifier("class");
10330                      FreeType(exp.expType);
10331                      exp.expType = MkClassType("ecere::com::Class");
10332                      return;
10333                   }
10334                   yylloc = exp.member.member.loc;
10335                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10336                   if(inCompiler)
10337                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10338                }
10339
10340                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10341                {
10342                   Class tClass;
10343
10344                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10345                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10346
10347                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10348                   {
10349                      int id = 0;
10350                      ClassTemplateParameter curParam = null;
10351                      Class sClass;
10352
10353                      for(sClass = tClass; sClass; sClass = sClass.base)
10354                      {
10355                         id = 0;
10356                         if(sClass.templateClass) sClass = sClass.templateClass;
10357                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10358                         {
10359                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10360                            {
10361                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10362                                  id += sClass.templateParams.count;
10363                               break;
10364                            }
10365                            id++;
10366                         }
10367                         if(curParam) break;
10368                      }
10369
10370                      if(curParam && tClass.templateArgs[id].dataTypeString)
10371                      {
10372                         ClassTemplateArgument arg = tClass.templateArgs[id];
10373                         Context context = SetupTemplatesContext(tClass);
10374                         bool constant = exp.expType.constant;
10375                         /*if(!arg.dataType)
10376                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10377                         FreeType(exp.expType);
10378
10379                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10380                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10381                         else if(exp.expType.kind == pointerType)
10382                         {
10383                            Type t = exp.expType.type;
10384                            while(t.kind == pointerType) t = t.type;
10385                            if(constant) t.constant = constant;
10386                         }
10387                         if(exp.expType)
10388                         {
10389                            if(exp.expType.kind == thisClassType)
10390                            {
10391                               FreeType(exp.expType);
10392                               exp.expType = ReplaceThisClassType(_class);
10393                            }
10394
10395                            if(tClass.templateClass && (exp.expType.kind != templateType || (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType))))
10396                               exp.expType.passAsTemplate = true;
10397                            //exp.expType.refCount++;
10398                            if(!exp.destType)
10399                            {
10400                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10401                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10402                               else if(exp.destType.kind == pointerType)
10403                               {
10404                                  Type t = exp.destType.type;
10405                                  while(t.kind == pointerType) t = t.type;
10406                                  if(constant) t.constant = constant;
10407                               }
10408
10409                               //exp.destType.refCount++;
10410
10411                               if(exp.destType.kind == thisClassType)
10412                               {
10413                                  FreeType(exp.destType);
10414                                  exp.destType = ReplaceThisClassType(_class);
10415                               }
10416                            }
10417                         }
10418                         FinishTemplatesContext(context);
10419                      }
10420                   }
10421                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10422                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10423                   {
10424                      int id = 0;
10425                      ClassTemplateParameter curParam = null;
10426                      Class sClass;
10427
10428                      for(sClass = tClass; sClass; sClass = sClass.base)
10429                      {
10430                         id = 0;
10431                         if(sClass.templateClass) sClass = sClass.templateClass;
10432                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10433                         {
10434                            if(curParam.type == TemplateParameterType::type &&
10435                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10436                            {
10437                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10438                                  id += sClass.templateParams.count;
10439                               break;
10440                            }
10441                            id++;
10442                         }
10443                         if(curParam) break;
10444                      }
10445
10446                      if(curParam)
10447                      {
10448                         ClassTemplateArgument arg = tClass.templateArgs[id];
10449                         Context context = SetupTemplatesContext(tClass);
10450                         Type basicType;
10451                         /*if(!arg.dataType)
10452                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10453
10454                         basicType = ProcessTypeString(arg.dataTypeString, false);
10455                         if(basicType)
10456                         {
10457                            if(basicType.kind == thisClassType)
10458                            {
10459                               FreeType(basicType);
10460                               basicType = ReplaceThisClassType(_class);
10461                            }
10462
10463                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10464                            if(tClass.templateClass)
10465                               basicType.passAsTemplate = true;
10466                            */
10467
10468                            FreeType(exp.expType);
10469
10470                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10471                            //exp.expType.refCount++;
10472                            if(!exp.destType)
10473                            {
10474                               exp.destType = exp.expType;
10475                               exp.destType.refCount++;
10476                            }
10477
10478                            {
10479                               Expression newExp { };
10480                               OldList * specs = MkList();
10481                               Declarator decl;
10482                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10483                               *newExp = *exp;
10484                               if(exp.destType) exp.destType.refCount++;
10485                               if(exp.expType)  exp.expType.refCount++;
10486                               exp.type = castExp;
10487                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10488                               exp.cast.exp = newExp;
10489                               //FreeType(exp.expType);
10490                               //exp.expType = null;
10491                               //ProcessExpressionType(sourceExp);
10492                            }
10493                         }
10494                         FinishTemplatesContext(context);
10495                      }
10496                   }
10497                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10498                   {
10499                      Class expClass = exp.expType._class.registered;
10500                      if(expClass)
10501                      {
10502                         Class cClass = null;
10503                         int p = 0;
10504                         int paramCount = 0;
10505                         int lastParam = -1;
10506                         char templateString[1024];
10507                         ClassTemplateParameter param;
10508                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10509                         while(cClass != expClass)
10510                         {
10511                            Class sClass;
10512                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10513                            cClass = sClass;
10514
10515                            for(param = cClass.templateParams.first; param; param = param.next)
10516                            {
10517                               Class cClassCur = null;
10518                               int cp = 0;
10519                               ClassTemplateParameter paramCur = null;
10520                               ClassTemplateArgument arg;
10521                               while(cClassCur != tClass && !paramCur)
10522                               {
10523                                  Class sClassCur;
10524                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10525                                  cClassCur = sClassCur;
10526
10527                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10528                                  {
10529                                     if(!strcmp(paramCur.name, param.name))
10530                                     {
10531
10532                                        break;
10533                                     }
10534                                     cp++;
10535                                  }
10536                               }
10537                               if(paramCur && paramCur.type == TemplateParameterType::type)
10538                                  arg = tClass.templateArgs[cp];
10539                               else
10540                                  arg = expClass.templateArgs[p];
10541
10542                               {
10543                                  char argument[256];
10544                                  argument[0] = '\0';
10545                                  /*if(arg.name)
10546                                  {
10547                                     strcat(argument, arg.name.string);
10548                                     strcat(argument, " = ");
10549                                  }*/
10550                                  switch(param.type)
10551                                  {
10552                                     case expression:
10553                                     {
10554                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10555                                        char expString[1024];
10556                                        OldList * specs = MkList();
10557                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10558                                        Expression exp;
10559                                        char * string = PrintHexUInt64(arg.expression.ui64);
10560                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10561                                        delete string;
10562
10563                                        ProcessExpressionType(exp);
10564                                        ComputeExpression(exp);
10565                                        expString[0] = '\0';
10566                                        PrintExpression(exp, expString);
10567                                        strcat(argument, expString);
10568                                        // delete exp;
10569                                        FreeExpression(exp);
10570                                        break;
10571                                     }
10572                                     case identifier:
10573                                     {
10574                                        strcat(argument, arg.member.name);
10575                                        break;
10576                                     }
10577                                     case TemplateParameterType::type:
10578                                     {
10579                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10580                                           strcat(argument, arg.dataTypeString);
10581                                        break;
10582                                     }
10583                                  }
10584                                  if(argument[0])
10585                                  {
10586                                     if(paramCount) strcat(templateString, ", ");
10587                                     if(lastParam != p - 1)
10588                                     {
10589                                        strcat(templateString, param.name);
10590                                        strcat(templateString, " = ");
10591                                     }
10592                                     strcat(templateString, argument);
10593                                     paramCount++;
10594                                     lastParam = p;
10595                                  }
10596                               }
10597                               p++;
10598                            }
10599                         }
10600                         {
10601                            int len = strlen(templateString);
10602                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10603                            templateString[len++] = '>';
10604                            templateString[len++] = '\0';
10605                         }
10606
10607                         FreeType(exp.expType);
10608                         {
10609                            Context context = SetupTemplatesContext(tClass);
10610                            exp.expType = ProcessTypeString(templateString, false);
10611                            FinishTemplatesContext(context);
10612                         }
10613                      }
10614                   }
10615                }
10616             }
10617             else
10618                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10619          }
10620          else if(type && (type.kind == structType || type.kind == unionType))
10621          {
10622             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10623             if(memberType)
10624             {
10625                exp.expType = memberType;
10626                if(memberType)
10627                   memberType.refCount++;
10628             }
10629          }
10630          else
10631          {
10632             char expString[10240];
10633             expString[0] = '\0';
10634             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10635             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10636          }
10637
10638          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10639          {
10640             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10641             {
10642                Identifier id = exp.member.member;
10643                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10644                if(_class)
10645                {
10646                   FreeType(exp.expType);
10647                   exp.expType = ReplaceThisClassType(_class);
10648                }
10649             }
10650          }
10651          yylloc = oldyylloc;
10652          break;
10653       }
10654       // Convert x->y into (*x).y
10655       case pointerExp:
10656       {
10657          Type destType = exp.destType;
10658
10659          // DOING THIS LATER NOW...
10660          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10661          {
10662             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10663             /* TODO: Name Space Fix ups
10664             if(!exp.member.member.classSym)
10665                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10666             */
10667          }
10668
10669          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10670          exp.type = memberExp;
10671          if(destType)
10672             destType.count++;
10673          ProcessExpressionType(exp);
10674          if(destType)
10675             destType.count--;
10676          break;
10677       }
10678       case classSizeExp:
10679       {
10680          //ComputeExpression(exp);
10681
10682          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10683          if(classSym && classSym.registered)
10684          {
10685             if(classSym.registered.type == noHeadClass)
10686             {
10687                char name[1024];
10688                name[0] = '\0';
10689                DeclareStruct(classSym.string, false);
10690                FreeSpecifier(exp._class);
10691                exp.type = typeSizeExp;
10692                FullClassNameCat(name, classSym.string, false);
10693                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10694             }
10695             else
10696             {
10697                if(classSym.registered.fixed)
10698                {
10699                   FreeSpecifier(exp._class);
10700                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10701                   exp.type = constantExp;
10702                }
10703                else
10704                {
10705                   char className[1024];
10706                   strcpy(className, "__ecereClass_");
10707                   FullClassNameCat(className, classSym.string, true);
10708                   //MangleClassName(className);
10709
10710                   DeclareClass(classSym, className);
10711
10712                   FreeExpContents(exp);
10713                   exp.type = pointerExp;
10714                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10715                   exp.member.member = MkIdentifier("structSize");
10716                }
10717             }
10718          }
10719
10720          exp.expType = Type
10721          {
10722             refCount = 1;
10723             kind = intSizeType;
10724          };
10725          // exp.isConstant = true;
10726          break;
10727       }
10728       case typeSizeExp:
10729       {
10730          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10731
10732          exp.expType = Type
10733          {
10734             refCount = 1;
10735             kind = intSizeType;
10736          };
10737          exp.isConstant = true;
10738
10739          DeclareType(type, false, false);
10740          FreeType(type);
10741          break;
10742       }
10743       case castExp:
10744       {
10745          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10746          type.count = 1;
10747          FreeType(exp.cast.exp.destType);
10748          exp.cast.exp.destType = type;
10749          type.refCount++;
10750          type.casted = true;
10751          ProcessExpressionType(exp.cast.exp);
10752          type.casted = false;
10753          type.count = 0;
10754          exp.expType = type;
10755          //type.refCount++;
10756
10757          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10758          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10759          {
10760             void * prev = exp.prev, * next = exp.next;
10761             Type expType = exp.cast.exp.destType;
10762             Expression castExp = exp.cast.exp;
10763             Type destType = exp.destType;
10764
10765             if(expType) expType.refCount++;
10766
10767             //FreeType(exp.destType);
10768             FreeType(exp.expType);
10769             FreeTypeName(exp.cast.typeName);
10770
10771             *exp = *castExp;
10772             FreeType(exp.expType);
10773             FreeType(exp.destType);
10774
10775             exp.expType = expType;
10776             exp.destType = destType;
10777
10778             delete castExp;
10779
10780             exp.prev = prev;
10781             exp.next = next;
10782
10783          }
10784          else
10785          {
10786             exp.isConstant = exp.cast.exp.isConstant;
10787          }
10788          //FreeType(type);
10789          break;
10790       }
10791       case extensionInitializerExp:
10792       {
10793          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10794          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10795          // ProcessInitializer(exp.initializer.initializer, type);
10796          exp.expType = type;
10797          break;
10798       }
10799       case vaArgExp:
10800       {
10801          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10802          ProcessExpressionType(exp.vaArg.exp);
10803          exp.expType = type;
10804          break;
10805       }
10806       case conditionExp:
10807       {
10808          Expression e;
10809          exp.isConstant = true;
10810
10811          FreeType(exp.cond.cond.destType);
10812          exp.cond.cond.destType = MkClassType("bool");
10813          exp.cond.cond.destType.truth = true;
10814          ProcessExpressionType(exp.cond.cond);
10815          if(!exp.cond.cond.isConstant)
10816             exp.isConstant = false;
10817          for(e = exp.cond.exp->first; e; e = e.next)
10818          {
10819             if(!e.next)
10820             {
10821                FreeType(e.destType);
10822                e.destType = exp.destType;
10823                if(e.destType) e.destType.refCount++;
10824             }
10825             ProcessExpressionType(e);
10826             if(!e.next)
10827             {
10828                exp.expType = e.expType;
10829                if(e.expType) e.expType.refCount++;
10830             }
10831             if(!e.isConstant)
10832                exp.isConstant = false;
10833          }
10834
10835          FreeType(exp.cond.elseExp.destType);
10836          // Added this check if we failed to find an expType
10837          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10838
10839          // Reversed it...
10840          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10841
10842          if(exp.cond.elseExp.destType)
10843             exp.cond.elseExp.destType.refCount++;
10844          ProcessExpressionType(exp.cond.elseExp);
10845
10846          // FIXED THIS: Was done before calling process on elseExp
10847          if(!exp.cond.elseExp.isConstant)
10848             exp.isConstant = false;
10849          break;
10850       }
10851       case extensionCompoundExp:
10852       {
10853          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10854          {
10855             Statement last = exp.compound.compound.statements->last;
10856             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10857             {
10858                ((Expression)last.expressions->last).destType = exp.destType;
10859                if(exp.destType)
10860                   exp.destType.refCount++;
10861             }
10862             ProcessStatement(exp.compound);
10863             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10864             if(exp.expType)
10865                exp.expType.refCount++;
10866          }
10867          break;
10868       }
10869       case classExp:
10870       {
10871          Specifier spec = exp._classExp.specifiers->first;
10872          if(spec && spec.type == nameSpecifier)
10873          {
10874             exp.expType = MkClassType(spec.name);
10875             exp.expType.kind = subClassType;
10876             exp.byReference = true;
10877          }
10878          else
10879          {
10880             exp.expType = MkClassType("ecere::com::Class");
10881             exp.byReference = true;
10882          }
10883          break;
10884       }
10885       case classDataExp:
10886       {
10887          Class _class = thisClass ? thisClass : currentClass;
10888          if(_class)
10889          {
10890             Identifier id = exp.classData.id;
10891             char structName[1024];
10892             Expression classExp;
10893             strcpy(structName, "__ecereClassData_");
10894             FullClassNameCat(structName, _class.fullName, false);
10895             exp.type = pointerExp;
10896             exp.member.member = id;
10897             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10898                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10899             else
10900                classExp = MkExpIdentifier(MkIdentifier("class"));
10901
10902             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10903                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10904                   MkExpBrackets(MkListOne(MkExpOp(
10905                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10906                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10907                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10908                      )));
10909
10910             ProcessExpressionType(exp);
10911             return;
10912          }
10913          break;
10914       }
10915       case arrayExp:
10916       {
10917          Type type = null;
10918          const char * typeString = null;
10919          char typeStringBuf[1024];
10920          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10921             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10922          {
10923             Class templateClass = exp.destType._class.registered;
10924             typeString = templateClass.templateArgs[2].dataTypeString;
10925          }
10926          else if(exp.list)
10927          {
10928             // Guess type from expressions in the array
10929             Expression e;
10930             for(e = exp.list->first; e; e = e.next)
10931             {
10932                ProcessExpressionType(e);
10933                if(e.expType)
10934                {
10935                   if(!type) { type = e.expType; type.refCount++; }
10936                   else
10937                   {
10938                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10939                      if(!MatchTypeExpression(e, type, null, false, true))
10940                      {
10941                         FreeType(type);
10942                         type = e.expType;
10943                         e.expType = null;
10944
10945                         e = exp.list->first;
10946                         ProcessExpressionType(e);
10947                         if(e.expType)
10948                         {
10949                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10950                            if(!MatchTypeExpression(e, type, null, false, true))
10951                            {
10952                               FreeType(e.expType);
10953                               e.expType = null;
10954                               FreeType(type);
10955                               type = null;
10956                               break;
10957                            }
10958                         }
10959                      }
10960                   }
10961                   if(e.expType)
10962                   {
10963                      FreeType(e.expType);
10964                      e.expType = null;
10965                   }
10966                }
10967             }
10968             if(type)
10969             {
10970                typeStringBuf[0] = '\0';
10971                PrintTypeNoConst(type, typeStringBuf, false, true);
10972                typeString = typeStringBuf;
10973                FreeType(type);
10974                type = null;
10975             }
10976          }
10977          if(typeString)
10978          {
10979             /*
10980             (Container)& (struct BuiltInContainer)
10981             {
10982                ._vTbl = class(BuiltInContainer)._vTbl,
10983                ._class = class(BuiltInContainer),
10984                .refCount = 0,
10985                .data = (int[]){ 1, 7, 3, 4, 5 },
10986                .count = 5,
10987                .type = class(int),
10988             }
10989             */
10990             char templateString[1024];
10991             OldList * initializers = MkList();
10992             OldList * structInitializers = MkList();
10993             OldList * specs = MkList();
10994             Expression expExt;
10995             Declarator decl = SpecDeclFromString(typeString, specs, null);
10996             sprintf(templateString, "Container<%s>", typeString);
10997
10998             if(exp.list)
10999             {
11000                Expression e;
11001                type = ProcessTypeString(typeString, false);
11002                while((e = exp.list->first))
11003                {
11004                   exp.list->Remove(e);
11005                   e.destType = type;
11006                   type.refCount++;
11007                   ProcessExpressionType(e);
11008                   ListAdd(initializers, MkInitializerAssignment(e));
11009                }
11010                FreeType(type);
11011                delete exp.list;
11012             }
11013
11014             DeclareStruct("ecere::com::BuiltInContainer", false);
11015
11016             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
11017                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11018             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
11019                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11020             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
11021                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11022             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
11023                MkTypeName(specs, MkDeclaratorArray(decl, null)),
11024                MkInitializerList(initializers))));
11025                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11026             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
11027                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11028             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
11029                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11030             exp.expType = ProcessTypeString(templateString, false);
11031             exp.type = bracketsExp;
11032             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
11033                MkExpOp(null, '&',
11034                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
11035                   MkInitializerList(structInitializers)))));
11036             ProcessExpressionType(expExt);
11037          }
11038          else
11039          {
11040             exp.expType = ProcessTypeString("Container", false);
11041             Compiler_Error($"Couldn't determine type of array elements\n");
11042          }
11043          break;
11044       }
11045    }
11046
11047    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
11048    {
11049       FreeType(exp.expType);
11050       exp.expType = ReplaceThisClassType(thisClass);
11051    }
11052
11053    // Resolve structures here
11054    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
11055    {
11056       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
11057       // TODO: Fix members reference...
11058       if(symbol)
11059       {
11060          if(exp.expType.kind != enumType)
11061          {
11062             Type member;
11063             String enumName = CopyString(exp.expType.enumName);
11064
11065             // Fixed a memory leak on self-referencing C structs typedefs
11066             // by instantiating a new type rather than simply copying members
11067             // into exp.expType
11068             FreeType(exp.expType);
11069             exp.expType = Type { };
11070             exp.expType.kind = symbol.type.kind;
11071             exp.expType.refCount++;
11072             exp.expType.enumName = enumName;
11073
11074             exp.expType.members = symbol.type.members;
11075             for(member = symbol.type.members.first; member; member = member.next)
11076                member.refCount++;
11077          }
11078          else
11079          {
11080             NamedLink64 member;
11081             for(member = symbol.type.members.first; member; member = member.next)
11082             {
11083                NamedLink64 value { name = CopyString(member.name) };
11084                exp.expType.members.Add(value);
11085             }
11086          }
11087       }
11088    }
11089
11090    yylloc = exp.loc;
11091    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11092    else if(exp.destType && !exp.destType.keepCast)
11093    {
11094       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11095          exp.needTemplateCast = 1;
11096
11097       if(exp.destType.kind == voidType);
11098       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11099       {
11100          if(!exp.destType.count || unresolved)
11101          {
11102             if(!exp.expType)
11103             {
11104                yylloc = exp.loc;
11105                if(exp.destType.kind != ellipsisType)
11106                {
11107                   char type2[1024];
11108                   type2[0] = '\0';
11109                   if(inCompiler)
11110                   {
11111                      char expString[10240];
11112                      expString[0] = '\0';
11113
11114                      PrintType(exp.destType, type2, false, true);
11115
11116                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11117                      if(unresolved)
11118                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11119                      else if(exp.type != dummyExp)
11120                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11121                   }
11122                }
11123                else
11124                {
11125                   char expString[10240] ;
11126                   expString[0] = '\0';
11127                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11128
11129                   if(unresolved)
11130                      Compiler_Error($"unresolved identifier %s\n", expString);
11131                   else if(exp.type != dummyExp)
11132                      Compiler_Error($"couldn't determine type of %s\n", expString);
11133                }
11134             }
11135             else
11136             {
11137                char type1[1024];
11138                char type2[1024];
11139                type1[0] = '\0';
11140                type2[0] = '\0';
11141                if(inCompiler)
11142                {
11143                   PrintType(exp.expType, type1, false, true);
11144                   PrintType(exp.destType, type2, false, true);
11145                }
11146
11147                //CheckExpressionType(exp, exp.destType, false);
11148
11149                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11150                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11151                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11152                else
11153                {
11154                   char expString[10240];
11155                   expString[0] = '\0';
11156                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11157
11158 #ifdef _DEBUG
11159                   CheckExpressionType(exp, exp.destType, false, true);
11160 #endif
11161                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11162                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
11163                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11164
11165                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11166                   FreeType(exp.expType);
11167                   exp.destType.refCount++;
11168                   exp.expType = exp.destType;
11169                }
11170             }
11171          }
11172       }
11173       /*else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
11174       {
11175          Expression newExp { };
11176          char typeString[1024];
11177          OldList * specs = MkList();
11178          Declarator decl;
11179
11180          typeString[0] = '\0';
11181
11182          *newExp = *exp;
11183
11184          if(exp.expType)  exp.expType.refCount++;
11185          if(exp.expType)  exp.expType.refCount++;
11186          exp.type = castExp;
11187          newExp.destType = exp.expType;
11188
11189          PrintType(exp.expType, typeString, false, false);
11190          decl = SpecDeclFromString(typeString, specs, null);
11191
11192          exp.cast.typeName = MkTypeName(specs, decl);
11193          exp.cast.exp = newExp;
11194       }*/
11195    }
11196    else if(unresolved)
11197    {
11198       if(exp.identifier._class && exp.identifier._class.name)
11199          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11200       else if(exp.identifier.string && exp.identifier.string[0])
11201          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11202    }
11203    else if(!exp.expType && exp.type != dummyExp)
11204    {
11205       char expString[10240];
11206       expString[0] = '\0';
11207       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11208       Compiler_Error($"couldn't determine type of %s\n", expString);
11209    }
11210
11211    // Let's try to support any_object & typed_object here:
11212    if(inCompiler)
11213       ApplyAnyObjectLogic(exp);
11214
11215    // Mark nohead classes as by reference, unless we're casting them to an integral type
11216    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11217       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11218          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11219           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11220    {
11221       exp.byReference = true;
11222    }
11223    yylloc = oldyylloc;
11224 }
11225
11226 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11227 {
11228    // THIS CODE WILL FIND NEXT MEMBER...
11229    if(*curMember)
11230    {
11231       *curMember = (*curMember).next;
11232
11233       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11234       {
11235          *curMember = subMemberStack[--(*subMemberStackPos)];
11236          *curMember = (*curMember).next;
11237       }
11238
11239       // SKIP ALL PROPERTIES HERE...
11240       while((*curMember) && (*curMember).isProperty)
11241          *curMember = (*curMember).next;
11242
11243       if(subMemberStackPos)
11244       {
11245          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11246          {
11247             subMemberStack[(*subMemberStackPos)++] = *curMember;
11248
11249             *curMember = (*curMember).members.first;
11250             while(*curMember && (*curMember).isProperty)
11251                *curMember = (*curMember).next;
11252          }
11253       }
11254    }
11255    while(!*curMember)
11256    {
11257       if(!*curMember)
11258       {
11259          if(subMemberStackPos && *subMemberStackPos)
11260          {
11261             *curMember = subMemberStack[--(*subMemberStackPos)];
11262             *curMember = (*curMember).next;
11263          }
11264          else
11265          {
11266             Class lastCurClass = *curClass;
11267
11268             if(*curClass == _class) break;     // REACHED THE END
11269
11270             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11271             *curMember = (*curClass).membersAndProperties.first;
11272          }
11273
11274          while((*curMember) && (*curMember).isProperty)
11275             *curMember = (*curMember).next;
11276          if(subMemberStackPos)
11277          {
11278             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11279             {
11280                subMemberStack[(*subMemberStackPos)++] = *curMember;
11281
11282                *curMember = (*curMember).members.first;
11283                while(*curMember && (*curMember).isProperty)
11284                   *curMember = (*curMember).next;
11285             }
11286          }
11287       }
11288    }
11289 }
11290
11291
11292 static void ProcessInitializer(Initializer init, Type type)
11293 {
11294    switch(init.type)
11295    {
11296       case expInitializer:
11297          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11298          {
11299             // TESTING THIS FOR SHUTTING = 0 WARNING
11300             if(init.exp && !init.exp.destType)
11301             {
11302                FreeType(init.exp.destType);
11303                init.exp.destType = type;
11304                if(type) type.refCount++;
11305             }
11306             if(init.exp)
11307             {
11308                ProcessExpressionType(init.exp);
11309                init.isConstant = init.exp.isConstant;
11310             }
11311             break;
11312          }
11313          else
11314          {
11315             Expression exp = init.exp;
11316             Instantiation inst = exp.instance;
11317             MembersInit members;
11318
11319             init.type = listInitializer;
11320             init.list = MkList();
11321
11322             if(inst.members)
11323             {
11324                for(members = inst.members->first; members; members = members.next)
11325                {
11326                   if(members.type == dataMembersInit)
11327                   {
11328                      MemberInit member;
11329                      for(member = members.dataMembers->first; member; member = member.next)
11330                      {
11331                         ListAdd(init.list, member.initializer);
11332                         member.initializer = null;
11333                      }
11334                   }
11335                   // Discard all MembersInitMethod
11336                }
11337             }
11338             FreeExpression(exp);
11339          }
11340       case listInitializer:
11341       {
11342          Initializer i;
11343          Type initializerType = null;
11344          Class curClass = null;
11345          DataMember curMember = null;
11346          DataMember subMemberStack[256];
11347          int subMemberStackPos = 0;
11348
11349          if(type && type.kind == arrayType)
11350             initializerType = Dereference(type);
11351          else if(type && (type.kind == structType || type.kind == unionType))
11352             initializerType = type.members.first;
11353
11354          for(i = init.list->first; i; i = i.next)
11355          {
11356             if(type && type.kind == classType && type._class && type._class.registered)
11357             {
11358                // 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)
11359                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11360                // TODO: Generate error on initializing a private data member this way from another module...
11361                if(curMember)
11362                {
11363                   if(!curMember.dataType)
11364                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11365                   initializerType = curMember.dataType;
11366                }
11367             }
11368             ProcessInitializer(i, initializerType);
11369             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11370                initializerType = initializerType.next;
11371             if(!i.isConstant)
11372                init.isConstant = false;
11373          }
11374
11375          if(type && type.kind == arrayType)
11376             FreeType(initializerType);
11377
11378          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11379          {
11380             Compiler_Error($"Assigning list initializer to non list\n");
11381          }
11382          break;
11383       }
11384    }
11385 }
11386
11387 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11388 {
11389    switch(spec.type)
11390    {
11391       case baseSpecifier:
11392       {
11393          if(spec.specifier == THISCLASS)
11394          {
11395             if(thisClass)
11396             {
11397                spec.type = nameSpecifier;
11398                spec.name = ReplaceThisClass(thisClass);
11399                spec.symbol = FindClass(spec.name);
11400                ProcessSpecifier(spec, declareStruct);
11401             }
11402          }
11403          break;
11404       }
11405       case nameSpecifier:
11406       {
11407          Symbol symbol = FindType(curContext, spec.name);
11408          if(symbol)
11409             DeclareType(symbol.type, true, true);
11410          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11411             DeclareStruct(spec.name, false);
11412          break;
11413       }
11414       case enumSpecifier:
11415       {
11416          Enumerator e;
11417          if(spec.list)
11418          {
11419             for(e = spec.list->first; e; e = e.next)
11420             {
11421                if(e.exp)
11422                   ProcessExpressionType(e.exp);
11423             }
11424          }
11425          break;
11426       }
11427       case structSpecifier:
11428       case unionSpecifier:
11429       {
11430          if(spec.definitions)
11431          {
11432             //ClassDef def;
11433             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11434             //if(symbol)
11435                ProcessClass(spec.definitions, symbol);
11436             /*else
11437             {
11438                for(def = spec.definitions->first; def; def = def.next)
11439                {
11440                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11441                      ProcessDeclaration(def.decl);
11442                }
11443             }*/
11444          }
11445          break;
11446       }
11447       /*
11448       case classSpecifier:
11449       {
11450          Symbol classSym = FindClass(spec.name);
11451          if(classSym && classSym.registered && classSym.registered.type == structClass)
11452             DeclareStruct(spec.name, false);
11453          break;
11454       }
11455       */
11456    }
11457 }
11458
11459
11460 static void ProcessDeclarator(Declarator decl)
11461 {
11462    switch(decl.type)
11463    {
11464       case identifierDeclarator:
11465          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11466          {
11467             FreeSpecifier(decl.identifier._class);
11468             decl.identifier._class = null;
11469          }
11470          break;
11471       case arrayDeclarator:
11472          if(decl.array.exp)
11473             ProcessExpressionType(decl.array.exp);
11474       case structDeclarator:
11475       case bracketsDeclarator:
11476       case functionDeclarator:
11477       case pointerDeclarator:
11478       case extendedDeclarator:
11479       case extendedDeclaratorEnd:
11480          if(decl.declarator)
11481             ProcessDeclarator(decl.declarator);
11482          if(decl.type == functionDeclarator)
11483          {
11484             Identifier id = GetDeclId(decl);
11485             if(id && id._class)
11486             {
11487                TypeName param
11488                {
11489                   qualifiers = MkListOne(id._class);
11490                   declarator = null;
11491                };
11492                if(!decl.function.parameters)
11493                   decl.function.parameters = MkList();
11494                decl.function.parameters->Insert(null, param);
11495                id._class = null;
11496             }
11497             if(decl.function.parameters)
11498             {
11499                TypeName param;
11500
11501                for(param = decl.function.parameters->first; param; param = param.next)
11502                {
11503                   if(param.qualifiers && param.qualifiers->first)
11504                   {
11505                      Specifier spec = param.qualifiers->first;
11506                      if(spec && spec.specifier == TYPED_OBJECT)
11507                      {
11508                         Declarator d = param.declarator;
11509                         TypeName newParam
11510                         {
11511                            qualifiers = MkListOne(MkSpecifier(VOID));
11512                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11513                         };
11514                         if(d.type != pointerDeclarator)
11515                            newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11516
11517                         FreeList(param.qualifiers, FreeSpecifier);
11518
11519                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11520                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11521
11522                         decl.function.parameters->Insert(param, newParam);
11523                         param = newParam;
11524                      }
11525                      else if(spec && spec.specifier == ANY_OBJECT)
11526                      {
11527                         Declarator d = param.declarator;
11528
11529                         FreeList(param.qualifiers, FreeSpecifier);
11530
11531                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11532                         if(d.type != pointerDeclarator)
11533                            param.qualifiers->Insert(null, MkSpecifier(CONST));
11534                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11535                      }
11536                      else if(spec.specifier == THISCLASS)
11537                      {
11538                         if(thisClass)
11539                         {
11540                            spec.type = nameSpecifier;
11541                            spec.name = ReplaceThisClass(thisClass);
11542                            spec.symbol = FindClass(spec.name);
11543                            ProcessSpecifier(spec, false);
11544                         }
11545                      }
11546                   }
11547
11548                   if(param.declarator)
11549                      ProcessDeclarator(param.declarator);
11550                }
11551             }
11552          }
11553          break;
11554    }
11555 }
11556
11557 static void ProcessDeclaration(Declaration decl)
11558 {
11559    yylloc = decl.loc;
11560    switch(decl.type)
11561    {
11562       case initDeclaration:
11563       {
11564          bool declareStruct = false;
11565          /*
11566          lineNum = decl.pos.line;
11567          column = decl.pos.col;
11568          */
11569
11570          if(decl.declarators)
11571          {
11572             InitDeclarator d;
11573
11574             for(d = decl.declarators->first; d; d = d.next)
11575             {
11576                Type type, subType;
11577                ProcessDeclarator(d.declarator);
11578
11579                type = ProcessType(decl.specifiers, d.declarator);
11580
11581                if(d.initializer)
11582                {
11583                   ProcessInitializer(d.initializer, type);
11584
11585                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11586
11587                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11588                      d.initializer.exp.type == instanceExp)
11589                   {
11590                      if(type.kind == classType && type._class ==
11591                         d.initializer.exp.expType._class)
11592                      {
11593                         Instantiation inst = d.initializer.exp.instance;
11594                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11595
11596                         d.initializer.exp.instance = null;
11597                         if(decl.specifiers)
11598                            FreeList(decl.specifiers, FreeSpecifier);
11599                         FreeList(decl.declarators, FreeInitDeclarator);
11600
11601                         d = null;
11602
11603                         decl.type = instDeclaration;
11604                         decl.inst = inst;
11605                      }
11606                   }
11607                }
11608                for(subType = type; subType;)
11609                {
11610                   if(subType.kind == classType)
11611                   {
11612                      declareStruct = true;
11613                      break;
11614                   }
11615                   else if(subType.kind == pointerType)
11616                      break;
11617                   else if(subType.kind == arrayType)
11618                      subType = subType.arrayType;
11619                   else
11620                      break;
11621                }
11622
11623                FreeType(type);
11624                if(!d) break;
11625             }
11626          }
11627
11628          if(decl.specifiers)
11629          {
11630             Specifier s;
11631             for(s = decl.specifiers->first; s; s = s.next)
11632             {
11633                ProcessSpecifier(s, declareStruct);
11634             }
11635          }
11636          break;
11637       }
11638       case instDeclaration:
11639       {
11640          ProcessInstantiationType(decl.inst);
11641          break;
11642       }
11643       case structDeclaration:
11644       {
11645          Specifier spec;
11646          Declarator d;
11647          bool declareStruct = false;
11648
11649          if(decl.declarators)
11650          {
11651             for(d = decl.declarators->first; d; d = d.next)
11652             {
11653                Type type = ProcessType(decl.specifiers, d.declarator);
11654                Type subType;
11655                ProcessDeclarator(d);
11656                for(subType = type; subType;)
11657                {
11658                   if(subType.kind == classType)
11659                   {
11660                      declareStruct = true;
11661                      break;
11662                   }
11663                   else if(subType.kind == pointerType)
11664                      break;
11665                   else if(subType.kind == arrayType)
11666                      subType = subType.arrayType;
11667                   else
11668                      break;
11669                }
11670                FreeType(type);
11671             }
11672          }
11673          if(decl.specifiers)
11674          {
11675             for(spec = decl.specifiers->first; spec; spec = spec.next)
11676                ProcessSpecifier(spec, declareStruct);
11677          }
11678          break;
11679       }
11680    }
11681 }
11682
11683 static FunctionDefinition curFunction;
11684
11685 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11686 {
11687    char propName[1024], propNameM[1024];
11688    char getName[1024], setName[1024];
11689    OldList * args;
11690
11691    DeclareProperty(prop, setName, getName);
11692
11693    // eInstance_FireWatchers(object, prop);
11694    strcpy(propName, "__ecereProp_");
11695    FullClassNameCat(propName, prop._class.fullName, false);
11696    strcat(propName, "_");
11697    // strcat(propName, prop.name);
11698    FullClassNameCat(propName, prop.name, true);
11699    //MangleClassName(propName);
11700
11701    strcpy(propNameM, "__ecerePropM_");
11702    FullClassNameCat(propNameM, prop._class.fullName, false);
11703    strcat(propNameM, "_");
11704    // strcat(propNameM, prop.name);
11705    FullClassNameCat(propNameM, prop.name, true);
11706    //MangleClassName(propNameM);
11707
11708    if(prop.isWatchable)
11709    {
11710       args = MkList();
11711       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11712       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11713       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11714
11715       args = MkList();
11716       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11717       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11718       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11719    }
11720
11721
11722    {
11723       args = MkList();
11724       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11725       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11726       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11727
11728       args = MkList();
11729       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11730       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11731       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11732    }
11733
11734    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11735       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11736       curFunction.propSet.fireWatchersDone = true;
11737 }
11738
11739 static void ProcessStatement(Statement stmt)
11740 {
11741    yylloc = stmt.loc;
11742    /*
11743    lineNum = stmt.pos.line;
11744    column = stmt.pos.col;
11745    */
11746    switch(stmt.type)
11747    {
11748       case labeledStmt:
11749          ProcessStatement(stmt.labeled.stmt);
11750          break;
11751       case caseStmt:
11752          // This expression should be constant...
11753          if(stmt.caseStmt.exp)
11754          {
11755             FreeType(stmt.caseStmt.exp.destType);
11756             stmt.caseStmt.exp.destType = curSwitchType;
11757             if(curSwitchType) curSwitchType.refCount++;
11758             ProcessExpressionType(stmt.caseStmt.exp);
11759             ComputeExpression(stmt.caseStmt.exp);
11760          }
11761          if(stmt.caseStmt.stmt)
11762             ProcessStatement(stmt.caseStmt.stmt);
11763          break;
11764       case compoundStmt:
11765       {
11766          if(stmt.compound.context)
11767          {
11768             Declaration decl;
11769             Statement s;
11770
11771             Statement prevCompound = curCompound;
11772             Context prevContext = curContext;
11773
11774             if(!stmt.compound.isSwitch)
11775                curCompound = stmt;
11776             curContext = stmt.compound.context;
11777
11778             if(stmt.compound.declarations)
11779             {
11780                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11781                   ProcessDeclaration(decl);
11782             }
11783             if(stmt.compound.statements)
11784             {
11785                for(s = stmt.compound.statements->first; s; s = s.next)
11786                   ProcessStatement(s);
11787             }
11788
11789             curContext = prevContext;
11790             curCompound = prevCompound;
11791          }
11792          break;
11793       }
11794       case expressionStmt:
11795       {
11796          Expression exp;
11797          if(stmt.expressions)
11798          {
11799             for(exp = stmt.expressions->first; exp; exp = exp.next)
11800                ProcessExpressionType(exp);
11801          }
11802          break;
11803       }
11804       case ifStmt:
11805       {
11806          Expression exp;
11807
11808          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11809          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11810          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11811          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11812          {
11813             ProcessExpressionType(exp);
11814          }
11815          if(stmt.ifStmt.stmt)
11816             ProcessStatement(stmt.ifStmt.stmt);
11817          if(stmt.ifStmt.elseStmt)
11818             ProcessStatement(stmt.ifStmt.elseStmt);
11819          break;
11820       }
11821       case switchStmt:
11822       {
11823          Type oldSwitchType = curSwitchType;
11824          if(stmt.switchStmt.exp)
11825          {
11826             Expression exp;
11827             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11828             {
11829                if(!exp.next)
11830                {
11831                   /*
11832                   Type destType
11833                   {
11834                      kind = intType;
11835                      refCount = 1;
11836                   };
11837                   e.exp.destType = destType;
11838                   */
11839
11840                   ProcessExpressionType(exp);
11841                }
11842                if(!exp.next)
11843                   curSwitchType = exp.expType;
11844             }
11845          }
11846          ProcessStatement(stmt.switchStmt.stmt);
11847          curSwitchType = oldSwitchType;
11848          break;
11849       }
11850       case whileStmt:
11851       {
11852          if(stmt.whileStmt.exp)
11853          {
11854             Expression exp;
11855
11856             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11857             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11858             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11859             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11860             {
11861                ProcessExpressionType(exp);
11862             }
11863          }
11864          if(stmt.whileStmt.stmt)
11865             ProcessStatement(stmt.whileStmt.stmt);
11866          break;
11867       }
11868       case doWhileStmt:
11869       {
11870          if(stmt.doWhile.exp)
11871          {
11872             Expression exp;
11873
11874             if(stmt.doWhile.exp->last)
11875             {
11876                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11877                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11878                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11879             }
11880             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11881             {
11882                ProcessExpressionType(exp);
11883             }
11884          }
11885          if(stmt.doWhile.stmt)
11886             ProcessStatement(stmt.doWhile.stmt);
11887          break;
11888       }
11889       case forStmt:
11890       {
11891          Expression exp;
11892          if(stmt.forStmt.init)
11893             ProcessStatement(stmt.forStmt.init);
11894
11895          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11896          {
11897             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11898             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11899             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11900          }
11901
11902          if(stmt.forStmt.check)
11903             ProcessStatement(stmt.forStmt.check);
11904          if(stmt.forStmt.increment)
11905          {
11906             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11907                ProcessExpressionType(exp);
11908          }
11909
11910          if(stmt.forStmt.stmt)
11911             ProcessStatement(stmt.forStmt.stmt);
11912          break;
11913       }
11914       case forEachStmt:
11915       {
11916          Identifier id = stmt.forEachStmt.id;
11917          OldList * exp = stmt.forEachStmt.exp;
11918          OldList * filter = stmt.forEachStmt.filter;
11919          Statement block = stmt.forEachStmt.stmt;
11920          char iteratorType[1024];
11921          Type source;
11922          Expression e;
11923          bool isBuiltin = exp && exp->last &&
11924             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11925               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11926          Expression arrayExp;
11927          const char * typeString = null;
11928          int builtinCount = 0;
11929
11930          for(e = exp ? exp->first : null; e; e = e.next)
11931          {
11932             if(!e.next)
11933             {
11934                FreeType(e.destType);
11935                e.destType = ProcessTypeString("Container", false);
11936             }
11937             if(!isBuiltin || e.next)
11938                ProcessExpressionType(e);
11939          }
11940
11941          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11942          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11943             eClass_IsDerived(source._class.registered, containerClass)))
11944          {
11945             Class _class = source ? source._class.registered : null;
11946             Symbol symbol;
11947             Expression expIt = null;
11948             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
11949             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11950             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11951             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11952             stmt.type = compoundStmt;
11953
11954             stmt.compound.context = Context { };
11955             stmt.compound.context.parent = curContext;
11956             curContext = stmt.compound.context;
11957
11958             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11959             {
11960                Class mapClass = eSystem_FindClass(privateModule, "Map");
11961                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11962                isCustomAVLTree = true;
11963                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
11964                   isAVLTree = true;
11965                else */if(eClass_IsDerived(source._class.registered, mapClass))
11966                   isMap = true;
11967             }
11968             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11969             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11970             {
11971                Class listClass = eSystem_FindClass(privateModule, "List");
11972                isLinkList = true;
11973                isList = eClass_IsDerived(source._class.registered, listClass);
11974             }
11975
11976             if(isArray)
11977             {
11978                Declarator decl;
11979                OldList * specs = MkList();
11980                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11981                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11982                stmt.compound.declarations = MkListOne(
11983                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11984                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11985                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11986                      MkInitializerAssignment(MkExpBrackets(exp))))));
11987             }
11988             else if(isBuiltin)
11989             {
11990                Type type = null;
11991                char typeStringBuf[1024];
11992
11993                // TODO: Merge this code?
11994                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11995                if(((Expression)exp->last).type == castExp)
11996                {
11997                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11998                   if(typeName)
11999                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
12000                }
12001
12002                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
12003                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
12004                   arrayExp.destType._class.registered.templateArgs)
12005                {
12006                   Class templateClass = arrayExp.destType._class.registered;
12007                   typeString = templateClass.templateArgs[2].dataTypeString;
12008                }
12009                else if(arrayExp.list)
12010                {
12011                   // Guess type from expressions in the array
12012                   Expression e;
12013                   for(e = arrayExp.list->first; e; e = e.next)
12014                   {
12015                      ProcessExpressionType(e);
12016                      if(e.expType)
12017                      {
12018                         if(!type) { type = e.expType; type.refCount++; }
12019                         else
12020                         {
12021                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
12022                            if(!MatchTypeExpression(e, type, null, false, true))
12023                            {
12024                               FreeType(type);
12025                               type = e.expType;
12026                               e.expType = null;
12027
12028                               e = arrayExp.list->first;
12029                               ProcessExpressionType(e);
12030                               if(e.expType)
12031                               {
12032                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
12033                                  if(!MatchTypeExpression(e, type, null, false, true))
12034                                  {
12035                                     FreeType(e.expType);
12036                                     e.expType = null;
12037                                     FreeType(type);
12038                                     type = null;
12039                                     break;
12040                                  }
12041                               }
12042                            }
12043                         }
12044                         if(e.expType)
12045                         {
12046                            FreeType(e.expType);
12047                            e.expType = null;
12048                         }
12049                      }
12050                   }
12051                   if(type)
12052                   {
12053                      typeStringBuf[0] = '\0';
12054                      PrintType(type, typeStringBuf, false, true);
12055                      typeString = typeStringBuf;
12056                      FreeType(type);
12057                   }
12058                }
12059                if(typeString)
12060                {
12061                   OldList * initializers = MkList();
12062                   Declarator decl;
12063                   OldList * specs = MkList();
12064                   if(arrayExp.list)
12065                   {
12066                      Expression e;
12067
12068                      builtinCount = arrayExp.list->count;
12069                      type = ProcessTypeString(typeString, false);
12070                      while((e = arrayExp.list->first))
12071                      {
12072                         arrayExp.list->Remove(e);
12073                         e.destType = type;
12074                         type.refCount++;
12075                         ProcessExpressionType(e);
12076                         ListAdd(initializers, MkInitializerAssignment(e));
12077                      }
12078                      FreeType(type);
12079                      delete arrayExp.list;
12080                   }
12081                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12082                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12083                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12084
12085                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12086                      PlugDeclarator(
12087                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12088                         ), MkInitializerList(initializers)))));
12089                   FreeList(exp, FreeExpression);
12090                }
12091                else
12092                {
12093                   arrayExp.expType = ProcessTypeString("Container", false);
12094                   Compiler_Error($"Couldn't determine type of array elements\n");
12095                }
12096
12097                /*
12098                Declarator decl;
12099                OldList * specs = MkList();
12100
12101                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12102                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12103                stmt.compound.declarations = MkListOne(
12104                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12105                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12106                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12107                      MkInitializerAssignment(MkExpBrackets(exp))))));
12108                */
12109             }
12110             else if(isLinkList && !isList)
12111             {
12112                Declarator decl;
12113                OldList * specs = MkList();
12114                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12115                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12116                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12117                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12118                      MkInitializerAssignment(MkExpBrackets(exp))))));
12119             }
12120             /*else if(isCustomAVLTree)
12121             {
12122                Declarator decl;
12123                OldList * specs = MkList();
12124                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12125                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12126                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12127                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12128                      MkInitializerAssignment(MkExpBrackets(exp))))));
12129             }*/
12130             else if(_class.templateArgs)
12131             {
12132                if(isMap)
12133                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12134                else
12135                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12136
12137                stmt.compound.declarations = MkListOne(
12138                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12139                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12140                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12141             }
12142             symbol = FindSymbol(id.string, curContext, curContext, false, false);
12143
12144             if(block)
12145             {
12146                // Reparent sub-contexts in this statement
12147                switch(block.type)
12148                {
12149                   case compoundStmt:
12150                      if(block.compound.context)
12151                         block.compound.context.parent = stmt.compound.context;
12152                      break;
12153                   case ifStmt:
12154                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12155                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12156                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12157                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12158                      break;
12159                   case switchStmt:
12160                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12161                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12162                      break;
12163                   case whileStmt:
12164                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12165                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12166                      break;
12167                   case doWhileStmt:
12168                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12169                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12170                      break;
12171                   case forStmt:
12172                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12173                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12174                      break;
12175                   case forEachStmt:
12176                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12177                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12178                      break;
12179                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12180                   case labeledStmt:
12181                   case caseStmt
12182                   case expressionStmt:
12183                   case gotoStmt:
12184                   case continueStmt:
12185                   case breakStmt
12186                   case returnStmt:
12187                   case asmStmt:
12188                   case badDeclarationStmt:
12189                   case fireWatchersStmt:
12190                   case stopWatchingStmt:
12191                   case watchStmt:
12192                   */
12193                }
12194             }
12195             if(filter)
12196             {
12197                block = MkIfStmt(filter, block, null);
12198             }
12199             if(isArray)
12200             {
12201                stmt.compound.statements = MkListOne(MkForStmt(
12202                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12203                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12204                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12205                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12206                   block));
12207               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12208               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12209               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12210             }
12211             else if(isBuiltin)
12212             {
12213                char count[128];
12214                //OldList * specs = MkList();
12215                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12216
12217                sprintf(count, "%d", builtinCount);
12218
12219                stmt.compound.statements = MkListOne(MkForStmt(
12220                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12221                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12222                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12223                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12224                   block));
12225
12226                /*
12227                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12228                stmt.compound.statements = MkListOne(MkForStmt(
12229                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12230                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12231                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12232                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12233                   block));
12234               */
12235               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12236               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12237               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12238             }
12239             else if(isLinkList && !isList)
12240             {
12241                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12242                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12243                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12244                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12245                {
12246                   stmt.compound.statements = MkListOne(MkForStmt(
12247                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12248                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12249                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12250                      block));
12251                }
12252                else
12253                {
12254                   OldList * specs = MkList();
12255                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12256                   stmt.compound.statements = MkListOne(MkForStmt(
12257                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12258                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12259                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12260                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12261                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12262                      block));
12263                }
12264                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12265                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12266                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12267             }
12268             /*else if(isCustomAVLTree)
12269             {
12270                stmt.compound.statements = MkListOne(MkForStmt(
12271                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12272                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12273                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12274                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12275                   block));
12276
12277                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12278                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12279                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12280             }*/
12281             else
12282             {
12283                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12284                   MkIdentifier("Next")), null)), block));
12285             }
12286             ProcessExpressionType(expIt);
12287             if(stmt.compound.declarations->first)
12288                ProcessDeclaration(stmt.compound.declarations->first);
12289
12290             if(symbol)
12291                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12292
12293             ProcessStatement(stmt);
12294             curContext = stmt.compound.context.parent;
12295             break;
12296          }
12297          else
12298          {
12299             Compiler_Error($"Expression is not a container\n");
12300          }
12301          break;
12302       }
12303       case gotoStmt:
12304          break;
12305       case continueStmt:
12306          break;
12307       case breakStmt:
12308          break;
12309       case returnStmt:
12310       {
12311          Expression exp;
12312          if(stmt.expressions)
12313          {
12314             for(exp = stmt.expressions->first; exp; exp = exp.next)
12315             {
12316                if(!exp.next)
12317                {
12318                   if(curFunction && !curFunction.type)
12319                      curFunction.type = ProcessType(
12320                         curFunction.specifiers, curFunction.declarator);
12321                   FreeType(exp.destType);
12322                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12323                   if(exp.destType) exp.destType.refCount++;
12324                }
12325                ProcessExpressionType(exp);
12326             }
12327          }
12328          break;
12329       }
12330       case badDeclarationStmt:
12331       {
12332          ProcessDeclaration(stmt.decl);
12333          break;
12334       }
12335       case asmStmt:
12336       {
12337          AsmField field;
12338          if(stmt.asmStmt.inputFields)
12339          {
12340             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12341                if(field.expression)
12342                   ProcessExpressionType(field.expression);
12343          }
12344          if(stmt.asmStmt.outputFields)
12345          {
12346             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12347                if(field.expression)
12348                   ProcessExpressionType(field.expression);
12349          }
12350          if(stmt.asmStmt.clobberedFields)
12351          {
12352             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12353             {
12354                if(field.expression)
12355                   ProcessExpressionType(field.expression);
12356             }
12357          }
12358          break;
12359       }
12360       case watchStmt:
12361       {
12362          PropertyWatch propWatch;
12363          OldList * watches = stmt._watch.watches;
12364          Expression object = stmt._watch.object;
12365          Expression watcher = stmt._watch.watcher;
12366          if(watcher)
12367             ProcessExpressionType(watcher);
12368          if(object)
12369             ProcessExpressionType(object);
12370
12371          if(inCompiler)
12372          {
12373             if(watcher || thisClass)
12374             {
12375                External external = curExternal;
12376                Context context = curContext;
12377
12378                stmt.type = expressionStmt;
12379                stmt.expressions = MkList();
12380
12381                curExternal = external.prev;
12382
12383                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12384                {
12385                   ClassFunction func;
12386                   char watcherName[1024];
12387                   Class watcherClass = watcher ?
12388                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12389                   External createdExternal;
12390
12391                   // Create a declaration above
12392                   External externalDecl = MkExternalDeclaration(null);
12393                   ast->Insert(curExternal.prev, externalDecl);
12394
12395                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12396                   if(propWatch.deleteWatch)
12397                      strcat(watcherName, "_delete");
12398                   else
12399                   {
12400                      Identifier propID;
12401                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12402                      {
12403                         strcat(watcherName, "_");
12404                         strcat(watcherName, propID.string);
12405                      }
12406                   }
12407
12408                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12409                   {
12410                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12411                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12412                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12413                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12414                      ProcessClassFunctionBody(func, propWatch.compound);
12415                      propWatch.compound = null;
12416
12417                      //afterExternal = afterExternal ? afterExternal : curExternal;
12418
12419                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12420                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12421                      // TESTING THIS...
12422                      createdExternal.symbol.idCode = external.symbol.idCode;
12423
12424                      curExternal = createdExternal;
12425                      ProcessFunction(createdExternal.function);
12426
12427
12428                      // Create a declaration above
12429                      {
12430                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12431                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12432                         externalDecl.declaration = decl;
12433                         if(decl.symbol && !decl.symbol.pointerExternal)
12434                            decl.symbol.pointerExternal = externalDecl;
12435                      }
12436
12437                      if(propWatch.deleteWatch)
12438                      {
12439                         OldList * args = MkList();
12440                         ListAdd(args, CopyExpression(object));
12441                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12442                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12443                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12444                      }
12445                      else
12446                      {
12447                         Class _class = object.expType._class.registered;
12448                         Identifier propID;
12449
12450                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12451                         {
12452                            char propName[1024];
12453                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12454                            if(prop)
12455                            {
12456                               char getName[1024], setName[1024];
12457                               OldList * args = MkList();
12458
12459                               DeclareProperty(prop, setName, getName);
12460
12461                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12462                               strcpy(propName, "__ecereProp_");
12463                               FullClassNameCat(propName, prop._class.fullName, false);
12464                               strcat(propName, "_");
12465                               // strcat(propName, prop.name);
12466                               FullClassNameCat(propName, prop.name, true);
12467
12468                               ListAdd(args, CopyExpression(object));
12469                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12470                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12471                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12472
12473                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12474                            }
12475                            else
12476                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12477                         }
12478                      }
12479                   }
12480                   else
12481                      Compiler_Error($"Invalid watched object\n");
12482                }
12483
12484                curExternal = external;
12485                curContext = context;
12486
12487                if(watcher)
12488                   FreeExpression(watcher);
12489                if(object)
12490                   FreeExpression(object);
12491                FreeList(watches, FreePropertyWatch);
12492             }
12493             else
12494                Compiler_Error($"No observer specified and not inside a _class\n");
12495          }
12496          else
12497          {
12498             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12499             {
12500                ProcessStatement(propWatch.compound);
12501             }
12502
12503          }
12504          break;
12505       }
12506       case fireWatchersStmt:
12507       {
12508          OldList * watches = stmt._watch.watches;
12509          Expression object = stmt._watch.object;
12510          Class _class;
12511          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12512          // printf("%X\n", watches);
12513          // printf("%X\n", stmt._watch.watches);
12514          if(object)
12515             ProcessExpressionType(object);
12516
12517          if(inCompiler)
12518          {
12519             _class = object ?
12520                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12521
12522             if(_class)
12523             {
12524                Identifier propID;
12525
12526                stmt.type = expressionStmt;
12527                stmt.expressions = MkList();
12528
12529                // Check if we're inside a property set
12530                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12531                {
12532                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12533                }
12534                else if(!watches)
12535                {
12536                   //Compiler_Error($"No property specified and not inside a property set\n");
12537                }
12538                if(watches)
12539                {
12540                   for(propID = watches->first; propID; propID = propID.next)
12541                   {
12542                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12543                      if(prop)
12544                      {
12545                         CreateFireWatcher(prop, object, stmt);
12546                      }
12547                      else
12548                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12549                   }
12550                }
12551                else
12552                {
12553                   // Fire all properties!
12554                   Property prop;
12555                   Class base;
12556                   for(base = _class; base; base = base.base)
12557                   {
12558                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12559                      {
12560                         if(prop.isProperty && prop.isWatchable)
12561                         {
12562                            CreateFireWatcher(prop, object, stmt);
12563                         }
12564                      }
12565                   }
12566                }
12567
12568                if(object)
12569                   FreeExpression(object);
12570                FreeList(watches, FreeIdentifier);
12571             }
12572             else
12573                Compiler_Error($"Invalid object specified and not inside a class\n");
12574          }
12575          break;
12576       }
12577       case stopWatchingStmt:
12578       {
12579          OldList * watches = stmt._watch.watches;
12580          Expression object = stmt._watch.object;
12581          Expression watcher = stmt._watch.watcher;
12582          Class _class;
12583          if(object)
12584             ProcessExpressionType(object);
12585          if(watcher)
12586             ProcessExpressionType(watcher);
12587          if(inCompiler)
12588          {
12589             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12590
12591             if(watcher || thisClass)
12592             {
12593                if(_class)
12594                {
12595                   Identifier propID;
12596
12597                   stmt.type = expressionStmt;
12598                   stmt.expressions = MkList();
12599
12600                   if(!watches)
12601                   {
12602                      OldList * args;
12603                      // eInstance_StopWatching(object, null, watcher);
12604                      args = MkList();
12605                      ListAdd(args, CopyExpression(object));
12606                      ListAdd(args, MkExpConstant("0"));
12607                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12608                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12609                   }
12610                   else
12611                   {
12612                      for(propID = watches->first; propID; propID = propID.next)
12613                      {
12614                         char propName[1024];
12615                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12616                         if(prop)
12617                         {
12618                            char getName[1024], setName[1024];
12619                            OldList * args = MkList();
12620
12621                            DeclareProperty(prop, setName, getName);
12622
12623                            // eInstance_StopWatching(object, prop, watcher);
12624                            strcpy(propName, "__ecereProp_");
12625                            FullClassNameCat(propName, prop._class.fullName, false);
12626                            strcat(propName, "_");
12627                            // strcat(propName, prop.name);
12628                            FullClassNameCat(propName, prop.name, true);
12629                            //MangleClassName(propName);
12630
12631                            ListAdd(args, CopyExpression(object));
12632                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12633                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12634                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12635                         }
12636                         else
12637                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12638                      }
12639                   }
12640
12641                   if(object)
12642                      FreeExpression(object);
12643                   if(watcher)
12644                      FreeExpression(watcher);
12645                   FreeList(watches, FreeIdentifier);
12646                }
12647                else
12648                   Compiler_Error($"Invalid object specified and not inside a class\n");
12649             }
12650             else
12651                Compiler_Error($"No observer specified and not inside a class\n");
12652          }
12653          break;
12654       }
12655    }
12656 }
12657
12658 static void ProcessFunction(FunctionDefinition function)
12659 {
12660    Identifier id = GetDeclId(function.declarator);
12661    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12662    Type type = symbol ? symbol.type : null;
12663    Class oldThisClass = thisClass;
12664    Context oldTopContext = topContext;
12665
12666    yylloc = function.loc;
12667    // Process thisClass
12668
12669    if(type && type.thisClass)
12670    {
12671       Symbol classSym = type.thisClass;
12672       Class _class = type.thisClass.registered;
12673       char className[1024];
12674       char structName[1024];
12675       Declarator funcDecl;
12676       Symbol thisSymbol;
12677
12678       bool typedObject = false;
12679
12680       if(_class && !_class.base)
12681       {
12682          _class = currentClass;
12683          if(_class && !_class.symbol)
12684             _class.symbol = FindClass(_class.fullName);
12685          classSym = _class ? _class.symbol : null;
12686          typedObject = true;
12687       }
12688
12689       thisClass = _class;
12690
12691       if(inCompiler && _class)
12692       {
12693          if(type.kind == functionType)
12694          {
12695             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12696             {
12697                //TypeName param = symbol.type.params.first;
12698                Type param = symbol.type.params.first;
12699                symbol.type.params.Remove(param);
12700                //FreeTypeName(param);
12701                FreeType(param);
12702             }
12703             if(type.classObjectType != classPointer)
12704             {
12705                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12706                symbol.type.staticMethod = true;
12707                symbol.type.thisClass = null;
12708
12709                // HIGH DANGER: VERIFYING THIS...
12710                symbol.type.extraParam = false;
12711             }
12712          }
12713
12714          strcpy(className, "__ecereClass_");
12715          FullClassNameCat(className, _class.fullName, true);
12716
12717          //MangleClassName(className);
12718
12719          structName[0] = 0;
12720          FullClassNameCat(structName, _class.fullName, false);
12721
12722          // [class] this
12723
12724
12725          funcDecl = GetFuncDecl(function.declarator);
12726          if(funcDecl)
12727          {
12728             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12729             {
12730                TypeName param = funcDecl.function.parameters->first;
12731                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12732                {
12733                   funcDecl.function.parameters->Remove(param);
12734                   FreeTypeName(param);
12735                }
12736             }
12737
12738             // DANGER: Watch for this... Check if it's a Conversion?
12739             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12740
12741             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12742             if(!function.propertyNoThis)
12743             {
12744                TypeName thisParam = null;
12745
12746                if(type.classObjectType != classPointer)
12747                {
12748                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12749                   if(!funcDecl.function.parameters)
12750                      funcDecl.function.parameters = MkList();
12751                   funcDecl.function.parameters->Insert(null, thisParam);
12752                }
12753
12754                if(typedObject)
12755                {
12756                   if(type.classObjectType != classPointer)
12757                   {
12758                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12759                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12760                   }
12761
12762                   thisParam = TypeName
12763                   {
12764                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12765                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12766                   };
12767                   funcDecl.function.parameters->Insert(null, thisParam);
12768                }
12769             }
12770          }
12771
12772          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12773          {
12774             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12775             funcDecl = GetFuncDecl(initDecl.declarator);
12776             if(funcDecl)
12777             {
12778                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12779                {
12780                   TypeName param = funcDecl.function.parameters->first;
12781                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12782                   {
12783                      funcDecl.function.parameters->Remove(param);
12784                      FreeTypeName(param);
12785                   }
12786                }
12787
12788                if(type.classObjectType != classPointer)
12789                {
12790                   // DANGER: Watch for this... Check if it's a Conversion?
12791                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12792                   {
12793                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12794
12795                      if(!funcDecl.function.parameters)
12796                         funcDecl.function.parameters = MkList();
12797                      funcDecl.function.parameters->Insert(null, thisParam);
12798                   }
12799                }
12800             }
12801          }
12802       }
12803
12804       // Add this to the context
12805       if(function.body)
12806       {
12807          if(type.classObjectType != classPointer)
12808          {
12809             thisSymbol = Symbol
12810             {
12811                string = CopyString("this");
12812                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12813             };
12814             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12815
12816             if(typedObject && thisSymbol.type)
12817             {
12818                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12819                thisSymbol.type.byReference = type.byReference;
12820                thisSymbol.type.typedByReference = type.byReference;
12821                /*
12822                thisSymbol = Symbol { string = CopyString("class") };
12823                function.body.compound.context.symbols.Add(thisSymbol);
12824                */
12825             }
12826          }
12827       }
12828
12829       // Pointer to class data
12830
12831       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12832       {
12833          DataMember member = null;
12834          {
12835             Class base;
12836             for(base = _class; base && base.type != systemClass; base = base.next)
12837             {
12838                for(member = base.membersAndProperties.first; member; member = member.next)
12839                   if(!member.isProperty)
12840                      break;
12841                if(member)
12842                   break;
12843             }
12844          }
12845          for(member = _class.membersAndProperties.first; member; member = member.next)
12846             if(!member.isProperty)
12847                break;
12848          if(member)
12849          {
12850             char pointerName[1024];
12851
12852             Declaration decl;
12853             Initializer initializer;
12854             Expression exp, bytePtr;
12855
12856             strcpy(pointerName, "__ecerePointer_");
12857             FullClassNameCat(pointerName, _class.fullName, false);
12858             {
12859                char className[1024];
12860                strcpy(className, "__ecereClass_");
12861                FullClassNameCat(className, classSym.string, true);
12862                //MangleClassName(className);
12863
12864                // Testing This
12865                DeclareClass(classSym, className);
12866             }
12867
12868             // ((byte *) this)
12869             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12870
12871             if(_class.fixed)
12872             {
12873                char string[256];
12874                sprintf(string, "%d", _class.offset);
12875                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12876             }
12877             else
12878             {
12879                // ([bytePtr] + [className]->offset)
12880                exp = QBrackets(MkExpOp(bytePtr, '+',
12881                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12882             }
12883
12884             // (this ? [exp] : 0)
12885             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12886             exp.expType = Type
12887             {
12888                refCount = 1;
12889                kind = pointerType;
12890                type = Type { refCount = 1, kind = voidType };
12891             };
12892
12893             if(function.body)
12894             {
12895                yylloc = function.body.loc;
12896                // ([structName] *) [exp]
12897                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12898                initializer = MkInitializerAssignment(
12899                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12900
12901                // [structName] * [pointerName] = [initializer];
12902                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12903
12904                {
12905                   Context prevContext = curContext;
12906                   OldList * list;
12907                   curContext = function.body.compound.context;
12908
12909                   decl = MkDeclaration((list = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null))),
12910                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12911                   list->Insert(null, MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
12912
12913                   curContext = prevContext;
12914                }
12915
12916                // WHY?
12917                decl.symbol = null;
12918
12919                if(!function.body.compound.declarations)
12920                   function.body.compound.declarations = MkList();
12921                function.body.compound.declarations->Insert(null, decl);
12922             }
12923          }
12924       }
12925
12926
12927       // Loop through the function and replace undeclared identifiers
12928       // which are a member of the class (methods, properties or data)
12929       // by "this.[member]"
12930    }
12931    else
12932       thisClass = null;
12933
12934    if(id)
12935    {
12936       FreeSpecifier(id._class);
12937       id._class = null;
12938
12939       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12940       {
12941          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12942          id = GetDeclId(initDecl.declarator);
12943
12944          FreeSpecifier(id._class);
12945          id._class = null;
12946       }
12947    }
12948    if(function.body)
12949       topContext = function.body.compound.context;
12950    {
12951       FunctionDefinition oldFunction = curFunction;
12952       curFunction = function;
12953       if(function.body)
12954          ProcessStatement(function.body);
12955
12956       // If this is a property set and no firewatchers has been done yet, add one here
12957       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12958       {
12959          Statement prevCompound = curCompound;
12960          Context prevContext = curContext;
12961
12962          Statement fireWatchers = MkFireWatchersStmt(null, null);
12963          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12964          ListAdd(function.body.compound.statements, fireWatchers);
12965
12966          curCompound = function.body;
12967          curContext = function.body.compound.context;
12968
12969          ProcessStatement(fireWatchers);
12970
12971          curContext = prevContext;
12972          curCompound = prevCompound;
12973
12974       }
12975
12976       curFunction = oldFunction;
12977    }
12978
12979    if(function.declarator)
12980    {
12981       ProcessDeclarator(function.declarator);
12982    }
12983
12984    topContext = oldTopContext;
12985    thisClass = oldThisClass;
12986 }
12987
12988 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12989 static void ProcessClass(OldList definitions, Symbol symbol)
12990 {
12991    ClassDef def;
12992    External external = curExternal;
12993    Class regClass = symbol ? symbol.registered : null;
12994
12995    // Process all functions
12996    for(def = definitions.first; def; def = def.next)
12997    {
12998       if(def.type == functionClassDef)
12999       {
13000          if(def.function.declarator)
13001             curExternal = def.function.declarator.symbol.pointerExternal;
13002          else
13003             curExternal = external;
13004
13005          ProcessFunction((FunctionDefinition)def.function);
13006       }
13007       else if(def.type == declarationClassDef)
13008       {
13009          if(def.decl.type == instDeclaration)
13010          {
13011             thisClass = regClass;
13012             ProcessInstantiationType(def.decl.inst);
13013             thisClass = null;
13014          }
13015          // Testing this
13016          else
13017          {
13018             Class backThisClass = thisClass;
13019             if(regClass) thisClass = regClass;
13020             ProcessDeclaration(def.decl);
13021             thisClass = backThisClass;
13022          }
13023       }
13024       else if(def.type == defaultPropertiesClassDef && def.defProperties)
13025       {
13026          MemberInit defProperty;
13027
13028          // Add this to the context
13029          Symbol thisSymbol = Symbol
13030          {
13031             string = CopyString("this");
13032             type = regClass ? MkClassType(regClass.fullName) : null;
13033          };
13034          globalContext.symbols.Add((BTNode)thisSymbol);
13035
13036          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
13037          {
13038             thisClass = regClass;
13039             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
13040             thisClass = null;
13041          }
13042
13043          globalContext.symbols.Remove((BTNode)thisSymbol);
13044          FreeSymbol(thisSymbol);
13045       }
13046       else if(def.type == propertyClassDef && def.propertyDef)
13047       {
13048          PropertyDef prop = def.propertyDef;
13049
13050          // Add this to the context
13051          /*
13052          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
13053          globalContext.symbols.Add(thisSymbol);
13054          */
13055
13056          thisClass = regClass;
13057          if(prop.setStmt)
13058          {
13059             if(regClass)
13060             {
13061                Symbol thisSymbol
13062                {
13063                   string = CopyString("this");
13064                   type = MkClassType(regClass.fullName);
13065                };
13066                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13067             }
13068
13069             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13070             ProcessStatement(prop.setStmt);
13071          }
13072          if(prop.getStmt)
13073          {
13074             if(regClass)
13075             {
13076                Symbol thisSymbol
13077                {
13078                   string = CopyString("this");
13079                   type = MkClassType(regClass.fullName);
13080                };
13081                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13082             }
13083
13084             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13085             ProcessStatement(prop.getStmt);
13086          }
13087          if(prop.issetStmt)
13088          {
13089             if(regClass)
13090             {
13091                Symbol thisSymbol
13092                {
13093                   string = CopyString("this");
13094                   type = MkClassType(regClass.fullName);
13095                };
13096                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13097             }
13098
13099             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13100             ProcessStatement(prop.issetStmt);
13101          }
13102
13103          thisClass = null;
13104
13105          /*
13106          globalContext.symbols.Remove(thisSymbol);
13107          FreeSymbol(thisSymbol);
13108          */
13109       }
13110       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13111       {
13112          PropertyWatch propertyWatch = def.propertyWatch;
13113
13114          thisClass = regClass;
13115          if(propertyWatch.compound)
13116          {
13117             Symbol thisSymbol
13118             {
13119                string = CopyString("this");
13120                type = regClass ? MkClassType(regClass.fullName) : null;
13121             };
13122
13123             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13124
13125             curExternal = null;
13126             ProcessStatement(propertyWatch.compound);
13127          }
13128          thisClass = null;
13129       }
13130    }
13131 }
13132
13133 void DeclareFunctionUtil(const String s)
13134 {
13135    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13136    if(function)
13137    {
13138       char name[1024];
13139       name[0] = 0;
13140       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13141          strcpy(name, "__ecereFunction_");
13142       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13143       DeclareFunction(function, name);
13144    }
13145 }
13146
13147 void ComputeDataTypes()
13148 {
13149    External external;
13150    External temp { };
13151    External after = null;
13152
13153    currentClass = null;
13154
13155    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13156
13157    for(external = ast->first; external; external = external.next)
13158    {
13159       if(external.type == declarationExternal)
13160       {
13161          Declaration decl = external.declaration;
13162          if(decl)
13163          {
13164             OldList * decls = decl.declarators;
13165             if(decls)
13166             {
13167                InitDeclarator initDecl = decls->first;
13168                if(initDecl)
13169                {
13170                   Declarator declarator = initDecl.declarator;
13171                   if(declarator && declarator.type == identifierDeclarator)
13172                   {
13173                      Identifier id = declarator.identifier;
13174                      if(id && id.string)
13175                      {
13176                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
13177                         {
13178                            external.symbol.id = -1001, external.symbol.idCode = -1001;
13179                            after = external;
13180                         }
13181                      }
13182                   }
13183                }
13184             }
13185          }
13186        }
13187    }
13188
13189    {
13190       // Workaround until we have proper toposort for declarations reordering
13191       External e = MkExternalDeclaration(MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Instance"), null)), null));
13192       ast->Insert(after, e);
13193       after = e;
13194    }
13195
13196    temp.symbol = Symbol { id = -1000, idCode = -1000 };
13197    ast->Insert(after, temp);
13198    curExternal = temp;
13199
13200    DeclareFunctionUtil("eSystem_New");
13201    DeclareFunctionUtil("eSystem_New0");
13202    DeclareFunctionUtil("eSystem_Renew");
13203    DeclareFunctionUtil("eSystem_Renew0");
13204    DeclareFunctionUtil("eSystem_Delete");
13205    DeclareFunctionUtil("eClass_GetProperty");
13206    DeclareFunctionUtil("eClass_SetProperty");
13207    DeclareFunctionUtil("eInstance_FireSelfWatchers");
13208    DeclareFunctionUtil("eInstance_SetMethod");
13209    DeclareFunctionUtil("eInstance_IncRef");
13210    DeclareFunctionUtil("eInstance_StopWatching");
13211    DeclareFunctionUtil("eInstance_Watch");
13212    DeclareFunctionUtil("eInstance_FireWatchers");
13213
13214    DeclareStruct("ecere::com::Class", false);
13215    DeclareStruct("ecere::com::Instance", false);
13216    DeclareStruct("ecere::com::Property", false);
13217    DeclareStruct("ecere::com::DataMember", false);
13218    DeclareStruct("ecere::com::Method", false);
13219    DeclareStruct("ecere::com::SerialBuffer", false);
13220    DeclareStruct("ecere::com::ClassTemplateArgument", false);
13221
13222    ast->Remove(temp);
13223
13224    for(external = ast->first; external; external = external.next)
13225    {
13226       afterExternal = curExternal = external;
13227       if(external.type == functionExternal)
13228       {
13229          currentClass = external.function._class;
13230          ProcessFunction(external.function);
13231       }
13232       // There shouldn't be any _class member access here anyways...
13233       else if(external.type == declarationExternal)
13234       {
13235          currentClass = null;
13236          if(external.declaration)
13237             ProcessDeclaration(external.declaration);
13238       }
13239       else if(external.type == classExternal)
13240       {
13241          ClassDefinition _class = external._class;
13242          currentClass = external.symbol.registered;
13243          if(_class.definitions)
13244          {
13245             ProcessClass(_class.definitions, _class.symbol);
13246          }
13247          if(inCompiler)
13248          {
13249             // Free class data...
13250             ast->Remove(external);
13251             delete external;
13252          }
13253       }
13254       else if(external.type == nameSpaceExternal)
13255       {
13256          thisNameSpace = external.id.string;
13257       }
13258    }
13259    currentClass = null;
13260    thisNameSpace = null;
13261    curExternal = null;
13262
13263    delete temp.symbol;
13264    delete temp;
13265 }