compiler/libec: (#276) Fixed mistake introduced in d3a2010cf68cf33b7f4deab52e2929beb4...
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50       bool backOutputLineNumbers = outputLineNumbers;
51       outputLineNumbers = false;
52
53       if(exp)
54          OutputExpression(exp, f);
55       f.Seek(0, start);
56       count = strlen(string);
57       count += f.Read(string + count, 1, 1023);
58       string[count] = '\0';
59       delete f;
60
61       outputLineNumbers = backOutputLineNumbers;
62    }
63 }
64
65 Type ProcessTemplateParameterType(TemplateParameter param)
66 {
67    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
68    {
69       // TOFIX: Will need to free this Type
70       if(!param.baseType)
71       {
72          if(param.dataTypeString)
73             param.baseType = ProcessTypeString(param.dataTypeString, false);
74          else
75             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
76       }
77       return param.baseType;
78    }
79    return null;
80 }
81
82 bool NeedCast(Type type1, Type type2)
83 {
84    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
85
86    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
87    {
88       return false;
89    }
90
91    if(type1.kind == type2.kind)
92    {
93       switch(type1.kind)
94       {
95          case _BoolType:
96          case charType:
97          case shortType:
98          case intType:
99          case int64Type:
100          case intPtrType:
101          case intSizeType:
102             if(type1.passAsTemplate && !type2.passAsTemplate)
103                return true;
104             return type1.isSigned != type2.isSigned;
105          case classType:
106             return type1._class != type2._class;
107          case pointerType:
108             return NeedCast(type1.type, type2.type);
109          default:
110             return true; //false; ????
111       }
112    }
113    return true;
114 }
115
116 static void ReplaceClassMembers(Expression exp, Class _class)
117 {
118    if(exp.type == identifierExp && exp.identifier)
119    {
120       Identifier id = exp.identifier;
121       Context ctx;
122       Symbol symbol = null;
123       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
124       {
125          // First, check if the identifier is declared inside the function
126          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
127          {
128             symbol = (Symbol)ctx.symbols.FindString(id.string);
129             if(symbol) break;
130          }
131       }
132
133       // If it is not, check if it is a member of the _class
134       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
135       {
136          Property prop = eClass_FindProperty(_class, id.string, privateModule);
137          Method method = null;
138          DataMember member = null;
139          ClassProperty classProp = null;
140          if(!prop)
141          {
142             method = eClass_FindMethod(_class, id.string, privateModule);
143          }
144          if(!prop && !method)
145             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
146          if(!prop && !method && !member)
147          {
148             classProp = eClass_FindClassProperty(_class, id.string);
149          }
150          if(prop || method || member || classProp)
151          {
152             // Replace by this.[member]
153             exp.type = memberExp;
154             exp.member.member = id;
155             exp.member.memberType = unresolvedMember;
156             exp.member.exp = QMkExpId("this");
157             //exp.member.exp.loc = exp.loc;
158             exp.addedThis = true;
159          }
160          else if(_class && _class.templateParams.first)
161          {
162             Class sClass;
163             for(sClass = _class; sClass; sClass = sClass.base)
164             {
165                if(sClass.templateParams.first)
166                {
167                   ClassTemplateParameter param;
168                   for(param = sClass.templateParams.first; param; param = param.next)
169                   {
170                      if(param.type == expression && !strcmp(param.name, id.string))
171                      {
172                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
173
174                         if(argExp)
175                         {
176                            Declarator decl;
177                            OldList * specs = MkList();
178
179                            FreeIdentifier(exp.member.member);
180
181                            ProcessExpressionType(argExp);
182
183                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
184
185                            exp.expType = ProcessType(specs, decl);
186
187                            // *[expType] *[argExp]
188                            exp.type = bracketsExp;
189                            exp.list = MkListOne(MkExpOp(null, '*',
190                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
191                         }
192                      }
193                   }
194                }
195             }
196          }
197       }
198    }
199 }
200
201 ////////////////////////////////////////////////////////////////////////
202 // PRINTING ////////////////////////////////////////////////////////////
203 ////////////////////////////////////////////////////////////////////////
204
205 public char * PrintInt(int64 result)
206 {
207    char temp[100];
208    if(result > MAXINT)
209       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
210    else
211       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
212    if(result > MAXINT || result < MININT)
213       strcat(temp, "LL");
214    return CopyString(temp);
215 }
216
217 public char * PrintUInt(uint64 result)
218 {
219    char temp[100];
220    if(result > MAXDWORD)
221       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
222    else if(result > MAXINT)
223       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
224    else
225       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char * PrintInt64(int64 result)
230 {
231    char temp[100];
232    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
233    return CopyString(temp);
234 }
235
236 public char * PrintUInt64(uint64 result)
237 {
238    char temp[100];
239    if(result > MAXINT64)
240       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
241    else
242       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
243    return CopyString(temp);
244 }
245
246 public char * PrintHexUInt(uint64 result)
247 {
248    char temp[100];
249    if(result > MAXDWORD)
250       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
251    else
252       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
253    if(result > MAXDWORD)
254       strcat(temp, "LL");
255    return CopyString(temp);
256 }
257
258 public char * PrintHexUInt64(uint64 result)
259 {
260    char temp[100];
261    if(result > MAXDWORD)
262       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
263    else
264       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
265    return CopyString(temp);
266 }
267
268 public char * PrintShort(short result)
269 {
270    char temp[100];
271    sprintf(temp, "%d", (unsigned short)result);
272    return CopyString(temp);
273 }
274
275 public char * PrintUShort(unsigned short result)
276 {
277    char temp[100];
278    if(result > 32767)
279       sprintf(temp, "0x%X", (int)result);
280    else
281       sprintf(temp, "%d", (int)result);
282    return CopyString(temp);
283 }
284
285 public char * PrintChar(char result)
286 {
287    char temp[100];
288    if(result > 0 && isprint(result))
289       sprintf(temp, "'%c'", result);
290    else if(result < 0)
291       sprintf(temp, "%d", (int)result);
292    else
293       //sprintf(temp, "%#X", result);
294       sprintf(temp, "0x%X", (unsigned char)result);
295    return CopyString(temp);
296 }
297
298 public char * PrintUChar(unsigned char result)
299 {
300    char temp[100];
301    sprintf(temp, "0x%X", result);
302    return CopyString(temp);
303 }
304
305 public char * PrintFloat(float result)
306 {
307    char temp[350];
308    if(result.isInf)
309    {
310       if(result.signBit)
311          strcpy(temp, "-inf");
312       else
313          strcpy(temp, "inf");
314    }
315    else if(result.isNan)
316    {
317       if(result.signBit)
318          strcpy(temp, "-nan");
319       else
320          strcpy(temp, "nan");
321    }
322    else
323       sprintf(temp, "%.16ff", result);
324    return CopyString(temp);
325 }
326
327 public char * PrintDouble(double result)
328 {
329    char temp[350];
330    if(result.isInf)
331    {
332       if(result.signBit)
333          strcpy(temp, "-inf");
334       else
335          strcpy(temp, "inf");
336    }
337    else if(result.isNan)
338    {
339       if(result.signBit)
340          strcpy(temp, "-nan");
341       else
342          strcpy(temp, "nan");
343    }
344    else
345       sprintf(temp, "%.16f", result);
346    return CopyString(temp);
347 }
348
349 ////////////////////////////////////////////////////////////////////////
350 ////////////////////////////////////////////////////////////////////////
351
352 //public Operand GetOperand(Expression exp);
353
354 #define GETVALUE(name, t) \
355    public bool GetOp##name(Operand op2, t * value2) \
356    {                                                        \
357       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
358       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
359       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
360       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
361       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
362       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
363       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
364       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
365       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
366       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
367       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
368       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
369       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
370       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
371       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
372       else                                                                          \
373          return false;                                                              \
374       return true;                                                                  \
375    } \
376    public bool Get##name(Expression exp, t * value2) \
377    {                                                        \
378       Operand op2 = GetOperand(exp);                        \
379       return GetOp##name(op2, value2); \
380    }
381
382 // To help the deubugger currently not preprocessing...
383 #define HELP(x) x
384
385 GETVALUE(Int, HELP(int));
386 GETVALUE(UInt, HELP(unsigned int));
387 GETVALUE(Int64, HELP(int64));
388 GETVALUE(UInt64, HELP(uint64));
389 GETVALUE(IntPtr, HELP(intptr));
390 GETVALUE(UIntPtr, HELP(uintptr));
391 GETVALUE(IntSize, HELP(intsize));
392 GETVALUE(UIntSize, HELP(uintsize));
393 GETVALUE(Short, HELP(short));
394 GETVALUE(UShort, HELP(unsigned short));
395 GETVALUE(Char, HELP(char));
396 GETVALUE(UChar, HELP(unsigned char));
397 GETVALUE(Float, HELP(float));
398 GETVALUE(Double, HELP(double));
399
400 void ComputeExpression(Expression exp);
401
402 void ComputeClassMembers(Class _class, bool isMember)
403 {
404    DataMember member = isMember ? (DataMember) _class : null;
405    Context context = isMember ? null : SetupTemplatesContext(_class);
406    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
407                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
408    {
409       int c;
410       int unionMemberOffset = 0;
411       int bitFields = 0;
412
413       /*
414       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
415          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
416       */
417
418       if(member)
419       {
420          member.memberOffset = 0;
421          if(targetBits < sizeof(void *) * 8)
422             member.structAlignment = 0;
423       }
424       else if(targetBits < sizeof(void *) * 8)
425          _class.structAlignment = 0;
426
427       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
428       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
429          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
430
431       if(!member && _class.destructionWatchOffset)
432          _class.memberOffset += sizeof(OldList);
433
434       // To avoid reentrancy...
435       //_class.structSize = -1;
436
437       {
438          DataMember dataMember;
439          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
440          {
441             if(!dataMember.isProperty)
442             {
443                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
444                {
445                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
446                   /*if(!dataMember.dataType)
447                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
448                      */
449                }
450             }
451          }
452       }
453
454       {
455          DataMember dataMember;
456          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
457          {
458             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
459             {
460                if(!isMember && _class.type == bitClass && dataMember.dataType)
461                {
462                   BitMember bitMember = (BitMember) dataMember;
463                   uint64 mask = 0;
464                   int d;
465
466                   ComputeTypeSize(dataMember.dataType);
467
468                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
469                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
470
471                   _class.memberOffset = bitMember.pos + bitMember.size;
472                   for(d = 0; d<bitMember.size; d++)
473                   {
474                      if(d)
475                         mask <<= 1;
476                      mask |= 1;
477                   }
478                   bitMember.mask = mask << bitMember.pos;
479                }
480                else if(dataMember.type == normalMember && dataMember.dataType)
481                {
482                   int size;
483                   int alignment = 0;
484
485                   // Prevent infinite recursion
486                   if(dataMember.dataType.kind != classType ||
487                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
488                      _class.type != structClass)))
489                      ComputeTypeSize(dataMember.dataType);
490
491                   if(dataMember.dataType.bitFieldCount)
492                   {
493                      bitFields += dataMember.dataType.bitFieldCount;
494                      size = 0;
495                   }
496                   else
497                   {
498                      if(bitFields)
499                      {
500                         int size = (bitFields + 7) / 8;
501
502                         if(isMember)
503                         {
504                            // TESTING THIS PADDING CODE
505                            if(alignment)
506                            {
507                               member.structAlignment = Max(member.structAlignment, alignment);
508
509                               if(member.memberOffset % alignment)
510                                  member.memberOffset += alignment - (member.memberOffset % alignment);
511                            }
512
513                            dataMember.offset = member.memberOffset;
514                            if(member.type == unionMember)
515                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
516                            else
517                            {
518                               member.memberOffset += size;
519                            }
520                         }
521                         else
522                         {
523                            // TESTING THIS PADDING CODE
524                            if(alignment)
525                            {
526                               _class.structAlignment = Max(_class.structAlignment, alignment);
527
528                               if(_class.memberOffset % alignment)
529                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
530                            }
531
532                            dataMember.offset = _class.memberOffset;
533                            _class.memberOffset += size;
534                         }
535                         bitFields = 0;
536                      }
537                      size = dataMember.dataType.size;
538                      alignment = dataMember.dataType.alignment;
539                   }
540
541                   if(isMember)
542                   {
543                      // TESTING THIS PADDING CODE
544                      if(alignment)
545                      {
546                         member.structAlignment = Max(member.structAlignment, alignment);
547
548                         if(member.memberOffset % alignment)
549                            member.memberOffset += alignment - (member.memberOffset % alignment);
550                      }
551
552                      dataMember.offset = member.memberOffset;
553                      if(member.type == unionMember)
554                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
555                      else
556                      {
557                         member.memberOffset += size;
558                      }
559                   }
560                   else
561                   {
562                      // TESTING THIS PADDING CODE
563                      if(alignment)
564                      {
565                         _class.structAlignment = Max(_class.structAlignment, alignment);
566
567                         if(_class.memberOffset % alignment)
568                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
569                      }
570
571                      dataMember.offset = _class.memberOffset;
572                      _class.memberOffset += size;
573                   }
574                }
575                else
576                {
577                   int alignment;
578
579                   ComputeClassMembers((Class)dataMember, true);
580                   alignment = dataMember.structAlignment;
581
582                   if(isMember)
583                   {
584                      if(alignment)
585                      {
586                         if(member.memberOffset % alignment)
587                            member.memberOffset += alignment - (member.memberOffset % alignment);
588
589                         member.structAlignment = Max(member.structAlignment, alignment);
590                      }
591                      dataMember.offset = member.memberOffset;
592                      if(member.type == unionMember)
593                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
594                      else
595                         member.memberOffset += dataMember.memberOffset;
596                   }
597                   else
598                   {
599                      if(alignment)
600                      {
601                         if(_class.memberOffset % alignment)
602                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
603                         _class.structAlignment = Max(_class.structAlignment, alignment);
604                      }
605                      dataMember.offset = _class.memberOffset;
606                      _class.memberOffset += dataMember.memberOffset;
607                   }
608                }
609             }
610          }
611          if(bitFields)
612          {
613             int alignment = 0;
614             int size = (bitFields + 7) / 8;
615
616             if(isMember)
617             {
618                // TESTING THIS PADDING CODE
619                if(alignment)
620                {
621                   member.structAlignment = Max(member.structAlignment, alignment);
622
623                   if(member.memberOffset % alignment)
624                      member.memberOffset += alignment - (member.memberOffset % alignment);
625                }
626
627                if(member.type == unionMember)
628                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
629                else
630                {
631                   member.memberOffset += size;
632                }
633             }
634             else
635             {
636                // TESTING THIS PADDING CODE
637                if(alignment)
638                {
639                   _class.structAlignment = Max(_class.structAlignment, alignment);
640
641                   if(_class.memberOffset % alignment)
642                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
643                }
644                _class.memberOffset += size;
645             }
646             bitFields = 0;
647          }
648       }
649       if(member && member.type == unionMember)
650       {
651          member.memberOffset = unionMemberOffset;
652       }
653
654       if(!isMember)
655       {
656          /*if(_class.type == structClass)
657             _class.size = _class.memberOffset;
658          else
659          */
660
661          if(_class.type != bitClass)
662          {
663             int extra = 0;
664             if(_class.structAlignment)
665             {
666                if(_class.memberOffset % _class.structAlignment)
667                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
668             }
669             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
670             if(!member)
671             {
672                Property prop;
673                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
674                {
675                   if(prop.isProperty && prop.isWatchable)
676                   {
677                      prop.watcherOffset = _class.structSize;
678                      _class.structSize += sizeof(OldList);
679                   }
680                }
681             }
682
683             // Fix Derivatives
684             {
685                OldLink derivative;
686                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
687                {
688                   Class deriv = derivative.data;
689
690                   if(deriv.computeSize)
691                   {
692                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
693                      deriv.offset = /*_class.offset + */_class.structSize;
694                      deriv.memberOffset = 0;
695                      // ----------------------
696
697                      deriv.structSize = deriv.offset;
698
699                      ComputeClassMembers(deriv, false);
700                   }
701                }
702             }
703          }
704       }
705    }
706    if(context)
707       FinishTemplatesContext(context);
708 }
709
710 public void ComputeModuleClasses(Module module)
711 {
712    Class _class;
713    OldLink subModule;
714
715    for(subModule = module.modules.first; subModule; subModule = subModule.next)
716       ComputeModuleClasses(subModule.data);
717    for(_class = module.classes.first; _class; _class = _class.next)
718       ComputeClassMembers(_class, false);
719 }
720
721
722 public int ComputeTypeSize(Type type)
723 {
724    uint size = type ? type.size : 0;
725    if(!size && type && !type.computing)
726    {
727       type.computing = true;
728       switch(type.kind)
729       {
730          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
731          case charType: type.alignment = size = sizeof(char); break;
732          case intType: type.alignment = size = sizeof(int); break;
733          case int64Type: type.alignment = size = sizeof(int64); break;
734          case intPtrType: type.alignment = size = targetBits / 8; break;
735          case intSizeType: type.alignment = size = targetBits / 8; break;
736          case longType: type.alignment = size = sizeof(long); break;
737          case shortType: type.alignment = size = sizeof(short); break;
738          case floatType: type.alignment = size = sizeof(float); break;
739          case doubleType: type.alignment = size = sizeof(double); break;
740          case classType:
741          {
742             Class _class = type._class ? type._class.registered : null;
743
744             if(_class && _class.type == structClass)
745             {
746                // Ensure all members are properly registered
747                ComputeClassMembers(_class, false);
748                type.alignment = _class.structAlignment;
749                size = _class.structSize;
750                if(type.alignment && size % type.alignment)
751                   size += type.alignment - (size % type.alignment);
752
753             }
754             else if(_class && (_class.type == unitClass ||
755                    _class.type == enumClass ||
756                    _class.type == bitClass))
757             {
758                if(!_class.dataType)
759                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
760                size = type.alignment = ComputeTypeSize(_class.dataType);
761             }
762             else
763                size = type.alignment = targetBits / 8; // sizeof(Instance *);
764             break;
765          }
766          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
767          case arrayType:
768             if(type.arraySizeExp)
769             {
770                ProcessExpressionType(type.arraySizeExp);
771                ComputeExpression(type.arraySizeExp);
772                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
773                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
774                {
775                   Location oldLoc = yylloc;
776                   // bool isConstant = type.arraySizeExp.isConstant;
777                   char expression[10240];
778                   expression[0] = '\0';
779                   type.arraySizeExp.expType = null;
780                   yylloc = type.arraySizeExp.loc;
781                   if(inCompiler)
782                      PrintExpression(type.arraySizeExp, expression);
783                   Compiler_Error($"Array size not constant int (%s)\n", expression);
784                   yylloc = oldLoc;
785                }
786                GetInt(type.arraySizeExp, &type.arraySize);
787             }
788             else if(type.enumClass)
789             {
790                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
791                {
792                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
793                }
794                else
795                   type.arraySize = 0;
796             }
797             else
798             {
799                // Unimplemented auto size
800                type.arraySize = 0;
801             }
802
803             size = ComputeTypeSize(type.type) * type.arraySize;
804             if(type.type)
805                type.alignment = type.type.alignment;
806
807             break;
808          case structType:
809          {
810             Type member;
811             for(member = type.members.first; member; member = member.next)
812             {
813                uint addSize = ComputeTypeSize(member);
814
815                member.offset = size;
816                if(member.alignment && size % member.alignment)
817                   member.offset += member.alignment - (size % member.alignment);
818                size = member.offset;
819
820                type.alignment = Max(type.alignment, member.alignment);
821                size += addSize;
822             }
823             if(type.alignment && size % type.alignment)
824                size += type.alignment - (size % type.alignment);
825             break;
826          }
827          case unionType:
828          {
829             Type member;
830             for(member = type.members.first; member; member = member.next)
831             {
832                uint addSize = ComputeTypeSize(member);
833
834                member.offset = size;
835                if(member.alignment && size % member.alignment)
836                   member.offset += member.alignment - (size % member.alignment);
837                size = member.offset;
838
839                type.alignment = Max(type.alignment, member.alignment);
840                size = Max(size, addSize);
841             }
842             if(type.alignment && size % type.alignment)
843                size += type.alignment - (size % type.alignment);
844             break;
845          }
846          case templateType:
847          {
848             TemplateParameter param = type.templateParameter;
849             Type baseType = ProcessTemplateParameterType(param);
850             if(baseType)
851             {
852                size = ComputeTypeSize(baseType);
853                type.alignment = baseType.alignment;
854             }
855             else
856                type.alignment = size = sizeof(uint64);
857             break;
858          }
859          case enumType:
860          {
861             type.alignment = size = sizeof(enum { test });
862             break;
863          }
864          case thisClassType:
865          {
866             type.alignment = size = targetBits / 8; //sizeof(void *);
867             break;
868          }
869       }
870       type.size = size;
871       type.computing = false;
872    }
873    return size;
874 }
875
876
877 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
878 {
879    // This function is in need of a major review when implementing private members etc.
880    DataMember topMember = isMember ? (DataMember) _class : null;
881    uint totalSize = 0;
882    uint maxSize = 0;
883    int alignment, size;
884    DataMember member;
885    Context context = isMember ? null : SetupTemplatesContext(_class);
886    if(addedPadding)
887       *addedPadding = false;
888
889    if(!isMember && _class.base)
890    {
891       maxSize = _class.structSize;
892       //if(_class.base.type != systemClass) // Commented out with new Instance _class
893       {
894          // DANGER: Testing this noHeadClass here...
895          if(_class.type == structClass || _class.type == noHeadClass)
896             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
897          else
898          {
899             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
900             if(maxSize > baseSize)
901                maxSize -= baseSize;
902             else
903                maxSize = 0;
904          }
905       }
906    }
907
908    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
909    {
910       if(!member.isProperty)
911       {
912          switch(member.type)
913          {
914             case normalMember:
915             {
916                if(member.dataTypeString)
917                {
918                   OldList * specs = MkList(), * decls = MkList();
919                   Declarator decl;
920
921                   decl = SpecDeclFromString(member.dataTypeString, specs,
922                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
923                   ListAdd(decls, MkStructDeclarator(decl, null));
924                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
925
926                   if(!member.dataType)
927                      member.dataType = ProcessType(specs, decl);
928
929                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
930
931                   {
932                      Type type = ProcessType(specs, decl);
933                      DeclareType(member.dataType, false, false);
934                      FreeType(type);
935                   }
936                   /*
937                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
938                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
939                      DeclareStruct(member.dataType._class.string, false);
940                   */
941
942                   ComputeTypeSize(member.dataType);
943                   size = member.dataType.size;
944                   alignment = member.dataType.alignment;
945
946                   if(alignment)
947                   {
948                      if(totalSize % alignment)
949                         totalSize += alignment - (totalSize % alignment);
950                   }
951                   totalSize += size;
952                }
953                break;
954             }
955             case unionMember:
956             case structMember:
957             {
958                OldList * specs = MkList(), * list = MkList();
959
960                size = 0;
961                AddMembers(list, (Class)member, true, &size, topClass, null);
962                ListAdd(specs,
963                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
964                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
965                alignment = member.structAlignment;
966
967                if(alignment)
968                {
969                   if(totalSize % alignment)
970                      totalSize += alignment - (totalSize % alignment);
971                }
972                totalSize += size;
973                break;
974             }
975          }
976       }
977    }
978    if(retSize)
979    {
980       if(topMember && topMember.type == unionMember)
981          *retSize = Max(*retSize, totalSize);
982       else
983          *retSize += totalSize;
984    }
985    else if(totalSize < maxSize && _class.type != systemClass)
986    {
987       int autoPadding = 0;
988       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
989          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
990       if(totalSize + autoPadding < maxSize)
991       {
992          char sizeString[50];
993          sprintf(sizeString, "%d", maxSize - totalSize);
994          ListAdd(declarations,
995             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
996             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
997          if(addedPadding)
998             *addedPadding = true;
999       }
1000    }
1001    if(context)
1002       FinishTemplatesContext(context);
1003    return topMember ? topMember.memberID : _class.memberID;
1004 }
1005
1006 static int DeclareMembers(Class _class, bool isMember)
1007 {
1008    DataMember topMember = isMember ? (DataMember) _class : null;
1009    uint totalSize = 0;
1010    DataMember member;
1011    Context context = isMember ? null : SetupTemplatesContext(_class);
1012
1013    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1014       DeclareMembers(_class.base, false);
1015
1016    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1017    {
1018       if(!member.isProperty)
1019       {
1020          switch(member.type)
1021          {
1022             case normalMember:
1023             {
1024                /*
1025                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1026                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1027                   DeclareStruct(member.dataType._class.string, false);
1028                   */
1029                if(!member.dataType && member.dataTypeString)
1030                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1031                if(member.dataType)
1032                   DeclareType(member.dataType, false, false);
1033                break;
1034             }
1035             case unionMember:
1036             case structMember:
1037             {
1038                DeclareMembers((Class)member, true);
1039                break;
1040             }
1041          }
1042       }
1043    }
1044    if(context)
1045       FinishTemplatesContext(context);
1046
1047    return topMember ? topMember.memberID : _class.memberID;
1048 }
1049
1050 void DeclareStruct(char * name, bool skipNoHead)
1051 {
1052    External external = null;
1053    Symbol classSym = FindClass(name);
1054
1055    if(!inCompiler || !classSym) return;
1056
1057    // We don't need any declaration for bit classes...
1058    if(classSym.registered &&
1059       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1060       return;
1061
1062    /*if(classSym.registered.templateClass)
1063       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1064    */
1065
1066    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1067    {
1068       // Add typedef struct
1069       Declaration decl;
1070       OldList * specifiers, * declarators;
1071       OldList * declarations = null;
1072       char structName[1024];
1073       Specifier spec = null;
1074       external = (classSym.registered && classSym.registered.type == structClass) ?
1075          classSym.pointerExternal : classSym.structExternal;
1076
1077       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1078       // Moved this one up because DeclareClass done later will need it
1079
1080       classSym.declaring++;
1081
1082       if(strchr(classSym.string, '<'))
1083       {
1084          if(classSym.registered.templateClass)
1085          {
1086             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1087             classSym.declaring--;
1088          }
1089          return;
1090       }
1091
1092       //if(!skipNoHead)
1093          DeclareMembers(classSym.registered, false);
1094
1095       structName[0] = 0;
1096       FullClassNameCat(structName, name, false);
1097
1098       if(external && external.declaration && external.declaration.specifiers)
1099       {
1100          for(spec = external.declaration.specifiers->first; spec; spec = spec.next)
1101          {
1102             if(spec.type == structSpecifier || spec.type == unionSpecifier)
1103                break;
1104          }
1105       }
1106
1107       /*if(!external)
1108          external = MkExternalDeclaration(null);*/
1109
1110       if(!skipNoHead && (!spec || !spec.definitions))
1111       {
1112          bool addedPadding = false;
1113          classSym.declaredStructSym = true;
1114
1115          declarations = MkList();
1116
1117          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1118
1119          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1120          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1121
1122          if(!declarations->count || (declarations->count == 1 && addedPadding))
1123          {
1124             FreeList(declarations, FreeClassDef);
1125             declarations = null;
1126          }
1127       }
1128       if(skipNoHead || declarations)
1129       {
1130          if(spec)
1131          {
1132             if(declarations)
1133                spec.definitions = declarations;
1134
1135             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1136             {
1137                // TODO: Fix this
1138                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1139
1140                // DANGER
1141                if(classSym.structExternal)
1142                   ast->Move(classSym.structExternal, curExternal.prev);
1143                ast->Move(classSym.pointerExternal, curExternal.prev);
1144
1145                classSym.id = curExternal.symbol.idCode;
1146                classSym.idCode = curExternal.symbol.idCode;
1147                // external = classSym.pointerExternal;
1148                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1149             }
1150          }
1151          else
1152          {
1153             if(!external)
1154                external = MkExternalDeclaration(null);
1155
1156             specifiers = MkList();
1157             declarators = MkList();
1158             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1159
1160             /*
1161             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1162             ListAdd(declarators, MkInitDeclarator(d, null));
1163             */
1164             external.declaration = decl = MkDeclaration(specifiers, declarators);
1165             if(decl.symbol && !decl.symbol.pointerExternal)
1166                decl.symbol.pointerExternal = external;
1167
1168             // For simple classes, keep the declaration as the external to move around
1169             if(classSym.registered && classSym.registered.type == structClass)
1170             {
1171                char className[1024];
1172                strcpy(className, "__ecereClass_");
1173                FullClassNameCat(className, classSym.string, true);
1174                MangleClassName(className);
1175
1176                // Testing This
1177                DeclareClass(classSym, className);
1178
1179                external.symbol = classSym;
1180                classSym.pointerExternal = external;
1181                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1182                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1183             }
1184             else
1185             {
1186                char className[1024];
1187                strcpy(className, "__ecereClass_");
1188                FullClassNameCat(className, classSym.string, true);
1189                MangleClassName(className);
1190
1191                // TOFIX: TESTING THIS...
1192                classSym.structExternal = external;
1193                DeclareClass(classSym, className);
1194                external.symbol = classSym;
1195             }
1196
1197             //if(curExternal)
1198                ast->Insert(curExternal ? curExternal.prev : null, external);
1199          }
1200       }
1201
1202       classSym.declaring--;
1203    }
1204    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1205    {
1206       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1207       // Moved this one up because DeclareClass done later will need it
1208
1209       // TESTING THIS:
1210       classSym.declaring++;
1211
1212       //if(!skipNoHead)
1213       {
1214          if(classSym.registered)
1215             DeclareMembers(classSym.registered, false);
1216       }
1217
1218       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1219       {
1220          // TODO: Fix this
1221          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1222
1223          // DANGER
1224          if(classSym.structExternal)
1225             ast->Move(classSym.structExternal, curExternal.prev);
1226          ast->Move(classSym.pointerExternal, curExternal.prev);
1227
1228          classSym.id = curExternal.symbol.idCode;
1229          classSym.idCode = curExternal.symbol.idCode;
1230          // external = classSym.pointerExternal;
1231          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1232       }
1233
1234       classSym.declaring--;
1235    }
1236    //return external;
1237 }
1238
1239 void DeclareProperty(Property prop, char * setName, char * getName)
1240 {
1241    Symbol symbol = prop.symbol;
1242    char propName[1024];
1243
1244    strcpy(setName, "__ecereProp_");
1245    FullClassNameCat(setName, prop._class.fullName, false);
1246    strcat(setName, "_Set_");
1247    // strcat(setName, prop.name);
1248    FullClassNameCat(setName, prop.name, true);
1249
1250    strcpy(getName, "__ecereProp_");
1251    FullClassNameCat(getName, prop._class.fullName, false);
1252    strcat(getName, "_Get_");
1253    FullClassNameCat(getName, prop.name, true);
1254    // strcat(getName, prop.name);
1255
1256    strcpy(propName, "__ecereProp_");
1257    FullClassNameCat(propName, prop._class.fullName, false);
1258    strcat(propName, "_");
1259    FullClassNameCat(propName, prop.name, true);
1260    // strcat(propName, prop.name);
1261
1262    // To support "char *" property
1263    MangleClassName(getName);
1264    MangleClassName(setName);
1265    MangleClassName(propName);
1266
1267    if(prop._class.type == structClass)
1268       DeclareStruct(prop._class.fullName, false);
1269
1270    if(!symbol || curExternal.symbol.idCode < symbol.id)
1271    {
1272       bool imported = false;
1273       bool dllImport = false;
1274       if(!symbol || symbol._import)
1275       {
1276          if(!symbol)
1277          {
1278             Symbol classSym;
1279             if(!prop._class.symbol)
1280                prop._class.symbol = FindClass(prop._class.fullName);
1281             classSym = prop._class.symbol;
1282             if(classSym && !classSym._import)
1283             {
1284                ModuleImport module;
1285
1286                if(prop._class.module)
1287                   module = FindModule(prop._class.module);
1288                else
1289                   module = mainModule;
1290
1291                classSym._import = ClassImport
1292                {
1293                   name = CopyString(prop._class.fullName);
1294                   isRemote = prop._class.isRemote;
1295                };
1296                module.classes.Add(classSym._import);
1297             }
1298             symbol = prop.symbol = Symbol { };
1299             symbol._import = (ClassImport)PropertyImport
1300             {
1301                name = CopyString(prop.name);
1302                isVirtual = false; //prop.isVirtual;
1303                hasSet = prop.Set ? true : false;
1304                hasGet = prop.Get ? true : false;
1305             };
1306             if(classSym)
1307                classSym._import.properties.Add(symbol._import);
1308          }
1309          imported = true;
1310          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1311          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1312             prop._class.module.importType != staticImport)
1313             dllImport = true;
1314       }
1315
1316       if(!symbol.type)
1317       {
1318          Context context = SetupTemplatesContext(prop._class);
1319          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1320          FinishTemplatesContext(context);
1321       }
1322
1323       // Get
1324       if(prop.Get)
1325       {
1326          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1327          {
1328             Declaration decl;
1329             OldList * specifiers, * declarators;
1330             Declarator d;
1331             OldList * params;
1332             Specifier spec;
1333             External external;
1334             Declarator typeDecl;
1335             bool simple = false;
1336
1337             specifiers = MkList();
1338             declarators = MkList();
1339             params = MkList();
1340
1341             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1342                MkDeclaratorIdentifier(MkIdentifier("this"))));
1343
1344             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1345             //if(imported)
1346             if(dllImport)
1347                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1348
1349             {
1350                Context context = SetupTemplatesContext(prop._class);
1351                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1352                FinishTemplatesContext(context);
1353             }
1354
1355             // Make sure the simple _class's type is declared
1356             for(spec = specifiers->first; spec; spec = spec.next)
1357             {
1358                if(spec.type == nameSpecifier /*SpecifierClass*/)
1359                {
1360                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1361                   {
1362                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1363                      symbol._class = classSym.registered;
1364                      if(classSym.registered && classSym.registered.type == structClass)
1365                      {
1366                         DeclareStruct(spec.name, false);
1367                         simple = true;
1368                      }
1369                   }
1370                }
1371             }
1372
1373             if(!simple)
1374                d = PlugDeclarator(typeDecl, d);
1375             else
1376             {
1377                ListAdd(params, MkTypeName(specifiers,
1378                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1379                specifiers = MkList();
1380             }
1381
1382             d = MkDeclaratorFunction(d, params);
1383
1384             //if(imported)
1385             if(dllImport)
1386                specifiers->Insert(null, MkSpecifier(EXTERN));
1387             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1388                specifiers->Insert(null, MkSpecifier(STATIC));
1389             if(simple)
1390                ListAdd(specifiers, MkSpecifier(VOID));
1391
1392             ListAdd(declarators, MkInitDeclarator(d, null));
1393
1394             decl = MkDeclaration(specifiers, declarators);
1395
1396             external = MkExternalDeclaration(decl);
1397             ast->Insert(curExternal.prev, external);
1398             external.symbol = symbol;
1399             symbol.externalGet = external;
1400
1401             ReplaceThisClassSpecifiers(specifiers, prop._class);
1402
1403             if(typeDecl)
1404                FreeDeclarator(typeDecl);
1405          }
1406          else
1407          {
1408             // Move declaration higher...
1409             ast->Move(symbol.externalGet, curExternal.prev);
1410          }
1411       }
1412
1413       // Set
1414       if(prop.Set)
1415       {
1416          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1417          {
1418             Declaration decl;
1419             OldList * specifiers, * declarators;
1420             Declarator d;
1421             OldList * params;
1422             Specifier spec;
1423             External external;
1424             Declarator typeDecl;
1425
1426             declarators = MkList();
1427             params = MkList();
1428
1429             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1430             if(!prop.conversion || prop._class.type == structClass)
1431             {
1432                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1433                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1434             }
1435
1436             specifiers = MkList();
1437
1438             {
1439                Context context = SetupTemplatesContext(prop._class);
1440                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1441                   MkDeclaratorIdentifier(MkIdentifier("value")));
1442                FinishTemplatesContext(context);
1443             }
1444             ListAdd(params, MkTypeName(specifiers, d));
1445
1446             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1447             //if(imported)
1448             if(dllImport)
1449                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1450             d = MkDeclaratorFunction(d, params);
1451
1452             // Make sure the simple _class's type is declared
1453             for(spec = specifiers->first; spec; spec = spec.next)
1454             {
1455                if(spec.type == nameSpecifier /*SpecifierClass*/)
1456                {
1457                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1458                   {
1459                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1460                      symbol._class = classSym.registered;
1461                      if(classSym.registered && classSym.registered.type == structClass)
1462                         DeclareStruct(spec.name, false);
1463                   }
1464                }
1465             }
1466
1467             ListAdd(declarators, MkInitDeclarator(d, null));
1468
1469             specifiers = MkList();
1470             //if(imported)
1471             if(dllImport)
1472                specifiers->Insert(null, MkSpecifier(EXTERN));
1473             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1474                specifiers->Insert(null, MkSpecifier(STATIC));
1475
1476             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1477             if(!prop.conversion || prop._class.type == structClass)
1478                ListAdd(specifiers, MkSpecifier(VOID));
1479             else
1480                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1481
1482             decl = MkDeclaration(specifiers, declarators);
1483
1484             external = MkExternalDeclaration(decl);
1485             ast->Insert(curExternal.prev, external);
1486             external.symbol = symbol;
1487             symbol.externalSet = external;
1488
1489             ReplaceThisClassSpecifiers(specifiers, prop._class);
1490          }
1491          else
1492          {
1493             // Move declaration higher...
1494             ast->Move(symbol.externalSet, curExternal.prev);
1495          }
1496       }
1497
1498       // Property (for Watchers)
1499       if(!symbol.externalPtr)
1500       {
1501          Declaration decl;
1502          External external;
1503          OldList * specifiers = MkList();
1504
1505          if(imported)
1506             specifiers->Insert(null, MkSpecifier(EXTERN));
1507          else
1508             specifiers->Insert(null, MkSpecifier(STATIC));
1509
1510          ListAdd(specifiers, MkSpecifierName("Property"));
1511
1512          {
1513             OldList * list = MkList();
1514             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1515                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1516
1517             if(!imported)
1518             {
1519                strcpy(propName, "__ecerePropM_");
1520                FullClassNameCat(propName, prop._class.fullName, false);
1521                strcat(propName, "_");
1522                // strcat(propName, prop.name);
1523                FullClassNameCat(propName, prop.name, true);
1524
1525                MangleClassName(propName);
1526
1527                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1528                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1529             }
1530             decl = MkDeclaration(specifiers, list);
1531          }
1532
1533          external = MkExternalDeclaration(decl);
1534          ast->Insert(curExternal.prev, external);
1535          external.symbol = symbol;
1536          symbol.externalPtr = external;
1537       }
1538       else
1539       {
1540          // Move declaration higher...
1541          ast->Move(symbol.externalPtr, curExternal.prev);
1542       }
1543
1544       symbol.id = curExternal.symbol.idCode;
1545    }
1546 }
1547
1548 // ***************** EXPRESSION PROCESSING ***************************
1549 public Type Dereference(Type source)
1550 {
1551    Type type = null;
1552    if(source)
1553    {
1554       if(source.kind == pointerType || source.kind == arrayType)
1555       {
1556          type = source.type;
1557          source.type.refCount++;
1558       }
1559       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1560       {
1561          type = Type
1562          {
1563             kind = charType;
1564             refCount = 1;
1565          };
1566       }
1567       // Support dereferencing of no head classes for now...
1568       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1569       {
1570          type = source;
1571          source.refCount++;
1572       }
1573       else
1574          Compiler_Error($"cannot dereference type\n");
1575    }
1576    return type;
1577 }
1578
1579 static Type Reference(Type source)
1580 {
1581    Type type = null;
1582    if(source)
1583    {
1584       type = Type
1585       {
1586          kind = pointerType;
1587          type = source;
1588          refCount = 1;
1589       };
1590       source.refCount++;
1591    }
1592    return type;
1593 }
1594
1595 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1596 {
1597    Identifier ident = member.identifiers ? member.identifiers->first : null;
1598    bool found = false;
1599    DataMember dataMember = null;
1600    Method method = null;
1601    bool freeType = false;
1602
1603    yylloc = member.loc;
1604
1605    if(!ident)
1606    {
1607       if(curMember)
1608       {
1609          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1610          if(*curMember)
1611          {
1612             found = true;
1613             dataMember = *curMember;
1614          }
1615       }
1616    }
1617    else
1618    {
1619       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1620       DataMember _subMemberStack[256];
1621       int _subMemberStackPos = 0;
1622
1623       // FILL MEMBER STACK
1624       if(!thisMember)
1625          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1626       if(thisMember)
1627       {
1628          dataMember = thisMember;
1629          if(curMember && thisMember.memberAccess == publicAccess)
1630          {
1631             *curMember = thisMember;
1632             *curClass = thisMember._class;
1633             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1634             *subMemberStackPos = _subMemberStackPos;
1635          }
1636          found = true;
1637       }
1638       else
1639       {
1640          // Setting a method
1641          method = eClass_FindMethod(_class, ident.string, privateModule);
1642          if(method && method.type == virtualMethod)
1643             found = true;
1644          else
1645             method = null;
1646       }
1647    }
1648
1649    if(found)
1650    {
1651       Type type = null;
1652       if(dataMember)
1653       {
1654          if(!dataMember.dataType && dataMember.dataTypeString)
1655          {
1656             //Context context = SetupTemplatesContext(dataMember._class);
1657             Context context = SetupTemplatesContext(_class);
1658             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1659             FinishTemplatesContext(context);
1660          }
1661          type = dataMember.dataType;
1662       }
1663       else if(method)
1664       {
1665          // This is for destination type...
1666          if(!method.dataType)
1667             ProcessMethodType(method);
1668          //DeclareMethod(method);
1669          // method.dataType = ((Symbol)method.symbol)->type;
1670          type = method.dataType;
1671       }
1672
1673       if(ident && ident.next)
1674       {
1675          for(ident = ident.next; ident && type; ident = ident.next)
1676          {
1677             if(type.kind == classType)
1678             {
1679                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1680                if(!dataMember)
1681                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1682                if(dataMember)
1683                   type = dataMember.dataType;
1684             }
1685             else if(type.kind == structType || type.kind == unionType)
1686             {
1687                Type memberType;
1688                for(memberType = type.members.first; memberType; memberType = memberType.next)
1689                {
1690                   if(!strcmp(memberType.name, ident.string))
1691                   {
1692                      type = memberType;
1693                      break;
1694                   }
1695                }
1696             }
1697          }
1698       }
1699
1700       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1701       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1702       {
1703          int id = 0;
1704          ClassTemplateParameter curParam = null;
1705          Class sClass;
1706          for(sClass = _class; sClass; sClass = sClass.base)
1707          {
1708             id = 0;
1709             if(sClass.templateClass) sClass = sClass.templateClass;
1710             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1711             {
1712                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1713                {
1714                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1715                   {
1716                      if(sClass.templateClass) sClass = sClass.templateClass;
1717                      id += sClass.templateParams.count;
1718                   }
1719                   break;
1720                }
1721                id++;
1722             }
1723             if(curParam) break;
1724          }
1725
1726          if(curParam)
1727          {
1728             ClassTemplateArgument arg = _class.templateArgs[id];
1729             if(arg.dataTypeString)
1730             {
1731                // FreeType(type);
1732                type = ProcessTypeString(arg.dataTypeString, false);
1733                freeType = true;
1734                if(type && _class.templateClass)
1735                   type.passAsTemplate = true;
1736                if(type)
1737                {
1738                   // type.refCount++;
1739                   /*if(!exp.destType)
1740                   {
1741                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1742                      exp.destType.refCount++;
1743                   }*/
1744                }
1745             }
1746          }
1747       }
1748       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1749       {
1750          Class expClass = type._class.registered;
1751          Class cClass = null;
1752          int c;
1753          int paramCount = 0;
1754          int lastParam = -1;
1755
1756          char templateString[1024];
1757          ClassTemplateParameter param;
1758          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1759          for(cClass = expClass; cClass; cClass = cClass.base)
1760          {
1761             int p = 0;
1762             if(cClass.templateClass) cClass = cClass.templateClass;
1763             for(param = cClass.templateParams.first; param; param = param.next)
1764             {
1765                int id = p;
1766                Class sClass;
1767                ClassTemplateArgument arg;
1768                for(sClass = cClass.base; sClass; sClass = sClass.base)
1769                {
1770                   if(sClass.templateClass) sClass = sClass.templateClass;
1771                   id += sClass.templateParams.count;
1772                }
1773                arg = expClass.templateArgs[id];
1774
1775                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1776                {
1777                   ClassTemplateParameter cParam;
1778                   //int p = numParams - sClass.templateParams.count;
1779                   int p = 0;
1780                   Class nextClass;
1781                   if(sClass.templateClass) sClass = sClass.templateClass;
1782
1783                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1784                   {
1785                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1786                      p += nextClass.templateParams.count;
1787                   }
1788
1789                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1790                   {
1791                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1792                      {
1793                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1794                         {
1795                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1796                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1797                            break;
1798                         }
1799                      }
1800                   }
1801                }
1802
1803                {
1804                   char argument[256];
1805                   argument[0] = '\0';
1806                   /*if(arg.name)
1807                   {
1808                      strcat(argument, arg.name.string);
1809                      strcat(argument, " = ");
1810                   }*/
1811                   switch(param.type)
1812                   {
1813                      case expression:
1814                      {
1815                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1816                         char expString[1024];
1817                         OldList * specs = MkList();
1818                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1819                         Expression exp;
1820                         char * string = PrintHexUInt64(arg.expression.ui64);
1821                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1822                         delete string;
1823
1824                         ProcessExpressionType(exp);
1825                         ComputeExpression(exp);
1826                         expString[0] = '\0';
1827                         PrintExpression(exp, expString);
1828                         strcat(argument, expString);
1829                         //delete exp;
1830                         FreeExpression(exp);
1831                         break;
1832                      }
1833                      case identifier:
1834                      {
1835                         strcat(argument, arg.member.name);
1836                         break;
1837                      }
1838                      case TemplateParameterType::type:
1839                      {
1840                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1841                            strcat(argument, arg.dataTypeString);
1842                         break;
1843                      }
1844                   }
1845                   if(argument[0])
1846                   {
1847                      if(paramCount) strcat(templateString, ", ");
1848                      if(lastParam != p - 1)
1849                      {
1850                         strcat(templateString, param.name);
1851                         strcat(templateString, " = ");
1852                      }
1853                      strcat(templateString, argument);
1854                      paramCount++;
1855                      lastParam = p;
1856                   }
1857                   p++;
1858                }
1859             }
1860          }
1861          {
1862             int len = strlen(templateString);
1863             if(templateString[len-1] == '<')
1864                len--;
1865             else
1866             {
1867                if(templateString[len-1] == '>')
1868                   templateString[len++] = ' ';
1869                templateString[len++] = '>';
1870             }
1871             templateString[len++] = '\0';
1872          }
1873          {
1874             Context context = SetupTemplatesContext(_class);
1875             if(freeType) FreeType(type);
1876             type = ProcessTypeString(templateString, false);
1877             freeType = true;
1878             FinishTemplatesContext(context);
1879          }
1880       }
1881
1882       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1883       {
1884          ProcessExpressionType(member.initializer.exp);
1885          if(!member.initializer.exp.expType)
1886          {
1887             if(inCompiler)
1888             {
1889                char expString[10240];
1890                expString[0] = '\0';
1891                PrintExpression(member.initializer.exp, expString);
1892                ChangeCh(expString, '\n', ' ');
1893                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1894             }
1895          }
1896          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1897          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1898          {
1899             Compiler_Error($"incompatible instance method %s\n", ident.string);
1900          }
1901       }
1902       else if(member.initializer)
1903       {
1904          /*
1905          FreeType(member.exp.destType);
1906          member.exp.destType = type;
1907          if(member.exp.destType)
1908             member.exp.destType.refCount++;
1909          ProcessExpressionType(member.exp);
1910          */
1911
1912          ProcessInitializer(member.initializer, type);
1913       }
1914       if(freeType) FreeType(type);
1915    }
1916    else
1917    {
1918       if(_class && _class.type == unitClass)
1919       {
1920          if(member.initializer)
1921          {
1922             /*
1923             FreeType(member.exp.destType);
1924             member.exp.destType = MkClassType(_class.fullName);
1925             ProcessExpressionType(member.initializer, type);
1926             */
1927             Type type = MkClassType(_class.fullName);
1928             ProcessInitializer(member.initializer, type);
1929             FreeType(type);
1930          }
1931       }
1932       else
1933       {
1934          if(member.initializer)
1935          {
1936             //ProcessExpressionType(member.exp);
1937             ProcessInitializer(member.initializer, null);
1938          }
1939          if(ident)
1940          {
1941             if(method)
1942             {
1943                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1944             }
1945             else if(_class)
1946             {
1947                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1948                if(inCompiler)
1949                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1950             }
1951          }
1952          else if(_class)
1953             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1954       }
1955    }
1956 }
1957
1958 void ProcessInstantiationType(Instantiation inst)
1959 {
1960    yylloc = inst.loc;
1961    if(inst._class)
1962    {
1963       MembersInit members;
1964       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1965       Class _class;
1966
1967       /*if(!inst._class.symbol)
1968          inst._class.symbol = FindClass(inst._class.name);*/
1969       classSym = inst._class.symbol;
1970       _class = classSym ? classSym.registered : null;
1971
1972       // DANGER: Patch for mutex not declaring its struct when not needed
1973       if(!_class || _class.type != noHeadClass)
1974          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1975
1976       afterExternal = afterExternal ? afterExternal : curExternal;
1977
1978       if(inst.exp)
1979          ProcessExpressionType(inst.exp);
1980
1981       inst.isConstant = true;
1982       if(inst.members)
1983       {
1984          DataMember curMember = null;
1985          Class curClass = null;
1986          DataMember subMemberStack[256];
1987          int subMemberStackPos = 0;
1988
1989          for(members = inst.members->first; members; members = members.next)
1990          {
1991             switch(members.type)
1992             {
1993                case methodMembersInit:
1994                {
1995                   char name[1024];
1996                   static uint instMethodID = 0;
1997                   External external = curExternal;
1998                   Context context = curContext;
1999                   Declarator declarator = members.function.declarator;
2000                   Identifier nameID = GetDeclId(declarator);
2001                   char * unmangled = nameID ? nameID.string : null;
2002                   Expression exp;
2003                   External createdExternal = null;
2004
2005                   if(inCompiler)
2006                   {
2007                      char number[16];
2008                      //members.function.dontMangle = true;
2009                      strcpy(name, "__ecereInstMeth_");
2010                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2011                      strcat(name, "_");
2012                      strcat(name, nameID.string);
2013                      strcat(name, "_");
2014                      sprintf(number, "_%08d", instMethodID++);
2015                      strcat(name, number);
2016                      nameID.string = CopyString(name);
2017                   }
2018
2019                   // Do modifications here...
2020                   if(declarator)
2021                   {
2022                      Symbol symbol = declarator.symbol;
2023                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2024
2025                      if(method && method.type == virtualMethod)
2026                      {
2027                         symbol.method = method;
2028                         ProcessMethodType(method);
2029
2030                         if(!symbol.type.thisClass)
2031                         {
2032                            if(method.dataType.thisClass && currentClass &&
2033                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2034                            {
2035                               if(!currentClass.symbol)
2036                                  currentClass.symbol = FindClass(currentClass.fullName);
2037                               symbol.type.thisClass = currentClass.symbol;
2038                            }
2039                            else
2040                            {
2041                               if(!_class.symbol)
2042                                  _class.symbol = FindClass(_class.fullName);
2043                               symbol.type.thisClass = _class.symbol;
2044                            }
2045                         }
2046                         // TESTING THIS HERE:
2047                         DeclareType(symbol.type, true, true);
2048
2049                      }
2050                      else if(classSym)
2051                      {
2052                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2053                            unmangled, classSym.string);
2054                      }
2055                   }
2056
2057                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2058                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2059
2060                   if(nameID)
2061                   {
2062                      FreeSpecifier(nameID._class);
2063                      nameID._class = null;
2064                   }
2065
2066                   if(inCompiler)
2067                   {
2068
2069                      Type type = declarator.symbol.type;
2070                      External oldExternal = curExternal;
2071
2072                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2073                      // *** It was commented out for problems such as
2074                      /*
2075                            class VirtualDesktop : Window
2076                            {
2077                               clientSize = Size { };
2078                               Timer timer
2079                               {
2080                                  bool DelayExpired()
2081                                  {
2082                                     clientSize.w;
2083                                     return true;
2084                                  }
2085                               };
2086                            }
2087                      */
2088                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2089
2090                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2091
2092                      /*
2093                      if(strcmp(declarator.symbol.string, name))
2094                      {
2095                         printf("TOCHECK: Look out for this\n");
2096                         delete declarator.symbol.string;
2097                         declarator.symbol.string = CopyString(name);
2098                      }
2099
2100                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2101                      {
2102                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2103                         excludedSymbols->Remove(declarator.symbol);
2104                         globalContext.symbols.Add((BTNode)declarator.symbol);
2105                         if(strstr(declarator.symbol.string), "::")
2106                            globalContext.hasNameSpace = true;
2107
2108                      }
2109                      */
2110
2111                      //curExternal = curExternal.prev;
2112                      //afterExternal = afterExternal->next;
2113
2114                      //ProcessFunction(afterExternal->function);
2115
2116                      //curExternal = afterExternal;
2117                      {
2118                         External externalDecl;
2119                         externalDecl = MkExternalDeclaration(null);
2120                         ast->Insert(oldExternal.prev, externalDecl);
2121
2122                         // Which function does this process?
2123                         if(createdExternal.function)
2124                         {
2125                            ProcessFunction(createdExternal.function);
2126
2127                            //curExternal = oldExternal;
2128
2129                            {
2130                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2131
2132                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2133                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2134
2135                               //externalDecl = MkExternalDeclaration(decl);
2136
2137                               //***** ast->Insert(external.prev, externalDecl);
2138                               //ast->Insert(curExternal.prev, externalDecl);
2139                               externalDecl.declaration = decl;
2140                               if(decl.symbol && !decl.symbol.pointerExternal)
2141                                  decl.symbol.pointerExternal = externalDecl;
2142
2143                               // Trying this out...
2144                               declarator.symbol.pointerExternal = externalDecl;
2145                            }
2146                         }
2147                      }
2148                   }
2149                   else if(declarator)
2150                   {
2151                      curExternal = declarator.symbol.pointerExternal;
2152                      ProcessFunction((FunctionDefinition)members.function);
2153                   }
2154                   curExternal = external;
2155                   curContext = context;
2156
2157                   if(inCompiler)
2158                   {
2159                      FreeClassFunction(members.function);
2160
2161                      // In this pass, turn this into a MemberInitData
2162                      exp = QMkExpId(name);
2163                      members.type = dataMembersInit;
2164                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2165
2166                      delete unmangled;
2167                   }
2168                   break;
2169                }
2170                case dataMembersInit:
2171                {
2172                   if(members.dataMembers && classSym)
2173                   {
2174                      MemberInit member;
2175                      Location oldyyloc = yylloc;
2176                      for(member = members.dataMembers->first; member; member = member.next)
2177                      {
2178                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2179                         if(member.initializer && !member.initializer.isConstant)
2180                            inst.isConstant = false;
2181                      }
2182                      yylloc = oldyyloc;
2183                   }
2184                   break;
2185                }
2186             }
2187          }
2188       }
2189    }
2190 }
2191
2192 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2193 {
2194    // OPTIMIZATIONS: TESTING THIS...
2195    if(inCompiler)
2196    {
2197       if(type.kind == functionType)
2198       {
2199          Type param;
2200          if(declareParams)
2201          {
2202             for(param = type.params.first; param; param = param.next)
2203                DeclareType(param, declarePointers, true);
2204          }
2205          DeclareType(type.returnType, declarePointers, true);
2206       }
2207       else if(type.kind == pointerType && declarePointers)
2208          DeclareType(type.type, declarePointers, false);
2209       else if(type.kind == classType)
2210       {
2211          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2212             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2213       }
2214       else if(type.kind == structType || type.kind == unionType)
2215       {
2216          Type member;
2217          for(member = type.members.first; member; member = member.next)
2218             DeclareType(member, false, false);
2219       }
2220       else if(type.kind == arrayType)
2221          DeclareType(type.arrayType, declarePointers, false);
2222    }
2223 }
2224
2225 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2226 {
2227    ClassTemplateArgument * arg = null;
2228    int id = 0;
2229    ClassTemplateParameter curParam = null;
2230    Class sClass;
2231    for(sClass = _class; sClass; sClass = sClass.base)
2232    {
2233       id = 0;
2234       if(sClass.templateClass) sClass = sClass.templateClass;
2235       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2236       {
2237          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2238          {
2239             for(sClass = sClass.base; sClass; sClass = sClass.base)
2240             {
2241                if(sClass.templateClass) sClass = sClass.templateClass;
2242                id += sClass.templateParams.count;
2243             }
2244             break;
2245          }
2246          id++;
2247       }
2248       if(curParam) break;
2249    }
2250    if(curParam)
2251    {
2252       arg = &_class.templateArgs[id];
2253       if(arg && param.type == type)
2254          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2255    }
2256    return arg;
2257 }
2258
2259 public Context SetupTemplatesContext(Class _class)
2260 {
2261    Context context = PushContext();
2262    context.templateTypesOnly = true;
2263    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2264    {
2265       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2266       for(; param; param = param.next)
2267       {
2268          if(param.type == type && param.identifier)
2269          {
2270             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2271             curContext.templateTypes.Add((BTNode)type);
2272          }
2273       }
2274    }
2275    else if(_class)
2276    {
2277       Class sClass;
2278       for(sClass = _class; sClass; sClass = sClass.base)
2279       {
2280          ClassTemplateParameter p;
2281          for(p = sClass.templateParams.first; p; p = p.next)
2282          {
2283             //OldList * specs = MkList();
2284             //Declarator decl = null;
2285             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2286             if(p.type == type)
2287             {
2288                TemplateParameter param = p.param;
2289                TemplatedType type;
2290                if(!param)
2291                {
2292                   // ADD DATA TYPE HERE...
2293                   p.param = param = TemplateParameter
2294                   {
2295                      identifier = MkIdentifier(p.name), type = p.type,
2296                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2297                   };
2298                }
2299                type = TemplatedType { key = (uintptr)p.name, param = param };
2300                curContext.templateTypes.Add((BTNode)type);
2301             }
2302          }
2303       }
2304    }
2305    return context;
2306 }
2307
2308 public void FinishTemplatesContext(Context context)
2309 {
2310    PopContext(context);
2311    FreeContext(context);
2312    delete context;
2313 }
2314
2315 public void ProcessMethodType(Method method)
2316 {
2317    if(!method.dataType)
2318    {
2319       Context context = SetupTemplatesContext(method._class);
2320
2321       method.dataType = ProcessTypeString(method.dataTypeString, false);
2322
2323       FinishTemplatesContext(context);
2324
2325       if(method.type != virtualMethod && method.dataType)
2326       {
2327          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2328          {
2329             if(!method._class.symbol)
2330                method._class.symbol = FindClass(method._class.fullName);
2331             method.dataType.thisClass = method._class.symbol;
2332          }
2333       }
2334
2335       // Why was this commented out? Working fine without now...
2336
2337       /*
2338       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2339          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2340          */
2341    }
2342
2343    /*
2344    if(type)
2345    {
2346       char * par = strstr(type, "(");
2347       char * classOp = null;
2348       int classOpLen = 0;
2349       if(par)
2350       {
2351          int c;
2352          for(c = par-type-1; c >= 0; c++)
2353          {
2354             if(type[c] == ':' && type[c+1] == ':')
2355             {
2356                classOp = type + c - 1;
2357                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2358                {
2359                   classOp--;
2360                   classOpLen++;
2361                }
2362                break;
2363             }
2364             else if(!isspace(type[c]))
2365                break;
2366          }
2367       }
2368       if(classOp)
2369       {
2370          char temp[1024];
2371          int typeLen = strlen(type);
2372          memcpy(temp, classOp, classOpLen);
2373          temp[classOpLen] = '\0';
2374          if(temp[0])
2375             _class = eSystem_FindClass(module, temp);
2376          else
2377             _class = null;
2378          method.dataTypeString = new char[typeLen - classOpLen + 1];
2379          memcpy(method.dataTypeString, type, classOp - type);
2380          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2381       }
2382       else
2383          method.dataTypeString = type;
2384    }
2385    */
2386 }
2387
2388
2389 public void ProcessPropertyType(Property prop)
2390 {
2391    if(!prop.dataType)
2392    {
2393       Context context = SetupTemplatesContext(prop._class);
2394       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2395       FinishTemplatesContext(context);
2396    }
2397 }
2398
2399 public void DeclareMethod(Method method, char * name)
2400 {
2401    Symbol symbol = method.symbol;
2402    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2403    {
2404       bool imported = false;
2405       bool dllImport = false;
2406
2407       if(!method.dataType)
2408          method.dataType = ProcessTypeString(method.dataTypeString, false);
2409
2410       if(!symbol || symbol._import || method.type == virtualMethod)
2411       {
2412          if(!symbol || method.type == virtualMethod)
2413          {
2414             Symbol classSym;
2415             if(!method._class.symbol)
2416                method._class.symbol = FindClass(method._class.fullName);
2417             classSym = method._class.symbol;
2418             if(!classSym._import)
2419             {
2420                ModuleImport module;
2421
2422                if(method._class.module && method._class.module.name)
2423                   module = FindModule(method._class.module);
2424                else
2425                   module = mainModule;
2426                classSym._import = ClassImport
2427                {
2428                   name = CopyString(method._class.fullName);
2429                   isRemote = method._class.isRemote;
2430                };
2431                module.classes.Add(classSym._import);
2432             }
2433             if(!symbol)
2434             {
2435                symbol = method.symbol = Symbol { };
2436             }
2437             if(!symbol._import)
2438             {
2439                symbol._import = (ClassImport)MethodImport
2440                {
2441                   name = CopyString(method.name);
2442                   isVirtual = method.type == virtualMethod;
2443                };
2444                classSym._import.methods.Add(symbol._import);
2445             }
2446             if(!symbol)
2447             {
2448                // Set the symbol type
2449                /*
2450                if(!type.thisClass)
2451                {
2452                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2453                }
2454                else if(type.thisClass == (void *)-1)
2455                {
2456                   type.thisClass = null;
2457                }
2458                */
2459                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2460                symbol.type = method.dataType;
2461                if(symbol.type) symbol.type.refCount++;
2462             }
2463             /*
2464             if(!method.thisClass || strcmp(method.thisClass, "void"))
2465                symbol.type.params.Insert(null,
2466                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2467             */
2468          }
2469          if(!method.dataType.dllExport)
2470          {
2471             imported = true;
2472             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2473                dllImport = true;
2474          }
2475       }
2476
2477       /* MOVING THIS UP
2478       if(!method.dataType)
2479          method.dataType = ((Symbol)method.symbol).type;
2480          //ProcessMethodType(method);
2481       */
2482
2483       if(method.type != virtualMethod && method.dataType)
2484          DeclareType(method.dataType, true, true);
2485
2486       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2487       {
2488          // We need a declaration here :)
2489          Declaration decl;
2490          OldList * specifiers, * declarators;
2491          Declarator d;
2492          Declarator funcDecl;
2493          External external;
2494
2495          specifiers = MkList();
2496          declarators = MkList();
2497
2498          //if(imported)
2499          if(dllImport)
2500             ListAdd(specifiers, MkSpecifier(EXTERN));
2501          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2502             ListAdd(specifiers, MkSpecifier(STATIC));
2503
2504          if(method.type == virtualMethod)
2505          {
2506             ListAdd(specifiers, MkSpecifier(INT));
2507             d = MkDeclaratorIdentifier(MkIdentifier(name));
2508          }
2509          else
2510          {
2511             d = MkDeclaratorIdentifier(MkIdentifier(name));
2512             //if(imported)
2513             if(dllImport)
2514                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2515             {
2516                Context context = SetupTemplatesContext(method._class);
2517                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2518                FinishTemplatesContext(context);
2519             }
2520             funcDecl = GetFuncDecl(d);
2521
2522             if(dllImport)
2523             {
2524                Specifier spec, next;
2525                for(spec = specifiers->first; spec; spec = next)
2526                {
2527                   next = spec.next;
2528                   if(spec.type == extendedSpecifier)
2529                   {
2530                      specifiers->Remove(spec);
2531                      FreeSpecifier(spec);
2532                   }
2533                }
2534             }
2535
2536             // Add this parameter if not a static method
2537             if(method.dataType && !method.dataType.staticMethod)
2538             {
2539                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2540                {
2541                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2542                   TypeName thisParam = MkTypeName(MkListOne(
2543                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2544                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2545                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2546                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2547
2548                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2549                   {
2550                      TypeName param = funcDecl.function.parameters->first;
2551                      funcDecl.function.parameters->Remove(param);
2552                      FreeTypeName(param);
2553                   }
2554
2555                   if(!funcDecl.function.parameters)
2556                      funcDecl.function.parameters = MkList();
2557                   funcDecl.function.parameters->Insert(null, thisParam);
2558                }
2559             }
2560             // Make sure we don't have empty parameter declarations for static methods...
2561             /*
2562             else if(!funcDecl.function.parameters)
2563             {
2564                funcDecl.function.parameters = MkList();
2565                funcDecl.function.parameters->Insert(null,
2566                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2567             }*/
2568          }
2569          // TESTING THIS:
2570          ProcessDeclarator(d);
2571
2572          ListAdd(declarators, MkInitDeclarator(d, null));
2573
2574          decl = MkDeclaration(specifiers, declarators);
2575
2576          ReplaceThisClassSpecifiers(specifiers, method._class);
2577
2578          // Keep a different symbol for the function definition than the declaration...
2579          if(symbol.pointerExternal)
2580          {
2581             Symbol functionSymbol { };
2582
2583             // Copy symbol
2584             {
2585                *functionSymbol = *symbol;
2586                functionSymbol.string = CopyString(symbol.string);
2587                if(functionSymbol.type)
2588                   functionSymbol.type.refCount++;
2589             }
2590
2591             excludedSymbols->Add(functionSymbol);
2592             symbol.pointerExternal.symbol = functionSymbol;
2593          }
2594          external = MkExternalDeclaration(decl);
2595          if(curExternal)
2596             ast->Insert(curExternal ? curExternal.prev : null, external);
2597          external.symbol = symbol;
2598          symbol.pointerExternal = external;
2599       }
2600       else if(ast)
2601       {
2602          // Move declaration higher...
2603          ast->Move(symbol.pointerExternal, curExternal.prev);
2604       }
2605
2606       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2607    }
2608 }
2609
2610 char * ReplaceThisClass(Class _class)
2611 {
2612    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2613    {
2614       bool first = true;
2615       int p = 0;
2616       ClassTemplateParameter param;
2617       int lastParam = -1;
2618
2619       char className[1024];
2620       strcpy(className, _class.fullName);
2621       for(param = _class.templateParams.first; param; param = param.next)
2622       {
2623          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2624          {
2625             if(first) strcat(className, "<");
2626             if(!first) strcat(className, ", ");
2627             if(lastParam + 1 != p)
2628             {
2629                strcat(className, param.name);
2630                strcat(className, " = ");
2631             }
2632             strcat(className, param.name);
2633             first = false;
2634             lastParam = p;
2635          }
2636          p++;
2637       }
2638       if(!first)
2639       {
2640          int len = strlen(className);
2641          if(className[len-1] == '>') className[len++] = ' ';
2642          className[len++] = '>';
2643          className[len++] = '\0';
2644       }
2645       return CopyString(className);
2646    }
2647    else
2648       return CopyString(_class.fullName);
2649 }
2650
2651 Type ReplaceThisClassType(Class _class)
2652 {
2653    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2654    {
2655       bool first = true;
2656       int p = 0;
2657       ClassTemplateParameter param;
2658       int lastParam = -1;
2659       char className[1024];
2660       strcpy(className, _class.fullName);
2661
2662       for(param = _class.templateParams.first; param; param = param.next)
2663       {
2664          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2665          {
2666             if(first) strcat(className, "<");
2667             if(!first) strcat(className, ", ");
2668             if(lastParam + 1 != p)
2669             {
2670                strcat(className, param.name);
2671                strcat(className, " = ");
2672             }
2673             strcat(className, param.name);
2674             first = false;
2675             lastParam = p;
2676          }
2677          p++;
2678       }
2679       if(!first)
2680       {
2681          int len = strlen(className);
2682          if(className[len-1] == '>') className[len++] = ' ';
2683          className[len++] = '>';
2684          className[len++] = '\0';
2685       }
2686       return MkClassType(className);
2687       //return ProcessTypeString(className, false);
2688    }
2689    else
2690    {
2691       return MkClassType(_class.fullName);
2692       //return ProcessTypeString(_class.fullName, false);
2693    }
2694 }
2695
2696 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2697 {
2698    if(specs != null && _class)
2699    {
2700       Specifier spec;
2701       for(spec = specs.first; spec; spec = spec.next)
2702       {
2703          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2704          {
2705             spec.type = nameSpecifier;
2706             spec.name = ReplaceThisClass(_class);
2707             spec.symbol = FindClass(spec.name); //_class.symbol;
2708          }
2709       }
2710    }
2711 }
2712
2713 // Returns imported or not
2714 bool DeclareFunction(GlobalFunction function, char * name)
2715 {
2716    Symbol symbol = function.symbol;
2717    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2718    {
2719       bool imported = false;
2720       bool dllImport = false;
2721
2722       if(!function.dataType)
2723       {
2724          function.dataType = ProcessTypeString(function.dataTypeString, false);
2725          if(!function.dataType.thisClass)
2726             function.dataType.staticMethod = true;
2727       }
2728
2729       if(inCompiler)
2730       {
2731          if(!symbol)
2732          {
2733             ModuleImport module = FindModule(function.module);
2734             // WARNING: This is not added anywhere...
2735             symbol = function.symbol = Symbol {  };
2736
2737             if(module.name)
2738             {
2739                if(!function.dataType.dllExport)
2740                {
2741                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2742                   module.functions.Add(symbol._import);
2743                }
2744             }
2745             // Set the symbol type
2746             {
2747                symbol.type = ProcessTypeString(function.dataTypeString, false);
2748                if(!symbol.type.thisClass)
2749                   symbol.type.staticMethod = true;
2750             }
2751          }
2752          imported = symbol._import ? true : false;
2753          if(imported && function.module != privateModule && function.module.importType != staticImport)
2754             dllImport = true;
2755       }
2756
2757       DeclareType(function.dataType, true, true);
2758
2759       if(inCompiler)
2760       {
2761          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2762          {
2763             // We need a declaration here :)
2764             Declaration decl;
2765             OldList * specifiers, * declarators;
2766             Declarator d;
2767             Declarator funcDecl;
2768             External external;
2769
2770             specifiers = MkList();
2771             declarators = MkList();
2772
2773             //if(imported)
2774                ListAdd(specifiers, MkSpecifier(EXTERN));
2775             /*
2776             else
2777                ListAdd(specifiers, MkSpecifier(STATIC));
2778             */
2779
2780             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2781             //if(imported)
2782             if(dllImport)
2783                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2784
2785             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2786             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2787             if(function.module.importType == staticImport)
2788             {
2789                Specifier spec;
2790                for(spec = specifiers->first; spec; spec = spec.next)
2791                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2792                   {
2793                      specifiers->Remove(spec);
2794                      FreeSpecifier(spec);
2795                      break;
2796                   }
2797             }
2798
2799             funcDecl = GetFuncDecl(d);
2800
2801             // Make sure we don't have empty parameter declarations for static methods...
2802             if(funcDecl && !funcDecl.function.parameters)
2803             {
2804                funcDecl.function.parameters = MkList();
2805                funcDecl.function.parameters->Insert(null,
2806                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2807             }
2808
2809             ListAdd(declarators, MkInitDeclarator(d, null));
2810
2811             {
2812                Context oldCtx = curContext;
2813                curContext = globalContext;
2814                decl = MkDeclaration(specifiers, declarators);
2815                curContext = oldCtx;
2816             }
2817
2818             // Keep a different symbol for the function definition than the declaration...
2819             if(symbol.pointerExternal)
2820             {
2821                Symbol functionSymbol { };
2822                // Copy symbol
2823                {
2824                   *functionSymbol = *symbol;
2825                   functionSymbol.string = CopyString(symbol.string);
2826                   if(functionSymbol.type)
2827                      functionSymbol.type.refCount++;
2828                }
2829
2830                excludedSymbols->Add(functionSymbol);
2831
2832                symbol.pointerExternal.symbol = functionSymbol;
2833             }
2834             external = MkExternalDeclaration(decl);
2835             if(curExternal)
2836                ast->Insert(curExternal.prev, external);
2837             external.symbol = symbol;
2838             symbol.pointerExternal = external;
2839          }
2840          else
2841          {
2842             // Move declaration higher...
2843             ast->Move(symbol.pointerExternal, curExternal.prev);
2844          }
2845
2846          if(curExternal)
2847             symbol.id = curExternal.symbol.idCode;
2848       }
2849    }
2850    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2851 }
2852
2853 void DeclareGlobalData(GlobalData data)
2854 {
2855    Symbol symbol = data.symbol;
2856    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2857    {
2858       if(inCompiler)
2859       {
2860          if(!symbol)
2861             symbol = data.symbol = Symbol { };
2862       }
2863       if(!data.dataType)
2864          data.dataType = ProcessTypeString(data.dataTypeString, false);
2865       DeclareType(data.dataType, true, true);
2866       if(inCompiler)
2867       {
2868          if(!symbol.pointerExternal)
2869          {
2870             // We need a declaration here :)
2871             Declaration decl;
2872             OldList * specifiers, * declarators;
2873             Declarator d;
2874             External external;
2875
2876             specifiers = MkList();
2877             declarators = MkList();
2878
2879             ListAdd(specifiers, MkSpecifier(EXTERN));
2880             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2881             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2882
2883             ListAdd(declarators, MkInitDeclarator(d, null));
2884
2885             decl = MkDeclaration(specifiers, declarators);
2886             external = MkExternalDeclaration(decl);
2887             if(curExternal)
2888                ast->Insert(curExternal.prev, external);
2889             external.symbol = symbol;
2890             symbol.pointerExternal = external;
2891          }
2892          else
2893          {
2894             // Move declaration higher...
2895             ast->Move(symbol.pointerExternal, curExternal.prev);
2896          }
2897
2898          if(curExternal)
2899             symbol.id = curExternal.symbol.idCode;
2900       }
2901    }
2902 }
2903
2904 class Conversion : struct
2905 {
2906    Conversion prev, next;
2907    Property convert;
2908    bool isGet;
2909    Type resultType;
2910 };
2911
2912 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2913 {
2914    if(source && dest)
2915    {
2916       // Property convert;
2917
2918       if(source.kind == templateType && dest.kind != templateType)
2919       {
2920          Type type = ProcessTemplateParameterType(source.templateParameter);
2921          if(type) source = type;
2922       }
2923
2924       if(dest.kind == templateType && source.kind != templateType)
2925       {
2926          Type type = ProcessTemplateParameterType(dest.templateParameter);
2927          if(type) dest = type;
2928       }
2929
2930       if(dest.classObjectType == typedObject)
2931       {
2932          if(source.classObjectType != anyObject)
2933             return true;
2934          else
2935          {
2936             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2937             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2938             {
2939                return true;
2940             }
2941          }
2942       }
2943       else
2944       {
2945          if(source.classObjectType == anyObject)
2946             return true;
2947          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2948             return true;
2949       }
2950
2951       if((dest.kind == structType && source.kind == structType) ||
2952          (dest.kind == unionType && source.kind == unionType))
2953       {
2954          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2955              (source.members.first && source.members.first == dest.members.first))
2956             return true;
2957       }
2958
2959       if(dest.kind == ellipsisType && source.kind != voidType)
2960          return true;
2961
2962       if(dest.kind == pointerType && dest.type.kind == voidType &&
2963          ((source.kind == classType && (!source._class || !source._class.registered || source._class.registered.type == structClass || source._class.registered.type == normalClass || source._class.registered.type == noHeadClass || source._class.registered.type == systemClass))
2964          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2965
2966          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2967
2968          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2969          return true;
2970       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2971          ((dest.kind == classType && (!dest._class || !dest._class.registered || dest._class.registered.type == structClass || dest._class.registered.type == normalClass || dest._class.registered.type == noHeadClass || dest._class.registered.type == systemClass))
2972          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2973
2974          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2975
2976          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2977          return true;
2978
2979       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2980       {
2981          if(source._class.registered && source._class.registered.type == unitClass)
2982          {
2983             if(conversions != null)
2984             {
2985                if(source._class.registered == dest._class.registered)
2986                   return true;
2987             }
2988             else
2989             {
2990                Class sourceBase, destBase;
2991                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2992                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2993                if(sourceBase == destBase)
2994                   return true;
2995             }
2996          }
2997          // Don't match enum inheriting from other enum if resolving enumeration values
2998          // TESTING: !dest.classObjectType
2999          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3000             (enumBaseType ||
3001                (!source._class.registered || source._class.registered.type != enumClass) ||
3002                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3003             return true;
3004          else
3005          {
3006             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3007             if(enumBaseType &&
3008                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3009                ((source._class && source._class.registered && source._class.registered.type != enumClass) || source.kind == classType)) // Added this here for a base enum to be acceptable for a derived enum (#139)
3010             {
3011                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3012                {
3013                   return true;
3014                }
3015             }
3016          }
3017       }
3018
3019       // JUST ADDED THIS...
3020       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3021          return true;
3022
3023       if(doConversion)
3024       {
3025          // Just added this for Straight conversion of ColorAlpha => Color
3026          if(source.kind == classType)
3027          {
3028             Class _class;
3029             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3030             {
3031                Property convert;
3032                for(convert = _class.conversions.first; convert; convert = convert.next)
3033                {
3034                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3035                   {
3036                      Conversion after = (conversions != null) ? conversions.last : null;
3037
3038                      if(!convert.dataType)
3039                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3040                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3041                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3042                         MatchTypes(convert.dataType, dest, conversions, null, null,
3043                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3044                               convert.dataType.kind == classType, false, true))
3045                      {
3046                         if(!conversions && !convert.Get)
3047                            return true;
3048                         else if(conversions != null)
3049                         {
3050                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3051                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3052                               (dest.kind != classType || dest._class.registered != _class.base))
3053                               return true;
3054                            else
3055                            {
3056                               Conversion conv { convert = convert, isGet = true };
3057                               // conversions.Add(conv);
3058                               conversions.Insert(after, conv);
3059                               return true;
3060                            }
3061                         }
3062                      }
3063                   }
3064                }
3065             }
3066          }
3067
3068          // MOVING THIS??
3069
3070          if(dest.kind == classType)
3071          {
3072             Class _class;
3073             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3074             {
3075                Property convert;
3076                for(convert = _class.conversions.first; convert; convert = convert.next)
3077                {
3078                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3079                   {
3080                      // Conversion after = (conversions != null) ? conversions.last : null;
3081
3082                      if(!convert.dataType)
3083                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3084                      // Just added this equality check to prevent recursion.... Make it safer?
3085                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3086                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3087                      {
3088                         if(!conversions && !convert.Set)
3089                            return true;
3090                         else if(conversions != null)
3091                         {
3092                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3093                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3094                               (source.kind != classType || source._class.registered != _class.base))
3095                               return true;
3096                            else
3097                            {
3098                               // *** Testing this! ***
3099                               Conversion conv { convert = convert };
3100                               conversions.Add(conv);
3101                               //conversions.Insert(after, conv);
3102                               return true;
3103                            }
3104                         }
3105                      }
3106                   }
3107                }
3108             }
3109             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3110             {
3111                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3112                   (source.kind != classType || source._class.registered.type != structClass))
3113                   return true;
3114             }*/
3115
3116             // TESTING THIS... IS THIS OK??
3117             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3118             {
3119                if(!dest._class.registered.dataType)
3120                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3121                // Only support this for classes...
3122                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3123                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3124                {
3125                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false))
3126                   {
3127                      return true;
3128                   }
3129                }
3130             }
3131          }
3132
3133          // Moved this lower
3134          if(source.kind == classType)
3135          {
3136             Class _class;
3137             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3138             {
3139                Property convert;
3140                for(convert = _class.conversions.first; convert; convert = convert.next)
3141                {
3142                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3143                   {
3144                      Conversion after = (conversions != null) ? conversions.last : null;
3145
3146                      if(!convert.dataType)
3147                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3148                      if(convert.dataType != source &&
3149                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3150                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true))
3151                      {
3152                         if(!conversions && !convert.Get)
3153                            return true;
3154                         else if(conversions != null)
3155                         {
3156                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3157                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3158                               (dest.kind != classType || dest._class.registered != _class.base))
3159                               return true;
3160                            else
3161                            {
3162                               Conversion conv { convert = convert, isGet = true };
3163
3164                               // conversions.Add(conv);
3165                               conversions.Insert(after, conv);
3166                               return true;
3167                            }
3168                         }
3169                      }
3170                   }
3171                }
3172             }
3173
3174             // TESTING THIS... IS THIS OK??
3175             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3176             {
3177                if(!source._class.registered.dataType)
3178                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3179                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3180                {
3181                   if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, source._class.registered.dataType.kind == classType, source._class.registered.dataType.kind == classType, false, false))
3182                      return true;
3183                   // For bool to be accepted by byte, short, etc.
3184                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false))
3185                      return true;
3186                }
3187             }
3188          }
3189       }
3190
3191       if(source.kind == classType || source.kind == subClassType)
3192          ;
3193       else if(dest.kind == source.kind &&
3194          (dest.kind != structType && dest.kind != unionType &&
3195           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3196           return true;
3197       // RECENTLY ADDED THESE
3198       else if(dest.kind == doubleType && source.kind == floatType)
3199          return true;
3200       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3201          return true;
3202       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3203          return true;
3204       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3205          return true;
3206       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3207          return true;
3208       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3209          return true;
3210       else if(source.kind == enumType &&
3211          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3212           return true;
3213       else if(dest.kind == enumType && !isConversionExploration &&
3214          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3215           return true;
3216       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3217               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3218       {
3219          Type paramSource, paramDest;
3220
3221          if(dest.kind == methodType)
3222             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3223          if(source.kind == methodType)
3224             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3225
3226          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3227          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3228          if(dest.kind == methodType)
3229             dest = dest.method.dataType;
3230          if(source.kind == methodType)
3231             source = source.method.dataType;
3232
3233          paramSource = source.params.first;
3234          if(paramSource && paramSource.kind == voidType) paramSource = null;
3235          paramDest = dest.params.first;
3236          if(paramDest && paramDest.kind == voidType) paramDest = null;
3237
3238
3239          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3240             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3241          {
3242             // Source thisClass must be derived from destination thisClass
3243             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3244                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3245             {
3246                if(paramDest && paramDest.kind == classType)
3247                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3248                else
3249                   Compiler_Error($"method class should not take an object\n");
3250                return false;
3251             }
3252             paramDest = paramDest.next;
3253          }
3254          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3255          {
3256             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3257             {
3258                if(dest.thisClass)
3259                {
3260                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3261                   {
3262                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3263                      return false;
3264                   }
3265                }
3266                else
3267                {
3268                   // THIS WAS BACKWARDS:
3269                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3270                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3271                   {
3272                      if(owningClassDest)
3273                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3274                      else
3275                         Compiler_Error($"overriding class expected to be derived from method class\n");
3276                      return false;
3277                   }
3278                }
3279                paramSource = paramSource.next;
3280             }
3281             else
3282             {
3283                if(dest.thisClass)
3284                {
3285                   // Source thisClass must be derived from destination thisClass
3286                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3287                   {
3288                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3289                      return false;
3290                   }
3291                }
3292                else
3293                {
3294                   // THIS WAS BACKWARDS TOO??
3295                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3296                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3297                   {
3298                      //if(owningClass)
3299                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3300                      //else
3301                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3302                      return false;
3303                   }
3304                }
3305             }
3306          }
3307
3308
3309          // Source return type must be derived from destination return type
3310          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3311          {
3312             Compiler_Warning($"incompatible return type for function\n");
3313             return false;
3314          }
3315
3316          // Check parameters
3317
3318          for(; paramDest; paramDest = paramDest.next)
3319          {
3320             if(!paramSource)
3321             {
3322                //Compiler_Warning($"not enough parameters\n");
3323                Compiler_Error($"not enough parameters\n");
3324                return false;
3325             }
3326             {
3327                Type paramDestType = paramDest;
3328                Type paramSourceType = paramSource;
3329                Type type = paramDestType;
3330
3331                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3332                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3333                   paramSource.kind != templateType)
3334                {
3335                   int id = 0;
3336                   ClassTemplateParameter curParam = null;
3337                   Class sClass;
3338                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3339                   {
3340                      id = 0;
3341                      if(sClass.templateClass) sClass = sClass.templateClass;
3342                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3343                      {
3344                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3345                         {
3346                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3347                            {
3348                               if(sClass.templateClass) sClass = sClass.templateClass;
3349                               id += sClass.templateParams.count;
3350                            }
3351                            break;
3352                         }
3353                         id++;
3354                      }
3355                      if(curParam) break;
3356                   }
3357
3358                   if(curParam)
3359                   {
3360                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3361                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3362                   }
3363                }
3364
3365                // paramDest must be derived from paramSource
3366                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3367                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3368                {
3369                   char type[1024];
3370                   type[0] = 0;
3371                   PrintType(paramDest, type, false, true);
3372                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3373
3374                   if(paramDestType != paramDest)
3375                      FreeType(paramDestType);
3376                   return false;
3377                }
3378                if(paramDestType != paramDest)
3379                   FreeType(paramDestType);
3380             }
3381
3382             paramSource = paramSource.next;
3383          }
3384          if(paramSource)
3385          {
3386             Compiler_Error($"too many parameters\n");
3387             return false;
3388          }
3389          return true;
3390       }
3391       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3392       {
3393          return true;
3394       }
3395       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3396          (source.kind == arrayType || source.kind == pointerType))
3397       {
3398          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3399             return true;
3400       }
3401    }
3402    return false;
3403 }
3404
3405 static void FreeConvert(Conversion convert)
3406 {
3407    if(convert.resultType)
3408       FreeType(convert.resultType);
3409 }
3410
3411 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3412                               char * string, OldList conversions)
3413 {
3414    BTNamedLink link;
3415
3416    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3417    {
3418       Class _class = link.data;
3419       if(_class.type == enumClass)
3420       {
3421          OldList converts { };
3422          Type type { };
3423          type.kind = classType;
3424
3425          if(!_class.symbol)
3426             _class.symbol = FindClass(_class.fullName);
3427          type._class = _class.symbol;
3428
3429          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3430          {
3431             NamedLink value;
3432             Class enumClass = eSystem_FindClass(privateModule, "enum");
3433             if(enumClass)
3434             {
3435                Class baseClass;
3436                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3437                {
3438                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3439                   for(value = e.values.first; value; value = value.next)
3440                   {
3441                      if(!strcmp(value.name, string))
3442                         break;
3443                   }
3444                   if(value)
3445                   {
3446                      FreeExpContents(sourceExp);
3447                      FreeType(sourceExp.expType);
3448
3449                      sourceExp.isConstant = true;
3450                      sourceExp.expType = MkClassType(baseClass.fullName);
3451                      //if(inCompiler)
3452                      {
3453                         char constant[256];
3454                         sourceExp.type = constantExp;
3455                         if(!strcmp(baseClass.dataTypeString, "int"))
3456                            sprintf(constant, "%d",(int)value.data);
3457                         else
3458                            sprintf(constant, "0x%X",(int)value.data);
3459                         sourceExp.constant = CopyString(constant);
3460                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3461                      }
3462
3463                      while(converts.first)
3464                      {
3465                         Conversion convert = converts.first;
3466                         converts.Remove(convert);
3467                         conversions.Add(convert);
3468                      }
3469                      delete type;
3470                      return true;
3471                   }
3472                }
3473             }
3474          }
3475          if(converts.first)
3476             converts.Free(FreeConvert);
3477          delete type;
3478       }
3479    }
3480    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3481       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3482          return true;
3483    return false;
3484 }
3485
3486 public bool ModuleVisibility(Module searchIn, Module searchFor)
3487 {
3488    SubModule subModule;
3489
3490    if(searchFor == searchIn)
3491       return true;
3492
3493    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3494    {
3495       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3496       {
3497          if(ModuleVisibility(subModule.module, searchFor))
3498             return true;
3499       }
3500    }
3501    return false;
3502 }
3503
3504 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3505 {
3506    Module module;
3507
3508    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3509       return true;
3510    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3511       return true;
3512    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3513       return true;
3514
3515    for(module = mainModule.application.allModules.first; module; module = module.next)
3516    {
3517       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3518          return true;
3519    }
3520    return false;
3521 }
3522
3523 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3524 {
3525    Type source;
3526    Type realDest = dest;
3527    Type backupSourceExpType = null;
3528    Expression computedExp = sourceExp;
3529    dest.refCount++;
3530
3531    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3532       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3533    {
3534       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3535       ComputeExpression(computedExp /*sourceExp*/);
3536    }
3537
3538    source = sourceExp.expType;
3539
3540    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3541    {
3542       if(computedExp != sourceExp)
3543       {
3544          FreeExpression(computedExp);
3545          computedExp = sourceExp;
3546       }
3547       FreeType(dest);
3548       return true;
3549    }
3550
3551    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3552    {
3553        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3554        {
3555           Class sourceBase, destBase;
3556           for(sourceBase = source._class.registered;
3557               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3558               sourceBase = sourceBase.base);
3559           for(destBase = dest._class.registered;
3560               destBase && destBase.base && destBase.base.type != systemClass;
3561               destBase = destBase.base);
3562           //if(source._class.registered == dest._class.registered)
3563           if(sourceBase == destBase)
3564           {
3565             if(computedExp != sourceExp)
3566             {
3567                FreeExpression(computedExp);
3568                computedExp = sourceExp;
3569             }
3570             FreeType(dest);
3571             return true;
3572          }
3573       }
3574    }
3575
3576    if(source)
3577    {
3578       OldList * specs;
3579       bool flag = false;
3580       int64 value = MAXINT;
3581
3582       source.refCount++;
3583
3584       if(computedExp.type == constantExp)
3585       {
3586          if(source.isSigned)
3587             value = strtoll(computedExp.constant, null, 0);
3588          else
3589             value = strtoull(computedExp.constant, null, 0);
3590       }
3591       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3592       {
3593          if(source.isSigned)
3594             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3595          else
3596             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3597       }
3598       if(computedExp != sourceExp)
3599       {
3600          FreeExpression(computedExp);
3601          computedExp = sourceExp;
3602       }
3603
3604       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3605          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3606       {
3607          FreeType(source);
3608          source = Type { kind = intType, isSigned = false, refCount = 1 };
3609       }
3610
3611       if(dest.kind == classType)
3612       {
3613          Class _class = dest._class ? dest._class.registered : null;
3614
3615          if(_class && _class.type == unitClass)
3616          {
3617             if(source.kind != classType)
3618             {
3619                Type tempType { };
3620                Type tempDest, tempSource;
3621
3622                for(; _class.base.type != systemClass; _class = _class.base);
3623                tempSource = dest;
3624                tempDest = tempType;
3625
3626                tempType.kind = classType;
3627                if(!_class.symbol)
3628                   _class.symbol = FindClass(_class.fullName);
3629
3630                tempType._class = _class.symbol;
3631                tempType.truth = dest.truth;
3632                if(tempType._class)
3633                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3634
3635                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3636                backupSourceExpType = sourceExp.expType;
3637                sourceExp.expType = dest; dest.refCount++;
3638                //sourceExp.expType = MkClassType(_class.fullName);
3639                flag = true;
3640
3641                delete tempType;
3642             }
3643          }
3644
3645
3646          // Why wasn't there something like this?
3647          if(_class && _class.type == bitClass && source.kind != classType)
3648          {
3649             if(!dest._class.registered.dataType)
3650                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3651             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3652             {
3653                FreeType(source);
3654                FreeType(sourceExp.expType);
3655                source = sourceExp.expType = MkClassType(dest._class.string);
3656                source.refCount++;
3657
3658                //source.kind = classType;
3659                //source._class = dest._class;
3660             }
3661          }
3662
3663          // Adding two enumerations
3664          /*
3665          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3666          {
3667             if(!source._class.registered.dataType)
3668                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3669             if(!dest._class.registered.dataType)
3670                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3671
3672             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3673             {
3674                FreeType(source);
3675                source = sourceExp.expType = MkClassType(dest._class.string);
3676                source.refCount++;
3677
3678                //source.kind = classType;
3679                //source._class = dest._class;
3680             }
3681          }*/
3682
3683          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3684          {
3685             OldList * specs = MkList();
3686             Declarator decl;
3687             char string[1024];
3688
3689             ReadString(string, sourceExp.string);
3690             decl = SpecDeclFromString(string, specs, null);
3691
3692             FreeExpContents(sourceExp);
3693             FreeType(sourceExp.expType);
3694
3695             sourceExp.type = classExp;
3696             sourceExp._classExp.specifiers = specs;
3697             sourceExp._classExp.decl = decl;
3698             sourceExp.expType = dest;
3699             dest.refCount++;
3700
3701             FreeType(source);
3702             FreeType(dest);
3703             if(backupSourceExpType) FreeType(backupSourceExpType);
3704             return true;
3705          }
3706       }
3707       else if(source.kind == classType)
3708       {
3709          Class _class = source._class ? source._class.registered : null;
3710
3711          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3712          {
3713             /*
3714             if(dest.kind != classType)
3715             {
3716                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3717                if(!source._class.registered.dataType)
3718                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3719
3720                FreeType(dest);
3721                dest = MkClassType(source._class.string);
3722                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3723                //   dest = MkClassType(source._class.string);
3724             }
3725             */
3726
3727             if(dest.kind != classType)
3728             {
3729                Type tempType { };
3730                Type tempDest, tempSource;
3731
3732                if(!source._class.registered.dataType)
3733                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3734
3735                for(; _class.base.type != systemClass; _class = _class.base);
3736                tempDest = source;
3737                tempSource = tempType;
3738                tempType.kind = classType;
3739                tempType._class = FindClass(_class.fullName);
3740                tempType.truth = source.truth;
3741                tempType.classObjectType = source.classObjectType;
3742
3743                if(tempType._class)
3744                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3745
3746                // PUT THIS BACK TESTING UNITS?
3747                if(conversions.last)
3748                {
3749                   ((Conversion)(conversions.last)).resultType = dest;
3750                   dest.refCount++;
3751                }
3752
3753                FreeType(sourceExp.expType);
3754                sourceExp.expType = MkClassType(_class.fullName);
3755                sourceExp.expType.truth = source.truth;
3756                sourceExp.expType.classObjectType = source.classObjectType;
3757
3758                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3759
3760                if(!sourceExp.destType)
3761                {
3762                   FreeType(sourceExp.destType);
3763                   sourceExp.destType = sourceExp.expType;
3764                   if(sourceExp.expType)
3765                      sourceExp.expType.refCount++;
3766                }
3767                //flag = true;
3768                //source = _class.dataType;
3769
3770
3771                // TOCHECK: TESTING THIS NEW CODE
3772                if(!_class.dataType)
3773                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3774                FreeType(dest);
3775                dest = MkClassType(source._class.string);
3776                dest.truth = source.truth;
3777                dest.classObjectType = source.classObjectType;
3778
3779                FreeType(source);
3780                source = _class.dataType;
3781                source.refCount++;
3782
3783                delete tempType;
3784             }
3785          }
3786       }
3787
3788       if(!flag)
3789       {
3790          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3791          {
3792             FreeType(source);
3793             FreeType(dest);
3794             return true;
3795          }
3796       }
3797
3798       // Implicit Casts
3799       /*
3800       if(source.kind == classType)
3801       {
3802          Class _class = source._class.registered;
3803          if(_class.type == unitClass)
3804          {
3805             if(!_class.dataType)
3806                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3807             source = _class.dataType;
3808          }
3809       }*/
3810
3811       if(dest.kind == classType)
3812       {
3813          Class _class = dest._class ? dest._class.registered : null;
3814          bool fittingValue = false;
3815          if(_class && _class.type == enumClass)
3816          {
3817             Class enumClass = eSystem_FindClass(privateModule, "enum");
3818             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3819             if(c && value >= 0 && value <= c.largest)
3820                fittingValue = true;
3821          }
3822
3823          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3824             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3825          {
3826             if(_class.type == normalClass || _class.type == noHeadClass)
3827             {
3828                Expression newExp { };
3829                *newExp = *sourceExp;
3830                if(sourceExp.destType) sourceExp.destType.refCount++;
3831                if(sourceExp.expType)  sourceExp.expType.refCount++;
3832                sourceExp.type = castExp;
3833                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3834                sourceExp.cast.exp = newExp;
3835                FreeType(sourceExp.expType);
3836                sourceExp.expType = null;
3837                ProcessExpressionType(sourceExp);
3838
3839                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3840                if(!inCompiler)
3841                {
3842                   FreeType(sourceExp.expType);
3843                   sourceExp.expType = dest;
3844                }
3845
3846                FreeType(source);
3847                if(inCompiler) FreeType(dest);
3848
3849                if(backupSourceExpType) FreeType(backupSourceExpType);
3850                return true;
3851             }
3852
3853             if(!_class.dataType)
3854                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3855             FreeType(dest);
3856             dest = _class.dataType;
3857             dest.refCount++;
3858          }
3859
3860          // Accept lower precision types for units, since we want to keep the unit type
3861          if(dest.kind == doubleType &&
3862             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3863              source.kind == charType || source.kind == _BoolType))
3864          {
3865             specs = MkListOne(MkSpecifier(DOUBLE));
3866          }
3867          else if(dest.kind == floatType &&
3868             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3869             source.kind == _BoolType || source.kind == doubleType))
3870          {
3871             specs = MkListOne(MkSpecifier(FLOAT));
3872          }
3873          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3874             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3875          {
3876             specs = MkList();
3877             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3878             ListAdd(specs, MkSpecifier(INT64));
3879          }
3880          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3881             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3882          {
3883             specs = MkList();
3884             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3885             ListAdd(specs, MkSpecifier(INT));
3886          }
3887          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3888             source.kind == floatType || source.kind == doubleType))
3889          {
3890             specs = MkList();
3891             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3892             ListAdd(specs, MkSpecifier(SHORT));
3893          }
3894          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3895             source.kind == floatType || source.kind == doubleType))
3896          {
3897             specs = MkList();
3898             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3899             ListAdd(specs, MkSpecifier(CHAR));
3900          }
3901          else
3902          {
3903             FreeType(source);
3904             FreeType(dest);
3905             if(backupSourceExpType)
3906             {
3907                // Failed to convert: revert previous exp type
3908                if(sourceExp.expType) FreeType(sourceExp.expType);
3909                sourceExp.expType = backupSourceExpType;
3910             }
3911             return false;
3912          }
3913       }
3914       else if(dest.kind == doubleType &&
3915          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3916           source.kind == _BoolType || source.kind == charType))
3917       {
3918          specs = MkListOne(MkSpecifier(DOUBLE));
3919       }
3920       else if(dest.kind == floatType &&
3921          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3922       {
3923          specs = MkListOne(MkSpecifier(FLOAT));
3924       }
3925       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3926          (value == 1 || value == 0))
3927       {
3928          specs = MkList();
3929          ListAdd(specs, MkSpecifier(BOOL));
3930       }
3931       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3932          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3933       {
3934          specs = MkList();
3935          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3936          ListAdd(specs, MkSpecifier(CHAR));
3937       }
3938       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3939          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3940       {
3941          specs = MkList();
3942          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3943          ListAdd(specs, MkSpecifier(SHORT));
3944       }
3945       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3946       {
3947          specs = MkList();
3948          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3949          ListAdd(specs, MkSpecifier(INT));
3950       }
3951       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3952       {
3953          specs = MkList();
3954          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3955          ListAdd(specs, MkSpecifier(INT64));
3956       }
3957       else if(dest.kind == enumType &&
3958          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3959       {
3960          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3961       }
3962       else
3963       {
3964          FreeType(source);
3965          FreeType(dest);
3966          if(backupSourceExpType)
3967          {
3968             // Failed to convert: revert previous exp type
3969             if(sourceExp.expType) FreeType(sourceExp.expType);
3970             sourceExp.expType = backupSourceExpType;
3971          }
3972          return false;
3973       }
3974
3975       if(!flag && !sourceExp.opDestType)
3976       {
3977          Expression newExp { };
3978          *newExp = *sourceExp;
3979          newExp.prev = null;
3980          newExp.next = null;
3981          if(sourceExp.destType) sourceExp.destType.refCount++;
3982          if(sourceExp.expType)  sourceExp.expType.refCount++;
3983
3984          sourceExp.type = castExp;
3985          if(realDest.kind == classType)
3986          {
3987             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3988             FreeList(specs, FreeSpecifier);
3989          }
3990          else
3991             sourceExp.cast.typeName = MkTypeName(specs, null);
3992          if(newExp.type == opExp)
3993          {
3994             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3995          }
3996          else
3997             sourceExp.cast.exp = newExp;
3998
3999          FreeType(sourceExp.expType);
4000          sourceExp.expType = null;
4001          ProcessExpressionType(sourceExp);
4002       }
4003       else
4004          FreeList(specs, FreeSpecifier);
4005
4006       FreeType(dest);
4007       FreeType(source);
4008       if(backupSourceExpType) FreeType(backupSourceExpType);
4009
4010       return true;
4011    }
4012    else
4013    {
4014       if(computedExp != sourceExp)
4015       {
4016          FreeExpression(computedExp);
4017          computedExp = sourceExp;
4018       }
4019
4020       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4021       if(sourceExp.type == identifierExp)
4022       {
4023          Identifier id = sourceExp.identifier;
4024          if(dest.kind == classType)
4025          {
4026             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4027             {
4028                Class _class = dest._class.registered;
4029                Class enumClass = eSystem_FindClass(privateModule, "enum");
4030                if(enumClass)
4031                {
4032                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4033                   {
4034                      NamedLink value;
4035                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4036                      for(value = e.values.first; value; value = value.next)
4037                      {
4038                         if(!strcmp(value.name, id.string))
4039                            break;
4040                      }
4041                      if(value)
4042                      {
4043                         FreeExpContents(sourceExp);
4044                         FreeType(sourceExp.expType);
4045
4046                         sourceExp.isConstant = true;
4047                         sourceExp.expType = MkClassType(_class.fullName);
4048                         //if(inCompiler)
4049                         {
4050                            char constant[256];
4051                            sourceExp.type = constantExp;
4052                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
4053                               sprintf(constant, "%d", (int) value.data);
4054                            else
4055                               sprintf(constant, "0x%X", (int) value.data);
4056                            sourceExp.constant = CopyString(constant);
4057                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4058                         }
4059                         FreeType(dest);
4060                         return true;
4061                      }
4062                   }
4063                }
4064             }
4065          }
4066
4067          // Loop through all enum classes
4068          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4069          {
4070             FreeType(dest);
4071             return true;
4072          }
4073       }
4074       FreeType(dest);
4075    }
4076    return false;
4077 }
4078
4079 #define TERTIARY(o, name, m, t, p) \
4080    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4081    {                                                              \
4082       exp.type = constantExp;                                    \
4083       exp.string = p(op1.m ? op2.m : op3.m);                     \
4084       if(!exp.expType) \
4085          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4086       return true;                                                \
4087    }
4088
4089 #define BINARY(o, name, m, t, p) \
4090    static bool name(Expression exp, Operand op1, Operand op2)   \
4091    {                                                              \
4092       t value2 = op2.m;                                           \
4093       exp.type = constantExp;                                    \
4094       exp.string = p((t)(op1.m o value2));                     \
4095       if(!exp.expType) \
4096          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4097       return true;                                                \
4098    }
4099
4100 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4101    static bool name(Expression exp, Operand op1, Operand op2)   \
4102    {                                                              \
4103       t value2 = op2.m;                                           \
4104       exp.type = constantExp;                                    \
4105       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4106       if(!exp.expType) \
4107          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4108       return true;                                                \
4109    }
4110
4111 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4112    static bool name(Expression exp, Operand op1, Operand op2)   \
4113    {                                                              \
4114       t value2 = op2.m;                                           \
4115       exp.type = constantExp;                                    \
4116       exp.string = p(op1.m o value2);             \
4117       if(!exp.expType) \
4118          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4119       return true;                                                \
4120    }
4121
4122 #define UNARY(o, name, m, t, p) \
4123    static bool name(Expression exp, Operand op1)                \
4124    {                                                              \
4125       exp.type = constantExp;                                    \
4126       exp.string = p((t)(o op1.m));                                   \
4127       if(!exp.expType) \
4128          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4129       return true;                                                \
4130    }
4131
4132 #define OPERATOR_ALL(macro, o, name) \
4133    macro(o, Int##name, i, int, PrintInt) \
4134    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4135    macro(o, Int64##name, i64, int64, PrintInt64) \
4136    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4137    macro(o, Short##name, s, short, PrintShort) \
4138    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4139    macro(o, Char##name, c, char, PrintChar) \
4140    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4141    macro(o, Float##name, f, float, PrintFloat) \
4142    macro(o, Double##name, d, double, PrintDouble)
4143
4144 #define OPERATOR_INTTYPES(macro, o, name) \
4145    macro(o, Int##name, i, int, PrintInt) \
4146    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4147    macro(o, Int64##name, i64, int64, PrintInt64) \
4148    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4149    macro(o, Short##name, s, short, PrintShort) \
4150    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4151    macro(o, Char##name, c, char, PrintChar) \
4152    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4153
4154 #define OPERATOR_REALTYPES(macro, o, name) \
4155    macro(o, Float##name, f, float, PrintFloat) \
4156    macro(o, Double##name, d, double, PrintDouble)
4157
4158 // binary arithmetic
4159 OPERATOR_ALL(BINARY, +, Add)
4160 OPERATOR_ALL(BINARY, -, Sub)
4161 OPERATOR_ALL(BINARY, *, Mul)
4162 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4163 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4164 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4165
4166 // unary arithmetic
4167 OPERATOR_ALL(UNARY, -, Neg)
4168
4169 // unary arithmetic increment and decrement
4170 OPERATOR_ALL(UNARY, ++, Inc)
4171 OPERATOR_ALL(UNARY, --, Dec)
4172
4173 // binary arithmetic assignment
4174 OPERATOR_ALL(BINARY, =, Asign)
4175 OPERATOR_ALL(BINARY, +=, AddAsign)
4176 OPERATOR_ALL(BINARY, -=, SubAsign)
4177 OPERATOR_ALL(BINARY, *=, MulAsign)
4178 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4179 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4180 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4181
4182 // binary bitwise
4183 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4184 OPERATOR_INTTYPES(BINARY, |, BitOr)
4185 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4186 OPERATOR_INTTYPES(BINARY, <<, LShift)
4187 OPERATOR_INTTYPES(BINARY, >>, RShift)
4188
4189 // unary bitwise
4190 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4191
4192 // binary bitwise assignment
4193 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4194 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4195 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4196 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4197 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4198
4199 // unary logical negation
4200 OPERATOR_INTTYPES(UNARY, !, Not)
4201
4202 // binary logical equality
4203 OPERATOR_ALL(BINARY, ==, Equ)
4204 OPERATOR_ALL(BINARY, !=, Nqu)
4205
4206 // binary logical
4207 OPERATOR_ALL(BINARY, &&, And)
4208 OPERATOR_ALL(BINARY, ||, Or)
4209
4210 // binary logical relational
4211 OPERATOR_ALL(BINARY, >, Grt)
4212 OPERATOR_ALL(BINARY, <, Sma)
4213 OPERATOR_ALL(BINARY, >=, GrtEqu)
4214 OPERATOR_ALL(BINARY, <=, SmaEqu)
4215
4216 // tertiary condition operator
4217 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4218
4219 //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
4220 #define OPERATOR_TABLE_ALL(name, type) \
4221     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4222                           type##Neg, \
4223                           type##Inc, type##Dec, \
4224                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4225                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4226                           type##BitNot, \
4227                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4228                           type##Not, \
4229                           type##Equ, type##Nqu, \
4230                           type##And, type##Or, \
4231                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4232                         }; \
4233
4234 #define OPERATOR_TABLE_INTTYPES(name, type) \
4235     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4236                           type##Neg, \
4237                           type##Inc, type##Dec, \
4238                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4239                           null, null, null, null, null, \
4240                           null, \
4241                           null, null, null, null, null, \
4242                           null, \
4243                           type##Equ, type##Nqu, \
4244                           type##And, type##Or, \
4245                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4246                         }; \
4247
4248 OPERATOR_TABLE_ALL(int, Int)
4249 OPERATOR_TABLE_ALL(uint, UInt)
4250 OPERATOR_TABLE_ALL(int64, Int64)
4251 OPERATOR_TABLE_ALL(uint64, UInt64)
4252 OPERATOR_TABLE_ALL(short, Short)
4253 OPERATOR_TABLE_ALL(ushort, UShort)
4254 OPERATOR_TABLE_INTTYPES(float, Float)
4255 OPERATOR_TABLE_INTTYPES(double, Double)
4256 OPERATOR_TABLE_ALL(char, Char)
4257 OPERATOR_TABLE_ALL(uchar, UChar)
4258
4259 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4260 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4261 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4262 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4263 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4264 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4265 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4266 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4267
4268 public void ReadString(char * output,  char * string)
4269 {
4270    int len = strlen(string);
4271    int c,d = 0;
4272    bool quoted = false, escaped = false;
4273    for(c = 0; c<len; c++)
4274    {
4275       char ch = string[c];
4276       if(escaped)
4277       {
4278          switch(ch)
4279          {
4280             case 'n': output[d] = '\n'; break;
4281             case 't': output[d] = '\t'; break;
4282             case 'a': output[d] = '\a'; break;
4283             case 'b': output[d] = '\b'; break;
4284             case 'f': output[d] = '\f'; break;
4285             case 'r': output[d] = '\r'; break;
4286             case 'v': output[d] = '\v'; break;
4287             case '\\': output[d] = '\\'; break;
4288             case '\"': output[d] = '\"'; break;
4289             case '\'': output[d] = '\''; break;
4290             default: output[d] = ch;
4291          }
4292          d++;
4293          escaped = false;
4294       }
4295       else
4296       {
4297          if(ch == '\"')
4298             quoted ^= true;
4299          else if(quoted)
4300          {
4301             if(ch == '\\')
4302                escaped = true;
4303             else
4304                output[d++] = ch;
4305          }
4306       }
4307    }
4308    output[d] = '\0';
4309 }
4310
4311 // String Unescape Copy
4312
4313 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4314 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4315 public int UnescapeString(char * d, char * s, int len)
4316 {
4317    int j = 0, k = 0;
4318    char ch;
4319    while(j < len && (ch = s[j]))
4320    {
4321       switch(ch)
4322       {
4323          case '\\':
4324             switch((ch = s[++j]))
4325             {
4326                case 'n': d[k] = '\n'; break;
4327                case 't': d[k] = '\t'; break;
4328                case 'a': d[k] = '\a'; break;
4329                case 'b': d[k] = '\b'; break;
4330                case 'f': d[k] = '\f'; break;
4331                case 'r': d[k] = '\r'; break;
4332                case 'v': d[k] = '\v'; break;
4333                case '\\': d[k] = '\\'; break;
4334                case '\"': d[k] = '\"'; break;
4335                case '\'': d[k] = '\''; break;
4336                default: d[k] = '\\'; d[k] = ch;
4337             }
4338             break;
4339          default:
4340             d[k] = ch;
4341       }
4342       j++, k++;
4343    }
4344    d[k] = '\0';
4345    return k;
4346 }
4347
4348 public char * OffsetEscapedString(char * s, int len, int offset)
4349 {
4350    char ch;
4351    int j = 0, k = 0;
4352    while(j < len && k < offset && (ch = s[j]))
4353    {
4354       if(ch == '\\') ++j;
4355       j++, k++;
4356    }
4357    return (k == offset) ? s + j : null;
4358 }
4359
4360 public Operand GetOperand(Expression exp)
4361 {
4362    Operand op { };
4363    Type type = exp.expType;
4364    if(type)
4365    {
4366       while(type.kind == classType &&
4367          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4368       {
4369          if(!type._class.registered.dataType)
4370             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4371          type = type._class.registered.dataType;
4372
4373       }
4374       if(exp.type == stringExp && op.kind == pointerType)
4375       {
4376          op.ui64 = (uint64)exp.string;
4377          op.kind = pointerType;
4378          op.ops = uint64Ops;
4379       }
4380       else if(exp.isConstant && exp.type == constantExp)
4381       {
4382          op.kind = type.kind;
4383          op.type = exp.expType;
4384
4385          switch(op.kind)
4386          {
4387             case _BoolType:
4388             case charType:
4389             {
4390                if(exp.constant[0] == '\'')
4391                {
4392                   op.c = exp.constant[1];
4393                   op.ops = charOps;
4394                }
4395                else if(type.isSigned)
4396                {
4397                   op.c = (char)strtol(exp.constant, null, 0);
4398                   op.ops = charOps;
4399                }
4400                else
4401                {
4402                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4403                   op.ops = ucharOps;
4404                }
4405                break;
4406             }
4407             case shortType:
4408                if(type.isSigned)
4409                {
4410                   op.s = (short)strtol(exp.constant, null, 0);
4411                   op.ops = shortOps;
4412                }
4413                else
4414                {
4415                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4416                   op.ops = ushortOps;
4417                }
4418                break;
4419             case intType:
4420             case longType:
4421                if(type.isSigned)
4422                {
4423                   op.i = (int)strtol(exp.constant, null, 0);
4424                   op.ops = intOps;
4425                }
4426                else
4427                {
4428                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4429                   op.ops = uintOps;
4430                }
4431                op.kind = intType;
4432                break;
4433             case int64Type:
4434                if(type.isSigned)
4435                {
4436                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4437                   op.ops = int64Ops;
4438                }
4439                else
4440                {
4441                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4442                   op.ops = uint64Ops;
4443                }
4444                op.kind = int64Type;
4445                break;
4446             case intPtrType:
4447                if(type.isSigned)
4448                {
4449                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4450                   op.ops = int64Ops;
4451                }
4452                else
4453                {
4454                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4455                   op.ops = uint64Ops;
4456                }
4457                op.kind = int64Type;
4458                break;
4459             case intSizeType:
4460                if(type.isSigned)
4461                {
4462                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4463                   op.ops = int64Ops;
4464                }
4465                else
4466                {
4467                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4468                   op.ops = uint64Ops;
4469                }
4470                op.kind = int64Type;
4471                break;
4472             case floatType:
4473                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4474                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4475                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4476                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4477                else
4478                   op.f = (float)strtod(exp.constant, null);
4479                op.ops = floatOps;
4480                break;
4481             case doubleType:
4482                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4483                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4484                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4485                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4486                else
4487                   op.d = (double)strtod(exp.constant, null);
4488                op.ops = doubleOps;
4489                break;
4490             //case classType:    For when we have operator overloading...
4491             // Pointer additions
4492             //case functionType:
4493             case arrayType:
4494             case pointerType:
4495             case classType:
4496                op.ui64 = _strtoui64(exp.constant, null, 0);
4497                op.kind = pointerType;
4498                op.ops = uint64Ops;
4499                // op.ptrSize =
4500                break;
4501          }
4502       }
4503    }
4504    return op;
4505 }
4506
4507 static void UnusedFunction()
4508 {
4509    int a;
4510    a.OnGetString(0,0,0);
4511 }
4512 default:
4513 extern int __ecereVMethodID_class_OnGetString;
4514 public:
4515
4516 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4517 {
4518    DataMember dataMember;
4519    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4520    {
4521       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4522          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4523       else
4524       {
4525          Expression exp { };
4526          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4527          Type type;
4528          void * ptr = inst.data + dataMember.offset + offset;
4529          char * result = null;
4530          exp.loc = member.loc = inst.loc;
4531          ((Identifier)member.identifiers->first).loc = inst.loc;
4532
4533          if(!dataMember.dataType)
4534             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4535          type = dataMember.dataType;
4536          if(type.kind == classType)
4537          {
4538             Class _class = type._class.registered;
4539             if(_class.type == enumClass)
4540             {
4541                Class enumClass = eSystem_FindClass(privateModule, "enum");
4542                if(enumClass)
4543                {
4544                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4545                   NamedLink item;
4546                   for(item = e.values.first; item; item = item.next)
4547                   {
4548                      if((int)item.data == *(int *)ptr)
4549                      {
4550                         result = item.name;
4551                         break;
4552                      }
4553                   }
4554                   if(result)
4555                   {
4556                      exp.identifier = MkIdentifier(result);
4557                      exp.type = identifierExp;
4558                      exp.destType = MkClassType(_class.fullName);
4559                      ProcessExpressionType(exp);
4560                   }
4561                }
4562             }
4563             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4564             {
4565                if(!_class.dataType)
4566                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4567                type = _class.dataType;
4568             }
4569          }
4570          if(!result)
4571          {
4572             switch(type.kind)
4573             {
4574                case floatType:
4575                {
4576                   FreeExpContents(exp);
4577
4578                   exp.constant = PrintFloat(*(float*)ptr);
4579                   exp.type = constantExp;
4580                   break;
4581                }
4582                case doubleType:
4583                {
4584                   FreeExpContents(exp);
4585
4586                   exp.constant = PrintDouble(*(double*)ptr);
4587                   exp.type = constantExp;
4588                   break;
4589                }
4590                case intType:
4591                {
4592                   FreeExpContents(exp);
4593
4594                   exp.constant = PrintInt(*(int*)ptr);
4595                   exp.type = constantExp;
4596                   break;
4597                }
4598                case int64Type:
4599                {
4600                   FreeExpContents(exp);
4601
4602                   exp.constant = PrintInt64(*(int64*)ptr);
4603                   exp.type = constantExp;
4604                   break;
4605                }
4606                case intPtrType:
4607                {
4608                   FreeExpContents(exp);
4609                   // TODO: This should probably use proper type
4610                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4611                   exp.type = constantExp;
4612                   break;
4613                }
4614                case intSizeType:
4615                {
4616                   FreeExpContents(exp);
4617                   // TODO: This should probably use proper type
4618                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4619                   exp.type = constantExp;
4620                   break;
4621                }
4622                default:
4623                   Compiler_Error($"Unhandled type populating instance\n");
4624             }
4625          }
4626          ListAdd(memberList, member);
4627       }
4628
4629       if(parentDataMember.type == unionMember)
4630          break;
4631    }
4632 }
4633
4634 void PopulateInstance(Instantiation inst)
4635 {
4636    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4637    Class _class = classSym.registered;
4638    DataMember dataMember;
4639    OldList * memberList = MkList();
4640    // Added this check and ->Add to prevent memory leaks on bad code
4641    if(!inst.members)
4642       inst.members = MkListOne(MkMembersInitList(memberList));
4643    else
4644       inst.members->Add(MkMembersInitList(memberList));
4645    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4646    {
4647       if(!dataMember.isProperty)
4648       {
4649          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4650             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4651          else
4652          {
4653             Expression exp { };
4654             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4655             Type type;
4656             void * ptr = inst.data + dataMember.offset;
4657             char * result = null;
4658
4659             exp.loc = member.loc = inst.loc;
4660             ((Identifier)member.identifiers->first).loc = inst.loc;
4661
4662             if(!dataMember.dataType)
4663                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4664             type = dataMember.dataType;
4665             if(type.kind == classType)
4666             {
4667                Class _class = type._class.registered;
4668                if(_class.type == enumClass)
4669                {
4670                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4671                   if(enumClass)
4672                   {
4673                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4674                      NamedLink item;
4675                      for(item = e.values.first; item; item = item.next)
4676                      {
4677                         if((int)item.data == *(int *)ptr)
4678                         {
4679                            result = item.name;
4680                            break;
4681                         }
4682                      }
4683                   }
4684                   if(result)
4685                   {
4686                      exp.identifier = MkIdentifier(result);
4687                      exp.type = identifierExp;
4688                      exp.destType = MkClassType(_class.fullName);
4689                      ProcessExpressionType(exp);
4690                   }
4691                }
4692                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4693                {
4694                   if(!_class.dataType)
4695                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4696                   type = _class.dataType;
4697                }
4698             }
4699             if(!result)
4700             {
4701                switch(type.kind)
4702                {
4703                   case floatType:
4704                   {
4705                      exp.constant = PrintFloat(*(float*)ptr);
4706                      exp.type = constantExp;
4707                      break;
4708                   }
4709                   case doubleType:
4710                   {
4711                      exp.constant = PrintDouble(*(double*)ptr);
4712                      exp.type = constantExp;
4713                      break;
4714                   }
4715                   case intType:
4716                   {
4717                      exp.constant = PrintInt(*(int*)ptr);
4718                      exp.type = constantExp;
4719                      break;
4720                   }
4721                   case int64Type:
4722                   {
4723                      exp.constant = PrintInt64(*(int64*)ptr);
4724                      exp.type = constantExp;
4725                      break;
4726                   }
4727                   case intPtrType:
4728                   {
4729                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4730                      exp.type = constantExp;
4731                      break;
4732                   }
4733                   default:
4734                      Compiler_Error($"Unhandled type populating instance\n");
4735                }
4736             }
4737             ListAdd(memberList, member);
4738          }
4739       }
4740    }
4741 }
4742
4743 void ComputeInstantiation(Expression exp)
4744 {
4745    Instantiation inst = exp.instance;
4746    MembersInit members;
4747    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4748    Class _class = classSym ? classSym.registered : null;
4749    DataMember curMember = null;
4750    Class curClass = null;
4751    DataMember subMemberStack[256];
4752    int subMemberStackPos = 0;
4753    uint64 bits = 0;
4754
4755    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4756    {
4757       // Don't recompute the instantiation...
4758       // Non Simple classes will have become constants by now
4759       if(inst.data)
4760          return;
4761
4762       if(_class.type == normalClass || _class.type == noHeadClass)
4763       {
4764          inst.data = (byte *)eInstance_New(_class);
4765          if(_class.type == normalClass)
4766             ((Instance)inst.data)._refCount++;
4767       }
4768       else
4769          inst.data = new0 byte[_class.structSize];
4770    }
4771
4772    if(inst.members)
4773    {
4774       for(members = inst.members->first; members; members = members.next)
4775       {
4776          switch(members.type)
4777          {
4778             case dataMembersInit:
4779             {
4780                if(members.dataMembers)
4781                {
4782                   MemberInit member;
4783                   for(member = members.dataMembers->first; member; member = member.next)
4784                   {
4785                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4786                      bool found = false;
4787
4788                      Property prop = null;
4789                      DataMember dataMember = null;
4790                      Method method = null;
4791                      uint dataMemberOffset;
4792
4793                      if(!ident)
4794                      {
4795                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4796                         if(curMember)
4797                         {
4798                            if(curMember.isProperty)
4799                               prop = (Property)curMember;
4800                            else
4801                            {
4802                               dataMember = curMember;
4803
4804                               // CHANGED THIS HERE
4805                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4806
4807                               // 2013/17/29 -- It seems that this was missing here!
4808                               if(_class.type == normalClass)
4809                                  dataMemberOffset += _class.base.structSize;
4810                               // dataMemberOffset = dataMember.offset;
4811                            }
4812                            found = true;
4813                         }
4814                      }
4815                      else
4816                      {
4817                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4818                         if(prop)
4819                         {
4820                            found = true;
4821                            if(prop.memberAccess == publicAccess)
4822                            {
4823                               curMember = (DataMember)prop;
4824                               curClass = prop._class;
4825                            }
4826                         }
4827                         else
4828                         {
4829                            DataMember _subMemberStack[256];
4830                            int _subMemberStackPos = 0;
4831
4832                            // FILL MEMBER STACK
4833                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4834
4835                            if(dataMember)
4836                            {
4837                               found = true;
4838                               if(dataMember.memberAccess == publicAccess)
4839                               {
4840                                  curMember = dataMember;
4841                                  curClass = dataMember._class;
4842                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4843                                  subMemberStackPos = _subMemberStackPos;
4844                               }
4845                            }
4846                         }
4847                      }
4848
4849                      if(found && member.initializer && member.initializer.type == expInitializer)
4850                      {
4851                         Expression value = member.initializer.exp;
4852                         Type type = null;
4853                         bool deepMember = false;
4854                         if(prop)
4855                         {
4856                            type = prop.dataType;
4857                         }
4858                         else if(dataMember)
4859                         {
4860                            if(!dataMember.dataType)
4861                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4862
4863                            type = dataMember.dataType;
4864                         }
4865
4866                         if(ident && ident.next)
4867                         {
4868                            deepMember = true;
4869
4870                            // for(; ident && type; ident = ident.next)
4871                            for(ident = ident.next; ident && type; ident = ident.next)
4872                            {
4873                               if(type.kind == classType)
4874                               {
4875                                  prop = eClass_FindProperty(type._class.registered,
4876                                     ident.string, privateModule);
4877                                  if(prop)
4878                                     type = prop.dataType;
4879                                  else
4880                                  {
4881                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4882                                        ident.string, &dataMemberOffset, privateModule, null, null);
4883                                     if(dataMember)
4884                                        type = dataMember.dataType;
4885                                  }
4886                               }
4887                               else if(type.kind == structType || type.kind == unionType)
4888                               {
4889                                  Type memberType;
4890                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4891                                  {
4892                                     if(!strcmp(memberType.name, ident.string))
4893                                     {
4894                                        type = memberType;
4895                                        break;
4896                                     }
4897                                  }
4898                               }
4899                            }
4900                         }
4901                         if(value)
4902                         {
4903                            FreeType(value.destType);
4904                            value.destType = type;
4905                            if(type) type.refCount++;
4906                            ComputeExpression(value);
4907                         }
4908                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4909                         {
4910                            if(type.kind == classType)
4911                            {
4912                               Class _class = type._class.registered;
4913                               if(_class.type == bitClass || _class.type == unitClass ||
4914                                  _class.type == enumClass)
4915                               {
4916                                  if(!_class.dataType)
4917                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4918                                  type = _class.dataType;
4919                               }
4920                            }
4921
4922                            if(dataMember)
4923                            {
4924                               void * ptr = inst.data + dataMemberOffset;
4925
4926                               if(value.type == constantExp)
4927                               {
4928                                  switch(type.kind)
4929                                  {
4930                                     case intType:
4931                                     {
4932                                        GetInt(value, (int*)ptr);
4933                                        break;
4934                                     }
4935                                     case int64Type:
4936                                     {
4937                                        GetInt64(value, (int64*)ptr);
4938                                        break;
4939                                     }
4940                                     case intPtrType:
4941                                     {
4942                                        GetIntPtr(value, (intptr*)ptr);
4943                                        break;
4944                                     }
4945                                     case intSizeType:
4946                                     {
4947                                        GetIntSize(value, (intsize*)ptr);
4948                                        break;
4949                                     }
4950                                     case floatType:
4951                                     {
4952                                        GetFloat(value, (float*)ptr);
4953                                        break;
4954                                     }
4955                                     case doubleType:
4956                                     {
4957                                        GetDouble(value, (double *)ptr);
4958                                        break;
4959                                     }
4960                                  }
4961                               }
4962                               else if(value.type == instanceExp)
4963                               {
4964                                  if(type.kind == classType)
4965                                  {
4966                                     Class _class = type._class.registered;
4967                                     if(_class.type == structClass)
4968                                     {
4969                                        ComputeTypeSize(type);
4970                                        if(value.instance.data)
4971                                           memcpy(ptr, value.instance.data, type.size);
4972                                     }
4973                                  }
4974                               }
4975                            }
4976                            else if(prop)
4977                            {
4978                               if(value.type == instanceExp && value.instance.data)
4979                               {
4980                                  if(type.kind == classType)
4981                                  {
4982                                     Class _class = type._class.registered;
4983                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4984                                     {
4985                                        void (*Set)(void *, void *) = (void *)prop.Set;
4986                                        Set(inst.data, value.instance.data);
4987                                        PopulateInstance(inst);
4988                                     }
4989                                  }
4990                               }
4991                               else if(value.type == constantExp)
4992                               {
4993                                  switch(type.kind)
4994                                  {
4995                                     case doubleType:
4996                                     {
4997                                        void (*Set)(void *, double) = (void *)prop.Set;
4998                                        Set(inst.data, strtod(value.constant, null) );
4999                                        break;
5000                                     }
5001                                     case floatType:
5002                                     {
5003                                        void (*Set)(void *, float) = (void *)prop.Set;
5004                                        Set(inst.data, (float)(strtod(value.constant, null)));
5005                                        break;
5006                                     }
5007                                     case intType:
5008                                     {
5009                                        void (*Set)(void *, int) = (void *)prop.Set;
5010                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5011                                        break;
5012                                     }
5013                                     case int64Type:
5014                                     {
5015                                        void (*Set)(void *, int64) = (void *)prop.Set;
5016                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5017                                        break;
5018                                     }
5019                                     case intPtrType:
5020                                     {
5021                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5022                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5023                                        break;
5024                                     }
5025                                     case intSizeType:
5026                                     {
5027                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5028                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5029                                        break;
5030                                     }
5031                                  }
5032                               }
5033                               else if(value.type == stringExp)
5034                               {
5035                                  char temp[1024];
5036                                  ReadString(temp, value.string);
5037                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5038                               }
5039                            }
5040                         }
5041                         else if(!deepMember && type && _class.type == unitClass)
5042                         {
5043                            if(prop)
5044                            {
5045                               // Only support converting units to units for now...
5046                               if(value.type == constantExp)
5047                               {
5048                                  if(type.kind == classType)
5049                                  {
5050                                     Class _class = type._class.registered;
5051                                     if(_class.type == unitClass)
5052                                     {
5053                                        if(!_class.dataType)
5054                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5055                                        type = _class.dataType;
5056                                     }
5057                                  }
5058                                  // TODO: Assuming same base type for units...
5059                                  switch(type.kind)
5060                                  {
5061                                     case floatType:
5062                                     {
5063                                        float fValue;
5064                                        float (*Set)(float) = (void *)prop.Set;
5065                                        GetFloat(member.initializer.exp, &fValue);
5066                                        exp.constant = PrintFloat(Set(fValue));
5067                                        exp.type = constantExp;
5068                                        break;
5069                                     }
5070                                     case doubleType:
5071                                     {
5072                                        double dValue;
5073                                        double (*Set)(double) = (void *)prop.Set;
5074                                        GetDouble(member.initializer.exp, &dValue);
5075                                        exp.constant = PrintDouble(Set(dValue));
5076                                        exp.type = constantExp;
5077                                        break;
5078                                     }
5079                                  }
5080                               }
5081                            }
5082                         }
5083                         else if(!deepMember && type && _class.type == bitClass)
5084                         {
5085                            if(prop)
5086                            {
5087                               if(value.type == instanceExp && value.instance.data)
5088                               {
5089                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5090                                  bits = Set(value.instance.data);
5091                               }
5092                               else if(value.type == constantExp)
5093                               {
5094                               }
5095                            }
5096                            else if(dataMember)
5097                            {
5098                               BitMember bitMember = (BitMember) dataMember;
5099                               Type type;
5100                               uint64 part;
5101                               bits = (bits & ~bitMember.mask);
5102                               if(!bitMember.dataType)
5103                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5104                               type = bitMember.dataType;
5105                               if(type.kind == classType && type._class && type._class.registered)
5106                               {
5107                                  if(!type._class.registered.dataType)
5108                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5109                                  type = type._class.registered.dataType;
5110                               }
5111                               switch(type.kind)
5112                               {
5113                                  case _BoolType:
5114                                  case charType:       { byte v; type.isSigned ? GetChar(value, &v) : GetUChar(value, &v); part = (uint64)v; break; }
5115                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, &v) : GetUShort(value, &v); part = (uint64)v; break; }
5116                                  case intType:
5117                                  case longType:       { uint v; type.isSigned ? GetInt(value, &v) : GetUInt(value, &v); part = (uint64)v; break; }
5118                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, &v) : GetUInt64(value, &v); part = (uint64)v; break; }
5119                                  case intPtrType:     { intptr v; type.isSigned ? GetIntPtr(value, &v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5120                                  case intSizeType:    { intsize v; type.isSigned ? GetIntSize(value, &v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5121                               }
5122                               bits |= part << bitMember.pos;
5123                            }
5124                         }
5125                      }
5126                      else
5127                      {
5128                         if(_class && _class.type == unitClass)
5129                         {
5130                            ComputeExpression(member.initializer.exp);
5131                            exp.constant = member.initializer.exp.constant;
5132                            exp.type = constantExp;
5133
5134                            member.initializer.exp.constant = null;
5135                         }
5136                      }
5137                   }
5138                }
5139                break;
5140             }
5141          }
5142       }
5143    }
5144    if(_class && _class.type == bitClass)
5145    {
5146       exp.constant = PrintHexUInt(bits);
5147       exp.type = constantExp;
5148    }
5149    if(exp.type != instanceExp)
5150    {
5151       FreeInstance(inst);
5152    }
5153 }
5154
5155 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5156 {
5157    bool result = false;
5158    switch(kind)
5159    {
5160       case shortType:
5161          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5162             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5163          break;
5164       case intType:
5165       case longType:
5166          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5167             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5168          break;
5169       case int64Type:
5170          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5171             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5172             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5173          break;
5174       case floatType:
5175          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5176             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5177             result = GetOpFloat(op, &op.f);
5178          break;
5179       case doubleType:
5180          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5181             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5182             result = GetOpDouble(op, &op.d);
5183          break;
5184       case pointerType:
5185          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5186             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5187             result = GetOpUIntPtr(op, &op.ui64);
5188          break;
5189       case enumType:
5190          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5191             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5192             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5193          break;
5194       case intPtrType:
5195          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5196             result = isSigned ? GetOpIntPtr(op, &op.i64) : GetOpUIntPtr(op, &op.i64);
5197          break;
5198       case intSizeType:
5199          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5200             result = isSigned ? GetOpIntSize(op, &op.ui64) : GetOpUIntSize(op, &op.ui64);
5201          break;
5202    }
5203    return result;
5204 }
5205
5206 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5207 {
5208    if(exp.op.op == SIZEOF)
5209    {
5210       FreeExpContents(exp);
5211       exp.type = constantExp;
5212       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5213    }
5214    else
5215    {
5216       if(!exp.op.exp1)
5217       {
5218          switch(exp.op.op)
5219          {
5220             // unary arithmetic
5221             case '+':
5222             {
5223                // Provide default unary +
5224                Expression exp2 = exp.op.exp2;
5225                exp.op.exp2 = null;
5226                FreeExpContents(exp);
5227                FreeType(exp.expType);
5228                FreeType(exp.destType);
5229                *exp = *exp2;
5230                delete exp2;
5231                break;
5232             }
5233             case '-':
5234                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5235                break;
5236             // unary arithmetic increment and decrement
5237                   //OPERATOR_ALL(UNARY, ++, Inc)
5238                   //OPERATOR_ALL(UNARY, --, Dec)
5239             // unary bitwise
5240             case '~':
5241                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5242                break;
5243             // unary logical negation
5244             case '!':
5245                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5246                break;
5247          }
5248       }
5249       else
5250       {
5251          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5252          {
5253             if(Promote(op2, op1.kind, op1.type.isSigned))
5254                op2.kind = op1.kind, op2.ops = op1.ops;
5255             else if(Promote(op1, op2.kind, op2.type.isSigned))
5256                op1.kind = op2.kind, op1.ops = op2.ops;
5257          }
5258          switch(exp.op.op)
5259          {
5260             // binary arithmetic
5261             case '+':
5262                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5263                break;
5264             case '-':
5265                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5266                break;
5267             case '*':
5268                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5269                break;
5270             case '/':
5271                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5272                break;
5273             case '%':
5274                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5275                break;
5276             // binary arithmetic assignment
5277                   //OPERATOR_ALL(BINARY, =, Asign)
5278                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5279                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5280                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5281                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5282                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5283             // binary bitwise
5284             case '&':
5285                if(exp.op.exp2)
5286                {
5287                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5288                }
5289                break;
5290             case '|':
5291                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5292                break;
5293             case '^':
5294                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5295                break;
5296             case LEFT_OP:
5297                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5298                break;
5299             case RIGHT_OP:
5300                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5301                break;
5302             // binary bitwise assignment
5303                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5304                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5305                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5306                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5307                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5308             // binary logical equality
5309             case EQ_OP:
5310                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5311                break;
5312             case NE_OP:
5313                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5314                break;
5315             // binary logical
5316             case AND_OP:
5317                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5318                break;
5319             case OR_OP:
5320                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5321                break;
5322             // binary logical relational
5323             case '>':
5324                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5325                break;
5326             case '<':
5327                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5328                break;
5329             case GE_OP:
5330                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5331                break;
5332             case LE_OP:
5333                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5334                break;
5335          }
5336       }
5337    }
5338 }
5339
5340 void ComputeExpression(Expression exp)
5341 {
5342    char expString[10240];
5343    expString[0] = '\0';
5344 #ifdef _DEBUG
5345    PrintExpression(exp, expString);
5346 #endif
5347
5348    switch(exp.type)
5349    {
5350       case instanceExp:
5351       {
5352          ComputeInstantiation(exp);
5353          break;
5354       }
5355       /*
5356       case constantExp:
5357          break;
5358       */
5359       case opExp:
5360       {
5361          Expression exp1, exp2 = null;
5362          Operand op1 { };
5363          Operand op2 { };
5364
5365          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5366          if(exp.op.exp2)
5367          {
5368             Expression e = exp.op.exp2;
5369
5370             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5371             {
5372                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5373                {
5374                   if(e.type == extensionCompoundExp)
5375                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5376                   else
5377                      e = e.list->last;
5378                }
5379             }
5380             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5381             {
5382                if(e.type == stringExp && e.string)
5383                {
5384                   char * string = e.string;
5385                   int len = strlen(string);
5386                   char * tmp = new char[len-2+1];
5387                   len = UnescapeString(tmp, string + 1, len - 2);
5388                   delete tmp;
5389                   FreeExpContents(exp);
5390                   exp.type = constantExp;
5391                   exp.constant = PrintUInt(len + 1);
5392                }
5393                else
5394                {
5395                   Type type = e.expType;
5396                   type.refCount++;
5397                   FreeExpContents(exp);
5398                   exp.type = constantExp;
5399                   exp.constant = PrintUInt(ComputeTypeSize(type));
5400                   FreeType(type);
5401                }
5402                break;
5403             }
5404             else
5405                ComputeExpression(exp.op.exp2);
5406          }
5407          if(exp.op.exp1)
5408          {
5409             ComputeExpression(exp.op.exp1);
5410             exp1 = exp.op.exp1;
5411             exp2 = exp.op.exp2;
5412             op1 = GetOperand(exp1);
5413             if(op1.type) op1.type.refCount++;
5414             if(exp2)
5415             {
5416                op2 = GetOperand(exp2);
5417                if(op2.type) op2.type.refCount++;
5418             }
5419          }
5420          else
5421          {
5422             exp1 = exp.op.exp2;
5423             op1 = GetOperand(exp1);
5424             if(op1.type) op1.type.refCount++;
5425          }
5426
5427          CallOperator(exp, exp1, exp2, op1, op2);
5428          /*
5429          switch(exp.op.op)
5430          {
5431             // Unary operators
5432             case '&':
5433                // Also binary
5434                if(exp.op.exp1 && exp.op.exp2)
5435                {
5436                   // Binary And
5437                   if(op1.ops.BitAnd)
5438                   {
5439                      FreeExpContents(exp);
5440                      op1.ops.BitAnd(exp, op1, op2);
5441                   }
5442                }
5443                break;
5444             case '*':
5445                if(exp.op.exp1)
5446                {
5447                   if(op1.ops.Mul)
5448                   {
5449                      FreeExpContents(exp);
5450                      op1.ops.Mul(exp, op1, op2);
5451                   }
5452                }
5453                break;
5454             case '+':
5455                if(exp.op.exp1)
5456                {
5457                   if(op1.ops.Add)
5458                   {
5459                      FreeExpContents(exp);
5460                      op1.ops.Add(exp, op1, op2);
5461                   }
5462                }
5463                else
5464                {
5465                   // Provide default unary +
5466                   Expression exp2 = exp.op.exp2;
5467                   exp.op.exp2 = null;
5468                   FreeExpContents(exp);
5469                   FreeType(exp.expType);
5470                   FreeType(exp.destType);
5471
5472                   *exp = *exp2;
5473                   delete exp2;
5474                }
5475                break;
5476             case '-':
5477                if(exp.op.exp1)
5478                {
5479                   if(op1.ops.Sub)
5480                   {
5481                      FreeExpContents(exp);
5482                      op1.ops.Sub(exp, op1, op2);
5483                   }
5484                }
5485                else
5486                {
5487                   if(op1.ops.Neg)
5488                   {
5489                      FreeExpContents(exp);
5490                      op1.ops.Neg(exp, op1);
5491                   }
5492                }
5493                break;
5494             case '~':
5495                if(op1.ops.BitNot)
5496                {
5497                   FreeExpContents(exp);
5498                   op1.ops.BitNot(exp, op1);
5499                }
5500                break;
5501             case '!':
5502                if(op1.ops.Not)
5503                {
5504                   FreeExpContents(exp);
5505                   op1.ops.Not(exp, op1);
5506                }
5507                break;
5508             // Binary only operators
5509             case '/':
5510                if(op1.ops.Div)
5511                {
5512                   FreeExpContents(exp);
5513                   op1.ops.Div(exp, op1, op2);
5514                }
5515                break;
5516             case '%':
5517                if(op1.ops.Mod)
5518                {
5519                   FreeExpContents(exp);
5520                   op1.ops.Mod(exp, op1, op2);
5521                }
5522                break;
5523             case LEFT_OP:
5524                break;
5525             case RIGHT_OP:
5526                break;
5527             case '<':
5528                if(exp.op.exp1)
5529                {
5530                   if(op1.ops.Sma)
5531                   {
5532                      FreeExpContents(exp);
5533                      op1.ops.Sma(exp, op1, op2);
5534                   }
5535                }
5536                break;
5537             case '>':
5538                if(exp.op.exp1)
5539                {
5540                   if(op1.ops.Grt)
5541                   {
5542                      FreeExpContents(exp);
5543                      op1.ops.Grt(exp, op1, op2);
5544                   }
5545                }
5546                break;
5547             case LE_OP:
5548                if(exp.op.exp1)
5549                {
5550                   if(op1.ops.SmaEqu)
5551                   {
5552                      FreeExpContents(exp);
5553                      op1.ops.SmaEqu(exp, op1, op2);
5554                   }
5555                }
5556                break;
5557             case GE_OP:
5558                if(exp.op.exp1)
5559                {
5560                   if(op1.ops.GrtEqu)
5561                   {
5562                      FreeExpContents(exp);
5563                      op1.ops.GrtEqu(exp, op1, op2);
5564                   }
5565                }
5566                break;
5567             case EQ_OP:
5568                if(exp.op.exp1)
5569                {
5570                   if(op1.ops.Equ)
5571                   {
5572                      FreeExpContents(exp);
5573                      op1.ops.Equ(exp, op1, op2);
5574                   }
5575                }
5576                break;
5577             case NE_OP:
5578                if(exp.op.exp1)
5579                {
5580                   if(op1.ops.Nqu)
5581                   {
5582                      FreeExpContents(exp);
5583                      op1.ops.Nqu(exp, op1, op2);
5584                   }
5585                }
5586                break;
5587             case '|':
5588                if(op1.ops.BitOr)
5589                {
5590                   FreeExpContents(exp);
5591                   op1.ops.BitOr(exp, op1, op2);
5592                }
5593                break;
5594             case '^':
5595                if(op1.ops.BitXor)
5596                {
5597                   FreeExpContents(exp);
5598                   op1.ops.BitXor(exp, op1, op2);
5599                }
5600                break;
5601             case AND_OP:
5602                break;
5603             case OR_OP:
5604                break;
5605             case SIZEOF:
5606                FreeExpContents(exp);
5607                exp.type = constantExp;
5608                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5609                break;
5610          }
5611          */
5612          if(op1.type) FreeType(op1.type);
5613          if(op2.type) FreeType(op2.type);
5614          break;
5615       }
5616       case bracketsExp:
5617       case extensionExpressionExp:
5618       {
5619          Expression e, n;
5620          for(e = exp.list->first; e; e = n)
5621          {
5622             n = e.next;
5623             if(!n)
5624             {
5625                OldList * list = exp.list;
5626                Expression prev = exp.prev;
5627                Expression next = exp.next;
5628                ComputeExpression(e);
5629                //FreeExpContents(exp);
5630                FreeType(exp.expType);
5631                FreeType(exp.destType);
5632                *exp = *e;
5633                exp.prev = prev;
5634                exp.next = next;
5635                delete e;
5636                delete list;
5637             }
5638             else
5639             {
5640                FreeExpression(e);
5641             }
5642          }
5643          break;
5644       }
5645       /*
5646
5647       case ExpIndex:
5648       {
5649          Expression e;
5650          exp.isConstant = true;
5651
5652          ComputeExpression(exp.index.exp);
5653          if(!exp.index.exp.isConstant)
5654             exp.isConstant = false;
5655
5656          for(e = exp.index.index->first; e; e = e.next)
5657          {
5658             ComputeExpression(e);
5659             if(!e.next)
5660             {
5661                // Check if this type is int
5662             }
5663             if(!e.isConstant)
5664                exp.isConstant = false;
5665          }
5666          exp.expType = Dereference(exp.index.exp.expType);
5667          break;
5668       }
5669       */
5670       case memberExp:
5671       {
5672          Expression memberExp = exp.member.exp;
5673          Identifier memberID = exp.member.member;
5674
5675          Type type;
5676          ComputeExpression(exp.member.exp);
5677          type = exp.member.exp.expType;
5678          if(type)
5679          {
5680             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);
5681             Property prop = null;
5682             DataMember member = null;
5683             Class convertTo = null;
5684             if(type.kind == subClassType && exp.member.exp.type == classExp)
5685                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5686
5687             if(!_class)
5688             {
5689                char string[256];
5690                Symbol classSym;
5691                string[0] = '\0';
5692                PrintTypeNoConst(type, string, false, true);
5693                classSym = FindClass(string);
5694                _class = classSym ? classSym.registered : null;
5695             }
5696
5697             if(exp.member.member)
5698             {
5699                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5700                if(!prop)
5701                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5702             }
5703             if(!prop && !member && _class && exp.member.member)
5704             {
5705                Symbol classSym = FindClass(exp.member.member.string);
5706                convertTo = _class;
5707                _class = classSym ? classSym.registered : null;
5708                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5709             }
5710
5711             if(prop)
5712             {
5713                if(prop.compiled)
5714                {
5715                   Type type = prop.dataType;
5716                   // TODO: Assuming same base type for units...
5717                   if(_class.type == unitClass)
5718                   {
5719                      if(type.kind == classType)
5720                      {
5721                         Class _class = type._class.registered;
5722                         if(_class.type == unitClass)
5723                         {
5724                            if(!_class.dataType)
5725                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5726                            type = _class.dataType;
5727                         }
5728                      }
5729                      switch(type.kind)
5730                      {
5731                         case floatType:
5732                         {
5733                            float value;
5734                            float (*Get)(float) = (void *)prop.Get;
5735                            GetFloat(exp.member.exp, &value);
5736                            exp.constant = PrintFloat(Get ? Get(value) : value);
5737                            exp.type = constantExp;
5738                            break;
5739                         }
5740                         case doubleType:
5741                         {
5742                            double value;
5743                            double (*Get)(double);
5744                            GetDouble(exp.member.exp, &value);
5745
5746                            if(convertTo)
5747                               Get = (void *)prop.Set;
5748                            else
5749                               Get = (void *)prop.Get;
5750                            exp.constant = PrintDouble(Get ? Get(value) : value);
5751                            exp.type = constantExp;
5752                            break;
5753                         }
5754                      }
5755                   }
5756                   else
5757                   {
5758                      if(convertTo)
5759                      {
5760                         Expression value = exp.member.exp;
5761                         Type type;
5762                         if(!prop.dataType)
5763                            ProcessPropertyType(prop);
5764
5765                         type = prop.dataType;
5766                         if(!type)
5767                         {
5768                             // printf("Investigate this\n");
5769                         }
5770                         else if(_class.type == structClass)
5771                         {
5772                            switch(type.kind)
5773                            {
5774                               case classType:
5775                               {
5776                                  Class propertyClass = type._class.registered;
5777                                  if(propertyClass.type == structClass && value.type == instanceExp)
5778                                  {
5779                                     void (*Set)(void *, void *) = (void *)prop.Set;
5780                                     exp.instance = Instantiation { };
5781                                     exp.instance.data = new0 byte[_class.structSize];
5782                                     exp.instance._class = MkSpecifierName(_class.fullName);
5783                                     exp.instance.loc = exp.loc;
5784                                     exp.type = instanceExp;
5785                                     Set(exp.instance.data, value.instance.data);
5786                                     PopulateInstance(exp.instance);
5787                                  }
5788                                  break;
5789                               }
5790                               case intType:
5791                               {
5792                                  int intValue;
5793                                  void (*Set)(void *, int) = (void *)prop.Set;
5794
5795                                  exp.instance = Instantiation { };
5796                                  exp.instance.data = new0 byte[_class.structSize];
5797                                  exp.instance._class = MkSpecifierName(_class.fullName);
5798                                  exp.instance.loc = exp.loc;
5799                                  exp.type = instanceExp;
5800
5801                                  GetInt(value, &intValue);
5802
5803                                  Set(exp.instance.data, intValue);
5804                                  PopulateInstance(exp.instance);
5805                                  break;
5806                               }
5807                               case int64Type:
5808                               {
5809                                  int64 intValue;
5810                                  void (*Set)(void *, int64) = (void *)prop.Set;
5811
5812                                  exp.instance = Instantiation { };
5813                                  exp.instance.data = new0 byte[_class.structSize];
5814                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5815                                  exp.instance.loc = exp.loc;
5816                                  exp.type = instanceExp;
5817
5818                                  GetInt64(value, &intValue);
5819
5820                                  Set(exp.instance.data, intValue);
5821                                  PopulateInstance(exp.instance);
5822                                  break;
5823                               }
5824                               case intPtrType:
5825                               {
5826                                  // TOFIX:
5827                                  intptr intValue;
5828                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5829
5830                                  exp.instance = Instantiation { };
5831                                  exp.instance.data = new0 byte[_class.structSize];
5832                                  exp.instance._class = MkSpecifierName(_class.fullName);
5833                                  exp.instance.loc = exp.loc;
5834                                  exp.type = instanceExp;
5835
5836                                  GetIntPtr(value, &intValue);
5837
5838                                  Set(exp.instance.data, intValue);
5839                                  PopulateInstance(exp.instance);
5840                                  break;
5841                               }
5842                               case intSizeType:
5843                               {
5844                                  // TOFIX:
5845                                  intsize intValue;
5846                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5847
5848                                  exp.instance = Instantiation { };
5849                                  exp.instance.data = new0 byte[_class.structSize];
5850                                  exp.instance._class = MkSpecifierName(_class.fullName);
5851                                  exp.instance.loc = exp.loc;
5852                                  exp.type = instanceExp;
5853
5854                                  GetIntSize(value, &intValue);
5855
5856                                  Set(exp.instance.data, intValue);
5857                                  PopulateInstance(exp.instance);
5858                                  break;
5859                               }
5860                               case floatType:
5861                               {
5862                                  float floatValue;
5863                                  void (*Set)(void *, float) = (void *)prop.Set;
5864
5865                                  exp.instance = Instantiation { };
5866                                  exp.instance.data = new0 byte[_class.structSize];
5867                                  exp.instance._class = MkSpecifierName(_class.fullName);
5868                                  exp.instance.loc = exp.loc;
5869                                  exp.type = instanceExp;
5870
5871                                  GetFloat(value, &floatValue);
5872
5873                                  Set(exp.instance.data, floatValue);
5874                                  PopulateInstance(exp.instance);
5875                                  break;
5876                               }
5877                               case doubleType:
5878                               {
5879                                  double doubleValue;
5880                                  void (*Set)(void *, double) = (void *)prop.Set;
5881
5882                                  exp.instance = Instantiation { };
5883                                  exp.instance.data = new0 byte[_class.structSize];
5884                                  exp.instance._class = MkSpecifierName(_class.fullName);
5885                                  exp.instance.loc = exp.loc;
5886                                  exp.type = instanceExp;
5887
5888                                  GetDouble(value, &doubleValue);
5889
5890                                  Set(exp.instance.data, doubleValue);
5891                                  PopulateInstance(exp.instance);
5892                                  break;
5893                               }
5894                            }
5895                         }
5896                         else if(_class.type == bitClass)
5897                         {
5898                            switch(type.kind)
5899                            {
5900                               case classType:
5901                               {
5902                                  Class propertyClass = type._class.registered;
5903                                  if(propertyClass.type == structClass && value.instance.data)
5904                                  {
5905                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5906                                     unsigned int bits = Set(value.instance.data);
5907                                     exp.constant = PrintHexUInt(bits);
5908                                     exp.type = constantExp;
5909                                     break;
5910                                  }
5911                                  else if(_class.type == bitClass)
5912                                  {
5913                                     unsigned int value;
5914                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5915                                     unsigned int bits;
5916
5917                                     GetUInt(exp.member.exp, &value);
5918                                     bits = Set(value);
5919                                     exp.constant = PrintHexUInt(bits);
5920                                     exp.type = constantExp;
5921                                  }
5922                               }
5923                            }
5924                         }
5925                      }
5926                      else
5927                      {
5928                         if(_class.type == bitClass)
5929                         {
5930                            unsigned int value;
5931                            GetUInt(exp.member.exp, &value);
5932
5933                            switch(type.kind)
5934                            {
5935                               case classType:
5936                               {
5937                                  Class _class = type._class.registered;
5938                                  if(_class.type == structClass)
5939                                  {
5940                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5941
5942                                     exp.instance = Instantiation { };
5943                                     exp.instance.data = new0 byte[_class.structSize];
5944                                     exp.instance._class = MkSpecifierName(_class.fullName);
5945                                     exp.instance.loc = exp.loc;
5946                                     //exp.instance.fullSet = true;
5947                                     exp.type = instanceExp;
5948                                     Get(value, exp.instance.data);
5949                                     PopulateInstance(exp.instance);
5950                                  }
5951                                  else if(_class.type == bitClass)
5952                                  {
5953                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5954                                     uint64 bits = Get(value);
5955                                     exp.constant = PrintHexUInt64(bits);
5956                                     exp.type = constantExp;
5957                                  }
5958                                  break;
5959                               }
5960                            }
5961                         }
5962                         else if(_class.type == structClass)
5963                         {
5964                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5965                            switch(type.kind)
5966                            {
5967                               case classType:
5968                               {
5969                                  Class _class = type._class.registered;
5970                                  if(_class.type == structClass && value)
5971                                  {
5972                                     void (*Get)(void *, void *) = (void *)prop.Get;
5973
5974                                     exp.instance = Instantiation { };
5975                                     exp.instance.data = new0 byte[_class.structSize];
5976                                     exp.instance._class = MkSpecifierName(_class.fullName);
5977                                     exp.instance.loc = exp.loc;
5978                                     //exp.instance.fullSet = true;
5979                                     exp.type = instanceExp;
5980                                     Get(value, exp.instance.data);
5981                                     PopulateInstance(exp.instance);
5982                                  }
5983                                  break;
5984                               }
5985                            }
5986                         }
5987                         /*else
5988                         {
5989                            char * value = exp.member.exp.instance.data;
5990                            switch(type.kind)
5991                            {
5992                               case classType:
5993                               {
5994                                  Class _class = type._class.registered;
5995                                  if(_class.type == normalClass)
5996                                  {
5997                                     void *(*Get)(void *) = (void *)prop.Get;
5998
5999                                     exp.instance = Instantiation { };
6000                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6001                                     exp.type = instanceExp;
6002                                     exp.instance.data = Get(value, exp.instance.data);
6003                                  }
6004                                  break;
6005                               }
6006                            }
6007                         }
6008                         */
6009                      }
6010                   }
6011                }
6012                else
6013                {
6014                   exp.isConstant = false;
6015                }
6016             }
6017             else if(member)
6018             {
6019             }
6020          }
6021
6022          if(exp.type != ExpressionType::memberExp)
6023          {
6024             FreeExpression(memberExp);
6025             FreeIdentifier(memberID);
6026          }
6027          break;
6028       }
6029       case typeSizeExp:
6030       {
6031          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6032          FreeExpContents(exp);
6033          exp.constant = PrintUInt(ComputeTypeSize(type));
6034          exp.type = constantExp;
6035          FreeType(type);
6036          break;
6037       }
6038       case classSizeExp:
6039       {
6040          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6041          if(classSym && classSym.registered)
6042          {
6043             if(classSym.registered.fixed)
6044             {
6045                FreeSpecifier(exp._class);
6046                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6047                exp.type = constantExp;
6048             }
6049             else
6050             {
6051                char className[1024];
6052                strcpy(className, "__ecereClass_");
6053                FullClassNameCat(className, classSym.string, true);
6054                MangleClassName(className);
6055
6056                DeclareClass(classSym, className);
6057
6058                FreeExpContents(exp);
6059                exp.type = pointerExp;
6060                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6061                exp.member.member = MkIdentifier("structSize");
6062             }
6063          }
6064          break;
6065       }
6066       case castExp:
6067       //case constantExp:
6068       {
6069          Type type;
6070          Expression e = exp;
6071          if(exp.type == castExp)
6072          {
6073             if(exp.cast.exp)
6074                ComputeExpression(exp.cast.exp);
6075             e = exp.cast.exp;
6076          }
6077          if(e && exp.expType)
6078          {
6079             /*if(exp.destType)
6080                type = exp.destType;
6081             else*/
6082                type = exp.expType;
6083             if(type.kind == classType)
6084             {
6085                Class _class = type._class.registered;
6086                if(_class && (_class.type == unitClass || _class.type == bitClass))
6087                {
6088                   if(!_class.dataType)
6089                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6090                   type = _class.dataType;
6091                }
6092             }
6093
6094             switch(type.kind)
6095             {
6096                case _BoolType:
6097                case charType:
6098                   if(type.isSigned)
6099                   {
6100                      char value = 0;
6101                      if(GetChar(e, &value))
6102                      {
6103                         FreeExpContents(exp);
6104                         exp.constant = PrintChar(value);
6105                         exp.type = constantExp;
6106                      }
6107                   }
6108                   else
6109                   {
6110                      unsigned char value = 0;
6111                      if(GetUChar(e, &value))
6112                      {
6113                         FreeExpContents(exp);
6114                         exp.constant = PrintUChar(value);
6115                         exp.type = constantExp;
6116                      }
6117                   }
6118                   break;
6119                case shortType:
6120                   if(type.isSigned)
6121                   {
6122                      short value = 0;
6123                      if(GetShort(e, &value))
6124                      {
6125                         FreeExpContents(exp);
6126                         exp.constant = PrintShort(value);
6127                         exp.type = constantExp;
6128                      }
6129                   }
6130                   else
6131                   {
6132                      unsigned short value = 0;
6133                      if(GetUShort(e, &value))
6134                      {
6135                         FreeExpContents(exp);
6136                         exp.constant = PrintUShort(value);
6137                         exp.type = constantExp;
6138                      }
6139                   }
6140                   break;
6141                case intType:
6142                   if(type.isSigned)
6143                   {
6144                      int value = 0;
6145                      if(GetInt(e, &value))
6146                      {
6147                         FreeExpContents(exp);
6148                         exp.constant = PrintInt(value);
6149                         exp.type = constantExp;
6150                      }
6151                   }
6152                   else
6153                   {
6154                      unsigned int value = 0;
6155                      if(GetUInt(e, &value))
6156                      {
6157                         FreeExpContents(exp);
6158                         exp.constant = PrintUInt(value);
6159                         exp.type = constantExp;
6160                      }
6161                   }
6162                   break;
6163                case int64Type:
6164                   if(type.isSigned)
6165                   {
6166                      int64 value = 0;
6167                      if(GetInt64(e, &value))
6168                      {
6169                         FreeExpContents(exp);
6170                         exp.constant = PrintInt64(value);
6171                         exp.type = constantExp;
6172                      }
6173                   }
6174                   else
6175                   {
6176                      uint64 value = 0;
6177                      if(GetUInt64(e, &value))
6178                      {
6179                         FreeExpContents(exp);
6180                         exp.constant = PrintUInt64(value);
6181                         exp.type = constantExp;
6182                      }
6183                   }
6184                   break;
6185                case intPtrType:
6186                   if(type.isSigned)
6187                   {
6188                      intptr value = 0;
6189                      if(GetIntPtr(e, &value))
6190                      {
6191                         FreeExpContents(exp);
6192                         exp.constant = PrintInt64((int64)value);
6193                         exp.type = constantExp;
6194                      }
6195                   }
6196                   else
6197                   {
6198                      uintptr value = 0;
6199                      if(GetUIntPtr(e, &value))
6200                      {
6201                         FreeExpContents(exp);
6202                         exp.constant = PrintUInt64((uint64)value);
6203                         exp.type = constantExp;
6204                      }
6205                   }
6206                   break;
6207                case intSizeType:
6208                   if(type.isSigned)
6209                   {
6210                      intsize value = 0;
6211                      if(GetIntSize(e, &value))
6212                      {
6213                         FreeExpContents(exp);
6214                         exp.constant = PrintInt64((int64)value);
6215                         exp.type = constantExp;
6216                      }
6217                   }
6218                   else
6219                   {
6220                      uintsize value = 0;
6221                      if(GetUIntSize(e, &value))
6222                      {
6223                         FreeExpContents(exp);
6224                         exp.constant = PrintUInt64((uint64)value);
6225                         exp.type = constantExp;
6226                      }
6227                   }
6228                   break;
6229                case floatType:
6230                {
6231                   float value = 0;
6232                   if(GetFloat(e, &value))
6233                   {
6234                      FreeExpContents(exp);
6235                      exp.constant = PrintFloat(value);
6236                      exp.type = constantExp;
6237                   }
6238                   break;
6239                }
6240                case doubleType:
6241                {
6242                   double value = 0;
6243                   if(GetDouble(e, &value))
6244                   {
6245                      FreeExpContents(exp);
6246                      exp.constant = PrintDouble(value);
6247                      exp.type = constantExp;
6248                   }
6249                   break;
6250                }
6251             }
6252          }
6253          break;
6254       }
6255       case conditionExp:
6256       {
6257          Operand op1 { };
6258          Operand op2 { };
6259          Operand op3 { };
6260
6261          if(exp.cond.exp)
6262             // Caring only about last expression for now...
6263             ComputeExpression(exp.cond.exp->last);
6264          if(exp.cond.elseExp)
6265             ComputeExpression(exp.cond.elseExp);
6266          if(exp.cond.cond)
6267             ComputeExpression(exp.cond.cond);
6268
6269          op1 = GetOperand(exp.cond.cond);
6270          if(op1.type) op1.type.refCount++;
6271          op2 = GetOperand(exp.cond.exp->last);
6272          if(op2.type) op2.type.refCount++;
6273          op3 = GetOperand(exp.cond.elseExp);
6274          if(op3.type) op3.type.refCount++;
6275
6276          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6277          if(op1.type) FreeType(op1.type);
6278          if(op2.type) FreeType(op2.type);
6279          if(op3.type) FreeType(op3.type);
6280          break;
6281       }
6282    }
6283 }
6284
6285 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
6286 {
6287    bool result = true;
6288    if(destType)
6289    {
6290       OldList converts { };
6291       Conversion convert;
6292
6293       if(destType.kind == voidType)
6294          return false;
6295
6296       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
6297          result = false;
6298       if(converts.count)
6299       {
6300          // for(convert = converts.last; convert; convert = convert.prev)
6301          for(convert = converts.first; convert; convert = convert.next)
6302          {
6303             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6304             if(!empty)
6305             {
6306                Expression newExp { };
6307                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6308
6309                // TODO: Check this...
6310                *newExp = *exp;
6311                newExp.prev = null;
6312                newExp.next = null;
6313                newExp.destType = null;
6314
6315                if(convert.isGet)
6316                {
6317                   // [exp].ColorRGB
6318                   exp.type = memberExp;
6319                   exp.addedThis = true;
6320                   exp.member.exp = newExp;
6321                   FreeType(exp.member.exp.expType);
6322
6323                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6324                   exp.member.exp.expType.classObjectType = objectType;
6325                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6326                   exp.member.memberType = propertyMember;
6327                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6328                   // TESTING THIS... for (int)degrees
6329                   exp.needCast = true;
6330                   if(exp.expType) exp.expType.refCount++;
6331                   ApplyAnyObjectLogic(exp.member.exp);
6332                }
6333                else
6334                {
6335
6336                   /*if(exp.isConstant)
6337                   {
6338                      // Color { ColorRGB = [exp] };
6339                      exp.type = instanceExp;
6340                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6341                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6342                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6343                   }
6344                   else*/
6345                   {
6346                      // If not constant, don't turn it yet into an instantiation
6347                      // (Go through the deep members system first)
6348                      exp.type = memberExp;
6349                      exp.addedThis = true;
6350                      exp.member.exp = newExp;
6351
6352                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6353                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6354                         newExp.expType._class.registered.type == noHeadClass)
6355                      {
6356                         newExp.byReference = true;
6357                      }
6358
6359                      FreeType(exp.member.exp.expType);
6360                      /*exp.member.exp.expType = convert.convert.dataType;
6361                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6362                      exp.member.exp.expType = null;
6363                      if(convert.convert.dataType)
6364                      {
6365                         exp.member.exp.expType = { };
6366                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6367                         exp.member.exp.expType.refCount = 1;
6368                         exp.member.exp.expType.classObjectType = objectType;
6369                         ApplyAnyObjectLogic(exp.member.exp);
6370                      }
6371
6372                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6373                      exp.member.memberType = reverseConversionMember;
6374                      exp.expType = convert.resultType ? convert.resultType :
6375                         MkClassType(convert.convert._class.fullName);
6376                      exp.needCast = true;
6377                      if(convert.resultType) convert.resultType.refCount++;
6378                   }
6379                }
6380             }
6381             else
6382             {
6383                FreeType(exp.expType);
6384                if(convert.isGet)
6385                {
6386                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6387                   exp.needCast = true;
6388                   if(exp.expType) exp.expType.refCount++;
6389                }
6390                else
6391                {
6392                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6393                   exp.needCast = true;
6394                   if(convert.resultType)
6395                      convert.resultType.refCount++;
6396                }
6397             }
6398          }
6399          if(exp.isConstant && inCompiler)
6400             ComputeExpression(exp);
6401
6402          converts.Free(FreeConvert);
6403       }
6404
6405       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6406       {
6407          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6408       }
6409       if(!result && exp.expType && exp.destType)
6410       {
6411          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6412              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6413             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6414             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6415             result = true;
6416       }
6417    }
6418    // if(result) CheckTemplateTypes(exp);
6419    return result;
6420 }
6421
6422 void CheckTemplateTypes(Expression exp)
6423 {
6424    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6425    {
6426       Expression newExp { };
6427       Statement compound;
6428       Context context;
6429       *newExp = *exp;
6430       if(exp.destType) exp.destType.refCount++;
6431       if(exp.expType)  exp.expType.refCount++;
6432       newExp.prev = null;
6433       newExp.next = null;
6434
6435       switch(exp.expType.kind)
6436       {
6437          case doubleType:
6438             if(exp.destType.classObjectType)
6439             {
6440                // We need to pass the address, just pass it along (Undo what was done above)
6441                if(exp.destType) exp.destType.refCount--;
6442                if(exp.expType)  exp.expType.refCount--;
6443                delete newExp;
6444             }
6445             else
6446             {
6447                // If we're looking for value:
6448                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6449                OldList * specs;
6450                OldList * unionDefs = MkList();
6451                OldList * statements = MkList();
6452                context = PushContext();
6453                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6454                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6455                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6456                exp.type = extensionCompoundExp;
6457                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6458                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6459                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6460                exp.compound.compound.context = context;
6461                PopContext(context);
6462             }
6463             break;
6464          default:
6465             exp.type = castExp;
6466             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6467             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6468             break;
6469       }
6470    }
6471    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6472    {
6473       Expression newExp { };
6474       Statement compound;
6475       Context context;
6476       *newExp = *exp;
6477       if(exp.destType) exp.destType.refCount++;
6478       if(exp.expType)  exp.expType.refCount++;
6479       newExp.prev = null;
6480       newExp.next = null;
6481
6482       switch(exp.expType.kind)
6483       {
6484          case doubleType:
6485             if(exp.destType.classObjectType)
6486             {
6487                // We need to pass the address, just pass it along (Undo what was done above)
6488                if(exp.destType) exp.destType.refCount--;
6489                if(exp.expType)  exp.expType.refCount--;
6490                delete newExp;
6491             }
6492             else
6493             {
6494                // If we're looking for value:
6495                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6496                OldList * specs;
6497                OldList * unionDefs = MkList();
6498                OldList * statements = MkList();
6499                context = PushContext();
6500                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6501                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6502                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6503                exp.type = extensionCompoundExp;
6504                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6505                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6506                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6507                exp.compound.compound.context = context;
6508                PopContext(context);
6509             }
6510             break;
6511          case classType:
6512          {
6513             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6514             {
6515                exp.type = bracketsExp;
6516                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6517                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6518                ProcessExpressionType(exp.list->first);
6519                break;
6520             }
6521             else
6522             {
6523                exp.type = bracketsExp;
6524                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6525                newExp.needCast = true;
6526                ProcessExpressionType(exp.list->first);
6527                break;
6528             }
6529          }
6530          default:
6531          {
6532             if(exp.expType.kind == templateType)
6533             {
6534                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6535                if(type)
6536                {
6537                   FreeType(exp.destType);
6538                   FreeType(exp.expType);
6539                   delete newExp;
6540                   break;
6541                }
6542             }
6543             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6544             {
6545                exp.type = opExp;
6546                exp.op.op = '*';
6547                exp.op.exp1 = null;
6548                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6549                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6550             }
6551             else
6552             {
6553                char typeString[1024];
6554                Declarator decl;
6555                OldList * specs = MkList();
6556                typeString[0] = '\0';
6557                PrintType(exp.expType, typeString, false, false);
6558                decl = SpecDeclFromString(typeString, specs, null);
6559
6560                exp.type = castExp;
6561                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6562                exp.cast.typeName = MkTypeName(specs, decl);
6563                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6564                exp.cast.exp.needCast = true;
6565             }
6566             break;
6567          }
6568       }
6569    }
6570 }
6571 // TODO: The Symbol tree should be reorganized by namespaces
6572 // Name Space:
6573 //    - Tree of all symbols within (stored without namespace)
6574 //    - Tree of sub-namespaces
6575
6576 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6577 {
6578    int nsLen = strlen(nameSpace);
6579    Symbol symbol;
6580    // Start at the name space prefix
6581    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6582    {
6583       char * s = symbol.string;
6584       if(!strncmp(s, nameSpace, nsLen))
6585       {
6586          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6587          int c;
6588          char * namePart;
6589          for(c = strlen(s)-1; c >= 0; c--)
6590             if(s[c] == ':')
6591                break;
6592
6593          namePart = s+c+1;
6594          if(!strcmp(namePart, name))
6595          {
6596             // TODO: Error on ambiguity
6597             return symbol;
6598          }
6599       }
6600       else
6601          break;
6602    }
6603    return null;
6604 }
6605
6606 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6607 {
6608    int c;
6609    char nameSpace[1024];
6610    char * namePart;
6611    bool gotColon = false;
6612
6613    nameSpace[0] = '\0';
6614    for(c = strlen(name)-1; c >= 0; c--)
6615       if(name[c] == ':')
6616       {
6617          gotColon = true;
6618          break;
6619       }
6620
6621    namePart = name+c+1;
6622    while(c >= 0 && name[c] == ':') c--;
6623    if(c >= 0)
6624    {
6625       // Try an exact match first
6626       Symbol symbol = (Symbol)tree.FindString(name);
6627       if(symbol)
6628          return symbol;
6629
6630       // Namespace specified
6631       memcpy(nameSpace, name, c + 1);
6632       nameSpace[c+1] = 0;
6633
6634       return ScanWithNameSpace(tree, nameSpace, namePart);
6635    }
6636    else if(gotColon)
6637    {
6638       // Looking for a global symbol, e.g. ::Sleep()
6639       Symbol symbol = (Symbol)tree.FindString(namePart);
6640       return symbol;
6641    }
6642    else
6643    {
6644       // Name only (no namespace specified)
6645       Symbol symbol = (Symbol)tree.FindString(namePart);
6646       if(symbol)
6647          return symbol;
6648       return ScanWithNameSpace(tree, "", namePart);
6649    }
6650    return null;
6651 }
6652
6653 static void ProcessDeclaration(Declaration decl);
6654
6655 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6656 {
6657 #ifdef _DEBUG
6658    //Time startTime = GetTime();
6659 #endif
6660    // Optimize this later? Do this before/less?
6661    Context ctx;
6662    Symbol symbol = null;
6663    // First, check if the identifier is declared inside the function
6664    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6665
6666    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6667    {
6668       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6669       {
6670          symbol = null;
6671          if(thisNameSpace)
6672          {
6673             char curName[1024];
6674             strcpy(curName, thisNameSpace);
6675             strcat(curName, "::");
6676             strcat(curName, name);
6677             // Try to resolve in current namespace first
6678             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6679          }
6680          if(!symbol)
6681             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6682       }
6683       else
6684          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6685
6686       if(symbol || ctx == endContext) break;
6687    }
6688    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6689    {
6690       if(symbol.pointerExternal.type == functionExternal)
6691       {
6692          FunctionDefinition function = symbol.pointerExternal.function;
6693
6694          // Modified this recently...
6695          Context tmpContext = curContext;
6696          curContext = null;
6697          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6698          curContext = tmpContext;
6699
6700          symbol.pointerExternal.symbol = symbol;
6701
6702          // TESTING THIS:
6703          DeclareType(symbol.type, true, true);
6704
6705          ast->Insert(curExternal.prev, symbol.pointerExternal);
6706
6707          symbol.id = curExternal.symbol.idCode;
6708
6709       }
6710       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6711       {
6712          ast->Move(symbol.pointerExternal, curExternal.prev);
6713          symbol.id = curExternal.symbol.idCode;
6714       }
6715    }
6716 #ifdef _DEBUG
6717    //findSymbolTotalTime += GetTime() - startTime;
6718 #endif
6719    return symbol;
6720 }
6721
6722 static void GetTypeSpecs(Type type, OldList * specs)
6723 {
6724    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6725    switch(type.kind)
6726    {
6727       case classType:
6728       {
6729          if(type._class.registered)
6730          {
6731             if(!type._class.registered.dataType)
6732                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6733             GetTypeSpecs(type._class.registered.dataType, specs);
6734          }
6735          break;
6736       }
6737       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6738       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6739       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6740       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6741       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6742       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6743       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6744       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6745       case intType:
6746       default:
6747          ListAdd(specs, MkSpecifier(INT)); break;
6748    }
6749 }
6750
6751 static void PrintArraySize(Type arrayType, char * string)
6752 {
6753    char size[256];
6754    size[0] = '\0';
6755    strcat(size, "[");
6756    if(arrayType.enumClass)
6757       strcat(size, arrayType.enumClass.string);
6758    else if(arrayType.arraySizeExp)
6759       PrintExpression(arrayType.arraySizeExp, size);
6760    strcat(size, "]");
6761    strcat(string, size);
6762 }
6763
6764 // WARNING : This function expects a null terminated string since it recursively concatenate...
6765 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6766 {
6767    if(type)
6768    {
6769       if(printConst && type.constant)
6770          strcat(string, "const ");
6771       switch(type.kind)
6772       {
6773          case classType:
6774          {
6775             Symbol c = type._class;
6776             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6777             //       look into merging with thisclass ?
6778             if(type.classObjectType == typedObject)
6779                strcat(string, "typed_object");
6780             else if(type.classObjectType == anyObject)
6781                strcat(string, "any_object");
6782             else
6783             {
6784                if(c && c.string)
6785                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6786             }
6787             if(type.byReference)
6788                strcat(string, " &");
6789             break;
6790          }
6791          case voidType: strcat(string, "void"); break;
6792          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6793          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6794          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6795          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6796          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6797          case _BoolType: strcat(string, "_Bool"); break;
6798          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6799          case floatType: strcat(string, "float"); break;
6800          case doubleType: strcat(string, "double"); break;
6801          case structType:
6802             if(type.enumName)
6803             {
6804                strcat(string, "struct ");
6805                strcat(string, type.enumName);
6806             }
6807             else if(type.typeName)
6808                strcat(string, type.typeName);
6809             else
6810             {
6811                Type member;
6812                strcat(string, "struct { ");
6813                for(member = type.members.first; member; member = member.next)
6814                {
6815                   PrintType(member, string, true, fullName);
6816                   strcat(string,"; ");
6817                }
6818                strcat(string,"}");
6819             }
6820             break;
6821          case unionType:
6822             if(type.enumName)
6823             {
6824                strcat(string, "union ");
6825                strcat(string, type.enumName);
6826             }
6827             else if(type.typeName)
6828                strcat(string, type.typeName);
6829             else
6830             {
6831                strcat(string, "union ");
6832                strcat(string,"(unnamed)");
6833             }
6834             break;
6835          case enumType:
6836             if(type.enumName)
6837             {
6838                strcat(string, "enum ");
6839                strcat(string, type.enumName);
6840             }
6841             else if(type.typeName)
6842                strcat(string, type.typeName);
6843             else
6844                strcat(string, "int"); // "enum");
6845             break;
6846          case ellipsisType:
6847             strcat(string, "...");
6848             break;
6849          case subClassType:
6850             strcat(string, "subclass(");
6851             strcat(string, type._class ? type._class.string : "int");
6852             strcat(string, ")");
6853             break;
6854          case templateType:
6855             strcat(string, type.templateParameter.identifier.string);
6856             break;
6857          case thisClassType:
6858             strcat(string, "thisclass");
6859             break;
6860          case vaListType:
6861             strcat(string, "__builtin_va_list");
6862             break;
6863       }
6864    }
6865 }
6866
6867 static void PrintName(Type type, char * string, bool fullName)
6868 {
6869    if(type.name && type.name[0])
6870    {
6871       if(fullName)
6872          strcat(string, type.name);
6873       else
6874       {
6875          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6876          if(name) name += 2; else name = type.name;
6877          strcat(string, name);
6878       }
6879    }
6880 }
6881
6882 static void PrintAttribs(Type type, char * string)
6883 {
6884    if(type)
6885    {
6886       if(type.dllExport)   strcat(string, "dllexport ");
6887       if(type.attrStdcall) strcat(string, "stdcall ");
6888    }
6889 }
6890
6891 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6892 {
6893    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6894    {
6895       Type attrType = null;
6896       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6897          PrintAttribs(type, string);
6898       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6899          strcat(string, " const");
6900       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6901       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6902          strcat(string, " (");
6903       if(type.kind == pointerType)
6904       {
6905          if(type.type.kind == functionType || type.type.kind == methodType)
6906             PrintAttribs(type.type, string);
6907       }
6908       if(type.kind == pointerType)
6909       {
6910          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6911             strcat(string, "*");
6912          else
6913             strcat(string, " *");
6914       }
6915       if(printConst && type.constant && type.kind == pointerType)
6916          strcat(string, " const");
6917    }
6918    else
6919       PrintTypeSpecs(type, string, fullName, printConst);
6920 }
6921
6922 static void PostPrintType(Type type, char * string, bool fullName)
6923 {
6924    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6925       strcat(string, ")");
6926    if(type.kind == arrayType)
6927       PrintArraySize(type, string);
6928    else if(type.kind == functionType)
6929    {
6930       Type param;
6931       strcat(string, "(");
6932       for(param = type.params.first; param; param = param.next)
6933       {
6934          PrintType(param, string, true, fullName);
6935          if(param.next) strcat(string, ", ");
6936       }
6937       strcat(string, ")");
6938    }
6939    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6940       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6941 }
6942
6943 // *****
6944 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6945 // *****
6946 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6947 {
6948    PrePrintType(type, string, fullName, null, printConst);
6949
6950    if(type.thisClass || (printName && type.name && type.name[0]))
6951       strcat(string, " ");
6952    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6953    {
6954       Symbol _class = type.thisClass;
6955       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6956       {
6957          if(type.classObjectType == classPointer)
6958             strcat(string, "class");
6959          else
6960             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6961       }
6962       else if(_class && _class.string)
6963       {
6964          String s = _class.string;
6965          if(fullName)
6966             strcat(string, s);
6967          else
6968          {
6969             char * name = RSearchString(s, "::", strlen(s), true, false);
6970             if(name) name += 2; else name = s;
6971             strcat(string, name);
6972          }
6973       }
6974       strcat(string, "::");
6975    }
6976
6977    if(printName && type.name)
6978       PrintName(type, string, fullName);
6979    PostPrintType(type, string, fullName);
6980    if(type.bitFieldCount)
6981    {
6982       char count[100];
6983       sprintf(count, ":%d", type.bitFieldCount);
6984       strcat(string, count);
6985    }
6986 }
6987
6988 void PrintType(Type type, char * string, bool printName, bool fullName)
6989 {
6990    _PrintType(type, string, printName, fullName, true);
6991 }
6992
6993 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6994 {
6995    _PrintType(type, string, printName, fullName, false);
6996 }
6997
6998 static Type FindMember(Type type, char * string)
6999 {
7000    Type memberType;
7001    for(memberType = type.members.first; memberType; memberType = memberType.next)
7002    {
7003       if(!memberType.name)
7004       {
7005          Type subType = FindMember(memberType, string);
7006          if(subType)
7007             return subType;
7008       }
7009       else if(!strcmp(memberType.name, string))
7010          return memberType;
7011    }
7012    return null;
7013 }
7014
7015 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7016 {
7017    Type memberType;
7018    for(memberType = type.members.first; memberType; memberType = memberType.next)
7019    {
7020       if(!memberType.name)
7021       {
7022          Type subType = FindMember(memberType, string);
7023          if(subType)
7024          {
7025             *offset += memberType.offset;
7026             return subType;
7027          }
7028       }
7029       else if(!strcmp(memberType.name, string))
7030       {
7031          *offset += memberType.offset;
7032          return memberType;
7033       }
7034    }
7035    return null;
7036 }
7037
7038 public bool GetParseError() { return parseError; }
7039
7040 Expression ParseExpressionString(char * expression)
7041 {
7042    parseError = false;
7043
7044    fileInput = TempFile { };
7045    fileInput.Write(expression, 1, strlen(expression));
7046    fileInput.Seek(0, start);
7047
7048    echoOn = false;
7049    parsedExpression = null;
7050    resetScanner();
7051    expression_yyparse();
7052    delete fileInput;
7053
7054    return parsedExpression;
7055 }
7056
7057 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7058 {
7059    Identifier id = exp.identifier;
7060    Method method = null;
7061    Property prop = null;
7062    DataMember member = null;
7063    ClassProperty classProp = null;
7064
7065    if(_class && _class.type == enumClass)
7066    {
7067       NamedLink value = null;
7068       Class enumClass = eSystem_FindClass(privateModule, "enum");
7069       if(enumClass)
7070       {
7071          Class baseClass;
7072          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7073          {
7074             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7075             for(value = e.values.first; value; value = value.next)
7076             {
7077                if(!strcmp(value.name, id.string))
7078                   break;
7079             }
7080             if(value)
7081             {
7082                char constant[256];
7083
7084                FreeExpContents(exp);
7085
7086                exp.type = constantExp;
7087                exp.isConstant = true;
7088                if(!strcmp(baseClass.dataTypeString, "int"))
7089                   sprintf(constant, "%d",(int)value.data);
7090                else
7091                   sprintf(constant, "0x%X",(int)value.data);
7092                exp.constant = CopyString(constant);
7093                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7094                exp.expType = MkClassType(baseClass.fullName);
7095                break;
7096             }
7097          }
7098       }
7099       if(value)
7100          return true;
7101    }
7102    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7103    {
7104       ProcessMethodType(method);
7105       exp.expType = Type
7106       {
7107          refCount = 1;
7108          kind = methodType;
7109          method = method;
7110          // Crash here?
7111          // TOCHECK: Put it back to what it was...
7112          // methodClass = _class;
7113          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7114       };
7115       //id._class = null;
7116       return true;
7117    }
7118    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7119    {
7120       if(!prop.dataType)
7121          ProcessPropertyType(prop);
7122       exp.expType = prop.dataType;
7123       if(prop.dataType) prop.dataType.refCount++;
7124       return true;
7125    }
7126    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7127    {
7128       if(!member.dataType)
7129          member.dataType = ProcessTypeString(member.dataTypeString, false);
7130       exp.expType = member.dataType;
7131       if(member.dataType) member.dataType.refCount++;
7132       return true;
7133    }
7134    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7135    {
7136       if(!classProp.dataType)
7137          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7138
7139       if(classProp.constant)
7140       {
7141          FreeExpContents(exp);
7142
7143          exp.isConstant = true;
7144          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7145          {
7146             //char constant[256];
7147             exp.type = stringExp;
7148             exp.constant = QMkString((char *)classProp.Get(_class));
7149          }
7150          else
7151          {
7152             char constant[256];
7153             exp.type = constantExp;
7154             sprintf(constant, "%d", (int)classProp.Get(_class));
7155             exp.constant = CopyString(constant);
7156          }
7157       }
7158       else
7159       {
7160          // TO IMPLEMENT...
7161       }
7162
7163       exp.expType = classProp.dataType;
7164       if(classProp.dataType) classProp.dataType.refCount++;
7165       return true;
7166    }
7167    return false;
7168 }
7169
7170 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7171 {
7172    BinaryTree * tree = &nameSpace.functions;
7173    GlobalData data = (GlobalData)tree->FindString(name);
7174    NameSpace * child;
7175    if(!data)
7176    {
7177       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7178       {
7179          data = ScanGlobalData(child, name);
7180          if(data)
7181             break;
7182       }
7183    }
7184    return data;
7185 }
7186
7187 static GlobalData FindGlobalData(char * name)
7188 {
7189    int start = 0, c;
7190    NameSpace * nameSpace;
7191    nameSpace = globalData;
7192    for(c = 0; name[c]; c++)
7193    {
7194       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7195       {
7196          NameSpace * newSpace;
7197          char * spaceName = new char[c - start + 1];
7198          strncpy(spaceName, name + start, c - start);
7199          spaceName[c-start] = '\0';
7200          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7201          delete spaceName;
7202          if(!newSpace)
7203             return null;
7204          nameSpace = newSpace;
7205          if(name[c] == ':') c++;
7206          start = c+1;
7207       }
7208    }
7209    if(c - start)
7210    {
7211       return ScanGlobalData(nameSpace, name + start);
7212    }
7213    return null;
7214 }
7215
7216 static int definedExpStackPos;
7217 static void * definedExpStack[512];
7218
7219 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7220 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7221 {
7222    Expression prev = checkedExp.prev, next = checkedExp.next;
7223
7224    FreeExpContents(checkedExp);
7225    FreeType(checkedExp.expType);
7226    FreeType(checkedExp.destType);
7227
7228    *checkedExp = *newExp;
7229
7230    delete newExp;
7231
7232    checkedExp.prev = prev;
7233    checkedExp.next = next;
7234 }
7235
7236 void ApplyAnyObjectLogic(Expression e)
7237 {
7238    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7239 #ifdef _DEBUG
7240    char debugExpString[4096];
7241    debugExpString[0] = '\0';
7242    PrintExpression(e, debugExpString);
7243 #endif
7244
7245    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7246    {
7247       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7248       //ellipsisDestType = destType;
7249       if(e && e.expType)
7250       {
7251          Type type = e.expType;
7252          Class _class = null;
7253          //Type destType = e.destType;
7254
7255          if(type.kind == classType && type._class && type._class.registered)
7256          {
7257             _class = type._class.registered;
7258          }
7259          else if(type.kind == subClassType)
7260          {
7261             _class = FindClass("ecere::com::Class").registered;
7262          }
7263          else
7264          {
7265             char string[1024] = "";
7266             Symbol classSym;
7267
7268             PrintTypeNoConst(type, string, false, true);
7269             classSym = FindClass(string);
7270             if(classSym) _class = classSym.registered;
7271          }
7272
7273          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...
7274             (!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))) ||
7275             destType.byReference)))
7276          {
7277             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7278             {
7279                Expression checkedExp = e, newExp;
7280
7281                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7282                {
7283                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7284                   {
7285                      if(checkedExp.type == extensionCompoundExp)
7286                      {
7287                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7288                      }
7289                      else
7290                         checkedExp = checkedExp.list->last;
7291                   }
7292                   else if(checkedExp.type == castExp)
7293                      checkedExp = checkedExp.cast.exp;
7294                }
7295
7296                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7297                {
7298                   newExp = checkedExp.op.exp2;
7299                   checkedExp.op.exp2 = null;
7300                   FreeExpContents(checkedExp);
7301
7302                   if(e.expType && e.expType.passAsTemplate)
7303                   {
7304                      char size[100];
7305                      ComputeTypeSize(e.expType);
7306                      sprintf(size, "%d", e.expType.size);
7307                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7308                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7309                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7310                   }
7311
7312                   ReplaceExpContents(checkedExp, newExp);
7313                   e.byReference = true;
7314                }
7315                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7316                {
7317                   Expression checkedExp, newExp;
7318
7319                   {
7320                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7321                      bool hasAddress =
7322                         e.type == identifierExp ||
7323                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7324                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7325                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7326                         e.type == indexExp;
7327
7328                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7329                      {
7330                         Context context = PushContext();
7331                         Declarator decl;
7332                         OldList * specs = MkList();
7333                         char typeString[1024];
7334                         Expression newExp { };
7335
7336                         typeString[0] = '\0';
7337                         *newExp = *e;
7338
7339                         //if(e.destType) e.destType.refCount++;
7340                         // if(exp.expType) exp.expType.refCount++;
7341                         newExp.prev = null;
7342                         newExp.next = null;
7343                         newExp.expType = null;
7344
7345                         PrintTypeNoConst(e.expType, typeString, false, true);
7346                         decl = SpecDeclFromString(typeString, specs, null);
7347                         newExp.destType = ProcessType(specs, decl);
7348
7349                         curContext = context;
7350
7351                         // We need a current compound for this
7352                         if(curCompound)
7353                         {
7354                            char name[100];
7355                            OldList * stmts = MkList();
7356                            e.type = extensionCompoundExp;
7357                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7358                            if(!curCompound.compound.declarations)
7359                               curCompound.compound.declarations = MkList();
7360                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7361                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7362                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7363                            e.compound = MkCompoundStmt(null, stmts);
7364                         }
7365                         else
7366                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7367
7368                         /*
7369                         e.compound = MkCompoundStmt(
7370                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7371                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7372
7373                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7374                         */
7375
7376                         {
7377                            Type type = e.destType;
7378                            e.destType = { };
7379                            CopyTypeInto(e.destType, type);
7380                            e.destType.refCount = 1;
7381                            e.destType.classObjectType = none;
7382                            FreeType(type);
7383                         }
7384
7385                         e.compound.compound.context = context;
7386                         PopContext(context);
7387                         curContext = context.parent;
7388                      }
7389                   }
7390
7391                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7392                   checkedExp = e;
7393                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7394                   {
7395                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7396                      {
7397                         if(checkedExp.type == extensionCompoundExp)
7398                         {
7399                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7400                         }
7401                         else
7402                            checkedExp = checkedExp.list->last;
7403                      }
7404                      else if(checkedExp.type == castExp)
7405                         checkedExp = checkedExp.cast.exp;
7406                   }
7407                   {
7408                      Expression operand { };
7409                      operand = *checkedExp;
7410                      checkedExp.destType = null;
7411                      checkedExp.expType = null;
7412                      checkedExp.Clear();
7413                      checkedExp.type = opExp;
7414                      checkedExp.op.op = '&';
7415                      checkedExp.op.exp1 = null;
7416                      checkedExp.op.exp2 = operand;
7417
7418                      //newExp = MkExpOp(null, '&', checkedExp);
7419                   }
7420                   //ReplaceExpContents(checkedExp, newExp);
7421                }
7422             }
7423          }
7424       }
7425    }
7426    {
7427       // If expression type is a simple class, make it an address
7428       // FixReference(e, true);
7429    }
7430 //#if 0
7431    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7432       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7433          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7434    {
7435       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"))
7436       {
7437          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7438       }
7439       else
7440       {
7441          Expression thisExp { };
7442
7443          *thisExp = *e;
7444          thisExp.prev = null;
7445          thisExp.next = null;
7446          e.Clear();
7447
7448          e.type = bracketsExp;
7449          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7450          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7451             ((Expression)e.list->first).byReference = true;
7452
7453          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7454          {
7455             e.expType = thisExp.expType;
7456             e.expType.refCount++;
7457          }
7458          else*/
7459          {
7460             e.expType = { };
7461             CopyTypeInto(e.expType, thisExp.expType);
7462             e.expType.byReference = false;
7463             e.expType.refCount = 1;
7464
7465             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7466                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7467             {
7468                e.expType.classObjectType = none;
7469             }
7470          }
7471       }
7472    }
7473 // TOFIX: Try this for a nice IDE crash!
7474 //#endif
7475    // The other way around
7476    else
7477 //#endif
7478    if(destType && e.expType &&
7479          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7480          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7481          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7482    {
7483       if(destType.kind == ellipsisType)
7484       {
7485          Compiler_Error($"Unspecified type\n");
7486       }
7487       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7488       {
7489          bool byReference = e.expType.byReference;
7490          Expression thisExp { };
7491          Declarator decl;
7492          OldList * specs = MkList();
7493          char typeString[1024]; // Watch buffer overruns
7494          Type type;
7495          ClassObjectType backupClassObjectType;
7496          bool backupByReference;
7497
7498          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7499             type = e.expType;
7500          else
7501             type = destType;
7502
7503          backupClassObjectType = type.classObjectType;
7504          backupByReference = type.byReference;
7505
7506          type.classObjectType = none;
7507          type.byReference = false;
7508
7509          typeString[0] = '\0';
7510          PrintType(type, typeString, false, true);
7511          decl = SpecDeclFromString(typeString, specs, null);
7512
7513          type.classObjectType = backupClassObjectType;
7514          type.byReference = backupByReference;
7515
7516          *thisExp = *e;
7517          thisExp.prev = null;
7518          thisExp.next = null;
7519          e.Clear();
7520
7521          if( ( type.kind == classType && type._class && type._class.registered &&
7522                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7523                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7524              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7525              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7526          {
7527             e.type = opExp;
7528             e.op.op = '*';
7529             e.op.exp1 = null;
7530             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7531
7532             e.expType = { };
7533             CopyTypeInto(e.expType, type);
7534             e.expType.byReference = false;
7535             e.expType.refCount = 1;
7536          }
7537          else
7538          {
7539             e.type = castExp;
7540             e.cast.typeName = MkTypeName(specs, decl);
7541             e.cast.exp = thisExp;
7542             e.byReference = true;
7543             e.expType = type;
7544             type.refCount++;
7545          }
7546          e.destType = destType;
7547          destType.refCount++;
7548       }
7549    }
7550 }
7551
7552 void ProcessExpressionType(Expression exp)
7553 {
7554    bool unresolved = false;
7555    Location oldyylloc = yylloc;
7556    bool notByReference = false;
7557 #ifdef _DEBUG
7558    char debugExpString[4096];
7559    debugExpString[0] = '\0';
7560    PrintExpression(exp, debugExpString);
7561 #endif
7562    if(!exp || exp.expType)
7563       return;
7564
7565    //eSystem_Logf("%s\n", expString);
7566
7567    // Testing this here
7568    yylloc = exp.loc;
7569    switch(exp.type)
7570    {
7571       case identifierExp:
7572       {
7573          Identifier id = exp.identifier;
7574          if(!id || !topContext) return;
7575
7576          // DOING THIS LATER NOW...
7577          if(id._class && id._class.name)
7578          {
7579             id.classSym = id._class.symbol; // FindClass(id._class.name);
7580             /* TODO: Name Space Fix ups
7581             if(!id.classSym)
7582                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7583             */
7584          }
7585
7586          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7587          {
7588             exp.expType = ProcessTypeString("Module", true);
7589             break;
7590          }
7591          else */if(strstr(id.string, "__ecereClass") == id.string)
7592          {
7593             exp.expType = ProcessTypeString("ecere::com::Class", true);
7594             break;
7595          }
7596          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7597          {
7598             // Added this here as well
7599             ReplaceClassMembers(exp, thisClass);
7600             if(exp.type != identifierExp)
7601             {
7602                ProcessExpressionType(exp);
7603                break;
7604             }
7605
7606             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7607                break;
7608          }
7609          else
7610          {
7611             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7612             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7613             if(!symbol/* && exp.destType*/)
7614             {
7615                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7616                   break;
7617                else
7618                {
7619                   if(thisClass)
7620                   {
7621                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7622                      if(exp.type != identifierExp)
7623                      {
7624                         ProcessExpressionType(exp);
7625                         break;
7626                      }
7627                   }
7628                   // Static methods called from inside the _class
7629                   else if(currentClass && !id._class)
7630                   {
7631                      if(ResolveIdWithClass(exp, currentClass, true))
7632                         break;
7633                   }
7634                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7635                }
7636             }
7637
7638             // If we manage to resolve this symbol
7639             if(symbol)
7640             {
7641                Type type = symbol.type;
7642                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7643
7644                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7645                {
7646                   Context context = SetupTemplatesContext(_class);
7647                   type = ReplaceThisClassType(_class);
7648                   FinishTemplatesContext(context);
7649                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7650                }
7651
7652                FreeSpecifier(id._class);
7653                id._class = null;
7654                delete id.string;
7655                id.string = CopyString(symbol.string);
7656
7657                id.classSym = null;
7658                exp.expType = type;
7659                if(type)
7660                   type.refCount++;
7661
7662                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7663                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7664                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7665                   // Add missing cases here... enum Classes...
7666                   exp.isConstant = true;
7667
7668                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7669                if(symbol.isParam || !strcmp(id.string, "this"))
7670                {
7671                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7672                      exp.byReference = true;
7673
7674                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7675                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7676                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7677                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7678                   {
7679                      Identifier id = exp.identifier;
7680                      exp.type = bracketsExp;
7681                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7682                   }*/
7683                }
7684
7685                if(symbol.isIterator)
7686                {
7687                   if(symbol.isIterator == 3)
7688                   {
7689                      exp.type = bracketsExp;
7690                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7691                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7692                      exp.expType = null;
7693                      ProcessExpressionType(exp);
7694                   }
7695                   else if(symbol.isIterator != 4)
7696                   {
7697                      exp.type = memberExp;
7698                      exp.member.exp = MkExpIdentifier(exp.identifier);
7699                      exp.member.exp.expType = exp.expType;
7700                      /*if(symbol.isIterator == 6)
7701                         exp.member.member = MkIdentifier("key");
7702                      else*/
7703                         exp.member.member = MkIdentifier("data");
7704                      exp.expType = null;
7705                      ProcessExpressionType(exp);
7706                   }
7707                }
7708                break;
7709             }
7710             else
7711             {
7712                DefinedExpression definedExp = null;
7713                if(thisNameSpace && !(id._class && !id._class.name))
7714                {
7715                   char name[1024];
7716                   strcpy(name, thisNameSpace);
7717                   strcat(name, "::");
7718                   strcat(name, id.string);
7719                   definedExp = eSystem_FindDefine(privateModule, name);
7720                }
7721                if(!definedExp)
7722                   definedExp = eSystem_FindDefine(privateModule, id.string);
7723                if(definedExp)
7724                {
7725                   int c;
7726                   for(c = 0; c<definedExpStackPos; c++)
7727                      if(definedExpStack[c] == definedExp)
7728                         break;
7729                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7730                   {
7731                      Location backupYylloc = yylloc;
7732                      File backInput = fileInput;
7733                      definedExpStack[definedExpStackPos++] = definedExp;
7734
7735                      fileInput = TempFile { };
7736                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7737                      fileInput.Seek(0, start);
7738
7739                      echoOn = false;
7740                      parsedExpression = null;
7741                      resetScanner();
7742                      expression_yyparse();
7743                      delete fileInput;
7744                      if(backInput)
7745                         fileInput = backInput;
7746
7747                      yylloc = backupYylloc;
7748
7749                      if(parsedExpression)
7750                      {
7751                         FreeIdentifier(id);
7752                         exp.type = bracketsExp;
7753                         exp.list = MkListOne(parsedExpression);
7754                         parsedExpression.loc = yylloc;
7755                         ProcessExpressionType(exp);
7756                         definedExpStackPos--;
7757                         return;
7758                      }
7759                      definedExpStackPos--;
7760                   }
7761                   else
7762                   {
7763                      if(inCompiler)
7764                      {
7765                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7766                      }
7767                   }
7768                }
7769                else
7770                {
7771                   GlobalData data = null;
7772                   if(thisNameSpace && !(id._class && !id._class.name))
7773                   {
7774                      char name[1024];
7775                      strcpy(name, thisNameSpace);
7776                      strcat(name, "::");
7777                      strcat(name, id.string);
7778                      data = FindGlobalData(name);
7779                   }
7780                   if(!data)
7781                      data = FindGlobalData(id.string);
7782                   if(data)
7783                   {
7784                      DeclareGlobalData(data);
7785                      exp.expType = data.dataType;
7786                      if(data.dataType) data.dataType.refCount++;
7787
7788                      delete id.string;
7789                      id.string = CopyString(data.fullName);
7790                      FreeSpecifier(id._class);
7791                      id._class = null;
7792
7793                      break;
7794                   }
7795                   else
7796                   {
7797                      GlobalFunction function = null;
7798                      if(thisNameSpace && !(id._class && !id._class.name))
7799                      {
7800                         char name[1024];
7801                         strcpy(name, thisNameSpace);
7802                         strcat(name, "::");
7803                         strcat(name, id.string);
7804                         function = eSystem_FindFunction(privateModule, name);
7805                      }
7806                      if(!function)
7807                         function = eSystem_FindFunction(privateModule, id.string);
7808                      if(function)
7809                      {
7810                         char name[1024];
7811                         delete id.string;
7812                         id.string = CopyString(function.name);
7813                         name[0] = 0;
7814
7815                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7816                            strcpy(name, "__ecereFunction_");
7817                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7818                         if(DeclareFunction(function, name))
7819                         {
7820                            delete id.string;
7821                            id.string = CopyString(name);
7822                         }
7823                         exp.expType = function.dataType;
7824                         if(function.dataType) function.dataType.refCount++;
7825
7826                         FreeSpecifier(id._class);
7827                         id._class = null;
7828
7829                         break;
7830                      }
7831                   }
7832                }
7833             }
7834          }
7835          unresolved = true;
7836          break;
7837       }
7838       case instanceExp:
7839       {
7840          Class _class;
7841          // Symbol classSym;
7842
7843          if(!exp.instance._class)
7844          {
7845             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7846             {
7847                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7848             }
7849          }
7850
7851          //classSym = FindClass(exp.instance._class.fullName);
7852          //_class = classSym ? classSym.registered : null;
7853
7854          ProcessInstantiationType(exp.instance);
7855          exp.isConstant = exp.instance.isConstant;
7856
7857          /*
7858          if(_class.type == unitClass && _class.base.type != systemClass)
7859          {
7860             {
7861                Type destType = exp.destType;
7862
7863                exp.destType = MkClassType(_class.base.fullName);
7864                exp.expType = MkClassType(_class.fullName);
7865                CheckExpressionType(exp, exp.destType, true);
7866
7867                exp.destType = destType;
7868             }
7869             exp.expType = MkClassType(_class.fullName);
7870          }
7871          else*/
7872          if(exp.instance._class)
7873          {
7874             exp.expType = MkClassType(exp.instance._class.name);
7875             /*if(exp.expType._class && exp.expType._class.registered &&
7876                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7877                exp.expType.byReference = true;*/
7878          }
7879          break;
7880       }
7881       case constantExp:
7882       {
7883          if(!exp.expType)
7884          {
7885             char * constant = exp.constant;
7886             Type type
7887             {
7888                refCount = 1;
7889                constant = true;
7890             };
7891             exp.expType = type;
7892
7893             if(constant[0] == '\'')
7894             {
7895                if((int)((byte *)constant)[1] > 127)
7896                {
7897                   int nb;
7898                   unichar ch = UTF8GetChar(constant + 1, &nb);
7899                   if(nb < 2) ch = constant[1];
7900                   delete constant;
7901                   exp.constant = PrintUInt(ch);
7902                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7903                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7904                   type._class = FindClass("unichar");
7905
7906                   type.isSigned = false;
7907                }
7908                else
7909                {
7910                   type.kind = charType;
7911                   type.isSigned = true;
7912                }
7913             }
7914             else
7915             {
7916                char * dot = strchr(constant, '.');
7917                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7918                char * exponent;
7919                if(isHex)
7920                {
7921                   exponent = strchr(constant, 'p');
7922                   if(!exponent) exponent = strchr(constant, 'P');
7923                }
7924                else
7925                {
7926                   exponent = strchr(constant, 'e');
7927                   if(!exponent) exponent = strchr(constant, 'E');
7928                }
7929
7930                if(dot || exponent)
7931                {
7932                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7933                      type.kind = floatType;
7934                   else
7935                      type.kind = doubleType;
7936                   type.isSigned = true;
7937                }
7938                else
7939                {
7940                   bool isSigned = constant[0] == '-';
7941                   char * endP = null;
7942                   int64 i64 = strtoll(constant, &endP, 0);
7943                   uint64 ui64 = strtoull(constant, &endP, 0);
7944                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
7945                   if(isSigned)
7946                   {
7947                      if(i64 < MININT)
7948                         is64Bit = true;
7949                   }
7950                   else
7951                   {
7952                      if(ui64 > MAXINT)
7953                      {
7954                         if(ui64 > MAXDWORD)
7955                         {
7956                            is64Bit = true;
7957                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7958                               isSigned = true;
7959                         }
7960                      }
7961                      else if(constant[0] != '0' || !constant[1])
7962                         isSigned = true;
7963                   }
7964                   type.kind = is64Bit ? int64Type : intType;
7965                   type.isSigned = isSigned;
7966                }
7967             }
7968             exp.isConstant = true;
7969             if(exp.destType && exp.destType.kind == doubleType)
7970                type.kind = doubleType;
7971             else if(exp.destType && exp.destType.kind == floatType)
7972                type.kind = floatType;
7973             else if(exp.destType && exp.destType.kind == int64Type)
7974                type.kind = int64Type;
7975          }
7976          break;
7977       }
7978       case stringExp:
7979       {
7980          exp.isConstant = true;      // Why wasn't this constant?
7981          exp.expType = Type
7982          {
7983             refCount = 1;
7984             kind = pointerType;
7985             type = Type
7986             {
7987                refCount = 1;
7988                kind = charType;
7989                constant = true;
7990                isSigned = true;
7991             }
7992          };
7993          break;
7994       }
7995       case newExp:
7996       case new0Exp:
7997          ProcessExpressionType(exp._new.size);
7998          exp.expType = Type
7999          {
8000             refCount = 1;
8001             kind = pointerType;
8002             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8003          };
8004          DeclareType(exp.expType.type, false, false);
8005          break;
8006       case renewExp:
8007       case renew0Exp:
8008          ProcessExpressionType(exp._renew.size);
8009          ProcessExpressionType(exp._renew.exp);
8010          exp.expType = Type
8011          {
8012             refCount = 1;
8013             kind = pointerType;
8014             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8015          };
8016          DeclareType(exp.expType.type, false, false);
8017          break;
8018       case opExp:
8019       {
8020          bool assign = false, boolResult = false, boolOps = false;
8021          Type type1 = null, type2 = null;
8022          bool useDestType = false, useSideType = false;
8023          Location oldyylloc = yylloc;
8024          bool useSideUnit = false;
8025          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8026
8027          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8028          Type dummy
8029          {
8030             count = 1;
8031             refCount = 1;
8032          };
8033
8034          switch(exp.op.op)
8035          {
8036             // Assignment Operators
8037             case '=':
8038             case MUL_ASSIGN:
8039             case DIV_ASSIGN:
8040             case MOD_ASSIGN:
8041             case ADD_ASSIGN:
8042             case SUB_ASSIGN:
8043             case LEFT_ASSIGN:
8044             case RIGHT_ASSIGN:
8045             case AND_ASSIGN:
8046             case XOR_ASSIGN:
8047             case OR_ASSIGN:
8048                assign = true;
8049                break;
8050             // boolean Operators
8051             case '!':
8052                // Expect boolean operators
8053                //boolOps = true;
8054                //boolResult = true;
8055                break;
8056             case AND_OP:
8057             case OR_OP:
8058                // Expect boolean operands
8059                boolOps = true;
8060                boolResult = true;
8061                break;
8062             // Comparisons
8063             case EQ_OP:
8064             case '<':
8065             case '>':
8066             case LE_OP:
8067             case GE_OP:
8068             case NE_OP:
8069                // Gives boolean result
8070                boolResult = true;
8071                useSideType = true;
8072                break;
8073             case '+':
8074             case '-':
8075                useSideUnit = true;
8076                useSideType = true;
8077                useDestType = true;
8078                break;
8079
8080             case LEFT_OP:
8081             case RIGHT_OP:
8082                useSideType = true;
8083                useDestType = true;
8084                break;
8085
8086             case '|':
8087             case '^':
8088                useSideType = true;
8089                useDestType = true;
8090                break;
8091
8092             case '/':
8093             case '%':
8094                useSideType = true;
8095                useDestType = true;
8096                break;
8097             case '&':
8098             case '*':
8099                if(exp.op.exp1)
8100                {
8101                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8102                   useSideType = true;
8103                   useDestType = true;
8104                }
8105                break;
8106
8107             /*// Implement speed etc.
8108             case '*':
8109             case '/':
8110                break;
8111             */
8112          }
8113          if(exp.op.op == '&')
8114          {
8115             // Added this here earlier for Iterator address as key
8116             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8117             {
8118                Identifier id = exp.op.exp2.identifier;
8119                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8120                if(symbol && symbol.isIterator == 2)
8121                {
8122                   exp.type = memberExp;
8123                   exp.member.exp = exp.op.exp2;
8124                   exp.member.member = MkIdentifier("key");
8125                   exp.expType = null;
8126                   exp.op.exp2.expType = symbol.type;
8127                   symbol.type.refCount++;
8128                   ProcessExpressionType(exp);
8129                   FreeType(dummy);
8130                   break;
8131                }
8132                // exp.op.exp2.usage.usageRef = true;
8133             }
8134          }
8135
8136          //dummy.kind = TypeDummy;
8137          if(exp.op.exp1)
8138          {
8139             // Added this check here to use the dest type only for units derived from the base unit
8140             // So that untyped units will use the side unit as opposed to the untyped destination unit
8141             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8142             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8143                useDestType = false;
8144
8145             if(destClass && useDestType &&
8146               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8147
8148               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8149             {
8150                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8151                exp.op.exp1.destType = exp.destType;
8152                exp.op.exp1.opDestType = true;
8153                if(exp.destType)
8154                   exp.destType.refCount++;
8155             }
8156             else if(!assign)
8157             {
8158                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8159                exp.op.exp1.destType = dummy;
8160                dummy.refCount++;
8161             }
8162
8163             // TESTING THIS HERE...
8164             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8165             ProcessExpressionType(exp.op.exp1);
8166             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8167
8168             exp.op.exp1.opDestType = false;
8169
8170             // Fix for unit and ++ / --
8171             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8172                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8173             {
8174                exp.op.exp2 = MkExpConstant("1");
8175                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8176                assign = true;
8177             }
8178
8179             if(exp.op.exp1.destType == dummy)
8180             {
8181                FreeType(dummy);
8182                exp.op.exp1.destType = null;
8183             }
8184             type1 = exp.op.exp1.expType;
8185          }
8186
8187          if(exp.op.exp2)
8188          {
8189             char expString[10240];
8190             expString[0] = '\0';
8191             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8192             {
8193                if(exp.op.exp1)
8194                {
8195                   exp.op.exp2.destType = exp.op.exp1.expType;
8196                   if(exp.op.exp1.expType)
8197                      exp.op.exp1.expType.refCount++;
8198                }
8199                else
8200                {
8201                   exp.op.exp2.destType = exp.destType;
8202                   if(!exp.op.exp1 || exp.op.op != '&')
8203                      exp.op.exp2.opDestType = true;
8204                   if(exp.destType)
8205                      exp.destType.refCount++;
8206                }
8207
8208                if(type1) type1.refCount++;
8209                exp.expType = type1;
8210             }
8211             else if(assign)
8212             {
8213                if(inCompiler)
8214                   PrintExpression(exp.op.exp2, expString);
8215
8216                if(type1 && type1.kind == pointerType)
8217                {
8218                   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 ||
8219                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8220                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8221                   else if(exp.op.op == '=')
8222                   {
8223                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8224                      exp.op.exp2.destType = type1;
8225                      if(type1)
8226                         type1.refCount++;
8227                   }
8228                }
8229                else
8230                {
8231                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8232                   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/* ||
8233                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8234                   else
8235                   {
8236                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8237                      exp.op.exp2.destType = type1;
8238                      if(type1)
8239                         type1.refCount++;
8240                   }
8241                }
8242                if(type1) type1.refCount++;
8243                exp.expType = type1;
8244             }
8245             else if(destClass &&
8246                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8247                   (destClass.type == enumClass && useDestType)))
8248             {
8249                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8250                exp.op.exp2.destType = exp.destType;
8251                if(exp.op.op != '&')
8252                   exp.op.exp2.opDestType = true;
8253                if(exp.destType)
8254                   exp.destType.refCount++;
8255             }
8256             else
8257             {
8258                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8259                exp.op.exp2.destType = dummy;
8260                dummy.refCount++;
8261             }
8262
8263             // TESTING THIS HERE... (DANGEROUS)
8264             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8265                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8266             {
8267                FreeType(exp.op.exp2.destType);
8268                exp.op.exp2.destType = type1;
8269                type1.refCount++;
8270             }
8271             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8272             // Cannot lose the cast on a sizeof
8273             if(exp.op.op == SIZEOF)
8274             {
8275                Expression e = exp.op.exp2;
8276                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8277                {
8278                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8279                   {
8280                      if(e.type == extensionCompoundExp)
8281                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8282                      else
8283                         e = e.list->last;
8284                   }
8285                }
8286                if(e.type == castExp && e.cast.exp)
8287                   e.cast.exp.needCast = true;
8288             }
8289             ProcessExpressionType(exp.op.exp2);
8290             exp.op.exp2.opDestType = false;
8291             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8292
8293             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8294             {
8295                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)
8296                {
8297                   if(exp.op.op != '=' && type1.type.kind == voidType)
8298                      Compiler_Error($"void *: unknown size\n");
8299                }
8300                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||
8301                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8302                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8303                               exp.op.exp2.expType._class.registered.type == structClass ||
8304                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8305                {
8306                   if(exp.op.op == ADD_ASSIGN)
8307                      Compiler_Error($"cannot add two pointers\n");
8308                }
8309                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8310                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8311                {
8312                   if(exp.op.op == ADD_ASSIGN)
8313                      Compiler_Error($"cannot add two pointers\n");
8314                }
8315                else if(inCompiler)
8316                {
8317                   char type1String[1024];
8318                   char type2String[1024];
8319                   type1String[0] = '\0';
8320                   type2String[0] = '\0';
8321
8322                   PrintType(exp.op.exp2.expType, type1String, false, true);
8323                   PrintType(type1, type2String, false, true);
8324                   ChangeCh(expString, '\n', ' ');
8325                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8326                }
8327             }
8328
8329             if(exp.op.exp2.destType == dummy)
8330             {
8331                FreeType(dummy);
8332                exp.op.exp2.destType = null;
8333             }
8334
8335             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8336             {
8337                type2 = { };
8338                type2.refCount = 1;
8339                CopyTypeInto(type2, exp.op.exp2.expType);
8340                type2.isSigned = true;
8341             }
8342             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8343             {
8344                type2 = { kind = intType };
8345                type2.refCount = 1;
8346                type2.isSigned = true;
8347             }
8348             else
8349             {
8350                type2 = exp.op.exp2.expType;
8351                if(type2) type2.refCount++;
8352             }
8353          }
8354
8355          dummy.kind = voidType;
8356
8357          if(exp.op.op == SIZEOF)
8358          {
8359             exp.expType = Type
8360             {
8361                refCount = 1;
8362                kind = intType;
8363             };
8364             exp.isConstant = true;
8365          }
8366          // Get type of dereferenced pointer
8367          else if(exp.op.op == '*' && !exp.op.exp1)
8368          {
8369             exp.expType = Dereference(type2);
8370             if(type2 && type2.kind == classType)
8371                notByReference = true;
8372          }
8373          else if(exp.op.op == '&' && !exp.op.exp1)
8374             exp.expType = Reference(type2);
8375          else if(!assign)
8376          {
8377             if(boolOps)
8378             {
8379                if(exp.op.exp1)
8380                {
8381                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8382                   exp.op.exp1.destType = MkClassType("bool");
8383                   exp.op.exp1.destType.truth = true;
8384                   if(!exp.op.exp1.expType)
8385                      ProcessExpressionType(exp.op.exp1);
8386                   else
8387                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8388                   FreeType(exp.op.exp1.expType);
8389                   exp.op.exp1.expType = MkClassType("bool");
8390                   exp.op.exp1.expType.truth = true;
8391                }
8392                if(exp.op.exp2)
8393                {
8394                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8395                   exp.op.exp2.destType = MkClassType("bool");
8396                   exp.op.exp2.destType.truth = true;
8397                   if(!exp.op.exp2.expType)
8398                      ProcessExpressionType(exp.op.exp2);
8399                   else
8400                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8401                   FreeType(exp.op.exp2.expType);
8402                   exp.op.exp2.expType = MkClassType("bool");
8403                   exp.op.exp2.expType.truth = true;
8404                }
8405             }
8406             else if(exp.op.exp1 && exp.op.exp2 &&
8407                ((useSideType /*&&
8408                      (useSideUnit ||
8409                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8410                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8411                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8412                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8413             {
8414                if(type1 && type2 &&
8415                   // If either both are class or both are not class
8416                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8417                {
8418                   // Added this check for enum subtraction to result in an int type:
8419                   if(exp.op.op == '-' &&
8420                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8421                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8422                   {
8423                      Type intType;
8424                      if(!type1._class.registered.dataType)
8425                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8426                      if(!type2._class.registered.dataType)
8427                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8428
8429                      intType = ProcessTypeString(
8430                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8431
8432                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8433                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8434                      exp.op.exp1.destType = intType;
8435                      exp.op.exp2.destType = intType;
8436                      intType.refCount++;
8437                   }
8438                   else
8439                   {
8440                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8441                      exp.op.exp2.destType = type1;
8442                      type1.refCount++;
8443                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8444                      exp.op.exp1.destType = type2;
8445                      type2.refCount++;
8446                   }
8447
8448                   // Warning here for adding Radians + Degrees with no destination type
8449                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8450                      type1._class.registered && type1._class.registered.type == unitClass &&
8451                      type2._class.registered && type2._class.registered.type == unitClass &&
8452                      type1._class.registered != type2._class.registered)
8453                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8454                         type1._class.string, type2._class.string, type1._class.string);
8455
8456                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8457                   {
8458                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8459                      if(argExp)
8460                      {
8461                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8462
8463                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8464                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8465                            exp.op.exp1)));
8466
8467                         ProcessExpressionType(exp.op.exp1);
8468
8469                         if(type2.kind != pointerType)
8470                         {
8471                            ProcessExpressionType(classExp);
8472
8473                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8474                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8475                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8476                                  // noHeadClass
8477                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8478                                     OR_OP,
8479                                  // normalClass
8480                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8481                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8482                                        MkPointer(null, null), null)))),
8483                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8484
8485                            if(!exp.op.exp2.expType)
8486                            {
8487                               if(type2)
8488                                  FreeType(type2);
8489                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8490                               type2.refCount++;
8491                            }
8492
8493                            ProcessExpressionType(exp.op.exp2);
8494                         }
8495                      }
8496                   }
8497
8498                   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)))
8499                   {
8500                      if(type1.kind != classType && type1.type.kind == voidType)
8501                         Compiler_Error($"void *: unknown size\n");
8502                      exp.expType = type1;
8503                      if(type1) type1.refCount++;
8504                   }
8505                   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)))
8506                   {
8507                      if(type2.kind != classType && type2.type.kind == voidType)
8508                         Compiler_Error($"void *: unknown size\n");
8509                      exp.expType = type2;
8510                      if(type2) type2.refCount++;
8511                   }
8512                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8513                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8514                   {
8515                      Compiler_Warning($"different levels of indirection\n");
8516                   }
8517                   else
8518                   {
8519                      bool success = false;
8520                      if(type1.kind == pointerType && type2.kind == pointerType)
8521                      {
8522                         if(exp.op.op == '+')
8523                            Compiler_Error($"cannot add two pointers\n");
8524                         else if(exp.op.op == '-')
8525                         {
8526                            // Pointer Subtraction gives integer
8527                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8528                            {
8529                               exp.expType = Type
8530                               {
8531                                  kind = intType;
8532                                  refCount = 1;
8533                               };
8534                               success = true;
8535
8536                               if(type1.type.kind == templateType)
8537                               {
8538                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8539                                  if(argExp)
8540                                  {
8541                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8542
8543                                     ProcessExpressionType(classExp);
8544
8545                                     exp.type = bracketsExp;
8546                                     exp.list = MkListOne(MkExpOp(
8547                                        MkExpBrackets(MkListOne(MkExpOp(
8548                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8549                                              , exp.op.op,
8550                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8551
8552                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8553
8554                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8555                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8556                                                 // noHeadClass
8557                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8558                                                    OR_OP,
8559                                                 // normalClass
8560                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8561                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8562                                                       MkPointer(null, null), null)))),
8563                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8564
8565
8566                                              ));
8567
8568                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8569                                     FreeType(dummy);
8570                                     return;
8571                                  }
8572                               }
8573                            }
8574                         }
8575                      }
8576
8577                      if(!success && exp.op.exp1.type == constantExp)
8578                      {
8579                         // If first expression is constant, try to match that first
8580                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8581                         {
8582                            if(exp.expType) FreeType(exp.expType);
8583                            exp.expType = exp.op.exp1.destType;
8584                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8585                            success = true;
8586                         }
8587                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8588                         {
8589                            if(exp.expType) FreeType(exp.expType);
8590                            exp.expType = exp.op.exp2.destType;
8591                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8592                            success = true;
8593                         }
8594                      }
8595                      else if(!success)
8596                      {
8597                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8598                         {
8599                            if(exp.expType) FreeType(exp.expType);
8600                            exp.expType = exp.op.exp2.destType;
8601                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8602                            success = true;
8603                         }
8604                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8605                         {
8606                            if(exp.expType) FreeType(exp.expType);
8607                            exp.expType = exp.op.exp1.destType;
8608                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8609                            success = true;
8610                         }
8611                      }
8612                      if(!success)
8613                      {
8614                         char expString1[10240];
8615                         char expString2[10240];
8616                         char type1[1024];
8617                         char type2[1024];
8618                         expString1[0] = '\0';
8619                         expString2[0] = '\0';
8620                         type1[0] = '\0';
8621                         type2[0] = '\0';
8622                         if(inCompiler)
8623                         {
8624                            PrintExpression(exp.op.exp1, expString1);
8625                            ChangeCh(expString1, '\n', ' ');
8626                            PrintExpression(exp.op.exp2, expString2);
8627                            ChangeCh(expString2, '\n', ' ');
8628                            PrintType(exp.op.exp1.expType, type1, false, true);
8629                            PrintType(exp.op.exp2.expType, type2, false, true);
8630                         }
8631
8632                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8633                      }
8634                   }
8635                }
8636                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8637                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8638                {
8639                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8640                   // Convert e.g. / 4 into / 4.0
8641                   exp.op.exp1.destType = type2._class.registered.dataType;
8642                   if(type2._class.registered.dataType)
8643                      type2._class.registered.dataType.refCount++;
8644                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8645                   exp.expType = type2;
8646                   if(type2) type2.refCount++;
8647                }
8648                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8649                {
8650                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8651                   // Convert e.g. / 4 into / 4.0
8652                   exp.op.exp2.destType = type1._class.registered.dataType;
8653                   if(type1._class.registered.dataType)
8654                      type1._class.registered.dataType.refCount++;
8655                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8656                   exp.expType = type1;
8657                   if(type1) type1.refCount++;
8658                }
8659                else if(type1)
8660                {
8661                   bool valid = false;
8662
8663                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8664                   {
8665                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8666
8667                      if(!type1._class.registered.dataType)
8668                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8669                      exp.op.exp2.destType = type1._class.registered.dataType;
8670                      exp.op.exp2.destType.refCount++;
8671
8672                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8673                      if(type2)
8674                         FreeType(type2);
8675                      type2 = exp.op.exp2.destType;
8676                      if(type2) type2.refCount++;
8677
8678                      exp.expType = type2;
8679                      type2.refCount++;
8680                   }
8681
8682                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8683                   {
8684                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8685
8686                      if(!type2._class.registered.dataType)
8687                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8688                      exp.op.exp1.destType = type2._class.registered.dataType;
8689                      exp.op.exp1.destType.refCount++;
8690
8691                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8692                      type1 = exp.op.exp1.destType;
8693                      exp.expType = type1;
8694                      type1.refCount++;
8695                   }
8696
8697                   // TESTING THIS NEW CODE
8698                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8699                   {
8700                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8701                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8702                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8703                      {
8704                         // Convert the enum to an int instead for these operators
8705                         if(op1IsEnum && exp.op.exp2.expType)
8706                         {
8707                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8708                            {
8709                               if(exp.expType) FreeType(exp.expType);
8710                               exp.expType = exp.op.exp2.expType;
8711                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8712                               valid = true;
8713                            }
8714                         }
8715                         else if(op2IsEnum && exp.op.exp1.expType)
8716                         {
8717                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8718                            {
8719                               if(exp.expType) FreeType(exp.expType);
8720                               exp.expType = exp.op.exp1.expType;
8721                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8722                               valid = true;
8723                            }
8724                         }
8725                      }
8726                      else
8727                      {
8728                         if(op1IsEnum && exp.op.exp2.expType)
8729                         {
8730                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8731                            {
8732                               if(exp.expType) FreeType(exp.expType);
8733                               exp.expType = exp.op.exp1.expType;
8734                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8735                               valid = true;
8736                            }
8737                         }
8738                         else if(op2IsEnum && exp.op.exp1.expType)
8739                         {
8740                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8741                            {
8742                               if(exp.expType) FreeType(exp.expType);
8743                               exp.expType = exp.op.exp2.expType;
8744                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8745                               valid = true;
8746                            }
8747                         }
8748                      }
8749                   }
8750
8751                   if(!valid)
8752                   {
8753                      // 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
8754                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8755                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8756                      {
8757                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8758                         exp.op.exp1.destType = type2;
8759                         type2.refCount++;
8760
8761                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8762                         {
8763                            if(exp.expType) FreeType(exp.expType);
8764                            exp.expType = exp.op.exp1.destType;
8765                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8766                         }
8767                      }
8768                      else
8769                      {
8770                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8771                         exp.op.exp2.destType = type1;
8772                         type1.refCount++;
8773
8774                      /*
8775                      // Maybe this was meant to be an enum...
8776                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8777                      {
8778                         Type oldType = exp.op.exp2.expType;
8779                         exp.op.exp2.expType = null;
8780                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8781                            FreeType(oldType);
8782                         else
8783                            exp.op.exp2.expType = oldType;
8784                      }
8785                      */
8786
8787                      /*
8788                      // TESTING THIS HERE... LATEST ADDITION
8789                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8790                      {
8791                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8792                         exp.op.exp2.destType = type2._class.registered.dataType;
8793                         if(type2._class.registered.dataType)
8794                            type2._class.registered.dataType.refCount++;
8795                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8796
8797                         //exp.expType = type2._class.registered.dataType; //type2;
8798                         //if(type2) type2.refCount++;
8799                      }
8800
8801                      // TESTING THIS HERE... LATEST ADDITION
8802                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8803                      {
8804                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8805                         exp.op.exp1.destType = type1._class.registered.dataType;
8806                         if(type1._class.registered.dataType)
8807                            type1._class.registered.dataType.refCount++;
8808                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8809                         exp.expType = type1._class.registered.dataType; //type1;
8810                         if(type1) type1.refCount++;
8811                      }
8812                      */
8813
8814                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8815                         {
8816                            if(exp.expType) FreeType(exp.expType);
8817                            exp.expType = exp.op.exp2.destType;
8818                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8819                         }
8820                         else if(type1 && type2)
8821                         {
8822                            char expString1[10240];
8823                            char expString2[10240];
8824                            char type1String[1024];
8825                            char type2String[1024];
8826                            expString1[0] = '\0';
8827                            expString2[0] = '\0';
8828                            type1String[0] = '\0';
8829                            type2String[0] = '\0';
8830                            if(inCompiler)
8831                            {
8832                               PrintExpression(exp.op.exp1, expString1);
8833                               ChangeCh(expString1, '\n', ' ');
8834                               PrintExpression(exp.op.exp2, expString2);
8835                               ChangeCh(expString2, '\n', ' ');
8836                               PrintType(exp.op.exp1.expType, type1String, false, true);
8837                               PrintType(exp.op.exp2.expType, type2String, false, true);
8838                            }
8839
8840                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8841
8842                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8843                            {
8844                               exp.expType = exp.op.exp1.expType;
8845                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8846                            }
8847                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8848                            {
8849                               exp.expType = exp.op.exp2.expType;
8850                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8851                            }
8852                         }
8853                      }
8854                   }
8855                }
8856                else if(type2)
8857                {
8858                   // Maybe this was meant to be an enum...
8859                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8860                   {
8861                      Type oldType = exp.op.exp1.expType;
8862                      exp.op.exp1.expType = null;
8863                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8864                         FreeType(oldType);
8865                      else
8866                         exp.op.exp1.expType = oldType;
8867                   }
8868
8869                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8870                   exp.op.exp1.destType = type2;
8871                   type2.refCount++;
8872                   /*
8873                   // TESTING THIS HERE... LATEST ADDITION
8874                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8875                   {
8876                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8877                      exp.op.exp1.destType = type1._class.registered.dataType;
8878                      if(type1._class.registered.dataType)
8879                         type1._class.registered.dataType.refCount++;
8880                   }
8881
8882                   // TESTING THIS HERE... LATEST ADDITION
8883                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8884                   {
8885                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8886                      exp.op.exp2.destType = type2._class.registered.dataType;
8887                      if(type2._class.registered.dataType)
8888                         type2._class.registered.dataType.refCount++;
8889                   }
8890                   */
8891
8892                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8893                   {
8894                      if(exp.expType) FreeType(exp.expType);
8895                      exp.expType = exp.op.exp1.destType;
8896                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8897                   }
8898                }
8899             }
8900             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8901             {
8902                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8903                {
8904                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8905                   // Convert e.g. / 4 into / 4.0
8906                   exp.op.exp1.destType = type2._class.registered.dataType;
8907                   if(type2._class.registered.dataType)
8908                      type2._class.registered.dataType.refCount++;
8909                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8910                }
8911                if(exp.op.op == '!')
8912                {
8913                   exp.expType = MkClassType("bool");
8914                   exp.expType.truth = true;
8915                }
8916                else
8917                {
8918                   exp.expType = type2;
8919                   if(type2) type2.refCount++;
8920                }
8921             }
8922             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8923             {
8924                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8925                {
8926                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8927                   // Convert e.g. / 4 into / 4.0
8928                   exp.op.exp2.destType = type1._class.registered.dataType;
8929                   if(type1._class.registered.dataType)
8930                      type1._class.registered.dataType.refCount++;
8931                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8932                }
8933                exp.expType = type1;
8934                if(type1) type1.refCount++;
8935             }
8936          }
8937
8938          yylloc = exp.loc;
8939          if(exp.op.exp1 && !exp.op.exp1.expType)
8940          {
8941             char expString[10000];
8942             expString[0] = '\0';
8943             if(inCompiler)
8944             {
8945                PrintExpression(exp.op.exp1, expString);
8946                ChangeCh(expString, '\n', ' ');
8947             }
8948             if(expString[0])
8949                Compiler_Error($"couldn't determine type of %s\n", expString);
8950          }
8951          if(exp.op.exp2 && !exp.op.exp2.expType)
8952          {
8953             char expString[10240];
8954             expString[0] = '\0';
8955             if(inCompiler)
8956             {
8957                PrintExpression(exp.op.exp2, expString);
8958                ChangeCh(expString, '\n', ' ');
8959             }
8960             if(expString[0])
8961                Compiler_Error($"couldn't determine type of %s\n", expString);
8962          }
8963
8964          if(boolResult)
8965          {
8966             FreeType(exp.expType);
8967             exp.expType = MkClassType("bool");
8968             exp.expType.truth = true;
8969          }
8970
8971          if(exp.op.op != SIZEOF)
8972             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8973                (!exp.op.exp2 || exp.op.exp2.isConstant);
8974
8975          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8976          {
8977             DeclareType(exp.op.exp2.expType, false, false);
8978          }
8979
8980          yylloc = oldyylloc;
8981
8982          FreeType(dummy);
8983          if(type2)
8984             FreeType(type2);
8985          break;
8986       }
8987       case bracketsExp:
8988       case extensionExpressionExp:
8989       {
8990          Expression e;
8991          exp.isConstant = true;
8992          for(e = exp.list->first; e; e = e.next)
8993          {
8994             bool inced = false;
8995             if(!e.next)
8996             {
8997                FreeType(e.destType);
8998                e.opDestType = exp.opDestType;
8999                e.destType = exp.destType;
9000                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9001             }
9002             ProcessExpressionType(e);
9003             if(inced)
9004                exp.destType.count--;
9005             if(!exp.expType && !e.next)
9006             {
9007                exp.expType = e.expType;
9008                if(e.expType) e.expType.refCount++;
9009             }
9010             if(!e.isConstant)
9011                exp.isConstant = false;
9012          }
9013
9014          // In case a cast became a member...
9015          e = exp.list->first;
9016          if(!e.next && e.type == memberExp)
9017          {
9018             // Preserve prev, next
9019             Expression next = exp.next, prev = exp.prev;
9020
9021
9022             FreeType(exp.expType);
9023             FreeType(exp.destType);
9024             delete exp.list;
9025
9026             *exp = *e;
9027
9028             exp.prev = prev;
9029             exp.next = next;
9030
9031             delete e;
9032
9033             ProcessExpressionType(exp);
9034          }
9035          break;
9036       }
9037       case indexExp:
9038       {
9039          Expression e;
9040          exp.isConstant = true;
9041
9042          ProcessExpressionType(exp.index.exp);
9043          if(!exp.index.exp.isConstant)
9044             exp.isConstant = false;
9045
9046          if(exp.index.exp.expType)
9047          {
9048             Type source = exp.index.exp.expType;
9049             if(source.kind == classType && source._class && source._class.registered)
9050             {
9051                Class _class = source._class.registered;
9052                Class c = _class.templateClass ? _class.templateClass : _class;
9053                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9054                {
9055                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9056
9057                   if(exp.index.index && exp.index.index->last)
9058                   {
9059                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9060                   }
9061                }
9062             }
9063          }
9064
9065          for(e = exp.index.index->first; e; e = e.next)
9066          {
9067             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9068             {
9069                if(e.destType) FreeType(e.destType);
9070                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9071             }
9072             ProcessExpressionType(e);
9073             if(!e.next)
9074             {
9075                // Check if this type is int
9076             }
9077             if(!e.isConstant)
9078                exp.isConstant = false;
9079          }
9080
9081          if(!exp.expType)
9082             exp.expType = Dereference(exp.index.exp.expType);
9083          if(exp.expType)
9084             DeclareType(exp.expType, false, false);
9085          break;
9086       }
9087       case callExp:
9088       {
9089          Expression e;
9090          Type functionType;
9091          Type methodType = null;
9092          char name[1024];
9093          name[0] = '\0';
9094
9095          if(inCompiler)
9096          {
9097             PrintExpression(exp.call.exp,  name);
9098             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9099             {
9100                //exp.call.exp.expType = null;
9101                PrintExpression(exp.call.exp,  name);
9102             }
9103          }
9104          if(exp.call.exp.type == identifierExp)
9105          {
9106             Expression idExp = exp.call.exp;
9107             Identifier id = idExp.identifier;
9108             if(!strcmp(id.string, "__builtin_frame_address"))
9109             {
9110                exp.expType = ProcessTypeString("void *", true);
9111                if(exp.call.arguments && exp.call.arguments->first)
9112                   ProcessExpressionType(exp.call.arguments->first);
9113                break;
9114             }
9115             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9116             {
9117                exp.expType = ProcessTypeString("int", true);
9118                if(exp.call.arguments && exp.call.arguments->first)
9119                   ProcessExpressionType(exp.call.arguments->first);
9120                break;
9121             }
9122             else if(!strcmp(id.string, "Max") ||
9123                !strcmp(id.string, "Min") ||
9124                !strcmp(id.string, "Sgn") ||
9125                !strcmp(id.string, "Abs"))
9126             {
9127                Expression a = null;
9128                Expression b = null;
9129                Expression tempExp1 = null, tempExp2 = null;
9130                if((!strcmp(id.string, "Max") ||
9131                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9132                {
9133                   a = exp.call.arguments->first;
9134                   b = exp.call.arguments->last;
9135                   tempExp1 = a;
9136                   tempExp2 = b;
9137                }
9138                else if(exp.call.arguments->count == 1)
9139                {
9140                   a = exp.call.arguments->first;
9141                   tempExp1 = a;
9142                }
9143
9144                if(a)
9145                {
9146                   exp.call.arguments->Clear();
9147                   idExp.identifier = null;
9148
9149                   FreeExpContents(exp);
9150
9151                   ProcessExpressionType(a);
9152                   if(b)
9153                      ProcessExpressionType(b);
9154
9155                   exp.type = bracketsExp;
9156                   exp.list = MkList();
9157
9158                   if(a.expType && (!b || b.expType))
9159                   {
9160                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9161                      {
9162                         // Use the simpleStruct name/ids for now...
9163                         if(inCompiler)
9164                         {
9165                            OldList * specs = MkList();
9166                            OldList * decls = MkList();
9167                            Declaration decl;
9168                            char temp1[1024], temp2[1024];
9169
9170                            GetTypeSpecs(a.expType, specs);
9171
9172                            if(a && !a.isConstant && a.type != identifierExp)
9173                            {
9174                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9175                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9176                               tempExp1 = QMkExpId(temp1);
9177                               tempExp1.expType = a.expType;
9178                               if(a.expType)
9179                                  a.expType.refCount++;
9180                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9181                            }
9182                            if(b && !b.isConstant && b.type != identifierExp)
9183                            {
9184                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9185                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9186                               tempExp2 = QMkExpId(temp2);
9187                               tempExp2.expType = b.expType;
9188                               if(b.expType)
9189                                  b.expType.refCount++;
9190                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9191                            }
9192
9193                            decl = MkDeclaration(specs, decls);
9194                            if(!curCompound.compound.declarations)
9195                               curCompound.compound.declarations = MkList();
9196                            curCompound.compound.declarations->Insert(null, decl);
9197                         }
9198                      }
9199                   }
9200
9201                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9202                   {
9203                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9204                      ListAdd(exp.list,
9205                         MkExpCondition(MkExpBrackets(MkListOne(
9206                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9207                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9208                      exp.expType = a.expType;
9209                      if(a.expType)
9210                         a.expType.refCount++;
9211                   }
9212                   else if(!strcmp(id.string, "Abs"))
9213                   {
9214                      ListAdd(exp.list,
9215                         MkExpCondition(MkExpBrackets(MkListOne(
9216                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9217                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9218                      exp.expType = a.expType;
9219                      if(a.expType)
9220                         a.expType.refCount++;
9221                   }
9222                   else if(!strcmp(id.string, "Sgn"))
9223                   {
9224                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9225                      ListAdd(exp.list,
9226                         MkExpCondition(MkExpBrackets(MkListOne(
9227                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9228                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9229                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9230                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9231                      exp.expType = ProcessTypeString("int", false);
9232                   }
9233
9234                   FreeExpression(tempExp1);
9235                   if(tempExp2) FreeExpression(tempExp2);
9236
9237                   FreeIdentifier(id);
9238                   break;
9239                }
9240             }
9241          }
9242
9243          {
9244             Type dummy
9245             {
9246                count = 1;
9247                refCount = 1;
9248             };
9249             if(!exp.call.exp.destType)
9250             {
9251                exp.call.exp.destType = dummy;
9252                dummy.refCount++;
9253             }
9254             ProcessExpressionType(exp.call.exp);
9255             if(exp.call.exp.destType == dummy)
9256             {
9257                FreeType(dummy);
9258                exp.call.exp.destType = null;
9259             }
9260             FreeType(dummy);
9261          }
9262
9263          // Check argument types against parameter types
9264          functionType = exp.call.exp.expType;
9265
9266          if(functionType && functionType.kind == TypeKind::methodType)
9267          {
9268             methodType = functionType;
9269             functionType = methodType.method.dataType;
9270
9271             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9272             // TOCHECK: Instead of doing this here could this be done per param?
9273             if(exp.call.exp.expType.usedClass)
9274             {
9275                char typeString[1024];
9276                typeString[0] = '\0';
9277                {
9278                   Symbol back = functionType.thisClass;
9279                   // Do not output class specifier here (thisclass was added to this)
9280                   functionType.thisClass = null;
9281                   PrintType(functionType, typeString, true, true);
9282                   functionType.thisClass = back;
9283                }
9284                if(strstr(typeString, "thisclass"))
9285                {
9286                   OldList * specs = MkList();
9287                   Declarator decl;
9288                   {
9289                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9290
9291                      decl = SpecDeclFromString(typeString, specs, null);
9292
9293                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9294                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9295                         exp.call.exp.expType.usedClass))
9296                         thisClassParams = false;
9297
9298                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9299                      {
9300                         Class backupThisClass = thisClass;
9301                         thisClass = exp.call.exp.expType.usedClass;
9302                         ProcessDeclarator(decl);
9303                         thisClass = backupThisClass;
9304                      }
9305
9306                      thisClassParams = true;
9307
9308                      functionType = ProcessType(specs, decl);
9309                      functionType.refCount = 0;
9310                      FinishTemplatesContext(context);
9311                   }
9312
9313                   FreeList(specs, FreeSpecifier);
9314                   FreeDeclarator(decl);
9315                 }
9316             }
9317          }
9318          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9319          {
9320             Type type = functionType.type;
9321             if(!functionType.refCount)
9322             {
9323                functionType.type = null;
9324                FreeType(functionType);
9325             }
9326             //methodType = functionType;
9327             functionType = type;
9328          }
9329          if(functionType && functionType.kind != TypeKind::functionType)
9330          {
9331             Compiler_Error($"called object %s is not a function\n", name);
9332          }
9333          else if(functionType)
9334          {
9335             bool emptyParams = false, noParams = false;
9336             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9337             Type type = functionType.params.first;
9338             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9339             int extra = 0;
9340             Location oldyylloc = yylloc;
9341
9342             if(!type) emptyParams = true;
9343
9344             // WORKING ON THIS:
9345             if(functionType.extraParam && e && functionType.thisClass)
9346             {
9347                e.destType = MkClassType(functionType.thisClass.string);
9348                e = e.next;
9349             }
9350
9351             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9352             // Fixed #141 by adding '&& !functionType.extraParam'
9353             if(!functionType.staticMethod && !functionType.extraParam)
9354             {
9355                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9356                   memberExp.member.exp.expType._class)
9357                {
9358                   type = MkClassType(memberExp.member.exp.expType._class.string);
9359                   if(e)
9360                   {
9361                      e.destType = type;
9362                      e = e.next;
9363                      type = functionType.params.first;
9364                   }
9365                   else
9366                      type.refCount = 0;
9367                }
9368                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9369                {
9370                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9371                   type.byReference = functionType.byReference;
9372                   type.typedByReference = functionType.typedByReference;
9373                   if(e)
9374                   {
9375                      // Allow manually passing a class for typed object
9376                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9377                         e = e.next;
9378                      e.destType = type;
9379                      e = e.next;
9380                      type = functionType.params.first;
9381                   }
9382                   else
9383                      type.refCount = 0;
9384                   //extra = 1;
9385                }
9386             }
9387
9388             if(type && type.kind == voidType)
9389             {
9390                noParams = true;
9391                if(!type.refCount) FreeType(type);
9392                type = null;
9393             }
9394
9395             for( ; e; e = e.next)
9396             {
9397                if(!type && !emptyParams)
9398                {
9399                   yylloc = e.loc;
9400                   if(methodType && methodType.methodClass)
9401                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9402                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9403                         noParams ? 0 : functionType.params.count);
9404                   else
9405                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9406                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9407                         noParams ? 0 : functionType.params.count);
9408                   break;
9409                }
9410
9411                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9412                {
9413                   Type templatedType = null;
9414                   Class _class = methodType.usedClass;
9415                   ClassTemplateParameter curParam = null;
9416                   int id = 0;
9417                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9418                   {
9419                      Class sClass;
9420                      for(sClass = _class; sClass; sClass = sClass.base)
9421                      {
9422                         if(sClass.templateClass) sClass = sClass.templateClass;
9423                         id = 0;
9424                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9425                         {
9426                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9427                            {
9428                               Class nextClass;
9429                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9430                               {
9431                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9432                                  id += nextClass.templateParams.count;
9433                               }
9434                               break;
9435                            }
9436                            id++;
9437                         }
9438                         if(curParam) break;
9439                      }
9440                   }
9441                   if(curParam && _class.templateArgs[id].dataTypeString)
9442                   {
9443                      ClassTemplateArgument arg = _class.templateArgs[id];
9444                      {
9445                         Context context = SetupTemplatesContext(_class);
9446
9447                         /*if(!arg.dataType)
9448                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9449                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9450                         FinishTemplatesContext(context);
9451                      }
9452                      e.destType = templatedType;
9453                      if(templatedType)
9454                      {
9455                         templatedType.passAsTemplate = true;
9456                         // templatedType.refCount++;
9457                      }
9458                   }
9459                   else
9460                   {
9461                      e.destType = type;
9462                      if(type) type.refCount++;
9463                   }
9464                }
9465                else
9466                {
9467                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9468                   {
9469                      e.destType = type.prev;
9470                      e.destType.refCount++;
9471                   }
9472                   else
9473                   {
9474                      e.destType = type;
9475                      if(type) type.refCount++;
9476                   }
9477                }
9478                // Don't reach the end for the ellipsis
9479                if(type && type.kind != ellipsisType)
9480                {
9481                   Type next = type.next;
9482                   if(!type.refCount) FreeType(type);
9483                   type = next;
9484                }
9485             }
9486
9487             if(type && type.kind != ellipsisType)
9488             {
9489                if(methodType && methodType.methodClass)
9490                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9491                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9492                      functionType.params.count + extra);
9493                else
9494                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9495                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9496                      functionType.params.count + extra);
9497             }
9498             yylloc = oldyylloc;
9499             if(type && !type.refCount) FreeType(type);
9500          }
9501          else
9502          {
9503             functionType = Type
9504             {
9505                refCount = 0;
9506                kind = TypeKind::functionType;
9507             };
9508
9509             if(exp.call.exp.type == identifierExp)
9510             {
9511                char * string = exp.call.exp.identifier.string;
9512                if(inCompiler)
9513                {
9514                   Symbol symbol;
9515                   Location oldyylloc = yylloc;
9516
9517                   yylloc = exp.call.exp.identifier.loc;
9518                   if(strstr(string, "__builtin_") == string)
9519                   {
9520                      if(exp.destType)
9521                      {
9522                         functionType.returnType = exp.destType;
9523                         exp.destType.refCount++;
9524                      }
9525                   }
9526                   else
9527                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9528                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9529                   globalContext.symbols.Add((BTNode)symbol);
9530                   if(strstr(symbol.string, "::"))
9531                      globalContext.hasNameSpace = true;
9532
9533                   yylloc = oldyylloc;
9534                }
9535             }
9536             else if(exp.call.exp.type == memberExp)
9537             {
9538                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9539                   exp.call.exp.member.member.string);*/
9540             }
9541             else
9542                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9543
9544             if(!functionType.returnType)
9545             {
9546                functionType.returnType = Type
9547                {
9548                   refCount = 1;
9549                   kind = intType;
9550                };
9551             }
9552          }
9553          if(functionType && functionType.kind == TypeKind::functionType)
9554          {
9555             exp.expType = functionType.returnType;
9556
9557             if(functionType.returnType)
9558                functionType.returnType.refCount++;
9559
9560             if(!functionType.refCount)
9561                FreeType(functionType);
9562          }
9563
9564          if(exp.call.arguments)
9565          {
9566             for(e = exp.call.arguments->first; e; e = e.next)
9567             {
9568                Type destType = e.destType;
9569                ProcessExpressionType(e);
9570             }
9571          }
9572          break;
9573       }
9574       case memberExp:
9575       {
9576          Type type;
9577          Location oldyylloc = yylloc;
9578          bool thisPtr;
9579          Expression checkExp = exp.member.exp;
9580          while(checkExp)
9581          {
9582             if(checkExp.type == castExp)
9583                checkExp = checkExp.cast.exp;
9584             else if(checkExp.type == bracketsExp)
9585                checkExp = checkExp.list ? checkExp.list->first : null;
9586             else
9587                break;
9588          }
9589
9590          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9591          exp.thisPtr = thisPtr;
9592
9593          // DOING THIS LATER NOW...
9594          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9595          {
9596             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9597             /* TODO: Name Space Fix ups
9598             if(!exp.member.member.classSym)
9599                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9600             */
9601          }
9602
9603          ProcessExpressionType(exp.member.exp);
9604          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9605             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9606          {
9607             exp.isConstant = false;
9608          }
9609          else
9610             exp.isConstant = exp.member.exp.isConstant;
9611          type = exp.member.exp.expType;
9612
9613          yylloc = exp.loc;
9614
9615          if(type && (type.kind == templateType))
9616          {
9617             Class _class = thisClass ? thisClass : currentClass;
9618             ClassTemplateParameter param = null;
9619             if(_class)
9620             {
9621                for(param = _class.templateParams.first; param; param = param.next)
9622                {
9623                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9624                      break;
9625                }
9626             }
9627             if(param && param.defaultArg.member)
9628             {
9629                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9630                if(argExp)
9631                {
9632                   Expression expMember = exp.member.exp;
9633                   Declarator decl;
9634                   OldList * specs = MkList();
9635                   char thisClassTypeString[1024];
9636
9637                   FreeIdentifier(exp.member.member);
9638
9639                   ProcessExpressionType(argExp);
9640
9641                   {
9642                      char * colon = strstr(param.defaultArg.memberString, "::");
9643                      if(colon)
9644                      {
9645                         char className[1024];
9646                         Class sClass;
9647
9648                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9649                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9650                      }
9651                      else
9652                         strcpy(thisClassTypeString, _class.fullName);
9653                   }
9654
9655                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9656
9657                   exp.expType = ProcessType(specs, decl);
9658                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9659                   {
9660                      Class expClass = exp.expType._class.registered;
9661                      Class cClass = null;
9662                      int c;
9663                      int paramCount = 0;
9664                      int lastParam = -1;
9665
9666                      char templateString[1024];
9667                      ClassTemplateParameter param;
9668                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9669                      for(cClass = expClass; cClass; cClass = cClass.base)
9670                      {
9671                         int p = 0;
9672                         for(param = cClass.templateParams.first; param; param = param.next)
9673                         {
9674                            int id = p;
9675                            Class sClass;
9676                            ClassTemplateArgument arg;
9677                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9678                            arg = expClass.templateArgs[id];
9679
9680                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9681                            {
9682                               ClassTemplateParameter cParam;
9683                               //int p = numParams - sClass.templateParams.count;
9684                               int p = 0;
9685                               Class nextClass;
9686                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9687
9688                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9689                               {
9690                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9691                                  {
9692                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9693                                     {
9694                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9695                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9696                                        break;
9697                                     }
9698                                  }
9699                               }
9700                            }
9701
9702                            {
9703                               char argument[256];
9704                               argument[0] = '\0';
9705                               /*if(arg.name)
9706                               {
9707                                  strcat(argument, arg.name.string);
9708                                  strcat(argument, " = ");
9709                               }*/
9710                               switch(param.type)
9711                               {
9712                                  case expression:
9713                                  {
9714                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9715                                     char expString[1024];
9716                                     OldList * specs = MkList();
9717                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9718                                     Expression exp;
9719                                     char * string = PrintHexUInt64(arg.expression.ui64);
9720                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9721                                     delete string;
9722
9723                                     ProcessExpressionType(exp);
9724                                     ComputeExpression(exp);
9725                                     expString[0] = '\0';
9726                                     PrintExpression(exp, expString);
9727                                     strcat(argument, expString);
9728                                     // delete exp;
9729                                     FreeExpression(exp);
9730                                     break;
9731                                  }
9732                                  case identifier:
9733                                  {
9734                                     strcat(argument, arg.member.name);
9735                                     break;
9736                                  }
9737                                  case TemplateParameterType::type:
9738                                  {
9739                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9740                                     {
9741                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9742                                           strcat(argument, thisClassTypeString);
9743                                        else
9744                                           strcat(argument, arg.dataTypeString);
9745                                     }
9746                                     break;
9747                                  }
9748                               }
9749                               if(argument[0])
9750                               {
9751                                  if(paramCount) strcat(templateString, ", ");
9752                                  if(lastParam != p - 1)
9753                                  {
9754                                     strcat(templateString, param.name);
9755                                     strcat(templateString, " = ");
9756                                  }
9757                                  strcat(templateString, argument);
9758                                  paramCount++;
9759                                  lastParam = p;
9760                               }
9761                               p++;
9762                            }
9763                         }
9764                      }
9765                      {
9766                         int len = strlen(templateString);
9767                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9768                         templateString[len++] = '>';
9769                         templateString[len++] = '\0';
9770                      }
9771                      {
9772                         Context context = SetupTemplatesContext(_class);
9773                         FreeType(exp.expType);
9774                         exp.expType = ProcessTypeString(templateString, false);
9775                         FinishTemplatesContext(context);
9776                      }
9777                   }
9778
9779                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9780                   exp.type = bracketsExp;
9781                   exp.list = MkListOne(MkExpOp(null, '*',
9782                   /*opExp;
9783                   exp.op.op = '*';
9784                   exp.op.exp1 = null;
9785                   exp.op.exp2 = */
9786                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9787                      MkExpBrackets(MkListOne(
9788                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9789                            '+',
9790                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9791                            '+',
9792                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9793
9794                            ));
9795                }
9796             }
9797             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9798                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9799             {
9800                type = ProcessTemplateParameterType(type.templateParameter);
9801             }
9802          }
9803          // TODO: *** This seems to be where we should add method support for all basic types ***
9804          if(type && (type.kind == templateType));
9805          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9806                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9807                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9808                           (type.kind == pointerType && type.type.kind == charType)))
9809          {
9810             Identifier id = exp.member.member;
9811             TypeKind typeKind = type.kind;
9812             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9813             if(typeKind == subClassType && exp.member.exp.type == classExp)
9814             {
9815                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9816                typeKind = classType;
9817             }
9818
9819             if(id)
9820             {
9821                if(typeKind == intType || typeKind == enumType)
9822                   _class = eSystem_FindClass(privateModule, "int");
9823                else if(!_class)
9824                {
9825                   if(type.kind == classType && type._class && type._class.registered)
9826                   {
9827                      _class = type._class.registered;
9828                   }
9829                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9830                   {
9831                      _class = FindClass("char *").registered;
9832                   }
9833                   else if(type.kind == pointerType)
9834                   {
9835                      _class = eSystem_FindClass(privateModule, "uintptr");
9836                      FreeType(exp.expType);
9837                      exp.expType = ProcessTypeString("uintptr", false);
9838                      exp.byReference = true;
9839                   }
9840                   else
9841                   {
9842                      char string[1024] = "";
9843                      Symbol classSym;
9844                      PrintTypeNoConst(type, string, false, true);
9845                      classSym = FindClass(string);
9846                      if(classSym) _class = classSym.registered;
9847                   }
9848                }
9849             }
9850
9851             if(_class && id)
9852             {
9853                /*bool thisPtr =
9854                   (exp.member.exp.type == identifierExp &&
9855                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9856                Property prop = null;
9857                Method method = null;
9858                DataMember member = null;
9859                Property revConvert = null;
9860                ClassProperty classProp = null;
9861
9862                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9863                   exp.member.memberType = propertyMember;
9864
9865                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9866                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9867
9868                if(typeKind != subClassType)
9869                {
9870                   // Prioritize data members over properties for "this"
9871                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9872                   {
9873                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9874                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9875                      {
9876                         prop = eClass_FindProperty(_class, id.string, privateModule);
9877                         if(prop)
9878                            member = null;
9879                      }
9880                      if(!member && !prop)
9881                         prop = eClass_FindProperty(_class, id.string, privateModule);
9882                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9883                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9884                         exp.member.thisPtr = true;
9885                   }
9886                   // Prioritize properties over data members otherwise
9887                   else
9888                   {
9889                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9890                      if(!id.classSym)
9891                      {
9892                         prop = eClass_FindProperty(_class, id.string, null);
9893                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9894                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9895                      }
9896
9897                      if(!prop && !member)
9898                      {
9899                         method = eClass_FindMethod(_class, id.string, null);
9900                         if(!method)
9901                         {
9902                            prop = eClass_FindProperty(_class, id.string, privateModule);
9903                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9904                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9905                         }
9906                      }
9907
9908                      if(member && prop)
9909                      {
9910                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9911                            prop = null;
9912                         else
9913                            member = null;
9914                      }
9915                   }
9916                }
9917                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9918                   method = eClass_FindMethod(_class, id.string, privateModule);
9919                if(!prop && !member && !method)
9920                {
9921                   if(typeKind == subClassType)
9922                   {
9923                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9924                      if(classProp)
9925                      {
9926                         exp.member.memberType = classPropertyMember;
9927                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9928                      }
9929                      else
9930                      {
9931                         // Assume this is a class_data member
9932                         char structName[1024];
9933                         Identifier id = exp.member.member;
9934                         Expression classExp = exp.member.exp;
9935                         type.refCount++;
9936
9937                         FreeType(classExp.expType);
9938                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9939
9940                         strcpy(structName, "__ecereClassData_");
9941                         FullClassNameCat(structName, type._class.string, false);
9942                         exp.type = pointerExp;
9943                         exp.member.member = id;
9944
9945                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9946                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9947                               MkExpBrackets(MkListOne(MkExpOp(
9948                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9949                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9950                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9951                                  )));
9952
9953                         FreeType(type);
9954
9955                         ProcessExpressionType(exp);
9956                         return;
9957                      }
9958                   }
9959                   else
9960                   {
9961                      // Check for reverse conversion
9962                      // (Convert in an instantiation later, so that we can use
9963                      //  deep properties system)
9964                      Symbol classSym = FindClass(id.string);
9965                      if(classSym)
9966                      {
9967                         Class convertClass = classSym.registered;
9968                         if(convertClass)
9969                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9970                      }
9971                   }
9972                }
9973
9974                if(prop)
9975                {
9976                   exp.member.memberType = propertyMember;
9977                   if(!prop.dataType)
9978                      ProcessPropertyType(prop);
9979                   exp.expType = prop.dataType;
9980                   if(prop.dataType) prop.dataType.refCount++;
9981                }
9982                else if(member)
9983                {
9984                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9985                   {
9986                      FreeExpContents(exp);
9987                      exp.type = identifierExp;
9988                      exp.identifier = MkIdentifier("class");
9989                      ProcessExpressionType(exp);
9990                      return;
9991                   }
9992
9993                   exp.member.memberType = dataMember;
9994                   DeclareStruct(_class.fullName, false);
9995                   if(!member.dataType)
9996                   {
9997                      Context context = SetupTemplatesContext(_class);
9998                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9999                      FinishTemplatesContext(context);
10000                   }
10001                   exp.expType = member.dataType;
10002                   if(member.dataType) member.dataType.refCount++;
10003                }
10004                else if(revConvert)
10005                {
10006                   exp.member.memberType = reverseConversionMember;
10007                   exp.expType = MkClassType(revConvert._class.fullName);
10008                }
10009                else if(method)
10010                {
10011                   //if(inCompiler)
10012                   {
10013                      /*if(id._class)
10014                      {
10015                         exp.type = identifierExp;
10016                         exp.identifier = exp.member.member;
10017                      }
10018                      else*/
10019                         exp.member.memberType = methodMember;
10020                   }
10021                   if(!method.dataType)
10022                      ProcessMethodType(method);
10023                   exp.expType = Type
10024                   {
10025                      refCount = 1;
10026                      kind = methodType;
10027                      method = method;
10028                   };
10029
10030                   // Tricky spot here... To use instance versus class virtual table
10031                   // Put it back to what it was... What did we break?
10032
10033                   // Had to put it back for overriding Main of Thread global instance
10034
10035                   //exp.expType.methodClass = _class;
10036                   exp.expType.methodClass = (id && id._class) ? _class : null;
10037
10038                   // Need the actual class used for templated classes
10039                   exp.expType.usedClass = _class;
10040                }
10041                else if(!classProp)
10042                {
10043                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10044                   {
10045                      FreeExpContents(exp);
10046                      exp.type = identifierExp;
10047                      exp.identifier = MkIdentifier("class");
10048                      FreeType(exp.expType);
10049                      exp.expType = MkClassType("ecere::com::Class");
10050                      return;
10051                   }
10052                   yylloc = exp.member.member.loc;
10053                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10054                   if(inCompiler)
10055                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10056                }
10057
10058                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10059                {
10060                   Class tClass;
10061
10062                   tClass = _class;
10063                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10064
10065                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10066                   {
10067                      int id = 0;
10068                      ClassTemplateParameter curParam = null;
10069                      Class sClass;
10070
10071                      for(sClass = tClass; sClass; sClass = sClass.base)
10072                      {
10073                         id = 0;
10074                         if(sClass.templateClass) sClass = sClass.templateClass;
10075                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10076                         {
10077                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10078                            {
10079                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10080                                  id += sClass.templateParams.count;
10081                               break;
10082                            }
10083                            id++;
10084                         }
10085                         if(curParam) break;
10086                      }
10087
10088                      if(curParam && tClass.templateArgs[id].dataTypeString)
10089                      {
10090                         ClassTemplateArgument arg = tClass.templateArgs[id];
10091                         Context context = SetupTemplatesContext(tClass);
10092                         /*if(!arg.dataType)
10093                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10094                         FreeType(exp.expType);
10095                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10096                         if(exp.expType)
10097                         {
10098                            if(exp.expType.kind == thisClassType)
10099                            {
10100                               FreeType(exp.expType);
10101                               exp.expType = ReplaceThisClassType(_class);
10102                            }
10103
10104                            if(tClass.templateClass)
10105                               exp.expType.passAsTemplate = true;
10106                            //exp.expType.refCount++;
10107                            if(!exp.destType)
10108                            {
10109                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10110                               //exp.destType.refCount++;
10111
10112                               if(exp.destType.kind == thisClassType)
10113                               {
10114                                  FreeType(exp.destType);
10115                                  exp.destType = ReplaceThisClassType(_class);
10116                               }
10117                            }
10118                         }
10119                         FinishTemplatesContext(context);
10120                      }
10121                   }
10122                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10123                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10124                   {
10125                      int id = 0;
10126                      ClassTemplateParameter curParam = null;
10127                      Class sClass;
10128
10129                      for(sClass = tClass; sClass; sClass = sClass.base)
10130                      {
10131                         id = 0;
10132                         if(sClass.templateClass) sClass = sClass.templateClass;
10133                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10134                         {
10135                            if(curParam.type == TemplateParameterType::type &&
10136                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10137                            {
10138                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10139                                  id += sClass.templateParams.count;
10140                               break;
10141                            }
10142                            id++;
10143                         }
10144                         if(curParam) break;
10145                      }
10146
10147                      if(curParam)
10148                      {
10149                         ClassTemplateArgument arg = tClass.templateArgs[id];
10150                         Context context = SetupTemplatesContext(tClass);
10151                         Type basicType;
10152                         /*if(!arg.dataType)
10153                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10154
10155                         basicType = ProcessTypeString(arg.dataTypeString, false);
10156                         if(basicType)
10157                         {
10158                            if(basicType.kind == thisClassType)
10159                            {
10160                               FreeType(basicType);
10161                               basicType = ReplaceThisClassType(_class);
10162                            }
10163
10164                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10165                            if(tClass.templateClass)
10166                               basicType.passAsTemplate = true;
10167                            */
10168
10169                            FreeType(exp.expType);
10170
10171                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10172                            //exp.expType.refCount++;
10173                            if(!exp.destType)
10174                            {
10175                               exp.destType = exp.expType;
10176                               exp.destType.refCount++;
10177                            }
10178
10179                            {
10180                               Expression newExp { };
10181                               OldList * specs = MkList();
10182                               Declarator decl;
10183                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10184                               *newExp = *exp;
10185                               if(exp.destType) exp.destType.refCount++;
10186                               if(exp.expType)  exp.expType.refCount++;
10187                               exp.type = castExp;
10188                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10189                               exp.cast.exp = newExp;
10190                               //FreeType(exp.expType);
10191                               //exp.expType = null;
10192                               //ProcessExpressionType(sourceExp);
10193                            }
10194                         }
10195                         FinishTemplatesContext(context);
10196                      }
10197                   }
10198                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10199                   {
10200                      Class expClass = exp.expType._class.registered;
10201                      if(expClass)
10202                      {
10203                         Class cClass = null;
10204                         int c;
10205                         int p = 0;
10206                         int paramCount = 0;
10207                         int lastParam = -1;
10208                         char templateString[1024];
10209                         ClassTemplateParameter param;
10210                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10211                         while(cClass != expClass)
10212                         {
10213                            Class sClass;
10214                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10215                            cClass = sClass;
10216
10217                            for(param = cClass.templateParams.first; param; param = param.next)
10218                            {
10219                               Class cClassCur = null;
10220                               int c;
10221                               int cp = 0;
10222                               ClassTemplateParameter paramCur = null;
10223                               ClassTemplateArgument arg;
10224                               while(cClassCur != tClass && !paramCur)
10225                               {
10226                                  Class sClassCur;
10227                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10228                                  cClassCur = sClassCur;
10229
10230                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10231                                  {
10232                                     if(!strcmp(paramCur.name, param.name))
10233                                     {
10234
10235                                        break;
10236                                     }
10237                                     cp++;
10238                                  }
10239                               }
10240                               if(paramCur && paramCur.type == TemplateParameterType::type)
10241                                  arg = tClass.templateArgs[cp];
10242                               else
10243                                  arg = expClass.templateArgs[p];
10244
10245                               {
10246                                  char argument[256];
10247                                  argument[0] = '\0';
10248                                  /*if(arg.name)
10249                                  {
10250                                     strcat(argument, arg.name.string);
10251                                     strcat(argument, " = ");
10252                                  }*/
10253                                  switch(param.type)
10254                                  {
10255                                     case expression:
10256                                     {
10257                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10258                                        char expString[1024];
10259                                        OldList * specs = MkList();
10260                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10261                                        Expression exp;
10262                                        char * string = PrintHexUInt64(arg.expression.ui64);
10263                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10264                                        delete string;
10265
10266                                        ProcessExpressionType(exp);
10267                                        ComputeExpression(exp);
10268                                        expString[0] = '\0';
10269                                        PrintExpression(exp, expString);
10270                                        strcat(argument, expString);
10271                                        // delete exp;
10272                                        FreeExpression(exp);
10273                                        break;
10274                                     }
10275                                     case identifier:
10276                                     {
10277                                        strcat(argument, arg.member.name);
10278                                        break;
10279                                     }
10280                                     case TemplateParameterType::type:
10281                                     {
10282                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10283                                           strcat(argument, arg.dataTypeString);
10284                                        break;
10285                                     }
10286                                  }
10287                                  if(argument[0])
10288                                  {
10289                                     if(paramCount) strcat(templateString, ", ");
10290                                     if(lastParam != p - 1)
10291                                     {
10292                                        strcat(templateString, param.name);
10293                                        strcat(templateString, " = ");
10294                                     }
10295                                     strcat(templateString, argument);
10296                                     paramCount++;
10297                                     lastParam = p;
10298                                  }
10299                               }
10300                               p++;
10301                            }
10302                         }
10303                         {
10304                            int len = strlen(templateString);
10305                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10306                            templateString[len++] = '>';
10307                            templateString[len++] = '\0';
10308                         }
10309
10310                         FreeType(exp.expType);
10311                         {
10312                            Context context = SetupTemplatesContext(tClass);
10313                            exp.expType = ProcessTypeString(templateString, false);
10314                            FinishTemplatesContext(context);
10315                         }
10316                      }
10317                   }
10318                }
10319             }
10320             else
10321                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10322          }
10323          else if(type && (type.kind == structType || type.kind == unionType))
10324          {
10325             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10326             if(memberType)
10327             {
10328                exp.expType = memberType;
10329                if(memberType)
10330                   memberType.refCount++;
10331             }
10332          }
10333          else
10334          {
10335             char expString[10240];
10336             expString[0] = '\0';
10337             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10338             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10339          }
10340
10341          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10342          {
10343             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10344             {
10345                Identifier id = exp.member.member;
10346                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10347                if(_class)
10348                {
10349                   FreeType(exp.expType);
10350                   exp.expType = ReplaceThisClassType(_class);
10351                }
10352             }
10353          }
10354          yylloc = oldyylloc;
10355          break;
10356       }
10357       // Convert x->y into (*x).y
10358       case pointerExp:
10359       {
10360          Type destType = exp.destType;
10361
10362          // DOING THIS LATER NOW...
10363          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10364          {
10365             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10366             /* TODO: Name Space Fix ups
10367             if(!exp.member.member.classSym)
10368                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10369             */
10370          }
10371
10372          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10373          exp.type = memberExp;
10374          if(destType)
10375             destType.count++;
10376          ProcessExpressionType(exp);
10377          if(destType)
10378             destType.count--;
10379          break;
10380       }
10381       case classSizeExp:
10382       {
10383          //ComputeExpression(exp);
10384
10385          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10386          if(classSym && classSym.registered)
10387          {
10388             if(classSym.registered.type == noHeadClass)
10389             {
10390                char name[1024];
10391                name[0] = '\0';
10392                DeclareStruct(classSym.string, false);
10393                FreeSpecifier(exp._class);
10394                exp.type = typeSizeExp;
10395                FullClassNameCat(name, classSym.string, false);
10396                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10397             }
10398             else
10399             {
10400                if(classSym.registered.fixed)
10401                {
10402                   FreeSpecifier(exp._class);
10403                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10404                   exp.type = constantExp;
10405                }
10406                else
10407                {
10408                   char className[1024];
10409                   strcpy(className, "__ecereClass_");
10410                   FullClassNameCat(className, classSym.string, true);
10411                   MangleClassName(className);
10412
10413                   DeclareClass(classSym, className);
10414
10415                   FreeExpContents(exp);
10416                   exp.type = pointerExp;
10417                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10418                   exp.member.member = MkIdentifier("structSize");
10419                }
10420             }
10421          }
10422
10423          exp.expType = Type
10424          {
10425             refCount = 1;
10426             kind = intType;
10427          };
10428          // exp.isConstant = true;
10429          break;
10430       }
10431       case typeSizeExp:
10432       {
10433          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10434
10435          exp.expType = Type
10436          {
10437             refCount = 1;
10438             kind = intType;
10439          };
10440          exp.isConstant = true;
10441
10442          DeclareType(type, false, false);
10443          FreeType(type);
10444          break;
10445       }
10446       case castExp:
10447       {
10448          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10449          type.count = 1;
10450          FreeType(exp.cast.exp.destType);
10451          exp.cast.exp.destType = type;
10452          type.refCount++;
10453          ProcessExpressionType(exp.cast.exp);
10454          type.count = 0;
10455          exp.expType = type;
10456          //type.refCount++;
10457
10458          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10459          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10460          {
10461             void * prev = exp.prev, * next = exp.next;
10462             Type expType = exp.cast.exp.destType;
10463             Expression castExp = exp.cast.exp;
10464             Type destType = exp.destType;
10465
10466             if(expType) expType.refCount++;
10467
10468             //FreeType(exp.destType);
10469             FreeType(exp.expType);
10470             FreeTypeName(exp.cast.typeName);
10471
10472             *exp = *castExp;
10473             FreeType(exp.expType);
10474             FreeType(exp.destType);
10475
10476             exp.expType = expType;
10477             exp.destType = destType;
10478
10479             delete castExp;
10480
10481             exp.prev = prev;
10482             exp.next = next;
10483
10484          }
10485          else
10486          {
10487             exp.isConstant = exp.cast.exp.isConstant;
10488          }
10489          //FreeType(type);
10490          break;
10491       }
10492       case extensionInitializerExp:
10493       {
10494          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10495          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10496          // ProcessInitializer(exp.initializer.initializer, type);
10497          exp.expType = type;
10498          break;
10499       }
10500       case vaArgExp:
10501       {
10502          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10503          ProcessExpressionType(exp.vaArg.exp);
10504          exp.expType = type;
10505          break;
10506       }
10507       case conditionExp:
10508       {
10509          Expression e;
10510          exp.isConstant = true;
10511
10512          FreeType(exp.cond.cond.destType);
10513          exp.cond.cond.destType = MkClassType("bool");
10514          exp.cond.cond.destType.truth = true;
10515          ProcessExpressionType(exp.cond.cond);
10516          if(!exp.cond.cond.isConstant)
10517             exp.isConstant = false;
10518          for(e = exp.cond.exp->first; e; e = e.next)
10519          {
10520             if(!e.next)
10521             {
10522                FreeType(e.destType);
10523                e.destType = exp.destType;
10524                if(e.destType) e.destType.refCount++;
10525             }
10526             ProcessExpressionType(e);
10527             if(!e.next)
10528             {
10529                exp.expType = e.expType;
10530                if(e.expType) e.expType.refCount++;
10531             }
10532             if(!e.isConstant)
10533                exp.isConstant = false;
10534          }
10535
10536          FreeType(exp.cond.elseExp.destType);
10537          // Added this check if we failed to find an expType
10538          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10539
10540          // Reversed it...
10541          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10542
10543          if(exp.cond.elseExp.destType)
10544             exp.cond.elseExp.destType.refCount++;
10545          ProcessExpressionType(exp.cond.elseExp);
10546
10547          // FIXED THIS: Was done before calling process on elseExp
10548          if(!exp.cond.elseExp.isConstant)
10549             exp.isConstant = false;
10550          break;
10551       }
10552       case extensionCompoundExp:
10553       {
10554          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10555          {
10556             Statement last = exp.compound.compound.statements->last;
10557             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10558             {
10559                ((Expression)last.expressions->last).destType = exp.destType;
10560                if(exp.destType)
10561                   exp.destType.refCount++;
10562             }
10563             ProcessStatement(exp.compound);
10564             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10565             if(exp.expType)
10566                exp.expType.refCount++;
10567          }
10568          break;
10569       }
10570       case classExp:
10571       {
10572          Specifier spec = exp._classExp.specifiers->first;
10573          if(spec && spec.type == nameSpecifier)
10574          {
10575             exp.expType = MkClassType(spec.name);
10576             exp.expType.kind = subClassType;
10577             exp.byReference = true;
10578          }
10579          else
10580          {
10581             exp.expType = MkClassType("ecere::com::Class");
10582             exp.byReference = true;
10583          }
10584          break;
10585       }
10586       case classDataExp:
10587       {
10588          Class _class = thisClass ? thisClass : currentClass;
10589          if(_class)
10590          {
10591             Identifier id = exp.classData.id;
10592             char structName[1024];
10593             Expression classExp;
10594             strcpy(structName, "__ecereClassData_");
10595             FullClassNameCat(structName, _class.fullName, false);
10596             exp.type = pointerExp;
10597             exp.member.member = id;
10598             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10599                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10600             else
10601                classExp = MkExpIdentifier(MkIdentifier("class"));
10602
10603             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10604                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10605                   MkExpBrackets(MkListOne(MkExpOp(
10606                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10607                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10608                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10609                      )));
10610
10611             ProcessExpressionType(exp);
10612             return;
10613          }
10614          break;
10615       }
10616       case arrayExp:
10617       {
10618          Type type = null;
10619          char * typeString = null;
10620          char typeStringBuf[1024];
10621          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10622             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10623          {
10624             Class templateClass = exp.destType._class.registered;
10625             typeString = templateClass.templateArgs[2].dataTypeString;
10626          }
10627          else if(exp.list)
10628          {
10629             // Guess type from expressions in the array
10630             Expression e;
10631             for(e = exp.list->first; e; e = e.next)
10632             {
10633                ProcessExpressionType(e);
10634                if(e.expType)
10635                {
10636                   if(!type) { type = e.expType; type.refCount++; }
10637                   else
10638                   {
10639                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10640                      if(!MatchTypeExpression(e, type, null, false))
10641                      {
10642                         FreeType(type);
10643                         type = e.expType;
10644                         e.expType = null;
10645
10646                         e = exp.list->first;
10647                         ProcessExpressionType(e);
10648                         if(e.expType)
10649                         {
10650                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10651                            if(!MatchTypeExpression(e, type, null, false))
10652                            {
10653                               FreeType(e.expType);
10654                               e.expType = null;
10655                               FreeType(type);
10656                               type = null;
10657                               break;
10658                            }
10659                         }
10660                      }
10661                   }
10662                   if(e.expType)
10663                   {
10664                      FreeType(e.expType);
10665                      e.expType = null;
10666                   }
10667                }
10668             }
10669             if(type)
10670             {
10671                typeStringBuf[0] = '\0';
10672                PrintTypeNoConst(type, typeStringBuf, false, true);
10673                typeString = typeStringBuf;
10674                FreeType(type);
10675                type = null;
10676             }
10677          }
10678          if(typeString)
10679          {
10680             /*
10681             (Container)& (struct BuiltInContainer)
10682             {
10683                ._vTbl = class(BuiltInContainer)._vTbl,
10684                ._class = class(BuiltInContainer),
10685                .refCount = 0,
10686                .data = (int[]){ 1, 7, 3, 4, 5 },
10687                .count = 5,
10688                .type = class(int),
10689             }
10690             */
10691             char templateString[1024];
10692             OldList * initializers = MkList();
10693             OldList * structInitializers = MkList();
10694             OldList * specs = MkList();
10695             Expression expExt;
10696             Declarator decl = SpecDeclFromString(typeString, specs, null);
10697             sprintf(templateString, "Container<%s>", typeString);
10698
10699             if(exp.list)
10700             {
10701                Expression e;
10702                type = ProcessTypeString(typeString, false);
10703                while(e = exp.list->first)
10704                {
10705                   exp.list->Remove(e);
10706                   e.destType = type;
10707                   type.refCount++;
10708                   ProcessExpressionType(e);
10709                   ListAdd(initializers, MkInitializerAssignment(e));
10710                }
10711                FreeType(type);
10712                delete exp.list;
10713             }
10714
10715             DeclareStruct("ecere::com::BuiltInContainer", false);
10716
10717             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10718                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10719             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10720                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10721             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10722                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10723             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10724                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10725                MkInitializerList(initializers))));
10726                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10727             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10728                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10729             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10730                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10731             exp.expType = ProcessTypeString(templateString, false);
10732             exp.type = bracketsExp;
10733             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10734                MkExpOp(null, '&',
10735                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10736                   MkInitializerList(structInitializers)))));
10737             ProcessExpressionType(expExt);
10738          }
10739          else
10740          {
10741             exp.expType = ProcessTypeString("Container", false);
10742             Compiler_Error($"Couldn't determine type of array elements\n");
10743          }
10744          break;
10745       }
10746    }
10747
10748    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10749    {
10750       FreeType(exp.expType);
10751       exp.expType = ReplaceThisClassType(thisClass);
10752    }
10753
10754    // Resolve structures here
10755    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10756    {
10757       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10758       // TODO: Fix members reference...
10759       if(symbol)
10760       {
10761          if(exp.expType.kind != enumType)
10762          {
10763             Type member;
10764             String enumName = CopyString(exp.expType.enumName);
10765
10766             // Fixed a memory leak on self-referencing C structs typedefs
10767             // by instantiating a new type rather than simply copying members
10768             // into exp.expType
10769             FreeType(exp.expType);
10770             exp.expType = Type { };
10771             exp.expType.kind = symbol.type.kind;
10772             exp.expType.refCount++;
10773             exp.expType.enumName = enumName;
10774
10775             exp.expType.members = symbol.type.members;
10776             for(member = symbol.type.members.first; member; member = member.next)
10777                member.refCount++;
10778          }
10779          else
10780          {
10781             NamedLink member;
10782             for(member = symbol.type.members.first; member; member = member.next)
10783             {
10784                NamedLink value { name = CopyString(member.name) };
10785                exp.expType.members.Add(value);
10786             }
10787          }
10788       }
10789    }
10790
10791    yylloc = exp.loc;
10792    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10793    else if(exp.destType && !exp.destType.keepCast)
10794    {
10795       if(!CheckExpressionType(exp, exp.destType, false))
10796       {
10797          if(!exp.destType.count || unresolved)
10798          {
10799             if(!exp.expType)
10800             {
10801                yylloc = exp.loc;
10802                if(exp.destType.kind != ellipsisType)
10803                {
10804                   char type2[1024];
10805                   type2[0] = '\0';
10806                   if(inCompiler)
10807                   {
10808                      char expString[10240];
10809                      expString[0] = '\0';
10810
10811                      PrintType(exp.destType, type2, false, true);
10812
10813                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10814                      if(unresolved)
10815                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10816                      else if(exp.type != dummyExp)
10817                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10818                   }
10819                }
10820                else
10821                {
10822                   char expString[10240] ;
10823                   expString[0] = '\0';
10824                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10825
10826                   if(unresolved)
10827                      Compiler_Error($"unresolved identifier %s\n", expString);
10828                   else if(exp.type != dummyExp)
10829                      Compiler_Error($"couldn't determine type of %s\n", expString);
10830                }
10831             }
10832             else
10833             {
10834                char type1[1024];
10835                char type2[1024];
10836                type1[0] = '\0';
10837                type2[0] = '\0';
10838                if(inCompiler)
10839                {
10840                   PrintType(exp.expType, type1, false, true);
10841                   PrintType(exp.destType, type2, false, true);
10842                }
10843
10844                //CheckExpressionType(exp, exp.destType, false);
10845
10846                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10847                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10848                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10849                else
10850                {
10851                   char expString[10240];
10852                   expString[0] = '\0';
10853                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10854
10855 #ifdef _DEBUG
10856                   CheckExpressionType(exp, exp.destType, false);
10857 #endif
10858                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10859                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10860                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10861
10862                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10863                   FreeType(exp.expType);
10864                   exp.destType.refCount++;
10865                   exp.expType = exp.destType;
10866                }
10867             }
10868          }
10869       }
10870       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10871       {
10872          Expression newExp { };
10873          char typeString[1024];
10874          OldList * specs = MkList();
10875          Declarator decl;
10876
10877          typeString[0] = '\0';
10878
10879          *newExp = *exp;
10880
10881          if(exp.expType)  exp.expType.refCount++;
10882          if(exp.expType)  exp.expType.refCount++;
10883          exp.type = castExp;
10884          newExp.destType = exp.expType;
10885
10886          PrintType(exp.expType, typeString, false, false);
10887          decl = SpecDeclFromString(typeString, specs, null);
10888
10889          exp.cast.typeName = MkTypeName(specs, decl);
10890          exp.cast.exp = newExp;
10891       }
10892    }
10893    else if(unresolved)
10894    {
10895       if(exp.identifier._class && exp.identifier._class.name)
10896          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10897       else if(exp.identifier.string && exp.identifier.string[0])
10898          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10899    }
10900    else if(!exp.expType && exp.type != dummyExp)
10901    {
10902       char expString[10240];
10903       expString[0] = '\0';
10904       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10905       Compiler_Error($"couldn't determine type of %s\n", expString);
10906    }
10907
10908    // Let's try to support any_object & typed_object here:
10909    if(inCompiler)
10910       ApplyAnyObjectLogic(exp);
10911
10912    // Mark nohead classes as by reference, unless we're casting them to an integral type
10913    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10914       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10915          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10916           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10917    {
10918       exp.byReference = true;
10919    }
10920    yylloc = oldyylloc;
10921 }
10922
10923 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10924 {
10925    // THIS CODE WILL FIND NEXT MEMBER...
10926    if(*curMember)
10927    {
10928       *curMember = (*curMember).next;
10929
10930       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10931       {
10932          *curMember = subMemberStack[--(*subMemberStackPos)];
10933          *curMember = (*curMember).next;
10934       }
10935
10936       // SKIP ALL PROPERTIES HERE...
10937       while((*curMember) && (*curMember).isProperty)
10938          *curMember = (*curMember).next;
10939
10940       if(subMemberStackPos)
10941       {
10942          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10943          {
10944             subMemberStack[(*subMemberStackPos)++] = *curMember;
10945
10946             *curMember = (*curMember).members.first;
10947             while(*curMember && (*curMember).isProperty)
10948                *curMember = (*curMember).next;
10949          }
10950       }
10951    }
10952    while(!*curMember)
10953    {
10954       if(!*curMember)
10955       {
10956          if(subMemberStackPos && *subMemberStackPos)
10957          {
10958             *curMember = subMemberStack[--(*subMemberStackPos)];
10959             *curMember = (*curMember).next;
10960          }
10961          else
10962          {
10963             Class lastCurClass = *curClass;
10964
10965             if(*curClass == _class) break;     // REACHED THE END
10966
10967             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10968             *curMember = (*curClass).membersAndProperties.first;
10969          }
10970
10971          while((*curMember) && (*curMember).isProperty)
10972             *curMember = (*curMember).next;
10973          if(subMemberStackPos)
10974          {
10975             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10976             {
10977                subMemberStack[(*subMemberStackPos)++] = *curMember;
10978
10979                *curMember = (*curMember).members.first;
10980                while(*curMember && (*curMember).isProperty)
10981                   *curMember = (*curMember).next;
10982             }
10983          }
10984       }
10985    }
10986 }
10987
10988
10989 static void ProcessInitializer(Initializer init, Type type)
10990 {
10991    switch(init.type)
10992    {
10993       case expInitializer:
10994          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10995          {
10996             // TESTING THIS FOR SHUTTING = 0 WARNING
10997             if(init.exp && !init.exp.destType)
10998             {
10999                FreeType(init.exp.destType);
11000                init.exp.destType = type;
11001                if(type) type.refCount++;
11002             }
11003             if(init.exp)
11004             {
11005                ProcessExpressionType(init.exp);
11006                init.isConstant = init.exp.isConstant;
11007             }
11008             break;
11009          }
11010          else
11011          {
11012             Expression exp = init.exp;
11013             Instantiation inst = exp.instance;
11014             MembersInit members;
11015
11016             init.type = listInitializer;
11017             init.list = MkList();
11018
11019             if(inst.members)
11020             {
11021                for(members = inst.members->first; members; members = members.next)
11022                {
11023                   if(members.type == dataMembersInit)
11024                   {
11025                      MemberInit member;
11026                      for(member = members.dataMembers->first; member; member = member.next)
11027                      {
11028                         ListAdd(init.list, member.initializer);
11029                         member.initializer = null;
11030                      }
11031                   }
11032                   // Discard all MembersInitMethod
11033                }
11034             }
11035             FreeExpression(exp);
11036          }
11037       case listInitializer:
11038       {
11039          Initializer i;
11040          Type initializerType = null;
11041          Class curClass = null;
11042          DataMember curMember = null;
11043          DataMember subMemberStack[256];
11044          int subMemberStackPos = 0;
11045
11046          if(type && type.kind == arrayType)
11047             initializerType = Dereference(type);
11048          else if(type && (type.kind == structType || type.kind == unionType))
11049             initializerType = type.members.first;
11050
11051          for(i = init.list->first; i; i = i.next)
11052          {
11053             if(type && type.kind == classType && type._class && type._class.registered)
11054             {
11055                // 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)
11056                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11057                // TODO: Generate error on initializing a private data member this way from another module...
11058                if(curMember)
11059                {
11060                   if(!curMember.dataType)
11061                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11062                   initializerType = curMember.dataType;
11063                }
11064             }
11065             ProcessInitializer(i, initializerType);
11066             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11067                initializerType = initializerType.next;
11068             if(!i.isConstant)
11069                init.isConstant = false;
11070          }
11071
11072          if(type && type.kind == arrayType)
11073             FreeType(initializerType);
11074
11075          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11076          {
11077             Compiler_Error($"Assigning list initializer to non list\n");
11078          }
11079          break;
11080       }
11081    }
11082 }
11083
11084 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11085 {
11086    switch(spec.type)
11087    {
11088       case baseSpecifier:
11089       {
11090          if(spec.specifier == THISCLASS)
11091          {
11092             if(thisClass)
11093             {
11094                spec.type = nameSpecifier;
11095                spec.name = ReplaceThisClass(thisClass);
11096                spec.symbol = FindClass(spec.name);
11097                ProcessSpecifier(spec, declareStruct);
11098             }
11099          }
11100          break;
11101       }
11102       case nameSpecifier:
11103       {
11104          Symbol symbol = FindType(curContext, spec.name);
11105          if(symbol)
11106             DeclareType(symbol.type, true, true);
11107          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11108             DeclareStruct(spec.name, false);
11109          break;
11110       }
11111       case enumSpecifier:
11112       {
11113          Enumerator e;
11114          if(spec.list)
11115          {
11116             for(e = spec.list->first; e; e = e.next)
11117             {
11118                if(e.exp)
11119                   ProcessExpressionType(e.exp);
11120             }
11121          }
11122          break;
11123       }
11124       case structSpecifier:
11125       case unionSpecifier:
11126       {
11127          if(spec.definitions)
11128          {
11129             ClassDef def;
11130             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11131             //if(symbol)
11132                ProcessClass(spec.definitions, symbol);
11133             /*else
11134             {
11135                for(def = spec.definitions->first; def; def = def.next)
11136                {
11137                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11138                      ProcessDeclaration(def.decl);
11139                }
11140             }*/
11141          }
11142          break;
11143       }
11144       /*
11145       case classSpecifier:
11146       {
11147          Symbol classSym = FindClass(spec.name);
11148          if(classSym && classSym.registered && classSym.registered.type == structClass)
11149             DeclareStruct(spec.name, false);
11150          break;
11151       }
11152       */
11153    }
11154 }
11155
11156
11157 static void ProcessDeclarator(Declarator decl)
11158 {
11159    switch(decl.type)
11160    {
11161       case identifierDeclarator:
11162          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11163          {
11164             FreeSpecifier(decl.identifier._class);
11165             decl.identifier._class = null;
11166          }
11167          break;
11168       case arrayDeclarator:
11169          if(decl.array.exp)
11170             ProcessExpressionType(decl.array.exp);
11171       case structDeclarator:
11172       case bracketsDeclarator:
11173       case functionDeclarator:
11174       case pointerDeclarator:
11175       case extendedDeclarator:
11176       case extendedDeclaratorEnd:
11177          if(decl.declarator)
11178             ProcessDeclarator(decl.declarator);
11179          if(decl.type == functionDeclarator)
11180          {
11181             Identifier id = GetDeclId(decl);
11182             if(id && id._class)
11183             {
11184                TypeName param
11185                {
11186                   qualifiers = MkListOne(id._class);
11187                   declarator = null;
11188                };
11189                if(!decl.function.parameters)
11190                   decl.function.parameters = MkList();
11191                decl.function.parameters->Insert(null, param);
11192                id._class = null;
11193             }
11194             if(decl.function.parameters)
11195             {
11196                TypeName param;
11197
11198                for(param = decl.function.parameters->first; param; param = param.next)
11199                {
11200                   if(param.qualifiers && param.qualifiers->first)
11201                   {
11202                      Specifier spec = param.qualifiers->first;
11203                      if(spec && spec.specifier == TYPED_OBJECT)
11204                      {
11205                         Declarator d = param.declarator;
11206                         TypeName newParam
11207                         {
11208                            qualifiers = MkListOne(MkSpecifier(VOID));
11209                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11210                         };
11211
11212                         FreeList(param.qualifiers, FreeSpecifier);
11213
11214                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11215                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11216
11217                         decl.function.parameters->Insert(param, newParam);
11218                         param = newParam;
11219                      }
11220                      else if(spec && spec.specifier == ANY_OBJECT)
11221                      {
11222                         Declarator d = param.declarator;
11223
11224                         FreeList(param.qualifiers, FreeSpecifier);
11225
11226                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11227                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11228                      }
11229                      else if(spec.specifier == THISCLASS)
11230                      {
11231                         if(thisClass)
11232                         {
11233                            spec.type = nameSpecifier;
11234                            spec.name = ReplaceThisClass(thisClass);
11235                            spec.symbol = FindClass(spec.name);
11236                            ProcessSpecifier(spec, false);
11237                         }
11238                      }
11239                   }
11240
11241                   if(param.declarator)
11242                      ProcessDeclarator(param.declarator);
11243                }
11244             }
11245          }
11246          break;
11247    }
11248 }
11249
11250 static void ProcessDeclaration(Declaration decl)
11251 {
11252    yylloc = decl.loc;
11253    switch(decl.type)
11254    {
11255       case initDeclaration:
11256       {
11257          bool declareStruct = false;
11258          /*
11259          lineNum = decl.pos.line;
11260          column = decl.pos.col;
11261          */
11262
11263          if(decl.declarators)
11264          {
11265             InitDeclarator d;
11266
11267             for(d = decl.declarators->first; d; d = d.next)
11268             {
11269                Type type, subType;
11270                ProcessDeclarator(d.declarator);
11271
11272                type = ProcessType(decl.specifiers, d.declarator);
11273
11274                if(d.initializer)
11275                {
11276                   ProcessInitializer(d.initializer, type);
11277
11278                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11279
11280                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11281                      d.initializer.exp.type == instanceExp)
11282                   {
11283                      if(type.kind == classType && type._class ==
11284                         d.initializer.exp.expType._class)
11285                      {
11286                         Instantiation inst = d.initializer.exp.instance;
11287                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11288
11289                         d.initializer.exp.instance = null;
11290                         if(decl.specifiers)
11291                            FreeList(decl.specifiers, FreeSpecifier);
11292                         FreeList(decl.declarators, FreeInitDeclarator);
11293
11294                         d = null;
11295
11296                         decl.type = instDeclaration;
11297                         decl.inst = inst;
11298                      }
11299                   }
11300                }
11301                for(subType = type; subType;)
11302                {
11303                   if(subType.kind == classType)
11304                   {
11305                      declareStruct = true;
11306                      break;
11307                   }
11308                   else if(subType.kind == pointerType)
11309                      break;
11310                   else if(subType.kind == arrayType)
11311                      subType = subType.arrayType;
11312                   else
11313                      break;
11314                }
11315
11316                FreeType(type);
11317                if(!d) break;
11318             }
11319          }
11320
11321          if(decl.specifiers)
11322          {
11323             Specifier s;
11324             for(s = decl.specifiers->first; s; s = s.next)
11325             {
11326                ProcessSpecifier(s, declareStruct);
11327             }
11328          }
11329          break;
11330       }
11331       case instDeclaration:
11332       {
11333          ProcessInstantiationType(decl.inst);
11334          break;
11335       }
11336       case structDeclaration:
11337       {
11338          Specifier spec;
11339          Declarator d;
11340          bool declareStruct = false;
11341
11342          if(decl.declarators)
11343          {
11344             for(d = decl.declarators->first; d; d = d.next)
11345             {
11346                Type type = ProcessType(decl.specifiers, d.declarator);
11347                Type subType;
11348                ProcessDeclarator(d);
11349                for(subType = type; subType;)
11350                {
11351                   if(subType.kind == classType)
11352                   {
11353                      declareStruct = true;
11354                      break;
11355                   }
11356                   else if(subType.kind == pointerType)
11357                      break;
11358                   else if(subType.kind == arrayType)
11359                      subType = subType.arrayType;
11360                   else
11361                      break;
11362                }
11363                FreeType(type);
11364             }
11365          }
11366          if(decl.specifiers)
11367          {
11368             for(spec = decl.specifiers->first; spec; spec = spec.next)
11369                ProcessSpecifier(spec, declareStruct);
11370          }
11371          break;
11372       }
11373    }
11374 }
11375
11376 static FunctionDefinition curFunction;
11377
11378 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11379 {
11380    char propName[1024], propNameM[1024];
11381    char getName[1024], setName[1024];
11382    OldList * args;
11383
11384    DeclareProperty(prop, setName, getName);
11385
11386    // eInstance_FireWatchers(object, prop);
11387    strcpy(propName, "__ecereProp_");
11388    FullClassNameCat(propName, prop._class.fullName, false);
11389    strcat(propName, "_");
11390    // strcat(propName, prop.name);
11391    FullClassNameCat(propName, prop.name, true);
11392    MangleClassName(propName);
11393
11394    strcpy(propNameM, "__ecerePropM_");
11395    FullClassNameCat(propNameM, prop._class.fullName, false);
11396    strcat(propNameM, "_");
11397    // strcat(propNameM, prop.name);
11398    FullClassNameCat(propNameM, prop.name, true);
11399    MangleClassName(propNameM);
11400
11401    if(prop.isWatchable)
11402    {
11403       args = MkList();
11404       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11405       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11406       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11407
11408       args = MkList();
11409       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11410       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11411       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11412    }
11413
11414
11415    {
11416       args = MkList();
11417       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11418       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11419       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11420
11421       args = MkList();
11422       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11423       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11424       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11425    }
11426
11427    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11428       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11429       curFunction.propSet.fireWatchersDone = true;
11430 }
11431
11432 static void ProcessStatement(Statement stmt)
11433 {
11434    yylloc = stmt.loc;
11435    /*
11436    lineNum = stmt.pos.line;
11437    column = stmt.pos.col;
11438    */
11439    switch(stmt.type)
11440    {
11441       case labeledStmt:
11442          ProcessStatement(stmt.labeled.stmt);
11443          break;
11444       case caseStmt:
11445          // This expression should be constant...
11446          if(stmt.caseStmt.exp)
11447          {
11448             FreeType(stmt.caseStmt.exp.destType);
11449             stmt.caseStmt.exp.destType = curSwitchType;
11450             if(curSwitchType) curSwitchType.refCount++;
11451             ProcessExpressionType(stmt.caseStmt.exp);
11452             ComputeExpression(stmt.caseStmt.exp);
11453          }
11454          if(stmt.caseStmt.stmt)
11455             ProcessStatement(stmt.caseStmt.stmt);
11456          break;
11457       case compoundStmt:
11458       {
11459          if(stmt.compound.context)
11460          {
11461             Declaration decl;
11462             Statement s;
11463
11464             Statement prevCompound = curCompound;
11465             Context prevContext = curContext;
11466
11467             if(!stmt.compound.isSwitch)
11468                curCompound = stmt;
11469             curContext = stmt.compound.context;
11470
11471             if(stmt.compound.declarations)
11472             {
11473                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11474                   ProcessDeclaration(decl);
11475             }
11476             if(stmt.compound.statements)
11477             {
11478                for(s = stmt.compound.statements->first; s; s = s.next)
11479                   ProcessStatement(s);
11480             }
11481
11482             curContext = prevContext;
11483             curCompound = prevCompound;
11484          }
11485          break;
11486       }
11487       case expressionStmt:
11488       {
11489          Expression exp;
11490          if(stmt.expressions)
11491          {
11492             for(exp = stmt.expressions->first; exp; exp = exp.next)
11493                ProcessExpressionType(exp);
11494          }
11495          break;
11496       }
11497       case ifStmt:
11498       {
11499          Expression exp;
11500
11501          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11502          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11503          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11504          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11505          {
11506             ProcessExpressionType(exp);
11507          }
11508          if(stmt.ifStmt.stmt)
11509             ProcessStatement(stmt.ifStmt.stmt);
11510          if(stmt.ifStmt.elseStmt)
11511             ProcessStatement(stmt.ifStmt.elseStmt);
11512          break;
11513       }
11514       case switchStmt:
11515       {
11516          Type oldSwitchType = curSwitchType;
11517          if(stmt.switchStmt.exp)
11518          {
11519             Expression exp;
11520             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11521             {
11522                if(!exp.next)
11523                {
11524                   /*
11525                   Type destType
11526                   {
11527                      kind = intType;
11528                      refCount = 1;
11529                   };
11530                   e.exp.destType = destType;
11531                   */
11532
11533                   ProcessExpressionType(exp);
11534                }
11535                if(!exp.next)
11536                   curSwitchType = exp.expType;
11537             }
11538          }
11539          ProcessStatement(stmt.switchStmt.stmt);
11540          curSwitchType = oldSwitchType;
11541          break;
11542       }
11543       case whileStmt:
11544       {
11545          if(stmt.whileStmt.exp)
11546          {
11547             Expression exp;
11548
11549             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11550             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11551             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11552             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11553             {
11554                ProcessExpressionType(exp);
11555             }
11556          }
11557          if(stmt.whileStmt.stmt)
11558             ProcessStatement(stmt.whileStmt.stmt);
11559          break;
11560       }
11561       case doWhileStmt:
11562       {
11563          if(stmt.doWhile.exp)
11564          {
11565             Expression exp;
11566
11567             if(stmt.doWhile.exp->last)
11568             {
11569                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11570                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11571                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11572             }
11573             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11574             {
11575                ProcessExpressionType(exp);
11576             }
11577          }
11578          if(stmt.doWhile.stmt)
11579             ProcessStatement(stmt.doWhile.stmt);
11580          break;
11581       }
11582       case forStmt:
11583       {
11584          Expression exp;
11585          if(stmt.forStmt.init)
11586             ProcessStatement(stmt.forStmt.init);
11587
11588          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11589          {
11590             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11591             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11592             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11593          }
11594
11595          if(stmt.forStmt.check)
11596             ProcessStatement(stmt.forStmt.check);
11597          if(stmt.forStmt.increment)
11598          {
11599             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11600                ProcessExpressionType(exp);
11601          }
11602
11603          if(stmt.forStmt.stmt)
11604             ProcessStatement(stmt.forStmt.stmt);
11605          break;
11606       }
11607       case forEachStmt:
11608       {
11609          Identifier id = stmt.forEachStmt.id;
11610          OldList * exp = stmt.forEachStmt.exp;
11611          OldList * filter = stmt.forEachStmt.filter;
11612          Statement block = stmt.forEachStmt.stmt;
11613          char iteratorType[1024];
11614          Type source;
11615          Expression e;
11616          bool isBuiltin = exp && exp->last &&
11617             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11618               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11619          Expression arrayExp;
11620          char * typeString = null;
11621          int builtinCount = 0;
11622
11623          for(e = exp ? exp->first : null; e; e = e.next)
11624          {
11625             if(!e.next)
11626             {
11627                FreeType(e.destType);
11628                e.destType = ProcessTypeString("Container", false);
11629             }
11630             if(!isBuiltin || e.next)
11631                ProcessExpressionType(e);
11632          }
11633
11634          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11635          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11636             eClass_IsDerived(source._class.registered, containerClass)))
11637          {
11638             Class _class = source ? source._class.registered : null;
11639             Symbol symbol;
11640             Expression expIt = null;
11641             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11642             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11643             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11644             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11645             stmt.type = compoundStmt;
11646
11647             stmt.compound.context = Context { };
11648             stmt.compound.context.parent = curContext;
11649             curContext = stmt.compound.context;
11650
11651             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11652             {
11653                Class mapClass = eSystem_FindClass(privateModule, "Map");
11654                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11655                isCustomAVLTree = true;
11656                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11657                   isAVLTree = true;
11658                else if(eClass_IsDerived(source._class.registered, mapClass))
11659                   isMap = true;
11660             }
11661             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11662             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11663             {
11664                Class listClass = eSystem_FindClass(privateModule, "List");
11665                isLinkList = true;
11666                isList = eClass_IsDerived(source._class.registered, listClass);
11667             }
11668
11669             if(isArray)
11670             {
11671                Declarator decl;
11672                OldList * specs = MkList();
11673                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11674                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11675                stmt.compound.declarations = MkListOne(
11676                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11677                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11678                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11679                      MkInitializerAssignment(MkExpBrackets(exp))))));
11680             }
11681             else if(isBuiltin)
11682             {
11683                Type type = null;
11684                char typeStringBuf[1024];
11685
11686                // TODO: Merge this code?
11687                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11688                if(((Expression)exp->last).type == castExp)
11689                {
11690                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11691                   if(typeName)
11692                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11693                }
11694
11695                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11696                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11697                   arrayExp.destType._class.registered.templateArgs)
11698                {
11699                   Class templateClass = arrayExp.destType._class.registered;
11700                   typeString = templateClass.templateArgs[2].dataTypeString;
11701                }
11702                else if(arrayExp.list)
11703                {
11704                   // Guess type from expressions in the array
11705                   Expression e;
11706                   for(e = arrayExp.list->first; e; e = e.next)
11707                   {
11708                      ProcessExpressionType(e);
11709                      if(e.expType)
11710                      {
11711                         if(!type) { type = e.expType; type.refCount++; }
11712                         else
11713                         {
11714                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11715                            if(!MatchTypeExpression(e, type, null, false))
11716                            {
11717                               FreeType(type);
11718                               type = e.expType;
11719                               e.expType = null;
11720
11721                               e = arrayExp.list->first;
11722                               ProcessExpressionType(e);
11723                               if(e.expType)
11724                               {
11725                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11726                                  if(!MatchTypeExpression(e, type, null, false))
11727                                  {
11728                                     FreeType(e.expType);
11729                                     e.expType = null;
11730                                     FreeType(type);
11731                                     type = null;
11732                                     break;
11733                                  }
11734                               }
11735                            }
11736                         }
11737                         if(e.expType)
11738                         {
11739                            FreeType(e.expType);
11740                            e.expType = null;
11741                         }
11742                      }
11743                   }
11744                   if(type)
11745                   {
11746                      typeStringBuf[0] = '\0';
11747                      PrintType(type, typeStringBuf, false, true);
11748                      typeString = typeStringBuf;
11749                      FreeType(type);
11750                   }
11751                }
11752                if(typeString)
11753                {
11754                   OldList * initializers = MkList();
11755                   Declarator decl;
11756                   OldList * specs = MkList();
11757                   if(arrayExp.list)
11758                   {
11759                      Expression e;
11760
11761                      builtinCount = arrayExp.list->count;
11762                      type = ProcessTypeString(typeString, false);
11763                      while(e = arrayExp.list->first)
11764                      {
11765                         arrayExp.list->Remove(e);
11766                         e.destType = type;
11767                         type.refCount++;
11768                         ProcessExpressionType(e);
11769                         ListAdd(initializers, MkInitializerAssignment(e));
11770                      }
11771                      FreeType(type);
11772                      delete arrayExp.list;
11773                   }
11774                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11775                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11776                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11777
11778                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11779                      PlugDeclarator(
11780                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11781                         ), MkInitializerList(initializers)))));
11782                   FreeList(exp, FreeExpression);
11783                }
11784                else
11785                {
11786                   arrayExp.expType = ProcessTypeString("Container", false);
11787                   Compiler_Error($"Couldn't determine type of array elements\n");
11788                }
11789
11790                /*
11791                Declarator decl;
11792                OldList * specs = MkList();
11793
11794                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11795                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11796                stmt.compound.declarations = MkListOne(
11797                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11798                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11799                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11800                      MkInitializerAssignment(MkExpBrackets(exp))))));
11801                */
11802             }
11803             else if(isLinkList && !isList)
11804             {
11805                Declarator decl;
11806                OldList * specs = MkList();
11807                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11808                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11809                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11810                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11811                      MkInitializerAssignment(MkExpBrackets(exp))))));
11812             }
11813             /*else if(isCustomAVLTree)
11814             {
11815                Declarator decl;
11816                OldList * specs = MkList();
11817                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11818                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11819                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11820                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11821                      MkInitializerAssignment(MkExpBrackets(exp))))));
11822             }*/
11823             else if(_class.templateArgs)
11824             {
11825                if(isMap)
11826                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11827                else
11828                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11829
11830                stmt.compound.declarations = MkListOne(
11831                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11832                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11833                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11834             }
11835             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11836
11837             if(block)
11838             {
11839                // Reparent sub-contexts in this statement
11840                switch(block.type)
11841                {
11842                   case compoundStmt:
11843                      if(block.compound.context)
11844                         block.compound.context.parent = stmt.compound.context;
11845                      break;
11846                   case ifStmt:
11847                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11848                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11849                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11850                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11851                      break;
11852                   case switchStmt:
11853                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11854                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11855                      break;
11856                   case whileStmt:
11857                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11858                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11859                      break;
11860                   case doWhileStmt:
11861                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11862                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11863                      break;
11864                   case forStmt:
11865                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11866                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11867                      break;
11868                   case forEachStmt:
11869                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11870                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11871                      break;
11872                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11873                   case labeledStmt:
11874                   case caseStmt
11875                   case expressionStmt:
11876                   case gotoStmt:
11877                   case continueStmt:
11878                   case breakStmt
11879                   case returnStmt:
11880                   case asmStmt:
11881                   case badDeclarationStmt:
11882                   case fireWatchersStmt:
11883                   case stopWatchingStmt:
11884                   case watchStmt:
11885                   */
11886                }
11887             }
11888             if(filter)
11889             {
11890                block = MkIfStmt(filter, block, null);
11891             }
11892             if(isArray)
11893             {
11894                stmt.compound.statements = MkListOne(MkForStmt(
11895                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11896                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11897                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11898                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11899                   block));
11900               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11901               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11902               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11903             }
11904             else if(isBuiltin)
11905             {
11906                char count[128];
11907                //OldList * specs = MkList();
11908                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11909
11910                sprintf(count, "%d", builtinCount);
11911
11912                stmt.compound.statements = MkListOne(MkForStmt(
11913                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11914                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11915                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11916                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11917                   block));
11918
11919                /*
11920                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11921                stmt.compound.statements = MkListOne(MkForStmt(
11922                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11923                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11924                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11925                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11926                   block));
11927               */
11928               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11929               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11930               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11931             }
11932             else if(isLinkList && !isList)
11933             {
11934                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11935                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11936                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11937                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11938                {
11939                   stmt.compound.statements = MkListOne(MkForStmt(
11940                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11941                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11942                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11943                      block));
11944                }
11945                else
11946                {
11947                   OldList * specs = MkList();
11948                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11949                   stmt.compound.statements = MkListOne(MkForStmt(
11950                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11951                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11952                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11953                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11954                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11955                      block));
11956                }
11957                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11958                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11959                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11960             }
11961             /*else if(isCustomAVLTree)
11962             {
11963                stmt.compound.statements = MkListOne(MkForStmt(
11964                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11965                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11966                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11967                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11968                   block));
11969
11970                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11971                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11972                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11973             }*/
11974             else
11975             {
11976                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11977                   MkIdentifier("Next")), null)), block));
11978             }
11979             ProcessExpressionType(expIt);
11980             if(stmt.compound.declarations->first)
11981                ProcessDeclaration(stmt.compound.declarations->first);
11982
11983             if(symbol)
11984                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11985
11986             ProcessStatement(stmt);
11987             curContext = stmt.compound.context.parent;
11988             break;
11989          }
11990          else
11991          {
11992             Compiler_Error($"Expression is not a container\n");
11993          }
11994          break;
11995       }
11996       case gotoStmt:
11997          break;
11998       case continueStmt:
11999          break;
12000       case breakStmt:
12001          break;
12002       case returnStmt:
12003       {
12004          Expression exp;
12005          if(stmt.expressions)
12006          {
12007             for(exp = stmt.expressions->first; exp; exp = exp.next)
12008             {
12009                if(!exp.next)
12010                {
12011                   if(curFunction && !curFunction.type)
12012                      curFunction.type = ProcessType(
12013                         curFunction.specifiers, curFunction.declarator);
12014                   FreeType(exp.destType);
12015                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12016                   if(exp.destType) exp.destType.refCount++;
12017                }
12018                ProcessExpressionType(exp);
12019             }
12020          }
12021          break;
12022       }
12023       case badDeclarationStmt:
12024       {
12025          ProcessDeclaration(stmt.decl);
12026          break;
12027       }
12028       case asmStmt:
12029       {
12030          AsmField field;
12031          if(stmt.asmStmt.inputFields)
12032          {
12033             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12034                if(field.expression)
12035                   ProcessExpressionType(field.expression);
12036          }
12037          if(stmt.asmStmt.outputFields)
12038          {
12039             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12040                if(field.expression)
12041                   ProcessExpressionType(field.expression);
12042          }
12043          if(stmt.asmStmt.clobberedFields)
12044          {
12045             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12046             {
12047                if(field.expression)
12048                   ProcessExpressionType(field.expression);
12049             }
12050          }
12051          break;
12052       }
12053       case watchStmt:
12054       {
12055          PropertyWatch propWatch;
12056          OldList * watches = stmt._watch.watches;
12057          Expression object = stmt._watch.object;
12058          Expression watcher = stmt._watch.watcher;
12059          if(watcher)
12060             ProcessExpressionType(watcher);
12061          if(object)
12062             ProcessExpressionType(object);
12063
12064          if(inCompiler)
12065          {
12066             if(watcher || thisClass)
12067             {
12068                External external = curExternal;
12069                Context context = curContext;
12070
12071                stmt.type = expressionStmt;
12072                stmt.expressions = MkList();
12073
12074                curExternal = external.prev;
12075
12076                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12077                {
12078                   ClassFunction func;
12079                   char watcherName[1024];
12080                   Class watcherClass = watcher ?
12081                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12082                   External createdExternal;
12083
12084                   // Create a declaration above
12085                   External externalDecl = MkExternalDeclaration(null);
12086                   ast->Insert(curExternal.prev, externalDecl);
12087
12088                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12089                   if(propWatch.deleteWatch)
12090                      strcat(watcherName, "_delete");
12091                   else
12092                   {
12093                      Identifier propID;
12094                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12095                      {
12096                         strcat(watcherName, "_");
12097                         strcat(watcherName, propID.string);
12098                      }
12099                   }
12100
12101                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12102                   {
12103                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12104                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12105                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12106                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12107                      ProcessClassFunctionBody(func, propWatch.compound);
12108                      propWatch.compound = null;
12109
12110                      //afterExternal = afterExternal ? afterExternal : curExternal;
12111
12112                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12113                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12114                      // TESTING THIS...
12115                      createdExternal.symbol.idCode = external.symbol.idCode;
12116
12117                      curExternal = createdExternal;
12118                      ProcessFunction(createdExternal.function);
12119
12120
12121                      // Create a declaration above
12122                      {
12123                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12124                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12125                         externalDecl.declaration = decl;
12126                         if(decl.symbol && !decl.symbol.pointerExternal)
12127                            decl.symbol.pointerExternal = externalDecl;
12128                      }
12129
12130                      if(propWatch.deleteWatch)
12131                      {
12132                         OldList * args = MkList();
12133                         ListAdd(args, CopyExpression(object));
12134                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12135                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12136                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12137                      }
12138                      else
12139                      {
12140                         Class _class = object.expType._class.registered;
12141                         Identifier propID;
12142
12143                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12144                         {
12145                            char propName[1024];
12146                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12147                            if(prop)
12148                            {
12149                               char getName[1024], setName[1024];
12150                               OldList * args = MkList();
12151
12152                               DeclareProperty(prop, setName, getName);
12153
12154                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12155                               strcpy(propName, "__ecereProp_");
12156                               FullClassNameCat(propName, prop._class.fullName, false);
12157                               strcat(propName, "_");
12158                               // strcat(propName, prop.name);
12159                               FullClassNameCat(propName, prop.name, true);
12160
12161                               ListAdd(args, CopyExpression(object));
12162                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12163                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12164                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12165
12166                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12167                            }
12168                            else
12169                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12170                         }
12171                      }
12172                   }
12173                   else
12174                      Compiler_Error($"Invalid watched object\n");
12175                }
12176
12177                curExternal = external;
12178                curContext = context;
12179
12180                if(watcher)
12181                   FreeExpression(watcher);
12182                if(object)
12183                   FreeExpression(object);
12184                FreeList(watches, FreePropertyWatch);
12185             }
12186             else
12187                Compiler_Error($"No observer specified and not inside a _class\n");
12188          }
12189          else
12190          {
12191             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12192             {
12193                ProcessStatement(propWatch.compound);
12194             }
12195
12196          }
12197          break;
12198       }
12199       case fireWatchersStmt:
12200       {
12201          OldList * watches = stmt._watch.watches;
12202          Expression object = stmt._watch.object;
12203          Class _class;
12204          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12205          // printf("%X\n", watches);
12206          // printf("%X\n", stmt._watch.watches);
12207          if(object)
12208             ProcessExpressionType(object);
12209
12210          if(inCompiler)
12211          {
12212             _class = object ?
12213                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12214
12215             if(_class)
12216             {
12217                Identifier propID;
12218
12219                stmt.type = expressionStmt;
12220                stmt.expressions = MkList();
12221
12222                // Check if we're inside a property set
12223                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12224                {
12225                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12226                }
12227                else if(!watches)
12228                {
12229                   //Compiler_Error($"No property specified and not inside a property set\n");
12230                }
12231                if(watches)
12232                {
12233                   for(propID = watches->first; propID; propID = propID.next)
12234                   {
12235                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12236                      if(prop)
12237                      {
12238                         CreateFireWatcher(prop, object, stmt);
12239                      }
12240                      else
12241                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12242                   }
12243                }
12244                else
12245                {
12246                   // Fire all properties!
12247                   Property prop;
12248                   Class base;
12249                   for(base = _class; base; base = base.base)
12250                   {
12251                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12252                      {
12253                         if(prop.isProperty && prop.isWatchable)
12254                         {
12255                            CreateFireWatcher(prop, object, stmt);
12256                         }
12257                      }
12258                   }
12259                }
12260
12261                if(object)
12262                   FreeExpression(object);
12263                FreeList(watches, FreeIdentifier);
12264             }
12265             else
12266                Compiler_Error($"Invalid object specified and not inside a class\n");
12267          }
12268          break;
12269       }
12270       case stopWatchingStmt:
12271       {
12272          OldList * watches = stmt._watch.watches;
12273          Expression object = stmt._watch.object;
12274          Expression watcher = stmt._watch.watcher;
12275          Class _class;
12276          if(object)
12277             ProcessExpressionType(object);
12278          if(watcher)
12279             ProcessExpressionType(watcher);
12280          if(inCompiler)
12281          {
12282             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12283
12284             if(watcher || thisClass)
12285             {
12286                if(_class)
12287                {
12288                   Identifier propID;
12289
12290                   stmt.type = expressionStmt;
12291                   stmt.expressions = MkList();
12292
12293                   if(!watches)
12294                   {
12295                      OldList * args;
12296                      // eInstance_StopWatching(object, null, watcher);
12297                      args = MkList();
12298                      ListAdd(args, CopyExpression(object));
12299                      ListAdd(args, MkExpConstant("0"));
12300                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12301                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12302                   }
12303                   else
12304                   {
12305                      for(propID = watches->first; propID; propID = propID.next)
12306                      {
12307                         char propName[1024];
12308                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12309                         if(prop)
12310                         {
12311                            char getName[1024], setName[1024];
12312                            OldList * args = MkList();
12313
12314                            DeclareProperty(prop, setName, getName);
12315
12316                            // eInstance_StopWatching(object, prop, watcher);
12317                            strcpy(propName, "__ecereProp_");
12318                            FullClassNameCat(propName, prop._class.fullName, false);
12319                            strcat(propName, "_");
12320                            // strcat(propName, prop.name);
12321                            FullClassNameCat(propName, prop.name, true);
12322                            MangleClassName(propName);
12323
12324                            ListAdd(args, CopyExpression(object));
12325                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12326                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12327                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12328                         }
12329                         else
12330                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12331                      }
12332                   }
12333
12334                   if(object)
12335                      FreeExpression(object);
12336                   if(watcher)
12337                      FreeExpression(watcher);
12338                   FreeList(watches, FreeIdentifier);
12339                }
12340                else
12341                   Compiler_Error($"Invalid object specified and not inside a class\n");
12342             }
12343             else
12344                Compiler_Error($"No observer specified and not inside a class\n");
12345          }
12346          break;
12347       }
12348    }
12349 }
12350
12351 static void ProcessFunction(FunctionDefinition function)
12352 {
12353    Identifier id = GetDeclId(function.declarator);
12354    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12355    Type type = symbol ? symbol.type : null;
12356    Class oldThisClass = thisClass;
12357    Context oldTopContext = topContext;
12358
12359    yylloc = function.loc;
12360    // Process thisClass
12361
12362    if(type && type.thisClass)
12363    {
12364       Symbol classSym = type.thisClass;
12365       Class _class = type.thisClass.registered;
12366       char className[1024];
12367       char structName[1024];
12368       Declarator funcDecl;
12369       Symbol thisSymbol;
12370
12371       bool typedObject = false;
12372
12373       if(_class && !_class.base)
12374       {
12375          _class = currentClass;
12376          if(_class && !_class.symbol)
12377             _class.symbol = FindClass(_class.fullName);
12378          classSym = _class ? _class.symbol : null;
12379          typedObject = true;
12380       }
12381
12382       thisClass = _class;
12383
12384       if(inCompiler && _class)
12385       {
12386          if(type.kind == functionType)
12387          {
12388             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12389             {
12390                //TypeName param = symbol.type.params.first;
12391                Type param = symbol.type.params.first;
12392                symbol.type.params.Remove(param);
12393                //FreeTypeName(param);
12394                FreeType(param);
12395             }
12396             if(type.classObjectType != classPointer)
12397             {
12398                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12399                symbol.type.staticMethod = true;
12400                symbol.type.thisClass = null;
12401
12402                // HIGH DANGER: VERIFYING THIS...
12403                symbol.type.extraParam = false;
12404             }
12405          }
12406
12407          strcpy(className, "__ecereClass_");
12408          FullClassNameCat(className, _class.fullName, true);
12409
12410          MangleClassName(className);
12411
12412          structName[0] = 0;
12413          FullClassNameCat(structName, _class.fullName, false);
12414
12415          // [class] this
12416
12417
12418          funcDecl = GetFuncDecl(function.declarator);
12419          if(funcDecl)
12420          {
12421             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12422             {
12423                TypeName param = funcDecl.function.parameters->first;
12424                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12425                {
12426                   funcDecl.function.parameters->Remove(param);
12427                   FreeTypeName(param);
12428                }
12429             }
12430
12431             // DANGER: Watch for this... Check if it's a Conversion?
12432             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12433
12434             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12435             if(!function.propertyNoThis)
12436             {
12437                TypeName thisParam;
12438
12439                if(type.classObjectType != classPointer)
12440                {
12441                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12442                   if(!funcDecl.function.parameters)
12443                      funcDecl.function.parameters = MkList();
12444                   funcDecl.function.parameters->Insert(null, thisParam);
12445                }
12446
12447                if(typedObject)
12448                {
12449                   if(type.classObjectType != classPointer)
12450                   {
12451                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12452                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12453                   }
12454
12455                   thisParam = TypeName
12456                   {
12457                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12458                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12459                   };
12460                   funcDecl.function.parameters->Insert(null, thisParam);
12461                }
12462             }
12463          }
12464
12465          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12466          {
12467             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12468             funcDecl = GetFuncDecl(initDecl.declarator);
12469             if(funcDecl)
12470             {
12471                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12472                {
12473                   TypeName param = funcDecl.function.parameters->first;
12474                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12475                   {
12476                      funcDecl.function.parameters->Remove(param);
12477                      FreeTypeName(param);
12478                   }
12479                }
12480
12481                if(type.classObjectType != classPointer)
12482                {
12483                   // DANGER: Watch for this... Check if it's a Conversion?
12484                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12485                   {
12486                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12487
12488                      if(!funcDecl.function.parameters)
12489                         funcDecl.function.parameters = MkList();
12490                      funcDecl.function.parameters->Insert(null, thisParam);
12491                   }
12492                }
12493             }
12494          }
12495       }
12496
12497       // Add this to the context
12498       if(function.body)
12499       {
12500          if(type.classObjectType != classPointer)
12501          {
12502             thisSymbol = Symbol
12503             {
12504                string = CopyString("this");
12505                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12506             };
12507             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12508
12509             if(typedObject && thisSymbol.type)
12510             {
12511                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12512                thisSymbol.type.byReference = type.byReference;
12513                thisSymbol.type.typedByReference = type.byReference;
12514                /*
12515                thisSymbol = Symbol { string = CopyString("class") };
12516                function.body.compound.context.symbols.Add(thisSymbol);
12517                */
12518             }
12519          }
12520       }
12521
12522       // Pointer to class data
12523
12524       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12525       {
12526          DataMember member = null;
12527          {
12528             Class base;
12529             for(base = _class; base && base.type != systemClass; base = base.next)
12530             {
12531                for(member = base.membersAndProperties.first; member; member = member.next)
12532                   if(!member.isProperty)
12533                      break;
12534                if(member)
12535                   break;
12536             }
12537          }
12538          for(member = _class.membersAndProperties.first; member; member = member.next)
12539             if(!member.isProperty)
12540                break;
12541          if(member)
12542          {
12543             char pointerName[1024];
12544
12545             Declaration decl;
12546             Initializer initializer;
12547             Expression exp, bytePtr;
12548
12549             strcpy(pointerName, "__ecerePointer_");
12550             FullClassNameCat(pointerName, _class.fullName, false);
12551             {
12552                char className[1024];
12553                strcpy(className, "__ecereClass_");
12554                FullClassNameCat(className, classSym.string, true);
12555                MangleClassName(className);
12556
12557                // Testing This
12558                DeclareClass(classSym, className);
12559             }
12560
12561             // ((byte *) this)
12562             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12563
12564             if(_class.fixed)
12565             {
12566                char string[256];
12567                sprintf(string, "%d", _class.offset);
12568                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12569             }
12570             else
12571             {
12572                // ([bytePtr] + [className]->offset)
12573                exp = QBrackets(MkExpOp(bytePtr, '+',
12574                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12575             }
12576
12577             // (this ? [exp] : 0)
12578             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12579             exp.expType = Type
12580             {
12581                refCount = 1;
12582                kind = pointerType;
12583                type = Type { refCount = 1, kind = voidType };
12584             };
12585
12586             if(function.body)
12587             {
12588                yylloc = function.body.loc;
12589                // ([structName] *) [exp]
12590                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12591                initializer = MkInitializerAssignment(
12592                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12593
12594                // [structName] * [pointerName] = [initializer];
12595                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12596
12597                {
12598                   Context prevContext = curContext;
12599                   curContext = function.body.compound.context;
12600
12601                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12602                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12603
12604                   curContext = prevContext;
12605                }
12606
12607                // WHY?
12608                decl.symbol = null;
12609
12610                if(!function.body.compound.declarations)
12611                   function.body.compound.declarations = MkList();
12612                function.body.compound.declarations->Insert(null, decl);
12613             }
12614          }
12615       }
12616
12617
12618       // Loop through the function and replace undeclared identifiers
12619       // which are a member of the class (methods, properties or data)
12620       // by "this.[member]"
12621    }
12622    else
12623       thisClass = null;
12624
12625    if(id)
12626    {
12627       FreeSpecifier(id._class);
12628       id._class = null;
12629
12630       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12631       {
12632          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12633          id = GetDeclId(initDecl.declarator);
12634
12635          FreeSpecifier(id._class);
12636          id._class = null;
12637       }
12638    }
12639    if(function.body)
12640       topContext = function.body.compound.context;
12641    {
12642       FunctionDefinition oldFunction = curFunction;
12643       curFunction = function;
12644       if(function.body)
12645          ProcessStatement(function.body);
12646
12647       // If this is a property set and no firewatchers has been done yet, add one here
12648       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12649       {
12650          Statement prevCompound = curCompound;
12651          Context prevContext = curContext;
12652
12653          Statement fireWatchers = MkFireWatchersStmt(null, null);
12654          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12655          ListAdd(function.body.compound.statements, fireWatchers);
12656
12657          curCompound = function.body;
12658          curContext = function.body.compound.context;
12659
12660          ProcessStatement(fireWatchers);
12661
12662          curContext = prevContext;
12663          curCompound = prevCompound;
12664
12665       }
12666
12667       curFunction = oldFunction;
12668    }
12669
12670    if(function.declarator)
12671    {
12672       ProcessDeclarator(function.declarator);
12673    }
12674
12675    topContext = oldTopContext;
12676    thisClass = oldThisClass;
12677 }
12678
12679 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12680 static void ProcessClass(OldList definitions, Symbol symbol)
12681 {
12682    ClassDef def;
12683    External external = curExternal;
12684    Class regClass = symbol ? symbol.registered : null;
12685
12686    // Process all functions
12687    for(def = definitions.first; def; def = def.next)
12688    {
12689       if(def.type == functionClassDef)
12690       {
12691          if(def.function.declarator)
12692             curExternal = def.function.declarator.symbol.pointerExternal;
12693          else
12694             curExternal = external;
12695
12696          ProcessFunction((FunctionDefinition)def.function);
12697       }
12698       else if(def.type == declarationClassDef)
12699       {
12700          if(def.decl.type == instDeclaration)
12701          {
12702             thisClass = regClass;
12703             ProcessInstantiationType(def.decl.inst);
12704             thisClass = null;
12705          }
12706          // Testing this
12707          else
12708          {
12709             Class backThisClass = thisClass;
12710             if(regClass) thisClass = regClass;
12711             ProcessDeclaration(def.decl);
12712             thisClass = backThisClass;
12713          }
12714       }
12715       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12716       {
12717          MemberInit defProperty;
12718
12719          // Add this to the context
12720          Symbol thisSymbol = Symbol
12721          {
12722             string = CopyString("this");
12723             type = regClass ? MkClassType(regClass.fullName) : null;
12724          };
12725          globalContext.symbols.Add((BTNode)thisSymbol);
12726
12727          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12728          {
12729             thisClass = regClass;
12730             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12731             thisClass = null;
12732          }
12733
12734          globalContext.symbols.Remove((BTNode)thisSymbol);
12735          FreeSymbol(thisSymbol);
12736       }
12737       else if(def.type == propertyClassDef && def.propertyDef)
12738       {
12739          PropertyDef prop = def.propertyDef;
12740
12741          // Add this to the context
12742          /*
12743          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12744          globalContext.symbols.Add(thisSymbol);
12745          */
12746
12747          thisClass = regClass;
12748          if(prop.setStmt)
12749          {
12750             if(regClass)
12751             {
12752                Symbol thisSymbol
12753                {
12754                   string = CopyString("this");
12755                   type = MkClassType(regClass.fullName);
12756                };
12757                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12758             }
12759
12760             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12761             ProcessStatement(prop.setStmt);
12762          }
12763          if(prop.getStmt)
12764          {
12765             if(regClass)
12766             {
12767                Symbol thisSymbol
12768                {
12769                   string = CopyString("this");
12770                   type = MkClassType(regClass.fullName);
12771                };
12772                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12773             }
12774
12775             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12776             ProcessStatement(prop.getStmt);
12777          }
12778          if(prop.issetStmt)
12779          {
12780             if(regClass)
12781             {
12782                Symbol thisSymbol
12783                {
12784                   string = CopyString("this");
12785                   type = MkClassType(regClass.fullName);
12786                };
12787                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12788             }
12789
12790             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12791             ProcessStatement(prop.issetStmt);
12792          }
12793
12794          thisClass = null;
12795
12796          /*
12797          globalContext.symbols.Remove(thisSymbol);
12798          FreeSymbol(thisSymbol);
12799          */
12800       }
12801       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12802       {
12803          PropertyWatch propertyWatch = def.propertyWatch;
12804
12805          thisClass = regClass;
12806          if(propertyWatch.compound)
12807          {
12808             Symbol thisSymbol
12809             {
12810                string = CopyString("this");
12811                type = regClass ? MkClassType(regClass.fullName) : null;
12812             };
12813
12814             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12815
12816             curExternal = null;
12817             ProcessStatement(propertyWatch.compound);
12818          }
12819          thisClass = null;
12820       }
12821    }
12822 }
12823
12824 void DeclareFunctionUtil(String s)
12825 {
12826    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12827    if(function)
12828    {
12829       char name[1024];
12830       name[0] = 0;
12831       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12832          strcpy(name, "__ecereFunction_");
12833       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12834       DeclareFunction(function, name);
12835    }
12836 }
12837
12838 void ComputeDataTypes()
12839 {
12840    External external;
12841    External temp { };
12842    External after = null;
12843
12844    currentClass = null;
12845
12846    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12847
12848    for(external = ast->first; external; external = external.next)
12849    {
12850       if(external.type == declarationExternal)
12851       {
12852          Declaration decl = external.declaration;
12853          if(decl)
12854          {
12855             OldList * decls = decl.declarators;
12856             if(decls)
12857             {
12858                InitDeclarator initDecl = decls->first;
12859                if(initDecl)
12860                {
12861                   Declarator declarator = initDecl.declarator;
12862                   if(declarator && declarator.type == identifierDeclarator)
12863                   {
12864                      Identifier id = declarator.identifier;
12865                      if(id && id.string)
12866                      {
12867                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12868                         {
12869                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12870                            after = external;
12871                         }
12872                      }
12873                   }
12874                }
12875             }
12876          }
12877        }
12878    }
12879
12880    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12881    ast->Insert(after, temp);
12882    curExternal = temp;
12883
12884    DeclareFunctionUtil("eSystem_New");
12885    DeclareFunctionUtil("eSystem_New0");
12886    DeclareFunctionUtil("eSystem_Renew");
12887    DeclareFunctionUtil("eSystem_Renew0");
12888    DeclareFunctionUtil("eSystem_Delete");
12889    DeclareFunctionUtil("eClass_GetProperty");
12890    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12891
12892    DeclareStruct("ecere::com::Class", false);
12893    DeclareStruct("ecere::com::Instance", false);
12894    DeclareStruct("ecere::com::Property", false);
12895    DeclareStruct("ecere::com::DataMember", false);
12896    DeclareStruct("ecere::com::Method", false);
12897    DeclareStruct("ecere::com::SerialBuffer", false);
12898    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12899
12900    ast->Remove(temp);
12901
12902    for(external = ast->first; external; external = external.next)
12903    {
12904       afterExternal = curExternal = external;
12905       if(external.type == functionExternal)
12906       {
12907          currentClass = external.function._class;
12908          ProcessFunction(external.function);
12909       }
12910       // There shouldn't be any _class member access here anyways...
12911       else if(external.type == declarationExternal)
12912       {
12913          currentClass = null;
12914          if(external.declaration)
12915             ProcessDeclaration(external.declaration);
12916       }
12917       else if(external.type == classExternal)
12918       {
12919          ClassDefinition _class = external._class;
12920          currentClass = external.symbol.registered;
12921          if(_class.definitions)
12922          {
12923             ProcessClass(_class.definitions, _class.symbol);
12924          }
12925          if(inCompiler)
12926          {
12927             // Free class data...
12928             ast->Remove(external);
12929             delete external;
12930          }
12931       }
12932       else if(external.type == nameSpaceExternal)
12933       {
12934          thisNameSpace = external.id.string;
12935       }
12936    }
12937    currentClass = null;
12938    thisNameSpace = null;
12939    curExternal = null;
12940
12941    delete temp.symbol;
12942    delete temp;
12943 }