Fixed many warnings
[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 unionMemberOffset = 0;
410       int bitFields = 0;
411
412       /*
413       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
414          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
415       */
416
417       if(member)
418       {
419          member.memberOffset = 0;
420          if(targetBits < sizeof(void *) * 8)
421             member.structAlignment = 0;
422       }
423       else if(targetBits < sizeof(void *) * 8)
424          _class.structAlignment = 0;
425
426       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
427       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
428          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
429
430       if(!member && _class.destructionWatchOffset)
431          _class.memberOffset += sizeof(OldList);
432
433       // To avoid reentrancy...
434       //_class.structSize = -1;
435
436       {
437          DataMember dataMember;
438          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
439          {
440             if(!dataMember.isProperty)
441             {
442                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
443                {
444                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
445                   /*if(!dataMember.dataType)
446                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
447                      */
448                }
449             }
450          }
451       }
452
453       {
454          DataMember dataMember;
455          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
456          {
457             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
458             {
459                if(!isMember && _class.type == bitClass && dataMember.dataType)
460                {
461                   BitMember bitMember = (BitMember) dataMember;
462                   uint64 mask = 0;
463                   int d;
464
465                   ComputeTypeSize(dataMember.dataType);
466
467                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
468                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
469
470                   _class.memberOffset = bitMember.pos + bitMember.size;
471                   for(d = 0; d<bitMember.size; d++)
472                   {
473                      if(d)
474                         mask <<= 1;
475                      mask |= 1;
476                   }
477                   bitMember.mask = mask << bitMember.pos;
478                }
479                else if(dataMember.type == normalMember && dataMember.dataType)
480                {
481                   int size;
482                   int alignment = 0;
483
484                   // Prevent infinite recursion
485                   if(dataMember.dataType.kind != classType ||
486                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
487                      _class.type != structClass)))
488                      ComputeTypeSize(dataMember.dataType);
489
490                   if(dataMember.dataType.bitFieldCount)
491                   {
492                      bitFields += dataMember.dataType.bitFieldCount;
493                      size = 0;
494                   }
495                   else
496                   {
497                      if(bitFields)
498                      {
499                         int size = (bitFields + 7) / 8;
500
501                         if(isMember)
502                         {
503                            // TESTING THIS PADDING CODE
504                            if(alignment)
505                            {
506                               member.structAlignment = Max(member.structAlignment, alignment);
507
508                               if(member.memberOffset % alignment)
509                                  member.memberOffset += alignment - (member.memberOffset % alignment);
510                            }
511
512                            dataMember.offset = member.memberOffset;
513                            if(member.type == unionMember)
514                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
515                            else
516                            {
517                               member.memberOffset += size;
518                            }
519                         }
520                         else
521                         {
522                            // TESTING THIS PADDING CODE
523                            if(alignment)
524                            {
525                               _class.structAlignment = Max(_class.structAlignment, alignment);
526
527                               if(_class.memberOffset % alignment)
528                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
529                            }
530
531                            dataMember.offset = _class.memberOffset;
532                            _class.memberOffset += size;
533                         }
534                         bitFields = 0;
535                      }
536                      size = dataMember.dataType.size;
537                      alignment = dataMember.dataType.alignment;
538                   }
539
540                   if(isMember)
541                   {
542                      // TESTING THIS PADDING CODE
543                      if(alignment)
544                      {
545                         member.structAlignment = Max(member.structAlignment, alignment);
546
547                         if(member.memberOffset % alignment)
548                            member.memberOffset += alignment - (member.memberOffset % alignment);
549                      }
550
551                      dataMember.offset = member.memberOffset;
552                      if(member.type == unionMember)
553                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
554                      else
555                      {
556                         member.memberOffset += size;
557                      }
558                   }
559                   else
560                   {
561                      // TESTING THIS PADDING CODE
562                      if(alignment)
563                      {
564                         _class.structAlignment = Max(_class.structAlignment, alignment);
565
566                         if(_class.memberOffset % alignment)
567                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
568                      }
569
570                      dataMember.offset = _class.memberOffset;
571                      _class.memberOffset += size;
572                   }
573                }
574                else
575                {
576                   int alignment;
577
578                   ComputeClassMembers((Class)dataMember, true);
579                   alignment = dataMember.structAlignment;
580
581                   if(isMember)
582                   {
583                      if(alignment)
584                      {
585                         if(member.memberOffset % alignment)
586                            member.memberOffset += alignment - (member.memberOffset % alignment);
587
588                         member.structAlignment = Max(member.structAlignment, alignment);
589                      }
590                      dataMember.offset = member.memberOffset;
591                      if(member.type == unionMember)
592                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
593                      else
594                         member.memberOffset += dataMember.memberOffset;
595                   }
596                   else
597                   {
598                      if(alignment)
599                      {
600                         if(_class.memberOffset % alignment)
601                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
602                         _class.structAlignment = Max(_class.structAlignment, alignment);
603                      }
604                      dataMember.offset = _class.memberOffset;
605                      _class.memberOffset += dataMember.memberOffset;
606                   }
607                }
608             }
609          }
610          if(bitFields)
611          {
612             int alignment = 0;
613             int size = (bitFields + 7) / 8;
614
615             if(isMember)
616             {
617                // TESTING THIS PADDING CODE
618                if(alignment)
619                {
620                   member.structAlignment = Max(member.structAlignment, alignment);
621
622                   if(member.memberOffset % alignment)
623                      member.memberOffset += alignment - (member.memberOffset % alignment);
624                }
625
626                if(member.type == unionMember)
627                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
628                else
629                {
630                   member.memberOffset += size;
631                }
632             }
633             else
634             {
635                // TESTING THIS PADDING CODE
636                if(alignment)
637                {
638                   _class.structAlignment = Max(_class.structAlignment, alignment);
639
640                   if(_class.memberOffset % alignment)
641                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
642                }
643                _class.memberOffset += size;
644             }
645             bitFields = 0;
646          }
647       }
648       if(member && member.type == unionMember)
649       {
650          member.memberOffset = unionMemberOffset;
651       }
652
653       if(!isMember)
654       {
655          /*if(_class.type == structClass)
656             _class.size = _class.memberOffset;
657          else
658          */
659
660          if(_class.type != bitClass)
661          {
662             int extra = 0;
663             if(_class.structAlignment)
664             {
665                if(_class.memberOffset % _class.structAlignment)
666                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
667             }
668             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
669             if(!member)
670             {
671                Property prop;
672                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
673                {
674                   if(prop.isProperty && prop.isWatchable)
675                   {
676                      prop.watcherOffset = _class.structSize;
677                      _class.structSize += sizeof(OldList);
678                   }
679                }
680             }
681
682             // Fix Derivatives
683             {
684                OldLink derivative;
685                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
686                {
687                   Class deriv = derivative.data;
688
689                   if(deriv.computeSize)
690                   {
691                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
692                      deriv.offset = /*_class.offset + */_class.structSize;
693                      deriv.memberOffset = 0;
694                      // ----------------------
695
696                      deriv.structSize = deriv.offset;
697
698                      ComputeClassMembers(deriv, false);
699                   }
700                }
701             }
702          }
703       }
704    }
705    if(context)
706       FinishTemplatesContext(context);
707 }
708
709 public void ComputeModuleClasses(Module module)
710 {
711    Class _class;
712    OldLink subModule;
713
714    for(subModule = module.modules.first; subModule; subModule = subModule.next)
715       ComputeModuleClasses(subModule.data);
716    for(_class = module.classes.first; _class; _class = _class.next)
717       ComputeClassMembers(_class, false);
718 }
719
720
721 public int ComputeTypeSize(Type type)
722 {
723    uint size = type ? type.size : 0;
724    if(!size && type && !type.computing)
725    {
726       type.computing = true;
727       switch(type.kind)
728       {
729          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
730          case charType: type.alignment = size = sizeof(char); break;
731          case intType: type.alignment = size = sizeof(int); break;
732          case int64Type: type.alignment = size = sizeof(int64); break;
733          case intPtrType: type.alignment = size = targetBits / 8; break;
734          case intSizeType: type.alignment = size = targetBits / 8; break;
735          case longType: type.alignment = size = sizeof(long); break;
736          case shortType: type.alignment = size = sizeof(short); break;
737          case floatType: type.alignment = size = sizeof(float); break;
738          case doubleType: type.alignment = size = sizeof(double); break;
739          case classType:
740          {
741             Class _class = type._class ? type._class.registered : null;
742
743             if(_class && _class.type == structClass)
744             {
745                // Ensure all members are properly registered
746                ComputeClassMembers(_class, false);
747                type.alignment = _class.structAlignment;
748                size = _class.structSize;
749                if(type.alignment && size % type.alignment)
750                   size += type.alignment - (size % type.alignment);
751
752             }
753             else if(_class && (_class.type == unitClass ||
754                    _class.type == enumClass ||
755                    _class.type == bitClass))
756             {
757                if(!_class.dataType)
758                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
759                size = type.alignment = ComputeTypeSize(_class.dataType);
760             }
761             else
762                size = type.alignment = targetBits / 8; // sizeof(Instance *);
763             break;
764          }
765          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
766          case arrayType:
767             if(type.arraySizeExp)
768             {
769                ProcessExpressionType(type.arraySizeExp);
770                ComputeExpression(type.arraySizeExp);
771                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
772                   type.arraySizeExp.expType.kind != shortType &&
773                   type.arraySizeExp.expType.kind != charType &&
774                   type.arraySizeExp.expType.kind != longType &&
775                   type.arraySizeExp.expType.kind != int64Type &&
776                   type.arraySizeExp.expType.kind != intSizeType &&
777                   type.arraySizeExp.expType.kind != intPtrType &&
778                   type.arraySizeExp.expType.kind != enumType &&
779                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
780                {
781                   Location oldLoc = yylloc;
782                   // bool isConstant = type.arraySizeExp.isConstant;
783                   char expression[10240];
784                   expression[0] = '\0';
785                   type.arraySizeExp.expType = null;
786                   yylloc = type.arraySizeExp.loc;
787                   if(inCompiler)
788                      PrintExpression(type.arraySizeExp, expression);
789                   Compiler_Error($"Array size not constant int (%s)\n", expression);
790                   yylloc = oldLoc;
791                }
792                GetInt(type.arraySizeExp, &type.arraySize);
793             }
794             else if(type.enumClass)
795             {
796                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
797                {
798                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
799                }
800                else
801                   type.arraySize = 0;
802             }
803             else
804             {
805                // Unimplemented auto size
806                type.arraySize = 0;
807             }
808
809             size = ComputeTypeSize(type.type) * type.arraySize;
810             if(type.type)
811                type.alignment = type.type.alignment;
812
813             break;
814          case structType:
815          {
816             Type member;
817             for(member = type.members.first; member; member = member.next)
818             {
819                uint addSize = ComputeTypeSize(member);
820
821                member.offset = size;
822                if(member.alignment && size % member.alignment)
823                   member.offset += member.alignment - (size % member.alignment);
824                size = member.offset;
825
826                type.alignment = Max(type.alignment, member.alignment);
827                size += addSize;
828             }
829             if(type.alignment && size % type.alignment)
830                size += type.alignment - (size % type.alignment);
831             break;
832          }
833          case unionType:
834          {
835             Type member;
836             for(member = type.members.first; member; member = member.next)
837             {
838                uint addSize = ComputeTypeSize(member);
839
840                member.offset = size;
841                if(member.alignment && size % member.alignment)
842                   member.offset += member.alignment - (size % member.alignment);
843                size = member.offset;
844
845                type.alignment = Max(type.alignment, member.alignment);
846                size = Max(size, addSize);
847             }
848             if(type.alignment && size % type.alignment)
849                size += type.alignment - (size % type.alignment);
850             break;
851          }
852          case templateType:
853          {
854             TemplateParameter param = type.templateParameter;
855             Type baseType = ProcessTemplateParameterType(param);
856             if(baseType)
857             {
858                size = ComputeTypeSize(baseType);
859                type.alignment = baseType.alignment;
860             }
861             else
862                type.alignment = size = sizeof(uint64);
863             break;
864          }
865          case enumType:
866          {
867             type.alignment = size = sizeof(enum { test });
868             break;
869          }
870          case thisClassType:
871          {
872             type.alignment = size = targetBits / 8; //sizeof(void *);
873             break;
874          }
875       }
876       type.size = size;
877       type.computing = false;
878    }
879    return size;
880 }
881
882
883 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
884 {
885    // This function is in need of a major review when implementing private members etc.
886    DataMember topMember = isMember ? (DataMember) _class : null;
887    uint totalSize = 0;
888    uint maxSize = 0;
889    int alignment;
890    uint size;
891    DataMember member;
892    Context context = isMember ? null : SetupTemplatesContext(_class);
893    if(addedPadding)
894       *addedPadding = false;
895
896    if(!isMember && _class.base)
897    {
898       maxSize = _class.structSize;
899       //if(_class.base.type != systemClass) // Commented out with new Instance _class
900       {
901          // DANGER: Testing this noHeadClass here...
902          if(_class.type == structClass || _class.type == noHeadClass)
903             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
904          else
905          {
906             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
907             if(maxSize > baseSize)
908                maxSize -= baseSize;
909             else
910                maxSize = 0;
911          }
912       }
913    }
914
915    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
916    {
917       if(!member.isProperty)
918       {
919          switch(member.type)
920          {
921             case normalMember:
922             {
923                if(member.dataTypeString)
924                {
925                   OldList * specs = MkList(), * decls = MkList();
926                   Declarator decl;
927
928                   decl = SpecDeclFromString(member.dataTypeString, specs,
929                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
930                   ListAdd(decls, MkStructDeclarator(decl, null));
931                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
932
933                   if(!member.dataType)
934                      member.dataType = ProcessType(specs, decl);
935
936                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
937
938                   {
939                      Type type = ProcessType(specs, decl);
940                      DeclareType(member.dataType, false, false);
941                      FreeType(type);
942                   }
943                   /*
944                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
945                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
946                      DeclareStruct(member.dataType._class.string, false);
947                   */
948
949                   ComputeTypeSize(member.dataType);
950                   size = member.dataType.size;
951                   alignment = member.dataType.alignment;
952
953                   if(alignment)
954                   {
955                      if(totalSize % alignment)
956                         totalSize += alignment - (totalSize % alignment);
957                   }
958                   totalSize += size;
959                }
960                break;
961             }
962             case unionMember:
963             case structMember:
964             {
965                OldList * specs = MkList(), * list = MkList();
966
967                size = 0;
968                AddMembers(list, (Class)member, true, &size, topClass, null);
969                ListAdd(specs,
970                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
971                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
972                alignment = member.structAlignment;
973
974                if(alignment)
975                {
976                   if(totalSize % alignment)
977                      totalSize += alignment - (totalSize % alignment);
978                }
979                totalSize += size;
980                break;
981             }
982          }
983       }
984    }
985    if(retSize)
986    {
987       if(topMember && topMember.type == unionMember)
988          *retSize = Max(*retSize, totalSize);
989       else
990          *retSize += totalSize;
991    }
992    else if(totalSize < maxSize && _class.type != systemClass)
993    {
994       int autoPadding = 0;
995       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
996          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
997       if(totalSize + autoPadding < maxSize)
998       {
999          char sizeString[50];
1000          sprintf(sizeString, "%d", maxSize - totalSize);
1001          ListAdd(declarations,
1002             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
1003             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1004          if(addedPadding)
1005             *addedPadding = true;
1006       }
1007    }
1008    if(context)
1009       FinishTemplatesContext(context);
1010    return topMember ? topMember.memberID : _class.memberID;
1011 }
1012
1013 static int DeclareMembers(Class _class, bool isMember)
1014 {
1015    DataMember topMember = isMember ? (DataMember) _class : null;
1016    DataMember member;
1017    Context context = isMember ? null : SetupTemplatesContext(_class);
1018
1019    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1020       DeclareMembers(_class.base, false);
1021
1022    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1023    {
1024       if(!member.isProperty)
1025       {
1026          switch(member.type)
1027          {
1028             case normalMember:
1029             {
1030                /*
1031                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1032                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1033                   DeclareStruct(member.dataType._class.string, false);
1034                   */
1035                if(!member.dataType && member.dataTypeString)
1036                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1037                if(member.dataType)
1038                   DeclareType(member.dataType, false, false);
1039                break;
1040             }
1041             case unionMember:
1042             case structMember:
1043             {
1044                DeclareMembers((Class)member, true);
1045                break;
1046             }
1047          }
1048       }
1049    }
1050    if(context)
1051       FinishTemplatesContext(context);
1052
1053    return topMember ? topMember.memberID : _class.memberID;
1054 }
1055
1056 void DeclareStruct(char * name, bool skipNoHead)
1057 {
1058    External external = null;
1059    Symbol classSym = FindClass(name);
1060
1061    if(!inCompiler || !classSym) return;
1062
1063    // We don't need any declaration for bit classes...
1064    if(classSym.registered &&
1065       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1066       return;
1067
1068    /*if(classSym.registered.templateClass)
1069       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1070    */
1071
1072    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1073    {
1074       // Add typedef struct
1075       Declaration decl;
1076       OldList * specifiers, * declarators;
1077       OldList * declarations = null;
1078       char structName[1024];
1079       Specifier spec = null;
1080       external = (classSym.registered && classSym.registered.type == structClass) ?
1081          classSym.pointerExternal : classSym.structExternal;
1082
1083       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1084       // Moved this one up because DeclareClass done later will need it
1085
1086       classSym.declaring++;
1087
1088       if(strchr(classSym.string, '<'))
1089       {
1090          if(classSym.registered.templateClass)
1091          {
1092             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1093             classSym.declaring--;
1094          }
1095          return;
1096       }
1097
1098       //if(!skipNoHead)
1099          DeclareMembers(classSym.registered, false);
1100
1101       structName[0] = 0;
1102       FullClassNameCat(structName, name, false);
1103
1104       if(external && external.declaration && external.declaration.specifiers)
1105       {
1106          for(spec = external.declaration.specifiers->first; spec; spec = spec.next)
1107          {
1108             if(spec.type == structSpecifier || spec.type == unionSpecifier)
1109                break;
1110          }
1111       }
1112
1113       /*if(!external)
1114          external = MkExternalDeclaration(null);*/
1115
1116       if(!skipNoHead && (!spec || !spec.definitions))
1117       {
1118          bool addedPadding = false;
1119          classSym.declaredStructSym = true;
1120
1121          declarations = MkList();
1122
1123          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1124
1125          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1126          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1127
1128          if(!declarations->count || (declarations->count == 1 && addedPadding))
1129          {
1130             FreeList(declarations, FreeClassDef);
1131             declarations = null;
1132          }
1133       }
1134       if(skipNoHead || declarations)
1135       {
1136          if(spec)
1137          {
1138             if(declarations)
1139                spec.definitions = declarations;
1140
1141             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1142             {
1143                // TODO: Fix this
1144                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1145
1146                // DANGER
1147                if(classSym.structExternal)
1148                   ast->Move(classSym.structExternal, curExternal.prev);
1149                ast->Move(classSym.pointerExternal, curExternal.prev);
1150
1151                classSym.id = curExternal.symbol.idCode;
1152                classSym.idCode = curExternal.symbol.idCode;
1153                // external = classSym.pointerExternal;
1154                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1155             }
1156          }
1157          else
1158          {
1159             if(!external)
1160                external = MkExternalDeclaration(null);
1161
1162             specifiers = MkList();
1163             declarators = MkList();
1164             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1165
1166             /*
1167             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1168             ListAdd(declarators, MkInitDeclarator(d, null));
1169             */
1170             external.declaration = decl = MkDeclaration(specifiers, declarators);
1171             if(decl.symbol && !decl.symbol.pointerExternal)
1172                decl.symbol.pointerExternal = external;
1173
1174             // For simple classes, keep the declaration as the external to move around
1175             if(classSym.registered && classSym.registered.type == structClass)
1176             {
1177                char className[1024];
1178                strcpy(className, "__ecereClass_");
1179                FullClassNameCat(className, classSym.string, true);
1180                MangleClassName(className);
1181
1182                // Testing This
1183                DeclareClass(classSym, className);
1184
1185                external.symbol = classSym;
1186                classSym.pointerExternal = external;
1187                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1188                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1189             }
1190             else
1191             {
1192                char className[1024];
1193                strcpy(className, "__ecereClass_");
1194                FullClassNameCat(className, classSym.string, true);
1195                MangleClassName(className);
1196
1197                // TOFIX: TESTING THIS...
1198                classSym.structExternal = external;
1199                DeclareClass(classSym, className);
1200                external.symbol = classSym;
1201             }
1202
1203             //if(curExternal)
1204                ast->Insert(curExternal ? curExternal.prev : null, external);
1205          }
1206       }
1207
1208       classSym.declaring--;
1209    }
1210    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1211    {
1212       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1213       // Moved this one up because DeclareClass done later will need it
1214
1215       // TESTING THIS:
1216       classSym.declaring++;
1217
1218       //if(!skipNoHead)
1219       {
1220          if(classSym.registered)
1221             DeclareMembers(classSym.registered, false);
1222       }
1223
1224       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1225       {
1226          // TODO: Fix this
1227          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1228
1229          // DANGER
1230          if(classSym.structExternal)
1231             ast->Move(classSym.structExternal, curExternal.prev);
1232          ast->Move(classSym.pointerExternal, curExternal.prev);
1233
1234          classSym.id = curExternal.symbol.idCode;
1235          classSym.idCode = curExternal.symbol.idCode;
1236          // external = classSym.pointerExternal;
1237          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1238       }
1239
1240       classSym.declaring--;
1241    }
1242    //return external;
1243 }
1244
1245 void DeclareProperty(Property prop, char * setName, char * getName)
1246 {
1247    Symbol symbol = prop.symbol;
1248    char propName[1024];
1249
1250    strcpy(setName, "__ecereProp_");
1251    FullClassNameCat(setName, prop._class.fullName, false);
1252    strcat(setName, "_Set_");
1253    // strcat(setName, prop.name);
1254    FullClassNameCat(setName, prop.name, true);
1255
1256    strcpy(getName, "__ecereProp_");
1257    FullClassNameCat(getName, prop._class.fullName, false);
1258    strcat(getName, "_Get_");
1259    FullClassNameCat(getName, prop.name, true);
1260    // strcat(getName, prop.name);
1261
1262    strcpy(propName, "__ecereProp_");
1263    FullClassNameCat(propName, prop._class.fullName, false);
1264    strcat(propName, "_");
1265    FullClassNameCat(propName, prop.name, true);
1266    // strcat(propName, prop.name);
1267
1268    // To support "char *" property
1269    MangleClassName(getName);
1270    MangleClassName(setName);
1271    MangleClassName(propName);
1272
1273    if(prop._class.type == structClass)
1274       DeclareStruct(prop._class.fullName, false);
1275
1276    if(!symbol || curExternal.symbol.idCode < symbol.id)
1277    {
1278       bool imported = false;
1279       bool dllImport = false;
1280       if(!symbol || symbol._import)
1281       {
1282          if(!symbol)
1283          {
1284             Symbol classSym;
1285             if(!prop._class.symbol)
1286                prop._class.symbol = FindClass(prop._class.fullName);
1287             classSym = prop._class.symbol;
1288             if(classSym && !classSym._import)
1289             {
1290                ModuleImport module;
1291
1292                if(prop._class.module)
1293                   module = FindModule(prop._class.module);
1294                else
1295                   module = mainModule;
1296
1297                classSym._import = ClassImport
1298                {
1299                   name = CopyString(prop._class.fullName);
1300                   isRemote = prop._class.isRemote;
1301                };
1302                module.classes.Add(classSym._import);
1303             }
1304             symbol = prop.symbol = Symbol { };
1305             symbol._import = (ClassImport)PropertyImport
1306             {
1307                name = CopyString(prop.name);
1308                isVirtual = false; //prop.isVirtual;
1309                hasSet = prop.Set ? true : false;
1310                hasGet = prop.Get ? true : false;
1311             };
1312             if(classSym)
1313                classSym._import.properties.Add(symbol._import);
1314          }
1315          imported = true;
1316          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1317          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1318             prop._class.module.importType != staticImport)
1319             dllImport = true;
1320       }
1321
1322       if(!symbol.type)
1323       {
1324          Context context = SetupTemplatesContext(prop._class);
1325          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1326          FinishTemplatesContext(context);
1327       }
1328
1329       // Get
1330       if(prop.Get)
1331       {
1332          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1333          {
1334             Declaration decl;
1335             OldList * specifiers, * declarators;
1336             Declarator d;
1337             OldList * params;
1338             Specifier spec;
1339             External external;
1340             Declarator typeDecl;
1341             bool simple = false;
1342
1343             specifiers = MkList();
1344             declarators = MkList();
1345             params = MkList();
1346
1347             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1348                MkDeclaratorIdentifier(MkIdentifier("this"))));
1349
1350             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1351             //if(imported)
1352             if(dllImport)
1353                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1354
1355             {
1356                Context context = SetupTemplatesContext(prop._class);
1357                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1358                FinishTemplatesContext(context);
1359             }
1360
1361             // Make sure the simple _class's type is declared
1362             for(spec = specifiers->first; spec; spec = spec.next)
1363             {
1364                if(spec.type == nameSpecifier /*SpecifierClass*/)
1365                {
1366                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1367                   {
1368                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1369                      symbol._class = classSym.registered;
1370                      if(classSym.registered && classSym.registered.type == structClass)
1371                      {
1372                         DeclareStruct(spec.name, false);
1373                         simple = true;
1374                      }
1375                   }
1376                }
1377             }
1378
1379             if(!simple)
1380                d = PlugDeclarator(typeDecl, d);
1381             else
1382             {
1383                ListAdd(params, MkTypeName(specifiers,
1384                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1385                specifiers = MkList();
1386             }
1387
1388             d = MkDeclaratorFunction(d, params);
1389
1390             //if(imported)
1391             if(dllImport)
1392                specifiers->Insert(null, MkSpecifier(EXTERN));
1393             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1394                specifiers->Insert(null, MkSpecifier(STATIC));
1395             if(simple)
1396                ListAdd(specifiers, MkSpecifier(VOID));
1397
1398             ListAdd(declarators, MkInitDeclarator(d, null));
1399
1400             decl = MkDeclaration(specifiers, declarators);
1401
1402             external = MkExternalDeclaration(decl);
1403             ast->Insert(curExternal.prev, external);
1404             external.symbol = symbol;
1405             symbol.externalGet = external;
1406
1407             ReplaceThisClassSpecifiers(specifiers, prop._class);
1408
1409             if(typeDecl)
1410                FreeDeclarator(typeDecl);
1411          }
1412          else
1413          {
1414             // Move declaration higher...
1415             ast->Move(symbol.externalGet, curExternal.prev);
1416          }
1417       }
1418
1419       // Set
1420       if(prop.Set)
1421       {
1422          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1423          {
1424             Declaration decl;
1425             OldList * specifiers, * declarators;
1426             Declarator d;
1427             OldList * params;
1428             Specifier spec;
1429             External external;
1430             Declarator typeDecl;
1431
1432             declarators = MkList();
1433             params = MkList();
1434
1435             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1436             if(!prop.conversion || prop._class.type == structClass)
1437             {
1438                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1439                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1440             }
1441
1442             specifiers = MkList();
1443
1444             {
1445                Context context = SetupTemplatesContext(prop._class);
1446                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1447                   MkDeclaratorIdentifier(MkIdentifier("value")));
1448                FinishTemplatesContext(context);
1449             }
1450             ListAdd(params, MkTypeName(specifiers, d));
1451
1452             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1453             //if(imported)
1454             if(dllImport)
1455                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1456             d = MkDeclaratorFunction(d, params);
1457
1458             // Make sure the simple _class's type is declared
1459             for(spec = specifiers->first; spec; spec = spec.next)
1460             {
1461                if(spec.type == nameSpecifier /*SpecifierClass*/)
1462                {
1463                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1464                   {
1465                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1466                      symbol._class = classSym.registered;
1467                      if(classSym.registered && classSym.registered.type == structClass)
1468                         DeclareStruct(spec.name, false);
1469                   }
1470                }
1471             }
1472
1473             ListAdd(declarators, MkInitDeclarator(d, null));
1474
1475             specifiers = MkList();
1476             //if(imported)
1477             if(dllImport)
1478                specifiers->Insert(null, MkSpecifier(EXTERN));
1479             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1480                specifiers->Insert(null, MkSpecifier(STATIC));
1481
1482             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1483             if(!prop.conversion || prop._class.type == structClass)
1484                ListAdd(specifiers, MkSpecifier(VOID));
1485             else
1486                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1487
1488             decl = MkDeclaration(specifiers, declarators);
1489
1490             external = MkExternalDeclaration(decl);
1491             ast->Insert(curExternal.prev, external);
1492             external.symbol = symbol;
1493             symbol.externalSet = external;
1494
1495             ReplaceThisClassSpecifiers(specifiers, prop._class);
1496          }
1497          else
1498          {
1499             // Move declaration higher...
1500             ast->Move(symbol.externalSet, curExternal.prev);
1501          }
1502       }
1503
1504       // Property (for Watchers)
1505       if(!symbol.externalPtr)
1506       {
1507          Declaration decl;
1508          External external;
1509          OldList * specifiers = MkList();
1510
1511          if(imported)
1512             specifiers->Insert(null, MkSpecifier(EXTERN));
1513          else
1514             specifiers->Insert(null, MkSpecifier(STATIC));
1515
1516          ListAdd(specifiers, MkSpecifierName("Property"));
1517
1518          {
1519             OldList * list = MkList();
1520             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1521                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1522
1523             if(!imported)
1524             {
1525                strcpy(propName, "__ecerePropM_");
1526                FullClassNameCat(propName, prop._class.fullName, false);
1527                strcat(propName, "_");
1528                // strcat(propName, prop.name);
1529                FullClassNameCat(propName, prop.name, true);
1530
1531                MangleClassName(propName);
1532
1533                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1534                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1535             }
1536             decl = MkDeclaration(specifiers, list);
1537          }
1538
1539          external = MkExternalDeclaration(decl);
1540          ast->Insert(curExternal.prev, external);
1541          external.symbol = symbol;
1542          symbol.externalPtr = external;
1543       }
1544       else
1545       {
1546          // Move declaration higher...
1547          ast->Move(symbol.externalPtr, curExternal.prev);
1548       }
1549
1550       symbol.id = curExternal.symbol.idCode;
1551    }
1552 }
1553
1554 // ***************** EXPRESSION PROCESSING ***************************
1555 public Type Dereference(Type source)
1556 {
1557    Type type = null;
1558    if(source)
1559    {
1560       if(source.kind == pointerType || source.kind == arrayType)
1561       {
1562          type = source.type;
1563          source.type.refCount++;
1564       }
1565       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1566       {
1567          type = Type
1568          {
1569             kind = charType;
1570             refCount = 1;
1571          };
1572       }
1573       // Support dereferencing of no head classes for now...
1574       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1575       {
1576          type = source;
1577          source.refCount++;
1578       }
1579       else
1580          Compiler_Error($"cannot dereference type\n");
1581    }
1582    return type;
1583 }
1584
1585 static Type Reference(Type source)
1586 {
1587    Type type = null;
1588    if(source)
1589    {
1590       type = Type
1591       {
1592          kind = pointerType;
1593          type = source;
1594          refCount = 1;
1595       };
1596       source.refCount++;
1597    }
1598    return type;
1599 }
1600
1601 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1602 {
1603    Identifier ident = member.identifiers ? member.identifiers->first : null;
1604    bool found = false;
1605    DataMember dataMember = null;
1606    Method method = null;
1607    bool freeType = false;
1608
1609    yylloc = member.loc;
1610
1611    if(!ident)
1612    {
1613       if(curMember)
1614       {
1615          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1616          if(*curMember)
1617          {
1618             found = true;
1619             dataMember = *curMember;
1620          }
1621       }
1622    }
1623    else
1624    {
1625       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1626       DataMember _subMemberStack[256];
1627       int _subMemberStackPos = 0;
1628
1629       // FILL MEMBER STACK
1630       if(!thisMember)
1631          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1632       if(thisMember)
1633       {
1634          dataMember = thisMember;
1635          if(curMember && thisMember.memberAccess == publicAccess)
1636          {
1637             *curMember = thisMember;
1638             *curClass = thisMember._class;
1639             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1640             *subMemberStackPos = _subMemberStackPos;
1641          }
1642          found = true;
1643       }
1644       else
1645       {
1646          // Setting a method
1647          method = eClass_FindMethod(_class, ident.string, privateModule);
1648          if(method && method.type == virtualMethod)
1649             found = true;
1650          else
1651             method = null;
1652       }
1653    }
1654
1655    if(found)
1656    {
1657       Type type = null;
1658       if(dataMember)
1659       {
1660          if(!dataMember.dataType && dataMember.dataTypeString)
1661          {
1662             //Context context = SetupTemplatesContext(dataMember._class);
1663             Context context = SetupTemplatesContext(_class);
1664             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1665             FinishTemplatesContext(context);
1666          }
1667          type = dataMember.dataType;
1668       }
1669       else if(method)
1670       {
1671          // This is for destination type...
1672          if(!method.dataType)
1673             ProcessMethodType(method);
1674          //DeclareMethod(method);
1675          // method.dataType = ((Symbol)method.symbol)->type;
1676          type = method.dataType;
1677       }
1678
1679       if(ident && ident.next)
1680       {
1681          for(ident = ident.next; ident && type; ident = ident.next)
1682          {
1683             if(type.kind == classType)
1684             {
1685                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1686                if(!dataMember)
1687                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1688                if(dataMember)
1689                   type = dataMember.dataType;
1690             }
1691             else if(type.kind == structType || type.kind == unionType)
1692             {
1693                Type memberType;
1694                for(memberType = type.members.first; memberType; memberType = memberType.next)
1695                {
1696                   if(!strcmp(memberType.name, ident.string))
1697                   {
1698                      type = memberType;
1699                      break;
1700                   }
1701                }
1702             }
1703          }
1704       }
1705
1706       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1707       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1708       {
1709          int id = 0;
1710          ClassTemplateParameter curParam = null;
1711          Class sClass;
1712          for(sClass = _class; sClass; sClass = sClass.base)
1713          {
1714             id = 0;
1715             if(sClass.templateClass) sClass = sClass.templateClass;
1716             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1717             {
1718                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1719                {
1720                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1721                   {
1722                      if(sClass.templateClass) sClass = sClass.templateClass;
1723                      id += sClass.templateParams.count;
1724                   }
1725                   break;
1726                }
1727                id++;
1728             }
1729             if(curParam) break;
1730          }
1731
1732          if(curParam)
1733          {
1734             ClassTemplateArgument arg = _class.templateArgs[id];
1735             if(arg.dataTypeString)
1736             {
1737                // FreeType(type);
1738                type = ProcessTypeString(arg.dataTypeString, false);
1739                freeType = true;
1740                if(type && _class.templateClass)
1741                   type.passAsTemplate = true;
1742                if(type)
1743                {
1744                   // type.refCount++;
1745                   /*if(!exp.destType)
1746                   {
1747                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1748                      exp.destType.refCount++;
1749                   }*/
1750                }
1751             }
1752          }
1753       }
1754       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1755       {
1756          Class expClass = type._class.registered;
1757          Class cClass = null;
1758          int c;
1759          int paramCount = 0;
1760          int lastParam = -1;
1761
1762          char templateString[1024];
1763          ClassTemplateParameter param;
1764          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1765          for(cClass = expClass; cClass; cClass = cClass.base)
1766          {
1767             int p = 0;
1768             if(cClass.templateClass) cClass = cClass.templateClass;
1769             for(param = cClass.templateParams.first; param; param = param.next)
1770             {
1771                int id = p;
1772                Class sClass;
1773                ClassTemplateArgument arg;
1774                for(sClass = cClass.base; sClass; sClass = sClass.base)
1775                {
1776                   if(sClass.templateClass) sClass = sClass.templateClass;
1777                   id += sClass.templateParams.count;
1778                }
1779                arg = expClass.templateArgs[id];
1780
1781                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1782                {
1783                   ClassTemplateParameter cParam;
1784                   //int p = numParams - sClass.templateParams.count;
1785                   int p = 0;
1786                   Class nextClass;
1787                   if(sClass.templateClass) sClass = sClass.templateClass;
1788
1789                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1790                   {
1791                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1792                      p += nextClass.templateParams.count;
1793                   }
1794
1795                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1796                   {
1797                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1798                      {
1799                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1800                         {
1801                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1802                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1803                            break;
1804                         }
1805                      }
1806                   }
1807                }
1808
1809                {
1810                   char argument[256];
1811                   argument[0] = '\0';
1812                   /*if(arg.name)
1813                   {
1814                      strcat(argument, arg.name.string);
1815                      strcat(argument, " = ");
1816                   }*/
1817                   switch(param.type)
1818                   {
1819                      case expression:
1820                      {
1821                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1822                         char expString[1024];
1823                         OldList * specs = MkList();
1824                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1825                         Expression exp;
1826                         char * string = PrintHexUInt64(arg.expression.ui64);
1827                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1828                         delete string;
1829
1830                         ProcessExpressionType(exp);
1831                         ComputeExpression(exp);
1832                         expString[0] = '\0';
1833                         PrintExpression(exp, expString);
1834                         strcat(argument, expString);
1835                         //delete exp;
1836                         FreeExpression(exp);
1837                         break;
1838                      }
1839                      case identifier:
1840                      {
1841                         strcat(argument, arg.member.name);
1842                         break;
1843                      }
1844                      case TemplateParameterType::type:
1845                      {
1846                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1847                            strcat(argument, arg.dataTypeString);
1848                         break;
1849                      }
1850                   }
1851                   if(argument[0])
1852                   {
1853                      if(paramCount) strcat(templateString, ", ");
1854                      if(lastParam != p - 1)
1855                      {
1856                         strcat(templateString, param.name);
1857                         strcat(templateString, " = ");
1858                      }
1859                      strcat(templateString, argument);
1860                      paramCount++;
1861                      lastParam = p;
1862                   }
1863                   p++;
1864                }
1865             }
1866          }
1867          {
1868             int len = strlen(templateString);
1869             if(templateString[len-1] == '<')
1870                len--;
1871             else
1872             {
1873                if(templateString[len-1] == '>')
1874                   templateString[len++] = ' ';
1875                templateString[len++] = '>';
1876             }
1877             templateString[len++] = '\0';
1878          }
1879          {
1880             Context context = SetupTemplatesContext(_class);
1881             if(freeType) FreeType(type);
1882             type = ProcessTypeString(templateString, false);
1883             freeType = true;
1884             FinishTemplatesContext(context);
1885          }
1886       }
1887
1888       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1889       {
1890          ProcessExpressionType(member.initializer.exp);
1891          if(!member.initializer.exp.expType)
1892          {
1893             if(inCompiler)
1894             {
1895                char expString[10240];
1896                expString[0] = '\0';
1897                PrintExpression(member.initializer.exp, expString);
1898                ChangeCh(expString, '\n', ' ');
1899                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1900             }
1901          }
1902          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1903          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1904          {
1905             Compiler_Error($"incompatible instance method %s\n", ident.string);
1906          }
1907       }
1908       else if(member.initializer)
1909       {
1910          /*
1911          FreeType(member.exp.destType);
1912          member.exp.destType = type;
1913          if(member.exp.destType)
1914             member.exp.destType.refCount++;
1915          ProcessExpressionType(member.exp);
1916          */
1917
1918          ProcessInitializer(member.initializer, type);
1919       }
1920       if(freeType) FreeType(type);
1921    }
1922    else
1923    {
1924       if(_class && _class.type == unitClass)
1925       {
1926          if(member.initializer)
1927          {
1928             /*
1929             FreeType(member.exp.destType);
1930             member.exp.destType = MkClassType(_class.fullName);
1931             ProcessExpressionType(member.initializer, type);
1932             */
1933             Type type = MkClassType(_class.fullName);
1934             ProcessInitializer(member.initializer, type);
1935             FreeType(type);
1936          }
1937       }
1938       else
1939       {
1940          if(member.initializer)
1941          {
1942             //ProcessExpressionType(member.exp);
1943             ProcessInitializer(member.initializer, null);
1944          }
1945          if(ident)
1946          {
1947             if(method)
1948             {
1949                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1950             }
1951             else if(_class)
1952             {
1953                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1954                if(inCompiler)
1955                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1956             }
1957          }
1958          else if(_class)
1959             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1960       }
1961    }
1962 }
1963
1964 void ProcessInstantiationType(Instantiation inst)
1965 {
1966    yylloc = inst.loc;
1967    if(inst._class)
1968    {
1969       MembersInit members;
1970       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1971       Class _class;
1972
1973       /*if(!inst._class.symbol)
1974          inst._class.symbol = FindClass(inst._class.name);*/
1975       classSym = inst._class.symbol;
1976       _class = classSym ? classSym.registered : null;
1977
1978       // DANGER: Patch for mutex not declaring its struct when not needed
1979       if(!_class || _class.type != noHeadClass)
1980          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1981
1982       afterExternal = afterExternal ? afterExternal : curExternal;
1983
1984       if(inst.exp)
1985          ProcessExpressionType(inst.exp);
1986
1987       inst.isConstant = true;
1988       if(inst.members)
1989       {
1990          DataMember curMember = null;
1991          Class curClass = null;
1992          DataMember subMemberStack[256];
1993          int subMemberStackPos = 0;
1994
1995          for(members = inst.members->first; members; members = members.next)
1996          {
1997             switch(members.type)
1998             {
1999                case methodMembersInit:
2000                {
2001                   char name[1024];
2002                   static uint instMethodID = 0;
2003                   External external = curExternal;
2004                   Context context = curContext;
2005                   Declarator declarator = members.function.declarator;
2006                   Identifier nameID = GetDeclId(declarator);
2007                   char * unmangled = nameID ? nameID.string : null;
2008                   Expression exp;
2009                   External createdExternal = null;
2010
2011                   if(inCompiler)
2012                   {
2013                      char number[16];
2014                      //members.function.dontMangle = true;
2015                      strcpy(name, "__ecereInstMeth_");
2016                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2017                      strcat(name, "_");
2018                      strcat(name, nameID.string);
2019                      strcat(name, "_");
2020                      sprintf(number, "_%08d", instMethodID++);
2021                      strcat(name, number);
2022                      nameID.string = CopyString(name);
2023                   }
2024
2025                   // Do modifications here...
2026                   if(declarator)
2027                   {
2028                      Symbol symbol = declarator.symbol;
2029                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2030
2031                      if(method && method.type == virtualMethod)
2032                      {
2033                         symbol.method = method;
2034                         ProcessMethodType(method);
2035
2036                         if(!symbol.type.thisClass)
2037                         {
2038                            if(method.dataType.thisClass && currentClass &&
2039                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2040                            {
2041                               if(!currentClass.symbol)
2042                                  currentClass.symbol = FindClass(currentClass.fullName);
2043                               symbol.type.thisClass = currentClass.symbol;
2044                            }
2045                            else
2046                            {
2047                               if(!_class.symbol)
2048                                  _class.symbol = FindClass(_class.fullName);
2049                               symbol.type.thisClass = _class.symbol;
2050                            }
2051                         }
2052                         // TESTING THIS HERE:
2053                         DeclareType(symbol.type, true, true);
2054
2055                      }
2056                      else if(classSym)
2057                      {
2058                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2059                            unmangled, classSym.string);
2060                      }
2061                   }
2062
2063                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2064                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2065
2066                   if(nameID)
2067                   {
2068                      FreeSpecifier(nameID._class);
2069                      nameID._class = null;
2070                   }
2071
2072                   if(inCompiler)
2073                   {
2074                      //Type type = declarator.symbol.type;
2075                      External oldExternal = curExternal;
2076
2077                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2078                      // *** It was commented out for problems such as
2079                      /*
2080                            class VirtualDesktop : Window
2081                            {
2082                               clientSize = Size { };
2083                               Timer timer
2084                               {
2085                                  bool DelayExpired()
2086                                  {
2087                                     clientSize.w;
2088                                     return true;
2089                                  }
2090                               };
2091                            }
2092                      */
2093                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2094
2095                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2096
2097                      /*
2098                      if(strcmp(declarator.symbol.string, name))
2099                      {
2100                         printf("TOCHECK: Look out for this\n");
2101                         delete declarator.symbol.string;
2102                         declarator.symbol.string = CopyString(name);
2103                      }
2104
2105                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2106                      {
2107                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2108                         excludedSymbols->Remove(declarator.symbol);
2109                         globalContext.symbols.Add((BTNode)declarator.symbol);
2110                         if(strstr(declarator.symbol.string), "::")
2111                            globalContext.hasNameSpace = true;
2112
2113                      }
2114                      */
2115
2116                      //curExternal = curExternal.prev;
2117                      //afterExternal = afterExternal->next;
2118
2119                      //ProcessFunction(afterExternal->function);
2120
2121                      //curExternal = afterExternal;
2122                      {
2123                         External externalDecl;
2124                         externalDecl = MkExternalDeclaration(null);
2125                         ast->Insert(oldExternal.prev, externalDecl);
2126
2127                         // Which function does this process?
2128                         if(createdExternal.function)
2129                         {
2130                            ProcessFunction(createdExternal.function);
2131
2132                            //curExternal = oldExternal;
2133
2134                            {
2135                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2136
2137                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2138                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2139
2140                               //externalDecl = MkExternalDeclaration(decl);
2141
2142                               //***** ast->Insert(external.prev, externalDecl);
2143                               //ast->Insert(curExternal.prev, externalDecl);
2144                               externalDecl.declaration = decl;
2145                               if(decl.symbol && !decl.symbol.pointerExternal)
2146                                  decl.symbol.pointerExternal = externalDecl;
2147
2148                               // Trying this out...
2149                               declarator.symbol.pointerExternal = externalDecl;
2150                            }
2151                         }
2152                      }
2153                   }
2154                   else if(declarator)
2155                   {
2156                      curExternal = declarator.symbol.pointerExternal;
2157                      ProcessFunction((FunctionDefinition)members.function);
2158                   }
2159                   curExternal = external;
2160                   curContext = context;
2161
2162                   if(inCompiler)
2163                   {
2164                      FreeClassFunction(members.function);
2165
2166                      // In this pass, turn this into a MemberInitData
2167                      exp = QMkExpId(name);
2168                      members.type = dataMembersInit;
2169                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2170
2171                      delete unmangled;
2172                   }
2173                   break;
2174                }
2175                case dataMembersInit:
2176                {
2177                   if(members.dataMembers && classSym)
2178                   {
2179                      MemberInit member;
2180                      Location oldyyloc = yylloc;
2181                      for(member = members.dataMembers->first; member; member = member.next)
2182                      {
2183                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2184                         if(member.initializer && !member.initializer.isConstant)
2185                            inst.isConstant = false;
2186                      }
2187                      yylloc = oldyyloc;
2188                   }
2189                   break;
2190                }
2191             }
2192          }
2193       }
2194    }
2195 }
2196
2197 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2198 {
2199    // OPTIMIZATIONS: TESTING THIS...
2200    if(inCompiler)
2201    {
2202       if(type.kind == functionType)
2203       {
2204          Type param;
2205          if(declareParams)
2206          {
2207             for(param = type.params.first; param; param = param.next)
2208                DeclareType(param, declarePointers, true);
2209          }
2210          DeclareType(type.returnType, declarePointers, true);
2211       }
2212       else if(type.kind == pointerType && declarePointers)
2213          DeclareType(type.type, declarePointers, false);
2214       else if(type.kind == classType)
2215       {
2216          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2217             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2218       }
2219       else if(type.kind == structType || type.kind == unionType)
2220       {
2221          Type member;
2222          for(member = type.members.first; member; member = member.next)
2223             DeclareType(member, false, false);
2224       }
2225       else if(type.kind == arrayType)
2226          DeclareType(type.arrayType, declarePointers, false);
2227    }
2228 }
2229
2230 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2231 {
2232    ClassTemplateArgument * arg = null;
2233    int id = 0;
2234    ClassTemplateParameter curParam = null;
2235    Class sClass;
2236    for(sClass = _class; sClass; sClass = sClass.base)
2237    {
2238       id = 0;
2239       if(sClass.templateClass) sClass = sClass.templateClass;
2240       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2241       {
2242          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2243          {
2244             for(sClass = sClass.base; sClass; sClass = sClass.base)
2245             {
2246                if(sClass.templateClass) sClass = sClass.templateClass;
2247                id += sClass.templateParams.count;
2248             }
2249             break;
2250          }
2251          id++;
2252       }
2253       if(curParam) break;
2254    }
2255    if(curParam)
2256    {
2257       arg = &_class.templateArgs[id];
2258       if(arg && param.type == type)
2259          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2260    }
2261    return arg;
2262 }
2263
2264 public Context SetupTemplatesContext(Class _class)
2265 {
2266    Context context = PushContext();
2267    context.templateTypesOnly = true;
2268    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2269    {
2270       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2271       for(; param; param = param.next)
2272       {
2273          if(param.type == type && param.identifier)
2274          {
2275             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2276             curContext.templateTypes.Add((BTNode)type);
2277          }
2278       }
2279    }
2280    else if(_class)
2281    {
2282       Class sClass;
2283       for(sClass = _class; sClass; sClass = sClass.base)
2284       {
2285          ClassTemplateParameter p;
2286          for(p = sClass.templateParams.first; p; p = p.next)
2287          {
2288             //OldList * specs = MkList();
2289             //Declarator decl = null;
2290             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2291             if(p.type == type)
2292             {
2293                TemplateParameter param = p.param;
2294                TemplatedType type;
2295                if(!param)
2296                {
2297                   // ADD DATA TYPE HERE...
2298                   p.param = param = TemplateParameter
2299                   {
2300                      identifier = MkIdentifier(p.name), type = p.type,
2301                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2302                   };
2303                }
2304                type = TemplatedType { key = (uintptr)p.name, param = param };
2305                curContext.templateTypes.Add((BTNode)type);
2306             }
2307          }
2308       }
2309    }
2310    return context;
2311 }
2312
2313 public void FinishTemplatesContext(Context context)
2314 {
2315    PopContext(context);
2316    FreeContext(context);
2317    delete context;
2318 }
2319
2320 public void ProcessMethodType(Method method)
2321 {
2322    if(!method.dataType)
2323    {
2324       Context context = SetupTemplatesContext(method._class);
2325
2326       method.dataType = ProcessTypeString(method.dataTypeString, false);
2327
2328       FinishTemplatesContext(context);
2329
2330       if(method.type != virtualMethod && method.dataType)
2331       {
2332          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2333          {
2334             if(!method._class.symbol)
2335                method._class.symbol = FindClass(method._class.fullName);
2336             method.dataType.thisClass = method._class.symbol;
2337          }
2338       }
2339
2340       // Why was this commented out? Working fine without now...
2341
2342       /*
2343       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2344          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2345          */
2346    }
2347
2348    /*
2349    if(type)
2350    {
2351       char * par = strstr(type, "(");
2352       char * classOp = null;
2353       int classOpLen = 0;
2354       if(par)
2355       {
2356          int c;
2357          for(c = par-type-1; c >= 0; c++)
2358          {
2359             if(type[c] == ':' && type[c+1] == ':')
2360             {
2361                classOp = type + c - 1;
2362                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2363                {
2364                   classOp--;
2365                   classOpLen++;
2366                }
2367                break;
2368             }
2369             else if(!isspace(type[c]))
2370                break;
2371          }
2372       }
2373       if(classOp)
2374       {
2375          char temp[1024];
2376          int typeLen = strlen(type);
2377          memcpy(temp, classOp, classOpLen);
2378          temp[classOpLen] = '\0';
2379          if(temp[0])
2380             _class = eSystem_FindClass(module, temp);
2381          else
2382             _class = null;
2383          method.dataTypeString = new char[typeLen - classOpLen + 1];
2384          memcpy(method.dataTypeString, type, classOp - type);
2385          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2386       }
2387       else
2388          method.dataTypeString = type;
2389    }
2390    */
2391 }
2392
2393
2394 public void ProcessPropertyType(Property prop)
2395 {
2396    if(!prop.dataType)
2397    {
2398       Context context = SetupTemplatesContext(prop._class);
2399       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2400       FinishTemplatesContext(context);
2401    }
2402 }
2403
2404 public void DeclareMethod(Method method, char * name)
2405 {
2406    Symbol symbol = method.symbol;
2407    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2408    {
2409       bool imported = false;
2410       bool dllImport = false;
2411
2412       if(!method.dataType)
2413          method.dataType = ProcessTypeString(method.dataTypeString, false);
2414
2415       if(!symbol || symbol._import || method.type == virtualMethod)
2416       {
2417          if(!symbol || method.type == virtualMethod)
2418          {
2419             Symbol classSym;
2420             if(!method._class.symbol)
2421                method._class.symbol = FindClass(method._class.fullName);
2422             classSym = method._class.symbol;
2423             if(!classSym._import)
2424             {
2425                ModuleImport module;
2426
2427                if(method._class.module && method._class.module.name)
2428                   module = FindModule(method._class.module);
2429                else
2430                   module = mainModule;
2431                classSym._import = ClassImport
2432                {
2433                   name = CopyString(method._class.fullName);
2434                   isRemote = method._class.isRemote;
2435                };
2436                module.classes.Add(classSym._import);
2437             }
2438             if(!symbol)
2439             {
2440                symbol = method.symbol = Symbol { };
2441             }
2442             if(!symbol._import)
2443             {
2444                symbol._import = (ClassImport)MethodImport
2445                {
2446                   name = CopyString(method.name);
2447                   isVirtual = method.type == virtualMethod;
2448                };
2449                classSym._import.methods.Add(symbol._import);
2450             }
2451             if(!symbol)
2452             {
2453                // Set the symbol type
2454                /*
2455                if(!type.thisClass)
2456                {
2457                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2458                }
2459                else if(type.thisClass == (void *)-1)
2460                {
2461                   type.thisClass = null;
2462                }
2463                */
2464                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2465                symbol.type = method.dataType;
2466                if(symbol.type) symbol.type.refCount++;
2467             }
2468             /*
2469             if(!method.thisClass || strcmp(method.thisClass, "void"))
2470                symbol.type.params.Insert(null,
2471                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2472             */
2473          }
2474          if(!method.dataType.dllExport)
2475          {
2476             imported = true;
2477             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2478                dllImport = true;
2479          }
2480       }
2481
2482       /* MOVING THIS UP
2483       if(!method.dataType)
2484          method.dataType = ((Symbol)method.symbol).type;
2485          //ProcessMethodType(method);
2486       */
2487
2488       if(method.type != virtualMethod && method.dataType)
2489          DeclareType(method.dataType, true, true);
2490
2491       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2492       {
2493          // We need a declaration here :)
2494          Declaration decl;
2495          OldList * specifiers, * declarators;
2496          Declarator d;
2497          Declarator funcDecl;
2498          External external;
2499
2500          specifiers = MkList();
2501          declarators = MkList();
2502
2503          //if(imported)
2504          if(dllImport)
2505             ListAdd(specifiers, MkSpecifier(EXTERN));
2506          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2507             ListAdd(specifiers, MkSpecifier(STATIC));
2508
2509          if(method.type == virtualMethod)
2510          {
2511             ListAdd(specifiers, MkSpecifier(INT));
2512             d = MkDeclaratorIdentifier(MkIdentifier(name));
2513          }
2514          else
2515          {
2516             d = MkDeclaratorIdentifier(MkIdentifier(name));
2517             //if(imported)
2518             if(dllImport)
2519                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2520             {
2521                Context context = SetupTemplatesContext(method._class);
2522                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2523                FinishTemplatesContext(context);
2524             }
2525             funcDecl = GetFuncDecl(d);
2526
2527             if(dllImport)
2528             {
2529                Specifier spec, next;
2530                for(spec = specifiers->first; spec; spec = next)
2531                {
2532                   next = spec.next;
2533                   if(spec.type == extendedSpecifier)
2534                   {
2535                      specifiers->Remove(spec);
2536                      FreeSpecifier(spec);
2537                   }
2538                }
2539             }
2540
2541             // Add this parameter if not a static method
2542             if(method.dataType && !method.dataType.staticMethod)
2543             {
2544                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2545                {
2546                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2547                   TypeName thisParam = MkTypeName(MkListOne(
2548                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2549                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2550                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2551                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2552
2553                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2554                   {
2555                      TypeName param = funcDecl.function.parameters->first;
2556                      funcDecl.function.parameters->Remove(param);
2557                      FreeTypeName(param);
2558                   }
2559
2560                   if(!funcDecl.function.parameters)
2561                      funcDecl.function.parameters = MkList();
2562                   funcDecl.function.parameters->Insert(null, thisParam);
2563                }
2564             }
2565             // Make sure we don't have empty parameter declarations for static methods...
2566             /*
2567             else if(!funcDecl.function.parameters)
2568             {
2569                funcDecl.function.parameters = MkList();
2570                funcDecl.function.parameters->Insert(null,
2571                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2572             }*/
2573          }
2574          // TESTING THIS:
2575          ProcessDeclarator(d);
2576
2577          ListAdd(declarators, MkInitDeclarator(d, null));
2578
2579          decl = MkDeclaration(specifiers, declarators);
2580
2581          ReplaceThisClassSpecifiers(specifiers, method._class);
2582
2583          // Keep a different symbol for the function definition than the declaration...
2584          if(symbol.pointerExternal)
2585          {
2586             Symbol functionSymbol { };
2587
2588             // Copy symbol
2589             {
2590                *functionSymbol = *symbol;
2591                functionSymbol.string = CopyString(symbol.string);
2592                if(functionSymbol.type)
2593                   functionSymbol.type.refCount++;
2594             }
2595
2596             excludedSymbols->Add(functionSymbol);
2597             symbol.pointerExternal.symbol = functionSymbol;
2598          }
2599          external = MkExternalDeclaration(decl);
2600          if(curExternal)
2601             ast->Insert(curExternal ? curExternal.prev : null, external);
2602          external.symbol = symbol;
2603          symbol.pointerExternal = external;
2604       }
2605       else if(ast)
2606       {
2607          // Move declaration higher...
2608          ast->Move(symbol.pointerExternal, curExternal.prev);
2609       }
2610
2611       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2612    }
2613 }
2614
2615 char * ReplaceThisClass(Class _class)
2616 {
2617    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2618    {
2619       bool first = true;
2620       int p = 0;
2621       ClassTemplateParameter param;
2622       int lastParam = -1;
2623
2624       char className[1024];
2625       strcpy(className, _class.fullName);
2626       for(param = _class.templateParams.first; param; param = param.next)
2627       {
2628          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2629          {
2630             if(first) strcat(className, "<");
2631             if(!first) strcat(className, ", ");
2632             if(lastParam + 1 != p)
2633             {
2634                strcat(className, param.name);
2635                strcat(className, " = ");
2636             }
2637             strcat(className, param.name);
2638             first = false;
2639             lastParam = p;
2640          }
2641          p++;
2642       }
2643       if(!first)
2644       {
2645          int len = strlen(className);
2646          if(className[len-1] == '>') className[len++] = ' ';
2647          className[len++] = '>';
2648          className[len++] = '\0';
2649       }
2650       return CopyString(className);
2651    }
2652    else
2653       return CopyString(_class.fullName);
2654 }
2655
2656 Type ReplaceThisClassType(Class _class)
2657 {
2658    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2659    {
2660       bool first = true;
2661       int p = 0;
2662       ClassTemplateParameter param;
2663       int lastParam = -1;
2664       char className[1024];
2665       strcpy(className, _class.fullName);
2666
2667       for(param = _class.templateParams.first; param; param = param.next)
2668       {
2669          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2670          {
2671             if(first) strcat(className, "<");
2672             if(!first) strcat(className, ", ");
2673             if(lastParam + 1 != p)
2674             {
2675                strcat(className, param.name);
2676                strcat(className, " = ");
2677             }
2678             strcat(className, param.name);
2679             first = false;
2680             lastParam = p;
2681          }
2682          p++;
2683       }
2684       if(!first)
2685       {
2686          int len = strlen(className);
2687          if(className[len-1] == '>') className[len++] = ' ';
2688          className[len++] = '>';
2689          className[len++] = '\0';
2690       }
2691       return MkClassType(className);
2692       //return ProcessTypeString(className, false);
2693    }
2694    else
2695    {
2696       return MkClassType(_class.fullName);
2697       //return ProcessTypeString(_class.fullName, false);
2698    }
2699 }
2700
2701 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2702 {
2703    if(specs != null && _class)
2704    {
2705       Specifier spec;
2706       for(spec = specs.first; spec; spec = spec.next)
2707       {
2708          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2709          {
2710             spec.type = nameSpecifier;
2711             spec.name = ReplaceThisClass(_class);
2712             spec.symbol = FindClass(spec.name); //_class.symbol;
2713          }
2714       }
2715    }
2716 }
2717
2718 // Returns imported or not
2719 bool DeclareFunction(GlobalFunction function, char * name)
2720 {
2721    Symbol symbol = function.symbol;
2722    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2723    {
2724       bool imported = false;
2725       bool dllImport = false;
2726
2727       if(!function.dataType)
2728       {
2729          function.dataType = ProcessTypeString(function.dataTypeString, false);
2730          if(!function.dataType.thisClass)
2731             function.dataType.staticMethod = true;
2732       }
2733
2734       if(inCompiler)
2735       {
2736          if(!symbol)
2737          {
2738             ModuleImport module = FindModule(function.module);
2739             // WARNING: This is not added anywhere...
2740             symbol = function.symbol = Symbol {  };
2741
2742             if(module.name)
2743             {
2744                if(!function.dataType.dllExport)
2745                {
2746                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2747                   module.functions.Add(symbol._import);
2748                }
2749             }
2750             // Set the symbol type
2751             {
2752                symbol.type = ProcessTypeString(function.dataTypeString, false);
2753                if(!symbol.type.thisClass)
2754                   symbol.type.staticMethod = true;
2755             }
2756          }
2757          imported = symbol._import ? true : false;
2758          if(imported && function.module != privateModule && function.module.importType != staticImport)
2759             dllImport = true;
2760       }
2761
2762       DeclareType(function.dataType, true, true);
2763
2764       if(inCompiler)
2765       {
2766          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2767          {
2768             // We need a declaration here :)
2769             Declaration decl;
2770             OldList * specifiers, * declarators;
2771             Declarator d;
2772             Declarator funcDecl;
2773             External external;
2774
2775             specifiers = MkList();
2776             declarators = MkList();
2777
2778             //if(imported)
2779                ListAdd(specifiers, MkSpecifier(EXTERN));
2780             /*
2781             else
2782                ListAdd(specifiers, MkSpecifier(STATIC));
2783             */
2784
2785             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2786             //if(imported)
2787             if(dllImport)
2788                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2789
2790             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2791             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2792             if(function.module.importType == staticImport)
2793             {
2794                Specifier spec;
2795                for(spec = specifiers->first; spec; spec = spec.next)
2796                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2797                   {
2798                      specifiers->Remove(spec);
2799                      FreeSpecifier(spec);
2800                      break;
2801                   }
2802             }
2803
2804             funcDecl = GetFuncDecl(d);
2805
2806             // Make sure we don't have empty parameter declarations for static methods...
2807             if(funcDecl && !funcDecl.function.parameters)
2808             {
2809                funcDecl.function.parameters = MkList();
2810                funcDecl.function.parameters->Insert(null,
2811                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2812             }
2813
2814             ListAdd(declarators, MkInitDeclarator(d, null));
2815
2816             {
2817                Context oldCtx = curContext;
2818                curContext = globalContext;
2819                decl = MkDeclaration(specifiers, declarators);
2820                curContext = oldCtx;
2821             }
2822
2823             // Keep a different symbol for the function definition than the declaration...
2824             if(symbol.pointerExternal)
2825             {
2826                Symbol functionSymbol { };
2827                // Copy symbol
2828                {
2829                   *functionSymbol = *symbol;
2830                   functionSymbol.string = CopyString(symbol.string);
2831                   if(functionSymbol.type)
2832                      functionSymbol.type.refCount++;
2833                }
2834
2835                excludedSymbols->Add(functionSymbol);
2836
2837                symbol.pointerExternal.symbol = functionSymbol;
2838             }
2839             external = MkExternalDeclaration(decl);
2840             if(curExternal)
2841                ast->Insert(curExternal.prev, external);
2842             external.symbol = symbol;
2843             symbol.pointerExternal = external;
2844          }
2845          else
2846          {
2847             // Move declaration higher...
2848             ast->Move(symbol.pointerExternal, curExternal.prev);
2849          }
2850
2851          if(curExternal)
2852             symbol.id = curExternal.symbol.idCode;
2853       }
2854    }
2855    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2856 }
2857
2858 void DeclareGlobalData(GlobalData data)
2859 {
2860    Symbol symbol = data.symbol;
2861    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2862    {
2863       if(inCompiler)
2864       {
2865          if(!symbol)
2866             symbol = data.symbol = Symbol { };
2867       }
2868       if(!data.dataType)
2869          data.dataType = ProcessTypeString(data.dataTypeString, false);
2870       DeclareType(data.dataType, true, true);
2871       if(inCompiler)
2872       {
2873          if(!symbol.pointerExternal)
2874          {
2875             // We need a declaration here :)
2876             Declaration decl;
2877             OldList * specifiers, * declarators;
2878             Declarator d;
2879             External external;
2880
2881             specifiers = MkList();
2882             declarators = MkList();
2883
2884             ListAdd(specifiers, MkSpecifier(EXTERN));
2885             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2886             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2887
2888             ListAdd(declarators, MkInitDeclarator(d, null));
2889
2890             decl = MkDeclaration(specifiers, declarators);
2891             external = MkExternalDeclaration(decl);
2892             if(curExternal)
2893                ast->Insert(curExternal.prev, external);
2894             external.symbol = symbol;
2895             symbol.pointerExternal = external;
2896          }
2897          else
2898          {
2899             // Move declaration higher...
2900             ast->Move(symbol.pointerExternal, curExternal.prev);
2901          }
2902
2903          if(curExternal)
2904             symbol.id = curExternal.symbol.idCode;
2905       }
2906    }
2907 }
2908
2909 class Conversion : struct
2910 {
2911    Conversion prev, next;
2912    Property convert;
2913    bool isGet;
2914    Type resultType;
2915 };
2916
2917 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2918 {
2919    if(source && dest)
2920    {
2921       // Property convert;
2922
2923       if(source.kind == templateType && dest.kind != templateType)
2924       {
2925          Type type = ProcessTemplateParameterType(source.templateParameter);
2926          if(type) source = type;
2927       }
2928
2929       if(dest.kind == templateType && source.kind != templateType)
2930       {
2931          Type type = ProcessTemplateParameterType(dest.templateParameter);
2932          if(type) dest = type;
2933       }
2934
2935       if(dest.classObjectType == typedObject)
2936       {
2937          if(source.classObjectType != anyObject)
2938             return true;
2939          else
2940          {
2941             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2942             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2943             {
2944                return true;
2945             }
2946          }
2947       }
2948       else
2949       {
2950          if(source.classObjectType == anyObject)
2951             return true;
2952          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2953             return true;
2954       }
2955
2956       if((dest.kind == structType && source.kind == structType) ||
2957          (dest.kind == unionType && source.kind == unionType))
2958       {
2959          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2960              (source.members.first && source.members.first == dest.members.first))
2961             return true;
2962       }
2963
2964       if(dest.kind == ellipsisType && source.kind != voidType)
2965          return true;
2966
2967       if(dest.kind == pointerType && dest.type.kind == voidType &&
2968          ((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))
2969          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2970
2971          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2972
2973          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2974          return true;
2975       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2976          ((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))
2977          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2978
2979          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2980
2981          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2982          return true;
2983
2984       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2985       {
2986          if(source._class.registered && source._class.registered.type == unitClass)
2987          {
2988             if(conversions != null)
2989             {
2990                if(source._class.registered == dest._class.registered)
2991                   return true;
2992             }
2993             else
2994             {
2995                Class sourceBase, destBase;
2996                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2997                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2998                if(sourceBase == destBase)
2999                   return true;
3000             }
3001          }
3002          // Don't match enum inheriting from other enum if resolving enumeration values
3003          // TESTING: !dest.classObjectType
3004          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3005             (enumBaseType ||
3006                (!source._class.registered || source._class.registered.type != enumClass) ||
3007                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3008             return true;
3009          else
3010          {
3011             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3012             if(enumBaseType &&
3013                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3014                ((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)
3015             {
3016                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3017                {
3018                   return true;
3019                }
3020             }
3021          }
3022       }
3023
3024       // JUST ADDED THIS...
3025       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3026          return true;
3027
3028       if(doConversion)
3029       {
3030          // Just added this for Straight conversion of ColorAlpha => Color
3031          if(source.kind == classType)
3032          {
3033             Class _class;
3034             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3035             {
3036                Property convert;
3037                for(convert = _class.conversions.first; convert; convert = convert.next)
3038                {
3039                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3040                   {
3041                      Conversion after = (conversions != null) ? conversions.last : null;
3042
3043                      if(!convert.dataType)
3044                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3045                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3046                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3047                         MatchTypes(convert.dataType, dest, conversions, null, null,
3048                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3049                               convert.dataType.kind == classType, false, true))
3050                      {
3051                         if(!conversions && !convert.Get)
3052                            return true;
3053                         else if(conversions != null)
3054                         {
3055                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3056                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3057                               (dest.kind != classType || dest._class.registered != _class.base))
3058                               return true;
3059                            else
3060                            {
3061                               Conversion conv { convert = convert, isGet = true };
3062                               // conversions.Add(conv);
3063                               conversions.Insert(after, conv);
3064                               return true;
3065                            }
3066                         }
3067                      }
3068                   }
3069                }
3070             }
3071          }
3072
3073          // MOVING THIS??
3074
3075          if(dest.kind == classType)
3076          {
3077             Class _class;
3078             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3079             {
3080                Property convert;
3081                for(convert = _class.conversions.first; convert; convert = convert.next)
3082                {
3083                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3084                   {
3085                      // Conversion after = (conversions != null) ? conversions.last : null;
3086
3087                      if(!convert.dataType)
3088                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3089                      // Just added this equality check to prevent recursion.... Make it safer?
3090                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3091                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3092                      {
3093                         if(!conversions && !convert.Set)
3094                            return true;
3095                         else if(conversions != null)
3096                         {
3097                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3098                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3099                               (source.kind != classType || source._class.registered != _class.base))
3100                               return true;
3101                            else
3102                            {
3103                               // *** Testing this! ***
3104                               Conversion conv { convert = convert };
3105                               conversions.Add(conv);
3106                               //conversions.Insert(after, conv);
3107                               return true;
3108                            }
3109                         }
3110                      }
3111                   }
3112                }
3113             }
3114             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3115             {
3116                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3117                   (source.kind != classType || source._class.registered.type != structClass))
3118                   return true;
3119             }*/
3120
3121             // TESTING THIS... IS THIS OK??
3122             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3123             {
3124                if(!dest._class.registered.dataType)
3125                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3126                // Only support this for classes...
3127                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3128                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3129                {
3130                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false))
3131                   {
3132                      return true;
3133                   }
3134                }
3135             }
3136          }
3137
3138          // Moved this lower
3139          if(source.kind == classType)
3140          {
3141             Class _class;
3142             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3143             {
3144                Property convert;
3145                for(convert = _class.conversions.first; convert; convert = convert.next)
3146                {
3147                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3148                   {
3149                      Conversion after = (conversions != null) ? conversions.last : null;
3150
3151                      if(!convert.dataType)
3152                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3153                      if(convert.dataType != source &&
3154                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3155                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true))
3156                      {
3157                         if(!conversions && !convert.Get)
3158                            return true;
3159                         else if(conversions != null)
3160                         {
3161                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3162                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3163                               (dest.kind != classType || dest._class.registered != _class.base))
3164                               return true;
3165                            else
3166                            {
3167                               Conversion conv { convert = convert, isGet = true };
3168
3169                               // conversions.Add(conv);
3170                               conversions.Insert(after, conv);
3171                               return true;
3172                            }
3173                         }
3174                      }
3175                   }
3176                }
3177             }
3178
3179             // TESTING THIS... IS THIS OK??
3180             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3181             {
3182                if(!source._class.registered.dataType)
3183                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3184                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3185                {
3186                   if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, source._class.registered.dataType.kind == classType, source._class.registered.dataType.kind == classType, false, false))
3187                      return true;
3188                   // For bool to be accepted by byte, short, etc.
3189                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false))
3190                      return true;
3191                }
3192             }
3193          }
3194       }
3195
3196       if(source.kind == classType || source.kind == subClassType)
3197          ;
3198       else if(dest.kind == source.kind &&
3199          (dest.kind != structType && dest.kind != unionType &&
3200           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3201           return true;
3202       // RECENTLY ADDED THESE
3203       else if(dest.kind == doubleType && source.kind == floatType)
3204          return true;
3205       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3206          return true;
3207       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3208          return true;
3209       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3210          return true;
3211       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3212          return true;
3213       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3214          return true;
3215       else if(source.kind == enumType &&
3216          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3217           return true;
3218       else if(dest.kind == enumType && !isConversionExploration &&
3219          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3220           return true;
3221       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3222               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3223       {
3224          Type paramSource, paramDest;
3225
3226          if(dest.kind == methodType)
3227             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3228          if(source.kind == methodType)
3229             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3230
3231          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3232          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3233          if(dest.kind == methodType)
3234             dest = dest.method.dataType;
3235          if(source.kind == methodType)
3236             source = source.method.dataType;
3237
3238          paramSource = source.params.first;
3239          if(paramSource && paramSource.kind == voidType) paramSource = null;
3240          paramDest = dest.params.first;
3241          if(paramDest && paramDest.kind == voidType) paramDest = null;
3242
3243
3244          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3245             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3246          {
3247             // Source thisClass must be derived from destination thisClass
3248             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3249                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3250             {
3251                if(paramDest && paramDest.kind == classType)
3252                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3253                else
3254                   Compiler_Error($"method class should not take an object\n");
3255                return false;
3256             }
3257             paramDest = paramDest.next;
3258          }
3259          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3260          {
3261             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3262             {
3263                if(dest.thisClass)
3264                {
3265                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3266                   {
3267                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3268                      return false;
3269                   }
3270                }
3271                else
3272                {
3273                   // THIS WAS BACKWARDS:
3274                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3275                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3276                   {
3277                      if(owningClassDest)
3278                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3279                      else
3280                         Compiler_Error($"overriding class expected to be derived from method class\n");
3281                      return false;
3282                   }
3283                }
3284                paramSource = paramSource.next;
3285             }
3286             else
3287             {
3288                if(dest.thisClass)
3289                {
3290                   // Source thisClass must be derived from destination thisClass
3291                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3292                   {
3293                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3294                      return false;
3295                   }
3296                }
3297                else
3298                {
3299                   // THIS WAS BACKWARDS TOO??
3300                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3301                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3302                   {
3303                      //if(owningClass)
3304                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3305                      //else
3306                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3307                      return false;
3308                   }
3309                }
3310             }
3311          }
3312
3313
3314          // Source return type must be derived from destination return type
3315          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3316          {
3317             Compiler_Warning($"incompatible return type for function\n");
3318             return false;
3319          }
3320
3321          // Check parameters
3322
3323          for(; paramDest; paramDest = paramDest.next)
3324          {
3325             if(!paramSource)
3326             {
3327                //Compiler_Warning($"not enough parameters\n");
3328                Compiler_Error($"not enough parameters\n");
3329                return false;
3330             }
3331             {
3332                Type paramDestType = paramDest;
3333                Type paramSourceType = paramSource;
3334                Type type = paramDestType;
3335
3336                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3337                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3338                   paramSource.kind != templateType)
3339                {
3340                   int id = 0;
3341                   ClassTemplateParameter curParam = null;
3342                   Class sClass;
3343                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3344                   {
3345                      id = 0;
3346                      if(sClass.templateClass) sClass = sClass.templateClass;
3347                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3348                      {
3349                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3350                         {
3351                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3352                            {
3353                               if(sClass.templateClass) sClass = sClass.templateClass;
3354                               id += sClass.templateParams.count;
3355                            }
3356                            break;
3357                         }
3358                         id++;
3359                      }
3360                      if(curParam) break;
3361                   }
3362
3363                   if(curParam)
3364                   {
3365                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3366                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3367                   }
3368                }
3369
3370                // paramDest must be derived from paramSource
3371                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3372                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3373                {
3374                   char type[1024];
3375                   type[0] = 0;
3376                   PrintType(paramDest, type, false, true);
3377                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3378
3379                   if(paramDestType != paramDest)
3380                      FreeType(paramDestType);
3381                   return false;
3382                }
3383                if(paramDestType != paramDest)
3384                   FreeType(paramDestType);
3385             }
3386
3387             paramSource = paramSource.next;
3388          }
3389          if(paramSource)
3390          {
3391             Compiler_Error($"too many parameters\n");
3392             return false;
3393          }
3394          return true;
3395       }
3396       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3397       {
3398          return true;
3399       }
3400       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3401          (source.kind == arrayType || source.kind == pointerType))
3402       {
3403          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3404             return true;
3405       }
3406    }
3407    return false;
3408 }
3409
3410 static void FreeConvert(Conversion convert)
3411 {
3412    if(convert.resultType)
3413       FreeType(convert.resultType);
3414 }
3415
3416 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3417                               char * string, OldList conversions)
3418 {
3419    BTNamedLink link;
3420
3421    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3422    {
3423       Class _class = link.data;
3424       if(_class.type == enumClass)
3425       {
3426          OldList converts { };
3427          Type type { };
3428          type.kind = classType;
3429
3430          if(!_class.symbol)
3431             _class.symbol = FindClass(_class.fullName);
3432          type._class = _class.symbol;
3433
3434          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3435          {
3436             NamedLink value;
3437             Class enumClass = eSystem_FindClass(privateModule, "enum");
3438             if(enumClass)
3439             {
3440                Class baseClass;
3441                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3442                {
3443                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3444                   for(value = e.values.first; value; value = value.next)
3445                   {
3446                      if(!strcmp(value.name, string))
3447                         break;
3448                   }
3449                   if(value)
3450                   {
3451                      FreeExpContents(sourceExp);
3452                      FreeType(sourceExp.expType);
3453
3454                      sourceExp.isConstant = true;
3455                      sourceExp.expType = MkClassType(baseClass.fullName);
3456                      //if(inCompiler)
3457                      {
3458                         char constant[256];
3459                         sourceExp.type = constantExp;
3460                         if(!strcmp(baseClass.dataTypeString, "int"))
3461                            sprintf(constant, "%d",(int)value.data);
3462                         else
3463                            sprintf(constant, "0x%X",(int)value.data);
3464                         sourceExp.constant = CopyString(constant);
3465                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3466                      }
3467
3468                      while(converts.first)
3469                      {
3470                         Conversion convert = converts.first;
3471                         converts.Remove(convert);
3472                         conversions.Add(convert);
3473                      }
3474                      delete type;
3475                      return true;
3476                   }
3477                }
3478             }
3479          }
3480          if(converts.first)
3481             converts.Free(FreeConvert);
3482          delete type;
3483       }
3484    }
3485    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3486       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3487          return true;
3488    return false;
3489 }
3490
3491 public bool ModuleVisibility(Module searchIn, Module searchFor)
3492 {
3493    SubModule subModule;
3494
3495    if(searchFor == searchIn)
3496       return true;
3497
3498    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3499    {
3500       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3501       {
3502          if(ModuleVisibility(subModule.module, searchFor))
3503             return true;
3504       }
3505    }
3506    return false;
3507 }
3508
3509 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3510 {
3511    Module module;
3512
3513    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3514       return true;
3515    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3516       return true;
3517    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3518       return true;
3519
3520    for(module = mainModule.application.allModules.first; module; module = module.next)
3521    {
3522       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3523          return true;
3524    }
3525    return false;
3526 }
3527
3528 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3529 {
3530    Type source;
3531    Type realDest = dest;
3532    Type backupSourceExpType = null;
3533    Expression computedExp = sourceExp;
3534    dest.refCount++;
3535
3536    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3537       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3538    {
3539       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3540       ComputeExpression(computedExp /*sourceExp*/);
3541    }
3542
3543    source = sourceExp.expType;
3544
3545    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3546    {
3547       if(computedExp != sourceExp)
3548       {
3549          FreeExpression(computedExp);
3550          computedExp = sourceExp;
3551       }
3552       FreeType(dest);
3553       return true;
3554    }
3555
3556    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3557    {
3558        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3559        {
3560           Class sourceBase, destBase;
3561           for(sourceBase = source._class.registered;
3562               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3563               sourceBase = sourceBase.base);
3564           for(destBase = dest._class.registered;
3565               destBase && destBase.base && destBase.base.type != systemClass;
3566               destBase = destBase.base);
3567           //if(source._class.registered == dest._class.registered)
3568           if(sourceBase == destBase)
3569           {
3570             if(computedExp != sourceExp)
3571             {
3572                FreeExpression(computedExp);
3573                computedExp = sourceExp;
3574             }
3575             FreeType(dest);
3576             return true;
3577          }
3578       }
3579    }
3580
3581    if(source)
3582    {
3583       OldList * specs;
3584       bool flag = false;
3585       int64 value = MAXINT;
3586
3587       source.refCount++;
3588
3589       if(computedExp.type == constantExp)
3590       {
3591          if(source.isSigned)
3592             value = strtoll(computedExp.constant, null, 0);
3593          else
3594             value = strtoull(computedExp.constant, null, 0);
3595       }
3596       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3597       {
3598          if(source.isSigned)
3599             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3600          else
3601             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3602       }
3603       if(computedExp != sourceExp)
3604       {
3605          FreeExpression(computedExp);
3606          computedExp = sourceExp;
3607       }
3608
3609       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3610          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3611       {
3612          FreeType(source);
3613          source = Type { kind = intType, isSigned = false, refCount = 1 };
3614       }
3615
3616       if(dest.kind == classType)
3617       {
3618          Class _class = dest._class ? dest._class.registered : null;
3619
3620          if(_class && _class.type == unitClass)
3621          {
3622             if(source.kind != classType)
3623             {
3624                Type tempType { };
3625                Type tempDest, tempSource;
3626
3627                for(; _class.base.type != systemClass; _class = _class.base);
3628                tempSource = dest;
3629                tempDest = tempType;
3630
3631                tempType.kind = classType;
3632                if(!_class.symbol)
3633                   _class.symbol = FindClass(_class.fullName);
3634
3635                tempType._class = _class.symbol;
3636                tempType.truth = dest.truth;
3637                if(tempType._class)
3638                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3639
3640                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3641                backupSourceExpType = sourceExp.expType;
3642                sourceExp.expType = dest; dest.refCount++;
3643                //sourceExp.expType = MkClassType(_class.fullName);
3644                flag = true;
3645
3646                delete tempType;
3647             }
3648          }
3649
3650
3651          // Why wasn't there something like this?
3652          if(_class && _class.type == bitClass && source.kind != classType)
3653          {
3654             if(!dest._class.registered.dataType)
3655                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3656             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3657             {
3658                FreeType(source);
3659                FreeType(sourceExp.expType);
3660                source = sourceExp.expType = MkClassType(dest._class.string);
3661                source.refCount++;
3662
3663                //source.kind = classType;
3664                //source._class = dest._class;
3665             }
3666          }
3667
3668          // Adding two enumerations
3669          /*
3670          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3671          {
3672             if(!source._class.registered.dataType)
3673                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3674             if(!dest._class.registered.dataType)
3675                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3676
3677             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3678             {
3679                FreeType(source);
3680                source = sourceExp.expType = MkClassType(dest._class.string);
3681                source.refCount++;
3682
3683                //source.kind = classType;
3684                //source._class = dest._class;
3685             }
3686          }*/
3687
3688          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3689          {
3690             OldList * specs = MkList();
3691             Declarator decl;
3692             char string[1024];
3693
3694             ReadString(string, sourceExp.string);
3695             decl = SpecDeclFromString(string, specs, null);
3696
3697             FreeExpContents(sourceExp);
3698             FreeType(sourceExp.expType);
3699
3700             sourceExp.type = classExp;
3701             sourceExp._classExp.specifiers = specs;
3702             sourceExp._classExp.decl = decl;
3703             sourceExp.expType = dest;
3704             dest.refCount++;
3705
3706             FreeType(source);
3707             FreeType(dest);
3708             if(backupSourceExpType) FreeType(backupSourceExpType);
3709             return true;
3710          }
3711       }
3712       else if(source.kind == classType)
3713       {
3714          Class _class = source._class ? source._class.registered : null;
3715
3716          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3717          {
3718             /*
3719             if(dest.kind != classType)
3720             {
3721                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3722                if(!source._class.registered.dataType)
3723                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3724
3725                FreeType(dest);
3726                dest = MkClassType(source._class.string);
3727                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3728                //   dest = MkClassType(source._class.string);
3729             }
3730             */
3731
3732             if(dest.kind != classType)
3733             {
3734                Type tempType { };
3735                Type tempDest, tempSource;
3736
3737                if(!source._class.registered.dataType)
3738                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3739
3740                for(; _class.base.type != systemClass; _class = _class.base);
3741                tempDest = source;
3742                tempSource = tempType;
3743                tempType.kind = classType;
3744                tempType._class = FindClass(_class.fullName);
3745                tempType.truth = source.truth;
3746                tempType.classObjectType = source.classObjectType;
3747
3748                if(tempType._class)
3749                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3750
3751                // PUT THIS BACK TESTING UNITS?
3752                if(conversions.last)
3753                {
3754                   ((Conversion)(conversions.last)).resultType = dest;
3755                   dest.refCount++;
3756                }
3757
3758                FreeType(sourceExp.expType);
3759                sourceExp.expType = MkClassType(_class.fullName);
3760                sourceExp.expType.truth = source.truth;
3761                sourceExp.expType.classObjectType = source.classObjectType;
3762
3763                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3764
3765                if(!sourceExp.destType)
3766                {
3767                   FreeType(sourceExp.destType);
3768                   sourceExp.destType = sourceExp.expType;
3769                   if(sourceExp.expType)
3770                      sourceExp.expType.refCount++;
3771                }
3772                //flag = true;
3773                //source = _class.dataType;
3774
3775
3776                // TOCHECK: TESTING THIS NEW CODE
3777                if(!_class.dataType)
3778                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3779                FreeType(dest);
3780                dest = MkClassType(source._class.string);
3781                dest.truth = source.truth;
3782                dest.classObjectType = source.classObjectType;
3783
3784                FreeType(source);
3785                source = _class.dataType;
3786                source.refCount++;
3787
3788                delete tempType;
3789             }
3790          }
3791       }
3792
3793       if(!flag)
3794       {
3795          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3796          {
3797             FreeType(source);
3798             FreeType(dest);
3799             return true;
3800          }
3801       }
3802
3803       // Implicit Casts
3804       /*
3805       if(source.kind == classType)
3806       {
3807          Class _class = source._class.registered;
3808          if(_class.type == unitClass)
3809          {
3810             if(!_class.dataType)
3811                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3812             source = _class.dataType;
3813          }
3814       }*/
3815
3816       if(dest.kind == classType)
3817       {
3818          Class _class = dest._class ? dest._class.registered : null;
3819          bool fittingValue = false;
3820          if(_class && _class.type == enumClass)
3821          {
3822             Class enumClass = eSystem_FindClass(privateModule, "enum");
3823             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3824             if(c && value >= 0 && value <= c.largest)
3825                fittingValue = true;
3826          }
3827
3828          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3829             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3830          {
3831             if(_class.type == normalClass || _class.type == noHeadClass)
3832             {
3833                Expression newExp { };
3834                *newExp = *sourceExp;
3835                if(sourceExp.destType) sourceExp.destType.refCount++;
3836                if(sourceExp.expType)  sourceExp.expType.refCount++;
3837                sourceExp.type = castExp;
3838                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3839                sourceExp.cast.exp = newExp;
3840                FreeType(sourceExp.expType);
3841                sourceExp.expType = null;
3842                ProcessExpressionType(sourceExp);
3843
3844                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3845                if(!inCompiler)
3846                {
3847                   FreeType(sourceExp.expType);
3848                   sourceExp.expType = dest;
3849                }
3850
3851                FreeType(source);
3852                if(inCompiler) FreeType(dest);
3853
3854                if(backupSourceExpType) FreeType(backupSourceExpType);
3855                return true;
3856             }
3857
3858             if(!_class.dataType)
3859                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3860             FreeType(dest);
3861             dest = _class.dataType;
3862             dest.refCount++;
3863          }
3864
3865          // Accept lower precision types for units, since we want to keep the unit type
3866          if(dest.kind == doubleType &&
3867             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3868              source.kind == charType || source.kind == _BoolType))
3869          {
3870             specs = MkListOne(MkSpecifier(DOUBLE));
3871          }
3872          else if(dest.kind == floatType &&
3873             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3874             source.kind == _BoolType || source.kind == doubleType))
3875          {
3876             specs = MkListOne(MkSpecifier(FLOAT));
3877          }
3878          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3879             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3880          {
3881             specs = MkList();
3882             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3883             ListAdd(specs, MkSpecifier(INT64));
3884          }
3885          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3886             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3887          {
3888             specs = MkList();
3889             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3890             ListAdd(specs, MkSpecifier(INT));
3891          }
3892          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3893             source.kind == floatType || source.kind == doubleType))
3894          {
3895             specs = MkList();
3896             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3897             ListAdd(specs, MkSpecifier(SHORT));
3898          }
3899          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3900             source.kind == floatType || source.kind == doubleType))
3901          {
3902             specs = MkList();
3903             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3904             ListAdd(specs, MkSpecifier(CHAR));
3905          }
3906          else
3907          {
3908             FreeType(source);
3909             FreeType(dest);
3910             if(backupSourceExpType)
3911             {
3912                // Failed to convert: revert previous exp type
3913                if(sourceExp.expType) FreeType(sourceExp.expType);
3914                sourceExp.expType = backupSourceExpType;
3915             }
3916             return false;
3917          }
3918       }
3919       else if(dest.kind == doubleType &&
3920          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3921           source.kind == _BoolType || source.kind == charType))
3922       {
3923          specs = MkListOne(MkSpecifier(DOUBLE));
3924       }
3925       else if(dest.kind == floatType &&
3926          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3927       {
3928          specs = MkListOne(MkSpecifier(FLOAT));
3929       }
3930       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3931          (value == 1 || value == 0))
3932       {
3933          specs = MkList();
3934          ListAdd(specs, MkSpecifier(BOOL));
3935       }
3936       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3937          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3938       {
3939          specs = MkList();
3940          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3941          ListAdd(specs, MkSpecifier(CHAR));
3942       }
3943       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3944          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3945       {
3946          specs = MkList();
3947          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3948          ListAdd(specs, MkSpecifier(SHORT));
3949       }
3950       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3951       {
3952          specs = MkList();
3953          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3954          ListAdd(specs, MkSpecifier(INT));
3955       }
3956       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3957       {
3958          specs = MkList();
3959          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3960          ListAdd(specs, MkSpecifier(INT64));
3961       }
3962       else if(dest.kind == enumType &&
3963          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3964       {
3965          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3966       }
3967       else
3968       {
3969          FreeType(source);
3970          FreeType(dest);
3971          if(backupSourceExpType)
3972          {
3973             // Failed to convert: revert previous exp type
3974             if(sourceExp.expType) FreeType(sourceExp.expType);
3975             sourceExp.expType = backupSourceExpType;
3976          }
3977          return false;
3978       }
3979
3980       if(!flag && !sourceExp.opDestType)
3981       {
3982          Expression newExp { };
3983          *newExp = *sourceExp;
3984          newExp.prev = null;
3985          newExp.next = null;
3986          if(sourceExp.destType) sourceExp.destType.refCount++;
3987          if(sourceExp.expType)  sourceExp.expType.refCount++;
3988
3989          sourceExp.type = castExp;
3990          if(realDest.kind == classType)
3991          {
3992             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3993             FreeList(specs, FreeSpecifier);
3994          }
3995          else
3996             sourceExp.cast.typeName = MkTypeName(specs, null);
3997          if(newExp.type == opExp)
3998          {
3999             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4000          }
4001          else
4002             sourceExp.cast.exp = newExp;
4003
4004          FreeType(sourceExp.expType);
4005          sourceExp.expType = null;
4006          ProcessExpressionType(sourceExp);
4007       }
4008       else
4009          FreeList(specs, FreeSpecifier);
4010
4011       FreeType(dest);
4012       FreeType(source);
4013       if(backupSourceExpType) FreeType(backupSourceExpType);
4014
4015       return true;
4016    }
4017    else
4018    {
4019       if(computedExp != sourceExp)
4020       {
4021          FreeExpression(computedExp);
4022          computedExp = sourceExp;
4023       }
4024
4025       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4026       if(sourceExp.type == identifierExp)
4027       {
4028          Identifier id = sourceExp.identifier;
4029          if(dest.kind == classType)
4030          {
4031             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4032             {
4033                Class _class = dest._class.registered;
4034                Class enumClass = eSystem_FindClass(privateModule, "enum");
4035                if(enumClass)
4036                {
4037                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4038                   {
4039                      NamedLink value;
4040                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4041                      for(value = e.values.first; value; value = value.next)
4042                      {
4043                         if(!strcmp(value.name, id.string))
4044                            break;
4045                      }
4046                      if(value)
4047                      {
4048                         FreeExpContents(sourceExp);
4049                         FreeType(sourceExp.expType);
4050
4051                         sourceExp.isConstant = true;
4052                         sourceExp.expType = MkClassType(_class.fullName);
4053                         //if(inCompiler)
4054                         {
4055                            char constant[256];
4056                            sourceExp.type = constantExp;
4057                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
4058                               sprintf(constant, "%d", (int) value.data);
4059                            else
4060                               sprintf(constant, "0x%X", (int) value.data);
4061                            sourceExp.constant = CopyString(constant);
4062                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4063                         }
4064                         FreeType(dest);
4065                         return true;
4066                      }
4067                   }
4068                }
4069             }
4070          }
4071
4072          // Loop through all enum classes
4073          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4074          {
4075             FreeType(dest);
4076             return true;
4077          }
4078       }
4079       FreeType(dest);
4080    }
4081    return false;
4082 }
4083
4084 #define TERTIARY(o, name, m, t, p) \
4085    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4086    {                                                              \
4087       exp.type = constantExp;                                    \
4088       exp.string = p(op1.m ? op2.m : op3.m);                     \
4089       if(!exp.expType) \
4090          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4091       return true;                                                \
4092    }
4093
4094 #define BINARY(o, name, m, t, p) \
4095    static bool name(Expression exp, Operand op1, Operand op2)   \
4096    {                                                              \
4097       t value2 = op2.m;                                           \
4098       exp.type = constantExp;                                    \
4099       exp.string = p((t)(op1.m o value2));                     \
4100       if(!exp.expType) \
4101          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4102       return true;                                                \
4103    }
4104
4105 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4106    static bool name(Expression exp, Operand op1, Operand op2)   \
4107    {                                                              \
4108       t value2 = op2.m;                                           \
4109       exp.type = constantExp;                                    \
4110       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4111       if(!exp.expType) \
4112          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4113       return true;                                                \
4114    }
4115
4116 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4117    static bool name(Expression exp, Operand op1, Operand op2)   \
4118    {                                                              \
4119       t value2 = op2.m;                                           \
4120       exp.type = constantExp;                                    \
4121       exp.string = p(op1.m o value2);             \
4122       if(!exp.expType) \
4123          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4124       return true;                                                \
4125    }
4126
4127 #define UNARY(o, name, m, t, p) \
4128    static bool name(Expression exp, Operand op1)                \
4129    {                                                              \
4130       exp.type = constantExp;                                    \
4131       exp.string = p((t)(o op1.m));                                   \
4132       if(!exp.expType) \
4133          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4134       return true;                                                \
4135    }
4136
4137 #define OPERATOR_ALL(macro, o, name) \
4138    macro(o, Int##name, i, int, PrintInt) \
4139    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4140    macro(o, Int64##name, i64, int64, PrintInt64) \
4141    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4142    macro(o, Short##name, s, short, PrintShort) \
4143    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4144    macro(o, Char##name, c, char, PrintChar) \
4145    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4146    macro(o, Float##name, f, float, PrintFloat) \
4147    macro(o, Double##name, d, double, PrintDouble)
4148
4149 #define OPERATOR_INTTYPES(macro, o, name) \
4150    macro(o, Int##name, i, int, PrintInt) \
4151    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4152    macro(o, Int64##name, i64, int64, PrintInt64) \
4153    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4154    macro(o, Short##name, s, short, PrintShort) \
4155    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4156    macro(o, Char##name, c, char, PrintChar) \
4157    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4158
4159 #define OPERATOR_REALTYPES(macro, o, name) \
4160    macro(o, Float##name, f, float, PrintFloat) \
4161    macro(o, Double##name, d, double, PrintDouble)
4162
4163 // binary arithmetic
4164 OPERATOR_ALL(BINARY, +, Add)
4165 OPERATOR_ALL(BINARY, -, Sub)
4166 OPERATOR_ALL(BINARY, *, Mul)
4167 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4168 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4169 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4170
4171 // unary arithmetic
4172 OPERATOR_ALL(UNARY, -, Neg)
4173
4174 // unary arithmetic increment and decrement
4175 OPERATOR_ALL(UNARY, ++, Inc)
4176 OPERATOR_ALL(UNARY, --, Dec)
4177
4178 // binary arithmetic assignment
4179 OPERATOR_ALL(BINARY, =, Asign)
4180 OPERATOR_ALL(BINARY, +=, AddAsign)
4181 OPERATOR_ALL(BINARY, -=, SubAsign)
4182 OPERATOR_ALL(BINARY, *=, MulAsign)
4183 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4184 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4185 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4186
4187 // binary bitwise
4188 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4189 OPERATOR_INTTYPES(BINARY, |, BitOr)
4190 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4191 OPERATOR_INTTYPES(BINARY, <<, LShift)
4192 OPERATOR_INTTYPES(BINARY, >>, RShift)
4193
4194 // unary bitwise
4195 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4196
4197 // binary bitwise assignment
4198 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4199 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4200 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4201 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4202 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4203
4204 // unary logical negation
4205 OPERATOR_INTTYPES(UNARY, !, Not)
4206
4207 // binary logical equality
4208 OPERATOR_ALL(BINARY, ==, Equ)
4209 OPERATOR_ALL(BINARY, !=, Nqu)
4210
4211 // binary logical
4212 OPERATOR_ALL(BINARY, &&, And)
4213 OPERATOR_ALL(BINARY, ||, Or)
4214
4215 // binary logical relational
4216 OPERATOR_ALL(BINARY, >, Grt)
4217 OPERATOR_ALL(BINARY, <, Sma)
4218 OPERATOR_ALL(BINARY, >=, GrtEqu)
4219 OPERATOR_ALL(BINARY, <=, SmaEqu)
4220
4221 // tertiary condition operator
4222 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4223
4224 //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
4225 #define OPERATOR_TABLE_ALL(name, type) \
4226     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4227                           type##Neg, \
4228                           type##Inc, type##Dec, \
4229                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4230                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4231                           type##BitNot, \
4232                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4233                           type##Not, \
4234                           type##Equ, type##Nqu, \
4235                           type##And, type##Or, \
4236                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4237                         }; \
4238
4239 #define OPERATOR_TABLE_INTTYPES(name, type) \
4240     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4241                           type##Neg, \
4242                           type##Inc, type##Dec, \
4243                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4244                           null, null, null, null, null, \
4245                           null, \
4246                           null, null, null, null, null, \
4247                           null, \
4248                           type##Equ, type##Nqu, \
4249                           type##And, type##Or, \
4250                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4251                         }; \
4252
4253 OPERATOR_TABLE_ALL(int, Int)
4254 OPERATOR_TABLE_ALL(uint, UInt)
4255 OPERATOR_TABLE_ALL(int64, Int64)
4256 OPERATOR_TABLE_ALL(uint64, UInt64)
4257 OPERATOR_TABLE_ALL(short, Short)
4258 OPERATOR_TABLE_ALL(ushort, UShort)
4259 OPERATOR_TABLE_INTTYPES(float, Float)
4260 OPERATOR_TABLE_INTTYPES(double, Double)
4261 OPERATOR_TABLE_ALL(char, Char)
4262 OPERATOR_TABLE_ALL(uchar, UChar)
4263
4264 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4265 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4266 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4267 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4268 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4269 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4270 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4271 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4272
4273 public void ReadString(char * output,  char * string)
4274 {
4275    int len = strlen(string);
4276    int c,d = 0;
4277    bool quoted = false, escaped = false;
4278    for(c = 0; c<len; c++)
4279    {
4280       char ch = string[c];
4281       if(escaped)
4282       {
4283          switch(ch)
4284          {
4285             case 'n': output[d] = '\n'; break;
4286             case 't': output[d] = '\t'; break;
4287             case 'a': output[d] = '\a'; break;
4288             case 'b': output[d] = '\b'; break;
4289             case 'f': output[d] = '\f'; break;
4290             case 'r': output[d] = '\r'; break;
4291             case 'v': output[d] = '\v'; break;
4292             case '\\': output[d] = '\\'; break;
4293             case '\"': output[d] = '\"'; break;
4294             case '\'': output[d] = '\''; break;
4295             default: output[d] = ch;
4296          }
4297          d++;
4298          escaped = false;
4299       }
4300       else
4301       {
4302          if(ch == '\"')
4303             quoted ^= true;
4304          else if(quoted)
4305          {
4306             if(ch == '\\')
4307                escaped = true;
4308             else
4309                output[d++] = ch;
4310          }
4311       }
4312    }
4313    output[d] = '\0';
4314 }
4315
4316 // String Unescape Copy
4317
4318 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4319 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4320 public int UnescapeString(char * d, char * s, int len)
4321 {
4322    int j = 0, k = 0;
4323    char ch;
4324    while(j < len && (ch = s[j]))
4325    {
4326       switch(ch)
4327       {
4328          case '\\':
4329             switch((ch = s[++j]))
4330             {
4331                case 'n': d[k] = '\n'; break;
4332                case 't': d[k] = '\t'; break;
4333                case 'a': d[k] = '\a'; break;
4334                case 'b': d[k] = '\b'; break;
4335                case 'f': d[k] = '\f'; break;
4336                case 'r': d[k] = '\r'; break;
4337                case 'v': d[k] = '\v'; break;
4338                case '\\': d[k] = '\\'; break;
4339                case '\"': d[k] = '\"'; break;
4340                case '\'': d[k] = '\''; break;
4341                default: d[k] = '\\'; d[k] = ch;
4342             }
4343             break;
4344          default:
4345             d[k] = ch;
4346       }
4347       j++, k++;
4348    }
4349    d[k] = '\0';
4350    return k;
4351 }
4352
4353 public char * OffsetEscapedString(char * s, int len, int offset)
4354 {
4355    char ch;
4356    int j = 0, k = 0;
4357    while(j < len && k < offset && (ch = s[j]))
4358    {
4359       if(ch == '\\') ++j;
4360       j++, k++;
4361    }
4362    return (k == offset) ? s + j : null;
4363 }
4364
4365 public Operand GetOperand(Expression exp)
4366 {
4367    Operand op { };
4368    Type type = exp.expType;
4369    if(type)
4370    {
4371       while(type.kind == classType &&
4372          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4373       {
4374          if(!type._class.registered.dataType)
4375             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4376          type = type._class.registered.dataType;
4377
4378       }
4379       if(exp.type == stringExp && op.kind == pointerType)
4380       {
4381          op.ui64 = (uint64)exp.string;
4382          op.kind = pointerType;
4383          op.ops = uint64Ops;
4384       }
4385       else if(exp.isConstant && exp.type == constantExp)
4386       {
4387          op.kind = type.kind;
4388          op.type = exp.expType;
4389
4390          switch(op.kind)
4391          {
4392             case _BoolType:
4393             case charType:
4394             {
4395                if(exp.constant[0] == '\'')
4396                {
4397                   op.c = exp.constant[1];
4398                   op.ops = charOps;
4399                }
4400                else if(type.isSigned)
4401                {
4402                   op.c = (char)strtol(exp.constant, null, 0);
4403                   op.ops = charOps;
4404                }
4405                else
4406                {
4407                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4408                   op.ops = ucharOps;
4409                }
4410                break;
4411             }
4412             case shortType:
4413                if(type.isSigned)
4414                {
4415                   op.s = (short)strtol(exp.constant, null, 0);
4416                   op.ops = shortOps;
4417                }
4418                else
4419                {
4420                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4421                   op.ops = ushortOps;
4422                }
4423                break;
4424             case intType:
4425             case longType:
4426                if(type.isSigned)
4427                {
4428                   op.i = (int)strtol(exp.constant, null, 0);
4429                   op.ops = intOps;
4430                }
4431                else
4432                {
4433                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4434                   op.ops = uintOps;
4435                }
4436                op.kind = intType;
4437                break;
4438             case int64Type:
4439                if(type.isSigned)
4440                {
4441                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4442                   op.ops = int64Ops;
4443                }
4444                else
4445                {
4446                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4447                   op.ops = uint64Ops;
4448                }
4449                op.kind = int64Type;
4450                break;
4451             case intPtrType:
4452                if(type.isSigned)
4453                {
4454                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4455                   op.ops = int64Ops;
4456                }
4457                else
4458                {
4459                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4460                   op.ops = uint64Ops;
4461                }
4462                op.kind = int64Type;
4463                break;
4464             case intSizeType:
4465                if(type.isSigned)
4466                {
4467                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4468                   op.ops = int64Ops;
4469                }
4470                else
4471                {
4472                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4473                   op.ops = uint64Ops;
4474                }
4475                op.kind = int64Type;
4476                break;
4477             case floatType:
4478                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4479                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4480                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4481                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4482                else
4483                   op.f = (float)strtod(exp.constant, null);
4484                op.ops = floatOps;
4485                break;
4486             case doubleType:
4487                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4488                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4489                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4490                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4491                else
4492                   op.d = (double)strtod(exp.constant, null);
4493                op.ops = doubleOps;
4494                break;
4495             //case classType:    For when we have operator overloading...
4496             // Pointer additions
4497             //case functionType:
4498             case arrayType:
4499             case pointerType:
4500             case classType:
4501                op.ui64 = _strtoui64(exp.constant, null, 0);
4502                op.kind = pointerType;
4503                op.ops = uint64Ops;
4504                // op.ptrSize =
4505                break;
4506          }
4507       }
4508    }
4509    return op;
4510 }
4511
4512 static __attribute__((unused)) void UnusedFunction()
4513 {
4514    int a;
4515    a.OnGetString(0,0,0);
4516 }
4517 default:
4518 extern int __ecereVMethodID_class_OnGetString;
4519 public:
4520
4521 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4522 {
4523    DataMember dataMember;
4524    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4525    {
4526       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4527          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4528       else
4529       {
4530          Expression exp { };
4531          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4532          Type type;
4533          void * ptr = inst.data + dataMember.offset + offset;
4534          char * result = null;
4535          exp.loc = member.loc = inst.loc;
4536          ((Identifier)member.identifiers->first).loc = inst.loc;
4537
4538          if(!dataMember.dataType)
4539             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4540          type = dataMember.dataType;
4541          if(type.kind == classType)
4542          {
4543             Class _class = type._class.registered;
4544             if(_class.type == enumClass)
4545             {
4546                Class enumClass = eSystem_FindClass(privateModule, "enum");
4547                if(enumClass)
4548                {
4549                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4550                   NamedLink item;
4551                   for(item = e.values.first; item; item = item.next)
4552                   {
4553                      if((int)item.data == *(int *)ptr)
4554                      {
4555                         result = item.name;
4556                         break;
4557                      }
4558                   }
4559                   if(result)
4560                   {
4561                      exp.identifier = MkIdentifier(result);
4562                      exp.type = identifierExp;
4563                      exp.destType = MkClassType(_class.fullName);
4564                      ProcessExpressionType(exp);
4565                   }
4566                }
4567             }
4568             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4569             {
4570                if(!_class.dataType)
4571                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4572                type = _class.dataType;
4573             }
4574          }
4575          if(!result)
4576          {
4577             switch(type.kind)
4578             {
4579                case floatType:
4580                {
4581                   FreeExpContents(exp);
4582
4583                   exp.constant = PrintFloat(*(float*)ptr);
4584                   exp.type = constantExp;
4585                   break;
4586                }
4587                case doubleType:
4588                {
4589                   FreeExpContents(exp);
4590
4591                   exp.constant = PrintDouble(*(double*)ptr);
4592                   exp.type = constantExp;
4593                   break;
4594                }
4595                case intType:
4596                {
4597                   FreeExpContents(exp);
4598
4599                   exp.constant = PrintInt(*(int*)ptr);
4600                   exp.type = constantExp;
4601                   break;
4602                }
4603                case int64Type:
4604                {
4605                   FreeExpContents(exp);
4606
4607                   exp.constant = PrintInt64(*(int64*)ptr);
4608                   exp.type = constantExp;
4609                   break;
4610                }
4611                case intPtrType:
4612                {
4613                   FreeExpContents(exp);
4614                   // TODO: This should probably use proper type
4615                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4616                   exp.type = constantExp;
4617                   break;
4618                }
4619                case intSizeType:
4620                {
4621                   FreeExpContents(exp);
4622                   // TODO: This should probably use proper type
4623                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4624                   exp.type = constantExp;
4625                   break;
4626                }
4627                default:
4628                   Compiler_Error($"Unhandled type populating instance\n");
4629             }
4630          }
4631          ListAdd(memberList, member);
4632       }
4633
4634       if(parentDataMember.type == unionMember)
4635          break;
4636    }
4637 }
4638
4639 void PopulateInstance(Instantiation inst)
4640 {
4641    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4642    Class _class = classSym.registered;
4643    DataMember dataMember;
4644    OldList * memberList = MkList();
4645    // Added this check and ->Add to prevent memory leaks on bad code
4646    if(!inst.members)
4647       inst.members = MkListOne(MkMembersInitList(memberList));
4648    else
4649       inst.members->Add(MkMembersInitList(memberList));
4650    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4651    {
4652       if(!dataMember.isProperty)
4653       {
4654          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4655             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4656          else
4657          {
4658             Expression exp { };
4659             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4660             Type type;
4661             void * ptr = inst.data + dataMember.offset;
4662             char * result = null;
4663
4664             exp.loc = member.loc = inst.loc;
4665             ((Identifier)member.identifiers->first).loc = inst.loc;
4666
4667             if(!dataMember.dataType)
4668                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4669             type = dataMember.dataType;
4670             if(type.kind == classType)
4671             {
4672                Class _class = type._class.registered;
4673                if(_class.type == enumClass)
4674                {
4675                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4676                   if(enumClass)
4677                   {
4678                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4679                      NamedLink item;
4680                      for(item = e.values.first; item; item = item.next)
4681                      {
4682                         if((int)item.data == *(int *)ptr)
4683                         {
4684                            result = item.name;
4685                            break;
4686                         }
4687                      }
4688                   }
4689                   if(result)
4690                   {
4691                      exp.identifier = MkIdentifier(result);
4692                      exp.type = identifierExp;
4693                      exp.destType = MkClassType(_class.fullName);
4694                      ProcessExpressionType(exp);
4695                   }
4696                }
4697                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4698                {
4699                   if(!_class.dataType)
4700                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4701                   type = _class.dataType;
4702                }
4703             }
4704             if(!result)
4705             {
4706                switch(type.kind)
4707                {
4708                   case floatType:
4709                   {
4710                      exp.constant = PrintFloat(*(float*)ptr);
4711                      exp.type = constantExp;
4712                      break;
4713                   }
4714                   case doubleType:
4715                   {
4716                      exp.constant = PrintDouble(*(double*)ptr);
4717                      exp.type = constantExp;
4718                      break;
4719                   }
4720                   case intType:
4721                   {
4722                      exp.constant = PrintInt(*(int*)ptr);
4723                      exp.type = constantExp;
4724                      break;
4725                   }
4726                   case int64Type:
4727                   {
4728                      exp.constant = PrintInt64(*(int64*)ptr);
4729                      exp.type = constantExp;
4730                      break;
4731                   }
4732                   case intPtrType:
4733                   {
4734                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4735                      exp.type = constantExp;
4736                      break;
4737                   }
4738                   default:
4739                      Compiler_Error($"Unhandled type populating instance\n");
4740                }
4741             }
4742             ListAdd(memberList, member);
4743          }
4744       }
4745    }
4746 }
4747
4748 void ComputeInstantiation(Expression exp)
4749 {
4750    Instantiation inst = exp.instance;
4751    MembersInit members;
4752    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4753    Class _class = classSym ? classSym.registered : null;
4754    DataMember curMember = null;
4755    Class curClass = null;
4756    DataMember subMemberStack[256];
4757    int subMemberStackPos = 0;
4758    uint64 bits = 0;
4759
4760    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4761    {
4762       // Don't recompute the instantiation...
4763       // Non Simple classes will have become constants by now
4764       if(inst.data)
4765          return;
4766
4767       if(_class.type == normalClass || _class.type == noHeadClass)
4768       {
4769          inst.data = (byte *)eInstance_New(_class);
4770          if(_class.type == normalClass)
4771             ((Instance)inst.data)._refCount++;
4772       }
4773       else
4774          inst.data = new0 byte[_class.structSize];
4775    }
4776
4777    if(inst.members)
4778    {
4779       for(members = inst.members->first; members; members = members.next)
4780       {
4781          switch(members.type)
4782          {
4783             case dataMembersInit:
4784             {
4785                if(members.dataMembers)
4786                {
4787                   MemberInit member;
4788                   for(member = members.dataMembers->first; member; member = member.next)
4789                   {
4790                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4791                      bool found = false;
4792
4793                      Property prop = null;
4794                      DataMember dataMember = null;
4795                      Method method = null;
4796                      uint dataMemberOffset;
4797
4798                      if(!ident)
4799                      {
4800                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4801                         if(curMember)
4802                         {
4803                            if(curMember.isProperty)
4804                               prop = (Property)curMember;
4805                            else
4806                            {
4807                               dataMember = curMember;
4808
4809                               // CHANGED THIS HERE
4810                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4811
4812                               // 2013/17/29 -- It seems that this was missing here!
4813                               if(_class.type == normalClass)
4814                                  dataMemberOffset += _class.base.structSize;
4815                               // dataMemberOffset = dataMember.offset;
4816                            }
4817                            found = true;
4818                         }
4819                      }
4820                      else
4821                      {
4822                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4823                         if(prop)
4824                         {
4825                            found = true;
4826                            if(prop.memberAccess == publicAccess)
4827                            {
4828                               curMember = (DataMember)prop;
4829                               curClass = prop._class;
4830                            }
4831                         }
4832                         else
4833                         {
4834                            DataMember _subMemberStack[256];
4835                            int _subMemberStackPos = 0;
4836
4837                            // FILL MEMBER STACK
4838                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4839
4840                            if(dataMember)
4841                            {
4842                               found = true;
4843                               if(dataMember.memberAccess == publicAccess)
4844                               {
4845                                  curMember = dataMember;
4846                                  curClass = dataMember._class;
4847                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4848                                  subMemberStackPos = _subMemberStackPos;
4849                               }
4850                            }
4851                         }
4852                      }
4853
4854                      if(found && member.initializer && member.initializer.type == expInitializer)
4855                      {
4856                         Expression value = member.initializer.exp;
4857                         Type type = null;
4858                         bool deepMember = false;
4859                         if(prop)
4860                         {
4861                            type = prop.dataType;
4862                         }
4863                         else if(dataMember)
4864                         {
4865                            if(!dataMember.dataType)
4866                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4867
4868                            type = dataMember.dataType;
4869                         }
4870
4871                         if(ident && ident.next)
4872                         {
4873                            deepMember = true;
4874
4875                            // for(; ident && type; ident = ident.next)
4876                            for(ident = ident.next; ident && type; ident = ident.next)
4877                            {
4878                               if(type.kind == classType)
4879                               {
4880                                  prop = eClass_FindProperty(type._class.registered,
4881                                     ident.string, privateModule);
4882                                  if(prop)
4883                                     type = prop.dataType;
4884                                  else
4885                                  {
4886                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4887                                        ident.string, &dataMemberOffset, privateModule, null, null);
4888                                     if(dataMember)
4889                                        type = dataMember.dataType;
4890                                  }
4891                               }
4892                               else if(type.kind == structType || type.kind == unionType)
4893                               {
4894                                  Type memberType;
4895                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4896                                  {
4897                                     if(!strcmp(memberType.name, ident.string))
4898                                     {
4899                                        type = memberType;
4900                                        break;
4901                                     }
4902                                  }
4903                               }
4904                            }
4905                         }
4906                         if(value)
4907                         {
4908                            FreeType(value.destType);
4909                            value.destType = type;
4910                            if(type) type.refCount++;
4911                            ComputeExpression(value);
4912                         }
4913                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4914                         {
4915                            if(type.kind == classType)
4916                            {
4917                               Class _class = type._class.registered;
4918                               if(_class.type == bitClass || _class.type == unitClass ||
4919                                  _class.type == enumClass)
4920                               {
4921                                  if(!_class.dataType)
4922                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4923                                  type = _class.dataType;
4924                               }
4925                            }
4926
4927                            if(dataMember)
4928                            {
4929                               void * ptr = inst.data + dataMemberOffset;
4930
4931                               if(value.type == constantExp)
4932                               {
4933                                  switch(type.kind)
4934                                  {
4935                                     case intType:
4936                                     {
4937                                        GetInt(value, (int*)ptr);
4938                                        break;
4939                                     }
4940                                     case int64Type:
4941                                     {
4942                                        GetInt64(value, (int64*)ptr);
4943                                        break;
4944                                     }
4945                                     case intPtrType:
4946                                     {
4947                                        GetIntPtr(value, (intptr*)ptr);
4948                                        break;
4949                                     }
4950                                     case intSizeType:
4951                                     {
4952                                        GetIntSize(value, (intsize*)ptr);
4953                                        break;
4954                                     }
4955                                     case floatType:
4956                                     {
4957                                        GetFloat(value, (float*)ptr);
4958                                        break;
4959                                     }
4960                                     case doubleType:
4961                                     {
4962                                        GetDouble(value, (double *)ptr);
4963                                        break;
4964                                     }
4965                                  }
4966                               }
4967                               else if(value.type == instanceExp)
4968                               {
4969                                  if(type.kind == classType)
4970                                  {
4971                                     Class _class = type._class.registered;
4972                                     if(_class.type == structClass)
4973                                     {
4974                                        ComputeTypeSize(type);
4975                                        if(value.instance.data)
4976                                           memcpy(ptr, value.instance.data, type.size);
4977                                     }
4978                                  }
4979                               }
4980                            }
4981                            else if(prop)
4982                            {
4983                               if(value.type == instanceExp && value.instance.data)
4984                               {
4985                                  if(type.kind == classType)
4986                                  {
4987                                     Class _class = type._class.registered;
4988                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4989                                     {
4990                                        void (*Set)(void *, void *) = (void *)prop.Set;
4991                                        Set(inst.data, value.instance.data);
4992                                        PopulateInstance(inst);
4993                                     }
4994                                  }
4995                               }
4996                               else if(value.type == constantExp)
4997                               {
4998                                  switch(type.kind)
4999                                  {
5000                                     case doubleType:
5001                                     {
5002                                        void (*Set)(void *, double) = (void *)prop.Set;
5003                                        Set(inst.data, strtod(value.constant, null) );
5004                                        break;
5005                                     }
5006                                     case floatType:
5007                                     {
5008                                        void (*Set)(void *, float) = (void *)prop.Set;
5009                                        Set(inst.data, (float)(strtod(value.constant, null)));
5010                                        break;
5011                                     }
5012                                     case intType:
5013                                     {
5014                                        void (*Set)(void *, int) = (void *)prop.Set;
5015                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5016                                        break;
5017                                     }
5018                                     case int64Type:
5019                                     {
5020                                        void (*Set)(void *, int64) = (void *)prop.Set;
5021                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5022                                        break;
5023                                     }
5024                                     case intPtrType:
5025                                     {
5026                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5027                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5028                                        break;
5029                                     }
5030                                     case intSizeType:
5031                                     {
5032                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5033                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5034                                        break;
5035                                     }
5036                                  }
5037                               }
5038                               else if(value.type == stringExp)
5039                               {
5040                                  char temp[1024];
5041                                  ReadString(temp, value.string);
5042                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5043                               }
5044                            }
5045                         }
5046                         else if(!deepMember && type && _class.type == unitClass)
5047                         {
5048                            if(prop)
5049                            {
5050                               // Only support converting units to units for now...
5051                               if(value.type == constantExp)
5052                               {
5053                                  if(type.kind == classType)
5054                                  {
5055                                     Class _class = type._class.registered;
5056                                     if(_class.type == unitClass)
5057                                     {
5058                                        if(!_class.dataType)
5059                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5060                                        type = _class.dataType;
5061                                     }
5062                                  }
5063                                  // TODO: Assuming same base type for units...
5064                                  switch(type.kind)
5065                                  {
5066                                     case floatType:
5067                                     {
5068                                        float fValue;
5069                                        float (*Set)(float) = (void *)prop.Set;
5070                                        GetFloat(member.initializer.exp, &fValue);
5071                                        exp.constant = PrintFloat(Set(fValue));
5072                                        exp.type = constantExp;
5073                                        break;
5074                                     }
5075                                     case doubleType:
5076                                     {
5077                                        double dValue;
5078                                        double (*Set)(double) = (void *)prop.Set;
5079                                        GetDouble(member.initializer.exp, &dValue);
5080                                        exp.constant = PrintDouble(Set(dValue));
5081                                        exp.type = constantExp;
5082                                        break;
5083                                     }
5084                                  }
5085                               }
5086                            }
5087                         }
5088                         else if(!deepMember && type && _class.type == bitClass)
5089                         {
5090                            if(prop)
5091                            {
5092                               if(value.type == instanceExp && value.instance.data)
5093                               {
5094                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5095                                  bits = Set(value.instance.data);
5096                               }
5097                               else if(value.type == constantExp)
5098                               {
5099                               }
5100                            }
5101                            else if(dataMember)
5102                            {
5103                               BitMember bitMember = (BitMember) dataMember;
5104                               Type type;
5105                               uint64 part;
5106                               bits = (bits & ~bitMember.mask);
5107                               if(!bitMember.dataType)
5108                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5109                               type = bitMember.dataType;
5110                               if(type.kind == classType && type._class && type._class.registered)
5111                               {
5112                                  if(!type._class.registered.dataType)
5113                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5114                                  type = type._class.registered.dataType;
5115                               }
5116                               switch(type.kind)
5117                               {
5118                                  case _BoolType:
5119                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5120                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (uint16 *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5121                                  case intType:
5122                                  case longType:       { uint v; type.isSigned ? GetInt(value, (uint *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5123                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (uint64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5124                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (uintptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5125                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (uintsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5126                               }
5127                               bits |= part << bitMember.pos;
5128                            }
5129                         }
5130                      }
5131                      else
5132                      {
5133                         if(_class && _class.type == unitClass)
5134                         {
5135                            ComputeExpression(member.initializer.exp);
5136                            exp.constant = member.initializer.exp.constant;
5137                            exp.type = constantExp;
5138
5139                            member.initializer.exp.constant = null;
5140                         }
5141                      }
5142                   }
5143                }
5144                break;
5145             }
5146          }
5147       }
5148    }
5149    if(_class && _class.type == bitClass)
5150    {
5151       exp.constant = PrintHexUInt(bits);
5152       exp.type = constantExp;
5153    }
5154    if(exp.type != instanceExp)
5155    {
5156       FreeInstance(inst);
5157    }
5158 }
5159
5160 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5161 {
5162    bool result = false;
5163    switch(kind)
5164    {
5165       case shortType:
5166          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5167             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5168          break;
5169       case intType:
5170       case longType:
5171          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5172             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5173          break;
5174       case int64Type:
5175          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5176             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5177             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5178          break;
5179       case floatType:
5180          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5181             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5182             result = GetOpFloat(op, &op.f);
5183          break;
5184       case doubleType:
5185          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5186             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5187             result = GetOpDouble(op, &op.d);
5188          break;
5189       case pointerType:
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 = GetOpUIntPtr(op, &op.ui64);
5193          break;
5194       case enumType:
5195          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5196             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5197             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5198          break;
5199       case intPtrType:
5200          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5201             result = isSigned ? GetOpIntPtr(op, &op.i64) : GetOpUIntPtr(op, &op.i64);
5202          break;
5203       case intSizeType:
5204          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5205             result = isSigned ? GetOpIntSize(op, &op.ui64) : GetOpUIntSize(op, &op.ui64);
5206          break;
5207    }
5208    return result;
5209 }
5210
5211 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5212 {
5213    if(exp.op.op == SIZEOF)
5214    {
5215       FreeExpContents(exp);
5216       exp.type = constantExp;
5217       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5218    }
5219    else
5220    {
5221       if(!exp.op.exp1)
5222       {
5223          switch(exp.op.op)
5224          {
5225             // unary arithmetic
5226             case '+':
5227             {
5228                // Provide default unary +
5229                Expression exp2 = exp.op.exp2;
5230                exp.op.exp2 = null;
5231                FreeExpContents(exp);
5232                FreeType(exp.expType);
5233                FreeType(exp.destType);
5234                *exp = *exp2;
5235                delete exp2;
5236                break;
5237             }
5238             case '-':
5239                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5240                break;
5241             // unary arithmetic increment and decrement
5242                   //OPERATOR_ALL(UNARY, ++, Inc)
5243                   //OPERATOR_ALL(UNARY, --, Dec)
5244             // unary bitwise
5245             case '~':
5246                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5247                break;
5248             // unary logical negation
5249             case '!':
5250                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5251                break;
5252          }
5253       }
5254       else
5255       {
5256          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5257          {
5258             if(Promote(op2, op1.kind, op1.type.isSigned))
5259                op2.kind = op1.kind, op2.ops = op1.ops;
5260             else if(Promote(op1, op2.kind, op2.type.isSigned))
5261                op1.kind = op2.kind, op1.ops = op2.ops;
5262          }
5263          switch(exp.op.op)
5264          {
5265             // binary arithmetic
5266             case '+':
5267                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5268                break;
5269             case '-':
5270                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5271                break;
5272             case '*':
5273                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5274                break;
5275             case '/':
5276                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5277                break;
5278             case '%':
5279                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5280                break;
5281             // binary arithmetic assignment
5282                   //OPERATOR_ALL(BINARY, =, Asign)
5283                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5284                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5285                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5286                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5287                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5288             // binary bitwise
5289             case '&':
5290                if(exp.op.exp2)
5291                {
5292                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5293                }
5294                break;
5295             case '|':
5296                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5297                break;
5298             case '^':
5299                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5300                break;
5301             case LEFT_OP:
5302                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5303                break;
5304             case RIGHT_OP:
5305                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5306                break;
5307             // binary bitwise assignment
5308                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5309                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5310                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5311                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5312                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5313             // binary logical equality
5314             case EQ_OP:
5315                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5316                break;
5317             case NE_OP:
5318                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5319                break;
5320             // binary logical
5321             case AND_OP:
5322                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5323                break;
5324             case OR_OP:
5325                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5326                break;
5327             // binary logical relational
5328             case '>':
5329                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5330                break;
5331             case '<':
5332                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5333                break;
5334             case GE_OP:
5335                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5336                break;
5337             case LE_OP:
5338                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5339                break;
5340          }
5341       }
5342    }
5343 }
5344
5345 void ComputeExpression(Expression exp)
5346 {
5347    char expString[10240];
5348    expString[0] = '\0';
5349 #ifdef _DEBUG
5350    PrintExpression(exp, expString);
5351 #endif
5352
5353    switch(exp.type)
5354    {
5355       case instanceExp:
5356       {
5357          ComputeInstantiation(exp);
5358          break;
5359       }
5360       /*
5361       case constantExp:
5362          break;
5363       */
5364       case opExp:
5365       {
5366          Expression exp1, exp2 = null;
5367          Operand op1 { };
5368          Operand op2 { };
5369
5370          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5371          if(exp.op.exp2)
5372          {
5373             Expression e = exp.op.exp2;
5374
5375             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5376             {
5377                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5378                {
5379                   if(e.type == extensionCompoundExp)
5380                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5381                   else
5382                      e = e.list->last;
5383                }
5384             }
5385             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5386             {
5387                if(e.type == stringExp && e.string)
5388                {
5389                   char * string = e.string;
5390                   int len = strlen(string);
5391                   char * tmp = new char[len-2+1];
5392                   len = UnescapeString(tmp, string + 1, len - 2);
5393                   delete tmp;
5394                   FreeExpContents(exp);
5395                   exp.type = constantExp;
5396                   exp.constant = PrintUInt(len + 1);
5397                }
5398                else
5399                {
5400                   Type type = e.expType;
5401                   type.refCount++;
5402                   FreeExpContents(exp);
5403                   exp.type = constantExp;
5404                   exp.constant = PrintUInt(ComputeTypeSize(type));
5405                   FreeType(type);
5406                }
5407                break;
5408             }
5409             else
5410                ComputeExpression(exp.op.exp2);
5411          }
5412          if(exp.op.exp1)
5413          {
5414             ComputeExpression(exp.op.exp1);
5415             exp1 = exp.op.exp1;
5416             exp2 = exp.op.exp2;
5417             op1 = GetOperand(exp1);
5418             if(op1.type) op1.type.refCount++;
5419             if(exp2)
5420             {
5421                op2 = GetOperand(exp2);
5422                if(op2.type) op2.type.refCount++;
5423             }
5424          }
5425          else
5426          {
5427             exp1 = exp.op.exp2;
5428             op1 = GetOperand(exp1);
5429             if(op1.type) op1.type.refCount++;
5430          }
5431
5432          CallOperator(exp, exp1, exp2, op1, op2);
5433          /*
5434          switch(exp.op.op)
5435          {
5436             // Unary operators
5437             case '&':
5438                // Also binary
5439                if(exp.op.exp1 && exp.op.exp2)
5440                {
5441                   // Binary And
5442                   if(op1.ops.BitAnd)
5443                   {
5444                      FreeExpContents(exp);
5445                      op1.ops.BitAnd(exp, op1, op2);
5446                   }
5447                }
5448                break;
5449             case '*':
5450                if(exp.op.exp1)
5451                {
5452                   if(op1.ops.Mul)
5453                   {
5454                      FreeExpContents(exp);
5455                      op1.ops.Mul(exp, op1, op2);
5456                   }
5457                }
5458                break;
5459             case '+':
5460                if(exp.op.exp1)
5461                {
5462                   if(op1.ops.Add)
5463                   {
5464                      FreeExpContents(exp);
5465                      op1.ops.Add(exp, op1, op2);
5466                   }
5467                }
5468                else
5469                {
5470                   // Provide default unary +
5471                   Expression exp2 = exp.op.exp2;
5472                   exp.op.exp2 = null;
5473                   FreeExpContents(exp);
5474                   FreeType(exp.expType);
5475                   FreeType(exp.destType);
5476
5477                   *exp = *exp2;
5478                   delete exp2;
5479                }
5480                break;
5481             case '-':
5482                if(exp.op.exp1)
5483                {
5484                   if(op1.ops.Sub)
5485                   {
5486                      FreeExpContents(exp);
5487                      op1.ops.Sub(exp, op1, op2);
5488                   }
5489                }
5490                else
5491                {
5492                   if(op1.ops.Neg)
5493                   {
5494                      FreeExpContents(exp);
5495                      op1.ops.Neg(exp, op1);
5496                   }
5497                }
5498                break;
5499             case '~':
5500                if(op1.ops.BitNot)
5501                {
5502                   FreeExpContents(exp);
5503                   op1.ops.BitNot(exp, op1);
5504                }
5505                break;
5506             case '!':
5507                if(op1.ops.Not)
5508                {
5509                   FreeExpContents(exp);
5510                   op1.ops.Not(exp, op1);
5511                }
5512                break;
5513             // Binary only operators
5514             case '/':
5515                if(op1.ops.Div)
5516                {
5517                   FreeExpContents(exp);
5518                   op1.ops.Div(exp, op1, op2);
5519                }
5520                break;
5521             case '%':
5522                if(op1.ops.Mod)
5523                {
5524                   FreeExpContents(exp);
5525                   op1.ops.Mod(exp, op1, op2);
5526                }
5527                break;
5528             case LEFT_OP:
5529                break;
5530             case RIGHT_OP:
5531                break;
5532             case '<':
5533                if(exp.op.exp1)
5534                {
5535                   if(op1.ops.Sma)
5536                   {
5537                      FreeExpContents(exp);
5538                      op1.ops.Sma(exp, op1, op2);
5539                   }
5540                }
5541                break;
5542             case '>':
5543                if(exp.op.exp1)
5544                {
5545                   if(op1.ops.Grt)
5546                   {
5547                      FreeExpContents(exp);
5548                      op1.ops.Grt(exp, op1, op2);
5549                   }
5550                }
5551                break;
5552             case LE_OP:
5553                if(exp.op.exp1)
5554                {
5555                   if(op1.ops.SmaEqu)
5556                   {
5557                      FreeExpContents(exp);
5558                      op1.ops.SmaEqu(exp, op1, op2);
5559                   }
5560                }
5561                break;
5562             case GE_OP:
5563                if(exp.op.exp1)
5564                {
5565                   if(op1.ops.GrtEqu)
5566                   {
5567                      FreeExpContents(exp);
5568                      op1.ops.GrtEqu(exp, op1, op2);
5569                   }
5570                }
5571                break;
5572             case EQ_OP:
5573                if(exp.op.exp1)
5574                {
5575                   if(op1.ops.Equ)
5576                   {
5577                      FreeExpContents(exp);
5578                      op1.ops.Equ(exp, op1, op2);
5579                   }
5580                }
5581                break;
5582             case NE_OP:
5583                if(exp.op.exp1)
5584                {
5585                   if(op1.ops.Nqu)
5586                   {
5587                      FreeExpContents(exp);
5588                      op1.ops.Nqu(exp, op1, op2);
5589                   }
5590                }
5591                break;
5592             case '|':
5593                if(op1.ops.BitOr)
5594                {
5595                   FreeExpContents(exp);
5596                   op1.ops.BitOr(exp, op1, op2);
5597                }
5598                break;
5599             case '^':
5600                if(op1.ops.BitXor)
5601                {
5602                   FreeExpContents(exp);
5603                   op1.ops.BitXor(exp, op1, op2);
5604                }
5605                break;
5606             case AND_OP:
5607                break;
5608             case OR_OP:
5609                break;
5610             case SIZEOF:
5611                FreeExpContents(exp);
5612                exp.type = constantExp;
5613                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5614                break;
5615          }
5616          */
5617          if(op1.type) FreeType(op1.type);
5618          if(op2.type) FreeType(op2.type);
5619          break;
5620       }
5621       case bracketsExp:
5622       case extensionExpressionExp:
5623       {
5624          Expression e, n;
5625          for(e = exp.list->first; e; e = n)
5626          {
5627             n = e.next;
5628             if(!n)
5629             {
5630                OldList * list = exp.list;
5631                Expression prev = exp.prev;
5632                Expression next = exp.next;
5633                ComputeExpression(e);
5634                //FreeExpContents(exp);
5635                FreeType(exp.expType);
5636                FreeType(exp.destType);
5637                *exp = *e;
5638                exp.prev = prev;
5639                exp.next = next;
5640                delete e;
5641                delete list;
5642             }
5643             else
5644             {
5645                FreeExpression(e);
5646             }
5647          }
5648          break;
5649       }
5650       /*
5651
5652       case ExpIndex:
5653       {
5654          Expression e;
5655          exp.isConstant = true;
5656
5657          ComputeExpression(exp.index.exp);
5658          if(!exp.index.exp.isConstant)
5659             exp.isConstant = false;
5660
5661          for(e = exp.index.index->first; e; e = e.next)
5662          {
5663             ComputeExpression(e);
5664             if(!e.next)
5665             {
5666                // Check if this type is int
5667             }
5668             if(!e.isConstant)
5669                exp.isConstant = false;
5670          }
5671          exp.expType = Dereference(exp.index.exp.expType);
5672          break;
5673       }
5674       */
5675       case memberExp:
5676       {
5677          Expression memberExp = exp.member.exp;
5678          Identifier memberID = exp.member.member;
5679
5680          Type type;
5681          ComputeExpression(exp.member.exp);
5682          type = exp.member.exp.expType;
5683          if(type)
5684          {
5685             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);
5686             Property prop = null;
5687             DataMember member = null;
5688             Class convertTo = null;
5689             if(type.kind == subClassType && exp.member.exp.type == classExp)
5690                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5691
5692             if(!_class)
5693             {
5694                char string[256];
5695                Symbol classSym;
5696                string[0] = '\0';
5697                PrintTypeNoConst(type, string, false, true);
5698                classSym = FindClass(string);
5699                _class = classSym ? classSym.registered : null;
5700             }
5701
5702             if(exp.member.member)
5703             {
5704                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5705                if(!prop)
5706                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5707             }
5708             if(!prop && !member && _class && exp.member.member)
5709             {
5710                Symbol classSym = FindClass(exp.member.member.string);
5711                convertTo = _class;
5712                _class = classSym ? classSym.registered : null;
5713                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5714             }
5715
5716             if(prop)
5717             {
5718                if(prop.compiled)
5719                {
5720                   Type type = prop.dataType;
5721                   // TODO: Assuming same base type for units...
5722                   if(_class.type == unitClass)
5723                   {
5724                      if(type.kind == classType)
5725                      {
5726                         Class _class = type._class.registered;
5727                         if(_class.type == unitClass)
5728                         {
5729                            if(!_class.dataType)
5730                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5731                            type = _class.dataType;
5732                         }
5733                      }
5734                      switch(type.kind)
5735                      {
5736                         case floatType:
5737                         {
5738                            float value;
5739                            float (*Get)(float) = (void *)prop.Get;
5740                            GetFloat(exp.member.exp, &value);
5741                            exp.constant = PrintFloat(Get ? Get(value) : value);
5742                            exp.type = constantExp;
5743                            break;
5744                         }
5745                         case doubleType:
5746                         {
5747                            double value;
5748                            double (*Get)(double);
5749                            GetDouble(exp.member.exp, &value);
5750
5751                            if(convertTo)
5752                               Get = (void *)prop.Set;
5753                            else
5754                               Get = (void *)prop.Get;
5755                            exp.constant = PrintDouble(Get ? Get(value) : value);
5756                            exp.type = constantExp;
5757                            break;
5758                         }
5759                      }
5760                   }
5761                   else
5762                   {
5763                      if(convertTo)
5764                      {
5765                         Expression value = exp.member.exp;
5766                         Type type;
5767                         if(!prop.dataType)
5768                            ProcessPropertyType(prop);
5769
5770                         type = prop.dataType;
5771                         if(!type)
5772                         {
5773                             // printf("Investigate this\n");
5774                         }
5775                         else if(_class.type == structClass)
5776                         {
5777                            switch(type.kind)
5778                            {
5779                               case classType:
5780                               {
5781                                  Class propertyClass = type._class.registered;
5782                                  if(propertyClass.type == structClass && value.type == instanceExp)
5783                                  {
5784                                     void (*Set)(void *, void *) = (void *)prop.Set;
5785                                     exp.instance = Instantiation { };
5786                                     exp.instance.data = new0 byte[_class.structSize];
5787                                     exp.instance._class = MkSpecifierName(_class.fullName);
5788                                     exp.instance.loc = exp.loc;
5789                                     exp.type = instanceExp;
5790                                     Set(exp.instance.data, value.instance.data);
5791                                     PopulateInstance(exp.instance);
5792                                  }
5793                                  break;
5794                               }
5795                               case intType:
5796                               {
5797                                  int intValue;
5798                                  void (*Set)(void *, int) = (void *)prop.Set;
5799
5800                                  exp.instance = Instantiation { };
5801                                  exp.instance.data = new0 byte[_class.structSize];
5802                                  exp.instance._class = MkSpecifierName(_class.fullName);
5803                                  exp.instance.loc = exp.loc;
5804                                  exp.type = instanceExp;
5805
5806                                  GetInt(value, &intValue);
5807
5808                                  Set(exp.instance.data, intValue);
5809                                  PopulateInstance(exp.instance);
5810                                  break;
5811                               }
5812                               case int64Type:
5813                               {
5814                                  int64 intValue;
5815                                  void (*Set)(void *, int64) = (void *)prop.Set;
5816
5817                                  exp.instance = Instantiation { };
5818                                  exp.instance.data = new0 byte[_class.structSize];
5819                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5820                                  exp.instance.loc = exp.loc;
5821                                  exp.type = instanceExp;
5822
5823                                  GetInt64(value, &intValue);
5824
5825                                  Set(exp.instance.data, intValue);
5826                                  PopulateInstance(exp.instance);
5827                                  break;
5828                               }
5829                               case intPtrType:
5830                               {
5831                                  // TOFIX:
5832                                  intptr intValue;
5833                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5834
5835                                  exp.instance = Instantiation { };
5836                                  exp.instance.data = new0 byte[_class.structSize];
5837                                  exp.instance._class = MkSpecifierName(_class.fullName);
5838                                  exp.instance.loc = exp.loc;
5839                                  exp.type = instanceExp;
5840
5841                                  GetIntPtr(value, &intValue);
5842
5843                                  Set(exp.instance.data, intValue);
5844                                  PopulateInstance(exp.instance);
5845                                  break;
5846                               }
5847                               case intSizeType:
5848                               {
5849                                  // TOFIX:
5850                                  intsize intValue;
5851                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5852
5853                                  exp.instance = Instantiation { };
5854                                  exp.instance.data = new0 byte[_class.structSize];
5855                                  exp.instance._class = MkSpecifierName(_class.fullName);
5856                                  exp.instance.loc = exp.loc;
5857                                  exp.type = instanceExp;
5858
5859                                  GetIntSize(value, &intValue);
5860
5861                                  Set(exp.instance.data, intValue);
5862                                  PopulateInstance(exp.instance);
5863                                  break;
5864                               }
5865                               case floatType:
5866                               {
5867                                  float floatValue;
5868                                  void (*Set)(void *, float) = (void *)prop.Set;
5869
5870                                  exp.instance = Instantiation { };
5871                                  exp.instance.data = new0 byte[_class.structSize];
5872                                  exp.instance._class = MkSpecifierName(_class.fullName);
5873                                  exp.instance.loc = exp.loc;
5874                                  exp.type = instanceExp;
5875
5876                                  GetFloat(value, &floatValue);
5877
5878                                  Set(exp.instance.data, floatValue);
5879                                  PopulateInstance(exp.instance);
5880                                  break;
5881                               }
5882                               case doubleType:
5883                               {
5884                                  double doubleValue;
5885                                  void (*Set)(void *, double) = (void *)prop.Set;
5886
5887                                  exp.instance = Instantiation { };
5888                                  exp.instance.data = new0 byte[_class.structSize];
5889                                  exp.instance._class = MkSpecifierName(_class.fullName);
5890                                  exp.instance.loc = exp.loc;
5891                                  exp.type = instanceExp;
5892
5893                                  GetDouble(value, &doubleValue);
5894
5895                                  Set(exp.instance.data, doubleValue);
5896                                  PopulateInstance(exp.instance);
5897                                  break;
5898                               }
5899                            }
5900                         }
5901                         else if(_class.type == bitClass)
5902                         {
5903                            switch(type.kind)
5904                            {
5905                               case classType:
5906                               {
5907                                  Class propertyClass = type._class.registered;
5908                                  if(propertyClass.type == structClass && value.instance.data)
5909                                  {
5910                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5911                                     unsigned int bits = Set(value.instance.data);
5912                                     exp.constant = PrintHexUInt(bits);
5913                                     exp.type = constantExp;
5914                                     break;
5915                                  }
5916                                  else if(_class.type == bitClass)
5917                                  {
5918                                     unsigned int value;
5919                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5920                                     unsigned int bits;
5921
5922                                     GetUInt(exp.member.exp, &value);
5923                                     bits = Set(value);
5924                                     exp.constant = PrintHexUInt(bits);
5925                                     exp.type = constantExp;
5926                                  }
5927                               }
5928                            }
5929                         }
5930                      }
5931                      else
5932                      {
5933                         if(_class.type == bitClass)
5934                         {
5935                            unsigned int value;
5936                            GetUInt(exp.member.exp, &value);
5937
5938                            switch(type.kind)
5939                            {
5940                               case classType:
5941                               {
5942                                  Class _class = type._class.registered;
5943                                  if(_class.type == structClass)
5944                                  {
5945                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5946
5947                                     exp.instance = Instantiation { };
5948                                     exp.instance.data = new0 byte[_class.structSize];
5949                                     exp.instance._class = MkSpecifierName(_class.fullName);
5950                                     exp.instance.loc = exp.loc;
5951                                     //exp.instance.fullSet = true;
5952                                     exp.type = instanceExp;
5953                                     Get(value, exp.instance.data);
5954                                     PopulateInstance(exp.instance);
5955                                  }
5956                                  else if(_class.type == bitClass)
5957                                  {
5958                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5959                                     uint64 bits = Get(value);
5960                                     exp.constant = PrintHexUInt64(bits);
5961                                     exp.type = constantExp;
5962                                  }
5963                                  break;
5964                               }
5965                            }
5966                         }
5967                         else if(_class.type == structClass)
5968                         {
5969                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5970                            switch(type.kind)
5971                            {
5972                               case classType:
5973                               {
5974                                  Class _class = type._class.registered;
5975                                  if(_class.type == structClass && value)
5976                                  {
5977                                     void (*Get)(void *, void *) = (void *)prop.Get;
5978
5979                                     exp.instance = Instantiation { };
5980                                     exp.instance.data = new0 byte[_class.structSize];
5981                                     exp.instance._class = MkSpecifierName(_class.fullName);
5982                                     exp.instance.loc = exp.loc;
5983                                     //exp.instance.fullSet = true;
5984                                     exp.type = instanceExp;
5985                                     Get(value, exp.instance.data);
5986                                     PopulateInstance(exp.instance);
5987                                  }
5988                                  break;
5989                               }
5990                            }
5991                         }
5992                         /*else
5993                         {
5994                            char * value = exp.member.exp.instance.data;
5995                            switch(type.kind)
5996                            {
5997                               case classType:
5998                               {
5999                                  Class _class = type._class.registered;
6000                                  if(_class.type == normalClass)
6001                                  {
6002                                     void *(*Get)(void *) = (void *)prop.Get;
6003
6004                                     exp.instance = Instantiation { };
6005                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6006                                     exp.type = instanceExp;
6007                                     exp.instance.data = Get(value, exp.instance.data);
6008                                  }
6009                                  break;
6010                               }
6011                            }
6012                         }
6013                         */
6014                      }
6015                   }
6016                }
6017                else
6018                {
6019                   exp.isConstant = false;
6020                }
6021             }
6022             else if(member)
6023             {
6024             }
6025          }
6026
6027          if(exp.type != ExpressionType::memberExp)
6028          {
6029             FreeExpression(memberExp);
6030             FreeIdentifier(memberID);
6031          }
6032          break;
6033       }
6034       case typeSizeExp:
6035       {
6036          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6037          FreeExpContents(exp);
6038          exp.constant = PrintUInt(ComputeTypeSize(type));
6039          exp.type = constantExp;
6040          FreeType(type);
6041          break;
6042       }
6043       case classSizeExp:
6044       {
6045          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6046          if(classSym && classSym.registered)
6047          {
6048             if(classSym.registered.fixed)
6049             {
6050                FreeSpecifier(exp._class);
6051                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6052                exp.type = constantExp;
6053             }
6054             else
6055             {
6056                char className[1024];
6057                strcpy(className, "__ecereClass_");
6058                FullClassNameCat(className, classSym.string, true);
6059                MangleClassName(className);
6060
6061                DeclareClass(classSym, className);
6062
6063                FreeExpContents(exp);
6064                exp.type = pointerExp;
6065                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6066                exp.member.member = MkIdentifier("structSize");
6067             }
6068          }
6069          break;
6070       }
6071       case castExp:
6072       //case constantExp:
6073       {
6074          Type type;
6075          Expression e = exp;
6076          if(exp.type == castExp)
6077          {
6078             if(exp.cast.exp)
6079                ComputeExpression(exp.cast.exp);
6080             e = exp.cast.exp;
6081          }
6082          if(e && exp.expType)
6083          {
6084             /*if(exp.destType)
6085                type = exp.destType;
6086             else*/
6087                type = exp.expType;
6088             if(type.kind == classType)
6089             {
6090                Class _class = type._class.registered;
6091                if(_class && (_class.type == unitClass || _class.type == bitClass))
6092                {
6093                   if(!_class.dataType)
6094                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6095                   type = _class.dataType;
6096                }
6097             }
6098
6099             switch(type.kind)
6100             {
6101                case _BoolType:
6102                case charType:
6103                   if(type.isSigned)
6104                   {
6105                      char value = 0;
6106                      if(GetChar(e, &value))
6107                      {
6108                         FreeExpContents(exp);
6109                         exp.constant = PrintChar(value);
6110                         exp.type = constantExp;
6111                      }
6112                   }
6113                   else
6114                   {
6115                      unsigned char value = 0;
6116                      if(GetUChar(e, &value))
6117                      {
6118                         FreeExpContents(exp);
6119                         exp.constant = PrintUChar(value);
6120                         exp.type = constantExp;
6121                      }
6122                   }
6123                   break;
6124                case shortType:
6125                   if(type.isSigned)
6126                   {
6127                      short value = 0;
6128                      if(GetShort(e, &value))
6129                      {
6130                         FreeExpContents(exp);
6131                         exp.constant = PrintShort(value);
6132                         exp.type = constantExp;
6133                      }
6134                   }
6135                   else
6136                   {
6137                      unsigned short value = 0;
6138                      if(GetUShort(e, &value))
6139                      {
6140                         FreeExpContents(exp);
6141                         exp.constant = PrintUShort(value);
6142                         exp.type = constantExp;
6143                      }
6144                   }
6145                   break;
6146                case intType:
6147                   if(type.isSigned)
6148                   {
6149                      int value = 0;
6150                      if(GetInt(e, &value))
6151                      {
6152                         FreeExpContents(exp);
6153                         exp.constant = PrintInt(value);
6154                         exp.type = constantExp;
6155                      }
6156                   }
6157                   else
6158                   {
6159                      unsigned int value = 0;
6160                      if(GetUInt(e, &value))
6161                      {
6162                         FreeExpContents(exp);
6163                         exp.constant = PrintUInt(value);
6164                         exp.type = constantExp;
6165                      }
6166                   }
6167                   break;
6168                case int64Type:
6169                   if(type.isSigned)
6170                   {
6171                      int64 value = 0;
6172                      if(GetInt64(e, &value))
6173                      {
6174                         FreeExpContents(exp);
6175                         exp.constant = PrintInt64(value);
6176                         exp.type = constantExp;
6177                      }
6178                   }
6179                   else
6180                   {
6181                      uint64 value = 0;
6182                      if(GetUInt64(e, &value))
6183                      {
6184                         FreeExpContents(exp);
6185                         exp.constant = PrintUInt64(value);
6186                         exp.type = constantExp;
6187                      }
6188                   }
6189                   break;
6190                case intPtrType:
6191                   if(type.isSigned)
6192                   {
6193                      intptr value = 0;
6194                      if(GetIntPtr(e, &value))
6195                      {
6196                         FreeExpContents(exp);
6197                         exp.constant = PrintInt64((int64)value);
6198                         exp.type = constantExp;
6199                      }
6200                   }
6201                   else
6202                   {
6203                      uintptr value = 0;
6204                      if(GetUIntPtr(e, &value))
6205                      {
6206                         FreeExpContents(exp);
6207                         exp.constant = PrintUInt64((uint64)value);
6208                         exp.type = constantExp;
6209                      }
6210                   }
6211                   break;
6212                case intSizeType:
6213                   if(type.isSigned)
6214                   {
6215                      intsize value = 0;
6216                      if(GetIntSize(e, &value))
6217                      {
6218                         FreeExpContents(exp);
6219                         exp.constant = PrintInt64((int64)value);
6220                         exp.type = constantExp;
6221                      }
6222                   }
6223                   else
6224                   {
6225                      uintsize value = 0;
6226                      if(GetUIntSize(e, &value))
6227                      {
6228                         FreeExpContents(exp);
6229                         exp.constant = PrintUInt64((uint64)value);
6230                         exp.type = constantExp;
6231                      }
6232                   }
6233                   break;
6234                case floatType:
6235                {
6236                   float value = 0;
6237                   if(GetFloat(e, &value))
6238                   {
6239                      FreeExpContents(exp);
6240                      exp.constant = PrintFloat(value);
6241                      exp.type = constantExp;
6242                   }
6243                   break;
6244                }
6245                case doubleType:
6246                {
6247                   double value = 0;
6248                   if(GetDouble(e, &value))
6249                   {
6250                      FreeExpContents(exp);
6251                      exp.constant = PrintDouble(value);
6252                      exp.type = constantExp;
6253                   }
6254                   break;
6255                }
6256             }
6257          }
6258          break;
6259       }
6260       case conditionExp:
6261       {
6262          Operand op1 { };
6263          Operand op2 { };
6264          Operand op3 { };
6265
6266          if(exp.cond.exp)
6267             // Caring only about last expression for now...
6268             ComputeExpression(exp.cond.exp->last);
6269          if(exp.cond.elseExp)
6270             ComputeExpression(exp.cond.elseExp);
6271          if(exp.cond.cond)
6272             ComputeExpression(exp.cond.cond);
6273
6274          op1 = GetOperand(exp.cond.cond);
6275          if(op1.type) op1.type.refCount++;
6276          op2 = GetOperand(exp.cond.exp->last);
6277          if(op2.type) op2.type.refCount++;
6278          op3 = GetOperand(exp.cond.elseExp);
6279          if(op3.type) op3.type.refCount++;
6280
6281          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6282          if(op1.type) FreeType(op1.type);
6283          if(op2.type) FreeType(op2.type);
6284          if(op3.type) FreeType(op3.type);
6285          break;
6286       }
6287    }
6288 }
6289
6290 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
6291 {
6292    bool result = true;
6293    if(destType)
6294    {
6295       OldList converts { };
6296       Conversion convert;
6297
6298       if(destType.kind == voidType)
6299          return false;
6300
6301       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
6302          result = false;
6303       if(converts.count)
6304       {
6305          // for(convert = converts.last; convert; convert = convert.prev)
6306          for(convert = converts.first; convert; convert = convert.next)
6307          {
6308             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6309             if(!empty)
6310             {
6311                Expression newExp { };
6312                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6313
6314                // TODO: Check this...
6315                *newExp = *exp;
6316                newExp.prev = null;
6317                newExp.next = null;
6318                newExp.destType = null;
6319
6320                if(convert.isGet)
6321                {
6322                   // [exp].ColorRGB
6323                   exp.type = memberExp;
6324                   exp.addedThis = true;
6325                   exp.member.exp = newExp;
6326                   FreeType(exp.member.exp.expType);
6327
6328                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6329                   exp.member.exp.expType.classObjectType = objectType;
6330                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6331                   exp.member.memberType = propertyMember;
6332                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6333                   // TESTING THIS... for (int)degrees
6334                   exp.needCast = true;
6335                   if(exp.expType) exp.expType.refCount++;
6336                   ApplyAnyObjectLogic(exp.member.exp);
6337                }
6338                else
6339                {
6340
6341                   /*if(exp.isConstant)
6342                   {
6343                      // Color { ColorRGB = [exp] };
6344                      exp.type = instanceExp;
6345                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6346                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6347                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6348                   }
6349                   else*/
6350                   {
6351                      // If not constant, don't turn it yet into an instantiation
6352                      // (Go through the deep members system first)
6353                      exp.type = memberExp;
6354                      exp.addedThis = true;
6355                      exp.member.exp = newExp;
6356
6357                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6358                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6359                         newExp.expType._class.registered.type == noHeadClass)
6360                      {
6361                         newExp.byReference = true;
6362                      }
6363
6364                      FreeType(exp.member.exp.expType);
6365                      /*exp.member.exp.expType = convert.convert.dataType;
6366                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6367                      exp.member.exp.expType = null;
6368                      if(convert.convert.dataType)
6369                      {
6370                         exp.member.exp.expType = { };
6371                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6372                         exp.member.exp.expType.refCount = 1;
6373                         exp.member.exp.expType.classObjectType = objectType;
6374                         ApplyAnyObjectLogic(exp.member.exp);
6375                      }
6376
6377                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6378                      exp.member.memberType = reverseConversionMember;
6379                      exp.expType = convert.resultType ? convert.resultType :
6380                         MkClassType(convert.convert._class.fullName);
6381                      exp.needCast = true;
6382                      if(convert.resultType) convert.resultType.refCount++;
6383                   }
6384                }
6385             }
6386             else
6387             {
6388                FreeType(exp.expType);
6389                if(convert.isGet)
6390                {
6391                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6392                   exp.needCast = true;
6393                   if(exp.expType) exp.expType.refCount++;
6394                }
6395                else
6396                {
6397                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6398                   exp.needCast = true;
6399                   if(convert.resultType)
6400                      convert.resultType.refCount++;
6401                }
6402             }
6403          }
6404          if(exp.isConstant && inCompiler)
6405             ComputeExpression(exp);
6406
6407          converts.Free(FreeConvert);
6408       }
6409
6410       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6411       {
6412          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6413       }
6414       if(!result && exp.expType && exp.destType)
6415       {
6416          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6417              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6418             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6419             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6420             result = true;
6421       }
6422    }
6423    // if(result) CheckTemplateTypes(exp);
6424    return result;
6425 }
6426
6427 void CheckTemplateTypes(Expression exp)
6428 {
6429    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6430    {
6431       Expression newExp { };
6432       Context context;
6433       *newExp = *exp;
6434       if(exp.destType) exp.destType.refCount++;
6435       if(exp.expType)  exp.expType.refCount++;
6436       newExp.prev = null;
6437       newExp.next = null;
6438
6439       switch(exp.expType.kind)
6440       {
6441          case doubleType:
6442             if(exp.destType.classObjectType)
6443             {
6444                // We need to pass the address, just pass it along (Undo what was done above)
6445                if(exp.destType) exp.destType.refCount--;
6446                if(exp.expType)  exp.expType.refCount--;
6447                delete newExp;
6448             }
6449             else
6450             {
6451                // If we're looking for value:
6452                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6453                OldList * specs;
6454                OldList * unionDefs = MkList();
6455                OldList * statements = MkList();
6456                context = PushContext();
6457                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6458                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6459                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6460                exp.type = extensionCompoundExp;
6461                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6462                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6463                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6464                exp.compound.compound.context = context;
6465                PopContext(context);
6466             }
6467             break;
6468          default:
6469             exp.type = castExp;
6470             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6471             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6472             break;
6473       }
6474    }
6475    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6476    {
6477       Expression newExp { };
6478       Statement compound;
6479       Context context;
6480       *newExp = *exp;
6481       if(exp.destType) exp.destType.refCount++;
6482       if(exp.expType)  exp.expType.refCount++;
6483       newExp.prev = null;
6484       newExp.next = null;
6485
6486       switch(exp.expType.kind)
6487       {
6488          case doubleType:
6489             if(exp.destType.classObjectType)
6490             {
6491                // We need to pass the address, just pass it along (Undo what was done above)
6492                if(exp.destType) exp.destType.refCount--;
6493                if(exp.expType)  exp.expType.refCount--;
6494                delete newExp;
6495             }
6496             else
6497             {
6498                // If we're looking for value:
6499                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6500                OldList * specs;
6501                OldList * unionDefs = MkList();
6502                OldList * statements = MkList();
6503                context = PushContext();
6504                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6505                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6506                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6507                exp.type = extensionCompoundExp;
6508                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6509                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6510                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6511                exp.compound.compound.context = context;
6512                PopContext(context);
6513             }
6514             break;
6515          case classType:
6516          {
6517             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6518             {
6519                exp.type = bracketsExp;
6520                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6521                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6522                ProcessExpressionType(exp.list->first);
6523                break;
6524             }
6525             else
6526             {
6527                exp.type = bracketsExp;
6528                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6529                newExp.needCast = true;
6530                ProcessExpressionType(exp.list->first);
6531                break;
6532             }
6533          }
6534          default:
6535          {
6536             if(exp.expType.kind == templateType)
6537             {
6538                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6539                if(type)
6540                {
6541                   FreeType(exp.destType);
6542                   FreeType(exp.expType);
6543                   delete newExp;
6544                   break;
6545                }
6546             }
6547             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6548             {
6549                exp.type = opExp;
6550                exp.op.op = '*';
6551                exp.op.exp1 = null;
6552                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6553                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6554             }
6555             else
6556             {
6557                char typeString[1024];
6558                Declarator decl;
6559                OldList * specs = MkList();
6560                typeString[0] = '\0';
6561                PrintType(exp.expType, typeString, false, false);
6562                decl = SpecDeclFromString(typeString, specs, null);
6563
6564                exp.type = castExp;
6565                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6566                exp.cast.typeName = MkTypeName(specs, decl);
6567                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6568                exp.cast.exp.needCast = true;
6569             }
6570             break;
6571          }
6572       }
6573    }
6574 }
6575 // TODO: The Symbol tree should be reorganized by namespaces
6576 // Name Space:
6577 //    - Tree of all symbols within (stored without namespace)
6578 //    - Tree of sub-namespaces
6579
6580 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6581 {
6582    int nsLen = strlen(nameSpace);
6583    Symbol symbol;
6584    // Start at the name space prefix
6585    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6586    {
6587       char * s = symbol.string;
6588       if(!strncmp(s, nameSpace, nsLen))
6589       {
6590          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6591          int c;
6592          char * namePart;
6593          for(c = strlen(s)-1; c >= 0; c--)
6594             if(s[c] == ':')
6595                break;
6596
6597          namePart = s+c+1;
6598          if(!strcmp(namePart, name))
6599          {
6600             // TODO: Error on ambiguity
6601             return symbol;
6602          }
6603       }
6604       else
6605          break;
6606    }
6607    return null;
6608 }
6609
6610 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6611 {
6612    int c;
6613    char nameSpace[1024];
6614    char * namePart;
6615    bool gotColon = false;
6616
6617    nameSpace[0] = '\0';
6618    for(c = strlen(name)-1; c >= 0; c--)
6619       if(name[c] == ':')
6620       {
6621          gotColon = true;
6622          break;
6623       }
6624
6625    namePart = name+c+1;
6626    while(c >= 0 && name[c] == ':') c--;
6627    if(c >= 0)
6628    {
6629       // Try an exact match first
6630       Symbol symbol = (Symbol)tree.FindString(name);
6631       if(symbol)
6632          return symbol;
6633
6634       // Namespace specified
6635       memcpy(nameSpace, name, c + 1);
6636       nameSpace[c+1] = 0;
6637
6638       return ScanWithNameSpace(tree, nameSpace, namePart);
6639    }
6640    else if(gotColon)
6641    {
6642       // Looking for a global symbol, e.g. ::Sleep()
6643       Symbol symbol = (Symbol)tree.FindString(namePart);
6644       return symbol;
6645    }
6646    else
6647    {
6648       // Name only (no namespace specified)
6649       Symbol symbol = (Symbol)tree.FindString(namePart);
6650       if(symbol)
6651          return symbol;
6652       return ScanWithNameSpace(tree, "", namePart);
6653    }
6654    return null;
6655 }
6656
6657 static void ProcessDeclaration(Declaration decl);
6658
6659 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6660 {
6661 #ifdef _DEBUG
6662    //Time startTime = GetTime();
6663 #endif
6664    // Optimize this later? Do this before/less?
6665    Context ctx;
6666    Symbol symbol = null;
6667    // First, check if the identifier is declared inside the function
6668    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6669
6670    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6671    {
6672       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6673       {
6674          symbol = null;
6675          if(thisNameSpace)
6676          {
6677             char curName[1024];
6678             strcpy(curName, thisNameSpace);
6679             strcat(curName, "::");
6680             strcat(curName, name);
6681             // Try to resolve in current namespace first
6682             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6683          }
6684          if(!symbol)
6685             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6686       }
6687       else
6688          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6689
6690       if(symbol || ctx == endContext) break;
6691    }
6692    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6693    {
6694       if(symbol.pointerExternal.type == functionExternal)
6695       {
6696          FunctionDefinition function = symbol.pointerExternal.function;
6697
6698          // Modified this recently...
6699          Context tmpContext = curContext;
6700          curContext = null;
6701          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6702          curContext = tmpContext;
6703
6704          symbol.pointerExternal.symbol = symbol;
6705
6706          // TESTING THIS:
6707          DeclareType(symbol.type, true, true);
6708
6709          ast->Insert(curExternal.prev, symbol.pointerExternal);
6710
6711          symbol.id = curExternal.symbol.idCode;
6712
6713       }
6714       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6715       {
6716          ast->Move(symbol.pointerExternal, curExternal.prev);
6717          symbol.id = curExternal.symbol.idCode;
6718       }
6719    }
6720 #ifdef _DEBUG
6721    //findSymbolTotalTime += GetTime() - startTime;
6722 #endif
6723    return symbol;
6724 }
6725
6726 static void GetTypeSpecs(Type type, OldList * specs)
6727 {
6728    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6729    switch(type.kind)
6730    {
6731       case classType:
6732       {
6733          if(type._class.registered)
6734          {
6735             if(!type._class.registered.dataType)
6736                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6737             GetTypeSpecs(type._class.registered.dataType, specs);
6738          }
6739          break;
6740       }
6741       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6742       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6743       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6744       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6745       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6746       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6747       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6748       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6749       case intType:
6750       default:
6751          ListAdd(specs, MkSpecifier(INT)); break;
6752    }
6753 }
6754
6755 static void PrintArraySize(Type arrayType, char * string)
6756 {
6757    char size[256];
6758    size[0] = '\0';
6759    strcat(size, "[");
6760    if(arrayType.enumClass)
6761       strcat(size, arrayType.enumClass.string);
6762    else if(arrayType.arraySizeExp)
6763       PrintExpression(arrayType.arraySizeExp, size);
6764    strcat(size, "]");
6765    strcat(string, size);
6766 }
6767
6768 // WARNING : This function expects a null terminated string since it recursively concatenate...
6769 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6770 {
6771    if(type)
6772    {
6773       if(printConst && type.constant)
6774          strcat(string, "const ");
6775       switch(type.kind)
6776       {
6777          case classType:
6778          {
6779             Symbol c = type._class;
6780             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6781             //       look into merging with thisclass ?
6782             if(type.classObjectType == typedObject)
6783                strcat(string, "typed_object");
6784             else if(type.classObjectType == anyObject)
6785                strcat(string, "any_object");
6786             else
6787             {
6788                if(c && c.string)
6789                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6790             }
6791             if(type.byReference)
6792                strcat(string, " &");
6793             break;
6794          }
6795          case voidType: strcat(string, "void"); break;
6796          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6797          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6798          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6799          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6800          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6801          case _BoolType: strcat(string, "_Bool"); break;
6802          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6803          case floatType: strcat(string, "float"); break;
6804          case doubleType: strcat(string, "double"); break;
6805          case structType:
6806             if(type.enumName)
6807             {
6808                strcat(string, "struct ");
6809                strcat(string, type.enumName);
6810             }
6811             else if(type.typeName)
6812                strcat(string, type.typeName);
6813             else
6814             {
6815                Type member;
6816                strcat(string, "struct { ");
6817                for(member = type.members.first; member; member = member.next)
6818                {
6819                   PrintType(member, string, true, fullName);
6820                   strcat(string,"; ");
6821                }
6822                strcat(string,"}");
6823             }
6824             break;
6825          case unionType:
6826             if(type.enumName)
6827             {
6828                strcat(string, "union ");
6829                strcat(string, type.enumName);
6830             }
6831             else if(type.typeName)
6832                strcat(string, type.typeName);
6833             else
6834             {
6835                strcat(string, "union ");
6836                strcat(string,"(unnamed)");
6837             }
6838             break;
6839          case enumType:
6840             if(type.enumName)
6841             {
6842                strcat(string, "enum ");
6843                strcat(string, type.enumName);
6844             }
6845             else if(type.typeName)
6846                strcat(string, type.typeName);
6847             else
6848                strcat(string, "int"); // "enum");
6849             break;
6850          case ellipsisType:
6851             strcat(string, "...");
6852             break;
6853          case subClassType:
6854             strcat(string, "subclass(");
6855             strcat(string, type._class ? type._class.string : "int");
6856             strcat(string, ")");
6857             break;
6858          case templateType:
6859             strcat(string, type.templateParameter.identifier.string);
6860             break;
6861          case thisClassType:
6862             strcat(string, "thisclass");
6863             break;
6864          case vaListType:
6865             strcat(string, "__builtin_va_list");
6866             break;
6867       }
6868    }
6869 }
6870
6871 static void PrintName(Type type, char * string, bool fullName)
6872 {
6873    if(type.name && type.name[0])
6874    {
6875       if(fullName)
6876          strcat(string, type.name);
6877       else
6878       {
6879          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6880          if(name) name += 2; else name = type.name;
6881          strcat(string, name);
6882       }
6883    }
6884 }
6885
6886 static void PrintAttribs(Type type, char * string)
6887 {
6888    if(type)
6889    {
6890       if(type.dllExport)   strcat(string, "dllexport ");
6891       if(type.attrStdcall) strcat(string, "stdcall ");
6892    }
6893 }
6894
6895 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6896 {
6897    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6898    {
6899       Type attrType = null;
6900       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6901          PrintAttribs(type, string);
6902       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6903          strcat(string, " const");
6904       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6905       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6906          strcat(string, " (");
6907       if(type.kind == pointerType)
6908       {
6909          if(type.type.kind == functionType || type.type.kind == methodType)
6910             PrintAttribs(type.type, string);
6911       }
6912       if(type.kind == pointerType)
6913       {
6914          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6915             strcat(string, "*");
6916          else
6917             strcat(string, " *");
6918       }
6919       if(printConst && type.constant && type.kind == pointerType)
6920          strcat(string, " const");
6921    }
6922    else
6923       PrintTypeSpecs(type, string, fullName, printConst);
6924 }
6925
6926 static void PostPrintType(Type type, char * string, bool fullName)
6927 {
6928    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6929       strcat(string, ")");
6930    if(type.kind == arrayType)
6931       PrintArraySize(type, string);
6932    else if(type.kind == functionType)
6933    {
6934       Type param;
6935       strcat(string, "(");
6936       for(param = type.params.first; param; param = param.next)
6937       {
6938          PrintType(param, string, true, fullName);
6939          if(param.next) strcat(string, ", ");
6940       }
6941       strcat(string, ")");
6942    }
6943    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6944       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6945 }
6946
6947 // *****
6948 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6949 // *****
6950 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6951 {
6952    PrePrintType(type, string, fullName, null, printConst);
6953
6954    if(type.thisClass || (printName && type.name && type.name[0]))
6955       strcat(string, " ");
6956    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6957    {
6958       Symbol _class = type.thisClass;
6959       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6960       {
6961          if(type.classObjectType == classPointer)
6962             strcat(string, "class");
6963          else
6964             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6965       }
6966       else if(_class && _class.string)
6967       {
6968          String s = _class.string;
6969          if(fullName)
6970             strcat(string, s);
6971          else
6972          {
6973             char * name = RSearchString(s, "::", strlen(s), true, false);
6974             if(name) name += 2; else name = s;
6975             strcat(string, name);
6976          }
6977       }
6978       strcat(string, "::");
6979    }
6980
6981    if(printName && type.name)
6982       PrintName(type, string, fullName);
6983    PostPrintType(type, string, fullName);
6984    if(type.bitFieldCount)
6985    {
6986       char count[100];
6987       sprintf(count, ":%d", type.bitFieldCount);
6988       strcat(string, count);
6989    }
6990 }
6991
6992 void PrintType(Type type, char * string, bool printName, bool fullName)
6993 {
6994    _PrintType(type, string, printName, fullName, true);
6995 }
6996
6997 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6998 {
6999    _PrintType(type, string, printName, fullName, false);
7000 }
7001
7002 static Type FindMember(Type type, char * string)
7003 {
7004    Type memberType;
7005    for(memberType = type.members.first; memberType; memberType = memberType.next)
7006    {
7007       if(!memberType.name)
7008       {
7009          Type subType = FindMember(memberType, string);
7010          if(subType)
7011             return subType;
7012       }
7013       else if(!strcmp(memberType.name, string))
7014          return memberType;
7015    }
7016    return null;
7017 }
7018
7019 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7020 {
7021    Type memberType;
7022    for(memberType = type.members.first; memberType; memberType = memberType.next)
7023    {
7024       if(!memberType.name)
7025       {
7026          Type subType = FindMember(memberType, string);
7027          if(subType)
7028          {
7029             *offset += memberType.offset;
7030             return subType;
7031          }
7032       }
7033       else if(!strcmp(memberType.name, string))
7034       {
7035          *offset += memberType.offset;
7036          return memberType;
7037       }
7038    }
7039    return null;
7040 }
7041
7042 public bool GetParseError() { return parseError; }
7043
7044 Expression ParseExpressionString(char * expression)
7045 {
7046    parseError = false;
7047
7048    fileInput = TempFile { };
7049    fileInput.Write(expression, 1, strlen(expression));
7050    fileInput.Seek(0, start);
7051
7052    echoOn = false;
7053    parsedExpression = null;
7054    resetScanner();
7055    expression_yyparse();
7056    delete fileInput;
7057
7058    return parsedExpression;
7059 }
7060
7061 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7062 {
7063    Identifier id = exp.identifier;
7064    Method method = null;
7065    Property prop = null;
7066    DataMember member = null;
7067    ClassProperty classProp = null;
7068
7069    if(_class && _class.type == enumClass)
7070    {
7071       NamedLink value = null;
7072       Class enumClass = eSystem_FindClass(privateModule, "enum");
7073       if(enumClass)
7074       {
7075          Class baseClass;
7076          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7077          {
7078             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7079             for(value = e.values.first; value; value = value.next)
7080             {
7081                if(!strcmp(value.name, id.string))
7082                   break;
7083             }
7084             if(value)
7085             {
7086                char constant[256];
7087
7088                FreeExpContents(exp);
7089
7090                exp.type = constantExp;
7091                exp.isConstant = true;
7092                if(!strcmp(baseClass.dataTypeString, "int"))
7093                   sprintf(constant, "%d",(int)value.data);
7094                else
7095                   sprintf(constant, "0x%X",(int)value.data);
7096                exp.constant = CopyString(constant);
7097                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7098                exp.expType = MkClassType(baseClass.fullName);
7099                break;
7100             }
7101          }
7102       }
7103       if(value)
7104          return true;
7105    }
7106    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7107    {
7108       ProcessMethodType(method);
7109       exp.expType = Type
7110       {
7111          refCount = 1;
7112          kind = methodType;
7113          method = method;
7114          // Crash here?
7115          // TOCHECK: Put it back to what it was...
7116          // methodClass = _class;
7117          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7118       };
7119       //id._class = null;
7120       return true;
7121    }
7122    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7123    {
7124       if(!prop.dataType)
7125          ProcessPropertyType(prop);
7126       exp.expType = prop.dataType;
7127       if(prop.dataType) prop.dataType.refCount++;
7128       return true;
7129    }
7130    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7131    {
7132       if(!member.dataType)
7133          member.dataType = ProcessTypeString(member.dataTypeString, false);
7134       exp.expType = member.dataType;
7135       if(member.dataType) member.dataType.refCount++;
7136       return true;
7137    }
7138    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7139    {
7140       if(!classProp.dataType)
7141          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7142
7143       if(classProp.constant)
7144       {
7145          FreeExpContents(exp);
7146
7147          exp.isConstant = true;
7148          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7149          {
7150             //char constant[256];
7151             exp.type = stringExp;
7152             exp.constant = QMkString((char *)classProp.Get(_class));
7153          }
7154          else
7155          {
7156             char constant[256];
7157             exp.type = constantExp;
7158             sprintf(constant, "%d", (int)classProp.Get(_class));
7159             exp.constant = CopyString(constant);
7160          }
7161       }
7162       else
7163       {
7164          // TO IMPLEMENT...
7165       }
7166
7167       exp.expType = classProp.dataType;
7168       if(classProp.dataType) classProp.dataType.refCount++;
7169       return true;
7170    }
7171    return false;
7172 }
7173
7174 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7175 {
7176    BinaryTree * tree = &nameSpace.functions;
7177    GlobalData data = (GlobalData)tree->FindString(name);
7178    NameSpace * child;
7179    if(!data)
7180    {
7181       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7182       {
7183          data = ScanGlobalData(child, name);
7184          if(data)
7185             break;
7186       }
7187    }
7188    return data;
7189 }
7190
7191 static GlobalData FindGlobalData(char * name)
7192 {
7193    int start = 0, c;
7194    NameSpace * nameSpace;
7195    nameSpace = globalData;
7196    for(c = 0; name[c]; c++)
7197    {
7198       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7199       {
7200          NameSpace * newSpace;
7201          char * spaceName = new char[c - start + 1];
7202          strncpy(spaceName, name + start, c - start);
7203          spaceName[c-start] = '\0';
7204          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7205          delete spaceName;
7206          if(!newSpace)
7207             return null;
7208          nameSpace = newSpace;
7209          if(name[c] == ':') c++;
7210          start = c+1;
7211       }
7212    }
7213    if(c - start)
7214    {
7215       return ScanGlobalData(nameSpace, name + start);
7216    }
7217    return null;
7218 }
7219
7220 static int definedExpStackPos;
7221 static void * definedExpStack[512];
7222
7223 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7224 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7225 {
7226    Expression prev = checkedExp.prev, next = checkedExp.next;
7227
7228    FreeExpContents(checkedExp);
7229    FreeType(checkedExp.expType);
7230    FreeType(checkedExp.destType);
7231
7232    *checkedExp = *newExp;
7233
7234    delete newExp;
7235
7236    checkedExp.prev = prev;
7237    checkedExp.next = next;
7238 }
7239
7240 void ApplyAnyObjectLogic(Expression e)
7241 {
7242    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7243 #ifdef _DEBUG
7244    char debugExpString[4096];
7245    debugExpString[0] = '\0';
7246    PrintExpression(e, debugExpString);
7247 #endif
7248
7249    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7250    {
7251       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7252       //ellipsisDestType = destType;
7253       if(e && e.expType)
7254       {
7255          Type type = e.expType;
7256          Class _class = null;
7257          //Type destType = e.destType;
7258
7259          if(type.kind == classType && type._class && type._class.registered)
7260          {
7261             _class = type._class.registered;
7262          }
7263          else if(type.kind == subClassType)
7264          {
7265             _class = FindClass("ecere::com::Class").registered;
7266          }
7267          else
7268          {
7269             char string[1024] = "";
7270             Symbol classSym;
7271
7272             PrintTypeNoConst(type, string, false, true);
7273             classSym = FindClass(string);
7274             if(classSym) _class = classSym.registered;
7275          }
7276
7277          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...
7278             (!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))) ||
7279             destType.byReference)))
7280          {
7281             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7282             {
7283                Expression checkedExp = e, newExp;
7284
7285                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7286                {
7287                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7288                   {
7289                      if(checkedExp.type == extensionCompoundExp)
7290                      {
7291                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7292                      }
7293                      else
7294                         checkedExp = checkedExp.list->last;
7295                   }
7296                   else if(checkedExp.type == castExp)
7297                      checkedExp = checkedExp.cast.exp;
7298                }
7299
7300                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7301                {
7302                   newExp = checkedExp.op.exp2;
7303                   checkedExp.op.exp2 = null;
7304                   FreeExpContents(checkedExp);
7305
7306                   if(e.expType && e.expType.passAsTemplate)
7307                   {
7308                      char size[100];
7309                      ComputeTypeSize(e.expType);
7310                      sprintf(size, "%d", e.expType.size);
7311                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7312                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7313                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7314                   }
7315
7316                   ReplaceExpContents(checkedExp, newExp);
7317                   e.byReference = true;
7318                }
7319                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7320                {
7321                   Expression checkedExp; //, newExp;
7322
7323                   {
7324                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7325                      bool hasAddress =
7326                         e.type == identifierExp ||
7327                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7328                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7329                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7330                         e.type == indexExp;
7331
7332                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7333                      {
7334                         Context context = PushContext();
7335                         Declarator decl;
7336                         OldList * specs = MkList();
7337                         char typeString[1024];
7338                         Expression newExp { };
7339
7340                         typeString[0] = '\0';
7341                         *newExp = *e;
7342
7343                         //if(e.destType) e.destType.refCount++;
7344                         // if(exp.expType) exp.expType.refCount++;
7345                         newExp.prev = null;
7346                         newExp.next = null;
7347                         newExp.expType = null;
7348
7349                         PrintTypeNoConst(e.expType, typeString, false, true);
7350                         decl = SpecDeclFromString(typeString, specs, null);
7351                         newExp.destType = ProcessType(specs, decl);
7352
7353                         curContext = context;
7354
7355                         // We need a current compound for this
7356                         if(curCompound)
7357                         {
7358                            char name[100];
7359                            OldList * stmts = MkList();
7360                            e.type = extensionCompoundExp;
7361                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7362                            if(!curCompound.compound.declarations)
7363                               curCompound.compound.declarations = MkList();
7364                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7365                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7366                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7367                            e.compound = MkCompoundStmt(null, stmts);
7368                         }
7369                         else
7370                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7371
7372                         /*
7373                         e.compound = MkCompoundStmt(
7374                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7375                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7376
7377                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7378                         */
7379
7380                         {
7381                            Type type = e.destType;
7382                            e.destType = { };
7383                            CopyTypeInto(e.destType, type);
7384                            e.destType.refCount = 1;
7385                            e.destType.classObjectType = none;
7386                            FreeType(type);
7387                         }
7388
7389                         e.compound.compound.context = context;
7390                         PopContext(context);
7391                         curContext = context.parent;
7392                      }
7393                   }
7394
7395                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7396                   checkedExp = e;
7397                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7398                   {
7399                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7400                      {
7401                         if(checkedExp.type == extensionCompoundExp)
7402                         {
7403                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7404                         }
7405                         else
7406                            checkedExp = checkedExp.list->last;
7407                      }
7408                      else if(checkedExp.type == castExp)
7409                         checkedExp = checkedExp.cast.exp;
7410                   }
7411                   {
7412                      Expression operand { };
7413                      operand = *checkedExp;
7414                      checkedExp.destType = null;
7415                      checkedExp.expType = null;
7416                      checkedExp.Clear();
7417                      checkedExp.type = opExp;
7418                      checkedExp.op.op = '&';
7419                      checkedExp.op.exp1 = null;
7420                      checkedExp.op.exp2 = operand;
7421
7422                      //newExp = MkExpOp(null, '&', checkedExp);
7423                   }
7424                   //ReplaceExpContents(checkedExp, newExp);
7425                }
7426             }
7427          }
7428       }
7429    }
7430    {
7431       // If expression type is a simple class, make it an address
7432       // FixReference(e, true);
7433    }
7434 //#if 0
7435    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7436       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7437          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7438    {
7439       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"))
7440       {
7441          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7442       }
7443       else
7444       {
7445          Expression thisExp { };
7446
7447          *thisExp = *e;
7448          thisExp.prev = null;
7449          thisExp.next = null;
7450          e.Clear();
7451
7452          e.type = bracketsExp;
7453          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7454          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7455             ((Expression)e.list->first).byReference = true;
7456
7457          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7458          {
7459             e.expType = thisExp.expType;
7460             e.expType.refCount++;
7461          }
7462          else*/
7463          {
7464             e.expType = { };
7465             CopyTypeInto(e.expType, thisExp.expType);
7466             e.expType.byReference = false;
7467             e.expType.refCount = 1;
7468
7469             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7470                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7471             {
7472                e.expType.classObjectType = none;
7473             }
7474          }
7475       }
7476    }
7477 // TOFIX: Try this for a nice IDE crash!
7478 //#endif
7479    // The other way around
7480    else
7481 //#endif
7482    if(destType && e.expType &&
7483          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7484          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7485          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7486    {
7487       if(destType.kind == ellipsisType)
7488       {
7489          Compiler_Error($"Unspecified type\n");
7490       }
7491       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7492       {
7493          bool byReference = e.expType.byReference;
7494          Expression thisExp { };
7495          Declarator decl;
7496          OldList * specs = MkList();
7497          char typeString[1024]; // Watch buffer overruns
7498          Type type;
7499          ClassObjectType backupClassObjectType;
7500          bool backupByReference;
7501
7502          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7503             type = e.expType;
7504          else
7505             type = destType;
7506
7507          backupClassObjectType = type.classObjectType;
7508          backupByReference = type.byReference;
7509
7510          type.classObjectType = none;
7511          type.byReference = false;
7512
7513          typeString[0] = '\0';
7514          PrintType(type, typeString, false, true);
7515          decl = SpecDeclFromString(typeString, specs, null);
7516
7517          type.classObjectType = backupClassObjectType;
7518          type.byReference = backupByReference;
7519
7520          *thisExp = *e;
7521          thisExp.prev = null;
7522          thisExp.next = null;
7523          e.Clear();
7524
7525          if( ( type.kind == classType && type._class && type._class.registered &&
7526                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7527                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7528              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7529              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7530          {
7531             e.type = opExp;
7532             e.op.op = '*';
7533             e.op.exp1 = null;
7534             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7535
7536             e.expType = { };
7537             CopyTypeInto(e.expType, type);
7538             e.expType.byReference = false;
7539             e.expType.refCount = 1;
7540          }
7541          else
7542          {
7543             e.type = castExp;
7544             e.cast.typeName = MkTypeName(specs, decl);
7545             e.cast.exp = thisExp;
7546             e.byReference = true;
7547             e.expType = type;
7548             type.refCount++;
7549          }
7550          e.destType = destType;
7551          destType.refCount++;
7552       }
7553    }
7554 }
7555
7556 void ProcessExpressionType(Expression exp)
7557 {
7558    bool unresolved = false;
7559    Location oldyylloc = yylloc;
7560    bool notByReference = false;
7561 #ifdef _DEBUG
7562    char debugExpString[4096];
7563    debugExpString[0] = '\0';
7564    PrintExpression(exp, debugExpString);
7565 #endif
7566    if(!exp || exp.expType)
7567       return;
7568
7569    //eSystem_Logf("%s\n", expString);
7570
7571    // Testing this here
7572    yylloc = exp.loc;
7573    switch(exp.type)
7574    {
7575       case identifierExp:
7576       {
7577          Identifier id = exp.identifier;
7578          if(!id || !topContext) return;
7579
7580          // DOING THIS LATER NOW...
7581          if(id._class && id._class.name)
7582          {
7583             id.classSym = id._class.symbol; // FindClass(id._class.name);
7584             /* TODO: Name Space Fix ups
7585             if(!id.classSym)
7586                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7587             */
7588          }
7589
7590          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7591          {
7592             exp.expType = ProcessTypeString("Module", true);
7593             break;
7594          }
7595          else */if(strstr(id.string, "__ecereClass") == id.string)
7596          {
7597             exp.expType = ProcessTypeString("ecere::com::Class", true);
7598             break;
7599          }
7600          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7601          {
7602             // Added this here as well
7603             ReplaceClassMembers(exp, thisClass);
7604             if(exp.type != identifierExp)
7605             {
7606                ProcessExpressionType(exp);
7607                break;
7608             }
7609
7610             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7611                break;
7612          }
7613          else
7614          {
7615             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7616             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7617             if(!symbol/* && exp.destType*/)
7618             {
7619                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7620                   break;
7621                else
7622                {
7623                   if(thisClass)
7624                   {
7625                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7626                      if(exp.type != identifierExp)
7627                      {
7628                         ProcessExpressionType(exp);
7629                         break;
7630                      }
7631                   }
7632                   // Static methods called from inside the _class
7633                   else if(currentClass && !id._class)
7634                   {
7635                      if(ResolveIdWithClass(exp, currentClass, true))
7636                         break;
7637                   }
7638                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7639                }
7640             }
7641
7642             // If we manage to resolve this symbol
7643             if(symbol)
7644             {
7645                Type type = symbol.type;
7646                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7647
7648                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7649                {
7650                   Context context = SetupTemplatesContext(_class);
7651                   type = ReplaceThisClassType(_class);
7652                   FinishTemplatesContext(context);
7653                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7654                }
7655
7656                FreeSpecifier(id._class);
7657                id._class = null;
7658                delete id.string;
7659                id.string = CopyString(symbol.string);
7660
7661                id.classSym = null;
7662                exp.expType = type;
7663                if(type)
7664                   type.refCount++;
7665
7666                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7667                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7668                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7669                   // Add missing cases here... enum Classes...
7670                   exp.isConstant = true;
7671
7672                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7673                if(symbol.isParam || !strcmp(id.string, "this"))
7674                {
7675                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7676                      exp.byReference = true;
7677
7678                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7679                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7680                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7681                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7682                   {
7683                      Identifier id = exp.identifier;
7684                      exp.type = bracketsExp;
7685                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7686                   }*/
7687                }
7688
7689                if(symbol.isIterator)
7690                {
7691                   if(symbol.isIterator == 3)
7692                   {
7693                      exp.type = bracketsExp;
7694                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7695                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7696                      exp.expType = null;
7697                      ProcessExpressionType(exp);
7698                   }
7699                   else if(symbol.isIterator != 4)
7700                   {
7701                      exp.type = memberExp;
7702                      exp.member.exp = MkExpIdentifier(exp.identifier);
7703                      exp.member.exp.expType = exp.expType;
7704                      /*if(symbol.isIterator == 6)
7705                         exp.member.member = MkIdentifier("key");
7706                      else*/
7707                         exp.member.member = MkIdentifier("data");
7708                      exp.expType = null;
7709                      ProcessExpressionType(exp);
7710                   }
7711                }
7712                break;
7713             }
7714             else
7715             {
7716                DefinedExpression definedExp = null;
7717                if(thisNameSpace && !(id._class && !id._class.name))
7718                {
7719                   char name[1024];
7720                   strcpy(name, thisNameSpace);
7721                   strcat(name, "::");
7722                   strcat(name, id.string);
7723                   definedExp = eSystem_FindDefine(privateModule, name);
7724                }
7725                if(!definedExp)
7726                   definedExp = eSystem_FindDefine(privateModule, id.string);
7727                if(definedExp)
7728                {
7729                   int c;
7730                   for(c = 0; c<definedExpStackPos; c++)
7731                      if(definedExpStack[c] == definedExp)
7732                         break;
7733                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7734                   {
7735                      Location backupYylloc = yylloc;
7736                      File backInput = fileInput;
7737                      definedExpStack[definedExpStackPos++] = definedExp;
7738
7739                      fileInput = TempFile { };
7740                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7741                      fileInput.Seek(0, start);
7742
7743                      echoOn = false;
7744                      parsedExpression = null;
7745                      resetScanner();
7746                      expression_yyparse();
7747                      delete fileInput;
7748                      if(backInput)
7749                         fileInput = backInput;
7750
7751                      yylloc = backupYylloc;
7752
7753                      if(parsedExpression)
7754                      {
7755                         FreeIdentifier(id);
7756                         exp.type = bracketsExp;
7757                         exp.list = MkListOne(parsedExpression);
7758                         parsedExpression.loc = yylloc;
7759                         ProcessExpressionType(exp);
7760                         definedExpStackPos--;
7761                         return;
7762                      }
7763                      definedExpStackPos--;
7764                   }
7765                   else
7766                   {
7767                      if(inCompiler)
7768                      {
7769                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7770                      }
7771                   }
7772                }
7773                else
7774                {
7775                   GlobalData data = null;
7776                   if(thisNameSpace && !(id._class && !id._class.name))
7777                   {
7778                      char name[1024];
7779                      strcpy(name, thisNameSpace);
7780                      strcat(name, "::");
7781                      strcat(name, id.string);
7782                      data = FindGlobalData(name);
7783                   }
7784                   if(!data)
7785                      data = FindGlobalData(id.string);
7786                   if(data)
7787                   {
7788                      DeclareGlobalData(data);
7789                      exp.expType = data.dataType;
7790                      if(data.dataType) data.dataType.refCount++;
7791
7792                      delete id.string;
7793                      id.string = CopyString(data.fullName);
7794                      FreeSpecifier(id._class);
7795                      id._class = null;
7796
7797                      break;
7798                   }
7799                   else
7800                   {
7801                      GlobalFunction function = null;
7802                      if(thisNameSpace && !(id._class && !id._class.name))
7803                      {
7804                         char name[1024];
7805                         strcpy(name, thisNameSpace);
7806                         strcat(name, "::");
7807                         strcat(name, id.string);
7808                         function = eSystem_FindFunction(privateModule, name);
7809                      }
7810                      if(!function)
7811                         function = eSystem_FindFunction(privateModule, id.string);
7812                      if(function)
7813                      {
7814                         char name[1024];
7815                         delete id.string;
7816                         id.string = CopyString(function.name);
7817                         name[0] = 0;
7818
7819                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7820                            strcpy(name, "__ecereFunction_");
7821                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7822                         if(DeclareFunction(function, name))
7823                         {
7824                            delete id.string;
7825                            id.string = CopyString(name);
7826                         }
7827                         exp.expType = function.dataType;
7828                         if(function.dataType) function.dataType.refCount++;
7829
7830                         FreeSpecifier(id._class);
7831                         id._class = null;
7832
7833                         break;
7834                      }
7835                   }
7836                }
7837             }
7838          }
7839          unresolved = true;
7840          break;
7841       }
7842       case instanceExp:
7843       {
7844          Class _class;
7845          // Symbol classSym;
7846
7847          if(!exp.instance._class)
7848          {
7849             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7850             {
7851                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7852             }
7853          }
7854
7855          //classSym = FindClass(exp.instance._class.fullName);
7856          //_class = classSym ? classSym.registered : null;
7857
7858          ProcessInstantiationType(exp.instance);
7859          exp.isConstant = exp.instance.isConstant;
7860
7861          /*
7862          if(_class.type == unitClass && _class.base.type != systemClass)
7863          {
7864             {
7865                Type destType = exp.destType;
7866
7867                exp.destType = MkClassType(_class.base.fullName);
7868                exp.expType = MkClassType(_class.fullName);
7869                CheckExpressionType(exp, exp.destType, true);
7870
7871                exp.destType = destType;
7872             }
7873             exp.expType = MkClassType(_class.fullName);
7874          }
7875          else*/
7876          if(exp.instance._class)
7877          {
7878             exp.expType = MkClassType(exp.instance._class.name);
7879             /*if(exp.expType._class && exp.expType._class.registered &&
7880                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7881                exp.expType.byReference = true;*/
7882          }
7883          break;
7884       }
7885       case constantExp:
7886       {
7887          if(!exp.expType)
7888          {
7889             char * constant = exp.constant;
7890             Type type
7891             {
7892                refCount = 1;
7893                constant = true;
7894             };
7895             exp.expType = type;
7896
7897             if(constant[0] == '\'')
7898             {
7899                if((int)((byte *)constant)[1] > 127)
7900                {
7901                   int nb;
7902                   unichar ch = UTF8GetChar(constant + 1, &nb);
7903                   if(nb < 2) ch = constant[1];
7904                   delete constant;
7905                   exp.constant = PrintUInt(ch);
7906                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7907                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7908                   type._class = FindClass("unichar");
7909
7910                   type.isSigned = false;
7911                }
7912                else
7913                {
7914                   type.kind = charType;
7915                   type.isSigned = true;
7916                }
7917             }
7918             else
7919             {
7920                char * dot = strchr(constant, '.');
7921                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7922                char * exponent;
7923                if(isHex)
7924                {
7925                   exponent = strchr(constant, 'p');
7926                   if(!exponent) exponent = strchr(constant, 'P');
7927                }
7928                else
7929                {
7930                   exponent = strchr(constant, 'e');
7931                   if(!exponent) exponent = strchr(constant, 'E');
7932                }
7933
7934                if(dot || exponent)
7935                {
7936                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7937                      type.kind = floatType;
7938                   else
7939                      type.kind = doubleType;
7940                   type.isSigned = true;
7941                }
7942                else
7943                {
7944                   bool isSigned = constant[0] == '-';
7945                   char * endP = null;
7946                   int64 i64 = strtoll(constant, &endP, 0);
7947                   uint64 ui64 = strtoull(constant, &endP, 0);
7948                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
7949                   if(isSigned)
7950                   {
7951                      if(i64 < MININT)
7952                         is64Bit = true;
7953                   }
7954                   else
7955                   {
7956                      if(ui64 > MAXINT)
7957                      {
7958                         if(ui64 > MAXDWORD)
7959                         {
7960                            is64Bit = true;
7961                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7962                               isSigned = true;
7963                         }
7964                      }
7965                      else if(constant[0] != '0' || !constant[1])
7966                         isSigned = true;
7967                   }
7968                   type.kind = is64Bit ? int64Type : intType;
7969                   type.isSigned = isSigned;
7970                }
7971             }
7972             exp.isConstant = true;
7973             if(exp.destType && exp.destType.kind == doubleType)
7974                type.kind = doubleType;
7975             else if(exp.destType && exp.destType.kind == floatType)
7976                type.kind = floatType;
7977             else if(exp.destType && exp.destType.kind == int64Type)
7978                type.kind = int64Type;
7979          }
7980          break;
7981       }
7982       case stringExp:
7983       {
7984          exp.isConstant = true;      // Why wasn't this constant?
7985          exp.expType = Type
7986          {
7987             refCount = 1;
7988             kind = pointerType;
7989             type = Type
7990             {
7991                refCount = 1;
7992                kind = charType;
7993                constant = true;
7994                isSigned = true;
7995             }
7996          };
7997          break;
7998       }
7999       case newExp:
8000       case new0Exp:
8001          ProcessExpressionType(exp._new.size);
8002          exp.expType = Type
8003          {
8004             refCount = 1;
8005             kind = pointerType;
8006             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8007          };
8008          DeclareType(exp.expType.type, false, false);
8009          break;
8010       case renewExp:
8011       case renew0Exp:
8012          ProcessExpressionType(exp._renew.size);
8013          ProcessExpressionType(exp._renew.exp);
8014          exp.expType = Type
8015          {
8016             refCount = 1;
8017             kind = pointerType;
8018             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8019          };
8020          DeclareType(exp.expType.type, false, false);
8021          break;
8022       case opExp:
8023       {
8024          bool assign = false, boolResult = false, boolOps = false;
8025          Type type1 = null, type2 = null;
8026          bool useDestType = false, useSideType = false;
8027          Location oldyylloc = yylloc;
8028          bool useSideUnit = false;
8029          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8030
8031          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8032          Type dummy
8033          {
8034             count = 1;
8035             refCount = 1;
8036          };
8037
8038          switch(exp.op.op)
8039          {
8040             // Assignment Operators
8041             case '=':
8042             case MUL_ASSIGN:
8043             case DIV_ASSIGN:
8044             case MOD_ASSIGN:
8045             case ADD_ASSIGN:
8046             case SUB_ASSIGN:
8047             case LEFT_ASSIGN:
8048             case RIGHT_ASSIGN:
8049             case AND_ASSIGN:
8050             case XOR_ASSIGN:
8051             case OR_ASSIGN:
8052                assign = true;
8053                break;
8054             // boolean Operators
8055             case '!':
8056                // Expect boolean operators
8057                //boolOps = true;
8058                //boolResult = true;
8059                break;
8060             case AND_OP:
8061             case OR_OP:
8062                // Expect boolean operands
8063                boolOps = true;
8064                boolResult = true;
8065                break;
8066             // Comparisons
8067             case EQ_OP:
8068             case '<':
8069             case '>':
8070             case LE_OP:
8071             case GE_OP:
8072             case NE_OP:
8073                // Gives boolean result
8074                boolResult = true;
8075                useSideType = true;
8076                break;
8077             case '+':
8078             case '-':
8079                useSideUnit = true;
8080                useSideType = true;
8081                useDestType = true;
8082                break;
8083
8084             case LEFT_OP:
8085             case RIGHT_OP:
8086                useSideType = true;
8087                useDestType = true;
8088                break;
8089
8090             case '|':
8091             case '^':
8092                useSideType = true;
8093                useDestType = true;
8094                break;
8095
8096             case '/':
8097             case '%':
8098                useSideType = true;
8099                useDestType = true;
8100                break;
8101             case '&':
8102             case '*':
8103                if(exp.op.exp1)
8104                {
8105                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8106                   useSideType = true;
8107                   useDestType = true;
8108                }
8109                break;
8110
8111             /*// Implement speed etc.
8112             case '*':
8113             case '/':
8114                break;
8115             */
8116          }
8117          if(exp.op.op == '&')
8118          {
8119             // Added this here earlier for Iterator address as key
8120             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8121             {
8122                Identifier id = exp.op.exp2.identifier;
8123                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8124                if(symbol && symbol.isIterator == 2)
8125                {
8126                   exp.type = memberExp;
8127                   exp.member.exp = exp.op.exp2;
8128                   exp.member.member = MkIdentifier("key");
8129                   exp.expType = null;
8130                   exp.op.exp2.expType = symbol.type;
8131                   symbol.type.refCount++;
8132                   ProcessExpressionType(exp);
8133                   FreeType(dummy);
8134                   break;
8135                }
8136                // exp.op.exp2.usage.usageRef = true;
8137             }
8138          }
8139
8140          //dummy.kind = TypeDummy;
8141          if(exp.op.exp1)
8142          {
8143             // Added this check here to use the dest type only for units derived from the base unit
8144             // So that untyped units will use the side unit as opposed to the untyped destination unit
8145             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8146             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8147                useDestType = false;
8148
8149             if(destClass && useDestType &&
8150               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8151
8152               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8153             {
8154                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8155                exp.op.exp1.destType = exp.destType;
8156                exp.op.exp1.opDestType = true;
8157                if(exp.destType)
8158                   exp.destType.refCount++;
8159             }
8160             else if(!assign)
8161             {
8162                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8163                exp.op.exp1.destType = dummy;
8164                dummy.refCount++;
8165             }
8166
8167             // TESTING THIS HERE...
8168             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8169             ProcessExpressionType(exp.op.exp1);
8170             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8171
8172             exp.op.exp1.opDestType = false;
8173
8174             // Fix for unit and ++ / --
8175             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8176                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8177             {
8178                exp.op.exp2 = MkExpConstant("1");
8179                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8180                assign = true;
8181             }
8182
8183             if(exp.op.exp1.destType == dummy)
8184             {
8185                FreeType(dummy);
8186                exp.op.exp1.destType = null;
8187             }
8188             type1 = exp.op.exp1.expType;
8189          }
8190
8191          if(exp.op.exp2)
8192          {
8193             char expString[10240];
8194             expString[0] = '\0';
8195             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8196             {
8197                if(exp.op.exp1)
8198                {
8199                   exp.op.exp2.destType = exp.op.exp1.expType;
8200                   if(exp.op.exp1.expType)
8201                      exp.op.exp1.expType.refCount++;
8202                }
8203                else
8204                {
8205                   exp.op.exp2.destType = exp.destType;
8206                   if(!exp.op.exp1 || exp.op.op != '&')
8207                      exp.op.exp2.opDestType = true;
8208                   if(exp.destType)
8209                      exp.destType.refCount++;
8210                }
8211
8212                if(type1) type1.refCount++;
8213                exp.expType = type1;
8214             }
8215             else if(assign)
8216             {
8217                if(inCompiler)
8218                   PrintExpression(exp.op.exp2, expString);
8219
8220                if(type1 && type1.kind == pointerType)
8221                {
8222                   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 ||
8223                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8224                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8225                   else if(exp.op.op == '=')
8226                   {
8227                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8228                      exp.op.exp2.destType = type1;
8229                      if(type1)
8230                         type1.refCount++;
8231                   }
8232                }
8233                else
8234                {
8235                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8236                   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/* ||
8237                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8238                   else
8239                   {
8240                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8241                      exp.op.exp2.destType = type1;
8242                      if(type1)
8243                         type1.refCount++;
8244                   }
8245                }
8246                if(type1) type1.refCount++;
8247                exp.expType = type1;
8248             }
8249             else if(destClass &&
8250                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8251                   (destClass.type == enumClass && useDestType)))
8252             {
8253                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8254                exp.op.exp2.destType = exp.destType;
8255                if(exp.op.op != '&')
8256                   exp.op.exp2.opDestType = true;
8257                if(exp.destType)
8258                   exp.destType.refCount++;
8259             }
8260             else
8261             {
8262                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8263                exp.op.exp2.destType = dummy;
8264                dummy.refCount++;
8265             }
8266
8267             // TESTING THIS HERE... (DANGEROUS)
8268             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8269                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8270             {
8271                FreeType(exp.op.exp2.destType);
8272                exp.op.exp2.destType = type1;
8273                type1.refCount++;
8274             }
8275             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8276             // Cannot lose the cast on a sizeof
8277             if(exp.op.op == SIZEOF)
8278             {
8279                Expression e = exp.op.exp2;
8280                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8281                {
8282                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8283                   {
8284                      if(e.type == extensionCompoundExp)
8285                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8286                      else
8287                         e = e.list->last;
8288                   }
8289                }
8290                if(e.type == castExp && e.cast.exp)
8291                   e.cast.exp.needCast = true;
8292             }
8293             ProcessExpressionType(exp.op.exp2);
8294             exp.op.exp2.opDestType = false;
8295             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8296
8297             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8298             {
8299                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)
8300                {
8301                   if(exp.op.op != '=' && type1.type.kind == voidType)
8302                      Compiler_Error($"void *: unknown size\n");
8303                }
8304                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||
8305                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8306                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8307                               exp.op.exp2.expType._class.registered.type == structClass ||
8308                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8309                {
8310                   if(exp.op.op == ADD_ASSIGN)
8311                      Compiler_Error($"cannot add two pointers\n");
8312                }
8313                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8314                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8315                {
8316                   if(exp.op.op == ADD_ASSIGN)
8317                      Compiler_Error($"cannot add two pointers\n");
8318                }
8319                else if(inCompiler)
8320                {
8321                   char type1String[1024];
8322                   char type2String[1024];
8323                   type1String[0] = '\0';
8324                   type2String[0] = '\0';
8325
8326                   PrintType(exp.op.exp2.expType, type1String, false, true);
8327                   PrintType(type1, type2String, false, true);
8328                   ChangeCh(expString, '\n', ' ');
8329                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8330                }
8331             }
8332
8333             if(exp.op.exp2.destType == dummy)
8334             {
8335                FreeType(dummy);
8336                exp.op.exp2.destType = null;
8337             }
8338
8339             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8340             {
8341                type2 = { };
8342                type2.refCount = 1;
8343                CopyTypeInto(type2, exp.op.exp2.expType);
8344                type2.isSigned = true;
8345             }
8346             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8347             {
8348                type2 = { kind = intType };
8349                type2.refCount = 1;
8350                type2.isSigned = true;
8351             }
8352             else
8353             {
8354                type2 = exp.op.exp2.expType;
8355                if(type2) type2.refCount++;
8356             }
8357          }
8358
8359          dummy.kind = voidType;
8360
8361          if(exp.op.op == SIZEOF)
8362          {
8363             exp.expType = Type
8364             {
8365                refCount = 1;
8366                kind = intSizeType;
8367             };
8368             exp.isConstant = true;
8369          }
8370          // Get type of dereferenced pointer
8371          else if(exp.op.op == '*' && !exp.op.exp1)
8372          {
8373             exp.expType = Dereference(type2);
8374             if(type2 && type2.kind == classType)
8375                notByReference = true;
8376          }
8377          else if(exp.op.op == '&' && !exp.op.exp1)
8378             exp.expType = Reference(type2);
8379          else if(!assign)
8380          {
8381             if(boolOps)
8382             {
8383                if(exp.op.exp1)
8384                {
8385                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8386                   exp.op.exp1.destType = MkClassType("bool");
8387                   exp.op.exp1.destType.truth = true;
8388                   if(!exp.op.exp1.expType)
8389                      ProcessExpressionType(exp.op.exp1);
8390                   else
8391                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8392                   FreeType(exp.op.exp1.expType);
8393                   exp.op.exp1.expType = MkClassType("bool");
8394                   exp.op.exp1.expType.truth = true;
8395                }
8396                if(exp.op.exp2)
8397                {
8398                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8399                   exp.op.exp2.destType = MkClassType("bool");
8400                   exp.op.exp2.destType.truth = true;
8401                   if(!exp.op.exp2.expType)
8402                      ProcessExpressionType(exp.op.exp2);
8403                   else
8404                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8405                   FreeType(exp.op.exp2.expType);
8406                   exp.op.exp2.expType = MkClassType("bool");
8407                   exp.op.exp2.expType.truth = true;
8408                }
8409             }
8410             else if(exp.op.exp1 && exp.op.exp2 &&
8411                ((useSideType /*&&
8412                      (useSideUnit ||
8413                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8414                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8415                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8416                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8417             {
8418                if(type1 && type2 &&
8419                   // If either both are class or both are not class
8420                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8421                {
8422                   // Added this check for enum subtraction to result in an int type:
8423                   if(exp.op.op == '-' &&
8424                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8425                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8426                   {
8427                      Type intType;
8428                      if(!type1._class.registered.dataType)
8429                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8430                      if(!type2._class.registered.dataType)
8431                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8432
8433                      intType = ProcessTypeString(
8434                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8435
8436                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8437                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8438                      exp.op.exp1.destType = intType;
8439                      exp.op.exp2.destType = intType;
8440                      intType.refCount++;
8441                   }
8442                   else
8443                   {
8444                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8445                      exp.op.exp2.destType = type1;
8446                      type1.refCount++;
8447                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8448                      exp.op.exp1.destType = type2;
8449                      type2.refCount++;
8450                   }
8451
8452                   // Warning here for adding Radians + Degrees with no destination type
8453                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8454                      type1._class.registered && type1._class.registered.type == unitClass &&
8455                      type2._class.registered && type2._class.registered.type == unitClass &&
8456                      type1._class.registered != type2._class.registered)
8457                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8458                         type1._class.string, type2._class.string, type1._class.string);
8459
8460                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8461                   {
8462                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8463                      if(argExp)
8464                      {
8465                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8466
8467                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8468                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8469                            exp.op.exp1)));
8470
8471                         ProcessExpressionType(exp.op.exp1);
8472
8473                         if(type2.kind != pointerType)
8474                         {
8475                            ProcessExpressionType(classExp);
8476
8477                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8478                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8479                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8480                                  // noHeadClass
8481                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8482                                     OR_OP,
8483                                  // normalClass
8484                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8485                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8486                                        MkPointer(null, null), null)))),
8487                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8488
8489                            if(!exp.op.exp2.expType)
8490                            {
8491                               if(type2)
8492                                  FreeType(type2);
8493                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8494                               type2.refCount++;
8495                            }
8496
8497                            ProcessExpressionType(exp.op.exp2);
8498                         }
8499                      }
8500                   }
8501
8502                   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)))
8503                   {
8504                      if(type1.kind != classType && type1.type.kind == voidType)
8505                         Compiler_Error($"void *: unknown size\n");
8506                      exp.expType = type1;
8507                      if(type1) type1.refCount++;
8508                   }
8509                   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)))
8510                   {
8511                      if(type2.kind != classType && type2.type.kind == voidType)
8512                         Compiler_Error($"void *: unknown size\n");
8513                      exp.expType = type2;
8514                      if(type2) type2.refCount++;
8515                   }
8516                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8517                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8518                   {
8519                      Compiler_Warning($"different levels of indirection\n");
8520                   }
8521                   else
8522                   {
8523                      bool success = false;
8524                      if(type1.kind == pointerType && type2.kind == pointerType)
8525                      {
8526                         if(exp.op.op == '+')
8527                            Compiler_Error($"cannot add two pointers\n");
8528                         else if(exp.op.op == '-')
8529                         {
8530                            // Pointer Subtraction gives integer
8531                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8532                            {
8533                               exp.expType = Type
8534                               {
8535                                  kind = intType;
8536                                  refCount = 1;
8537                               };
8538                               success = true;
8539
8540                               if(type1.type.kind == templateType)
8541                               {
8542                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8543                                  if(argExp)
8544                                  {
8545                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8546
8547                                     ProcessExpressionType(classExp);
8548
8549                                     exp.type = bracketsExp;
8550                                     exp.list = MkListOne(MkExpOp(
8551                                        MkExpBrackets(MkListOne(MkExpOp(
8552                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8553                                              , exp.op.op,
8554                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8555
8556                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8557
8558                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8559                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8560                                                 // noHeadClass
8561                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8562                                                    OR_OP,
8563                                                 // normalClass
8564                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8565                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8566                                                       MkPointer(null, null), null)))),
8567                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8568
8569
8570                                              ));
8571
8572                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8573                                     FreeType(dummy);
8574                                     return;
8575                                  }
8576                               }
8577                            }
8578                         }
8579                      }
8580
8581                      if(!success && exp.op.exp1.type == constantExp)
8582                      {
8583                         // If first expression is constant, try to match that first
8584                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8585                         {
8586                            if(exp.expType) FreeType(exp.expType);
8587                            exp.expType = exp.op.exp1.destType;
8588                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8589                            success = true;
8590                         }
8591                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8592                         {
8593                            if(exp.expType) FreeType(exp.expType);
8594                            exp.expType = exp.op.exp2.destType;
8595                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8596                            success = true;
8597                         }
8598                      }
8599                      else if(!success)
8600                      {
8601                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8602                         {
8603                            if(exp.expType) FreeType(exp.expType);
8604                            exp.expType = exp.op.exp2.destType;
8605                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8606                            success = true;
8607                         }
8608                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8609                         {
8610                            if(exp.expType) FreeType(exp.expType);
8611                            exp.expType = exp.op.exp1.destType;
8612                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8613                            success = true;
8614                         }
8615                      }
8616                      if(!success)
8617                      {
8618                         char expString1[10240];
8619                         char expString2[10240];
8620                         char type1[1024];
8621                         char type2[1024];
8622                         expString1[0] = '\0';
8623                         expString2[0] = '\0';
8624                         type1[0] = '\0';
8625                         type2[0] = '\0';
8626                         if(inCompiler)
8627                         {
8628                            PrintExpression(exp.op.exp1, expString1);
8629                            ChangeCh(expString1, '\n', ' ');
8630                            PrintExpression(exp.op.exp2, expString2);
8631                            ChangeCh(expString2, '\n', ' ');
8632                            PrintType(exp.op.exp1.expType, type1, false, true);
8633                            PrintType(exp.op.exp2.expType, type2, false, true);
8634                         }
8635
8636                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8637                      }
8638                   }
8639                }
8640                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8641                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8642                {
8643                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8644                   // Convert e.g. / 4 into / 4.0
8645                   exp.op.exp1.destType = type2._class.registered.dataType;
8646                   if(type2._class.registered.dataType)
8647                      type2._class.registered.dataType.refCount++;
8648                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8649                   exp.expType = type2;
8650                   if(type2) type2.refCount++;
8651                }
8652                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8653                {
8654                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8655                   // Convert e.g. / 4 into / 4.0
8656                   exp.op.exp2.destType = type1._class.registered.dataType;
8657                   if(type1._class.registered.dataType)
8658                      type1._class.registered.dataType.refCount++;
8659                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8660                   exp.expType = type1;
8661                   if(type1) type1.refCount++;
8662                }
8663                else if(type1)
8664                {
8665                   bool valid = false;
8666
8667                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8668                   {
8669                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8670
8671                      if(!type1._class.registered.dataType)
8672                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8673                      exp.op.exp2.destType = type1._class.registered.dataType;
8674                      exp.op.exp2.destType.refCount++;
8675
8676                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8677                      if(type2)
8678                         FreeType(type2);
8679                      type2 = exp.op.exp2.destType;
8680                      if(type2) type2.refCount++;
8681
8682                      exp.expType = type2;
8683                      type2.refCount++;
8684                   }
8685
8686                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8687                   {
8688                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8689
8690                      if(!type2._class.registered.dataType)
8691                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8692                      exp.op.exp1.destType = type2._class.registered.dataType;
8693                      exp.op.exp1.destType.refCount++;
8694
8695                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8696                      type1 = exp.op.exp1.destType;
8697                      exp.expType = type1;
8698                      type1.refCount++;
8699                   }
8700
8701                   // TESTING THIS NEW CODE
8702                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8703                   {
8704                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8705                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8706                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8707                      {
8708                         // Convert the enum to an int instead for these operators
8709                         if(op1IsEnum && exp.op.exp2.expType)
8710                         {
8711                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8712                            {
8713                               if(exp.expType) FreeType(exp.expType);
8714                               exp.expType = exp.op.exp2.expType;
8715                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8716                               valid = true;
8717                            }
8718                         }
8719                         else if(op2IsEnum && exp.op.exp1.expType)
8720                         {
8721                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8722                            {
8723                               if(exp.expType) FreeType(exp.expType);
8724                               exp.expType = exp.op.exp1.expType;
8725                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8726                               valid = true;
8727                            }
8728                         }
8729                      }
8730                      else
8731                      {
8732                         if(op1IsEnum && exp.op.exp2.expType)
8733                         {
8734                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8735                            {
8736                               if(exp.expType) FreeType(exp.expType);
8737                               exp.expType = exp.op.exp1.expType;
8738                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8739                               valid = true;
8740                            }
8741                         }
8742                         else if(op2IsEnum && exp.op.exp1.expType)
8743                         {
8744                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8745                            {
8746                               if(exp.expType) FreeType(exp.expType);
8747                               exp.expType = exp.op.exp2.expType;
8748                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8749                               valid = true;
8750                            }
8751                         }
8752                      }
8753                   }
8754
8755                   if(!valid)
8756                   {
8757                      // 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
8758                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8759                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8760                      {
8761                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8762                         exp.op.exp1.destType = type2;
8763                         type2.refCount++;
8764
8765                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8766                         {
8767                            if(exp.expType) FreeType(exp.expType);
8768                            exp.expType = exp.op.exp1.destType;
8769                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8770                         }
8771                      }
8772                      else
8773                      {
8774                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8775                         exp.op.exp2.destType = type1;
8776                         type1.refCount++;
8777
8778                      /*
8779                      // Maybe this was meant to be an enum...
8780                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8781                      {
8782                         Type oldType = exp.op.exp2.expType;
8783                         exp.op.exp2.expType = null;
8784                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8785                            FreeType(oldType);
8786                         else
8787                            exp.op.exp2.expType = oldType;
8788                      }
8789                      */
8790
8791                      /*
8792                      // TESTING THIS HERE... LATEST ADDITION
8793                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8794                      {
8795                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8796                         exp.op.exp2.destType = type2._class.registered.dataType;
8797                         if(type2._class.registered.dataType)
8798                            type2._class.registered.dataType.refCount++;
8799                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8800
8801                         //exp.expType = type2._class.registered.dataType; //type2;
8802                         //if(type2) type2.refCount++;
8803                      }
8804
8805                      // TESTING THIS HERE... LATEST ADDITION
8806                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8807                      {
8808                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8809                         exp.op.exp1.destType = type1._class.registered.dataType;
8810                         if(type1._class.registered.dataType)
8811                            type1._class.registered.dataType.refCount++;
8812                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8813                         exp.expType = type1._class.registered.dataType; //type1;
8814                         if(type1) type1.refCount++;
8815                      }
8816                      */
8817
8818                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8819                         {
8820                            if(exp.expType) FreeType(exp.expType);
8821                            exp.expType = exp.op.exp2.destType;
8822                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8823                         }
8824                         else if(type1 && type2)
8825                         {
8826                            char expString1[10240];
8827                            char expString2[10240];
8828                            char type1String[1024];
8829                            char type2String[1024];
8830                            expString1[0] = '\0';
8831                            expString2[0] = '\0';
8832                            type1String[0] = '\0';
8833                            type2String[0] = '\0';
8834                            if(inCompiler)
8835                            {
8836                               PrintExpression(exp.op.exp1, expString1);
8837                               ChangeCh(expString1, '\n', ' ');
8838                               PrintExpression(exp.op.exp2, expString2);
8839                               ChangeCh(expString2, '\n', ' ');
8840                               PrintType(exp.op.exp1.expType, type1String, false, true);
8841                               PrintType(exp.op.exp2.expType, type2String, false, true);
8842                            }
8843
8844                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8845
8846                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8847                            {
8848                               exp.expType = exp.op.exp1.expType;
8849                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8850                            }
8851                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8852                            {
8853                               exp.expType = exp.op.exp2.expType;
8854                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8855                            }
8856                         }
8857                      }
8858                   }
8859                }
8860                else if(type2)
8861                {
8862                   // Maybe this was meant to be an enum...
8863                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8864                   {
8865                      Type oldType = exp.op.exp1.expType;
8866                      exp.op.exp1.expType = null;
8867                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8868                         FreeType(oldType);
8869                      else
8870                         exp.op.exp1.expType = oldType;
8871                   }
8872
8873                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8874                   exp.op.exp1.destType = type2;
8875                   type2.refCount++;
8876                   /*
8877                   // TESTING THIS HERE... LATEST ADDITION
8878                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8879                   {
8880                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8881                      exp.op.exp1.destType = type1._class.registered.dataType;
8882                      if(type1._class.registered.dataType)
8883                         type1._class.registered.dataType.refCount++;
8884                   }
8885
8886                   // TESTING THIS HERE... LATEST ADDITION
8887                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8888                   {
8889                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8890                      exp.op.exp2.destType = type2._class.registered.dataType;
8891                      if(type2._class.registered.dataType)
8892                         type2._class.registered.dataType.refCount++;
8893                   }
8894                   */
8895
8896                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8897                   {
8898                      if(exp.expType) FreeType(exp.expType);
8899                      exp.expType = exp.op.exp1.destType;
8900                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8901                   }
8902                }
8903             }
8904             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8905             {
8906                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8907                {
8908                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8909                   // Convert e.g. / 4 into / 4.0
8910                   exp.op.exp1.destType = type2._class.registered.dataType;
8911                   if(type2._class.registered.dataType)
8912                      type2._class.registered.dataType.refCount++;
8913                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8914                }
8915                if(exp.op.op == '!')
8916                {
8917                   exp.expType = MkClassType("bool");
8918                   exp.expType.truth = true;
8919                }
8920                else
8921                {
8922                   exp.expType = type2;
8923                   if(type2) type2.refCount++;
8924                }
8925             }
8926             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8927             {
8928                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8929                {
8930                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8931                   // Convert e.g. / 4 into / 4.0
8932                   exp.op.exp2.destType = type1._class.registered.dataType;
8933                   if(type1._class.registered.dataType)
8934                      type1._class.registered.dataType.refCount++;
8935                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8936                }
8937                exp.expType = type1;
8938                if(type1) type1.refCount++;
8939             }
8940          }
8941
8942          yylloc = exp.loc;
8943          if(exp.op.exp1 && !exp.op.exp1.expType)
8944          {
8945             char expString[10000];
8946             expString[0] = '\0';
8947             if(inCompiler)
8948             {
8949                PrintExpression(exp.op.exp1, expString);
8950                ChangeCh(expString, '\n', ' ');
8951             }
8952             if(expString[0])
8953                Compiler_Error($"couldn't determine type of %s\n", expString);
8954          }
8955          if(exp.op.exp2 && !exp.op.exp2.expType)
8956          {
8957             char expString[10240];
8958             expString[0] = '\0';
8959             if(inCompiler)
8960             {
8961                PrintExpression(exp.op.exp2, expString);
8962                ChangeCh(expString, '\n', ' ');
8963             }
8964             if(expString[0])
8965                Compiler_Error($"couldn't determine type of %s\n", expString);
8966          }
8967
8968          if(boolResult)
8969          {
8970             FreeType(exp.expType);
8971             exp.expType = MkClassType("bool");
8972             exp.expType.truth = true;
8973          }
8974
8975          if(exp.op.op != SIZEOF)
8976             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8977                (!exp.op.exp2 || exp.op.exp2.isConstant);
8978
8979          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8980          {
8981             DeclareType(exp.op.exp2.expType, false, false);
8982          }
8983
8984          yylloc = oldyylloc;
8985
8986          FreeType(dummy);
8987          if(type2)
8988             FreeType(type2);
8989          break;
8990       }
8991       case bracketsExp:
8992       case extensionExpressionExp:
8993       {
8994          Expression e;
8995          exp.isConstant = true;
8996          for(e = exp.list->first; e; e = e.next)
8997          {
8998             bool inced = false;
8999             if(!e.next)
9000             {
9001                FreeType(e.destType);
9002                e.opDestType = exp.opDestType;
9003                e.destType = exp.destType;
9004                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9005             }
9006             ProcessExpressionType(e);
9007             if(inced)
9008                exp.destType.count--;
9009             if(!exp.expType && !e.next)
9010             {
9011                exp.expType = e.expType;
9012                if(e.expType) e.expType.refCount++;
9013             }
9014             if(!e.isConstant)
9015                exp.isConstant = false;
9016          }
9017
9018          // In case a cast became a member...
9019          e = exp.list->first;
9020          if(!e.next && e.type == memberExp)
9021          {
9022             // Preserve prev, next
9023             Expression next = exp.next, prev = exp.prev;
9024
9025
9026             FreeType(exp.expType);
9027             FreeType(exp.destType);
9028             delete exp.list;
9029
9030             *exp = *e;
9031
9032             exp.prev = prev;
9033             exp.next = next;
9034
9035             delete e;
9036
9037             ProcessExpressionType(exp);
9038          }
9039          break;
9040       }
9041       case indexExp:
9042       {
9043          Expression e;
9044          exp.isConstant = true;
9045
9046          ProcessExpressionType(exp.index.exp);
9047          if(!exp.index.exp.isConstant)
9048             exp.isConstant = false;
9049
9050          if(exp.index.exp.expType)
9051          {
9052             Type source = exp.index.exp.expType;
9053             if(source.kind == classType && source._class && source._class.registered)
9054             {
9055                Class _class = source._class.registered;
9056                Class c = _class.templateClass ? _class.templateClass : _class;
9057                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9058                {
9059                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9060
9061                   if(exp.index.index && exp.index.index->last)
9062                   {
9063                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9064                   }
9065                }
9066             }
9067          }
9068
9069          for(e = exp.index.index->first; e; e = e.next)
9070          {
9071             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9072             {
9073                if(e.destType) FreeType(e.destType);
9074                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9075             }
9076             ProcessExpressionType(e);
9077             if(!e.next)
9078             {
9079                // Check if this type is int
9080             }
9081             if(!e.isConstant)
9082                exp.isConstant = false;
9083          }
9084
9085          if(!exp.expType)
9086             exp.expType = Dereference(exp.index.exp.expType);
9087          if(exp.expType)
9088             DeclareType(exp.expType, false, false);
9089          break;
9090       }
9091       case callExp:
9092       {
9093          Expression e;
9094          Type functionType;
9095          Type methodType = null;
9096          char name[1024];
9097          name[0] = '\0';
9098
9099          if(inCompiler)
9100          {
9101             PrintExpression(exp.call.exp,  name);
9102             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9103             {
9104                //exp.call.exp.expType = null;
9105                PrintExpression(exp.call.exp,  name);
9106             }
9107          }
9108          if(exp.call.exp.type == identifierExp)
9109          {
9110             Expression idExp = exp.call.exp;
9111             Identifier id = idExp.identifier;
9112             if(!strcmp(id.string, "__builtin_frame_address"))
9113             {
9114                exp.expType = ProcessTypeString("void *", true);
9115                if(exp.call.arguments && exp.call.arguments->first)
9116                   ProcessExpressionType(exp.call.arguments->first);
9117                break;
9118             }
9119             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9120             {
9121                exp.expType = ProcessTypeString("int", true);
9122                if(exp.call.arguments && exp.call.arguments->first)
9123                   ProcessExpressionType(exp.call.arguments->first);
9124                break;
9125             }
9126             else if(!strcmp(id.string, "Max") ||
9127                !strcmp(id.string, "Min") ||
9128                !strcmp(id.string, "Sgn") ||
9129                !strcmp(id.string, "Abs"))
9130             {
9131                Expression a = null;
9132                Expression b = null;
9133                Expression tempExp1 = null, tempExp2 = null;
9134                if((!strcmp(id.string, "Max") ||
9135                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9136                {
9137                   a = exp.call.arguments->first;
9138                   b = exp.call.arguments->last;
9139                   tempExp1 = a;
9140                   tempExp2 = b;
9141                }
9142                else if(exp.call.arguments->count == 1)
9143                {
9144                   a = exp.call.arguments->first;
9145                   tempExp1 = a;
9146                }
9147
9148                if(a)
9149                {
9150                   exp.call.arguments->Clear();
9151                   idExp.identifier = null;
9152
9153                   FreeExpContents(exp);
9154
9155                   ProcessExpressionType(a);
9156                   if(b)
9157                      ProcessExpressionType(b);
9158
9159                   exp.type = bracketsExp;
9160                   exp.list = MkList();
9161
9162                   if(a.expType && (!b || b.expType))
9163                   {
9164                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9165                      {
9166                         // Use the simpleStruct name/ids for now...
9167                         if(inCompiler)
9168                         {
9169                            OldList * specs = MkList();
9170                            OldList * decls = MkList();
9171                            Declaration decl;
9172                            char temp1[1024], temp2[1024];
9173
9174                            GetTypeSpecs(a.expType, specs);
9175
9176                            if(a && !a.isConstant && a.type != identifierExp)
9177                            {
9178                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9179                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9180                               tempExp1 = QMkExpId(temp1);
9181                               tempExp1.expType = a.expType;
9182                               if(a.expType)
9183                                  a.expType.refCount++;
9184                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9185                            }
9186                            if(b && !b.isConstant && b.type != identifierExp)
9187                            {
9188                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9189                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9190                               tempExp2 = QMkExpId(temp2);
9191                               tempExp2.expType = b.expType;
9192                               if(b.expType)
9193                                  b.expType.refCount++;
9194                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9195                            }
9196
9197                            decl = MkDeclaration(specs, decls);
9198                            if(!curCompound.compound.declarations)
9199                               curCompound.compound.declarations = MkList();
9200                            curCompound.compound.declarations->Insert(null, decl);
9201                         }
9202                      }
9203                   }
9204
9205                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9206                   {
9207                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9208                      ListAdd(exp.list,
9209                         MkExpCondition(MkExpBrackets(MkListOne(
9210                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9211                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9212                      exp.expType = a.expType;
9213                      if(a.expType)
9214                         a.expType.refCount++;
9215                   }
9216                   else if(!strcmp(id.string, "Abs"))
9217                   {
9218                      ListAdd(exp.list,
9219                         MkExpCondition(MkExpBrackets(MkListOne(
9220                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9221                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9222                      exp.expType = a.expType;
9223                      if(a.expType)
9224                         a.expType.refCount++;
9225                   }
9226                   else if(!strcmp(id.string, "Sgn"))
9227                   {
9228                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9229                      ListAdd(exp.list,
9230                         MkExpCondition(MkExpBrackets(MkListOne(
9231                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9232                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9233                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9234                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9235                      exp.expType = ProcessTypeString("int", false);
9236                   }
9237
9238                   FreeExpression(tempExp1);
9239                   if(tempExp2) FreeExpression(tempExp2);
9240
9241                   FreeIdentifier(id);
9242                   break;
9243                }
9244             }
9245          }
9246
9247          {
9248             Type dummy
9249             {
9250                count = 1;
9251                refCount = 1;
9252             };
9253             if(!exp.call.exp.destType)
9254             {
9255                exp.call.exp.destType = dummy;
9256                dummy.refCount++;
9257             }
9258             ProcessExpressionType(exp.call.exp);
9259             if(exp.call.exp.destType == dummy)
9260             {
9261                FreeType(dummy);
9262                exp.call.exp.destType = null;
9263             }
9264             FreeType(dummy);
9265          }
9266
9267          // Check argument types against parameter types
9268          functionType = exp.call.exp.expType;
9269
9270          if(functionType && functionType.kind == TypeKind::methodType)
9271          {
9272             methodType = functionType;
9273             functionType = methodType.method.dataType;
9274
9275             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9276             // TOCHECK: Instead of doing this here could this be done per param?
9277             if(exp.call.exp.expType.usedClass)
9278             {
9279                char typeString[1024];
9280                typeString[0] = '\0';
9281                {
9282                   Symbol back = functionType.thisClass;
9283                   // Do not output class specifier here (thisclass was added to this)
9284                   functionType.thisClass = null;
9285                   PrintType(functionType, typeString, true, true);
9286                   functionType.thisClass = back;
9287                }
9288                if(strstr(typeString, "thisclass"))
9289                {
9290                   OldList * specs = MkList();
9291                   Declarator decl;
9292                   {
9293                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9294
9295                      decl = SpecDeclFromString(typeString, specs, null);
9296
9297                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9298                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9299                         exp.call.exp.expType.usedClass))
9300                         thisClassParams = false;
9301
9302                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9303                      {
9304                         Class backupThisClass = thisClass;
9305                         thisClass = exp.call.exp.expType.usedClass;
9306                         ProcessDeclarator(decl);
9307                         thisClass = backupThisClass;
9308                      }
9309
9310                      thisClassParams = true;
9311
9312                      functionType = ProcessType(specs, decl);
9313                      functionType.refCount = 0;
9314                      FinishTemplatesContext(context);
9315                   }
9316
9317                   FreeList(specs, FreeSpecifier);
9318                   FreeDeclarator(decl);
9319                 }
9320             }
9321          }
9322          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9323          {
9324             Type type = functionType.type;
9325             if(!functionType.refCount)
9326             {
9327                functionType.type = null;
9328                FreeType(functionType);
9329             }
9330             //methodType = functionType;
9331             functionType = type;
9332          }
9333          if(functionType && functionType.kind != TypeKind::functionType)
9334          {
9335             Compiler_Error($"called object %s is not a function\n", name);
9336          }
9337          else if(functionType)
9338          {
9339             bool emptyParams = false, noParams = false;
9340             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9341             Type type = functionType.params.first;
9342             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9343             int extra = 0;
9344             Location oldyylloc = yylloc;
9345
9346             if(!type) emptyParams = true;
9347
9348             // WORKING ON THIS:
9349             if(functionType.extraParam && e && functionType.thisClass)
9350             {
9351                e.destType = MkClassType(functionType.thisClass.string);
9352                e = e.next;
9353             }
9354
9355             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9356             // Fixed #141 by adding '&& !functionType.extraParam'
9357             if(!functionType.staticMethod && !functionType.extraParam)
9358             {
9359                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9360                   memberExp.member.exp.expType._class)
9361                {
9362                   type = MkClassType(memberExp.member.exp.expType._class.string);
9363                   if(e)
9364                   {
9365                      e.destType = type;
9366                      e = e.next;
9367                      type = functionType.params.first;
9368                   }
9369                   else
9370                      type.refCount = 0;
9371                }
9372                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9373                {
9374                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9375                   type.byReference = functionType.byReference;
9376                   type.typedByReference = functionType.typedByReference;
9377                   if(e)
9378                   {
9379                      // Allow manually passing a class for typed object
9380                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9381                         e = e.next;
9382                      e.destType = type;
9383                      e = e.next;
9384                      type = functionType.params.first;
9385                   }
9386                   else
9387                      type.refCount = 0;
9388                   //extra = 1;
9389                }
9390             }
9391
9392             if(type && type.kind == voidType)
9393             {
9394                noParams = true;
9395                if(!type.refCount) FreeType(type);
9396                type = null;
9397             }
9398
9399             for( ; e; e = e.next)
9400             {
9401                if(!type && !emptyParams)
9402                {
9403                   yylloc = e.loc;
9404                   if(methodType && methodType.methodClass)
9405                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9406                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9407                         noParams ? 0 : functionType.params.count);
9408                   else
9409                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9410                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9411                         noParams ? 0 : functionType.params.count);
9412                   break;
9413                }
9414
9415                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9416                {
9417                   Type templatedType = null;
9418                   Class _class = methodType.usedClass;
9419                   ClassTemplateParameter curParam = null;
9420                   int id = 0;
9421                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9422                   {
9423                      Class sClass;
9424                      for(sClass = _class; sClass; sClass = sClass.base)
9425                      {
9426                         if(sClass.templateClass) sClass = sClass.templateClass;
9427                         id = 0;
9428                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9429                         {
9430                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9431                            {
9432                               Class nextClass;
9433                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9434                               {
9435                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9436                                  id += nextClass.templateParams.count;
9437                               }
9438                               break;
9439                            }
9440                            id++;
9441                         }
9442                         if(curParam) break;
9443                      }
9444                   }
9445                   if(curParam && _class.templateArgs[id].dataTypeString)
9446                   {
9447                      ClassTemplateArgument arg = _class.templateArgs[id];
9448                      {
9449                         Context context = SetupTemplatesContext(_class);
9450
9451                         /*if(!arg.dataType)
9452                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9453                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9454                         FinishTemplatesContext(context);
9455                      }
9456                      e.destType = templatedType;
9457                      if(templatedType)
9458                      {
9459                         templatedType.passAsTemplate = true;
9460                         // templatedType.refCount++;
9461                      }
9462                   }
9463                   else
9464                   {
9465                      e.destType = type;
9466                      if(type) type.refCount++;
9467                   }
9468                }
9469                else
9470                {
9471                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9472                   {
9473                      e.destType = type.prev;
9474                      e.destType.refCount++;
9475                   }
9476                   else
9477                   {
9478                      e.destType = type;
9479                      if(type) type.refCount++;
9480                   }
9481                }
9482                // Don't reach the end for the ellipsis
9483                if(type && type.kind != ellipsisType)
9484                {
9485                   Type next = type.next;
9486                   if(!type.refCount) FreeType(type);
9487                   type = next;
9488                }
9489             }
9490
9491             if(type && type.kind != ellipsisType)
9492             {
9493                if(methodType && methodType.methodClass)
9494                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9495                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9496                      functionType.params.count + extra);
9497                else
9498                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9499                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9500                      functionType.params.count + extra);
9501             }
9502             yylloc = oldyylloc;
9503             if(type && !type.refCount) FreeType(type);
9504          }
9505          else
9506          {
9507             functionType = Type
9508             {
9509                refCount = 0;
9510                kind = TypeKind::functionType;
9511             };
9512
9513             if(exp.call.exp.type == identifierExp)
9514             {
9515                char * string = exp.call.exp.identifier.string;
9516                if(inCompiler)
9517                {
9518                   Symbol symbol;
9519                   Location oldyylloc = yylloc;
9520
9521                   yylloc = exp.call.exp.identifier.loc;
9522                   if(strstr(string, "__builtin_") == string)
9523                   {
9524                      if(exp.destType)
9525                      {
9526                         functionType.returnType = exp.destType;
9527                         exp.destType.refCount++;
9528                      }
9529                   }
9530                   else
9531                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9532                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9533                   globalContext.symbols.Add((BTNode)symbol);
9534                   if(strstr(symbol.string, "::"))
9535                      globalContext.hasNameSpace = true;
9536
9537                   yylloc = oldyylloc;
9538                }
9539             }
9540             else if(exp.call.exp.type == memberExp)
9541             {
9542                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9543                   exp.call.exp.member.member.string);*/
9544             }
9545             else
9546                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9547
9548             if(!functionType.returnType)
9549             {
9550                functionType.returnType = Type
9551                {
9552                   refCount = 1;
9553                   kind = intType;
9554                };
9555             }
9556          }
9557          if(functionType && functionType.kind == TypeKind::functionType)
9558          {
9559             exp.expType = functionType.returnType;
9560
9561             if(functionType.returnType)
9562                functionType.returnType.refCount++;
9563
9564             if(!functionType.refCount)
9565                FreeType(functionType);
9566          }
9567
9568          if(exp.call.arguments)
9569          {
9570             for(e = exp.call.arguments->first; e; e = e.next)
9571             {
9572                Type destType = e.destType;
9573                ProcessExpressionType(e);
9574             }
9575          }
9576          break;
9577       }
9578       case memberExp:
9579       {
9580          Type type;
9581          Location oldyylloc = yylloc;
9582          bool thisPtr;
9583          Expression checkExp = exp.member.exp;
9584          while(checkExp)
9585          {
9586             if(checkExp.type == castExp)
9587                checkExp = checkExp.cast.exp;
9588             else if(checkExp.type == bracketsExp)
9589                checkExp = checkExp.list ? checkExp.list->first : null;
9590             else
9591                break;
9592          }
9593
9594          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9595          exp.thisPtr = thisPtr;
9596
9597          // DOING THIS LATER NOW...
9598          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9599          {
9600             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9601             /* TODO: Name Space Fix ups
9602             if(!exp.member.member.classSym)
9603                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9604             */
9605          }
9606
9607          ProcessExpressionType(exp.member.exp);
9608          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9609             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9610          {
9611             exp.isConstant = false;
9612          }
9613          else
9614             exp.isConstant = exp.member.exp.isConstant;
9615          type = exp.member.exp.expType;
9616
9617          yylloc = exp.loc;
9618
9619          if(type && (type.kind == templateType))
9620          {
9621             Class _class = thisClass ? thisClass : currentClass;
9622             ClassTemplateParameter param = null;
9623             if(_class)
9624             {
9625                for(param = _class.templateParams.first; param; param = param.next)
9626                {
9627                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9628                      break;
9629                }
9630             }
9631             if(param && param.defaultArg.member)
9632             {
9633                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9634                if(argExp)
9635                {
9636                   Expression expMember = exp.member.exp;
9637                   Declarator decl;
9638                   OldList * specs = MkList();
9639                   char thisClassTypeString[1024];
9640
9641                   FreeIdentifier(exp.member.member);
9642
9643                   ProcessExpressionType(argExp);
9644
9645                   {
9646                      char * colon = strstr(param.defaultArg.memberString, "::");
9647                      if(colon)
9648                      {
9649                         char className[1024];
9650                         Class sClass;
9651
9652                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9653                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9654                      }
9655                      else
9656                         strcpy(thisClassTypeString, _class.fullName);
9657                   }
9658
9659                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9660
9661                   exp.expType = ProcessType(specs, decl);
9662                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9663                   {
9664                      Class expClass = exp.expType._class.registered;
9665                      Class cClass = null;
9666                      int c;
9667                      int paramCount = 0;
9668                      int lastParam = -1;
9669
9670                      char templateString[1024];
9671                      ClassTemplateParameter param;
9672                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9673                      for(cClass = expClass; cClass; cClass = cClass.base)
9674                      {
9675                         int p = 0;
9676                         for(param = cClass.templateParams.first; param; param = param.next)
9677                         {
9678                            int id = p;
9679                            Class sClass;
9680                            ClassTemplateArgument arg;
9681                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9682                            arg = expClass.templateArgs[id];
9683
9684                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9685                            {
9686                               ClassTemplateParameter cParam;
9687                               //int p = numParams - sClass.templateParams.count;
9688                               int p = 0;
9689                               Class nextClass;
9690                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9691
9692                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9693                               {
9694                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9695                                  {
9696                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9697                                     {
9698                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9699                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9700                                        break;
9701                                     }
9702                                  }
9703                               }
9704                            }
9705
9706                            {
9707                               char argument[256];
9708                               argument[0] = '\0';
9709                               /*if(arg.name)
9710                               {
9711                                  strcat(argument, arg.name.string);
9712                                  strcat(argument, " = ");
9713                               }*/
9714                               switch(param.type)
9715                               {
9716                                  case expression:
9717                                  {
9718                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9719                                     char expString[1024];
9720                                     OldList * specs = MkList();
9721                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9722                                     Expression exp;
9723                                     char * string = PrintHexUInt64(arg.expression.ui64);
9724                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9725                                     delete string;
9726
9727                                     ProcessExpressionType(exp);
9728                                     ComputeExpression(exp);
9729                                     expString[0] = '\0';
9730                                     PrintExpression(exp, expString);
9731                                     strcat(argument, expString);
9732                                     // delete exp;
9733                                     FreeExpression(exp);
9734                                     break;
9735                                  }
9736                                  case identifier:
9737                                  {
9738                                     strcat(argument, arg.member.name);
9739                                     break;
9740                                  }
9741                                  case TemplateParameterType::type:
9742                                  {
9743                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9744                                     {
9745                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9746                                           strcat(argument, thisClassTypeString);
9747                                        else
9748                                           strcat(argument, arg.dataTypeString);
9749                                     }
9750                                     break;
9751                                  }
9752                               }
9753                               if(argument[0])
9754                               {
9755                                  if(paramCount) strcat(templateString, ", ");
9756                                  if(lastParam != p - 1)
9757                                  {
9758                                     strcat(templateString, param.name);
9759                                     strcat(templateString, " = ");
9760                                  }
9761                                  strcat(templateString, argument);
9762                                  paramCount++;
9763                                  lastParam = p;
9764                               }
9765                               p++;
9766                            }
9767                         }
9768                      }
9769                      {
9770                         int len = strlen(templateString);
9771                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9772                         templateString[len++] = '>';
9773                         templateString[len++] = '\0';
9774                      }
9775                      {
9776                         Context context = SetupTemplatesContext(_class);
9777                         FreeType(exp.expType);
9778                         exp.expType = ProcessTypeString(templateString, false);
9779                         FinishTemplatesContext(context);
9780                      }
9781                   }
9782
9783                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9784                   exp.type = bracketsExp;
9785                   exp.list = MkListOne(MkExpOp(null, '*',
9786                   /*opExp;
9787                   exp.op.op = '*';
9788                   exp.op.exp1 = null;
9789                   exp.op.exp2 = */
9790                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9791                      MkExpBrackets(MkListOne(
9792                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9793                            '+',
9794                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9795                            '+',
9796                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9797
9798                            ));
9799                }
9800             }
9801             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9802                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9803             {
9804                type = ProcessTemplateParameterType(type.templateParameter);
9805             }
9806          }
9807          // TODO: *** This seems to be where we should add method support for all basic types ***
9808          if(type && (type.kind == templateType));
9809          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9810                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9811                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9812                           (type.kind == pointerType && type.type.kind == charType)))
9813          {
9814             Identifier id = exp.member.member;
9815             TypeKind typeKind = type.kind;
9816             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9817             if(typeKind == subClassType && exp.member.exp.type == classExp)
9818             {
9819                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9820                typeKind = classType;
9821             }
9822
9823             if(id)
9824             {
9825                if(typeKind == intType || typeKind == enumType)
9826                   _class = eSystem_FindClass(privateModule, "int");
9827                else if(!_class)
9828                {
9829                   if(type.kind == classType && type._class && type._class.registered)
9830                   {
9831                      _class = type._class.registered;
9832                   }
9833                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9834                   {
9835                      _class = FindClass("char *").registered;
9836                   }
9837                   else if(type.kind == pointerType)
9838                   {
9839                      _class = eSystem_FindClass(privateModule, "uintptr");
9840                      FreeType(exp.expType);
9841                      exp.expType = ProcessTypeString("uintptr", false);
9842                      exp.byReference = true;
9843                   }
9844                   else
9845                   {
9846                      char string[1024] = "";
9847                      Symbol classSym;
9848                      PrintTypeNoConst(type, string, false, true);
9849                      classSym = FindClass(string);
9850                      if(classSym) _class = classSym.registered;
9851                   }
9852                }
9853             }
9854
9855             if(_class && id)
9856             {
9857                /*bool thisPtr =
9858                   (exp.member.exp.type == identifierExp &&
9859                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9860                Property prop = null;
9861                Method method = null;
9862                DataMember member = null;
9863                Property revConvert = null;
9864                ClassProperty classProp = null;
9865
9866                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9867                   exp.member.memberType = propertyMember;
9868
9869                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9870                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9871
9872                if(typeKind != subClassType)
9873                {
9874                   // Prioritize data members over properties for "this"
9875                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9876                   {
9877                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9878                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9879                      {
9880                         prop = eClass_FindProperty(_class, id.string, privateModule);
9881                         if(prop)
9882                            member = null;
9883                      }
9884                      if(!member && !prop)
9885                         prop = eClass_FindProperty(_class, id.string, privateModule);
9886                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9887                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9888                         exp.member.thisPtr = true;
9889                   }
9890                   // Prioritize properties over data members otherwise
9891                   else
9892                   {
9893                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9894                      if(!id.classSym)
9895                      {
9896                         prop = eClass_FindProperty(_class, id.string, null);
9897                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9898                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9899                      }
9900
9901                      if(!prop && !member)
9902                      {
9903                         method = eClass_FindMethod(_class, id.string, null);
9904                         if(!method)
9905                         {
9906                            prop = eClass_FindProperty(_class, id.string, privateModule);
9907                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9908                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9909                         }
9910                      }
9911
9912                      if(member && prop)
9913                      {
9914                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9915                            prop = null;
9916                         else
9917                            member = null;
9918                      }
9919                   }
9920                }
9921                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9922                   method = eClass_FindMethod(_class, id.string, privateModule);
9923                if(!prop && !member && !method)
9924                {
9925                   if(typeKind == subClassType)
9926                   {
9927                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9928                      if(classProp)
9929                      {
9930                         exp.member.memberType = classPropertyMember;
9931                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9932                      }
9933                      else
9934                      {
9935                         // Assume this is a class_data member
9936                         char structName[1024];
9937                         Identifier id = exp.member.member;
9938                         Expression classExp = exp.member.exp;
9939                         type.refCount++;
9940
9941                         FreeType(classExp.expType);
9942                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9943
9944                         strcpy(structName, "__ecereClassData_");
9945                         FullClassNameCat(structName, type._class.string, false);
9946                         exp.type = pointerExp;
9947                         exp.member.member = id;
9948
9949                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9950                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9951                               MkExpBrackets(MkListOne(MkExpOp(
9952                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9953                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9954                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9955                                  )));
9956
9957                         FreeType(type);
9958
9959                         ProcessExpressionType(exp);
9960                         return;
9961                      }
9962                   }
9963                   else
9964                   {
9965                      // Check for reverse conversion
9966                      // (Convert in an instantiation later, so that we can use
9967                      //  deep properties system)
9968                      Symbol classSym = FindClass(id.string);
9969                      if(classSym)
9970                      {
9971                         Class convertClass = classSym.registered;
9972                         if(convertClass)
9973                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9974                      }
9975                   }
9976                }
9977
9978                if(prop)
9979                {
9980                   exp.member.memberType = propertyMember;
9981                   if(!prop.dataType)
9982                      ProcessPropertyType(prop);
9983                   exp.expType = prop.dataType;
9984                   if(prop.dataType) prop.dataType.refCount++;
9985                }
9986                else if(member)
9987                {
9988                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9989                   {
9990                      FreeExpContents(exp);
9991                      exp.type = identifierExp;
9992                      exp.identifier = MkIdentifier("class");
9993                      ProcessExpressionType(exp);
9994                      return;
9995                   }
9996
9997                   exp.member.memberType = dataMember;
9998                   DeclareStruct(_class.fullName, false);
9999                   if(!member.dataType)
10000                   {
10001                      Context context = SetupTemplatesContext(_class);
10002                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10003                      FinishTemplatesContext(context);
10004                   }
10005                   exp.expType = member.dataType;
10006                   if(member.dataType) member.dataType.refCount++;
10007                }
10008                else if(revConvert)
10009                {
10010                   exp.member.memberType = reverseConversionMember;
10011                   exp.expType = MkClassType(revConvert._class.fullName);
10012                }
10013                else if(method)
10014                {
10015                   //if(inCompiler)
10016                   {
10017                      /*if(id._class)
10018                      {
10019                         exp.type = identifierExp;
10020                         exp.identifier = exp.member.member;
10021                      }
10022                      else*/
10023                         exp.member.memberType = methodMember;
10024                   }
10025                   if(!method.dataType)
10026                      ProcessMethodType(method);
10027                   exp.expType = Type
10028                   {
10029                      refCount = 1;
10030                      kind = methodType;
10031                      method = method;
10032                   };
10033
10034                   // Tricky spot here... To use instance versus class virtual table
10035                   // Put it back to what it was... What did we break?
10036
10037                   // Had to put it back for overriding Main of Thread global instance
10038
10039                   //exp.expType.methodClass = _class;
10040                   exp.expType.methodClass = (id && id._class) ? _class : null;
10041
10042                   // Need the actual class used for templated classes
10043                   exp.expType.usedClass = _class;
10044                }
10045                else if(!classProp)
10046                {
10047                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10048                   {
10049                      FreeExpContents(exp);
10050                      exp.type = identifierExp;
10051                      exp.identifier = MkIdentifier("class");
10052                      FreeType(exp.expType);
10053                      exp.expType = MkClassType("ecere::com::Class");
10054                      return;
10055                   }
10056                   yylloc = exp.member.member.loc;
10057                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10058                   if(inCompiler)
10059                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10060                }
10061
10062                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10063                {
10064                   Class tClass;
10065
10066                   tClass = _class;
10067                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10068
10069                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10070                   {
10071                      int id = 0;
10072                      ClassTemplateParameter curParam = null;
10073                      Class sClass;
10074
10075                      for(sClass = tClass; sClass; sClass = sClass.base)
10076                      {
10077                         id = 0;
10078                         if(sClass.templateClass) sClass = sClass.templateClass;
10079                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10080                         {
10081                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10082                            {
10083                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10084                                  id += sClass.templateParams.count;
10085                               break;
10086                            }
10087                            id++;
10088                         }
10089                         if(curParam) break;
10090                      }
10091
10092                      if(curParam && tClass.templateArgs[id].dataTypeString)
10093                      {
10094                         ClassTemplateArgument arg = tClass.templateArgs[id];
10095                         Context context = SetupTemplatesContext(tClass);
10096                         /*if(!arg.dataType)
10097                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10098                         FreeType(exp.expType);
10099                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10100                         if(exp.expType)
10101                         {
10102                            if(exp.expType.kind == thisClassType)
10103                            {
10104                               FreeType(exp.expType);
10105                               exp.expType = ReplaceThisClassType(_class);
10106                            }
10107
10108                            if(tClass.templateClass)
10109                               exp.expType.passAsTemplate = true;
10110                            //exp.expType.refCount++;
10111                            if(!exp.destType)
10112                            {
10113                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10114                               //exp.destType.refCount++;
10115
10116                               if(exp.destType.kind == thisClassType)
10117                               {
10118                                  FreeType(exp.destType);
10119                                  exp.destType = ReplaceThisClassType(_class);
10120                               }
10121                            }
10122                         }
10123                         FinishTemplatesContext(context);
10124                      }
10125                   }
10126                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10127                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10128                   {
10129                      int id = 0;
10130                      ClassTemplateParameter curParam = null;
10131                      Class sClass;
10132
10133                      for(sClass = tClass; sClass; sClass = sClass.base)
10134                      {
10135                         id = 0;
10136                         if(sClass.templateClass) sClass = sClass.templateClass;
10137                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10138                         {
10139                            if(curParam.type == TemplateParameterType::type &&
10140                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10141                            {
10142                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10143                                  id += sClass.templateParams.count;
10144                               break;
10145                            }
10146                            id++;
10147                         }
10148                         if(curParam) break;
10149                      }
10150
10151                      if(curParam)
10152                      {
10153                         ClassTemplateArgument arg = tClass.templateArgs[id];
10154                         Context context = SetupTemplatesContext(tClass);
10155                         Type basicType;
10156                         /*if(!arg.dataType)
10157                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10158
10159                         basicType = ProcessTypeString(arg.dataTypeString, false);
10160                         if(basicType)
10161                         {
10162                            if(basicType.kind == thisClassType)
10163                            {
10164                               FreeType(basicType);
10165                               basicType = ReplaceThisClassType(_class);
10166                            }
10167
10168                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10169                            if(tClass.templateClass)
10170                               basicType.passAsTemplate = true;
10171                            */
10172
10173                            FreeType(exp.expType);
10174
10175                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10176                            //exp.expType.refCount++;
10177                            if(!exp.destType)
10178                            {
10179                               exp.destType = exp.expType;
10180                               exp.destType.refCount++;
10181                            }
10182
10183                            {
10184                               Expression newExp { };
10185                               OldList * specs = MkList();
10186                               Declarator decl;
10187                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10188                               *newExp = *exp;
10189                               if(exp.destType) exp.destType.refCount++;
10190                               if(exp.expType)  exp.expType.refCount++;
10191                               exp.type = castExp;
10192                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10193                               exp.cast.exp = newExp;
10194                               //FreeType(exp.expType);
10195                               //exp.expType = null;
10196                               //ProcessExpressionType(sourceExp);
10197                            }
10198                         }
10199                         FinishTemplatesContext(context);
10200                      }
10201                   }
10202                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10203                   {
10204                      Class expClass = exp.expType._class.registered;
10205                      if(expClass)
10206                      {
10207                         Class cClass = null;
10208                         int c;
10209                         int p = 0;
10210                         int paramCount = 0;
10211                         int lastParam = -1;
10212                         char templateString[1024];
10213                         ClassTemplateParameter param;
10214                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10215                         while(cClass != expClass)
10216                         {
10217                            Class sClass;
10218                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10219                            cClass = sClass;
10220
10221                            for(param = cClass.templateParams.first; param; param = param.next)
10222                            {
10223                               Class cClassCur = null;
10224                               int c;
10225                               int cp = 0;
10226                               ClassTemplateParameter paramCur = null;
10227                               ClassTemplateArgument arg;
10228                               while(cClassCur != tClass && !paramCur)
10229                               {
10230                                  Class sClassCur;
10231                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10232                                  cClassCur = sClassCur;
10233
10234                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10235                                  {
10236                                     if(!strcmp(paramCur.name, param.name))
10237                                     {
10238
10239                                        break;
10240                                     }
10241                                     cp++;
10242                                  }
10243                               }
10244                               if(paramCur && paramCur.type == TemplateParameterType::type)
10245                                  arg = tClass.templateArgs[cp];
10246                               else
10247                                  arg = expClass.templateArgs[p];
10248
10249                               {
10250                                  char argument[256];
10251                                  argument[0] = '\0';
10252                                  /*if(arg.name)
10253                                  {
10254                                     strcat(argument, arg.name.string);
10255                                     strcat(argument, " = ");
10256                                  }*/
10257                                  switch(param.type)
10258                                  {
10259                                     case expression:
10260                                     {
10261                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10262                                        char expString[1024];
10263                                        OldList * specs = MkList();
10264                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10265                                        Expression exp;
10266                                        char * string = PrintHexUInt64(arg.expression.ui64);
10267                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10268                                        delete string;
10269
10270                                        ProcessExpressionType(exp);
10271                                        ComputeExpression(exp);
10272                                        expString[0] = '\0';
10273                                        PrintExpression(exp, expString);
10274                                        strcat(argument, expString);
10275                                        // delete exp;
10276                                        FreeExpression(exp);
10277                                        break;
10278                                     }
10279                                     case identifier:
10280                                     {
10281                                        strcat(argument, arg.member.name);
10282                                        break;
10283                                     }
10284                                     case TemplateParameterType::type:
10285                                     {
10286                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10287                                           strcat(argument, arg.dataTypeString);
10288                                        break;
10289                                     }
10290                                  }
10291                                  if(argument[0])
10292                                  {
10293                                     if(paramCount) strcat(templateString, ", ");
10294                                     if(lastParam != p - 1)
10295                                     {
10296                                        strcat(templateString, param.name);
10297                                        strcat(templateString, " = ");
10298                                     }
10299                                     strcat(templateString, argument);
10300                                     paramCount++;
10301                                     lastParam = p;
10302                                  }
10303                               }
10304                               p++;
10305                            }
10306                         }
10307                         {
10308                            int len = strlen(templateString);
10309                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10310                            templateString[len++] = '>';
10311                            templateString[len++] = '\0';
10312                         }
10313
10314                         FreeType(exp.expType);
10315                         {
10316                            Context context = SetupTemplatesContext(tClass);
10317                            exp.expType = ProcessTypeString(templateString, false);
10318                            FinishTemplatesContext(context);
10319                         }
10320                      }
10321                   }
10322                }
10323             }
10324             else
10325                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10326          }
10327          else if(type && (type.kind == structType || type.kind == unionType))
10328          {
10329             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10330             if(memberType)
10331             {
10332                exp.expType = memberType;
10333                if(memberType)
10334                   memberType.refCount++;
10335             }
10336          }
10337          else
10338          {
10339             char expString[10240];
10340             expString[0] = '\0';
10341             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10342             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10343          }
10344
10345          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10346          {
10347             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10348             {
10349                Identifier id = exp.member.member;
10350                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10351                if(_class)
10352                {
10353                   FreeType(exp.expType);
10354                   exp.expType = ReplaceThisClassType(_class);
10355                }
10356             }
10357          }
10358          yylloc = oldyylloc;
10359          break;
10360       }
10361       // Convert x->y into (*x).y
10362       case pointerExp:
10363       {
10364          Type destType = exp.destType;
10365
10366          // DOING THIS LATER NOW...
10367          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10368          {
10369             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10370             /* TODO: Name Space Fix ups
10371             if(!exp.member.member.classSym)
10372                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10373             */
10374          }
10375
10376          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10377          exp.type = memberExp;
10378          if(destType)
10379             destType.count++;
10380          ProcessExpressionType(exp);
10381          if(destType)
10382             destType.count--;
10383          break;
10384       }
10385       case classSizeExp:
10386       {
10387          //ComputeExpression(exp);
10388
10389          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10390          if(classSym && classSym.registered)
10391          {
10392             if(classSym.registered.type == noHeadClass)
10393             {
10394                char name[1024];
10395                name[0] = '\0';
10396                DeclareStruct(classSym.string, false);
10397                FreeSpecifier(exp._class);
10398                exp.type = typeSizeExp;
10399                FullClassNameCat(name, classSym.string, false);
10400                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10401             }
10402             else
10403             {
10404                if(classSym.registered.fixed)
10405                {
10406                   FreeSpecifier(exp._class);
10407                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10408                   exp.type = constantExp;
10409                }
10410                else
10411                {
10412                   char className[1024];
10413                   strcpy(className, "__ecereClass_");
10414                   FullClassNameCat(className, classSym.string, true);
10415                   MangleClassName(className);
10416
10417                   DeclareClass(classSym, className);
10418
10419                   FreeExpContents(exp);
10420                   exp.type = pointerExp;
10421                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10422                   exp.member.member = MkIdentifier("structSize");
10423                }
10424             }
10425          }
10426
10427          exp.expType = Type
10428          {
10429             refCount = 1;
10430             kind = intSizeType;
10431          };
10432          // exp.isConstant = true;
10433          break;
10434       }
10435       case typeSizeExp:
10436       {
10437          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10438
10439          exp.expType = Type
10440          {
10441             refCount = 1;
10442             kind = intSizeType;
10443          };
10444          exp.isConstant = true;
10445
10446          DeclareType(type, false, false);
10447          FreeType(type);
10448          break;
10449       }
10450       case castExp:
10451       {
10452          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10453          type.count = 1;
10454          FreeType(exp.cast.exp.destType);
10455          exp.cast.exp.destType = type;
10456          type.refCount++;
10457          ProcessExpressionType(exp.cast.exp);
10458          type.count = 0;
10459          exp.expType = type;
10460          //type.refCount++;
10461
10462          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10463          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10464          {
10465             void * prev = exp.prev, * next = exp.next;
10466             Type expType = exp.cast.exp.destType;
10467             Expression castExp = exp.cast.exp;
10468             Type destType = exp.destType;
10469
10470             if(expType) expType.refCount++;
10471
10472             //FreeType(exp.destType);
10473             FreeType(exp.expType);
10474             FreeTypeName(exp.cast.typeName);
10475
10476             *exp = *castExp;
10477             FreeType(exp.expType);
10478             FreeType(exp.destType);
10479
10480             exp.expType = expType;
10481             exp.destType = destType;
10482
10483             delete castExp;
10484
10485             exp.prev = prev;
10486             exp.next = next;
10487
10488          }
10489          else
10490          {
10491             exp.isConstant = exp.cast.exp.isConstant;
10492          }
10493          //FreeType(type);
10494          break;
10495       }
10496       case extensionInitializerExp:
10497       {
10498          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10499          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10500          // ProcessInitializer(exp.initializer.initializer, type);
10501          exp.expType = type;
10502          break;
10503       }
10504       case vaArgExp:
10505       {
10506          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10507          ProcessExpressionType(exp.vaArg.exp);
10508          exp.expType = type;
10509          break;
10510       }
10511       case conditionExp:
10512       {
10513          Expression e;
10514          exp.isConstant = true;
10515
10516          FreeType(exp.cond.cond.destType);
10517          exp.cond.cond.destType = MkClassType("bool");
10518          exp.cond.cond.destType.truth = true;
10519          ProcessExpressionType(exp.cond.cond);
10520          if(!exp.cond.cond.isConstant)
10521             exp.isConstant = false;
10522          for(e = exp.cond.exp->first; e; e = e.next)
10523          {
10524             if(!e.next)
10525             {
10526                FreeType(e.destType);
10527                e.destType = exp.destType;
10528                if(e.destType) e.destType.refCount++;
10529             }
10530             ProcessExpressionType(e);
10531             if(!e.next)
10532             {
10533                exp.expType = e.expType;
10534                if(e.expType) e.expType.refCount++;
10535             }
10536             if(!e.isConstant)
10537                exp.isConstant = false;
10538          }
10539
10540          FreeType(exp.cond.elseExp.destType);
10541          // Added this check if we failed to find an expType
10542          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10543
10544          // Reversed it...
10545          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10546
10547          if(exp.cond.elseExp.destType)
10548             exp.cond.elseExp.destType.refCount++;
10549          ProcessExpressionType(exp.cond.elseExp);
10550
10551          // FIXED THIS: Was done before calling process on elseExp
10552          if(!exp.cond.elseExp.isConstant)
10553             exp.isConstant = false;
10554          break;
10555       }
10556       case extensionCompoundExp:
10557       {
10558          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10559          {
10560             Statement last = exp.compound.compound.statements->last;
10561             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10562             {
10563                ((Expression)last.expressions->last).destType = exp.destType;
10564                if(exp.destType)
10565                   exp.destType.refCount++;
10566             }
10567             ProcessStatement(exp.compound);
10568             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10569             if(exp.expType)
10570                exp.expType.refCount++;
10571          }
10572          break;
10573       }
10574       case classExp:
10575       {
10576          Specifier spec = exp._classExp.specifiers->first;
10577          if(spec && spec.type == nameSpecifier)
10578          {
10579             exp.expType = MkClassType(spec.name);
10580             exp.expType.kind = subClassType;
10581             exp.byReference = true;
10582          }
10583          else
10584          {
10585             exp.expType = MkClassType("ecere::com::Class");
10586             exp.byReference = true;
10587          }
10588          break;
10589       }
10590       case classDataExp:
10591       {
10592          Class _class = thisClass ? thisClass : currentClass;
10593          if(_class)
10594          {
10595             Identifier id = exp.classData.id;
10596             char structName[1024];
10597             Expression classExp;
10598             strcpy(structName, "__ecereClassData_");
10599             FullClassNameCat(structName, _class.fullName, false);
10600             exp.type = pointerExp;
10601             exp.member.member = id;
10602             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10603                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10604             else
10605                classExp = MkExpIdentifier(MkIdentifier("class"));
10606
10607             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10608                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10609                   MkExpBrackets(MkListOne(MkExpOp(
10610                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10611                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10612                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10613                      )));
10614
10615             ProcessExpressionType(exp);
10616             return;
10617          }
10618          break;
10619       }
10620       case arrayExp:
10621       {
10622          Type type = null;
10623          char * typeString = null;
10624          char typeStringBuf[1024];
10625          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10626             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10627          {
10628             Class templateClass = exp.destType._class.registered;
10629             typeString = templateClass.templateArgs[2].dataTypeString;
10630          }
10631          else if(exp.list)
10632          {
10633             // Guess type from expressions in the array
10634             Expression e;
10635             for(e = exp.list->first; e; e = e.next)
10636             {
10637                ProcessExpressionType(e);
10638                if(e.expType)
10639                {
10640                   if(!type) { type = e.expType; type.refCount++; }
10641                   else
10642                   {
10643                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10644                      if(!MatchTypeExpression(e, type, null, false))
10645                      {
10646                         FreeType(type);
10647                         type = e.expType;
10648                         e.expType = null;
10649
10650                         e = exp.list->first;
10651                         ProcessExpressionType(e);
10652                         if(e.expType)
10653                         {
10654                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10655                            if(!MatchTypeExpression(e, type, null, false))
10656                            {
10657                               FreeType(e.expType);
10658                               e.expType = null;
10659                               FreeType(type);
10660                               type = null;
10661                               break;
10662                            }
10663                         }
10664                      }
10665                   }
10666                   if(e.expType)
10667                   {
10668                      FreeType(e.expType);
10669                      e.expType = null;
10670                   }
10671                }
10672             }
10673             if(type)
10674             {
10675                typeStringBuf[0] = '\0';
10676                PrintTypeNoConst(type, typeStringBuf, false, true);
10677                typeString = typeStringBuf;
10678                FreeType(type);
10679                type = null;
10680             }
10681          }
10682          if(typeString)
10683          {
10684             /*
10685             (Container)& (struct BuiltInContainer)
10686             {
10687                ._vTbl = class(BuiltInContainer)._vTbl,
10688                ._class = class(BuiltInContainer),
10689                .refCount = 0,
10690                .data = (int[]){ 1, 7, 3, 4, 5 },
10691                .count = 5,
10692                .type = class(int),
10693             }
10694             */
10695             char templateString[1024];
10696             OldList * initializers = MkList();
10697             OldList * structInitializers = MkList();
10698             OldList * specs = MkList();
10699             Expression expExt;
10700             Declarator decl = SpecDeclFromString(typeString, specs, null);
10701             sprintf(templateString, "Container<%s>", typeString);
10702
10703             if(exp.list)
10704             {
10705                Expression e;
10706                type = ProcessTypeString(typeString, false);
10707                while(e = exp.list->first)
10708                {
10709                   exp.list->Remove(e);
10710                   e.destType = type;
10711                   type.refCount++;
10712                   ProcessExpressionType(e);
10713                   ListAdd(initializers, MkInitializerAssignment(e));
10714                }
10715                FreeType(type);
10716                delete exp.list;
10717             }
10718
10719             DeclareStruct("ecere::com::BuiltInContainer", false);
10720
10721             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10722                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10723             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10724                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10725             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10726                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10727             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10728                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10729                MkInitializerList(initializers))));
10730                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10731             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10732                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10733             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10734                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10735             exp.expType = ProcessTypeString(templateString, false);
10736             exp.type = bracketsExp;
10737             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10738                MkExpOp(null, '&',
10739                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10740                   MkInitializerList(structInitializers)))));
10741             ProcessExpressionType(expExt);
10742          }
10743          else
10744          {
10745             exp.expType = ProcessTypeString("Container", false);
10746             Compiler_Error($"Couldn't determine type of array elements\n");
10747          }
10748          break;
10749       }
10750    }
10751
10752    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10753    {
10754       FreeType(exp.expType);
10755       exp.expType = ReplaceThisClassType(thisClass);
10756    }
10757
10758    // Resolve structures here
10759    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10760    {
10761       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10762       // TODO: Fix members reference...
10763       if(symbol)
10764       {
10765          if(exp.expType.kind != enumType)
10766          {
10767             Type member;
10768             String enumName = CopyString(exp.expType.enumName);
10769
10770             // Fixed a memory leak on self-referencing C structs typedefs
10771             // by instantiating a new type rather than simply copying members
10772             // into exp.expType
10773             FreeType(exp.expType);
10774             exp.expType = Type { };
10775             exp.expType.kind = symbol.type.kind;
10776             exp.expType.refCount++;
10777             exp.expType.enumName = enumName;
10778
10779             exp.expType.members = symbol.type.members;
10780             for(member = symbol.type.members.first; member; member = member.next)
10781                member.refCount++;
10782          }
10783          else
10784          {
10785             NamedLink member;
10786             for(member = symbol.type.members.first; member; member = member.next)
10787             {
10788                NamedLink value { name = CopyString(member.name) };
10789                exp.expType.members.Add(value);
10790             }
10791          }
10792       }
10793    }
10794
10795    yylloc = exp.loc;
10796    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10797    else if(exp.destType && !exp.destType.keepCast)
10798    {
10799       if(!CheckExpressionType(exp, exp.destType, false))
10800       {
10801          if(!exp.destType.count || unresolved)
10802          {
10803             if(!exp.expType)
10804             {
10805                yylloc = exp.loc;
10806                if(exp.destType.kind != ellipsisType)
10807                {
10808                   char type2[1024];
10809                   type2[0] = '\0';
10810                   if(inCompiler)
10811                   {
10812                      char expString[10240];
10813                      expString[0] = '\0';
10814
10815                      PrintType(exp.destType, type2, false, true);
10816
10817                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10818                      if(unresolved)
10819                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10820                      else if(exp.type != dummyExp)
10821                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10822                   }
10823                }
10824                else
10825                {
10826                   char expString[10240] ;
10827                   expString[0] = '\0';
10828                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10829
10830                   if(unresolved)
10831                      Compiler_Error($"unresolved identifier %s\n", expString);
10832                   else if(exp.type != dummyExp)
10833                      Compiler_Error($"couldn't determine type of %s\n", expString);
10834                }
10835             }
10836             else
10837             {
10838                char type1[1024];
10839                char type2[1024];
10840                type1[0] = '\0';
10841                type2[0] = '\0';
10842                if(inCompiler)
10843                {
10844                   PrintType(exp.expType, type1, false, true);
10845                   PrintType(exp.destType, type2, false, true);
10846                }
10847
10848                //CheckExpressionType(exp, exp.destType, false);
10849
10850                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10851                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10852                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10853                else
10854                {
10855                   char expString[10240];
10856                   expString[0] = '\0';
10857                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10858
10859 #ifdef _DEBUG
10860                   CheckExpressionType(exp, exp.destType, false);
10861 #endif
10862                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10863                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10864                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10865
10866                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10867                   FreeType(exp.expType);
10868                   exp.destType.refCount++;
10869                   exp.expType = exp.destType;
10870                }
10871             }
10872          }
10873       }
10874       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10875       {
10876          Expression newExp { };
10877          char typeString[1024];
10878          OldList * specs = MkList();
10879          Declarator decl;
10880
10881          typeString[0] = '\0';
10882
10883          *newExp = *exp;
10884
10885          if(exp.expType)  exp.expType.refCount++;
10886          if(exp.expType)  exp.expType.refCount++;
10887          exp.type = castExp;
10888          newExp.destType = exp.expType;
10889
10890          PrintType(exp.expType, typeString, false, false);
10891          decl = SpecDeclFromString(typeString, specs, null);
10892
10893          exp.cast.typeName = MkTypeName(specs, decl);
10894          exp.cast.exp = newExp;
10895       }
10896    }
10897    else if(unresolved)
10898    {
10899       if(exp.identifier._class && exp.identifier._class.name)
10900          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10901       else if(exp.identifier.string && exp.identifier.string[0])
10902          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10903    }
10904    else if(!exp.expType && exp.type != dummyExp)
10905    {
10906       char expString[10240];
10907       expString[0] = '\0';
10908       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10909       Compiler_Error($"couldn't determine type of %s\n", expString);
10910    }
10911
10912    // Let's try to support any_object & typed_object here:
10913    if(inCompiler)
10914       ApplyAnyObjectLogic(exp);
10915
10916    // Mark nohead classes as by reference, unless we're casting them to an integral type
10917    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10918       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10919          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10920           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10921    {
10922       exp.byReference = true;
10923    }
10924    yylloc = oldyylloc;
10925 }
10926
10927 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10928 {
10929    // THIS CODE WILL FIND NEXT MEMBER...
10930    if(*curMember)
10931    {
10932       *curMember = (*curMember).next;
10933
10934       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10935       {
10936          *curMember = subMemberStack[--(*subMemberStackPos)];
10937          *curMember = (*curMember).next;
10938       }
10939
10940       // SKIP ALL PROPERTIES HERE...
10941       while((*curMember) && (*curMember).isProperty)
10942          *curMember = (*curMember).next;
10943
10944       if(subMemberStackPos)
10945       {
10946          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10947          {
10948             subMemberStack[(*subMemberStackPos)++] = *curMember;
10949
10950             *curMember = (*curMember).members.first;
10951             while(*curMember && (*curMember).isProperty)
10952                *curMember = (*curMember).next;
10953          }
10954       }
10955    }
10956    while(!*curMember)
10957    {
10958       if(!*curMember)
10959       {
10960          if(subMemberStackPos && *subMemberStackPos)
10961          {
10962             *curMember = subMemberStack[--(*subMemberStackPos)];
10963             *curMember = (*curMember).next;
10964          }
10965          else
10966          {
10967             Class lastCurClass = *curClass;
10968
10969             if(*curClass == _class) break;     // REACHED THE END
10970
10971             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10972             *curMember = (*curClass).membersAndProperties.first;
10973          }
10974
10975          while((*curMember) && (*curMember).isProperty)
10976             *curMember = (*curMember).next;
10977          if(subMemberStackPos)
10978          {
10979             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10980             {
10981                subMemberStack[(*subMemberStackPos)++] = *curMember;
10982
10983                *curMember = (*curMember).members.first;
10984                while(*curMember && (*curMember).isProperty)
10985                   *curMember = (*curMember).next;
10986             }
10987          }
10988       }
10989    }
10990 }
10991
10992
10993 static void ProcessInitializer(Initializer init, Type type)
10994 {
10995    switch(init.type)
10996    {
10997       case expInitializer:
10998          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10999          {
11000             // TESTING THIS FOR SHUTTING = 0 WARNING
11001             if(init.exp && !init.exp.destType)
11002             {
11003                FreeType(init.exp.destType);
11004                init.exp.destType = type;
11005                if(type) type.refCount++;
11006             }
11007             if(init.exp)
11008             {
11009                ProcessExpressionType(init.exp);
11010                init.isConstant = init.exp.isConstant;
11011             }
11012             break;
11013          }
11014          else
11015          {
11016             Expression exp = init.exp;
11017             Instantiation inst = exp.instance;
11018             MembersInit members;
11019
11020             init.type = listInitializer;
11021             init.list = MkList();
11022
11023             if(inst.members)
11024             {
11025                for(members = inst.members->first; members; members = members.next)
11026                {
11027                   if(members.type == dataMembersInit)
11028                   {
11029                      MemberInit member;
11030                      for(member = members.dataMembers->first; member; member = member.next)
11031                      {
11032                         ListAdd(init.list, member.initializer);
11033                         member.initializer = null;
11034                      }
11035                   }
11036                   // Discard all MembersInitMethod
11037                }
11038             }
11039             FreeExpression(exp);
11040          }
11041       case listInitializer:
11042       {
11043          Initializer i;
11044          Type initializerType = null;
11045          Class curClass = null;
11046          DataMember curMember = null;
11047          DataMember subMemberStack[256];
11048          int subMemberStackPos = 0;
11049
11050          if(type && type.kind == arrayType)
11051             initializerType = Dereference(type);
11052          else if(type && (type.kind == structType || type.kind == unionType))
11053             initializerType = type.members.first;
11054
11055          for(i = init.list->first; i; i = i.next)
11056          {
11057             if(type && type.kind == classType && type._class && type._class.registered)
11058             {
11059                // 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)
11060                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11061                // TODO: Generate error on initializing a private data member this way from another module...
11062                if(curMember)
11063                {
11064                   if(!curMember.dataType)
11065                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11066                   initializerType = curMember.dataType;
11067                }
11068             }
11069             ProcessInitializer(i, initializerType);
11070             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11071                initializerType = initializerType.next;
11072             if(!i.isConstant)
11073                init.isConstant = false;
11074          }
11075
11076          if(type && type.kind == arrayType)
11077             FreeType(initializerType);
11078
11079          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11080          {
11081             Compiler_Error($"Assigning list initializer to non list\n");
11082          }
11083          break;
11084       }
11085    }
11086 }
11087
11088 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11089 {
11090    switch(spec.type)
11091    {
11092       case baseSpecifier:
11093       {
11094          if(spec.specifier == THISCLASS)
11095          {
11096             if(thisClass)
11097             {
11098                spec.type = nameSpecifier;
11099                spec.name = ReplaceThisClass(thisClass);
11100                spec.symbol = FindClass(spec.name);
11101                ProcessSpecifier(spec, declareStruct);
11102             }
11103          }
11104          break;
11105       }
11106       case nameSpecifier:
11107       {
11108          Symbol symbol = FindType(curContext, spec.name);
11109          if(symbol)
11110             DeclareType(symbol.type, true, true);
11111          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11112             DeclareStruct(spec.name, false);
11113          break;
11114       }
11115       case enumSpecifier:
11116       {
11117          Enumerator e;
11118          if(spec.list)
11119          {
11120             for(e = spec.list->first; e; e = e.next)
11121             {
11122                if(e.exp)
11123                   ProcessExpressionType(e.exp);
11124             }
11125          }
11126          break;
11127       }
11128       case structSpecifier:
11129       case unionSpecifier:
11130       {
11131          if(spec.definitions)
11132          {
11133             ClassDef def;
11134             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11135             //if(symbol)
11136                ProcessClass(spec.definitions, symbol);
11137             /*else
11138             {
11139                for(def = spec.definitions->first; def; def = def.next)
11140                {
11141                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11142                      ProcessDeclaration(def.decl);
11143                }
11144             }*/
11145          }
11146          break;
11147       }
11148       /*
11149       case classSpecifier:
11150       {
11151          Symbol classSym = FindClass(spec.name);
11152          if(classSym && classSym.registered && classSym.registered.type == structClass)
11153             DeclareStruct(spec.name, false);
11154          break;
11155       }
11156       */
11157    }
11158 }
11159
11160
11161 static void ProcessDeclarator(Declarator decl)
11162 {
11163    switch(decl.type)
11164    {
11165       case identifierDeclarator:
11166          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11167          {
11168             FreeSpecifier(decl.identifier._class);
11169             decl.identifier._class = null;
11170          }
11171          break;
11172       case arrayDeclarator:
11173          if(decl.array.exp)
11174             ProcessExpressionType(decl.array.exp);
11175       case structDeclarator:
11176       case bracketsDeclarator:
11177       case functionDeclarator:
11178       case pointerDeclarator:
11179       case extendedDeclarator:
11180       case extendedDeclaratorEnd:
11181          if(decl.declarator)
11182             ProcessDeclarator(decl.declarator);
11183          if(decl.type == functionDeclarator)
11184          {
11185             Identifier id = GetDeclId(decl);
11186             if(id && id._class)
11187             {
11188                TypeName param
11189                {
11190                   qualifiers = MkListOne(id._class);
11191                   declarator = null;
11192                };
11193                if(!decl.function.parameters)
11194                   decl.function.parameters = MkList();
11195                decl.function.parameters->Insert(null, param);
11196                id._class = null;
11197             }
11198             if(decl.function.parameters)
11199             {
11200                TypeName param;
11201
11202                for(param = decl.function.parameters->first; param; param = param.next)
11203                {
11204                   if(param.qualifiers && param.qualifiers->first)
11205                   {
11206                      Specifier spec = param.qualifiers->first;
11207                      if(spec && spec.specifier == TYPED_OBJECT)
11208                      {
11209                         Declarator d = param.declarator;
11210                         TypeName newParam
11211                         {
11212                            qualifiers = MkListOne(MkSpecifier(VOID));
11213                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11214                         };
11215
11216                         FreeList(param.qualifiers, FreeSpecifier);
11217
11218                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11219                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11220
11221                         decl.function.parameters->Insert(param, newParam);
11222                         param = newParam;
11223                      }
11224                      else if(spec && spec.specifier == ANY_OBJECT)
11225                      {
11226                         Declarator d = param.declarator;
11227
11228                         FreeList(param.qualifiers, FreeSpecifier);
11229
11230                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11231                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11232                      }
11233                      else if(spec.specifier == THISCLASS)
11234                      {
11235                         if(thisClass)
11236                         {
11237                            spec.type = nameSpecifier;
11238                            spec.name = ReplaceThisClass(thisClass);
11239                            spec.symbol = FindClass(spec.name);
11240                            ProcessSpecifier(spec, false);
11241                         }
11242                      }
11243                   }
11244
11245                   if(param.declarator)
11246                      ProcessDeclarator(param.declarator);
11247                }
11248             }
11249          }
11250          break;
11251    }
11252 }
11253
11254 static void ProcessDeclaration(Declaration decl)
11255 {
11256    yylloc = decl.loc;
11257    switch(decl.type)
11258    {
11259       case initDeclaration:
11260       {
11261          bool declareStruct = false;
11262          /*
11263          lineNum = decl.pos.line;
11264          column = decl.pos.col;
11265          */
11266
11267          if(decl.declarators)
11268          {
11269             InitDeclarator d;
11270
11271             for(d = decl.declarators->first; d; d = d.next)
11272             {
11273                Type type, subType;
11274                ProcessDeclarator(d.declarator);
11275
11276                type = ProcessType(decl.specifiers, d.declarator);
11277
11278                if(d.initializer)
11279                {
11280                   ProcessInitializer(d.initializer, type);
11281
11282                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11283
11284                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11285                      d.initializer.exp.type == instanceExp)
11286                   {
11287                      if(type.kind == classType && type._class ==
11288                         d.initializer.exp.expType._class)
11289                      {
11290                         Instantiation inst = d.initializer.exp.instance;
11291                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11292
11293                         d.initializer.exp.instance = null;
11294                         if(decl.specifiers)
11295                            FreeList(decl.specifiers, FreeSpecifier);
11296                         FreeList(decl.declarators, FreeInitDeclarator);
11297
11298                         d = null;
11299
11300                         decl.type = instDeclaration;
11301                         decl.inst = inst;
11302                      }
11303                   }
11304                }
11305                for(subType = type; subType;)
11306                {
11307                   if(subType.kind == classType)
11308                   {
11309                      declareStruct = true;
11310                      break;
11311                   }
11312                   else if(subType.kind == pointerType)
11313                      break;
11314                   else if(subType.kind == arrayType)
11315                      subType = subType.arrayType;
11316                   else
11317                      break;
11318                }
11319
11320                FreeType(type);
11321                if(!d) break;
11322             }
11323          }
11324
11325          if(decl.specifiers)
11326          {
11327             Specifier s;
11328             for(s = decl.specifiers->first; s; s = s.next)
11329             {
11330                ProcessSpecifier(s, declareStruct);
11331             }
11332          }
11333          break;
11334       }
11335       case instDeclaration:
11336       {
11337          ProcessInstantiationType(decl.inst);
11338          break;
11339       }
11340       case structDeclaration:
11341       {
11342          Specifier spec;
11343          Declarator d;
11344          bool declareStruct = false;
11345
11346          if(decl.declarators)
11347          {
11348             for(d = decl.declarators->first; d; d = d.next)
11349             {
11350                Type type = ProcessType(decl.specifiers, d.declarator);
11351                Type subType;
11352                ProcessDeclarator(d);
11353                for(subType = type; subType;)
11354                {
11355                   if(subType.kind == classType)
11356                   {
11357                      declareStruct = true;
11358                      break;
11359                   }
11360                   else if(subType.kind == pointerType)
11361                      break;
11362                   else if(subType.kind == arrayType)
11363                      subType = subType.arrayType;
11364                   else
11365                      break;
11366                }
11367                FreeType(type);
11368             }
11369          }
11370          if(decl.specifiers)
11371          {
11372             for(spec = decl.specifiers->first; spec; spec = spec.next)
11373                ProcessSpecifier(spec, declareStruct);
11374          }
11375          break;
11376       }
11377    }
11378 }
11379
11380 static FunctionDefinition curFunction;
11381
11382 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11383 {
11384    char propName[1024], propNameM[1024];
11385    char getName[1024], setName[1024];
11386    OldList * args;
11387
11388    DeclareProperty(prop, setName, getName);
11389
11390    // eInstance_FireWatchers(object, prop);
11391    strcpy(propName, "__ecereProp_");
11392    FullClassNameCat(propName, prop._class.fullName, false);
11393    strcat(propName, "_");
11394    // strcat(propName, prop.name);
11395    FullClassNameCat(propName, prop.name, true);
11396    MangleClassName(propName);
11397
11398    strcpy(propNameM, "__ecerePropM_");
11399    FullClassNameCat(propNameM, prop._class.fullName, false);
11400    strcat(propNameM, "_");
11401    // strcat(propNameM, prop.name);
11402    FullClassNameCat(propNameM, prop.name, true);
11403    MangleClassName(propNameM);
11404
11405    if(prop.isWatchable)
11406    {
11407       args = MkList();
11408       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11409       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11410       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11411
11412       args = MkList();
11413       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11414       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11415       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11416    }
11417
11418
11419    {
11420       args = MkList();
11421       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11422       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11423       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11424
11425       args = MkList();
11426       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11427       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11428       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11429    }
11430
11431    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11432       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11433       curFunction.propSet.fireWatchersDone = true;
11434 }
11435
11436 static void ProcessStatement(Statement stmt)
11437 {
11438    yylloc = stmt.loc;
11439    /*
11440    lineNum = stmt.pos.line;
11441    column = stmt.pos.col;
11442    */
11443    switch(stmt.type)
11444    {
11445       case labeledStmt:
11446          ProcessStatement(stmt.labeled.stmt);
11447          break;
11448       case caseStmt:
11449          // This expression should be constant...
11450          if(stmt.caseStmt.exp)
11451          {
11452             FreeType(stmt.caseStmt.exp.destType);
11453             stmt.caseStmt.exp.destType = curSwitchType;
11454             if(curSwitchType) curSwitchType.refCount++;
11455             ProcessExpressionType(stmt.caseStmt.exp);
11456             ComputeExpression(stmt.caseStmt.exp);
11457          }
11458          if(stmt.caseStmt.stmt)
11459             ProcessStatement(stmt.caseStmt.stmt);
11460          break;
11461       case compoundStmt:
11462       {
11463          if(stmt.compound.context)
11464          {
11465             Declaration decl;
11466             Statement s;
11467
11468             Statement prevCompound = curCompound;
11469             Context prevContext = curContext;
11470
11471             if(!stmt.compound.isSwitch)
11472                curCompound = stmt;
11473             curContext = stmt.compound.context;
11474
11475             if(stmt.compound.declarations)
11476             {
11477                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11478                   ProcessDeclaration(decl);
11479             }
11480             if(stmt.compound.statements)
11481             {
11482                for(s = stmt.compound.statements->first; s; s = s.next)
11483                   ProcessStatement(s);
11484             }
11485
11486             curContext = prevContext;
11487             curCompound = prevCompound;
11488          }
11489          break;
11490       }
11491       case expressionStmt:
11492       {
11493          Expression exp;
11494          if(stmt.expressions)
11495          {
11496             for(exp = stmt.expressions->first; exp; exp = exp.next)
11497                ProcessExpressionType(exp);
11498          }
11499          break;
11500       }
11501       case ifStmt:
11502       {
11503          Expression exp;
11504
11505          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11506          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11507          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11508          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11509          {
11510             ProcessExpressionType(exp);
11511          }
11512          if(stmt.ifStmt.stmt)
11513             ProcessStatement(stmt.ifStmt.stmt);
11514          if(stmt.ifStmt.elseStmt)
11515             ProcessStatement(stmt.ifStmt.elseStmt);
11516          break;
11517       }
11518       case switchStmt:
11519       {
11520          Type oldSwitchType = curSwitchType;
11521          if(stmt.switchStmt.exp)
11522          {
11523             Expression exp;
11524             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11525             {
11526                if(!exp.next)
11527                {
11528                   /*
11529                   Type destType
11530                   {
11531                      kind = intType;
11532                      refCount = 1;
11533                   };
11534                   e.exp.destType = destType;
11535                   */
11536
11537                   ProcessExpressionType(exp);
11538                }
11539                if(!exp.next)
11540                   curSwitchType = exp.expType;
11541             }
11542          }
11543          ProcessStatement(stmt.switchStmt.stmt);
11544          curSwitchType = oldSwitchType;
11545          break;
11546       }
11547       case whileStmt:
11548       {
11549          if(stmt.whileStmt.exp)
11550          {
11551             Expression exp;
11552
11553             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11554             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11555             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11556             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11557             {
11558                ProcessExpressionType(exp);
11559             }
11560          }
11561          if(stmt.whileStmt.stmt)
11562             ProcessStatement(stmt.whileStmt.stmt);
11563          break;
11564       }
11565       case doWhileStmt:
11566       {
11567          if(stmt.doWhile.exp)
11568          {
11569             Expression exp;
11570
11571             if(stmt.doWhile.exp->last)
11572             {
11573                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11574                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11575                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11576             }
11577             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11578             {
11579                ProcessExpressionType(exp);
11580             }
11581          }
11582          if(stmt.doWhile.stmt)
11583             ProcessStatement(stmt.doWhile.stmt);
11584          break;
11585       }
11586       case forStmt:
11587       {
11588          Expression exp;
11589          if(stmt.forStmt.init)
11590             ProcessStatement(stmt.forStmt.init);
11591
11592          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11593          {
11594             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11595             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11596             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11597          }
11598
11599          if(stmt.forStmt.check)
11600             ProcessStatement(stmt.forStmt.check);
11601          if(stmt.forStmt.increment)
11602          {
11603             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11604                ProcessExpressionType(exp);
11605          }
11606
11607          if(stmt.forStmt.stmt)
11608             ProcessStatement(stmt.forStmt.stmt);
11609          break;
11610       }
11611       case forEachStmt:
11612       {
11613          Identifier id = stmt.forEachStmt.id;
11614          OldList * exp = stmt.forEachStmt.exp;
11615          OldList * filter = stmt.forEachStmt.filter;
11616          Statement block = stmt.forEachStmt.stmt;
11617          char iteratorType[1024];
11618          Type source;
11619          Expression e;
11620          bool isBuiltin = exp && exp->last &&
11621             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11622               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11623          Expression arrayExp;
11624          char * typeString = null;
11625          int builtinCount = 0;
11626
11627          for(e = exp ? exp->first : null; e; e = e.next)
11628          {
11629             if(!e.next)
11630             {
11631                FreeType(e.destType);
11632                e.destType = ProcessTypeString("Container", false);
11633             }
11634             if(!isBuiltin || e.next)
11635                ProcessExpressionType(e);
11636          }
11637
11638          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11639          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11640             eClass_IsDerived(source._class.registered, containerClass)))
11641          {
11642             Class _class = source ? source._class.registered : null;
11643             Symbol symbol;
11644             Expression expIt = null;
11645             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
11646             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11647             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11648             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11649             stmt.type = compoundStmt;
11650
11651             stmt.compound.context = Context { };
11652             stmt.compound.context.parent = curContext;
11653             curContext = stmt.compound.context;
11654
11655             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11656             {
11657                Class mapClass = eSystem_FindClass(privateModule, "Map");
11658                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11659                isCustomAVLTree = true;
11660                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
11661                   isAVLTree = true;
11662                else */if(eClass_IsDerived(source._class.registered, mapClass))
11663                   isMap = true;
11664             }
11665             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11666             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11667             {
11668                Class listClass = eSystem_FindClass(privateModule, "List");
11669                isLinkList = true;
11670                isList = eClass_IsDerived(source._class.registered, listClass);
11671             }
11672
11673             if(isArray)
11674             {
11675                Declarator decl;
11676                OldList * specs = MkList();
11677                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11678                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11679                stmt.compound.declarations = MkListOne(
11680                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11681                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11682                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11683                      MkInitializerAssignment(MkExpBrackets(exp))))));
11684             }
11685             else if(isBuiltin)
11686             {
11687                Type type = null;
11688                char typeStringBuf[1024];
11689
11690                // TODO: Merge this code?
11691                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11692                if(((Expression)exp->last).type == castExp)
11693                {
11694                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11695                   if(typeName)
11696                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11697                }
11698
11699                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11700                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11701                   arrayExp.destType._class.registered.templateArgs)
11702                {
11703                   Class templateClass = arrayExp.destType._class.registered;
11704                   typeString = templateClass.templateArgs[2].dataTypeString;
11705                }
11706                else if(arrayExp.list)
11707                {
11708                   // Guess type from expressions in the array
11709                   Expression e;
11710                   for(e = arrayExp.list->first; e; e = e.next)
11711                   {
11712                      ProcessExpressionType(e);
11713                      if(e.expType)
11714                      {
11715                         if(!type) { type = e.expType; type.refCount++; }
11716                         else
11717                         {
11718                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11719                            if(!MatchTypeExpression(e, type, null, false))
11720                            {
11721                               FreeType(type);
11722                               type = e.expType;
11723                               e.expType = null;
11724
11725                               e = arrayExp.list->first;
11726                               ProcessExpressionType(e);
11727                               if(e.expType)
11728                               {
11729                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11730                                  if(!MatchTypeExpression(e, type, null, false))
11731                                  {
11732                                     FreeType(e.expType);
11733                                     e.expType = null;
11734                                     FreeType(type);
11735                                     type = null;
11736                                     break;
11737                                  }
11738                               }
11739                            }
11740                         }
11741                         if(e.expType)
11742                         {
11743                            FreeType(e.expType);
11744                            e.expType = null;
11745                         }
11746                      }
11747                   }
11748                   if(type)
11749                   {
11750                      typeStringBuf[0] = '\0';
11751                      PrintType(type, typeStringBuf, false, true);
11752                      typeString = typeStringBuf;
11753                      FreeType(type);
11754                   }
11755                }
11756                if(typeString)
11757                {
11758                   OldList * initializers = MkList();
11759                   Declarator decl;
11760                   OldList * specs = MkList();
11761                   if(arrayExp.list)
11762                   {
11763                      Expression e;
11764
11765                      builtinCount = arrayExp.list->count;
11766                      type = ProcessTypeString(typeString, false);
11767                      while(e = arrayExp.list->first)
11768                      {
11769                         arrayExp.list->Remove(e);
11770                         e.destType = type;
11771                         type.refCount++;
11772                         ProcessExpressionType(e);
11773                         ListAdd(initializers, MkInitializerAssignment(e));
11774                      }
11775                      FreeType(type);
11776                      delete arrayExp.list;
11777                   }
11778                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11779                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11780                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11781
11782                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11783                      PlugDeclarator(
11784                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11785                         ), MkInitializerList(initializers)))));
11786                   FreeList(exp, FreeExpression);
11787                }
11788                else
11789                {
11790                   arrayExp.expType = ProcessTypeString("Container", false);
11791                   Compiler_Error($"Couldn't determine type of array elements\n");
11792                }
11793
11794                /*
11795                Declarator decl;
11796                OldList * specs = MkList();
11797
11798                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11799                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11800                stmt.compound.declarations = MkListOne(
11801                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11802                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11803                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11804                      MkInitializerAssignment(MkExpBrackets(exp))))));
11805                */
11806             }
11807             else if(isLinkList && !isList)
11808             {
11809                Declarator decl;
11810                OldList * specs = MkList();
11811                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11812                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11813                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11814                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11815                      MkInitializerAssignment(MkExpBrackets(exp))))));
11816             }
11817             /*else if(isCustomAVLTree)
11818             {
11819                Declarator decl;
11820                OldList * specs = MkList();
11821                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11822                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11823                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11824                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11825                      MkInitializerAssignment(MkExpBrackets(exp))))));
11826             }*/
11827             else if(_class.templateArgs)
11828             {
11829                if(isMap)
11830                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11831                else
11832                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11833
11834                stmt.compound.declarations = MkListOne(
11835                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11836                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11837                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11838             }
11839             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11840
11841             if(block)
11842             {
11843                // Reparent sub-contexts in this statement
11844                switch(block.type)
11845                {
11846                   case compoundStmt:
11847                      if(block.compound.context)
11848                         block.compound.context.parent = stmt.compound.context;
11849                      break;
11850                   case ifStmt:
11851                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11852                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11853                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11854                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11855                      break;
11856                   case switchStmt:
11857                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11858                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11859                      break;
11860                   case whileStmt:
11861                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11862                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11863                      break;
11864                   case doWhileStmt:
11865                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11866                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11867                      break;
11868                   case forStmt:
11869                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11870                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11871                      break;
11872                   case forEachStmt:
11873                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11874                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11875                      break;
11876                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11877                   case labeledStmt:
11878                   case caseStmt
11879                   case expressionStmt:
11880                   case gotoStmt:
11881                   case continueStmt:
11882                   case breakStmt
11883                   case returnStmt:
11884                   case asmStmt:
11885                   case badDeclarationStmt:
11886                   case fireWatchersStmt:
11887                   case stopWatchingStmt:
11888                   case watchStmt:
11889                   */
11890                }
11891             }
11892             if(filter)
11893             {
11894                block = MkIfStmt(filter, block, null);
11895             }
11896             if(isArray)
11897             {
11898                stmt.compound.statements = MkListOne(MkForStmt(
11899                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11900                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11901                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11902                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11903                   block));
11904               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11905               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11906               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11907             }
11908             else if(isBuiltin)
11909             {
11910                char count[128];
11911                //OldList * specs = MkList();
11912                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11913
11914                sprintf(count, "%d", builtinCount);
11915
11916                stmt.compound.statements = MkListOne(MkForStmt(
11917                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11918                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11919                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11920                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11921                   block));
11922
11923                /*
11924                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11925                stmt.compound.statements = MkListOne(MkForStmt(
11926                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11927                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11928                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11929                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11930                   block));
11931               */
11932               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11933               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11934               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11935             }
11936             else if(isLinkList && !isList)
11937             {
11938                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11939                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11940                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11941                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11942                {
11943                   stmt.compound.statements = MkListOne(MkForStmt(
11944                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11945                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11946                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11947                      block));
11948                }
11949                else
11950                {
11951                   OldList * specs = MkList();
11952                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11953                   stmt.compound.statements = MkListOne(MkForStmt(
11954                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11955                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11956                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11957                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11958                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11959                      block));
11960                }
11961                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11962                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11963                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11964             }
11965             /*else if(isCustomAVLTree)
11966             {
11967                stmt.compound.statements = MkListOne(MkForStmt(
11968                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11969                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11970                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11971                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11972                   block));
11973
11974                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11975                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11976                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11977             }*/
11978             else
11979             {
11980                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11981                   MkIdentifier("Next")), null)), block));
11982             }
11983             ProcessExpressionType(expIt);
11984             if(stmt.compound.declarations->first)
11985                ProcessDeclaration(stmt.compound.declarations->first);
11986
11987             if(symbol)
11988                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11989
11990             ProcessStatement(stmt);
11991             curContext = stmt.compound.context.parent;
11992             break;
11993          }
11994          else
11995          {
11996             Compiler_Error($"Expression is not a container\n");
11997          }
11998          break;
11999       }
12000       case gotoStmt:
12001          break;
12002       case continueStmt:
12003          break;
12004       case breakStmt:
12005          break;
12006       case returnStmt:
12007       {
12008          Expression exp;
12009          if(stmt.expressions)
12010          {
12011             for(exp = stmt.expressions->first; exp; exp = exp.next)
12012             {
12013                if(!exp.next)
12014                {
12015                   if(curFunction && !curFunction.type)
12016                      curFunction.type = ProcessType(
12017                         curFunction.specifiers, curFunction.declarator);
12018                   FreeType(exp.destType);
12019                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12020                   if(exp.destType) exp.destType.refCount++;
12021                }
12022                ProcessExpressionType(exp);
12023             }
12024          }
12025          break;
12026       }
12027       case badDeclarationStmt:
12028       {
12029          ProcessDeclaration(stmt.decl);
12030          break;
12031       }
12032       case asmStmt:
12033       {
12034          AsmField field;
12035          if(stmt.asmStmt.inputFields)
12036          {
12037             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12038                if(field.expression)
12039                   ProcessExpressionType(field.expression);
12040          }
12041          if(stmt.asmStmt.outputFields)
12042          {
12043             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12044                if(field.expression)
12045                   ProcessExpressionType(field.expression);
12046          }
12047          if(stmt.asmStmt.clobberedFields)
12048          {
12049             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12050             {
12051                if(field.expression)
12052                   ProcessExpressionType(field.expression);
12053             }
12054          }
12055          break;
12056       }
12057       case watchStmt:
12058       {
12059          PropertyWatch propWatch;
12060          OldList * watches = stmt._watch.watches;
12061          Expression object = stmt._watch.object;
12062          Expression watcher = stmt._watch.watcher;
12063          if(watcher)
12064             ProcessExpressionType(watcher);
12065          if(object)
12066             ProcessExpressionType(object);
12067
12068          if(inCompiler)
12069          {
12070             if(watcher || thisClass)
12071             {
12072                External external = curExternal;
12073                Context context = curContext;
12074
12075                stmt.type = expressionStmt;
12076                stmt.expressions = MkList();
12077
12078                curExternal = external.prev;
12079
12080                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12081                {
12082                   ClassFunction func;
12083                   char watcherName[1024];
12084                   Class watcherClass = watcher ?
12085                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12086                   External createdExternal;
12087
12088                   // Create a declaration above
12089                   External externalDecl = MkExternalDeclaration(null);
12090                   ast->Insert(curExternal.prev, externalDecl);
12091
12092                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12093                   if(propWatch.deleteWatch)
12094                      strcat(watcherName, "_delete");
12095                   else
12096                   {
12097                      Identifier propID;
12098                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12099                      {
12100                         strcat(watcherName, "_");
12101                         strcat(watcherName, propID.string);
12102                      }
12103                   }
12104
12105                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12106                   {
12107                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12108                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12109                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12110                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12111                      ProcessClassFunctionBody(func, propWatch.compound);
12112                      propWatch.compound = null;
12113
12114                      //afterExternal = afterExternal ? afterExternal : curExternal;
12115
12116                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12117                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12118                      // TESTING THIS...
12119                      createdExternal.symbol.idCode = external.symbol.idCode;
12120
12121                      curExternal = createdExternal;
12122                      ProcessFunction(createdExternal.function);
12123
12124
12125                      // Create a declaration above
12126                      {
12127                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12128                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12129                         externalDecl.declaration = decl;
12130                         if(decl.symbol && !decl.symbol.pointerExternal)
12131                            decl.symbol.pointerExternal = externalDecl;
12132                      }
12133
12134                      if(propWatch.deleteWatch)
12135                      {
12136                         OldList * args = MkList();
12137                         ListAdd(args, CopyExpression(object));
12138                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12139                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12140                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12141                      }
12142                      else
12143                      {
12144                         Class _class = object.expType._class.registered;
12145                         Identifier propID;
12146
12147                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12148                         {
12149                            char propName[1024];
12150                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12151                            if(prop)
12152                            {
12153                               char getName[1024], setName[1024];
12154                               OldList * args = MkList();
12155
12156                               DeclareProperty(prop, setName, getName);
12157
12158                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12159                               strcpy(propName, "__ecereProp_");
12160                               FullClassNameCat(propName, prop._class.fullName, false);
12161                               strcat(propName, "_");
12162                               // strcat(propName, prop.name);
12163                               FullClassNameCat(propName, prop.name, true);
12164
12165                               ListAdd(args, CopyExpression(object));
12166                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12167                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12168                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12169
12170                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12171                            }
12172                            else
12173                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12174                         }
12175                      }
12176                   }
12177                   else
12178                      Compiler_Error($"Invalid watched object\n");
12179                }
12180
12181                curExternal = external;
12182                curContext = context;
12183
12184                if(watcher)
12185                   FreeExpression(watcher);
12186                if(object)
12187                   FreeExpression(object);
12188                FreeList(watches, FreePropertyWatch);
12189             }
12190             else
12191                Compiler_Error($"No observer specified and not inside a _class\n");
12192          }
12193          else
12194          {
12195             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12196             {
12197                ProcessStatement(propWatch.compound);
12198             }
12199
12200          }
12201          break;
12202       }
12203       case fireWatchersStmt:
12204       {
12205          OldList * watches = stmt._watch.watches;
12206          Expression object = stmt._watch.object;
12207          Class _class;
12208          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12209          // printf("%X\n", watches);
12210          // printf("%X\n", stmt._watch.watches);
12211          if(object)
12212             ProcessExpressionType(object);
12213
12214          if(inCompiler)
12215          {
12216             _class = object ?
12217                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12218
12219             if(_class)
12220             {
12221                Identifier propID;
12222
12223                stmt.type = expressionStmt;
12224                stmt.expressions = MkList();
12225
12226                // Check if we're inside a property set
12227                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12228                {
12229                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12230                }
12231                else if(!watches)
12232                {
12233                   //Compiler_Error($"No property specified and not inside a property set\n");
12234                }
12235                if(watches)
12236                {
12237                   for(propID = watches->first; propID; propID = propID.next)
12238                   {
12239                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12240                      if(prop)
12241                      {
12242                         CreateFireWatcher(prop, object, stmt);
12243                      }
12244                      else
12245                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12246                   }
12247                }
12248                else
12249                {
12250                   // Fire all properties!
12251                   Property prop;
12252                   Class base;
12253                   for(base = _class; base; base = base.base)
12254                   {
12255                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12256                      {
12257                         if(prop.isProperty && prop.isWatchable)
12258                         {
12259                            CreateFireWatcher(prop, object, stmt);
12260                         }
12261                      }
12262                   }
12263                }
12264
12265                if(object)
12266                   FreeExpression(object);
12267                FreeList(watches, FreeIdentifier);
12268             }
12269             else
12270                Compiler_Error($"Invalid object specified and not inside a class\n");
12271          }
12272          break;
12273       }
12274       case stopWatchingStmt:
12275       {
12276          OldList * watches = stmt._watch.watches;
12277          Expression object = stmt._watch.object;
12278          Expression watcher = stmt._watch.watcher;
12279          Class _class;
12280          if(object)
12281             ProcessExpressionType(object);
12282          if(watcher)
12283             ProcessExpressionType(watcher);
12284          if(inCompiler)
12285          {
12286             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12287
12288             if(watcher || thisClass)
12289             {
12290                if(_class)
12291                {
12292                   Identifier propID;
12293
12294                   stmt.type = expressionStmt;
12295                   stmt.expressions = MkList();
12296
12297                   if(!watches)
12298                   {
12299                      OldList * args;
12300                      // eInstance_StopWatching(object, null, watcher);
12301                      args = MkList();
12302                      ListAdd(args, CopyExpression(object));
12303                      ListAdd(args, MkExpConstant("0"));
12304                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12305                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12306                   }
12307                   else
12308                   {
12309                      for(propID = watches->first; propID; propID = propID.next)
12310                      {
12311                         char propName[1024];
12312                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12313                         if(prop)
12314                         {
12315                            char getName[1024], setName[1024];
12316                            OldList * args = MkList();
12317
12318                            DeclareProperty(prop, setName, getName);
12319
12320                            // eInstance_StopWatching(object, prop, watcher);
12321                            strcpy(propName, "__ecereProp_");
12322                            FullClassNameCat(propName, prop._class.fullName, false);
12323                            strcat(propName, "_");
12324                            // strcat(propName, prop.name);
12325                            FullClassNameCat(propName, prop.name, true);
12326                            MangleClassName(propName);
12327
12328                            ListAdd(args, CopyExpression(object));
12329                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12330                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12331                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12332                         }
12333                         else
12334                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12335                      }
12336                   }
12337
12338                   if(object)
12339                      FreeExpression(object);
12340                   if(watcher)
12341                      FreeExpression(watcher);
12342                   FreeList(watches, FreeIdentifier);
12343                }
12344                else
12345                   Compiler_Error($"Invalid object specified and not inside a class\n");
12346             }
12347             else
12348                Compiler_Error($"No observer specified and not inside a class\n");
12349          }
12350          break;
12351       }
12352    }
12353 }
12354
12355 static void ProcessFunction(FunctionDefinition function)
12356 {
12357    Identifier id = GetDeclId(function.declarator);
12358    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12359    Type type = symbol ? symbol.type : null;
12360    Class oldThisClass = thisClass;
12361    Context oldTopContext = topContext;
12362
12363    yylloc = function.loc;
12364    // Process thisClass
12365
12366    if(type && type.thisClass)
12367    {
12368       Symbol classSym = type.thisClass;
12369       Class _class = type.thisClass.registered;
12370       char className[1024];
12371       char structName[1024];
12372       Declarator funcDecl;
12373       Symbol thisSymbol;
12374
12375       bool typedObject = false;
12376
12377       if(_class && !_class.base)
12378       {
12379          _class = currentClass;
12380          if(_class && !_class.symbol)
12381             _class.symbol = FindClass(_class.fullName);
12382          classSym = _class ? _class.symbol : null;
12383          typedObject = true;
12384       }
12385
12386       thisClass = _class;
12387
12388       if(inCompiler && _class)
12389       {
12390          if(type.kind == functionType)
12391          {
12392             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12393             {
12394                //TypeName param = symbol.type.params.first;
12395                Type param = symbol.type.params.first;
12396                symbol.type.params.Remove(param);
12397                //FreeTypeName(param);
12398                FreeType(param);
12399             }
12400             if(type.classObjectType != classPointer)
12401             {
12402                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12403                symbol.type.staticMethod = true;
12404                symbol.type.thisClass = null;
12405
12406                // HIGH DANGER: VERIFYING THIS...
12407                symbol.type.extraParam = false;
12408             }
12409          }
12410
12411          strcpy(className, "__ecereClass_");
12412          FullClassNameCat(className, _class.fullName, true);
12413
12414          MangleClassName(className);
12415
12416          structName[0] = 0;
12417          FullClassNameCat(structName, _class.fullName, false);
12418
12419          // [class] this
12420
12421
12422          funcDecl = GetFuncDecl(function.declarator);
12423          if(funcDecl)
12424          {
12425             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12426             {
12427                TypeName param = funcDecl.function.parameters->first;
12428                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12429                {
12430                   funcDecl.function.parameters->Remove(param);
12431                   FreeTypeName(param);
12432                }
12433             }
12434
12435             // DANGER: Watch for this... Check if it's a Conversion?
12436             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12437
12438             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12439             if(!function.propertyNoThis)
12440             {
12441                TypeName thisParam;
12442
12443                if(type.classObjectType != classPointer)
12444                {
12445                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12446                   if(!funcDecl.function.parameters)
12447                      funcDecl.function.parameters = MkList();
12448                   funcDecl.function.parameters->Insert(null, thisParam);
12449                }
12450
12451                if(typedObject)
12452                {
12453                   if(type.classObjectType != classPointer)
12454                   {
12455                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12456                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12457                   }
12458
12459                   thisParam = TypeName
12460                   {
12461                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12462                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12463                   };
12464                   funcDecl.function.parameters->Insert(null, thisParam);
12465                }
12466             }
12467          }
12468
12469          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12470          {
12471             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12472             funcDecl = GetFuncDecl(initDecl.declarator);
12473             if(funcDecl)
12474             {
12475                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12476                {
12477                   TypeName param = funcDecl.function.parameters->first;
12478                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12479                   {
12480                      funcDecl.function.parameters->Remove(param);
12481                      FreeTypeName(param);
12482                   }
12483                }
12484
12485                if(type.classObjectType != classPointer)
12486                {
12487                   // DANGER: Watch for this... Check if it's a Conversion?
12488                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12489                   {
12490                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12491
12492                      if(!funcDecl.function.parameters)
12493                         funcDecl.function.parameters = MkList();
12494                      funcDecl.function.parameters->Insert(null, thisParam);
12495                   }
12496                }
12497             }
12498          }
12499       }
12500
12501       // Add this to the context
12502       if(function.body)
12503       {
12504          if(type.classObjectType != classPointer)
12505          {
12506             thisSymbol = Symbol
12507             {
12508                string = CopyString("this");
12509                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12510             };
12511             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12512
12513             if(typedObject && thisSymbol.type)
12514             {
12515                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12516                thisSymbol.type.byReference = type.byReference;
12517                thisSymbol.type.typedByReference = type.byReference;
12518                /*
12519                thisSymbol = Symbol { string = CopyString("class") };
12520                function.body.compound.context.symbols.Add(thisSymbol);
12521                */
12522             }
12523          }
12524       }
12525
12526       // Pointer to class data
12527
12528       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12529       {
12530          DataMember member = null;
12531          {
12532             Class base;
12533             for(base = _class; base && base.type != systemClass; base = base.next)
12534             {
12535                for(member = base.membersAndProperties.first; member; member = member.next)
12536                   if(!member.isProperty)
12537                      break;
12538                if(member)
12539                   break;
12540             }
12541          }
12542          for(member = _class.membersAndProperties.first; member; member = member.next)
12543             if(!member.isProperty)
12544                break;
12545          if(member)
12546          {
12547             char pointerName[1024];
12548
12549             Declaration decl;
12550             Initializer initializer;
12551             Expression exp, bytePtr;
12552
12553             strcpy(pointerName, "__ecerePointer_");
12554             FullClassNameCat(pointerName, _class.fullName, false);
12555             {
12556                char className[1024];
12557                strcpy(className, "__ecereClass_");
12558                FullClassNameCat(className, classSym.string, true);
12559                MangleClassName(className);
12560
12561                // Testing This
12562                DeclareClass(classSym, className);
12563             }
12564
12565             // ((byte *) this)
12566             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12567
12568             if(_class.fixed)
12569             {
12570                char string[256];
12571                sprintf(string, "%d", _class.offset);
12572                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12573             }
12574             else
12575             {
12576                // ([bytePtr] + [className]->offset)
12577                exp = QBrackets(MkExpOp(bytePtr, '+',
12578                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12579             }
12580
12581             // (this ? [exp] : 0)
12582             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12583             exp.expType = Type
12584             {
12585                refCount = 1;
12586                kind = pointerType;
12587                type = Type { refCount = 1, kind = voidType };
12588             };
12589
12590             if(function.body)
12591             {
12592                yylloc = function.body.loc;
12593                // ([structName] *) [exp]
12594                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12595                initializer = MkInitializerAssignment(
12596                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12597
12598                // [structName] * [pointerName] = [initializer];
12599                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12600
12601                {
12602                   Context prevContext = curContext;
12603                   curContext = function.body.compound.context;
12604
12605                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12606                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12607
12608                   curContext = prevContext;
12609                }
12610
12611                // WHY?
12612                decl.symbol = null;
12613
12614                if(!function.body.compound.declarations)
12615                   function.body.compound.declarations = MkList();
12616                function.body.compound.declarations->Insert(null, decl);
12617             }
12618          }
12619       }
12620
12621
12622       // Loop through the function and replace undeclared identifiers
12623       // which are a member of the class (methods, properties or data)
12624       // by "this.[member]"
12625    }
12626    else
12627       thisClass = null;
12628
12629    if(id)
12630    {
12631       FreeSpecifier(id._class);
12632       id._class = null;
12633
12634       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12635       {
12636          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12637          id = GetDeclId(initDecl.declarator);
12638
12639          FreeSpecifier(id._class);
12640          id._class = null;
12641       }
12642    }
12643    if(function.body)
12644       topContext = function.body.compound.context;
12645    {
12646       FunctionDefinition oldFunction = curFunction;
12647       curFunction = function;
12648       if(function.body)
12649          ProcessStatement(function.body);
12650
12651       // If this is a property set and no firewatchers has been done yet, add one here
12652       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12653       {
12654          Statement prevCompound = curCompound;
12655          Context prevContext = curContext;
12656
12657          Statement fireWatchers = MkFireWatchersStmt(null, null);
12658          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12659          ListAdd(function.body.compound.statements, fireWatchers);
12660
12661          curCompound = function.body;
12662          curContext = function.body.compound.context;
12663
12664          ProcessStatement(fireWatchers);
12665
12666          curContext = prevContext;
12667          curCompound = prevCompound;
12668
12669       }
12670
12671       curFunction = oldFunction;
12672    }
12673
12674    if(function.declarator)
12675    {
12676       ProcessDeclarator(function.declarator);
12677    }
12678
12679    topContext = oldTopContext;
12680    thisClass = oldThisClass;
12681 }
12682
12683 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12684 static void ProcessClass(OldList definitions, Symbol symbol)
12685 {
12686    ClassDef def;
12687    External external = curExternal;
12688    Class regClass = symbol ? symbol.registered : null;
12689
12690    // Process all functions
12691    for(def = definitions.first; def; def = def.next)
12692    {
12693       if(def.type == functionClassDef)
12694       {
12695          if(def.function.declarator)
12696             curExternal = def.function.declarator.symbol.pointerExternal;
12697          else
12698             curExternal = external;
12699
12700          ProcessFunction((FunctionDefinition)def.function);
12701       }
12702       else if(def.type == declarationClassDef)
12703       {
12704          if(def.decl.type == instDeclaration)
12705          {
12706             thisClass = regClass;
12707             ProcessInstantiationType(def.decl.inst);
12708             thisClass = null;
12709          }
12710          // Testing this
12711          else
12712          {
12713             Class backThisClass = thisClass;
12714             if(regClass) thisClass = regClass;
12715             ProcessDeclaration(def.decl);
12716             thisClass = backThisClass;
12717          }
12718       }
12719       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12720       {
12721          MemberInit defProperty;
12722
12723          // Add this to the context
12724          Symbol thisSymbol = Symbol
12725          {
12726             string = CopyString("this");
12727             type = regClass ? MkClassType(regClass.fullName) : null;
12728          };
12729          globalContext.symbols.Add((BTNode)thisSymbol);
12730
12731          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12732          {
12733             thisClass = regClass;
12734             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12735             thisClass = null;
12736          }
12737
12738          globalContext.symbols.Remove((BTNode)thisSymbol);
12739          FreeSymbol(thisSymbol);
12740       }
12741       else if(def.type == propertyClassDef && def.propertyDef)
12742       {
12743          PropertyDef prop = def.propertyDef;
12744
12745          // Add this to the context
12746          /*
12747          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12748          globalContext.symbols.Add(thisSymbol);
12749          */
12750
12751          thisClass = regClass;
12752          if(prop.setStmt)
12753          {
12754             if(regClass)
12755             {
12756                Symbol thisSymbol
12757                {
12758                   string = CopyString("this");
12759                   type = MkClassType(regClass.fullName);
12760                };
12761                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12762             }
12763
12764             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12765             ProcessStatement(prop.setStmt);
12766          }
12767          if(prop.getStmt)
12768          {
12769             if(regClass)
12770             {
12771                Symbol thisSymbol
12772                {
12773                   string = CopyString("this");
12774                   type = MkClassType(regClass.fullName);
12775                };
12776                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12777             }
12778
12779             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12780             ProcessStatement(prop.getStmt);
12781          }
12782          if(prop.issetStmt)
12783          {
12784             if(regClass)
12785             {
12786                Symbol thisSymbol
12787                {
12788                   string = CopyString("this");
12789                   type = MkClassType(regClass.fullName);
12790                };
12791                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12792             }
12793
12794             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12795             ProcessStatement(prop.issetStmt);
12796          }
12797
12798          thisClass = null;
12799
12800          /*
12801          globalContext.symbols.Remove(thisSymbol);
12802          FreeSymbol(thisSymbol);
12803          */
12804       }
12805       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12806       {
12807          PropertyWatch propertyWatch = def.propertyWatch;
12808
12809          thisClass = regClass;
12810          if(propertyWatch.compound)
12811          {
12812             Symbol thisSymbol
12813             {
12814                string = CopyString("this");
12815                type = regClass ? MkClassType(regClass.fullName) : null;
12816             };
12817
12818             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12819
12820             curExternal = null;
12821             ProcessStatement(propertyWatch.compound);
12822          }
12823          thisClass = null;
12824       }
12825    }
12826 }
12827
12828 void DeclareFunctionUtil(String s)
12829 {
12830    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12831    if(function)
12832    {
12833       char name[1024];
12834       name[0] = 0;
12835       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12836          strcpy(name, "__ecereFunction_");
12837       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12838       DeclareFunction(function, name);
12839    }
12840 }
12841
12842 void ComputeDataTypes()
12843 {
12844    External external;
12845    External temp { };
12846    External after = null;
12847
12848    currentClass = null;
12849
12850    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12851
12852    for(external = ast->first; external; external = external.next)
12853    {
12854       if(external.type == declarationExternal)
12855       {
12856          Declaration decl = external.declaration;
12857          if(decl)
12858          {
12859             OldList * decls = decl.declarators;
12860             if(decls)
12861             {
12862                InitDeclarator initDecl = decls->first;
12863                if(initDecl)
12864                {
12865                   Declarator declarator = initDecl.declarator;
12866                   if(declarator && declarator.type == identifierDeclarator)
12867                   {
12868                      Identifier id = declarator.identifier;
12869                      if(id && id.string)
12870                      {
12871                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12872                         {
12873                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12874                            after = external;
12875                         }
12876                      }
12877                   }
12878                }
12879             }
12880          }
12881        }
12882    }
12883
12884    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12885    ast->Insert(after, temp);
12886    curExternal = temp;
12887
12888    DeclareFunctionUtil("eSystem_New");
12889    DeclareFunctionUtil("eSystem_New0");
12890    DeclareFunctionUtil("eSystem_Renew");
12891    DeclareFunctionUtil("eSystem_Renew0");
12892    DeclareFunctionUtil("eSystem_Delete");
12893    DeclareFunctionUtil("eClass_GetProperty");
12894    DeclareFunctionUtil("eClass_SetProperty");
12895    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12896    DeclareFunctionUtil("eInstance_SetMethod");
12897    DeclareFunctionUtil("eInstance_IncRef");
12898    DeclareFunctionUtil("eInstance_StopWatching");
12899    DeclareFunctionUtil("eInstance_Watch");
12900    DeclareFunctionUtil("eInstance_FireWatchers");
12901
12902    DeclareStruct("ecere::com::Class", false);
12903    DeclareStruct("ecere::com::Instance", false);
12904    DeclareStruct("ecere::com::Property", false);
12905    DeclareStruct("ecere::com::DataMember", false);
12906    DeclareStruct("ecere::com::Method", false);
12907    DeclareStruct("ecere::com::SerialBuffer", false);
12908    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12909
12910    ast->Remove(temp);
12911
12912    for(external = ast->first; external; external = external.next)
12913    {
12914       afterExternal = curExternal = external;
12915       if(external.type == functionExternal)
12916       {
12917          currentClass = external.function._class;
12918          ProcessFunction(external.function);
12919       }
12920       // There shouldn't be any _class member access here anyways...
12921       else if(external.type == declarationExternal)
12922       {
12923          currentClass = null;
12924          if(external.declaration)
12925             ProcessDeclaration(external.declaration);
12926       }
12927       else if(external.type == classExternal)
12928       {
12929          ClassDefinition _class = external._class;
12930          currentClass = external.symbol.registered;
12931          if(_class.definitions)
12932          {
12933             ProcessClass(_class.definitions, _class.symbol);
12934          }
12935          if(inCompiler)
12936          {
12937             // Free class data...
12938             ast->Remove(external);
12939             delete external;
12940          }
12941       }
12942       else if(external.type == nameSpaceExternal)
12943       {
12944          thisNameSpace = external.id.string;
12945       }
12946    }
12947    currentClass = null;
12948    thisNameSpace = null;
12949    curExternal = null;
12950
12951    delete temp.symbol;
12952    delete temp;
12953 }